| 1 | import assert from "node:assert/strict"; |
| 2 | import React, { act } from "react"; |
| 3 | import { createRoot } from "react-dom/client"; |
| 4 | import { JSDOM } from "jsdom"; |
| 5 | import { projectSessionRowKey } from "../lib/projectSessionIdentity"; |
| 6 | import type { ProjectNode } from "../lib/types"; |
| 7 | |
| 8 | const dom = new JSDOM("<div id='root'></div>"); |
| 9 | Object.assign(globalThis, { window: dom.window, document: dom.window.document, IS_REACT_ACT_ENVIRONMENT: true }); |
| 10 | const source: ProjectNode = { key: "source", kind: "global_topic", label: "History", |
| 11 | source: { hostId: "local", sourceKey: "row-identity-source", path: "/history.jsonl" } }; |
| 12 | const sourceKey = projectSessionRowKey(source); |
| 13 | const original: ProjectNode = { key: "original", kind: "global_topic", label: "History", |
| 14 | session: { hostId: "local", sessionId: "row-original" }, identityAliases: [sourceKey] }; |
| 15 | const reviewed: ProjectNode = { ...original, key: "reviewed", |
| 16 | session: { hostId: "local", sessionId: "row-reviewed" } }; |
| 17 | assert.equal(projectSessionRowKey(original), sourceKey, "adoption preserves the mounted source row"); |
| 18 | assert.notEqual(projectSessionRowKey({ ...source, |
| 19 | source: { ...source.source!, sourceKey: "unowned-source" }, identityAliases: [sourceKey] }), sourceKey, |
| 20 | "an unowned source cannot borrow an already canonical key through a secondary alias"); |
| 21 | const root = createRoot(document.getElementById("root")!); |
| 22 | const warnings: unknown[][] = []; |
| 23 | const oldError = console.error; |
| 24 | console.error = (...args) => { warnings.push(args); }; |
| 25 | try { |
| 26 | // Old peers/previously loaded pages may still publish a conflicting alias. |
| 27 | // Reorder siblings and insert/remove a third row as catalog pushes do. |
| 28 | const other: ProjectNode = { key: "other", kind: "global_topic", label: "Other", |
| 29 | session: { hostId: "local", sessionId: "row-other" } }; |
| 30 | for (let cycle = 0; cycle < 20; cycle++) { |
| 31 | const rows = cycle % 2 ? [source, other, reviewed, original] : [original, reviewed]; |
| 32 | await act(async () => root.render(<ul>{rows.map(row => |
| 33 | <li key={projectSessionRowKey(row)} data-session={row.session?.sessionId ?? "source"}>{row.label}</li>)}</ul>)); |
| 34 | assert.deepEqual(Array.from(document.querySelectorAll("li"), row => row.getAttribute("data-session")), |
| 35 | rows.map(row => row.session?.sessionId ?? "source"), "catalog refresh must not multiply mounted rows"); |
| 36 | } |
| 37 | assert.notEqual(projectSessionRowKey(original), projectSessionRowKey(reviewed), "canonical sessions never share a React key"); |
| 38 | assert.equal(warnings.length, 0, "no duplicate-key or reconciliation warnings"); |
| 39 | } finally { |
| 40 | console.error = oldError; |
| 41 | await act(async () => root.unmount()); |
| 42 | dom.window.close(); |
| 43 | } |
| 44 | console.log("PASS source adoption preserves identity without merging independent canonical rows"); |
| 45 |