import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { DIGEST_SCHEMA_VERSION, isSectionFresh, type DigestFreshnessTarget, type DigestProvenance, } from "./digest"; import { loadDigest, writeDigestFailure, writeDigestSection, } from "./digest-server"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test common/lib/digest-server.test.ts async function withVideoDir(fn: (dir: string) => Promise): Promise { const dir = await mkdtemp(path.join(tmpdir(), "ttb-digest-server-")); try { await fn(dir); } finally { await rm(dir, { recursive: true, force: true }); } } const PROVENANCE: DigestProvenance = { appId: "ollama-direct", model: "qwen2.5:7b", modelRequested: "qwen2.5:7b", lane: "local-gpu", generatedAt: "2026-07-29T00:00:00.000Z", promptVersion: 2, contextHash: "ctx", chunks: 3, chunksOk: 3, }; const TARGET: DigestFreshnessTarget = { appId: "ollama-direct", model: "qwen2.5:7b", promptVersion: 2, contextHash: "ctx", }; // The whole point of `failures`: the two total-failure paths in digestVideo // deliberately write no section so the video RETRIES, which used to mean the // warnings from the worst outputs survived nowhere but a rotating job log. test("a recorded failure persists its warnings without making the video look fresh", async () => { await withVideoDir(async (dir) => { await writeDigestFailure(dir, { section: "chapters", at: "2026-07-29T00:00:00.000Z", appId: "ollama-direct", model: "qwen2.5:7b", promptVersion: 2, reason: "all-rejected", chunks: 3, chunksOk: 3, warnings: [ { code: "out-of-range", section: "chapters", detail: "13 clamped" }, ], }); const record = await loadDigest(dir); assert.equal(record?.failures?.length, 1); assert.equal(record?.failures?.[0].reason, "all-rejected"); assert.equal(record?.failures?.[0].warnings.length, 1); // The retry behaviour is the constraint this design exists to preserve. assert.equal(record?.sections.chapters, undefined); assert.equal( isSectionFresh(record, "chapters", TARGET), false, "a failure must never read as fresh, or the video is never retried", ); }); }); // "the model proposed nothing" and "the model proposed and every item was // rejected by a guard" look identical from outside and need different fixes. test("the two failure reasons are recorded distinctly", async () => { await withVideoDir(async (dir) => { await writeDigestFailure(dir, { section: "chapters", at: "2026-07-29T00:00:00.000Z", appId: "a", model: "m", promptVersion: 2, reason: "no-output", chunks: 5, chunksOk: 0, warnings: [], }); assert.equal((await loadDigest(dir))?.failures?.[0].reason, "no-output"); assert.equal((await loadDigest(dir))?.failures?.[0].chunksOk, 0); }); }); // A sweep retries. Appending would grow the sidecar without bound on a video // that fails every single pass over 81 days. test("only the latest failure per section is kept", async () => { await withVideoDir(async (dir) => { for (const at of ["2026-07-01T00:00:00.000Z", "2026-07-29T00:00:00.000Z"]) { await writeDigestFailure(dir, { section: "chapters", at, appId: "a", model: "m", promptVersion: 2, reason: "no-output", chunks: 1, chunksOk: 0, warnings: [], }); } const record = await loadDigest(dir); assert.equal(record?.failures?.length, 1); assert.equal(record?.failures?.[0].at, "2026-07-29T00:00:00.000Z"); }); }); test("a failure in one section does not disturb another section's", async () => { await withVideoDir(async (dir) => { for (const section of ["chapters", "tags"] as const) { await writeDigestFailure(dir, { section, at: "2026-07-29T00:00:00.000Z", appId: "a", model: "m", promptVersion: 2, reason: "no-output", chunks: 1, chunksOk: 0, warnings: [], }); } const record = await loadDigest(dir); assert.equal(record?.failures?.length, 2); assert.deepEqual( record?.failures?.map((f) => f.section).sort(), ["chapters", "tags"], ); }); }); test("a section that later succeeds clears its own failure, not the other's", async () => { await withVideoDir(async (dir) => { for (const section of ["chapters", "tags"] as const) { await writeDigestFailure(dir, { section, at: "2026-07-29T00:00:00.000Z", appId: "a", model: "m", promptVersion: 2, reason: "all-rejected", chunks: 1, chunksOk: 1, warnings: [], }); } await writeDigestSection(dir, { section: "chapters", items: [ { id: "c0", start: 0, clock: "0:00", title: "Intro", decidedBy: "ai" }, ], provenance: PROVENANCE, warnings: [], }); const record = await loadDigest(dir); assert.deepEqual( record?.failures?.map((f) => f.section), ["tags"], "the succeeding section's failure is history; the other's is not ours to clear", ); assert.equal(isSectionFresh(record, "chapters", TARGET), true); assert.equal(record?.digestSchemaVersion, DIGEST_SCHEMA_VERSION); }); }); // A mirror carrying a borrowed digest whose own regeneration fails must not // lose its attribution — the viewer labels borrowed content as borrowed. test("recording a failure preserves derivedFrom", async () => { await withVideoDir(async (dir) => { const { writeSharedDigest } = await import("./digest-server"); await writeSharedDigest( dir, { digestSchemaVersion: DIGEST_SCHEMA_VERSION, promptVersion: 2, contextHash: "ctx", warnings: [], sections: { chapters: { provenance: PROVENANCE, items: [] }, }, }, { slug: "chan/canon", clusterId: "c1", sharedAt: "2026-07-29T00:00:00.000Z", offsetSeconds: 0.2, }, ); await writeDigestFailure(dir, { section: "tags", at: "2026-07-29T00:00:00.000Z", appId: "a", model: "m", promptVersion: 2, reason: "no-output", chunks: 1, chunksOk: 0, warnings: [], }); const record = await loadDigest(dir); assert.equal(record?.derivedFrom?.slug, "chan/canon"); }); });