import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, readdir, rm, writeFile, mkdir } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { LookoutStoreError, ideaKey, listLookoutRunIds, lookoutReadout, lookoutRunFile, lookoutVerdictsFile, lookoutsRoot, measuredSecondsPerCall, newLookoutRunId, readLookoutRun, readLookoutVerdicts, sanitizeLookoutVerdicts, setLookoutVerdict, surfacedIdeas, writeLookoutRun, type LookoutIdea, type LookoutRun, } from "./lookoutStore"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/lookoutStore.test.ts async function corpus() { const transcriptsDir = await mkdtemp(path.join(os.tmpdir(), "lookout-store-")); return { transcriptsDir }; } const M = (channel: string, video: string, start: number) => ({ channel, video, start }); type Mo = ReturnType; // The key of moments that have one (every list here cites at least one). const K = (ms: Mo[]): string => { const k = ideaKey(ms); assert.ok(k, "a cited idea has a key"); return k; }; test("an idea's key is its story: order-free, 5-second grain, changed by a moment added", () => { const a = K([M("example-news", "v1", 100), M("example-live", "v2", 2000)]); assert.match(a, /^[0-9a-f]{16}$/); assert.equal(K([M("example-live", "v2", 2001), M("example-news", "v1", 99)]), a); assert.notEqual(K([M("example-news", "v1", 100), M("example-live", "v2", 2010)]), a); assert.notEqual(K([M("example-news", "v1", 100), M("example-live", "v2", 2000), M("example-news", "v3", 5)]), a); }); test("the store is a dot path under the corpus, and ids are checked before any path is made", async () => { const p = await corpus(); try { assert.equal(lookoutsRoot(p), path.join(p.transcriptsDir, ".lookouts")); assert.equal(lookoutVerdictsFile(p, "weekly"), path.join(p.transcriptsDir, ".lookouts", "weekly", "verdicts.json")); assert.throws(() => lookoutVerdictsFile(p, "../escape"), LookoutStoreError); assert.throws(() => lookoutRunFile(p, "weekly", "../x"), LookoutStoreError); assert.match(newLookoutRunId(new Date("2026-11-11T17:45:00.123Z")), /^20261111T174500Z-[0-9a-f]{4}$/); } finally { await rm(p.transcriptsDir, { recursive: true, force: true }); } }); test("a verdict is set atomically, keeps the note it had, and two at once both land", async () => { const p = await corpus(); try { const k1 = K([M("example-news", "v1", 10), M("example-news", "v2", 20)]); const k2 = K([M("example-news", "v3", 10), M("example-news", "v4", 20)]); const now = new Date("2026-11-11T12:00:00Z"); await setLookoutVerdict(p, "weekly", { key: k1, verdict: "maybe", note: " check the date ", now }); const made = await setLookoutVerdict(p, "weekly", { key: k1, verdict: "make", project: "shorts-2026-11/x", now }); assert.deepEqual(made, { verdict: "make", at: now.toISOString(), note: "check the date", project: "shorts-2026-11/x" }); await Promise.all([ setLookoutVerdict(p, "weekly", { key: k2, verdict: "no", now }), setLookoutVerdict(p, "weekly", { key: k1, verdict: "make", note: "", now }), ]); const v = await readLookoutVerdicts(p, "weekly"); assert.equal(v.ideas[k2]?.verdict, "no"); assert.equal(v.ideas[k1]?.note, undefined, "an empty note clears it"); assert.equal(v.ideas[k1]?.project, "shorts-2026-11/x"); // No temp file left beside it. assert.deepEqual(await readdir(path.dirname(lookoutVerdictsFile(p, "weekly"))), ["verdicts.json"]); await assert.rejects(setLookoutVerdict(p, "weekly", { key: "nope", verdict: "make" }), LookoutStoreError); await assert.rejects( setLookoutVerdict(p, "weekly", { key: k1, verdict: "yes" as never }), LookoutStoreError, ); } finally { await rm(p.transcriptsDir, { recursive: true, force: true }); } }); test("a hand-damaged verdicts file reads as what is valid in it", async () => { const k = K([M("a", "b", 0)]); assert.deepEqual( sanitizeLookoutVerdicts({ ideas: { [k]: { verdict: "make", at: "x" }, bad: { verdict: "make", at: "x" }, [k.replace(/^./, "f")]: { verdict: "meh", at: "x" } } }), { version: 1, ideas: { [k]: { verdict: "make", at: "x" } } }, ); const p = await corpus(); try { await mkdir(path.dirname(lookoutVerdictsFile(p, "weekly")), { recursive: true }); await writeFile(lookoutVerdictsFile(p, "weekly"), "{ not json"); assert.deepEqual(await readLookoutVerdicts(p, "weekly"), { version: 1, ideas: {} }); // ...and is never written over as if it were empty. await assert.rejects(setLookoutVerdict(p, "weekly", { key: k, verdict: "make" }), /unparseable.*not written/); assert.equal(await readFile(lookoutVerdictsFile(p, "weekly"), "utf8"), "{ not json"); } finally { await rm(p.transcriptsDir, { recursive: true, force: true }); } }); function idea(moments: Mo[]): LookoutIdea { const key = K(moments); return { id: key.slice(0, 6), key, workingTitle: "t", angle: "a", whyNow: "w", kind: "reversal", estimatedLengthSec: 60, confidence: "high", confidenceWhy: "c", moments: moments.map((m) => ({ ...m, date: "2026-11-10", end: m.start + 10, quote: "q" })), caveats: [], sensitive: false, }; } function run(runId: string, ideas: LookoutIdea[], calls = 0, seconds = 0): LookoutRun { return { version: 1, lookoutId: "weekly", runId, startedAt: "2026-11-11T12:00:00.000Z", endedAt: "2026-11-11T12:10:00.000Z", status: "done", window: { from: "2026-11-05", to: "2026-11-11", days: 7, timeZone: "UTC" }, model: "local-model", calls, seconds, selection: null, ideas, }; } test("runs: listed newest first, the latest summarised, unruled ideas counted, seconds per call measured", async () => { const p = await corpus(); try { assert.deepEqual(await lookoutReadout(p, "weekly"), { lastRun: null, runs: 0, unruled: 0, verdicts: { make: 0, maybe: 0, no: 0 }, }); assert.equal(await measuredSecondsPerCall(p, "weekly"), null); const m1 = [M("a", "b", 0), M("a", "c", 0)]; const m2 = [M("a", "d", 0), M("a", "e", 0)]; const k1 = K(m1); await writeLookoutRun(p, run("20261110T120000Z-aaaa", [idea(m1)], 100, 400)); await writeLookoutRun(p, run("20261111T120000Z-bbbb", [idea(m1), idea(m2)], 50, 100)); await setLookoutVerdict(p, "weekly", { key: k1, verdict: "no" }); assert.deepEqual(await listLookoutRunIds(p, "weekly"), ["20261111T120000Z-bbbb", "20261110T120000Z-aaaa"]); const r = await lookoutReadout(p, "weekly"); assert.equal(r.runs, 2); assert.equal(r.lastRun?.runId, "20261111T120000Z-bbbb"); assert.equal(r.lastRun?.ideas, 2); assert.equal(r.unruled, 1, "the story ruled in an earlier run keeps its verdict"); assert.deepEqual(r.verdicts, { make: 0, maybe: 0, no: 1 }); assert.deepEqual(await measuredSecondsPerCall(p, "weekly"), { secondsPerCall: 500 / 150, runs: 2, calls: 150 }); assert.equal(await readLookoutRun(p, "weekly", "20261109T120000Z-cccc"), null); const text = await readFile(lookoutRunFile(p, "weekly", "20261111T120000Z-bbbb"), "utf8"); assert.equal(JSON.parse(text).ideas.length, 2); } finally { await rm(p.transcriptsDir, { recursive: true, force: true }); } }); test("no citation, no key: a moment-less idea has none, and a stored one is not surfaced, counted or ruled", async () => { assert.equal(ideaKey([]), null); // Two moment-less ideas would otherwise share one key (the empty list's // hash) — and one verdict, and the de-duplication would fold them. const cited = idea([M("a", "b", 0), M("a", "c", 30)]); const uncited: LookoutIdea = { ...cited, id: "bare", key: "FAKE-TEST-VALUE", moments: [] }; const forged: LookoutIdea = { ...cited, id: "forged", key: "FAKE-TEST-VALUE" }; assert.deepEqual( surfacedIdeas({ ideas: [cited, uncited, forged] }).map((i) => i.id), [cited.id], "an idea with no moment, and one whose key is not its moments', are not findings", ); const p = await corpus(); try { await writeLookoutRun(p, run("20261111T120000Z-dddd", [cited, uncited, forged])); const r = await lookoutReadout(p, "weekly"); assert.equal(r.lastRun?.ideas, 1); assert.equal(r.unruled, 1); } finally { await rm(p.transcriptsDir, { recursive: true, force: true }); } }); test("a damaged run file never throws: a moment or idea that is not one is skipped, and the readout still answers", async () => { const good = [M("a", "v", 10), M("a", "w", 20)]; const k = K(good); const ideaOf = (moments: unknown[]) => ({ ...idea(good), key: k, moments }) as unknown as LookoutIdea; const clean = surfacedIdeas({ ideas: [ideaOf(good), ideaOf([null, ...good]), ideaOf(["x"]), null as never, "y" as never] }); assert.equal(clean.length, 2, "the idea and the one whose null moment is skipped"); assert.ok(clean.every((i) => i.moments.length === 2 && i.moments.every((m) => typeof m === "object" && m !== null))); const p = await corpus(); try { await writeLookoutRun(p, run("20261111T120000Z-eeee", [ideaOf([null, ...good]), ideaOf([7])])); const r = await lookoutReadout(p, "weekly"); assert.equal(r.lastRun?.ideas, 1); assert.equal(r.unruled, 1); } finally { await rm(p.transcriptsDir, { recursive: true, force: true }); } });