|
| 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-preview-overlay-fix-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 computeMergeUiState({ canPreviewMerge, previewExists, previewFresh, previewConfirmed, previewHasConflicts }) { |
| 27 | + let mergeEnableState = "Select two different sessions to enable Preview Merge."; |
| 28 | + let confirmDisabled = true; |
| 29 | + let applyDisabled = true; |
| 30 | + |
| 31 | + if (canPreviewMerge) { |
| 32 | + const previewReadyForConfirm = Boolean(previewExists && !previewConfirmed && previewFresh && !previewHasConflicts); |
| 33 | + const previewReadyForApply = Boolean(previewExists && previewConfirmed && previewFresh && !previewHasConflicts); |
| 34 | + confirmDisabled = !previewReadyForConfirm; |
| 35 | + applyDisabled = !previewReadyForApply; |
| 36 | + if (previewHasConflicts && previewFresh) { |
| 37 | + mergeEnableState = "Preview has conflicts. Resolve conflicts before applying."; |
| 38 | + } else if (previewReadyForApply) { |
| 39 | + mergeEnableState = "Preview confirmed. Ready to apply merge."; |
| 40 | + } else if (previewReadyForConfirm) { |
| 41 | + mergeEnableState = "Preview ready. Confirm to enable apply."; |
| 42 | + } else { |
| 43 | + mergeEnableState = "Ready to preview merge."; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return { mergeEnableState, confirmDisabled, applyDisabled }; |
| 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", "V2MergePreviewOverlayFix.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/V2MergePreviewOverlayFix.test.mjs failed syntax check."); |
| 63 | + |
| 64 | + if (!html.includes("id=\"workspaceV2MergeOutput\"")) failures.push("Merge preview output node is missing."); |
| 65 | + if (!html.includes("max-height: 18rem; overflow: auto; position: relative;")) { |
| 66 | + failures.push("Merge preview output container is not explicitly bounded/contained."); |
| 67 | + } |
| 68 | + if (html.includes("position: fixed") || html.includes("inset: 0")) { |
| 69 | + failures.push("Unexpected page-wide overlay styles found in merge panel HTML."); |
| 70 | + } |
| 71 | + |
| 72 | + const requiredStatusTexts = [ |
| 73 | + "Ready to preview merge.", |
| 74 | + "Preview ready. Confirm to enable apply.", |
| 75 | + "Preview confirmed. Ready to apply merge.", |
| 76 | + "Preview has conflicts. Resolve conflicts before applying." |
| 77 | + ]; |
| 78 | + requiredStatusTexts.forEach((text) => { |
| 79 | + if (!js.includes(text)) failures.push(`Missing merge status text: ${text}`); |
| 80 | + }); |
| 81 | + |
| 82 | + const beforePreview = computeMergeUiState({ |
| 83 | + canPreviewMerge: true, |
| 84 | + previewExists: false, |
| 85 | + previewFresh: false, |
| 86 | + previewConfirmed: false, |
| 87 | + previewHasConflicts: false |
| 88 | + }); |
| 89 | + if (beforePreview.mergeEnableState !== "Ready to preview merge.") failures.push("Before preview status mismatch."); |
| 90 | + if (!beforePreview.confirmDisabled) failures.push("Confirm should be disabled before preview."); |
| 91 | + if (!beforePreview.applyDisabled) failures.push("Apply should be disabled before preview."); |
| 92 | + |
| 93 | + const previewReady = computeMergeUiState({ |
| 94 | + canPreviewMerge: true, |
| 95 | + previewExists: true, |
| 96 | + previewFresh: true, |
| 97 | + previewConfirmed: false, |
| 98 | + previewHasConflicts: false |
| 99 | + }); |
| 100 | + if (previewReady.mergeEnableState !== "Preview ready. Confirm to enable apply.") failures.push("Preview-ready status mismatch."); |
| 101 | + if (previewReady.confirmDisabled) failures.push("Confirm should be enabled after valid non-conflict preview."); |
| 102 | + if (!previewReady.applyDisabled) failures.push("Apply should stay disabled before confirm."); |
| 103 | + |
| 104 | + const previewConfirmed = computeMergeUiState({ |
| 105 | + canPreviewMerge: true, |
| 106 | + previewExists: true, |
| 107 | + previewFresh: true, |
| 108 | + previewConfirmed: true, |
| 109 | + previewHasConflicts: false |
| 110 | + }); |
| 111 | + if (previewConfirmed.mergeEnableState !== "Preview confirmed. Ready to apply merge.") failures.push("Preview-confirmed status mismatch."); |
| 112 | + if (!previewConfirmed.confirmDisabled) failures.push("Confirm should be disabled once preview is confirmed."); |
| 113 | + if (previewConfirmed.applyDisabled) failures.push("Apply should be enabled after confirmed fresh preview."); |
| 114 | + |
| 115 | + const conflictPreview = computeMergeUiState({ |
| 116 | + canPreviewMerge: true, |
| 117 | + previewExists: true, |
| 118 | + previewFresh: true, |
| 119 | + previewConfirmed: false, |
| 120 | + previewHasConflicts: true |
| 121 | + }); |
| 122 | + if (conflictPreview.mergeEnableState !== "Preview has conflicts. Resolve conflicts before applying.") { |
| 123 | + failures.push("Conflict preview status mismatch."); |
| 124 | + } |
| 125 | + if (!conflictPreview.confirmDisabled || !conflictPreview.applyDisabled) { |
| 126 | + failures.push("Conflict preview should keep Confirm/Apply disabled."); |
| 127 | + } |
| 128 | + |
| 129 | + const noSelection = computeMergeUiState({ |
| 130 | + canPreviewMerge: false, |
| 131 | + previewExists: false, |
| 132 | + previewFresh: false, |
| 133 | + previewConfirmed: false, |
| 134 | + previewHasConflicts: false |
| 135 | + }); |
| 136 | + if (noSelection.mergeEnableState !== "Select two different sessions to enable Preview Merge.") { |
| 137 | + failures.push("No-selection status mismatch."); |
| 138 | + } |
| 139 | + |
| 140 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 141 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 142 | + generatedAt: new Date().toISOString(), |
| 143 | + failures, |
| 144 | + checks: { htmlExists, jsExists, jsSyntax, testSyntax }, |
| 145 | + states: { noSelection, beforePreview, previewReady, previewConfirmed, conflictPreview } |
| 146 | + }, null, 2)}\n`, "utf8"); |
| 147 | + |
| 148 | + console.log(`v2 merge-preview-overlay-fix results: ${resultsPath}`); |
| 149 | + assert.equal(failures.length, 0, `V2 merge-preview-overlay-fix failures: ${failures.join(" | ")}`); |
| 150 | + return { failures }; |
| 151 | +} |
| 152 | + |
| 153 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 154 | + try { |
| 155 | + const summary = run(); |
| 156 | + console.log(JSON.stringify(summary, null, 2)); |
| 157 | + } catch (error) { |
| 158 | + console.error(error); |
| 159 | + process.exitCode = 1; |
| 160 | + } |
| 161 | +} |
| 162 | + |
0 commit comments