| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { sameProjectSession, projectSessionExcluded, projectSessionIdentity } from "../lib/projectSessionIdentity"; |
| 4 | import { mergeProjectTopicPage, mergeIncompleteProjectTopicPage, projectTreeWithoutTopic, projectTreeWithoutTopics } from "../lib/projectTreeTopic"; |
| 5 | import { createSessionLifecycleFences } from "../lib/sessionLifecycleFences"; |
| 6 | import { projectTreeApplyRuntimeTopics } from "../lib/projectTreeRuntime"; |
| 7 | import type { ProjectNode } from "../lib/types"; |
| 8 | |
| 9 | const alias = "path\0/shared.jsonl"; |
| 10 | const row = (id: string, hostId = "local"): ProjectNode => ({ key: id, kind: "topic", label: id, |
| 11 | session: { hostId, sessionId: id }, identityAliases: [alias], lifecycleGeneration: 10 }); |
| 12 | const a = row("original"), b = row("reviewed"); |
| 13 | const source: ProjectNode = { key: "source", kind: "topic", label: "Source", sessionPath: "/shared.jsonl" }; |
| 14 | const ids = (rows: ProjectNode[]) => rows.map(projectSessionIdentity); |
| 15 | |
| 16 | test("canonical identities survive colliding source aliases across pages and hosts", () => { |
| 17 | for (const sibling of [b, row("original", "other-host")]) { |
| 18 | assert.equal(sameProjectSession(a, sibling), false); |
| 19 | assert.deepEqual(ids(mergeProjectTopicPage([a], [sibling], true)), ids([a, sibling])); |
| 20 | assert.deepEqual(ids(mergeIncompleteProjectTopicPage([a], [sibling])), ids([a, sibling])); |
| 21 | assert.deepEqual(ids(mergeProjectTopicPage([{ ...a, pinned: true }], [sibling], false)), ids([sibling, a])); |
| 22 | } |
| 23 | }); |
| 24 | |
| 25 | test("unique source adoption survives stale pages but ambiguous sources do not pick an owner", () => { |
| 26 | assert.deepEqual(ids(mergeProjectTopicPage([source], [a], true)), ids([a])); |
| 27 | assert.deepEqual(mergeProjectTopicPage([a], [source], true), [a]); |
| 28 | assert.deepEqual(ids(mergeProjectTopicPage([a, b], [source], true)), ids([a, b, source])); |
| 29 | assert.deepEqual(ids(mergeProjectTopicPage([], [source, a], false)), ids([a])); |
| 30 | }); |
| 31 | |
| 32 | test("runtime projection cannot attach a shared source to an arbitrary canonical owner", () => { |
| 33 | const tree: ProjectNode[] = [{ key: "p", kind: "project", root: "/repo", label: "Project", children: [a, b, source] }]; |
| 34 | const projected = projectTreeApplyRuntimeTopics(tree, [ |
| 35 | { scope: "project", workspaceRoot: "/repo", node: { ...a, running: true } }, |
| 36 | { scope: "project", workspaceRoot: "/repo", node: { ...b, running: false } }, |
| 37 | ])[0].children!; |
| 38 | assert.deepEqual(ids(projected), ids([a, b, source])); |
| 39 | assert.deepEqual(projected.map(node => node.running), [true, false, undefined]); |
| 40 | }); |
| 41 | |
| 42 | test("archiving and restoring one owner cannot hide or restore a different owner", () => { |
| 43 | const fences = createSessionLifecycleFences(); |
| 44 | fences.archive(a, { lifecycleGeneration: 10, operationId: "archive-a" }); |
| 45 | assert.equal(projectSessionExcluded(a, fences.keys()), true); |
| 46 | assert.equal(projectSessionExcluded(b, fences.keys()), false); |
| 47 | assert.equal(projectSessionExcluded(source, fences.keys()), true); |
| 48 | fences.observeDirectory([{ ...b, lifecycleGeneration: 999 }, { ...source, lifecycleGeneration: 999 }]); |
| 49 | assert.equal(projectSessionExcluded(a, fences.keys()), true); |
| 50 | fences.archive(b, { lifecycleGeneration: 20, operationId: "archive-b" }); |
| 51 | fences.observeDirectory([{ ...a, lifecycleGeneration: 11 }]); |
| 52 | assert.equal(projectSessionExcluded(a, fences.keys()), false); |
| 53 | assert.equal(projectSessionExcluded(b, fences.keys()), true); |
| 54 | assert.equal(projectSessionExcluded(source, fences.keys()), true, "another archive still owns the shared alias"); |
| 55 | fences.observeDirectory([{ ...b, lifecycleGeneration: 21 }]); |
| 56 | assert.equal(fences.keys().size, 0); |
| 57 | }); |
| 58 | |
| 59 | test("a committed topic archive removes and fences its canonical row", () => { |
| 60 | const topic: ProjectNode = { ...a, topicId: "topic-a" }; |
| 61 | const tree: ProjectNode[] = [{ key: "p", kind: "project", root: "/repo", label: "Project", children: [topic, b] }]; |
| 62 | assert.deepEqual(ids(projectTreeWithoutTopic(tree, "topic-a")[0].children!), ids([b])); |
| 63 | const tombstones = new Set(["topic-a"]); |
| 64 | assert.deepEqual(ids(projectTreeWithoutTopics(tree, tombstones)[0].children!), ids([b])); |
| 65 | const projected = projectTreeApplyRuntimeTopics([{ ...tree[0], children: [b] }], [ |
| 66 | { scope: "project", workspaceRoot: "/repo", node: { ...topic, running: true } }, |
| 67 | ], tombstones)[0].children!; |
| 68 | assert.deepEqual(ids(projected), ids([b])); |
| 69 | }); |
| 70 | |
| 71 | test("source archive receipts retain independent canonical owners through restoration", () => { |
| 72 | const fences = createSessionLifecycleFences(); |
| 73 | fences.archive(source, { lifecycleGeneration: 10, operationId: "archive-source", |
| 74 | identityAliases: [projectSessionIdentity(a), projectSessionIdentity(b)] }); |
| 75 | fences.observeDirectory([{ ...row("unrelated"), lifecycleGeneration: 999 }]); |
| 76 | assert.equal(projectSessionExcluded(a, fences.keys()), true); |
| 77 | assert.equal(projectSessionExcluded(b, fences.keys()), true); |
| 78 | fences.observeDirectory([{ ...a, lifecycleGeneration: 11 }]); |
| 79 | assert.equal(projectSessionExcluded(a, fences.keys()), false); |
| 80 | assert.equal(projectSessionExcluded(b, fences.keys()), true); |
| 81 | assert.equal(projectSessionExcluded(source, fences.keys()), true); |
| 82 | }); |
| 83 |