|
| 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 jsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 11 | +const resultsPath = path.join(repoRoot, "tmp", "v2-selection-sync-row-actions-results.json"); |
| 12 | + |
| 13 | +function readText(filePath) { |
| 14 | + return fs.readFileSync(filePath, "utf8"); |
| 15 | +} |
| 16 | + |
| 17 | +function checkSyntax(filePath) { |
| 18 | + try { |
| 19 | + execFileSync(process.execPath, ["--check", filePath], { |
| 20 | + cwd: repoRoot, |
| 21 | + stdio: ["ignore", "pipe", "pipe"] |
| 22 | + }); |
| 23 | + return { ok: true, error: "" }; |
| 24 | + } catch (error) { |
| 25 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function findById(candidates, id) { |
| 30 | + if (!Array.isArray(candidates)) return null; |
| 31 | + return candidates.find((entry) => entry.id === id) || null; |
| 32 | +} |
| 33 | + |
| 34 | +function findByContext(candidates, contextId) { |
| 35 | + if (!Array.isArray(candidates)) return null; |
| 36 | + return candidates.find((entry) => entry.contextId === contextId) || null; |
| 37 | +} |
| 38 | + |
| 39 | +function syncSlots(state, contextId) { |
| 40 | + const selected = findByContext(state.candidates, contextId); |
| 41 | + if (!selected) return false; |
| 42 | + const leftEntry = findById(state.candidates, state.left); |
| 43 | + const rightEntry = findById(state.candidates, state.right); |
| 44 | + if (!leftEntry) { |
| 45 | + if (rightEntry && rightEntry.id === selected.id) return false; |
| 46 | + state.left = selected.id; |
| 47 | + return true; |
| 48 | + } |
| 49 | + if (!rightEntry) { |
| 50 | + if (leftEntry.id === selected.id) return false; |
| 51 | + state.right = selected.id; |
| 52 | + return true; |
| 53 | + } |
| 54 | + return false; |
| 55 | +} |
| 56 | + |
| 57 | +function computeEnabled(state) { |
| 58 | + const leftEntry = findById(state.candidates, state.left); |
| 59 | + const rightEntry = findById(state.candidates, state.right); |
| 60 | + return Boolean(leftEntry && rightEntry && leftEntry.id !== rightEntry.id); |
| 61 | +} |
| 62 | + |
| 63 | +export function run() { |
| 64 | + const failures = []; |
| 65 | + const jsExists = fs.existsSync(jsPath); |
| 66 | + const js = jsExists ? readText(jsPath) : ""; |
| 67 | + const jsSyntax = checkSyntax(jsPath); |
| 68 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2SelectionSyncRowActions.test.mjs")); |
| 69 | + |
| 70 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 71 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 72 | + if (!testSyntax.ok) failures.push("tests/runtime/V2SelectionSyncRowActions.test.mjs failed syntax check."); |
| 73 | + |
| 74 | + const requiredTokens = [ |
| 75 | + "syncSelectionSlotsFromContextId(leftSelectNode, rightSelectNode, candidates, contextId)", |
| 76 | + "syncDiffAndMergeSelectionSlotsFromContextId(contextId)", |
| 77 | + "this.syncDiffAndMergeSelectionSlotsFromContextId(sessionId.trim());", |
| 78 | + "this.updateDiffSelectionFeedbackAndState();", |
| 79 | + "this.updateMergeSelectionFeedbackAndState();" |
| 80 | + ]; |
| 81 | + requiredTokens.forEach((token) => { |
| 82 | + if (!js.includes(token)) { |
| 83 | + failures.push(`Missing required selection-sync token: ${token}`); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + const candidates = [ |
| 88 | + { id: "library:s1", contextId: "s1" }, |
| 89 | + { id: "library:s2", contextId: "s2" }, |
| 90 | + { id: "library:s3", contextId: "s3" } |
| 91 | + ]; |
| 92 | + |
| 93 | + const state = { candidates, left: "", right: "" }; |
| 94 | + const firstFill = syncSlots(state, "s1"); |
| 95 | + if (!firstFill || state.left !== "library:s1" || state.right !== "") { |
| 96 | + failures.push("First row action should fill Session A when empty."); |
| 97 | + } |
| 98 | + |
| 99 | + const secondFill = syncSlots(state, "s2"); |
| 100 | + if (!secondFill || state.left !== "library:s1" || state.right !== "library:s2") { |
| 101 | + failures.push("Second row action should fill Session B when A already selected."); |
| 102 | + } |
| 103 | + |
| 104 | + const thirdNoOverwrite = syncSlots(state, "s3"); |
| 105 | + if (thirdNoOverwrite || state.left !== "library:s1" || state.right !== "library:s2") { |
| 106 | + failures.push("Third row action should not overwrite when A/B already selected."); |
| 107 | + } |
| 108 | + |
| 109 | + const dupBlocked = syncSlots(state, "s1"); |
| 110 | + if (dupBlocked || state.left !== "library:s1" || state.right !== "library:s2") { |
| 111 | + failures.push("Same session must not be duplicated across A/B."); |
| 112 | + } |
| 113 | + |
| 114 | + const enabledAfterTwo = computeEnabled({ candidates, left: "library:s1", right: "library:s2" }); |
| 115 | + const disabledWithOne = computeEnabled({ candidates, left: "library:s1", right: "" }); |
| 116 | + if (!enabledAfterTwo) failures.push("Diff/Merge should be enabled after valid A/B selections."); |
| 117 | + if (disabledWithOne) failures.push("Diff/Merge should remain disabled with one selection."); |
| 118 | + |
| 119 | + const useInLibraryWiresSync = js.includes("useSavedSessionIdInLibraryInput(sessionId)") && js.includes("this.syncDiffAndMergeSelectionSlotsFromContextId(sessionId.trim());"); |
| 120 | + const loadWiresSync = js.includes("loadSavedSessionById(sessionId)") && js.includes("this.syncDiffAndMergeSelectionSlotsFromContextId(sessionId.trim());"); |
| 121 | + if (!useInLibraryWiresSync) failures.push("Use in Library does not wire selection sync."); |
| 122 | + if (!loadWiresSync) failures.push("Load row action does not wire selection sync."); |
| 123 | + |
| 124 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 125 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 126 | + generatedAt: new Date().toISOString(), |
| 127 | + failures, |
| 128 | + checks: { |
| 129 | + jsExists, |
| 130 | + jsSyntax, |
| 131 | + testSyntax |
| 132 | + }, |
| 133 | + scenarios: { |
| 134 | + state, |
| 135 | + firstFill, |
| 136 | + secondFill, |
| 137 | + thirdNoOverwrite, |
| 138 | + dupBlocked, |
| 139 | + enabledAfterTwo, |
| 140 | + disabledWithOne |
| 141 | + } |
| 142 | + }, null, 2)}\n`, "utf8"); |
| 143 | + |
| 144 | + console.log(`v2 selection-sync row-actions results: ${resultsPath}`); |
| 145 | + assert.equal(failures.length, 0, `V2 selection-sync row-actions 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