// @vitest-environment node import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it, onTestFinished } from "vitest"; import { withCliWrites } from "./cliWrites.js"; import { historyDeps, withOwner, withReader, writeTurn } from "./historyOwner.js"; function tempDir(prefix: string): string { const dir = mkdtempSync(join(tmpdir(), prefix)); onTestFinished(() => rmSync(dir, { recursive: true, force: true })); return dir; } /** A project whose history has seen index.html "A" and notes.html "N", kept in a temp root, no preview. */ async function project(): Promise { const dir = tempDir("hf-cli-writes-"); writeFileSync(join(dir, "index.html"), "A"); writeFileSync(join(dir, "notes.html"), "N"); historyDeps.historyRoot = tempDir("hf-cli-writes-root-"); historyDeps.findServer = async () => null; historyDeps.ownerWaitMs = undefined; historyDeps.now = () => Date.now(); await withOwner(dir, (owner) => owner.checkpoint()); return dir; } const entries = (dir: string) => withReader(dir, (reader) => reader.list()); describe("withCliWrites", () => { it("files a CLI command's writes under the command, not as changed outside the app", async () => { const dir = await project(); await withCliWrites(dir, "add grain-overlay", () => writeFileSync(join(dir, "index.html"), "B"), ); const [entry, ...rest] = await entries(dir); expect(rest).toEqual([]); expect(entry).toMatchObject({ who: { kind: "person", name: "You" }, label: "hyperframes add grain-overlay", }); expect(entry?.files.map((file) => file.path)).toEqual(["index.html"]); }); it("leaves a hand edit made alongside it outside the app", async () => { const dir = await project(); writeFileSync(join(dir, "notes.html"), "M"); await withCliWrites(dir, "media-treatment", () => writeFileSync(join(dir, "index.html"), "B")); await withOwner(dir, (owner) => owner.checkpoint()); const byLabel = Object.fromEntries( (await entries(dir)).map((entry) => [entry.label, entry.files.map((file) => file.path)]), ); expect(byLabel["hyperframes media-treatment"]).toEqual(["index.html"]); expect(byLabel["Changed outside the app"]).toEqual(["notes.html"]); }); it("leaves the writes to an open agent turn", async () => { const dir = await project(); const now = Date.now(); writeTurn(dir, { via: "direct", id: "00000000-0000-4000-8000-000000000001", who: { kind: "agent", name: "claude" }, label: "Bigger title", startedAt: now - 1000, lastWriteAt: now - 1000, parts: [], }); await withCliWrites(dir, "add grain-overlay", () => writeFileSync(join(dir, "index.html"), "B"), ); await withOwner(dir, (owner) => owner.checkpoint()); const labels = (await entries(dir)).map((entry) => entry.label); expect(labels).not.toContain("hyperframes add grain-overlay"); }); it("still writes, unrecorded, when another process keeps the history", async () => { const dir = await project(); await withOwner(dir, async () => { const value = await withCliWrites(dir, "add x", () => { writeFileSync(join(dir, "index.html"), "B"); return 7; }); expect(value).toBe(7); }); expect(readFileSync(join(dir, "index.html"), "utf-8")).toBe("B"); }); it("fails with the write's own error", async () => { const dir = await project(); await expect( withCliWrites(dir, "add x", () => { throw new Error("disk full"); }), ).rejects.toThrow("disk full"); }); });