|
| 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 workspaceJsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 11 | +const workspaceHtmlPath = path.join(repoRoot, "tools", "workspace-v2", "index.html"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-diff-merge-button-state-results.json"); |
| 13 | + |
| 14 | +function readText(filePath) { |
| 15 | + return fs.readFileSync(filePath, "utf8"); |
| 16 | +} |
| 17 | + |
| 18 | +function checkJsSyntax(jsPath) { |
| 19 | + try { |
| 20 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 21 | + cwd: repoRoot, |
| 22 | + stdio: ["ignore", "pipe", "pipe"] |
| 23 | + }); |
| 24 | + return { syntaxValid: true, syntaxError: "" }; |
| 25 | + } catch (error) { |
| 26 | + return { |
| 27 | + syntaxValid: false, |
| 28 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 29 | + }; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function computeActionEnabled(leftId, rightId) { |
| 34 | + return Boolean(leftId && rightId && leftId !== rightId); |
| 35 | +} |
| 36 | + |
| 37 | +function previewState(previewExists, confirmed, fresh, conflicts) { |
| 38 | + const canConfirm = Boolean(previewExists && !confirmed && fresh && !conflicts); |
| 39 | + const canApply = Boolean(previewExists && confirmed && fresh && !conflicts); |
| 40 | + return { canConfirm, canApply }; |
| 41 | +} |
| 42 | + |
| 43 | +export function run() { |
| 44 | + const failures = []; |
| 45 | + const jsExists = fs.existsSync(workspaceJsPath); |
| 46 | + const htmlExists = fs.existsSync(workspaceHtmlPath); |
| 47 | + const js = jsExists ? readText(workspaceJsPath) : ""; |
| 48 | + const html = htmlExists ? readText(workspaceHtmlPath) : ""; |
| 49 | + const { syntaxValid, syntaxError } = checkJsSyntax(workspaceJsPath); |
| 50 | + |
| 51 | + const hasSelectionStateNodes = |
| 52 | + html.includes("workspaceV2DiffSelectionState") && |
| 53 | + html.includes("workspaceV2MergeSelectionState"); |
| 54 | + const hasSameSelectionInline = js.includes("Choose two different sessions."); |
| 55 | + const hasDiffDisableRule = js.includes("this.computeDiffButton.disabled = !canRunDiff;"); |
| 56 | + const hasMergeDisableRule = js.includes("this.computeMergeButton.disabled = !canPreviewMerge;"); |
| 57 | + const hasConfirmRule = js.includes("const previewReadyForConfirm = Boolean(this.pendingMergePreview && !this.pendingMergePreview.confirmed && previewIsFresh && !previewHasConflicts);"); |
| 58 | + const hasApplyRule = js.includes("const previewReadyForApply = Boolean(this.pendingMergePreview && this.pendingMergePreview.confirmed && previewIsFresh && !previewHasConflicts);"); |
| 59 | + const hasUpdaterCalls = js.includes("this.updateMergeSelectionFeedbackAndState();") && js.includes("this.updateDiffSelectionFeedbackAndState();"); |
| 60 | + const hasBlockedMessages = js.includes("Diff blocked. Session A and Session B selections are missing.") && js.includes("Merge preview blocked. Session A and Session B selections are missing."); |
| 61 | + |
| 62 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 63 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 64 | + if (!syntaxValid) failures.push("workspace-v2/index.js failed syntax check."); |
| 65 | + if (!hasSelectionStateNodes) failures.push("Missing inline selection state nodes."); |
| 66 | + if (!hasSameSelectionInline) failures.push("Missing inline same-selection state text."); |
| 67 | + if (!hasDiffDisableRule) failures.push("Missing compute-diff disable rule."); |
| 68 | + if (!hasMergeDisableRule) failures.push("Missing preview-merge disable rule."); |
| 69 | + if (!hasConfirmRule) failures.push("Missing confirm enable-state rule."); |
| 70 | + if (!hasApplyRule) failures.push("Missing apply enable-state rule."); |
| 71 | + if (!hasUpdaterCalls) failures.push("Missing selection-state updater calls."); |
| 72 | + if (!hasBlockedMessages) failures.push("Missing existing blocked messages for direct invalid calls."); |
| 73 | + |
| 74 | + const matrix = { |
| 75 | + zero: computeActionEnabled("", ""), |
| 76 | + onlyA: computeActionEnabled("a", ""), |
| 77 | + onlyB: computeActionEnabled("", "b"), |
| 78 | + same: computeActionEnabled("a", "a"), |
| 79 | + distinct: computeActionEnabled("a", "b") |
| 80 | + }; |
| 81 | + |
| 82 | + if (matrix.zero) failures.push("Buttons must be disabled for zero selections."); |
| 83 | + if (matrix.onlyA) failures.push("Buttons must be disabled for only Session A selected."); |
| 84 | + if (matrix.onlyB) failures.push("Buttons must be disabled for only Session B selected."); |
| 85 | + if (matrix.same) failures.push("Buttons must be disabled for same-session selection."); |
| 86 | + if (!matrix.distinct) failures.push("Buttons must be enabled for distinct selections."); |
| 87 | + |
| 88 | + const previewCases = { |
| 89 | + none: previewState(false, false, false, false), |
| 90 | + validPreview: previewState(true, false, true, false), |
| 91 | + confirmedPreview: previewState(true, true, true, false), |
| 92 | + conflicted: previewState(true, false, true, true), |
| 93 | + stale: previewState(true, false, false, false), |
| 94 | + staleConfirmed: previewState(true, true, false, false) |
| 95 | + }; |
| 96 | + |
| 97 | + if (previewCases.none.canConfirm || previewCases.none.canApply) failures.push("No preview should disable confirm/apply."); |
| 98 | + if (!previewCases.validPreview.canConfirm || previewCases.validPreview.canApply) failures.push("Valid preview should enable confirm only."); |
| 99 | + if (previewCases.confirmedPreview.canConfirm || !previewCases.confirmedPreview.canApply) failures.push("Confirmed preview should enable apply only."); |
| 100 | + if (previewCases.conflicted.canConfirm || previewCases.conflicted.canApply) failures.push("Conflicted preview should disable confirm/apply."); |
| 101 | + if (previewCases.stale.canConfirm || previewCases.stale.canApply) failures.push("Stale preview should disable confirm/apply."); |
| 102 | + if (previewCases.staleConfirmed.canApply) failures.push("Stale confirmed preview should disable apply."); |
| 103 | + |
| 104 | + let diffOutput = "No diff computed."; |
| 105 | + const diffClickEnabled = matrix.onlyA; |
| 106 | + if (diffClickEnabled) { |
| 107 | + diffOutput = "Diff blocked. Session A and Session B selections are missing."; |
| 108 | + } |
| 109 | + if (diffOutput !== "No diff computed.") { |
| 110 | + failures.push("Disabled Diff button simulation should not append blocked/error output."); |
| 111 | + } |
| 112 | + |
| 113 | + let mergeOutput = "No merge computed."; |
| 114 | + const mergeClickEnabled = matrix.onlyA; |
| 115 | + if (mergeClickEnabled) { |
| 116 | + mergeOutput = "Merge preview blocked. Session A and Session B selections are missing."; |
| 117 | + } |
| 118 | + if (mergeOutput !== "No merge computed.") { |
| 119 | + failures.push("Disabled Merge button simulation should not append blocked/error output."); |
| 120 | + } |
| 121 | + |
| 122 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 123 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 124 | + generatedAt: new Date().toISOString(), |
| 125 | + failures, |
| 126 | + checks: { |
| 127 | + jsExists, |
| 128 | + htmlExists, |
| 129 | + syntaxValid, |
| 130 | + syntaxError, |
| 131 | + hasSelectionStateNodes, |
| 132 | + hasSameSelectionInline, |
| 133 | + hasDiffDisableRule, |
| 134 | + hasMergeDisableRule, |
| 135 | + hasConfirmRule, |
| 136 | + hasApplyRule, |
| 137 | + hasUpdaterCalls, |
| 138 | + hasBlockedMessages, |
| 139 | + matrix, |
| 140 | + previewCases |
| 141 | + } |
| 142 | + }, null, 2)}\n`, "utf8"); |
| 143 | + |
| 144 | + console.log(`v2 diff/merge button-state results: ${resultsPath}`); |
| 145 | + assert.equal(failures.length, 0, `V2 diff/merge button-state failures: ${failures.join(" | ")}`); |
| 146 | + return { failures }; |
| 147 | +} |
| 148 | + |
| 149 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 150 | + try { |
| 151 | + const summary = run(); |
| 152 | + console.log(JSON.stringify(summary, null, 2)); |
| 153 | + } catch (error) { |
| 154 | + console.error(error); |
| 155 | + process.exitCode = 1; |
| 156 | + } |
| 157 | +} |
0 commit comments