|
| 1 | +import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { expect, it } from "vitest"; |
| 5 | +import { registerRenderOutput, readRenderOutputs } from "./renderOutputs.js"; |
| 6 | +import { listProjectFiles, affectsProjectSignature } from "./projectSignature.js"; |
| 7 | +import { openProjectHistory } from "../history/projectHistory.js"; |
| 8 | +import { beginArtifactCapture, recordRenderSuccess } from "../history/videoState.js"; |
| 9 | + |
| 10 | +it("keeps root render outputs out of revisions while tracking source video edits", async () => { |
| 11 | + const root = mkdtempSync(join(tmpdir(), "hf-render-history-")); |
| 12 | + const project = join(root, "project"); |
| 13 | + mkdirSync(project); |
| 14 | + writeFileSync(join(project, "index.html"), "initial"); |
| 15 | + writeFileSync(join(project, "input.mp4"), "source video"); |
| 16 | + const history = await openProjectHistory({ |
| 17 | + projectDir: project, |
| 18 | + historyRoot: join(root, "history"), |
| 19 | + }); |
| 20 | + const state = () => |
| 21 | + JSON.parse(readFileSync(join(project, ".hyperframes/video-state.json"), "utf8")); |
| 22 | + try { |
| 23 | + const window = await history.beginWindow({ kind: "agent", name: "test" }, "edit"); |
| 24 | + writeFileSync(join(project, "index.html"), "edited"); |
| 25 | + await history.checkpoint(); |
| 26 | + const revision = state().currentRevision; |
| 27 | + const output = join(project, "benefits.mp4"); |
| 28 | + await registerRenderOutput(project, output); |
| 29 | + const capture = await beginArtifactCapture(project); |
| 30 | + expect(capture.revision).toBe(revision); |
| 31 | + for (const bytes of ["first render", "second render"]) { |
| 32 | + writeFileSync(output, bytes); |
| 33 | + const render = await recordRenderSuccess(project, output, "completed", capture); |
| 34 | + expect(render.revision).toBe(revision); |
| 35 | + await history.checkpoint(); |
| 36 | + expect(state().currentRevision).toBe(revision); |
| 37 | + expect(state().pendingChanges).toBe(false); |
| 38 | + expect(state().files["benefits.mp4"]).toBeUndefined(); |
| 39 | + } |
| 40 | + expect(affectsProjectSignature(project, output)).toBe(false); |
| 41 | + expect(affectsProjectSignature(project, join(project, "input.mp4"))).toBe(true); |
| 42 | + writeFileSync(join(project, "input.mp4"), "updated source video"); |
| 43 | + await window.close(); |
| 44 | + expect(state().currentRevision).not.toBe(revision); |
| 45 | + expect(Object.keys(state().revisions[state().currentRevision].files)).toEqual(["input.mp4"]); |
| 46 | + const nextCapture = await beginArtifactCapture(project); |
| 47 | + writeFileSync(join(project, "index.html"), "changed during rendering"); |
| 48 | + writeFileSync(output, "third render"); |
| 49 | + expect( |
| 50 | + (await recordRenderSuccess(project, output, "completed", nextCapture)).revision, |
| 51 | + ).toBeNull(); |
| 52 | + } finally { |
| 53 | + await history.close(); |
| 54 | + rmSync(root, { recursive: true, force: true }); |
| 55 | + } |
| 56 | +}); |
| 57 | +it("registers concurrent custom outputs and directory renders without excluding other media", async () => { |
| 58 | + const root = mkdtempSync(join(tmpdir(), "hf-render-registry-")); |
| 59 | + try { |
| 60 | + await Promise.all([ |
| 61 | + registerRenderOutput(root, join(root, "a.webm")), |
| 62 | + registerRenderOutput(root, join(root, "hls")), |
| 63 | + ]); |
| 64 | + expect(readRenderOutputs(root)).toEqual(["a.webm", "hls"]); |
| 65 | + mkdirSync(join(root, "hls")); |
| 66 | + writeFileSync(join(root, "hls", "chunk.ts"), "output"); |
| 67 | + writeFileSync(join(root, "a.webm"), "output"); |
| 68 | + writeFileSync(join(root, "b.webm"), "input"); |
| 69 | + expect(listProjectFiles(root).map((file) => file.path)).toEqual(["b.webm"]); |
| 70 | + await registerRenderOutput(root, join(root, "..", "external.mp4")); |
| 71 | + expect(readRenderOutputs(root)).toEqual(["a.webm", "hls"]); |
| 72 | + await expect(registerRenderOutput(root, root)).rejects.toThrow("project directory"); |
| 73 | + } finally { |
| 74 | + rmSync(root, { recursive: true, force: true }); |
| 75 | + } |
| 76 | +}); |
| 77 | + |
| 78 | +it("lists project files when the render output registry is unreadable", () => { |
| 79 | + const project = mkdtempSync(join(tmpdir(), "hf-render-registry-")); |
| 80 | + try { |
| 81 | + writeFileSync(join(project, "index.html"), "x"); |
| 82 | + writeFileSync(join(project, ".hyperframes"), "a file, not a folder"); |
| 83 | + expect(readRenderOutputs(project)).toEqual([]); |
| 84 | + rmSync(join(project, ".hyperframes")); |
| 85 | + mkdirSync(join(project, ".hyperframes")); |
| 86 | + writeFileSync(join(project, ".hyperframes", "render-outputs.json"), "{not json"); |
| 87 | + expect(() => readRenderOutputs(project)).toThrow(); |
| 88 | + expect(listProjectFiles(project).length).toBeGreaterThan(0); |
| 89 | + expect(affectsProjectSignature(project, join(project, "index.html"))).toBe(true); |
| 90 | + } finally { |
| 91 | + rmSync(project, { recursive: true, force: true }); |
| 92 | + } |
| 93 | +}); |
0 commit comments