| 1 | import assert from "node:assert/strict"; |
| 2 | import { inspect, remove, markTopic, trash, recover } from "../lib/topicRemovalMock"; |
| 3 | import { makeMockSessionLifecycleBindings } from "../lib/sessionLifecycleBindings"; |
| 4 | import type { ProjectNode } from "../lib/types"; |
| 5 | |
| 6 | const tree: ProjectNode[] = [{ key: "global", kind: "global_folder", label: "Global", children: [ |
| 7 | { key: "empty", topicId: "empty", kind: "global_topic", label: "New conversation" }, |
| 8 | { key: "named", topicId: "named", kind: "global_topic", label: "My plan" }, |
| 9 | ] }]; |
| 10 | markTopic(tree, "empty", true); |
| 11 | const target = { workspaceId: "global", topicId: "empty" }; |
| 12 | let view = inspect(tree, target); |
| 13 | assert.equal(view.disposition, "discard_placeholder"); |
| 14 | assert.ok(remove(tree, { operationId: "empty", target, expectedToken: view.token }).committed); |
| 15 | assert.equal(trash(tree).items.length, 0); |
| 16 | view = inspect(tree, { ...target, topicId: "named" }); |
| 17 | const request = { operationId: "named", target: view.target, expectedToken: view.token }; |
| 18 | tree[0].children![0].label = "Renamed"; |
| 19 | assert.equal(remove(tree, request).errorCode, "state_conflict"); |
| 20 | view = inspect(tree, request.target); |
| 21 | request.expectedToken = view.token; |
| 22 | const result = remove(tree, request); |
| 23 | assert.ok(result.committed); |
| 24 | assert.equal(trash(tree).items[0].canPreview, false); |
| 25 | recover(tree, result.recoveryEntryId!, "global", "restore"); |
| 26 | assert.equal(trash(tree).items.length, 0); |
| 27 | assert.equal(tree[0].children![0].label, "Renamed"); |
| 28 | assert.deepEqual(remove(tree, request), result); |
| 29 | assert.equal(tree[0].children!.length, 1); |
| 30 | markTopic(tree, "named", false); |
| 31 | tree[0].children![0].label = "New conversation"; |
| 32 | assert.equal(inspect(tree, request.target).disposition, "archive_placeholder"); |
| 33 | const bindings = { ...makeMockSessionLifecycleBindings( |
| 34 | () => { throw new Error("topic removal must use its own project tree"); }, |
| 35 | new Set(), new Set(), () => {}, tree, |
| 36 | ) }; |
| 37 | assert.equal((await bindings.InspectTopicRemoval(request.target)).disposition, "archive_placeholder"); |
| 38 | assert.deepEqual(await bindings.RemoveTopic(request), result, "lazy bridge retains the same removal receipt"); |
| 39 | assert.equal(tree[0].children!.length, 1, "lazy duplicate cannot remove the restored topic"); |
| 40 | console.log("PASS mock placeholder classification, recovery and idempotency"); |
| 41 |