import { test } from "node:test"; import assert from "node:assert/strict"; import { DIGEST_SCHEMA_VERSION, chapterId, digestPromptVariant, effectiveDigest, isSectionFresh, isSharedFrom, tagId, type DigestOverrides, type DigestRecord, } from "./digest"; // The override model is the single most important thing in the digest layer: a // full sweep is weeks of wall-clock, so a human correction that a regeneration // destroys is work that can never be affordably redone. function machineRecord(over: Partial = {}): DigestRecord { return { digestSchemaVersion: DIGEST_SCHEMA_VERSION, promptVersion: 1, contextHash: "abc123", warnings: [], sections: { chapters: { provenance: { appId: "ollama-direct", model: "qwen2.5:7b", modelRequested: "qwen2.5:7b", lane: "local-gpu", generatedAt: "2026-07-26T00:00:00.000Z", promptVersion: 1, contextHash: "abc123", }, items: [ { id: "c0", start: 0, clock: "00:00:00", title: "Intro", decidedBy: "ai" }, { id: "c300", start: 300, clock: "00:05:00", title: "Court filing deadlnes", decidedBy: "ai", }, ], }, }, ...over, }; } test("effectiveDigest returns machine content when there are no overrides", () => { const e = effectiveDigest(machineRecord(), null); assert.deepEqual( e.chapters.map((c) => c.title), ["Intro", "Court filing deadlnes"], ); assert.equal(e.hasOverrides, false); assert.ok(e.chapters.every((c) => c.decidedBy === "ai")); }); test("a human override REPLACES the machine item and reports decidedBy: human", () => { const overrides: DigestOverrides = { version: 1, chapters: [ { id: "c300", start: 300, clock: "00:05:00", title: "Court filing deadlines", decidedBy: "human" }, ], }; const e = effectiveDigest(machineRecord(), overrides); const fixed = e.chapters.find((c) => c.id === "c300"); assert.equal(fixed?.title, "Court filing deadlines", "the typo is corrected"); assert.equal(fixed?.decidedBy, "human"); assert.equal(e.chapters.length, 2, "the untouched machine chapter survives"); assert.equal(e.hasOverrides, true); }); test("an override authored as a patch does not blank the machine's start", () => { const overrides: DigestOverrides = { version: 1, // Title-only patch: no `start`. chapters: [{ id: "c300", title: "Retitled", decidedBy: "human" }] as never, }; const e = effectiveDigest(machineRecord(), overrides); const patched = e.chapters.find((c) => c.id === "c300"); assert.equal(patched?.start, 300, "the machine's start is preserved"); assert.equal(patched?.title, "Retitled"); }); test("enabled: false SUPPRESSES a generated item without deleting it", () => { const overrides: DigestOverrides = { version: 1, chapters: [{ id: "c0", title: "", decidedBy: "human", enabled: false }] as never, }; const e = effectiveDigest(machineRecord(), overrides); assert.deepEqual( e.chapters.map((c) => c.id), ["c300"], ); // The suppression is a recorded decision, so re-generating the same item does // not resurrect something a human rejected. assert.equal(e.hasOverrides, true); }); test("an override with a new id APPENDS a human-authored chapter", () => { const overrides: DigestOverrides = { version: 1, chapters: [ { id: "c600", start: 600, clock: "00:10:00", title: "Missed topic", decidedBy: "human" }, ], }; const e = effectiveDigest(machineRecord(), overrides); assert.deepEqual( e.chapters.map((c) => c.start), [0, 300, 600], "composed list stays sorted by start", ); }); test("overrides survive a regeneration that rewrites the machine file", () => { const overrides: DigestOverrides = { version: 1, chapters: [{ id: "c300", title: "Court filing deadlines", decidedBy: "human" }] as never, }; // A later run at a new promptVersion re-emits a DIFFERENT title at the same // snapped start — the id is derived from the start, so the human's correction // still shadows it. const regenerated = machineRecord({ promptVersion: 2, sections: { chapters: { provenance: { appId: "ollama-direct", model: "qwen2.5:7b", modelRequested: "qwen2.5:7b", lane: "local-gpu", generatedAt: "2026-08-01T00:00:00.000Z", promptVersion: 2, contextHash: "abc123", }, items: [ { id: "c300", start: 300, clock: "00:05:00", title: "Some new machine title", decidedBy: "ai", }, ], }, }, }); const e = effectiveDigest(regenerated, overrides); assert.equal(e.chapters[0].title, "Court filing deadlines"); assert.equal(e.chapters[0].decidedBy, "human"); }); test("chapterId is derived from the snapped start, so it is stable", () => { assert.equal(chapterId(300), "c300"); assert.equal(chapterId(300.9), "c300"); }); test("tagId normalizes so a re-generated tag lands on the same id", () => { assert.equal(tagId("Court Filings"), "tcourt-filings"); assert.equal(tagId("court filings!"), "tcourt-filings"); }); // --------------------------------------------------------------------------- // Freshness — what keeps a re-run from becoming a second multi-week sweep // --------------------------------------------------------------------------- const TARGET = { appId: "ollama-direct", model: "qwen2.5:7b", promptVersion: 1, contextHash: "abc123", }; test("an unchanged identity is fresh (a re-run is a no-op)", () => { assert.equal(isSectionFresh(machineRecord(), "chapters", TARGET), true); }); test("a missing record or section is stale", () => { assert.equal(isSectionFresh(null, "chapters", TARGET), false); assert.equal(isSectionFresh(machineRecord(), "tags", TARGET), false); }); test("a bumped promptVersion makes the section stale", () => { assert.equal( isSectionFresh(machineRecord(), "chapters", { ...TARGET, promptVersion: 2 }), false, ); }); test("a changed contextHash makes the section stale", () => { // This is why contextHash is plumbed BEFORE the sweep even with empty notes: // adding a channel note later must invalidate that channel, not the corpus. assert.equal( isSectionFresh(machineRecord(), "chapters", { ...TARGET, contextHash: "zzz" }), false, ); }); test("a different engine or model makes the section stale", () => { assert.equal( isSectionFresh(machineRecord(), "chapters", { ...TARGET, appId: "claude-code" }), false, ); assert.equal( isSectionFresh(machineRecord(), "chapters", { ...TARGET, model: "llama3" }), false, ); }); test("freshness compares the REQUESTED model, not the resolved one", () => { // An alias resolving to a full tag is not a model change; treating it as one // would re-run the entire corpus. const record = machineRecord({ sections: { chapters: { provenance: { appId: "ollama-direct", model: "qwen2.5:7b", modelRequested: "qwen2.5", lane: "local-gpu", generatedAt: "2026-07-26T00:00:00.000Z", promptVersion: 1, contextHash: "abc123", }, items: [], }, }, }); assert.equal( isSectionFresh(record, "chapters", { ...TARGET, model: "qwen2.5" }), true, ); }); test("a schema-version bump invalidates every digest on disk", () => { const record = machineRecord({ digestSchemaVersion: 0 }); assert.equal(isSectionFresh(record, "chapters", TARGET), false); }); test("isSharedFrom recognizes a digest copied from a cluster's canonical member", () => { const shared = machineRecord({ derivedFrom: { slug: "Quartering/abc", clusterId: "cluster1", sharedAt: "2026-07-26T00:00:00.000Z", offsetSeconds: 0.4, }, }); assert.equal(isSharedFrom(shared, "Quartering/abc"), true); assert.equal(isSharedFrom(shared, "Quartering/other"), false); assert.equal(isSharedFrom(machineRecord(), "Quartering/abc"), false); }); // --------------------------------------------------------------------------- // promptVariant — the identity field that lets several prompt shapes coexist // --------------------------------------------------------------------------- test("digestPromptVariant: the default configuration has no variant", () => { // The derivation is RELATIVE TO THE DEFAULTS, so this tracks whatever they // currently are — today chunk-local at 600 cues. Pinned explicitly (rather // than via the constants) so that flipping a default without bumping // PROMPT_VERSION fails here loudly instead of silently re-marking every // existing digest as fresh. assert.equal(digestPromptVariant({}), undefined); assert.equal(digestPromptVariant({ timestampMode: "chunk-local" }), undefined); assert.equal(digestPromptVariant({ maxCues: 600 }), undefined); assert.equal( digestPromptVariant({ timestampMode: "chunk-local", maxCues: 600 }), undefined, ); assert.equal(digestPromptVariant({ promptVariant: " " }), undefined); }); test("digestPromptVariant: a non-default timestampMode is folded in", () => { // Folded in rather than left to the operator to remember: a knob that changes // the output but not the identity would let a re-run under a different mode // skip every video as "fresh". Now that chunk-local is the default, it is // ABSOLUTE that must carry an identity of its own. assert.equal(digestPromptVariant({ timestampMode: "absolute" }), "absolute"); assert.equal( digestPromptVariant({ promptVariant: "dense", timestampMode: "absolute" }), "dense+absolute", ); assert.equal(digestPromptVariant({ promptVariant: "dense" }), "dense"); }); test("isSectionFresh: a record with no promptVariant stays fresh by default", () => { // The compatibility guarantee. Every digest written before the field existed // must keep matching, or adding it would have invalidated the whole corpus. assert.equal(isSectionFresh(machineRecord(), "chapters", TARGET), true); }); test("isSectionFresh: a variant change invalidates the section", () => { assert.equal( isSectionFresh(machineRecord(), "chapters", { ...TARGET, promptVariant: "chunk-local", }), false, "a chunk-local re-run must not skip absolute-mode records as fresh", ); }); test("isSectionFresh: a variant record is stale against the default target", () => { const record = machineRecord({ sections: { chapters: { provenance: { appId: "ollama-direct", model: "qwen2.5:7b", modelRequested: "qwen2.5:7b", lane: "local-gpu", generatedAt: "2026-07-26T00:00:00.000Z", promptVersion: 1, promptVariant: "chunk-local", contextHash: "abc123", }, items: [], }, }, }); assert.equal(isSectionFresh(record, "chapters", TARGET), false); assert.equal( isSectionFresh(record, "chapters", { ...TARGET, promptVariant: "chunk-local", }), true, ); }); test("digestPromptVariant: the default chunk size contributes nothing", () => { assert.equal(digestPromptVariant({ maxCues: 600 }), undefined); assert.equal(digestPromptVariant({ maxCues: 1200 }), "c1200"); assert.equal( digestPromptVariant({ timestampMode: "absolute", maxCues: 1200 }), "absolute+c1200", ); }); test("isSectionFresh: a chunk-size change invalidates the section", () => { // Measured: halving the chunk took qwen2.5:7b from 9.34 to 13.37 chapters per // hour and its worst coverage gap from 1:27:48 to 24:13. A change that large // must not be able to hide behind an unchanged identity. 600 is now the // default (and so contributes nothing), so the departure tested here is a // move BACK UP to the old 1200. assert.equal( isSectionFresh(machineRecord(), "chapters", { ...TARGET, promptVariant: digestPromptVariant({ maxCues: 1200 }), }), false, ); });