|
| 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-confirm-preview-enable-state-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 evaluateState({ canPreviewMerge, previewExists, previewFresh, previewConfirmed, conflictCount }) { |
| 27 | + const hasConflicts = previewExists && conflictCount > 0; |
| 28 | + const previewReadyForConfirm = previewExists && !previewConfirmed && previewFresh && !hasConflicts; |
| 29 | + const previewReadyForApply = previewExists && previewConfirmed && previewFresh && !hasConflicts; |
| 30 | + const confirmDisabled = !previewReadyForConfirm; |
| 31 | + const applyDisabled = !previewReadyForApply; |
| 32 | + let status = "Select two different sessions to enable Preview Merge."; |
| 33 | + if (canPreviewMerge) { |
| 34 | + if (previewExists && !previewFresh) status = "Preview is stale. Run Preview Merge again."; |
| 35 | + else if (hasConflicts) status = "Preview has conflicts. Resolve conflicts before applying."; |
| 36 | + else if (previewReadyForApply) status = "Preview confirmed. Ready to apply merge."; |
| 37 | + else if (previewReadyForConfirm) status = "Preview ready. Confirm to enable apply."; |
| 38 | + else status = "Ready to preview merge."; |
| 39 | + } |
| 40 | + return { confirmDisabled, applyDisabled, status }; |
| 41 | +} |
| 42 | + |
| 43 | +export function run() { |
| 44 | + const failures = []; |
| 45 | + const htmlExists = fs.existsSync(htmlPath); |
| 46 | + const jsExists = fs.existsSync(jsPath); |
| 47 | + const html = htmlExists ? fs.readFileSync(htmlPath, "utf8") : ""; |
| 48 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 49 | + const jsSyntax = checkSyntax(jsPath); |
| 50 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2ConfirmPreviewEnableState.test.mjs")); |
| 51 | + |
| 52 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 53 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 54 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 55 | + if (!testSyntax.ok) failures.push("tests/runtime/V2ConfirmPreviewEnableState.test.mjs failed syntax check."); |
| 56 | + |
| 57 | + const requiredTokens = [ |
| 58 | + "conflictCount: Object.keys(result.conflicts).length", |
| 59 | + "previewFingerprint:", |
| 60 | + "Preview ready. Confirm to enable apply.", |
| 61 | + "Preview confirmed. Ready to apply merge.", |
| 62 | + "Preview has conflicts. Resolve conflicts before applying.", |
| 63 | + "Preview is stale. Run Preview Merge again." |
| 64 | + ]; |
| 65 | + requiredTokens.forEach((token) => { |
| 66 | + if (!js.includes(token)) failures.push(`Missing merge preview state token/text: ${token}`); |
| 67 | + }); |
| 68 | + |
| 69 | + if (!html.includes("id=\"workspaceV2MergeOutput\"") || !html.includes("max-height: 18rem; overflow: auto; position: relative;")) { |
| 70 | + failures.push("Merge preview output container is not explicitly contained."); |
| 71 | + } |
| 72 | + |
| 73 | + const noSelection = evaluateState({ canPreviewMerge: false, previewExists: false, previewFresh: false, previewConfirmed: false, conflictCount: 0 }); |
| 74 | + if (noSelection.status !== "Select two different sessions to enable Preview Merge.") failures.push("No-selection status mismatch."); |
| 75 | + |
| 76 | + const readyToPreview = evaluateState({ canPreviewMerge: true, previewExists: false, previewFresh: false, previewConfirmed: false, conflictCount: 0 }); |
| 77 | + if (readyToPreview.status !== "Ready to preview merge.") failures.push("Ready-to-preview status mismatch."); |
| 78 | + |
| 79 | + const previewReady = evaluateState({ canPreviewMerge: true, previewExists: true, previewFresh: true, previewConfirmed: false, conflictCount: 0 }); |
| 80 | + if (previewReady.confirmDisabled) failures.push("Conflict-free fresh preview should enable Confirm."); |
| 81 | + if (!previewReady.applyDisabled) failures.push("Apply must remain disabled before Confirm."); |
| 82 | + if (previewReady.status !== "Preview ready. Confirm to enable apply.") failures.push("Preview-ready status mismatch."); |
| 83 | + |
| 84 | + const previewConfirmed = evaluateState({ canPreviewMerge: true, previewExists: true, previewFresh: true, previewConfirmed: true, conflictCount: 0 }); |
| 85 | + if (!previewConfirmed.confirmDisabled) failures.push("Confirm should disable after confirmation."); |
| 86 | + if (previewConfirmed.applyDisabled) failures.push("Apply should enable after confirmed fresh preview."); |
| 87 | + if (previewConfirmed.status !== "Preview confirmed. Ready to apply merge.") failures.push("Preview-confirmed status mismatch."); |
| 88 | + |
| 89 | + const conflictPreview = evaluateState({ canPreviewMerge: true, previewExists: true, previewFresh: true, previewConfirmed: false, conflictCount: 2 }); |
| 90 | + if (!conflictPreview.confirmDisabled || !conflictPreview.applyDisabled) failures.push("Conflict preview must disable Confirm and Apply."); |
| 91 | + if (conflictPreview.status !== "Preview has conflicts. Resolve conflicts before applying.") failures.push("Conflict status mismatch."); |
| 92 | + |
| 93 | + const stalePreview = evaluateState({ canPreviewMerge: true, previewExists: true, previewFresh: false, previewConfirmed: false, conflictCount: 0 }); |
| 94 | + if (!stalePreview.confirmDisabled || !stalePreview.applyDisabled) failures.push("Stale preview must disable Confirm and Apply."); |
| 95 | + if (stalePreview.status !== "Preview is stale. Run Preview Merge again.") failures.push("Stale status mismatch."); |
| 96 | + |
| 97 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 98 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 99 | + generatedAt: new Date().toISOString(), |
| 100 | + failures, |
| 101 | + checks: { htmlExists, jsExists, jsSyntax, testSyntax }, |
| 102 | + states: { noSelection, readyToPreview, previewReady, previewConfirmed, conflictPreview, stalePreview } |
| 103 | + }, null, 2)}\n`, "utf8"); |
| 104 | + |
| 105 | + console.log(`v2 confirm-preview-enable-state results: ${resultsPath}`); |
| 106 | + assert.equal(failures.length, 0, `V2 confirm-preview-enable-state failures: ${failures.join(" | ")}`); |
| 107 | + return { failures }; |
| 108 | +} |
| 109 | + |
| 110 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 111 | + try { |
| 112 | + const summary = run(); |
| 113 | + console.log(JSON.stringify(summary, null, 2)); |
| 114 | + } catch (error) { |
| 115 | + console.error(error); |
| 116 | + process.exitCode = 1; |
| 117 | + } |
| 118 | +} |
| 119 | + |
0 commit comments