linkedin-studio/scripts/trends/tests/store.test.ts
Kjell Tore Guttormsen 7a158030b6 feat(linkedin-studio): RE-R2a — item→store capture bridge + publishedAt persistence (schema v1→v2, lossless migrate) [skip-docs]
Closes the research-engine capture loop RE-R1 deferred:
- itemToInput(item, capturedAt): pure envelope→TrendInput bridge in item.ts —
  injects capturedAt, carries publishedAt verbatim; no id, no re-validate
- publishedAt persisted: TrendRecord/TrendInput gain it; addTrend conditional-spread,
  first-sight kept on re-capture (no back-fill). SCHEMA_VERSION 1→2 with a lossless
  forward migrate-on-load: Math.max(onDisk, current) + numeric-typeof coercion
  (string/NaN/absent → current; non-array trends coercion preserved verbatim)
- `capture` CLI: stdin raw item|batch → normalize → bridge → addTrend → saveStore once;
  tally {added,duplicates,merged,errors} from AddResult; content-invalid → errors[],
  exit 2 only on bad stdin; --json summary
- wiring: trend-spotter.md Step 4.5 N×`add` → one normalizing `capture` batch; README
  add/capture framing corrected; test-runner Section 16h (capture wiring, unconditional)
  + floors bumped (trends 62→79, ASSERT 87→90)

TDD: 17 new tests (12 genuinely-RED logic-RED + 5 regression guards), tsc clean,
gate 105/0/0. No version bump (additive, v0.5.2 dev).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VmHCQjJHUyWwxGAVVjNLgp
2026-06-24 11:12:50 +02:00

496 lines
17 KiB
TypeScript

