import { describe, test } from "node:test"; import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { fileURLToPath } from "node:url"; import { mkdtempSync, rmSync, readFileSync, existsSync, writeFileSync } from "node:fs"; import { join } from "node:path"; import { tmpdir } from "node:os"; // Resolve the package root (scripts/trends) so the subprocess `src/cli.ts` path + the // `tsx` loader resolve regardless of the runner's cwd. const trendsDir = fileURLToPath(new URL("..", import.meta.url)); function run(args: string[], input: string): { status: number | null; stdout: string } { const res = spawnSync("node", ["--import", "tsx", "src/cli.ts", ...args], { input, encoding: "utf8", cwd: trendsDir, }); return { status: res.status, stdout: res.stdout }; } describe("trends CLI — normalize/score subcommands (RE-R1 / Step 4)", () => { describe("normalize (stdin JSON in, JSON out)", () => { test("happy path: a JSON batch on stdin -> exit 0 + {items,errors} JSON", () => { const batch = JSON.stringify([ { source: "tavily", title: "Good", url: "https://example.com/a", topics: ["AI", "ai"] }, { source: "tavily", title: "", url: "https://example.com/b", topics: ["x"] }, // bad: empty title ]); const { status, stdout } = run(["normalize"], batch); assert.equal(status, 0); const out = JSON.parse(stdout); assert.equal(out.items.length, 1); assert.deepEqual(out.items[0].topics, ["ai"]); // deduped + lowercased assert.equal(out.errors.length, 1); assert.equal(out.errors[0].index, 1); }); test("bad invocation: unparseable stdin -> exit 2", () => { const { status } = run(["normalize"], "not json at all"); assert.equal(status, 2); }); }); describe("score (stdin JSON in, JSON out)", () => { test("happy path: scored candidates on stdin -> exit 0 + {kept,dropped} JSON", () => { const candidates = JSON.stringify([ { id: "high", scores: { pillar: 8, audience: 8, timing: 8, angle: 8, authority: 8 } }, // 8.0 { id: "low", scores: { pillar: 2, audience: 2, timing: 2, angle: 2, authority: 2 } }, // 2.0 ]); const { status, stdout } = run(["score", "--mode", "kortform", "--threshold", "4.0"], candidates); assert.equal(status, 0); const out = JSON.parse(stdout); assert.deepEqual( out.kept.map((k: { id: string }) => k.id), ["high"], ); assert.equal(out.kept[0].composite, 8.0); assert.equal(out.kept[0].band.priority, "Immediate"); assert.deepEqual( out.dropped.map((d: { id: string }) => d.id), ["low"], ); }); test("bad invocation: an unknown --mode -> exit 2", () => { const candidates = JSON.stringify([ { id: "x", scores: { pillar: 5, audience: 5, timing: 5, angle: 5, authority: 5 } }, ]); const { status } = run(["score", "--mode", "bogus"], candidates); assert.equal(status, 2); }); }); describe("capture (stdin raw item|batch -> folds into the store) (RE-R2a / Step 4)", () => { const tmpStore = () => join(mkdtempSync(join(tmpdir(), "trends-capture-")), "trends.json"); test("happy path: a valid item piped in -> folded into the store, added:1, publishedAt persisted", () => { const store = tmpStore(); try { const batch = JSON.stringify([ { source: "tavily", title: "Captured", url: "https://example.com/c", topics: ["ai"], publishedAt: "2026-06-20", }, ]); const { status, stdout } = run(["capture", "--store", store, "--json"], batch); assert.equal(status, 0); const summary = JSON.parse(stdout); assert.equal(summary.added, 1); assert.equal(summary.errors.length, 0); assert.equal( summary.added + summary.merged + summary.duplicates + summary.errors.length, 1, "tally must sum to the input size", ); const persisted = JSON.parse(readFileSync(store, "utf8")); assert.equal(persisted.schemaVersion, 2); assert.equal(persisted.trends.length, 1); assert.equal(persisted.trends[0].publishedAt, "2026-06-20"); assert.match(persisted.trends[0].capturedAt, /^\d{4}-\d{2}-\d{2}$/); assert.notEqual( persisted.trends[0].capturedAt, persisted.trends[0].publishedAt, "capturedAt (when WE saw it) must be distinct from publishedAt (source date)", ); } finally { rmSync(join(store, ".."), { recursive: true, force: true }); } }); test("a batch with one content-invalid item -> valid added, invalid in errors[], exit 0", () => { const store = tmpStore(); try { const batch = JSON.stringify([ { source: "tavily", title: "Valid", url: "https://example.com/v", topics: ["x"] }, { title: "no source or url" }, ]); const { status, stdout } = run(["capture", "--store", store, "--json"], batch); assert.equal(status, 0); const summary = JSON.parse(stdout); assert.equal(summary.added, 1); assert.equal(summary.errors.length, 1); assert.equal( summary.added + summary.merged + summary.duplicates + summary.errors.length, 2, ); } finally { rmSync(join(store, ".."), { recursive: true, force: true }); } }); test("re-capturing the same trend with a new topic -> merged:1, tally still sums", () => { const store = tmpStore(); try { const item = (topics: string[]) => JSON.stringify([{ source: "tavily", title: "Dup", url: "https://example.com/d", topics }]); run(["capture", "--store", store, "--json"], item(["a"])); const { status, stdout } = run(["capture", "--store", store, "--json"], item(["a", "b"])); assert.equal(status, 0); const summary = JSON.parse(stdout); assert.equal(summary.added, 0); assert.equal(summary.merged, 1); assert.equal(summary.duplicates, 0); assert.equal( summary.added + summary.merged + summary.duplicates + summary.errors.length, 1, ); } finally { rmSync(join(store, ".."), { recursive: true, force: true }); } }); test("bad invocation: empty stdin -> exit 2", () => { const { status } = run(["capture"], ""); assert.equal(status, 2); }); }); }); describe("trends CLI — brief subcommand (RE-R2b / Step 3)", () => { // brief is flag-driven (reads the store, not stdin). spawn with an env-overridable // LINKEDIN_STUDIO_DATA so defaultBriefDir() resolves into a temp root (never the real HOME). function runBrief(args: string[], env: Record = {}): { status: number | null; stdout: string } { const res = spawnSync("node", ["--import", "tsx", "src/cli.ts", "brief", ...args], { input: "", encoding: "utf8", cwd: trendsDir, env: { ...process.env, ...env }, }); return { status: res.status, stdout: res.stdout }; } const freshIso = new Date(Date.now() - 2 * 86400000).toISOString().slice(0, 10); function seedStore(trends: unknown[]): string { const store = join(mkdtempSync(join(tmpdir(), "trends-brief-")), "trends.json"); writeFileSync(store, JSON.stringify({ schemaVersion: 2, trends })); return store; } test("happy: writes a dated brief, --json carries path/date/totals/summary", () => { const store = seedStore([ { id: "a", title: "Fresh Match", url: "https://e/a", source: "tavily", capturedAt: freshIso, publishedAt: freshIso, topics: ["ai", "gov"] }, ]); const out = mkdtempSync(join(tmpdir(), "brief-out-")); try { const { status, stdout } = runBrief(["--pillars", "ai,gov", "--store", store, "--out", out, "--json"]); assert.equal(status, 0); const summary = JSON.parse(stdout); assert.match(summary.path, /\d{4}-\d{2}-\d{2}\.md$/); assert.match(summary.date, /^\d{4}-\d{2}-\d{2}$/); assert.equal(summary.totals.trends, 1); assert.equal(summary.totals.fresh, 1); assert.ok(existsSync(summary.path), "the dated brief file is written"); const md = readFileSync(summary.path, "utf8"); const m = md.match(/^summary: (.*)$/m); assert.ok(m, "the brief frontmatter has a summary line"); assert.equal(m![1], summary.summary, "--json summary equals the file frontmatter summary (one source)"); } finally { rmSync(join(store, ".."), { recursive: true, force: true }); rmSync(out, { recursive: true, force: true }); } }); test("bad invocation: --fresh-days non-numeric -> exit 2", () => { const store = seedStore([]); try { const { status } = runBrief(["--pillars", "ai", "--store", store, "--fresh-days", "xyz"]); assert.equal(status, 2); } finally { rmSync(join(store, ".."), { recursive: true, force: true }); } }); test("empty --pillars -> exit 0 + a no-match brief is still written", () => { const store = seedStore([ { id: "a", title: "X", url: "https://e/x", source: "tavily", capturedAt: freshIso, publishedAt: freshIso, topics: ["ai"] }, ]); const out = mkdtempSync(join(tmpdir(), "brief-out-")); try { const { status, stdout } = runBrief(["--store", store, "--out", out, "--json"]); assert.equal(status, 0); const summary = JSON.parse(stdout); assert.equal(summary.totals.matched, 0, "no pillars -> nothing matched"); assert.ok(existsSync(summary.path), "a dated no-match brief is still written"); } finally { rmSync(join(store, ".."), { recursive: true, force: true }); rmSync(out, { recursive: true, force: true }); } }); test("bare --out (no value) -> falls back to defaultBriefDir, never ./true", () => { const store = seedStore([]); const dataRoot = mkdtempSync(join(tmpdir(), "brief-data-")); try { // bare --out --json: parseFlags yields out:"true"; the !== "true" guard must // fall back to defaultBriefDir() = /trends/morning-brief. const { status, stdout } = runBrief(["--pillars", "ai", "--store", store, "--json", "--out"], { LINKEDIN_STUDIO_DATA: dataRoot }); assert.equal(status, 0); const summary = JSON.parse(stdout); assert.ok( summary.path.startsWith(join(dataRoot, "trends", "morning-brief")), "bare --out must fall back to defaultBriefDir under LINKEDIN_STUDIO_DATA, not ./true", ); assert.ok(!existsSync(join(trendsDir, "true")), "no ./true dir was created"); } finally { rmSync(join(store, ".."), { recursive: true, force: true }); rmSync(dataRoot, { recursive: true, force: true }); } }); });