diff --git a/apps/mobile/preview/gallery.test.mjs b/apps/mobile/preview/gallery.test.mjs new file mode 100644 index 0000000..bcd04c5 --- /dev/null +++ b/apps/mobile/preview/gallery.test.mjs @@ -0,0 +1,110 @@ +// #268: out/index.html, the page writeIndex writes after a run of the preview harness, checked without a browser and +// without a build. Each test calls it with made-up shots in a fresh temp folder (removed at the end), reads the file +// back and asserts on the HTML. gallery.mjs itself is untouched. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { writeIndex } from "./gallery.mjs"; + +const sections = (html) => html.match(/
[\s\S]*?<\/section>/g) ?? []; +const figures = (html) => html.match(/
[\s\S]*?<\/figure>/g) ?? []; + +/** Writes the page in its own throwaway folder and returns the HTML. `mockup` also creates mockups/ next to the index. */ +const run = (shots, mockup) => { + const dir = mkdtempSync(join(tmpdir(), "hermit-gallery-")); + try { + if (mockup) { + mkdirSync(join(dir, "mockups")); + writeFileSync(join(dir, "mockups", mockup), ""); + } + writeIndex(join(dir, "index.html"), shots); + return readFileSync(join(dir, "index.html"), "utf8"); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}; + +/** A made-up shot with the fields writeIndex reads; nothing real in any of them. */ +const shot = (over = {}) => ({ + id: "HM-CHAT-01", + name: "Hermit chat", + route: "chat", + variant: "", + theme: "light", + lang: "es-419", + file: "chat-light-es-419.png", + errors: [], + ...over, +}); + +test("shots with the same id, name and variant share one section; a different variant gets its own", () => { + const html = run([ + shot({ file: "chat-light-es-419.png" }), + shot({ theme: "dark", file: "chat-dark-es-419.png" }), + shot({ variant: "empty", theme: "dark", file: "chat-empty-dark.png" }), + ]); + assert.equal(sections(html).length, 2); + const [base, empty] = sections(html); + // Both shots of the base state sit under one heading, one figure each. + assert.equal(figures(base).length, 2); + assert.ok(base.includes("

HM-CHAT-01 Hermit chat

")); + assert.ok(base.includes("src=\"screens/chat-light-es-419.png\"")); + assert.ok(base.includes("src=\"screens/chat-dark-es-419.png\"")); + // The empty state keeps the same id and name but is its own section, with the state named. + assert.equal(figures(empty).length, 1); + assert.ok(empty.includes("empty")); + assert.ok(!base.includes("empty")); +}); + +test("a name or a note with <, >, & and \" comes out escaped, and no raw > "z"`, + note: `A & B > "z"`, + }), + ]); + const escaped = "A & B <script>alert("gotcha")</script> > "z""; + assert.ok(!html.includes("