src/image-render.test.lua

77 lines

1-- Tests for image-render.lua (Issue 9-013 renderer side).
2-- Run: luajit src/image-render.test.lua
3package.path = "./src/?.lua;./libs/?.lua;" .. package.path
4local R = require("image-render")
5
6local passed, failed = 0, 0
7local function check(name, cond)
8 if cond then passed = passed + 1 else failed = failed + 1; print(" FAIL: " .. name) end
9end
10
11-- A poems_data that already contains a class-2 image-only post (poem_index 4).
12local poems = { poems = {
13 { poem_index = 1, id = 1, content = "real poem" },
14 { poem_index = 4, id = 4, content = "🖼", category = "fediverse",
15 attachments = {{ relative_path = "files/1/2/y.png", media_type = "image/png" }} },
16}}
17
18local manifest = {
19 ["4"] = { class = 2, poem_index = 4, category = "fediverse" },
20 ["5"] = { class = 3, poem_index = 5, source_name = "my-art",
21 display_title = "my-art: factory-cube.png",
22 relative_path = "/p/input/images/my-art/factory-cube.png",
23 gallery_anchor = "img-abc123",
24 width = 100, height = 80, creation_date = "2024-01-02T00:00:00Z" },
25}
26
27R.inject_pseudo_poems(poems, manifest)
28
29-- Class 2: existing post tagged in place, NOT duplicated.
30local p4
31for _, p in ipairs(poems.poems) do if p.poem_index == 4 then p4 = p end end
32check("class-2 post tagged is_image in place", p4 and p4.is_image == true and p4.image_class == 2)
33check("class-2 keeps its original attachments", p4 and p4.attachments[1].relative_path == "files/1/2/y.png")
34
35-- Class 3: appended as a new pseudo-poem with a usable attachment.
36local p5
37for _, p in ipairs(poems.poems) do if p.poem_index == 5 then p5 = p end end
38check("class-3 pseudo-poem appended", p5 and p5.is_image == true and p5.image_class == 3)
39-- Class-3 now keeps the FULL relative_path (not just the basename) so the
40-- renderer can namespace art by source+subdir -- the bare-filename form was what
41-- let same-named art images collide in output/media/.
42check("class-3 attachment keeps the full relative_path", p5 and p5.attachments[1].relative_path == "/p/input/images/my-art/factory-cube.png")
43check("class-3 attachment media type from ext", p5 and p5.attachments[1].media_type == "image/png")
44check("class-3 display title carried", p5 and p5.display_title == "my-art: factory-cube.png")
45check("total poems is 3 (1 + tagged 4 + appended 5)", #poems.poems == 3)
46
47-- Idempotency: a second inject must not append again.
48R.inject_pseudo_poems(poems, manifest)
49check("idempotent: no duplicate append", #poems.poems == 3)
50
51-- format_image_entry: self-contained image box built from the injected attachment.
52local html = R.format_image_entry(p5)
53check("format includes the title", html:find("my%-art: factory%-cube%.png", 1) ~= nil)
54-- art src is namespaced by source: output/media/my-art/factory-cube.png, NOT a
55-- flat output/media/factory-cube.png (which would collide with any other source's
56-- factory-cube.png). Mastodon hashes still flatten (see text_image_link below).
57check("format namespaces art src under its source (relative)", html:find("%.%./media/my%-art/factory%-cube%.png", 1) ~= nil)
58check("format carries width/height hints", html:find('width="100"') ~= nil and html:find('height="80"') ~= nil)
59check("format is css-free (no class= attrs)", html:find('class=') == nil)
60check("class-3 pseudo-poem carries gallery_anchor", p5.gallery_anchor == "img-abc123")
61check("title deep-links to the chronological gallery anchor",
62 html:find("gallery/chronological%.html#img%-abc123", 1) ~= nil)
63-- An image-only post (no gallery_anchor) keeps a plain title (no gallery link).
64local html2 = R.format_image_entry({ display_title = "image post", attachments = {} })
65check("image-only title is not gallery-linked", html2:find("chronological%.html", 1) == nil)
66
67-- text_image_link: "image.png" link for a text+image post; "" for pure text.
68do
69 local link = R.text_image_link({ attachments = {{ relative_path = "files/9/8/photo.jpg", media_type = "image/jpeg" }} })
70 check("text+image link labelled image.png", link:find(">image%.png</a>", 1) ~= nil)
71 check("text+image link targets the media file (relative)", link:find("%.%./media/photo%.jpg", 1) ~= nil)
72 check("pure-text poem gets no link", R.text_image_link({ content = "just words" }) == "")
73end
74
75print(string.format("\nimage-render: %d passed, %d failed", passed, failed))
76os.exit(failed == 0 and 0 or 1)
77