import { afterEach, describe, expect, it } from "vitest"; import { Hono } from "hono"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { parseHTML } from "linkedom"; import { resolveSubCompositionContent, type ParsableDocumentLike, } from "@hyperframes/parsers/sub-composition-validity"; import { registerFileRoutes } from "./files"; import type { StudioApiAdapter } from "../types"; const tempDirs: string[] = []; afterEach(() => { for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); const parse = (html: string) => parseHTML(html).document as unknown as ParsableDocumentLike; const SCENE_WITH_LOCAL_MOUNT = `
`; function createTwoFileProject(indexHtml: string, sceneHtml = SCENE_WITH_LOCAL_MOUNT) { const dir = mkdtempSync(join(tmpdir(), "hf-cross-file-root-")); tempDirs.push(dir); mkdirSync(join(dir, "compositions"), { recursive: true }); writeFileSync(join(dir, "index.html"), indexHtml); writeFileSync(join(dir, "compositions", "a.html"), sceneHtml); const adapter: StudioApiAdapter = { listProjects: () => [], resolveProject: async (id) => ({ id, dir }), bundle: async () => null, lint: async () => ({ findings: [] }), runtimeUrl: "/api/runtime.js", rendersDir: () => join(dir, "renders"), startRender: () => ({ id: "job", status: "rendering", progress: 0, outputPath: "out.mp4" }), }; const app = new Hono(); registerFileRoutes(app, adapter); const removeElements = (filePath: string, ids: string[]) => app.request(`http://localhost/projects/demo/file-mutations/remove-elements/${filePath}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ targets: ids.map((id) => ({ id })) }), }); const removeElement = (filePath: string, id: string) => app.request(`http://localhost/projects/demo/file-mutations/remove-element/${filePath}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ target: { id } }), }); return { dir, removeElements, removeElement }; } describe("cross-file template reachability on delete", () => { // fails without the fix: deleting #nested removed the scene template referenced by index.html. it("refuses deleting a wrapper holding a template and local mount when another file references this file", async () => { const indexHtml = `
`; const { dir, removeElements, removeElement } = createTwoFileProject(indexHtml); await removeElements("compositions/a.html", ["nested"]); const afterBatch = readFileSync(join(dir, "compositions", "a.html"), "utf-8"); expect(afterBatch).toBe(SCENE_WITH_LOCAL_MOUNT); expect(resolveSubCompositionContent(afterBatch, parse(afterBatch)).contentHtml).toContain( 'data-composition-id="child"', ); await removeElement("compositions/a.html", "nested"); expect(readFileSync(join(dir, "compositions", "a.html"), "utf-8")).toBe(SCENE_WITH_LOCAL_MOUNT); }); it("still removes the wrapper when no other file references the file or its template", async () => { const indexHtml = `

Standalone

`; const { dir, removeElements } = createTwoFileProject(indexHtml); await removeElements("compositions/a.html", ["nested"]); const after = readFileSync(join(dir, "compositions", "a.html"), "utf-8"); expect(after).not.toContain('id="nested"'); expect(after).toContain('data-composition-id="a"'); }); // fails without the fix: nested project-root-relative data-composition-src was resolved against compositions/. it("refuses deleting a wrapper referenced via a nested project-root-relative path in a 3-file project", async () => { const indexHtml = `
`; const middleHtml = `
`; const { dir, removeElements } = createTwoFileProject(indexHtml); writeFileSync(join(dir, "compositions", "middle.html"), middleHtml); await removeElements("compositions/a.html", ["nested"]); expect(readFileSync(join(dir, "compositions", "a.html"), "utf-8")).toBe(SCENE_WITH_LOCAL_MOUNT); }); // fails without the fix: an unrelated file carrying an empty element with the same id blocked a valid delete. it("does not let an unrelated file with the same composition id block deleting a local wrapper", async () => { const indexHtml = `

Standalone

`; const unrelatedHtml = `
`; const { dir, removeElements } = createTwoFileProject(indexHtml); writeFileSync(join(dir, "unrelated.html"), unrelatedHtml); await removeElements("compositions/a.html", ["nested"]); const after = readFileSync(join(dir, "compositions", "a.html"), "utf-8"); expect(after).not.toContain('id="nested"'); expect(after).toContain('data-composition-id="a"'); }); // fails without the fix: adding edges to both root and referrer-dir blocked deletes in unused compositions/a.html. it("allows deleting a removable wrapper in unused compositions/a.html when a mounted template scene selects root a.html", async () => { const indexHtml = `
`; const middleHtml = ``; const { dir, removeElements } = createTwoFileProject(indexHtml); writeFileSync(join(dir, "a.html"), SCENE_WITH_LOCAL_MOUNT); writeFileSync(join(dir, "compositions", "middle.html"), middleHtml); await removeElements("a.html", ["nested"]); expect(readFileSync(join(dir, "a.html"), "utf-8")).toBe(SCENE_WITH_LOCAL_MOUNT); await removeElements("compositions/a.html", ["nested"]); const unusedAfter = readFileSync(join(dir, "compositions", "a.html"), "utf-8"); expect(unusedAfter).not.toContain('id="nested"'); expect(unusedAfter).toContain('data-composition-id="a"'); }); // fails without the fix: root-first lookup missed directory-local a.html used by standalone compositions/parent.html. it("protects compositions/a.html when standalone nested compositions/parent.html mounts a.html beside a root a.html", async () => { const indexHtml = `

Home

`; const parentHtml = `
`; const { dir, removeElements } = createTwoFileProject(indexHtml); writeFileSync(join(dir, "a.html"), SCENE_WITH_LOCAL_MOUNT); writeFileSync(join(dir, "compositions", "parent.html"), parentHtml); await removeElements("compositions/a.html", ["nested"]); expect(readFileSync(join(dir, "compositions", "a.html"), "utf-8")).toBe(SCENE_WITH_LOCAL_MOUNT); }); });