import { APP_URL } from "../../src/config/brand"; import assert from "node:assert/strict"; import { buildKnowledgeMap, type MapInputs } from "../../src/lib/knowledge-map/build"; import { connections, constellation, neighborhood, projectHandoff, } from "../../src/lib/knowledge-map/model"; import { mapConnectionFacts } from "../../src/lib/knowledge-map/facts"; import { planRetrieval } from "../../src/lib/agent/plan"; const at = new Date("2026-11-15T00:00:00Z"); const input: MapInputs = { projects: [ { id: "p", userId: "alice", entityProjectId: "e", name: "alpha", description: "A project", gitUrl: "https://github.com/example/alpha?token=SECRET-TOKEN", liveUrl: "https://user:SECRET-TOKEN@example.com", updatedAt: at, devLog: [ { date: "2026-11-15", done: "Earlier same-day report", next: "Superseded next step", tests: "Old report", todos: "", health: "", }, { date: "2026-11-15", done: "Built", next: "Verify", tests: "Reported pass", todos: "", health: "", }, ], }, { id: "other", userId: "bob", entityProjectId: "foreign", name: "Secret", description: "PRIVATE", updatedAt: at, devLog: null, }, ], things: [ { id: "e", userId: "alice", name: "alpha", title: "Alpha", type: "project", description: "Purpose", updatedAt: at, }, { id: "person", userId: "alice", name: "Private contact", title: null, type: "person", description: "Personal detail", updatedAt: at, }, { id: "foreign", userId: "bob", name: "Secret", title: null, type: "project", description: "PRIVATE", updatedAt: at, }, ], relations: [ { id: "good", userId: "alice", fromEntityId: "e", toEntityId: "person", type: "knows", updatedAt: at, }, { id: "bad", userId: "alice", fromEntityId: "e", toEntityId: "foreign", type: "leak", updatedAt: at, }, { id: "wrong-owner", userId: "bob", fromEntityId: "e", toEntityId: "person", type: "private", updatedAt: at, }, ], goals: [ { id: "g", userId: "alice", entityId: "e", title: "Launch", description: "Ship", status: "active", progress: 20, updatedAt: at, }, { id: "bad-goal", userId: "alice", entityId: "foreign", title: "Secret goal", description: null, status: "active", progress: 0, updatedAt: at, }, ], packages: [ { projectId: "p", userId: "alice", packageName: "@example/kit", version: "1.0.0", observedAt: at, }, { projectId: "other", userId: "alice", packageName: "secret-kit", version: null, observedAt: at, }, ], }; // Extra database fields cannot escape an explicit projection. Object.assign(input.projects[0], { resources: [{ kind: "credential", notes: "SECRET-TOKEN" }], notes: "secret operator notes", }); const map = buildKnowledgeMap("alice", input, [], at); assert.equal( map.nodes.filter((n) => n.kind === "project").length, 1, "deduplicates runtime project and entity", ); assert.equal( map.edges.length, 3, "drops both cross-owner endpoints and foreign relationship owners", ); assert(!JSON.stringify(map).includes("SECRET")); assert(!JSON.stringify(map).includes("foreign")); assert(!JSON.stringify(map).includes("secret operator")); assert.equal(map.nodes.find((n) => n.kind === "project")?.href, `${APP_URL}/projects/e`); const handoff = projectHandoff(map, "project:p"); assert(handoff.includes("Verify")); assert( !handoff.includes("Superseded next step"), "equal-date reports use append order, never an older handoff", ); assert(handoff.includes("Repository: https://github.com/example/alpha")); assert(!handoff.includes("SECRET-TOKEN")); assert(handoff.includes("not independently verified")); assert(handoff.includes("@example/kit")); assert(!handoff.includes("Private contact")); assert(!handoff.includes("Personal detail")); assert.throws(() => projectHandoff(map, "entity:person")); const malformed = buildKnowledgeMap( "alice", { ...input, projects: [{ ...input.projects[0], entityProjectId: "foreign" }] }, [], at, ); assert.equal( malformed.nodes.find((node) => node.id === "project:p")?.href, `${APP_URL}/projects`, "a foreign entity FK never becomes an open-project link", ); const facts = mapConnectionFacts(map, "Alpha connections"); assert.equal(facts.length, 3); assert(!JSON.stringify(facts).includes("Private contact")); assert(facts.slice(1).every((fact) => fact.fields.excerpt?.includes("not a permission grant"))); assert(planRetrieval("Show my knowledge map connections").sources.includes("projects")); assert.equal(neighborhood(map, "project:p", 1).nodes.length, 1); assert.equal(neighborhood(map, "project:p", 1).total, 3); assert.equal(connections(map, "package:@example/kit").length, 1); assert.deepEqual(constellation(map.nodes), constellation(map.nodes)); const bounded = buildKnowledgeMap("alice", input, ["Older records omitted"], at); assert(projectHandoff(bounded, "project:p").includes("Older records omitted")); console.log( "knowledge-map: ownership, canonical identity, narrow handoff, grounded agent context and bounds passed", );