linkedin-studio/scripts/brain/tests/fold.test.ts
Kjell Tore Guttormsen 8c927198f5 feat(linkedin-studio): second-brain SB-S0 foundation — brain/ scaffold + profile fold + id/provenance spine [skip-docs]
SB-S0 (Foundation) of the second-brain arc: a new TS package scripts/brain/
(structural copy of specifics-bank) establishing the spine later slices hang on.
TDD throughout (failing test first); fold scope = P1+P2 only per operator decision.

- types.ts: one provenance vocab (human|published|ai-draft) + the six-field
  ProfileFact record + two-layer ProfileDoc.
- id.ts: slugify + mintEntityId (sha256[:12], kind-namespaced, slug-keyed so the
  id is stable across value edits) + normalizeProvenance (throws on unknown). [SC4]
- profile.ts: no-YAML line-grammar parse/serialize (parse∘serialize = identity over
  the whole doc; values may contain ]/|/quotes) [SC2] + foldUserProfile: lossless,
  idempotent, source-absent-aware fold of config/user-profile.template.md. Pinned
  extraction — P1 labeled scalars (group-headers skipped, [placeholder]→empty) + P2
  expertise group; stops at "### Research Tooling" so deferred explainer prose can't
  leak in as fields. Checkbox-prefs (Goals/Tone/MCPs/Assets) deferred (§8). [SC3]
- dataRoot.ts: inline per-package resolver (repo idiom; no new seam → SC6).
- scaffold.ts/cli.ts: idempotent `brain init` — brain/{index,profile,operations}.md
  + journal/ + ingest/{inbox,published} under the data-dir; compare-then-skip, never
  clobbers a user edit. No session-start wiring (SB-S2 owns it). [SC1]

Gate: new BRAIN floor in test-runner.sh (34 tests; trends/specifics/contract floors
unchanged) + anti-erosion floor 74→75. Full gate 90/0/0, tsc --noEmit clean. [SC5/SC6]
No new command/agent/reference → no version bump, no structure-count change.

Refs docs/second-brain/{brief,plan-sb-s0,architecture}.md.

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

113 lines
5 KiB
TypeScript

import { describe, test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { foldUserProfile } from "../src/profile.js";
import { mintEntityId } from "../src/id.js";
import type { ProfileDoc } from "../src/types.js";
const here = dirname(fileURLToPath(import.meta.url));
const TEMPLATE = readFileSync(join(here, "../../../config/user-profile.template.md"), "utf8");
const POPULATED = readFileSync(join(here, "../fixtures/user-profile.populated.md"), "utf8");
const D1 = "2026-06-23";
const D2 = "2026-07-01";
/** Look a fact up by its source label via the canonical id. */
const byLabel = (doc: ProfileDoc, label: string) =>
doc.static.find((f) => f.id === mintEntityId({ kind: "profile-field", key: label }));
const FIELD_COUNT = 27; // 22 P1 labeled scalars + 5 P2 expertise areas
describe("foldUserProfile (SC3)", () => {
describe("SC3a — source-absent", () => {
const doc = foldUserProfile({ templateText: TEMPLATE, today: D1 });
test("lists every template field (22 P1 + 5 P2), no dynamic facts", () => {
assert.equal(doc.static.length, FIELD_COUNT);
assert.equal(doc.dynamic.length, 0);
});
test("placeholder fields fold to an empty value", () => {
assert.equal(byLabel(doc, "Name")?.value, "");
assert.equal(byLabel(doc, "Current Role")?.value, "");
assert.equal(byLabel(doc, "expertise-area-1")?.value, "");
assert.equal(byLabel(doc, "Follower count")?.value, "");
assert.equal(byLabel(doc, "Topics to AVOID")?.value, "");
});
test("the lone literal template field (Disclaimer) keeps its boilerplate", () => {
assert.equal(byLabel(doc, "Important Disclaimer")?.value.startsWith("All articles"), true);
});
test("deferred Research-Tooling prose does not leak in as fields", () => {
assert.equal(byLabel(doc, "Always-available floor (no MCP needed)"), undefined);
assert.equal(byLabel(doc, "Preferred order (optional)"), undefined);
});
test("every folded fact has the canonical shape", () => {
for (const f of doc.static) {
assert.match(f.id, /^[0-9a-f]{12}$/);
assert.equal(f.provenance, "human");
assert.equal(f.status, "active");
assert.equal(f.evidence_count, 0);
assert.equal(f.first_seen, D1);
assert.equal(f.last_seen, D1);
}
});
});
describe("SC3b — populated (lossless)", () => {
const doc = foldUserProfile({ templateText: TEMPLATE, instanceText: POPULATED, today: D1 });
test("same field-set size — fold neither drops nor invents fields", () => {
assert.equal(doc.static.length, FIELD_COUNT);
});
test("every filled value is carried (none dropped)", () => {
assert.equal(doc.static.every((f) => f.value !== ""), true);
});
test("specific filled values land on the right fields", () => {
assert.equal(byLabel(doc, "Name")?.value, "Jordan Avery");
assert.equal(byLabel(doc, "Current Role")?.value, "Senior Data Engineer");
assert.equal(byLabel(doc, "expertise-area-1")?.value, "Streaming data pipelines");
assert.equal(byLabel(doc, "expertise-area-5")?.value, "Team data literacy");
assert.equal(byLabel(doc, "Topics to AVOID")?.value, "Crypto");
assert.equal(byLabel(doc, "Follower count")?.value, "2,400");
assert.equal(byLabel(doc, "Phrases you commonly use")?.value, '"ship the boring thing"');
});
});
describe("SC3c — idempotent re-run", () => {
test("re-running source-absent over its own output changes nothing", () => {
const a = foldUserProfile({ templateText: TEMPLATE, today: D1 });
const a2 = foldUserProfile({ templateText: TEMPLATE, existing: a, today: D1 });
assert.deepEqual(a2, a);
});
test("re-running populated over its own output changes nothing (no dup)", () => {
const b = foldUserProfile({ templateText: TEMPLATE, instanceText: POPULATED, today: D1 });
const b2 = foldUserProfile({ templateText: TEMPLATE, instanceText: POPULATED, existing: b, today: D1 });
assert.deepEqual(b2, b);
assert.equal(b2.static.length, FIELD_COUNT);
});
test("a later run bumps last_seen but preserves first_seen and value", () => {
const b = foldUserProfile({ templateText: TEMPLATE, instanceText: POPULATED, today: D1 });
const later = foldUserProfile({ templateText: TEMPLATE, instanceText: POPULATED, existing: b, today: D2 });
const name = byLabel(later, "Name");
assert.equal(name?.first_seen, D1);
assert.equal(name?.last_seen, D2);
assert.equal(name?.value, "Jordan Avery");
});
test("re-folding source-absent over a populated existing never wipes a filled value", () => {
const b = foldUserProfile({ templateText: TEMPLATE, instanceText: POPULATED, today: D1 });
const over = foldUserProfile({ templateText: TEMPLATE, existing: b, today: D2 });
assert.equal(byLabel(over, "Name")?.value, "Jordan Avery");
});
});
});