Files
vscode-cowriting-plugin/test/baselineStore.test.ts
T
Ben Stull 6f0b903596 feat(f6): BaselineStore — vscode-free per-doc baseline persistence (SLICE-1)
F6 §6.2/§6.3, INV-19. One JSON per docPath under VS Code workspace storage,
never the repo. Mirrors CoauthorStore; unit-tested round-trip/paths/overwrite.

Also lands the F6 implementation plan
(docs/superpowers/plans/2026-06-11-f6-diff-view-toggle.md).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-11 07:11:42 -07:00

54 lines
2.0 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { BaselineStore, type Baseline } from "../src/baselineStore";
let dir: string;
beforeEach(() => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), "baseline-store-"));
});
afterEach(() => {
fs.rmSync(dir, { recursive: true, force: true });
});
function sample(docPath = "notes/chapter-1.md"): Baseline {
return { docPath, text: "hello\nworld\n", capturedAt: "2026-06-11T00:00:00.000Z", reason: "opened" };
}
describe("BaselineStore", () => {
it("returns null for a document with no baseline", () => {
expect(new BaselineStore(dir).load("notes/chapter-1.md")).toBeNull();
});
it("computes the baseline path as baselines/<docPath>.json", () => {
const store = new BaselineStore(dir);
expect(store.baselinePath("notes/chapter-1.md")).toBe(
path.join(dir, "baselines", "notes", "chapter-1.md.json"),
);
});
it("save then load round-trips the baseline (including nested docPaths)", () => {
const store = new BaselineStore(dir);
const b = sample();
store.save(b.docPath, b);
expect(store.load(b.docPath)).toEqual(b);
});
it("overwrites in place: the newest epoch wins, no history kept", () => {
const store = new BaselineStore(dir);
store.save("d.md", { docPath: "d.md", text: "v1", capturedAt: "2026-06-11T00:00:00.000Z", reason: "opened" });
store.save("d.md", { docPath: "d.md", text: "v2", capturedAt: "2026-06-11T00:01:00.000Z", reason: "pinned" });
expect(store.load("d.md")).toEqual({ docPath: "d.md", text: "v2", capturedAt: "2026-06-11T00:01:00.000Z", reason: "pinned" });
});
it("writes pretty JSON with a trailing newline", () => {
const store = new BaselineStore(dir);
const b = sample("d.md");
store.save("d.md", b);
const raw = fs.readFileSync(store.baselinePath("d.md"), "utf8");
expect(raw).toBe(JSON.stringify(b, null, 2) + "\n");
});
});