|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | +import { spawnSync } from "node:child_process"; |
| 4 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | +import { join } from "node:path"; |
| 7 | +import { fileURLToPath } from "node:url"; |
| 8 | +import { assertDetailPreview } from "./docs-catalog-shared.mjs"; |
| 9 | + |
| 10 | +const source = "/public/catalog/blocks/character.json"; |
| 11 | +const payload = { html: "<html><body>character</body></html>" }; |
| 12 | + |
| 13 | +test("a playable payload must be wired into its detail page", () => { |
| 14 | + assert.throws( |
| 15 | + () => assertDetailPreview('<CatalogDetail\n video="character.mp4"\n>', payload, source), |
| 16 | + /detail previewSrc must match/, |
| 17 | + ); |
| 18 | +}); |
| 19 | + |
| 20 | +test("a detail page cannot mount another item's payload", () => { |
| 21 | + assert.throws( |
| 22 | + () => assertDetailPreview('<CatalogDetail\n previewSrc="/other.json"\n>', payload, source), |
| 23 | + /detail previewSrc must match/, |
| 24 | + ); |
| 25 | +}); |
| 26 | + |
| 27 | +test("a recorded clip cannot preempt a playable non-WebGPU composition", () => { |
| 28 | + assert.throws( |
| 29 | + () => |
| 30 | + assertDetailPreview( |
| 31 | + `<CatalogDetail\n previewSrc="${source}"\n video="character.mp4"\n>`, |
| 32 | + payload, |
| 33 | + source, |
| 34 | + ), |
| 35 | + /recorded video hides/, |
| 36 | + ); |
| 37 | +}); |
| 38 | + |
| 39 | +test("a matching playable preview is accepted", () => { |
| 40 | + assertDetailPreview(`<CatalogDetail\n previewSrc="${source}"\n>`, payload, source); |
| 41 | +}); |
| 42 | + |
| 43 | +test("a WebGPU preview can keep its recorded fallback", () => { |
| 44 | + assertDetailPreview( |
| 45 | + `<CatalogDetail\n previewSrc="${source}"\n video="character.mp4"\n webgpu\n>`, |
| 46 | + { html: "navigator.gpu.requestAdapter()" }, |
| 47 | + source, |
| 48 | + ); |
| 49 | +}); |
| 50 | + |
| 51 | +test("a WebGPU recorded fallback must enable the adapter check", () => { |
| 52 | + assert.throws( |
| 53 | + () => |
| 54 | + assertDetailPreview( |
| 55 | + `<CatalogDetail\n previewSrc="${source}"\n video="character.mp4"\n>`, |
| 56 | + { html: "navigator.gpu.requestAdapter()" }, |
| 57 | + source, |
| 58 | + ), |
| 59 | + /recorded video hides/, |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +test("an unsupported preview keeps its recorded clip without mounting a payload", () => { |
| 64 | + assertDetailPreview( |
| 65 | + '<CatalogDetail\n video="character.mp4"\n>', |
| 66 | + { unsupported: "canvas-draw-element" }, |
| 67 | + source, |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +test("a page cannot mount a missing payload", () => { |
| 72 | + assert.throws( |
| 73 | + () => assertDetailPreview(`<CatalogDetail\n previewSrc="${source}"\n>`, {}, source), |
| 74 | + /detail previewSrc must match/, |
| 75 | + ); |
| 76 | +}); |
| 77 | + |
| 78 | +test("the CI command rejects a stale published page and accepts its repaired wiring", (t) => { |
| 79 | + const docs = mkdtempSync(join(tmpdir(), "catalog-preview-check-")); |
| 80 | + t.after(() => rmSync(docs, { recursive: true, force: true })); |
| 81 | + for (const directory of ["catalog/blocks", "catalog/components", "public/catalog/blocks"]) { |
| 82 | + mkdirSync(join(docs, directory), { recursive: true }); |
| 83 | + } |
| 84 | + const page = join(docs, "catalog/blocks/character.mdx"); |
| 85 | + writeFileSync(join(docs, source.slice(1)), JSON.stringify(payload)); |
| 86 | + writeFileSync(page, '<CatalogDetail\n video="character.mp4"\n>'); |
| 87 | + const check = () => |
| 88 | + spawnSync( |
| 89 | + process.execPath, |
| 90 | + [fileURLToPath(new URL("./check-catalog-detail-previews.mjs", import.meta.url)), docs], |
| 91 | + { encoding: "utf8" }, |
| 92 | + ); |
| 93 | + const stale = check(); |
| 94 | + assert.equal(stale.status, 1); |
| 95 | + assert.match(stale.stderr, /detail previewSrc must match/); |
| 96 | + writeFileSync(page, `<CatalogDetail\n previewSrc="${source}"\n>`); |
| 97 | + const repaired = check(); |
| 98 | + assert.equal(repaired.status, 0, repaired.stderr); |
| 99 | + assert.match(repaired.stdout, /PASS 1 catalog detail previews/); |
| 100 | +}); |
0 commit comments