|
| 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-merge-state-ssot-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 resolveAuthoritativeLastMergedHostContextId(lastMergedHostContextId, history, sessionStorageMap) { |
| 26 | + if (!lastMergedHostContextId) { |
| 27 | + return ""; |
| 28 | + } |
| 29 | + const mergedRecentEntry = history.find((entry) => entry.hostContextId === lastMergedHostContextId); |
| 30 | + const existsInRecent = Boolean(mergedRecentEntry); |
| 31 | + const existsInSessionStorage = Object.prototype.hasOwnProperty.call(sessionStorageMap, lastMergedHostContextId); |
| 32 | + const existsAsMergedRecent = Boolean( |
| 33 | + mergedRecentEntry && |
| 34 | + mergedRecentEntry.payload && |
| 35 | + typeof mergedRecentEntry.payload === "object" && |
| 36 | + !Array.isArray(mergedRecentEntry.payload) && |
| 37 | + mergedRecentEntry.payload.mergeResultMeta && |
| 38 | + typeof mergedRecentEntry.payload.mergeResultMeta === "object" && |
| 39 | + mergedRecentEntry.payload.mergeResultMeta.isMergedResult === true |
| 40 | + ); |
| 41 | + return existsInRecent && existsInSessionStorage && existsAsMergedRecent ? lastMergedHostContextId : ""; |
| 42 | +} |
| 43 | + |
| 44 | +function undoEnabled(lastMergedHostContextId, history, sessionStorageMap) { |
| 45 | + return Boolean(resolveAuthoritativeLastMergedHostContextId(lastMergedHostContextId, history, sessionStorageMap)); |
| 46 | +} |
| 47 | + |
| 48 | +export function run() { |
| 49 | + const failures = []; |
| 50 | + const jsExists = fs.existsSync(jsPath); |
| 51 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 52 | + const jsSyntax = checkSyntax(jsPath); |
| 53 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2MergeStateSingleSourceOfTruth.test.mjs")); |
| 54 | + |
| 55 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 56 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 57 | + if (!testSyntax.ok) failures.push("tests/runtime/V2MergeStateSingleSourceOfTruth.test.mjs failed syntax check."); |
| 58 | + |
| 59 | + const requiredTokens = [ |
| 60 | + "resolveAuthoritativeLastMergedHostContextId()", |
| 61 | + "const mergedHostContextId = this.resolveAuthoritativeLastMergedHostContextId();", |
| 62 | + "existsAsMergedRecent", |
| 63 | + "this.writeLastMergedHostContextId(\"\");", |
| 64 | + "console.debug(\"[WorkspaceV2UndoLastMerge] stale_authoritative_merge_record\", {", |
| 65 | + "const lastMergedId = this.resolveAuthoritativeLastMergedHostContextId();" |
| 66 | + ]; |
| 67 | + requiredTokens.forEach((token) => { |
| 68 | + if (!js.includes(token)) failures.push(`Missing merge-state SSoT token/text: ${token}`); |
| 69 | + }); |
| 70 | + |
| 71 | + const mergedId = "asset-browser-v2-merged-1777777777777-abcd1234"; |
| 72 | + const validMergedPayload = { |
| 73 | + version: "v2", |
| 74 | + toolId: "asset-browser-v2", |
| 75 | + mergeResultMeta: { isMergedResult: true } |
| 76 | + }; |
| 77 | + |
| 78 | + const initialHistory = [{ hostContextId: "asset-browser-v2-regular", payload: { version: "v2", toolId: "asset-browser-v2" } }]; |
| 79 | + const initialStorage = { "asset-browser-v2-regular": "{\"version\":\"v2\",\"toolId\":\"asset-browser-v2\"}" }; |
| 80 | + if (undoEnabled("", initialHistory, initialStorage)) failures.push("Undo must be disabled on initial load."); |
| 81 | + |
| 82 | + const applyHistory = [ |
| 83 | + { hostContextId: mergedId, payload: validMergedPayload }, |
| 84 | + ...initialHistory |
| 85 | + ]; |
| 86 | + const applyStorage = { |
| 87 | + ...initialStorage, |
| 88 | + [mergedId]: JSON.stringify(validMergedPayload) |
| 89 | + }; |
| 90 | + if (!undoEnabled(mergedId, applyHistory, applyStorage)) failures.push("Undo must be enabled immediately after successful merge apply."); |
| 91 | + |
| 92 | + const undoHistory = initialHistory; |
| 93 | + const undoStorage = initialStorage; |
| 94 | + if (undoEnabled("", undoHistory, undoStorage)) failures.push("Undo must be disabled immediately after undo clears authoritative record."); |
| 95 | + |
| 96 | + const deletedMergedHistory = initialHistory; |
| 97 | + const deletedMergedStorage = applyStorage; |
| 98 | + if (undoEnabled(mergedId, deletedMergedHistory, deletedMergedStorage)) failures.push("Undo must be disabled if merged recent entry is deleted."); |
| 99 | + |
| 100 | + const staleStorageHistory = applyHistory; |
| 101 | + const staleStorage = initialStorage; |
| 102 | + if (undoEnabled(mergedId, staleStorageHistory, staleStorage)) failures.push("Undo must be disabled if merged session storage entry is missing."); |
| 103 | + |
| 104 | + const staleMetaHistory = [ |
| 105 | + { hostContextId: mergedId, payload: { version: "v2", toolId: "asset-browser-v2" } }, |
| 106 | + ...initialHistory |
| 107 | + ]; |
| 108 | + if (undoEnabled(mergedId, staleMetaHistory, applyStorage)) failures.push("Undo must be disabled if authoritative recent entry is not a merged result."); |
| 109 | + |
| 110 | + const refreshRecompute = resolveAuthoritativeLastMergedHostContextId(mergedId, deletedMergedHistory, deletedMergedStorage); |
| 111 | + if (refreshRecompute !== "") failures.push("Refresh recompute should clear stale authoritative merge record."); |
| 112 | + |
| 113 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 114 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 115 | + generatedAt: new Date().toISOString(), |
| 116 | + failures, |
| 117 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 118 | + scenarios: { |
| 119 | + initialUndoEnabled: undoEnabled("", initialHistory, initialStorage), |
| 120 | + applyUndoEnabled: undoEnabled(mergedId, applyHistory, applyStorage), |
| 121 | + postUndoUndoEnabled: undoEnabled("", undoHistory, undoStorage), |
| 122 | + deletedMergedUndoEnabled: undoEnabled(mergedId, deletedMergedHistory, deletedMergedStorage), |
| 123 | + staleStorageUndoEnabled: undoEnabled(mergedId, staleStorageHistory, staleStorage), |
| 124 | + staleMetaUndoEnabled: undoEnabled(mergedId, staleMetaHistory, applyStorage), |
| 125 | + refreshRecompute |
| 126 | + } |
| 127 | + }, null, 2)}\n`, "utf8"); |
| 128 | + |
| 129 | + console.log(`v2 merge-state-ssot results: ${resultsPath}`); |
| 130 | + assert.equal(failures.length, 0, `V2 merge-state-ssot failures: ${failures.join(" | ")}`); |
| 131 | + return { failures }; |
| 132 | +} |
| 133 | + |
| 134 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 135 | + try { |
| 136 | + const summary = run(); |
| 137 | + console.log(JSON.stringify(summary, null, 2)); |
| 138 | + } catch (error) { |
| 139 | + console.error(error); |
| 140 | + process.exitCode = 1; |
| 141 | + } |
| 142 | +} |
0 commit comments