|
| 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 htmlPath = path.join(repoRoot, "tools", "workspace-v2", "index.html"); |
| 11 | +const jsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-merge-state-status-reset-results.json"); |
| 13 | + |
| 14 | +function checkSyntax(filePath) { |
| 15 | + try { |
| 16 | + execFileSync(process.execPath, ["--check", filePath], { |
| 17 | + cwd: repoRoot, |
| 18 | + stdio: ["ignore", "pipe", "pipe"] |
| 19 | + }); |
| 20 | + return { ok: true, error: "" }; |
| 21 | + } catch (error) { |
| 22 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function clearMergePanelTransientState(summaryMessage, outputMessage, statusMessage) { |
| 27 | + return { |
| 28 | + pendingMergePreview: null, |
| 29 | + mergeOutputSelectionKey: "", |
| 30 | + lastMergedSessionResult: null, |
| 31 | + mergedSessionId: "", |
| 32 | + mergedSessionStatus: "No merged session result to save.", |
| 33 | + mergeResultSummary: summaryMessage, |
| 34 | + mergeOutput: outputMessage, |
| 35 | + mergeConflictHidden: true, |
| 36 | + mergeConflictText: "", |
| 37 | + confirmDisabled: true, |
| 38 | + applyDisabled: true, |
| 39 | + status: statusMessage |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +function isUndoAvailable(lastMergedHostContextId, recentIds, sessionIds) { |
| 44 | + if (!lastMergedHostContextId) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + return recentIds.includes(lastMergedHostContextId) && sessionIds.includes(lastMergedHostContextId); |
| 48 | +} |
| 49 | + |
| 50 | +export function run() { |
| 51 | + const failures = []; |
| 52 | + const htmlExists = fs.existsSync(htmlPath); |
| 53 | + const jsExists = fs.existsSync(jsPath); |
| 54 | + const html = htmlExists ? fs.readFileSync(htmlPath, "utf8") : ""; |
| 55 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 56 | + const jsSyntax = checkSyntax(jsPath); |
| 57 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2MergeStateStatusReset.test.mjs")); |
| 58 | + |
| 59 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 60 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 61 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 62 | + if (!testSyntax.ok) failures.push("tests/runtime/V2MergeStateStatusReset.test.mjs failed syntax check."); |
| 63 | + |
| 64 | + const requiredTokens = [ |
| 65 | + "clearMergePanelTransientState(summaryMessage, outputMessage, statusMessage)", |
| 66 | + "this.clearMergePanelTransientState(", |
| 67 | + "Selections changed. Run Preview Merge again.", |
| 68 | + "No merge preview available.", |
| 69 | + "No merge summary yet." |
| 70 | + ]; |
| 71 | + requiredTokens.forEach((token) => { |
| 72 | + if (!js.includes(token)) failures.push(`Missing merge-state-reset token/text: ${token}`); |
| 73 | + }); |
| 74 | + |
| 75 | + if (!html.includes("id=\"workspaceV2MergeResultSummary\"")) failures.push("Missing merge result summary node."); |
| 76 | + if (!html.includes("id=\"workspaceV2MergeOutput\"")) failures.push("Missing merge output node."); |
| 77 | + if (!html.includes("id=\"workspaceV2MergedSessionStatus\"")) failures.push("Missing merged session status node."); |
| 78 | + |
| 79 | + const previewState = { |
| 80 | + mergeResultSummary: "Merge Preview Summary\nSource Session ID: history:asset-browser-v2-a", |
| 81 | + mergeOutput: "{\n \"source\": \"history:asset-browser-v2-a\"\n}", |
| 82 | + mergedSessionStatus: "Merged session ready. Choose an ID and save if desired.", |
| 83 | + confirmDisabled: false, |
| 84 | + applyDisabled: false |
| 85 | + }; |
| 86 | + if (!previewState.mergeResultSummary.includes("Merge Preview Summary")) failures.push("Preview summary should appear after Preview Merge."); |
| 87 | + if (!previewState.mergeOutput.includes("\"source\"")) failures.push("Raw JSON should appear after Preview Merge."); |
| 88 | + |
| 89 | + const selectionChangeReset = clearMergePanelTransientState( |
| 90 | + "Selections changed. Run Preview Merge again.", |
| 91 | + "No merge preview available.", |
| 92 | + "Selections changed. Run Preview Merge again." |
| 93 | + ); |
| 94 | + if (selectionChangeReset.mergeResultSummary !== "Selections changed. Run Preview Merge again.") failures.push("Selection change should clear summary to stale notice."); |
| 95 | + if (selectionChangeReset.mergeOutput !== "No merge preview available.") failures.push("Selection change should clear raw preview JSON."); |
| 96 | + if (selectionChangeReset.mergedSessionStatus !== "No merged session result to save.") failures.push("Selection change should clear merged-session save controls."); |
| 97 | + if (!selectionChangeReset.confirmDisabled || !selectionChangeReset.applyDisabled) failures.push("Selection change should disable Confirm/Apply."); |
| 98 | + |
| 99 | + const undoReset = clearMergePanelTransientState( |
| 100 | + "Last merged session removed.\nRemoved Session ID: asset-browser-v2-merged-1777777777777-abc123xy", |
| 101 | + "No merge preview available.", |
| 102 | + "Last merged session removed." |
| 103 | + ); |
| 104 | + if (!undoReset.mergeResultSummary.includes("Last merged session removed.")) failures.push("Undo should clear stale summary and show undo result."); |
| 105 | + if (undoReset.mergeOutput !== "No merge preview available.") failures.push("Undo should clear raw merge output."); |
| 106 | + |
| 107 | + const mergedDeleteReset = clearMergePanelTransientState( |
| 108 | + "Selections changed. Run Preview Merge again.", |
| 109 | + "No merge preview available.", |
| 110 | + "Selections changed. Run Preview Merge again." |
| 111 | + ); |
| 112 | + if (mergedDeleteReset.mergeOutput !== "No merge preview available.") failures.push("Merged recent deletion should clear stale output."); |
| 113 | + |
| 114 | + const invalidSelectionReset = clearMergePanelTransientState( |
| 115 | + "Merge preview blocked. Session A and Session B selections are missing.", |
| 116 | + "Merge preview blocked. Session A and Session B selections are missing.", |
| 117 | + "Merge preview blocked. Select Session A and Session B, then run Preview Merge (Dry Run)." |
| 118 | + ); |
| 119 | + if (!invalidSelectionReset.mergeResultSummary.includes("selections are missing")) failures.push("Invalid/missing selections should clear stale summary and show blocked reason."); |
| 120 | + if (!invalidSelectionReset.confirmDisabled || !invalidSelectionReset.applyDisabled) failures.push("Invalid/missing selections should keep Confirm/Apply disabled."); |
| 121 | + |
| 122 | + const refreshBaseline = clearMergePanelTransientState( |
| 123 | + "No merge summary yet.", |
| 124 | + "No merge preview available.", |
| 125 | + "" |
| 126 | + ); |
| 127 | + if (refreshBaseline.mergeResultSummary !== "No merge summary yet.") failures.push("Refresh baseline should clear stale summary."); |
| 128 | + if (refreshBaseline.mergeOutput !== "No merge preview available.") failures.push("Refresh baseline should clear stale raw output."); |
| 129 | + |
| 130 | + const undoAvailabilityAfterResets = isUndoAvailable( |
| 131 | + "asset-browser-v2-merged-1777777777777-abc123xy", |
| 132 | + ["asset-browser-v2-merged-1777777777777-abc123xy", "asset-browser-v2-regular"], |
| 133 | + ["asset-browser-v2-merged-1777777777777-abc123xy", "asset-browser-v2-regular"] |
| 134 | + ); |
| 135 | + if (!undoAvailabilityAfterResets) failures.push("Undo availability should remain based on actual recent+session entries."); |
| 136 | + |
| 137 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 138 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 139 | + generatedAt: new Date().toISOString(), |
| 140 | + failures, |
| 141 | + checks: { htmlExists, jsExists, jsSyntax, testSyntax }, |
| 142 | + scenarios: { |
| 143 | + previewState, |
| 144 | + selectionChangeReset, |
| 145 | + undoReset, |
| 146 | + mergedDeleteReset, |
| 147 | + invalidSelectionReset, |
| 148 | + refreshBaseline, |
| 149 | + undoAvailabilityAfterResets |
| 150 | + } |
| 151 | + }, null, 2)}\n`, "utf8"); |
| 152 | + |
| 153 | + console.log(`v2 merge-state-status-reset results: ${resultsPath}`); |
| 154 | + assert.equal(failures.length, 0, `V2 merge-state-status-reset failures: ${failures.join(" | ")}`); |
| 155 | + return { failures }; |
| 156 | +} |
| 157 | + |
| 158 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 159 | + try { |
| 160 | + const summary = run(); |
| 161 | + console.log(JSON.stringify(summary, null, 2)); |
| 162 | + } catch (error) { |
| 163 | + console.error(error); |
| 164 | + process.exitCode = 1; |
| 165 | + } |
| 166 | +} |
0 commit comments