|
| 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-clear-stale-merge-preview-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 selectionKey(leftId, rightId) { |
| 26 | + return `${leftId || ""}|${rightId || ""}`; |
| 27 | +} |
| 28 | + |
| 29 | +function undoAvailable(lastMergedHostContextId, recent) { |
| 30 | + return Boolean(lastMergedHostContextId && recent.some((entry) => entry.hostContextId === lastMergedHostContextId)); |
| 31 | +} |
| 32 | + |
| 33 | +function onSelectionChange(state, nextLeft, nextRight) { |
| 34 | + const next = { |
| 35 | + ...state, |
| 36 | + mergeLeft: nextLeft, |
| 37 | + mergeRight: nextRight |
| 38 | + }; |
| 39 | + const currentKey = selectionKey(nextLeft, nextRight); |
| 40 | + if (next.mergeOutputSelectionKey && next.mergeOutputSelectionKey !== currentKey) { |
| 41 | + next.pendingMergePreview = null; |
| 42 | + next.mergeOutputSelectionKey = ""; |
| 43 | + next.lastMergedSessionResult = null; |
| 44 | + next.mergedSessionId = ""; |
| 45 | + next.mergedSessionStatus = "No merged session result to save."; |
| 46 | + next.mergeResultSummary = "Selections changed. Run Preview Merge again."; |
| 47 | + next.mergeOutput = "No merge preview available."; |
| 48 | + next.confirmDisabled = true; |
| 49 | + next.applyDisabled = true; |
| 50 | + } else { |
| 51 | + next.confirmDisabled = !next.pendingMergePreview; |
| 52 | + next.applyDisabled = !next.pendingMergePreview || !next.pendingMergePreview.confirmed; |
| 53 | + } |
| 54 | + next.previewEnabled = Boolean(nextLeft && nextRight && nextLeft !== nextRight); |
| 55 | + return next; |
| 56 | +} |
| 57 | + |
| 58 | +export function run() { |
| 59 | + const failures = []; |
| 60 | + const jsExists = fs.existsSync(jsPath); |
| 61 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 62 | + const jsSyntax = checkSyntax(jsPath); |
| 63 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2ClearStaleMergePreview.test.mjs")); |
| 64 | + |
| 65 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 66 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 67 | + if (!testSyntax.ok) failures.push("tests/runtime/V2ClearStaleMergePreview.test.mjs failed syntax check."); |
| 68 | + |
| 69 | + const requiredTokens = [ |
| 70 | + "handleMergeSelectionChange()", |
| 71 | + "clearMergeOutputForSelectionChange()", |
| 72 | + "Selections changed. Run Preview Merge again.", |
| 73 | + "this.mergeOutputNode.textContent = \"No merge preview available.\";", |
| 74 | + "this.lastMergedSessionResult = null;", |
| 75 | + "this.confirmMergeButton.disabled = true;", |
| 76 | + "this.applyMergeButton.disabled = true;", |
| 77 | + "this.mergeOutputSelectionKey = this.buildMergeSelectionKey(left.id, right.id);" |
| 78 | + ]; |
| 79 | + requiredTokens.forEach((token) => { |
| 80 | + if (!js.includes(token)) failures.push(`Missing stale-preview-clear token/text: ${token}`); |
| 81 | + }); |
| 82 | + |
| 83 | + const originalLeft = "history:asset-browser-v2-a"; |
| 84 | + const originalRight = "history:asset-browser-v2-b"; |
| 85 | + const originalKey = selectionKey(originalLeft, originalRight); |
| 86 | + const mergedRecentId = "asset-browser-v2-merged-1777777777777-abc123xy"; |
| 87 | + const base = { |
| 88 | + mergeLeft: originalLeft, |
| 89 | + mergeRight: originalRight, |
| 90 | + mergeOutputSelectionKey: originalKey, |
| 91 | + pendingMergePreview: { |
| 92 | + source: { id: originalLeft }, |
| 93 | + target: { id: originalRight }, |
| 94 | + confirmed: true |
| 95 | + }, |
| 96 | + lastMergedSessionResult: { payload: { ok: true } }, |
| 97 | + mergedSessionId: "asset-browser-v2-merged-1777777777777", |
| 98 | + mergedSessionStatus: "Merged session ready. Choose an ID and save if desired.", |
| 99 | + mergeResultSummary: "Merge Apply Summary", |
| 100 | + mergeOutput: "{\"source\":\"history:asset-browser-v2-a\"}", |
| 101 | + confirmDisabled: false, |
| 102 | + applyDisabled: false, |
| 103 | + recent: [{ hostContextId: mergedRecentId }], |
| 104 | + lastMergedHostContextId: mergedRecentId, |
| 105 | + previewEnabled: true |
| 106 | + }; |
| 107 | + |
| 108 | + if (!base.mergeResultSummary.includes("Merge Apply Summary")) failures.push("Preview/apply summary should exist before selection change."); |
| 109 | + if (!base.mergeOutput.includes("history:asset-browser-v2-a")) failures.push("Raw preview/apply JSON should exist before selection change."); |
| 110 | + |
| 111 | + const changeA = onSelectionChange(base, "history:palette-manager-v2-a", originalRight); |
| 112 | + if (changeA.mergeResultSummary !== "Selections changed. Run Preview Merge again.") failures.push("Changing Session A should set stale-selection status."); |
| 113 | + if (changeA.mergeOutput !== "No merge preview available.") failures.push("Changing Session A should clear raw merge JSON."); |
| 114 | + if (changeA.mergedSessionId !== "" || changeA.lastMergedSessionResult !== null) failures.push("Changing Session A should clear merged-save controls/output."); |
| 115 | + if (!changeA.confirmDisabled || !changeA.applyDisabled) failures.push("Changing Session A should disable Confirm and Apply."); |
| 116 | + |
| 117 | + const changeB = onSelectionChange(base, originalLeft, "history:palette-manager-v2-b"); |
| 118 | + if (changeB.mergeResultSummary !== "Selections changed. Run Preview Merge again.") failures.push("Changing Session B should set stale-selection status."); |
| 119 | + if (changeB.mergeOutput !== "No merge preview available.") failures.push("Changing Session B should clear raw merge JSON."); |
| 120 | + |
| 121 | + const changeBoth = onSelectionChange(base, "history:palette-manager-v2-a", "history:palette-manager-v2-b"); |
| 122 | + if (changeBoth.mergedSessionStatus !== "No merged session result to save.") failures.push("Changing both selections should clear merged-session status."); |
| 123 | + if (changeBoth.mergeOutputSelectionKey !== "") failures.push("Changing both selections should clear previous merge output key."); |
| 124 | + |
| 125 | + const switchBack = onSelectionChange(changeBoth, originalLeft, originalRight); |
| 126 | + if (!switchBack.confirmDisabled || !switchBack.applyDisabled) failures.push("Switching back to old pair should still require Preview Merge again."); |
| 127 | + if (switchBack.mergeResultSummary !== "Selections changed. Run Preview Merge again.") failures.push("Switching back should not silently restore stale preview."); |
| 128 | + |
| 129 | + const undoStillAvailableBefore = undoAvailable(base.lastMergedHostContextId, base.recent); |
| 130 | + const undoStillAvailableAfter = undoAvailable(changeBoth.lastMergedHostContextId, changeBoth.recent); |
| 131 | + if (!undoStillAvailableBefore || !undoStillAvailableAfter) failures.push("Undo Last Merge availability should remain unchanged by selection-change stale clear."); |
| 132 | + |
| 133 | + const noSelection = onSelectionChange({ |
| 134 | + ...base, |
| 135 | + mergeOutputSelectionKey: "", |
| 136 | + pendingMergePreview: null, |
| 137 | + mergeResultSummary: "No merge summary yet.", |
| 138 | + mergeOutput: "No merge preview available." |
| 139 | + }, "", ""); |
| 140 | + if (noSelection.previewEnabled) failures.push("Zero selections should keep Preview disabled."); |
| 141 | + |
| 142 | + const sameSelection = onSelectionChange({ |
| 143 | + ...base, |
| 144 | + mergeOutputSelectionKey: "", |
| 145 | + pendingMergePreview: null |
| 146 | + }, "history:palette-manager-v2-a", "history:palette-manager-v2-a"); |
| 147 | + if (sameSelection.previewEnabled) failures.push("Same-session selection should keep Preview disabled."); |
| 148 | + |
| 149 | + const distinctSelection = onSelectionChange({ |
| 150 | + ...base, |
| 151 | + mergeOutputSelectionKey: "", |
| 152 | + pendingMergePreview: null |
| 153 | + }, "history:palette-manager-v2-a", "history:palette-manager-v2-b"); |
| 154 | + if (!distinctSelection.previewEnabled) failures.push("Distinct selections should keep Preview enabled for normal validation flow."); |
| 155 | + |
| 156 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 157 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 158 | + generatedAt: new Date().toISOString(), |
| 159 | + failures, |
| 160 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 161 | + scenarios: { |
| 162 | + before: { mergeResultSummary: base.mergeResultSummary, mergeOutput: base.mergeOutput }, |
| 163 | + changeA, |
| 164 | + changeB, |
| 165 | + changeBoth, |
| 166 | + switchBack, |
| 167 | + undoStillAvailableBefore, |
| 168 | + undoStillAvailableAfter, |
| 169 | + noSelection, |
| 170 | + sameSelection, |
| 171 | + distinctSelection |
| 172 | + } |
| 173 | + }, null, 2)}\n`, "utf8"); |
| 174 | + |
| 175 | + console.log(`v2 clear-stale-merge-preview results: ${resultsPath}`); |
| 176 | + assert.equal(failures.length, 0, `V2 clear-stale-merge-preview failures: ${failures.join(" | ")}`); |
| 177 | + return { failures }; |
| 178 | +} |
| 179 | + |
| 180 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 181 | + try { |
| 182 | + const summary = run(); |
| 183 | + console.log(JSON.stringify(summary, null, 2)); |
| 184 | + } catch (error) { |
| 185 | + console.error(error); |
| 186 | + process.exitCode = 1; |
| 187 | + } |
| 188 | +} |
0 commit comments