-- SPDX-FileCopyrightText: © 2022—2026 Vladimir Zorin -- SPDX-License-Identifier: LicenseRef-OWL-1.0-or-later -- Licensed under OWL v1.0+. See LICENSE. local testimony = require("testimony") local render = require("lilgit.render") local testify = testimony.new("== lilgit.render ==") testify:that("html_escape: escapes all five special characters", function() testimony.assert_equal("&<>"'", render.html_escape("&<>\"'")) end) testify:that("html_escape: does not double-escape ampersands in entities", function() testimony.assert_equal("&amp;", render.html_escape("&")) end) testify:that("href: escapes each segment, keeps ref slashes literal", function() testimony.assert_equal( "/repo/tree/feature/foo/a%20b/c/", render.href("repo", "tree", "feature/foo", { "a b", "c" }, true) ) testimony.assert_equal("/repo/blob/main/file.md", render.href("repo", "blob", "main", { "file.md" }, false)) end) testify:that("href: nested repo name keeps its slash literal", function() testimony.assert_equal("/cat1/repo1/tree/main/", render.href("cat1/repo1", "tree", "main", nil, true)) testimony.assert_equal("/c%20t/r%20p/blob/main/f.md", render.href("c t/r p", "blob", "main", { "f.md" }, false)) end) testify:that("repo_prefix: escapes per segment", function() testimony.assert_equal("/repo", render.repo_prefix("repo")) testimony.assert_equal("/cat1/repo1", render.repo_prefix("cat1/repo1")) testimony.assert_equal("/a%20b/c%3Fd", render.repo_prefix("a b/c?d")) end) testify:that("markdown: extracts the first h1 as title and removes it from the body", function() local html, title = render.markdown("# Hello *World*\n\nBody text.\n", "file.md") testimony.assert_equal("Hello World", title) testimony.assert_nil(html:match("', html) testimony.assert_match('gap', html) end) testify:that("assets css styles the markdown extensions", function() local assets = require("lilgit.assets") testimony.assert_match("div%.warning", assets.css) testimony.assert_match("div%.caution", assets.css) testimony.assert_match("div%.function", assets.css) testimony.assert_match("code%.str", assets.css) testimony.assert_match("code%.or_nil::after", assets.css) testimony.assert_match("task%-item", assets.css) end) testify:that("build_templates: user overrides win over defaults", function() local templates = render.build_templates({ templates = { page = "${body}" } }) testimony.assert_equal("${body}", templates.page) testimony.assert_not_nil(render.build_templates({}).page) end) testify:that("tree_page: fills the shell, escapes names, links entries", function() local state = { cfg = { site_title = "test site" }, templates = render.build_templates({}), } local repo = { name = "repo", default_branch = "main" } local listing = { { name = "sub", sha = string.rep("a", 40), kind = "tree", mode = "40000" }, { name = "ab", html) testimony.assert_match('class="size">2%.0K', html) end) testify:that("ref switcher lists branches and tags, marks the current ref", function() local state = { cfg = { site_title = "site" }, templates = render.build_templates({}), } local repo = { name = "repo", default_branch = "main" } local html = render.tree_page(state, { repo = repo, ref = "feature/foo", path_segs = {}, listing = {}, branches = { { name = "feature/foo", sha = "x" }, { name = "main", sha = "y" } }, tags = { { name = "v1.0", sha = "z", target = "z" } }, }) testimony.assert_match("