|
| 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-undo-enable-state-actual-availability-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 computeUndoAvailability(lastMergedHostContextId, recent, sessionStorageMap) { |
| 26 | + const mergedHostContextId = typeof lastMergedHostContextId === "string" ? lastMergedHostContextId.trim() : ""; |
| 27 | + if (!mergedHostContextId) { |
| 28 | + return { enabled: false, cleared: true, reason: "missing-lastMergedHostContextId", nextLastMergedHostContextId: "" }; |
| 29 | + } |
| 30 | + const existsInRecent = recent.some((entry) => entry.hostContextId === mergedHostContextId); |
| 31 | + const existsInSessionStorage = Object.prototype.hasOwnProperty.call(sessionStorageMap, mergedHostContextId); |
| 32 | + const enabled = Boolean(mergedHostContextId && existsInRecent && existsInSessionStorage); |
| 33 | + if (!enabled) { |
| 34 | + return { |
| 35 | + enabled: false, |
| 36 | + cleared: true, |
| 37 | + reason: "stale-lastMergedHostContextId", |
| 38 | + nextLastMergedHostContextId: "", |
| 39 | + diagnostics: { existsInRecent, existsInSessionStorage } |
| 40 | + }; |
| 41 | + } |
| 42 | + return { enabled: true, cleared: false, reason: "available", nextLastMergedHostContextId: mergedHostContextId }; |
| 43 | +} |
| 44 | + |
| 45 | +export function run() { |
| 46 | + const failures = []; |
| 47 | + const jsExists = fs.existsSync(jsPath); |
| 48 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 49 | + const jsSyntax = checkSyntax(jsPath); |
| 50 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2UndoEnableStateActualAvailability.test.mjs")); |
| 51 | + |
| 52 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 53 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 54 | + if (!testSyntax.ok) failures.push("tests/runtime/V2UndoEnableStateActualAvailability.test.mjs failed syntax check."); |
| 55 | + |
| 56 | + const requiredTokens = [ |
| 57 | + "const existsInRecent = history.some((entry) => entry.hostContextId === mergedHostContextId);", |
| 58 | + "const existsInSessionStorage = typeof sessionStorage.getItem(mergedHostContextId) === \"string\";", |
| 59 | + "console.debug(\"[WorkspaceV2UndoLastMerge] stale_last_merged_context\", {", |
| 60 | + "this.writeLastMergedHostContextId(\"\");", |
| 61 | + "this.undoLastMergeButton.disabled = !hasRecentMerged;" |
| 62 | + ]; |
| 63 | + requiredTokens.forEach((token) => { |
| 64 | + if (!js.includes(token)) failures.push(`Missing undo-availability token/text: ${token}`); |
| 65 | + }); |
| 66 | + |
| 67 | + const mergedId = "asset-browser-v2-merged-1777777777777-abcd1234"; |
| 68 | + const recent = [{ hostContextId: mergedId }, { hostContextId: "asset-browser-v2-regular" }]; |
| 69 | + const storageMap = { |
| 70 | + [mergedId]: "{\"version\":\"v2\",\"toolId\":\"asset-browser-v2\"}", |
| 71 | + "asset-browser-v2-regular": "{\"version\":\"v2\",\"toolId\":\"asset-browser-v2\"}" |
| 72 | + }; |
| 73 | + |
| 74 | + const initialLoad = computeUndoAvailability("", recent, storageMap); |
| 75 | + if (initialLoad.enabled) failures.push("Undo must be disabled on initial load with no last merge."); |
| 76 | + |
| 77 | + const afterApply = computeUndoAvailability(mergedId, recent, storageMap); |
| 78 | + if (!afterApply.enabled) failures.push("Undo must be enabled immediately after apply when recent+storage are present."); |
| 79 | + |
| 80 | + const afterUndo = computeUndoAvailability("", recent.filter((entry) => entry.hostContextId !== mergedId), { |
| 81 | + "asset-browser-v2-regular": "{\"version\":\"v2\",\"toolId\":\"asset-browser-v2\"}" |
| 82 | + }); |
| 83 | + if (afterUndo.enabled) failures.push("Undo must be disabled immediately after undo clears last merge."); |
| 84 | + |
| 85 | + const manualDelete = computeUndoAvailability(mergedId, recent.filter((entry) => entry.hostContextId !== mergedId), storageMap); |
| 86 | + if (manualDelete.enabled) failures.push("Undo must be disabled if merged recent session is deleted."); |
| 87 | + if (manualDelete.nextLastMergedHostContextId !== "") failures.push("Stale last merged id should be cleared when recent entry is missing."); |
| 88 | + |
| 89 | + const staleStorage = computeUndoAvailability(mergedId, recent, { |
| 90 | + "asset-browser-v2-regular": "{\"version\":\"v2\",\"toolId\":\"asset-browser-v2\"}" |
| 91 | + }); |
| 92 | + if (staleStorage.enabled) failures.push("Undo must be disabled if merged session payload is missing from sessionStorage."); |
| 93 | + if (staleStorage.nextLastMergedHostContextId !== "") failures.push("Stale last merged id should be cleared when storage entry is missing."); |
| 94 | + |
| 95 | + const refreshRecompute = computeUndoAvailability(staleStorage.nextLastMergedHostContextId, recent, storageMap); |
| 96 | + if (refreshRecompute.enabled) failures.push("Undo must remain disabled on refresh after stale id clear."); |
| 97 | + |
| 98 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 99 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 100 | + generatedAt: new Date().toISOString(), |
| 101 | + failures, |
| 102 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 103 | + scenarios: { initialLoad, afterApply, afterUndo, manualDelete, staleStorage, refreshRecompute } |
| 104 | + }, null, 2)}\n`, "utf8"); |
| 105 | + |
| 106 | + console.log(`v2 undo-enable-state-actual-availability results: ${resultsPath}`); |
| 107 | + assert.equal(failures.length, 0, `V2 undo-enable-state-actual-availability failures: ${failures.join(" | ")}`); |
| 108 | + return { failures }; |
| 109 | +} |
| 110 | + |
| 111 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 112 | + try { |
| 113 | + const summary = run(); |
| 114 | + console.log(JSON.stringify(summary, null, 2)); |
| 115 | + } catch (error) { |
| 116 | + console.error(error); |
| 117 | + process.exitCode = 1; |
| 118 | + } |
| 119 | +} |
0 commit comments