|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { execFileSync } from "node:child_process"; |
| 5 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | +const repoRoot = path.resolve(__dirname, "..", ".."); |
| 10 | +const jsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 11 | +const resultsPath = path.join(repoRoot, "tmp", "v2-merged-recent-session-registration-results.json"); |
| 12 | + |
| 13 | +function checkSyntax(filePath) { |
| 14 | + try { |
| 15 | + execFileSync(process.execPath, ["--check", filePath], { |
| 16 | + cwd: repoRoot, |
| 17 | + stdio: ["ignore", "pipe", "pipe"] |
| 18 | + }); |
| 19 | + return { ok: true, error: "" }; |
| 20 | + } catch (error) { |
| 21 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function createMergedHostContextId(toolId, timestamp, shortId) { |
| 26 | + return `${toolId}-merged-${timestamp}-${shortId}`; |
| 27 | +} |
| 28 | + |
| 29 | +function registerMergedResult(state, toolId, mergedPayload, timestamp, shortId) { |
| 30 | + const next = { |
| 31 | + ...state, |
| 32 | + sessionStorage: { ...state.sessionStorage }, |
| 33 | + recent: [...state.recent], |
| 34 | + library: { ...state.library } |
| 35 | + }; |
| 36 | + const hostContextId = createMergedHostContextId(toolId, timestamp, shortId); |
| 37 | + const mergedResultPayload = { |
| 38 | + ...mergedPayload, |
| 39 | + version: "v2", |
| 40 | + toolId, |
| 41 | + mergeResultMeta: { |
| 42 | + isMergedResult: true, |
| 43 | + sourceSessionContextId: "source", |
| 44 | + targetSessionContextId: "target", |
| 45 | + mergedAt: "2026-05-02T00:00:00.000Z" |
| 46 | + } |
| 47 | + }; |
| 48 | + next.sessionStorage[hostContextId] = JSON.stringify(mergedResultPayload); |
| 49 | + next.recent = next.recent.filter((entry) => entry.hostContextId !== hostContextId); |
| 50 | + next.recent.unshift({ |
| 51 | + hostContextId, |
| 52 | + tool: toolId, |
| 53 | + timestamp: "2026-05-02T00:00:00.000Z", |
| 54 | + payload: mergedResultPayload |
| 55 | + }); |
| 56 | + return { next, hostContextId, mergedResultPayload }; |
| 57 | +} |
| 58 | + |
| 59 | +export function run() { |
| 60 | + const failures = []; |
| 61 | + const jsExists = fs.existsSync(jsPath); |
| 62 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 63 | + const jsSyntax = checkSyntax(jsPath); |
| 64 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2MergedRecentSessionRegistration.test.mjs")); |
| 65 | + |
| 66 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 67 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 68 | + if (!testSyntax.ok) failures.push("tests/runtime/V2MergedRecentSessionRegistration.test.mjs failed syntax check."); |
| 69 | + |
| 70 | + const requiredTokens = [ |
| 71 | + "createMergedHostContextId(toolId)", |
| 72 | + "-merged-", |
| 73 | + "mergeResultMeta", |
| 74 | + "isMergedResult: true", |
| 75 | + "this.addRecentSessionEntry(hostContextId, this.pendingMergePreview.selectedToolId, appliedPayload);", |
| 76 | + "const titleToolLabel = isMergedResult ? `${entry.tool} (merged)` : entry.tool;" |
| 77 | + ]; |
| 78 | + requiredTokens.forEach((token) => { |
| 79 | + if (!js.includes(token)) failures.push(`Missing merged recent-session token: ${token}`); |
| 80 | + }); |
| 81 | + |
| 82 | + const baseState = { |
| 83 | + sessionStorage: {}, |
| 84 | + recent: [ |
| 85 | + { hostContextId: "asset-browser-v2-123", tool: "asset-browser-v2", timestamp: "2026-05-01T00:00:00.000Z", payload: { version: "v2", toolId: "asset-browser-v2" } } |
| 86 | + ], |
| 87 | + library: {} |
| 88 | + }; |
| 89 | + const mergedPayload = { payloadJson: { combined: true } }; |
| 90 | + const { next, hostContextId, mergedResultPayload } = registerMergedResult(baseState, "asset-browser-v2", mergedPayload, 1777777777777, "abc123xy"); |
| 91 | + |
| 92 | + if (!hostContextId.startsWith("asset-browser-v2-merged-1777777777777-")) { |
| 93 | + failures.push("Merged hostContextId format is incorrect."); |
| 94 | + } |
| 95 | + if (!Object.prototype.hasOwnProperty.call(next.sessionStorage, hostContextId)) { |
| 96 | + failures.push("Merged result was not registered in sessionStorage."); |
| 97 | + } |
| 98 | + if (!next.recent.length || next.recent[0].hostContextId !== hostContextId) { |
| 99 | + failures.push("Merged result should appear at the top of Recent Sessions."); |
| 100 | + } |
| 101 | + if (!next.recent[0].payload.mergeResultMeta || next.recent[0].payload.mergeResultMeta.isMergedResult !== true) { |
| 102 | + failures.push("Merged recent payload is missing merged-result metadata."); |
| 103 | + } |
| 104 | + if (!next.recent[0].payload.version || next.recent[0].payload.version !== "v2") { |
| 105 | + failures.push("Merged recent payload must include version v2."); |
| 106 | + } |
| 107 | + if (!next.recent[0].payload.toolId || next.recent[0].payload.toolId !== "asset-browser-v2") { |
| 108 | + failures.push("Merged recent payload must include toolId."); |
| 109 | + } |
| 110 | + |
| 111 | + const reopenedPayload = JSON.parse(next.sessionStorage[hostContextId]); |
| 112 | + if (JSON.stringify(reopenedPayload) !== JSON.stringify(mergedResultPayload)) { |
| 113 | + failures.push("Merged session reopen payload mismatch."); |
| 114 | + } |
| 115 | + |
| 116 | + const diffMergeSelectable = next.recent.some((entry) => entry.hostContextId === hostContextId); |
| 117 | + if (!diffMergeSelectable) { |
| 118 | + failures.push("Merged recent session is not selectable for Diff/Merge."); |
| 119 | + } |
| 120 | + |
| 121 | + const afterDelete = { ...next, recent: next.recent.filter((entry) => entry.hostContextId !== hostContextId), sessionStorage: { ...next.sessionStorage } }; |
| 122 | + delete afterDelete.sessionStorage[hostContextId]; |
| 123 | + if (afterDelete.recent.some((entry) => entry.hostContextId === hostContextId)) { |
| 124 | + failures.push("Deleting merged recent session should remove recent entry."); |
| 125 | + } |
| 126 | + if (Object.prototype.hasOwnProperty.call(afterDelete.sessionStorage, hostContextId)) { |
| 127 | + failures.push("Deleting merged recent session should remove sessionStorage payload."); |
| 128 | + } |
| 129 | + |
| 130 | + if (Object.keys(next.library).length !== 0) { |
| 131 | + failures.push("Session Library should remain unchanged unless explicitly saved."); |
| 132 | + } |
| 133 | + |
| 134 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 135 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 136 | + generatedAt: new Date().toISOString(), |
| 137 | + failures, |
| 138 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 139 | + scenarios: { hostContextId, recentTop: next.recent[0], librarySize: Object.keys(next.library).length } |
| 140 | + }, null, 2)}\n`, "utf8"); |
| 141 | + |
| 142 | + console.log(`v2 merged-recent-session-registration results: ${resultsPath}`); |
| 143 | + assert.equal(failures.length, 0, `V2 merged-recent-session-registration failures: ${failures.join(" | ")}`); |
| 144 | + return { failures }; |
| 145 | +} |
| 146 | + |
| 147 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 148 | + try { |
| 149 | + const summary = run(); |
| 150 | + console.log(JSON.stringify(summary, null, 2)); |
| 151 | + } catch (error) { |
| 152 | + console.error(error); |
| 153 | + process.exitCode = 1; |
| 154 | + } |
| 155 | +} |
| 156 | + |
0 commit comments