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
This commit is contained in:
Kjell Tore Guttormsen 2026-06-24 11:12:50 +02:00
commit 7a158030b6
10 changed files with 465 additions and 31 deletions

View file

@ -2,6 +2,9 @@ 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 } 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.
@ -67,4 +70,92 @@ describe("trends CLI — normalize/score subcommands (RE-R1 / Step 4)", () => {
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);
});
});
});

View file

@ -1,7 +1,7 @@
import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { normalizeItem, normalizeItems } from "../src/item.js";
import { normalizeItem, normalizeItems, itemToInput } from "../src/item.js";
import { normalizeField } from "../src/store.js";
describe("trends item normalizer (RE-R1 / B1)", () => {
@ -179,4 +179,49 @@ describe("trends item normalizer (RE-R1 / B1)", () => {
assert.deepEqual(errors, []);
});
});
describe("itemToInput — bridge to the store input (RE-R2a / B-bridge)", () => {
// Build a realistic envelope via the normalizer (the actual upstream path).
const mkItem = (extra: Record<string, unknown> = {}) => {
const r = normalizeItem({
source: "tavily",
title: "OpenAI ships a reasoning model",
url: "https://example.com/Article",
topics: ["ai", "reasoning"],
summary: "A short summary.",
...extra,
});
assert.equal(r.ok, true);
if (!r.ok) throw new Error("fixture item failed to normalize");
return r.item;
};
test("injects capturedAt and carries source/title/url/topics/summary/publishedAt verbatim", () => {
const input = itemToInput(mkItem({ publishedAt: "2026-06-20" }), "2026-06-24");
assert.equal(input.capturedAt, "2026-06-24");
assert.equal(input.source, "tavily");
assert.equal(input.title, "OpenAI ships a reasoning model");
assert.equal(input.url, "https://example.com/Article");
assert.deepEqual(input.topics, ["ai", "reasoning"]);
assert.equal(input.summary, "A short summary.");
assert.equal(input.publishedAt, "2026-06-20");
});
test("derives NO id (the store owns id via addTrend->trendId)", () => {
const input = itemToInput(mkItem(), "2026-06-24") as Record<string, unknown>;
assert.equal("id" in input, false);
});
test("an item without publishedAt -> input omits the key (not undefined-valued)", () => {
const input = itemToInput(mkItem(), "2026-06-24"); // mkItem carries no publishedAt
assert.equal("publishedAt" in input, false);
});
test("field-confusion guard: capturedAt (injected) is distinct from publishedAt (carried)", () => {
const input = itemToInput(mkItem({ publishedAt: "2026-06-20" }), "2026-06-24");
assert.notEqual(input.capturedAt, input.publishedAt);
assert.equal(input.publishedAt, "2026-06-20");
assert.equal(input.capturedAt, "2026-06-24");
});
});
});

View file

@ -1,6 +1,6 @@
import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, existsSync } from "node:fs";
import { mkdtempSync, rmSync, existsSync, writeFileSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
@ -206,6 +206,75 @@ describe("trends store", () => {
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", () => {
@ -330,4 +399,98 @@ describe("trends 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);
});
});
});
});