import { afterEach, describe, expect, it } from "vitest"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { compositionInputSignature, compositionsAffectedBy } from "./compositionInputs"; const dirs: string[] = []; afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function project(files: Record): string { const dir = mkdtempSync(join(tmpdir(), "hf-composition-inputs-")); dirs.push(dir); for (const [path, content] of Object.entries(files)) { mkdirSync(join(dir, path, ".."), { recursive: true }); writeFileSync(join(dir, path), content); } return dir; } const mount = (src: string) => `
`; describe("compositionsAffectedBy", () => { const dir = () => project({ "index.html": mount("compositions/a.html") + mount("./compositions/b.html"), "compositions/a.html": ``, "compositions/b.html": "", "compositions/nested.html": "", "compositions/unmounted.html": "", "assets/logo.svg": "", }); it("names a scene and the compositions that mount it, not its siblings", () => { expect(compositionsAffectedBy(dir(), "compositions/b.html")).toEqual([ "index.html", "compositions/b.html", ]); expect(compositionsAffectedBy(dir(), "compositions/nested.html")).toEqual([ "index.html", "compositions/a.html", "compositions/nested.html", ]); }); it("reaches every composition from an asset or a file the root does not mount", () => { expect(compositionsAffectedBy(dir(), "assets/logo.svg")).toBeNull(); expect(compositionsAffectedBy(dir(), "compositions/unmounted.html")).toBeNull(); }); it("reads watcher paths with either separator", () => { expect(compositionsAffectedBy(dir(), "compositions\\b.html")).toEqual([ "index.html", "compositions/b.html", ]); }); }); describe("a root write", () => { const html = (head: string, body: string) => `${head}${body}${mount("compositions/a.html")}`; const files = (head: string, body: string) => ({ "index.html": html(head, body), "compositions/a.html": "", }); const rewrite = (dir: string, head: string, body: string) => writeFileSync(join(dir, "index.html"), html(head, body)); it("moves every composition when the head changed, or when no head was seen yet", async () => { const dir = project(files("", "x")); expect(compositionsAffectedBy(dir, "index.html")).toBeNull(); await compositionInputSignature(dir, "compositions/a.html", "s1"); rewrite(dir, "", "x"); expect(compositionsAffectedBy(dir, "index.html")).toBeNull(); }); it("moves only the root when the head is as it was", async () => { const dir = project(files("", "x")); await compositionInputSignature(dir, "compositions/a.html", "s1"); rewrite(dir, "", "moved"); expect(compositionsAffectedBy(dir, "index.html")).toEqual(["index.html"]); rewrite(dir, "", "moved again"); expect(compositionsAffectedBy(dir, "index.html")).toEqual(["index.html"]); }); it("tells every subscriber of one head edit the same thing, even after a thumbnail request", async () => { const dir = project(files("", "x")); await compositionInputSignature(dir, "compositions/a.html", "s1"); rewrite(dir, "", "x"); expect(compositionsAffectedBy(dir, "index.html")).toBeNull(); await compositionInputSignature(dir, "compositions/a.html", "s2"); expect(compositionsAffectedBy(dir, "index.html")).toBeNull(); }); it("leaves a scene's input signature alone for a body edit, not for a head edit", async () => { const dir = project(files("", "x")); const before = await compositionInputSignature(dir, "compositions/a.html", "s1"); rewrite(dir, "", "moved"); expect(await compositionInputSignature(dir, "compositions/a.html", "s2")).toBe(before); rewrite(dir, "", "moved"); expect(await compositionInputSignature(dir, "compositions/a.html", "s3")).not.toBe(before); }); it("changes the root's own input signature for a body edit", async () => { const dir = project(files("", "x")); const before = await compositionInputSignature(dir, "index.html", "s1"); rewrite(dir, "", "moved"); expect(await compositionInputSignature(dir, "index.html", "s2")).not.toBe(before); }); });