R3 slice 1 (research-deepening). Stop discarding the relevance judgment the
trend-spotter already computes: persist a 4-field TrendScore {mode, dimensions,
composite, priority} on TrendRecord (schema v2->v3, additive lossless migrate),
computed by the existing score.ts composite()+band() (one owner, no new arithmetic),
threaded item->store; then rankForBrief sorts each bucket composite-first (sentinel
-1 for unscored) and renderBrief surfaces "· <priority> (<mode>)" per body entry
(briefSummary shows the band only). First-sight only; mode-blind ranking with the mode
shown so the operator can disambiguate instruments.
- score.ts: TrendScore + requiredDimensions(mode) (ordered) + scoreEnvelope (composes
composite+band; throws on bad dim by contract)
- types.ts: SCHEMA_VERSION 2->3; TrendRecord.score?
- store.ts: TrendInput.score?; addTrend persists first-sight (duplicate keeps it);
migrate comment v1->v2->v3 (logic unchanged, JSON.stringify preserves the field)
- item.ts: TrendItem.score?; normalizeItem validates (non-array score/dimensions + the
mode's five dims in [1,10]) -> structured error never throw, carries validated dims;
itemToInput -> scoreEnvelope (no throw on the capture path; direct call throws by contract)
- brief.ts: composite-primary comparator; band+mode render; exact ranking: descriptor
- cli.ts: capture persists score via itemToInput (doc-only); add/score paths unchanged
- agents/trend-spotter.md Step 4.5: capture batch carries the Step-2 dimensions
- gate: TRENDS_TESTS_FLOOR 104->146; new unconditional Section 16j; ASSERT floor 94->99
Tests: trends 146/146 (RED two-phase: logic-RED store/brief/cli; stub-first then
assertion-RED score/item). Gate green (Passed 114 / Failed 0; 113 checks >= 99).
Hook suite 139/139 untouched. Counts 27/19/29 unchanged. No new source file/agent/command.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VmHCQjJHUyWwxGAVVjNLgp
667 lines
23 KiB
TypeScript
667 lines
23 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");
|
|
});
|
|
|
|
// ── RE-R3a: relevance score first-sight persistence (SC3) ──
|
|
const kortScore = {
|
|
mode: "kortform" as const,
|
|
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
|
|
composite: 8.1,
|
|
priority: "Immediate" as const,
|
|
};
|
|
|
|
test("RED: persists score on a new record when present (first-sight)", () => {
|
|
const res = addTrend(emptyStore(), {
|
|
title: "Scored trend",
|
|
url: "https://example.com/sc",
|
|
source: "tavily",
|
|
capturedAt: "2026-06-24",
|
|
topics: ["ai"],
|
|
score: kortScore,
|
|
});
|
|
assert.deepEqual(res.store.trends[0].score, kortScore);
|
|
});
|
|
|
|
test("regression guard: omits score when absent (no undefined-valued key)", () => {
|
|
const res = addTrend(emptyStore(), {
|
|
title: "Unscored trend",
|
|
url: "https://example.com/us",
|
|
source: "tavily",
|
|
capturedAt: "2026-06-24",
|
|
topics: ["ai"],
|
|
});
|
|
assert.equal("score" in res.store.trends[0], false);
|
|
});
|
|
|
|
test("RED: re-capture keeps the first sighting's score (no overwrite), unions topics", () => {
|
|
let store = emptyStore();
|
|
store = addTrend(store, {
|
|
title: "Same scored trend",
|
|
url: "https://example.com/ssc",
|
|
source: "tavily",
|
|
capturedAt: "2026-06-01",
|
|
topics: ["a"],
|
|
score: kortScore,
|
|
}).store;
|
|
const lowerScore = {
|
|
mode: "kortform" as const,
|
|
dimensions: { pillar: 6, audience: 5, timing: 6, angle: 5, authority: 5 },
|
|
composite: 5.6,
|
|
priority: "Medium" as const,
|
|
};
|
|
const res2 = addTrend(store, {
|
|
title: "Same scored trend",
|
|
url: "https://example.com/ssc",
|
|
source: "gemini",
|
|
capturedAt: "2026-06-20",
|
|
topics: ["a", "b"],
|
|
score: lowerScore,
|
|
});
|
|
assert.equal(res2.added, false);
|
|
assert.equal(res2.merged, true);
|
|
assert.deepEqual(res2.store.trends[0].score, kortScore, "first-sight score kept (D3)");
|
|
assert.deepEqual([...res2.store.trends[0].topics].sort(), ["a", "b"]);
|
|
});
|
|
});
|
|
|
|
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, SCHEMA_VERSION, "v1 store must migrate to the current version");
|
|
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 the current schemaVersion to disk", () => {
|
|
withFixture(JSON.stringify({ schemaVersion: 1, trends: [] }), (path) => {
|
|
saveStore(path, loadStore(path));
|
|
const onDisk = JSON.parse(readFileSync(path, "utf8"));
|
|
assert.equal(onDisk.schemaVersion, SCHEMA_VERSION);
|
|
});
|
|
});
|
|
|
|
test("RED: a non-numeric schemaVersion is coerced to the current version (old code passes the string through)", () => {
|
|
withFixture(JSON.stringify({ schemaVersion: "weird", trends: [] }), (path) => {
|
|
assert.equal(loadStore(path).schemaVersion, SCHEMA_VERSION);
|
|
});
|
|
});
|
|
|
|
// ── GREEN-only regression guards: old code already returns the current version ──
|
|
test("regression guard: a v2 store loads 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, SCHEMA_VERSION);
|
|
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);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("schema migration (RE-R3a / score v2→v3)", () => {
|
|
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: pre-bump SCHEMA_VERSION=2 → loadStore(v2).schemaVersion===2 ≠ 3 ──
|
|
test("RED: a v2 store (no score) loads stamped as v3, records intact, no score invented", () => {
|
|
const v2 = JSON.stringify({
|
|
schemaVersion: 2,
|
|
trends: [
|
|
{
|
|
id: "abc123",
|
|
title: "Old dated trend",
|
|
url: "https://example.com/o",
|
|
source: "tavily",
|
|
capturedAt: "2026-05-01",
|
|
topics: ["ai"],
|
|
publishedAt: "2026-04-30",
|
|
},
|
|
],
|
|
});
|
|
withFixture(v2, (path) => {
|
|
const s = loadStore(path);
|
|
assert.equal(s.schemaVersion, 3, "v2 store must migrate to v3");
|
|
assert.equal(s.trends.length, 1);
|
|
assert.equal(s.trends[0].title, "Old dated trend");
|
|
assert.equal(s.trends[0].capturedAt, "2026-05-01");
|
|
assert.equal(s.trends[0].publishedAt, "2026-04-30");
|
|
assert.deepEqual(s.trends[0].topics, ["ai"]);
|
|
assert.equal("score" in s.trends[0], false, "migration must not invent a score");
|
|
});
|
|
});
|
|
|
|
test("RED: round-trip loadStore→saveStore writes schemaVersion:3 to disk", () => {
|
|
withFixture(JSON.stringify({ schemaVersion: 2, trends: [] }), (path) => {
|
|
saveStore(path, loadStore(path));
|
|
const onDisk = JSON.parse(readFileSync(path, "utf8"));
|
|
assert.equal(onDisk.schemaVersion, 3);
|
|
});
|
|
});
|
|
|
|
test("RED: a v3 store with score on records loads idempotent", () => {
|
|
const v3 = JSON.stringify({
|
|
schemaVersion: 3,
|
|
trends: [
|
|
{
|
|
id: "x",
|
|
title: "Scored",
|
|
url: "https://example.com/s",
|
|
source: "tavily",
|
|
capturedAt: "2026-06-01",
|
|
topics: ["ai"],
|
|
score: {
|
|
mode: "kortform",
|
|
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
|
|
composite: 8.1,
|
|
priority: "Immediate",
|
|
},
|
|
},
|
|
],
|
|
});
|
|
withFixture(v3, (path) => {
|
|
const s = loadStore(path);
|
|
assert.equal(s.schemaVersion, 3);
|
|
assert.deepEqual(s.trends[0].score, {
|
|
mode: "kortform",
|
|
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
|
|
composite: 8.1,
|
|
priority: "Immediate",
|
|
});
|
|
});
|
|
});
|
|
|
|
test("RED: a v3 store's score survives load → save → load (field preservation)", () => {
|
|
const score = {
|
|
mode: "kortform",
|
|
dimensions: { pillar: 9, audience: 8, timing: 9, angle: 7, authority: 6 },
|
|
composite: 8.1,
|
|
priority: "Immediate",
|
|
};
|
|
const v3 = JSON.stringify({
|
|
schemaVersion: 3,
|
|
trends: [
|
|
{
|
|
id: "x",
|
|
title: "Survivor",
|
|
url: "https://example.com/sv",
|
|
source: "tavily",
|
|
capturedAt: "2026-06-01",
|
|
topics: ["ai"],
|
|
score,
|
|
},
|
|
],
|
|
});
|
|
withFixture(v3, (path) => {
|
|
const first = loadStore(path);
|
|
saveStore(path, first);
|
|
const second = loadStore(path);
|
|
assert.deepEqual(second.trends[0].score, score, "score must survive a load+resave (no field stripping)");
|
|
});
|
|
});
|
|
});
|
|
});
|