|
| 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-selection-feedback-enable-state-results.json"); |
| 13 | + |
| 14 | +function read(filePath) { |
| 15 | + return fs.readFileSync(filePath, "utf8"); |
| 16 | +} |
| 17 | + |
| 18 | +function checkSyntax(jsPath) { |
| 19 | + try { |
| 20 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 21 | + cwd: repoRoot, |
| 22 | + stdio: ["ignore", "pipe", "pipe"] |
| 23 | + }); |
| 24 | + return { ok: true, error: "" }; |
| 25 | + } catch (error) { |
| 26 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function isActionEnabled(leftId, rightId) { |
| 31 | + return Boolean(leftId && rightId && leftId !== rightId); |
| 32 | +} |
| 33 | + |
| 34 | +export function run() { |
| 35 | + const failures = []; |
| 36 | + const jsExists = fs.existsSync(workspaceJsPath); |
| 37 | + const htmlExists = fs.existsSync(workspaceHtmlPath); |
| 38 | + const js = jsExists ? read(workspaceJsPath) : ""; |
| 39 | + const html = htmlExists ? read(workspaceHtmlPath) : ""; |
| 40 | + const syntax = checkSyntax(workspaceJsPath); |
| 41 | + |
| 42 | + const htmlHasLabels = |
| 43 | + html.includes("workspaceV2DiffLeftSelectedLabel") && |
| 44 | + html.includes("workspaceV2DiffRightSelectedLabel") && |
| 45 | + html.includes("workspaceV2MergeLeftSelectedLabel") && |
| 46 | + html.includes("workspaceV2MergeRightSelectedLabel"); |
| 47 | + const htmlHasNoSessionDefaults = (html.match(/No session selected/g) || []).length >= 4; |
| 48 | + |
| 49 | + const jsHasDiffUpdater = js.includes("updateDiffSelectionFeedbackAndState()"); |
| 50 | + const jsHasMergeUpdater = js.includes("updateMergeSelectionFeedbackAndState()"); |
| 51 | + const jsHasChangeListeners = |
| 52 | + js.includes("this.diffLeftSelect.addEventListener(\"change\"") && |
| 53 | + js.includes("this.diffRightSelect.addEventListener(\"change\"") && |
| 54 | + js.includes("this.mergeLeftSelect.addEventListener(\"change\"") && |
| 55 | + js.includes("this.mergeRightSelect.addEventListener(\"change\""); |
| 56 | + const jsHasDiffDisableRule = js.includes("this.computeDiffButton.disabled = !(left && right && left.id !== right.id);"); |
| 57 | + const jsHasMergeDisableRule = js.includes("this.computeMergeButton.disabled = !(left && right && left.id !== right.id);"); |
| 58 | + const jsHasConfirmAfterPreview = js.includes("this.confirmMergeButton.disabled = false;"); |
| 59 | + const jsHasApplyAfterConfirm = js.includes("this.applyMergeButton.disabled = false;"); |
| 60 | + |
| 61 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 62 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 63 | + if (!syntax.ok) failures.push("workspace-v2/index.js failed syntax check."); |
| 64 | + if (!htmlHasLabels) failures.push("Missing inline selected-session feedback labels in HTML."); |
| 65 | + if (!htmlHasNoSessionDefaults) failures.push("Missing 'No session selected' defaults in HTML."); |
| 66 | + if (!jsHasDiffUpdater) failures.push("Missing diff selection feedback/state updater."); |
| 67 | + if (!jsHasMergeUpdater) failures.push("Missing merge selection feedback/state updater."); |
| 68 | + if (!jsHasChangeListeners) failures.push("Missing select change listeners for state wiring."); |
| 69 | + if (!jsHasDiffDisableRule) failures.push("Missing Compute Diff enable/disable rule."); |
| 70 | + if (!jsHasMergeDisableRule) failures.push("Missing Preview Merge enable/disable rule."); |
| 71 | + if (!jsHasConfirmAfterPreview) failures.push("Missing confirm button enable state after preview."); |
| 72 | + if (!jsHasApplyAfterConfirm) failures.push("Missing apply button enable state after confirm."); |
| 73 | + |
| 74 | + const stateChecks = { |
| 75 | + noneSelected: isActionEnabled("", ""), |
| 76 | + onlyASelected: isActionEnabled("a", ""), |
| 77 | + onlyBSelected: isActionEnabled("", "b"), |
| 78 | + sameSelected: isActionEnabled("a", "a"), |
| 79 | + distinctSelected: isActionEnabled("a", "b") |
| 80 | + }; |
| 81 | + |
| 82 | + if (stateChecks.noneSelected) failures.push("Buttons should be disabled when nothing is selected."); |
| 83 | + if (stateChecks.onlyASelected) failures.push("Buttons should be disabled when only Session A is selected."); |
| 84 | + if (stateChecks.onlyBSelected) failures.push("Buttons should be disabled when only Session B is selected."); |
| 85 | + if (stateChecks.sameSelected) failures.push("Buttons should be disabled when Session A and Session B are the same."); |
| 86 | + if (!stateChecks.distinctSelected) failures.push("Buttons should be enabled when Session A and Session B are distinct."); |
| 87 | + |
| 88 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 89 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 90 | + generatedAt: new Date().toISOString(), |
| 91 | + failures, |
| 92 | + checks: { |
| 93 | + jsExists, |
| 94 | + htmlExists, |
| 95 | + syntax, |
| 96 | + htmlHasLabels, |
| 97 | + htmlHasNoSessionDefaults, |
| 98 | + jsHasDiffUpdater, |
| 99 | + jsHasMergeUpdater, |
| 100 | + jsHasChangeListeners, |
| 101 | + jsHasDiffDisableRule, |
| 102 | + jsHasMergeDisableRule, |
| 103 | + jsHasConfirmAfterPreview, |
| 104 | + jsHasApplyAfterConfirm, |
| 105 | + stateChecks |
| 106 | + } |
| 107 | + }, null, 2)}\n`, "utf8"); |
| 108 | + |
| 109 | + console.log(`v2 selection feedback/enable-state results: ${resultsPath}`); |
| 110 | + assert.equal(failures.length, 0, `V2 selection feedback/enable-state failures: ${failures.join(" | ")}`); |
| 111 | + return { failures }; |
| 112 | +} |
| 113 | + |
| 114 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 115 | + try { |
| 116 | + const summary = run(); |
| 117 | + console.log(JSON.stringify(summary, null, 2)); |
| 118 | + } catch (error) { |
| 119 | + console.error(error); |
| 120 | + process.exitCode = 1; |
| 121 | + } |
| 122 | +} |
0 commit comments