import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, existsSync, writeFileSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import {
normalizeField,
trendId,
emptyStore,
loadStore,
saveStore,
addTrend,
queryByTopic,
history,
newestCaptureDate,
} from "../src/store.js";
import { SCHEMA_VERSION } from "../src/types.js";
import type { TrendStore } from "../src/types.js";
const tmp = () => mkdtempSync(join(tmpdir(), "trends-store-"));
describe("trends store", () => {
describe("normalizeField + trendId", () => {
test("normalizeField collapses whitespace + lowercases", () => {
assert.equal(normalizeField(" AI Trends\n2026 "), "ai trends 2026");
});
test("id is deterministic and (title+url)-derived", () => {
assert.equal(
trendId("AI agents go mainstream", "https://example.com/a"),
trendId("AI agents go mainstream", "https://example.com/a"),
);
});
test("id is stable across case + surrounding/collapsed whitespace in title AND url", () => {
const a = trendId("AI agents go mainstream", "https://example.com/article");
const b = trendId(" ai AGENTS go mainstream ", " HTTPS://EXAMPLE.COM/article ");
assert.equal(a, b, "normalization should collapse case + whitespace on both fields");
});
test("different title yields a different id (same url)", () => {
assert.notEqual(
trendId("AI agents go mainstream", "https://example.com/a"),
trendId("AI copilots go mainstream", "https://example.com/a"),
);
});
test("different url yields a different id (same title)", () => {
assert.notEqual(
trendId("Same headline", "https://example.com/a"),
trendId("Same headline", "https://example.com/b"),
);
});
});
describe("emptyStore + load/save round-trip", () => {
test("emptyStore has the schema version and no trends", () => {
const s = emptyStore();
assert.equal(s.schemaVersion, SCHEMA_VERSION);
assert.deepEqual(s.trends, []);
});
test("loadStore on a missing file returns an empty store (never throws)", () => {
const dir = tmp();
try {
const s = loadStore(join(dir, "does-not-exist.json"));
assert.equal(s.schemaVersion, SCHEMA_VERSION);
assert.deepEqual(s.trends, []);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("saveStore creates the parent dir, then loadStore round-trips", () => {
const dir = tmp();
const path = join(dir, "nested", "trends.json");
try {
let store: TrendStore = emptyStore();
store = addTrend(store, {
title: "Norway opens public-sector AI sandbox",
url: "https://example.com/sandbox",
source: "tavily",
capturedAt: "2026-06-20",
topics: ["public-sector", "ai-policy"],
summary: "A regulatory sandbox for public bodies.",
}).store;
saveStore(path, store);
assert.ok(existsSync(path));
const loaded = loadStore(path);
assert.equal(loaded.trends.length, 1);
assert.equal(loaded.trends[0].title, "Norway opens public-sector AI sandbox");
assert.equal(loaded.trends[0].source, "tavily");
assert.equal(loaded.trends[0].summary, "A regulatory sandbox for public bodies.");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
describe("addTrend", () => {
test("adds a new trend with a computed id and reports added:true", () => {
const res = addTrend(emptyStore(), {
title: "Agentic workflows hit production",
url: "https://example.com/agentic",
source: "websearch",
capturedAt: "2026-06-20",
topics: ["agents", "engineering"],
});
assert.equal(res.added, true);
assert.equal(res.merged, false);
assert.equal(res.store.trends.length, 1);
assert.equal(res.store.trends[0].id, trendId(res.store.trends[0].title, res.store.trends[0].url));
});
test("preserves title/url/summary verbatim (normalizes only for the id)", () => {
const title = " Mixed CASE Headline ";
const url = " https://EXAMPLE.com/Path ";
const summary = " A summary with spacing. ";
const res = addTrend(emptyStore(), {
title,
url,
source: "manual",
capturedAt: "2026-06-20",
topics: ["x"],
summary,
});
assert.equal(res.store.trends[0].title, title, "title must not be mutated");
assert.equal(res.store.trends[0].url, url, "url must not be mutated");
assert.equal(res.store.trends[0].summary, summary, "summary must not be mutated");
});
test("omitting summary stores no summary key", () => {
const res = addTrend(emptyStore(), {
title: "No summary here",
url: "https://example.com/n",
source: "manual",
capturedAt: "2026-06-20",
topics: ["x"],
});
assert.equal("summary" in res.store.trends[0], false);
});
test("dedupes on normalized title+url: same trend twice → one entry, topics unioned", () => {
let store = emptyStore();
store = addTrend(store, {
title: "Reasoning models reshape RAG",
url: "https://example.com/rag",
source: "tavily",
capturedAt: "2026-06-19",
topics: ["rag"],
}).store;
const res2 = addTrend(store, {
title: " reasoning MODELS reshape rag ",
url: " HTTPS://EXAMPLE.COM/rag ",
source: "gemini",
capturedAt: "2026-06-20",
topics: ["search", "rag"],
});
assert.equal(res2.added, false, "duplicate title+url must not add a second entry");
assert.equal(res2.merged, true, "new topics on a duplicate must report merged:true");
assert.equal(res2.store.trends.length, 1);
assert.deepEqual(
[...res2.store.trends[0].topics].sort(),
["rag", "search"],
"topics must be unioned on re-capture",
);
});
test("a duplicate keeps the first sighting's source + capturedAt (provenance of first sight)", () => {
let store = emptyStore();
store = addTrend(store, {
title: "Same trend",
url: "https://example.com/s",
source: "first-source",
capturedAt: "2026-06-01",
topics: ["a"],
}).store;
store = addTrend(store, {
title: "Same trend",
url: "https://example.com/s",
source: "second-source",
capturedAt: "2026-06-20",
topics: ["b"],
}).store;
assert.equal(store.trends[0].source, "first-source");
assert.equal(store.trends[0].capturedAt, "2026-06-01");
});
test("a duplicate with no new topics reports merged:false", () => {
let store = emptyStore();
store = addTrend(store, {
title: "Stable trend",
url: "https://example.com/st",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["a", "b"],
}).store;
const res2 = addTrend(store, {
title: "Stable trend",
url: "https://example.com/st",
source: "tavily",
capturedAt: "2026-06-02",
topics: ["b", "a"],
});
assert.equal(res2.added, false);
assert.equal(res2.merged, false, "no new tags → merged:false");
});
// ── RE-R2a: publishedAt persistence ──
test("RED: persists publishedAt on a new record when present", () => {
const res = addTrend(emptyStore(), {
title: "Dated trend",
url: "https://example.com/d",
source: "tavily",
capturedAt: "2026-06-24",
topics: ["ai"],
publishedAt: "2026-06-20",
});
assert.equal(res.store.trends[0].publishedAt, "2026-06-20");
});
test("regression guard: omits publishedAt when absent (no undefined-valued key)", () => {
const res = addTrend(emptyStore(), {
title: "Undated trend",
url: "https://example.com/u",
source: "tavily",
capturedAt: "2026-06-24",
topics: ["ai"],
});
assert.equal("publishedAt" in res.store.trends[0], false);
});
test("RED: re-capture keeps the first sighting's publishedAt (no overwrite), unions topics", () => {
let store = emptyStore();
store = addTrend(store, {
title: "Same dated trend",
url: "https://example.com/sd",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["a"],
publishedAt: "2026-05-30",
}).store;
const res2 = addTrend(store, {
title: "Same dated trend",
url: "https://example.com/sd",
source: "gemini",
capturedAt: "2026-06-20",
topics: ["a", "b"],
publishedAt: "2026-06-15",
});
assert.equal(res2.added, false);
assert.equal(res2.merged, true);
assert.equal(res2.store.trends[0].publishedAt, "2026-05-30", "first-sight publishedAt kept");
assert.deepEqual([...res2.store.trends[0].topics].sort(), ["a", "b"]);
});
test("regression guard: no back-fill — re-capture carrying publishedAt onto a record that lacked one does NOT add it", () => {
let store = emptyStore();
store = addTrend(store, {
title: "Undated first sight",
url: "https://example.com/uf",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["a"],
}).store; // no publishedAt on first sight
const res2 = addTrend(store, {
title: "Undated first sight",
url: "https://example.com/uf",
source: "tavily",
capturedAt: "2026-06-20",
topics: ["a", "b"],
publishedAt: "2026-06-15",
});
assert.equal(res2.merged, true, "topic union still reported");
assert.equal("publishedAt" in res2.store.trends[0], false, "no back-fill of first-sight provenance");
});
});
describe("queryByTopic", () => {
const seed = (): TrendStore => {
let store = emptyStore();
store = addTrend(store, {
title: "Trend A",
url: "https://example.com/a",
source: "tavily",
capturedAt: "2026-05-01",
topics: ["agents", "engineering"],
}).store;
store = addTrend(store, {
title: "Trend B",
url: "https://example.com/b",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["agents", "engineering", "public-sector"],
}).store;
store = addTrend(store, {
title: "Trend C",
url: "https://example.com/c",
source: "manual",
capturedAt: "2026-06-10",
topics: ["privacy"],
}).store;
return store;
};
test("ranks hits by topic overlap (desc)", () => {
// B carries both queried topics; A carries only "agents" → distinct overlaps.
const hits = queryByTopic(seed(), ["agents", "public-sector"]);
assert.equal(hits.length, 2);
assert.equal(hits[0].trend.title, "Trend B"); // overlap 2
assert.equal(hits[0].topicOverlap, 2);
assert.equal(hits[1].trend.title, "Trend A"); // overlap 1 (agents only)
assert.equal(hits[1].topicOverlap, 1);
});
test("matching is case-insensitive on topics", () => {
const hits = queryByTopic(seed(), ["AGENTS"]);
assert.equal(hits.length, 2);
});
test("returns [] when nothing matches", () => {
assert.deepEqual(queryByTopic(seed(), ["nothing"]), []);
});
test("ties on overlap are broken by capturedAt (newest first)", () => {
const hits = queryByTopic(seed(), ["agents"]); // A and B both overlap 1
assert.equal(hits[0].trend.title, "Trend B"); // 2026-06-01 newer than 2026-05-01
assert.equal(hits[1].trend.title, "Trend A");
});
});
describe("history", () => {
const seed = (): TrendStore => {
let store = emptyStore();
for (const [title, capturedAt] of [
["Oldest", "2026-04-01"],
["Middle", "2026-05-01"],
["Newest", "2026-06-01"],
] as const) {
store = addTrend(store, {
title,
url: `https://example.com/${title}`,
source: "tavily",
capturedAt,
topics: ["x"],
}).store;
}
return store;
};
test("returns all trends newest-first", () => {
const h = history(seed());
assert.deepEqual(h.map((t) => t.title), ["Newest", "Middle", "Oldest"]);
});
test("respects an inclusive `since` filter", () => {
const h = history(seed(), { since: "2026-05-01" });
assert.deepEqual(h.map((t) => t.title), ["Newest", "Middle"]);
});
test("respects `limit`", () => {
const h = history(seed(), { limit: 1 });
assert.deepEqual(h.map((t) => t.title), ["Newest"]);
});
});
describe("newestCaptureDate (B-S3 staleness signal)", () => {
test("empty store → null (no captures yet → no nudge)", () => {
assert.equal(newestCaptureDate(emptyStore()), null);
});
test("single trend → its capturedAt", () => {
const store = addTrend(emptyStore(), {
title: "Solo",
url: "https://example.com/solo",
source: "tavily",
capturedAt: "2026-06-05",
topics: ["x"],
}).store;
assert.equal(newestCaptureDate(store), "2026-06-05");
});
test("multiple trends → the max (newest) capturedAt, regardless of insertion order", () => {
let store = emptyStore();
for (const [title, capturedAt] of [
["Middle", "2026-05-01"],
["Newest", "2026-06-01"],
["Oldest", "2026-04-01"],
] as const) {
store = addTrend(store, {
title,
url: `https://example.com/${title}`,
source: "tavily",
capturedAt,
topics: ["x"],
}).store;
}
assert.equal(newestCaptureDate(store), "2026-06-01");
});
});
describe("schema migration (RE-R2a / publishedAt v1→v2)", () => {
const withFixture = (contents: string, fn: (path: string) => void) => {
const dir = tmp();
const path = join(dir, "trends.json");
try {
writeFileSync(path, contents, "utf8");
fn(path);
} finally {
rmSync(dir, { recursive: true, force: true });
}
};
// ── genuinely RED: old loadStore returns the on-disk/garbage version unchanged ──
test("RED: a v1 store loads stamped as v2, records intact, no publishedAt invented", () => {
const v1 = JSON.stringify({
schemaVersion: 1,
trends: [
{
id: "abc123",
title: "Old trend",
url: "https://example.com/o",
source: "tavily",
capturedAt: "2026-05-01",
topics: ["ai"],
},
],
});
withFixture(v1, (path) => {
const s = loadStore(path);
assert.equal(s.schemaVersion, 2, "v1 store must migrate to v2");
assert.equal(s.trends.length, 1);
assert.equal(s.trends[0].title, "Old trend");
assert.equal(s.trends[0].capturedAt, "2026-05-01");
assert.deepEqual(s.trends[0].topics, ["ai"]);
assert.equal("publishedAt" in s.trends[0], false, "migration must not invent publishedAt");
});
});
test("RED: round-trip loadStore→saveStore writes schemaVersion:2 to disk", () => {
withFixture(JSON.stringify({ schemaVersion: 1, trends: [] }), (path) => {
saveStore(path, loadStore(path));
const onDisk = JSON.parse(readFileSync(path, "utf8"));
assert.equal(onDisk.schemaVersion, 2);
});
});
test("RED: a non-numeric schemaVersion is coerced to v2 (old code passes the string through)", () => {
withFixture(JSON.stringify({ schemaVersion: "weird", trends: [] }), (path) => {
assert.equal(loadStore(path).schemaVersion, 2);
});
});
// ── GREEN-only regression guards: old code already returns the current version ──
test("regression guard: a v2 store loads as v2, idempotent (records + publishedAt intact)", () => {
const v2 = JSON.stringify({
schemaVersion: 2,
trends: [
{
id: "x",
title: "T",
url: "https://example.com/t",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["ai"],
publishedAt: "2026-05-30",
},
],
});
withFixture(v2, (path) => {
const s = loadStore(path);
assert.equal(s.schemaVersion, 2);
assert.equal(s.trends[0].publishedAt, "2026-05-30");
});
});
test("regression guard: a store with no schemaVersion is stamped to the current version", () => {
const noVer = JSON.stringify({
trends: [
{
id: "x",
title: "T",
url: "https://example.com/t",
source: "tavily",
capturedAt: "2026-06-01",
topics: ["ai"],
},
],
});
withFixture(noVer, (path) => {
assert.equal(loadStore(path).schemaVersion, SCHEMA_VERSION);
});
});
});
});