|
| 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 testPath = path.join(repoRoot, "tests", "runtime", "V2DeterministicStateTransitions.test.mjs"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-deterministic-state-transitions-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 computeStateFromModel(model) { |
| 27 | + if (model.undoEnabled) return "undo_available"; |
| 28 | + if (model.hasMergedResult) return "merge_applied"; |
| 29 | + if (model.previewExists && (model.mergeCanConfirm || model.mergeCanApply)) return "preview_ready"; |
| 30 | + if (model.previewExists) return "preview_active"; |
| 31 | + if (model.mergeCanPreview) return "valid_selection"; |
| 32 | + return "idle"; |
| 33 | +} |
| 34 | + |
| 35 | +function isAllowed(actionName, model) { |
| 36 | + if (actionName === "refresh_load") return true; |
| 37 | + if (actionName === "selection_change") return true; |
| 38 | + if (actionName === "delete_session") return true; |
| 39 | + if (actionName === "preview_merge") return model.mergeCanPreview; |
| 40 | + if (actionName === "confirm_preview") return model.mergeCanConfirm; |
| 41 | + if (actionName === "apply_merge") return model.mergeCanApply; |
| 42 | + if (actionName === "undo_merge") return model.undoEnabled; |
| 43 | + return false; |
| 44 | +} |
| 45 | + |
| 46 | +function nextState(actionName, model) { |
| 47 | + if (actionName === "preview_merge") return "preview_active"; |
| 48 | + if (actionName === "confirm_preview") return "preview_ready"; |
| 49 | + if (actionName === "apply_merge") return model.undoEnabled ? "undo_available" : "merge_applied"; |
| 50 | + return computeStateFromModel(model); |
| 51 | +} |
| 52 | + |
| 53 | +export function run() { |
| 54 | + const failures = []; |
| 55 | + const jsExists = fs.existsSync(jsPath); |
| 56 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 57 | + const jsSyntax = checkSyntax(jsPath); |
| 58 | + const testSyntax = checkSyntax(testPath); |
| 59 | + |
| 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/V2DeterministicStateTransitions.test.mjs failed syntax check."); |
| 63 | + |
| 64 | + const requiredTokens = [ |
| 65 | + "this.workspaceTransitionState = \"idle\";", |
| 66 | + "computeWorkspaceTransitionStateFromModel(model)", |
| 67 | + "isWorkspaceTransitionAllowed(actionName, model)", |
| 68 | + "computeNextWorkspaceTransitionState(actionName, model)", |
| 69 | + "requestWorkspaceTransition(actionName, model)", |
| 70 | + "refreshWorkspaceSessionUiStateModel(actionName = \"refresh_load\")", |
| 71 | + "this.refreshWorkspaceSessionUiStateModel(\"selection_change\")", |
| 72 | + "this.refreshWorkspaceSessionUiStateModel(\"delete_session\")", |
| 73 | + "this.requestWorkspaceTransition(\"preview_merge\", this.computeWorkspaceSessionUiStateModel())", |
| 74 | + "this.requestWorkspaceTransition(\"confirm_preview\", this.computeWorkspaceSessionUiStateModel())", |
| 75 | + "this.requestWorkspaceTransition(\"apply_merge\", this.computeWorkspaceSessionUiStateModel())", |
| 76 | + "this.requestWorkspaceTransition(\"undo_merge\", this.computeWorkspaceSessionUiStateModel())" |
| 77 | + ]; |
| 78 | + requiredTokens.forEach((token) => { |
| 79 | + if (!js.includes(token)) failures.push(`Missing deterministic-transition token/text: ${token}`); |
| 80 | + }); |
| 81 | + |
| 82 | + const preservedModelTokens = [ |
| 83 | + "computeWorkspaceSessionUiStateModel()", |
| 84 | + "renderWorkspaceSessionUiStateModel(model)", |
| 85 | + "Compute Diff is enabled.", |
| 86 | + "Preview Merge is enabled.", |
| 87 | + "Confirm Preview is enabled.", |
| 88 | + "Apply Merge is enabled." |
| 89 | + ]; |
| 90 | + preservedModelTokens.forEach((token) => { |
| 91 | + if (!js.includes(token)) failures.push(`PR_11_264 regression token missing: ${token}`); |
| 92 | + }); |
| 93 | + |
| 94 | + const modelIdle = { |
| 95 | + mergeCanPreview: false, |
| 96 | + mergeCanConfirm: false, |
| 97 | + mergeCanApply: false, |
| 98 | + previewExists: false, |
| 99 | + undoEnabled: false, |
| 100 | + hasMergedResult: false |
| 101 | + }; |
| 102 | + const modelValidSelection = { |
| 103 | + ...modelIdle, |
| 104 | + mergeCanPreview: true |
| 105 | + }; |
| 106 | + const modelPreviewConfirmable = { |
| 107 | + ...modelIdle, |
| 108 | + mergeCanPreview: true, |
| 109 | + previewExists: true, |
| 110 | + mergeCanConfirm: true |
| 111 | + }; |
| 112 | + const modelPreviewApply = { |
| 113 | + ...modelIdle, |
| 114 | + mergeCanPreview: true, |
| 115 | + previewExists: true, |
| 116 | + mergeCanApply: true |
| 117 | + }; |
| 118 | + const modelUndo = { |
| 119 | + ...modelIdle, |
| 120 | + undoEnabled: true |
| 121 | + }; |
| 122 | + |
| 123 | + const initialState = computeStateFromModel(modelIdle); |
| 124 | + if (initialState !== "idle") failures.push("Initial state should be idle."); |
| 125 | + if (isAllowed("preview_merge", modelIdle)) failures.push("Preview must be blocked without a valid selection."); |
| 126 | + if (!isAllowed("preview_merge", modelValidSelection)) failures.push("Preview must be allowed with valid distinct selection."); |
| 127 | + |
| 128 | + const previewState = nextState("preview_merge", modelValidSelection); |
| 129 | + if (previewState !== "preview_active") failures.push("preview_merge should transition to preview_active."); |
| 130 | + if (!isAllowed("confirm_preview", modelPreviewConfirmable)) failures.push("confirm_preview should be allowed for fresh conflict-free preview."); |
| 131 | + |
| 132 | + const confirmState = nextState("confirm_preview", modelPreviewConfirmable); |
| 133 | + if (confirmState !== "preview_ready") failures.push("confirm_preview should transition to preview_ready."); |
| 134 | + if (!isAllowed("apply_merge", modelPreviewApply)) failures.push("apply_merge should be allowed only after confirm-ready model."); |
| 135 | + |
| 136 | + const applyState = nextState("apply_merge", modelPreviewApply); |
| 137 | + if (applyState !== "merge_applied") failures.push("apply_merge should transition to merge_applied before authoritative undo recompute."); |
| 138 | + if (!isAllowed("undo_merge", modelUndo)) failures.push("undo_merge should be allowed when authoritative merge record exists."); |
| 139 | + |
| 140 | + const recomputedAfterUndo = nextState("undo_merge", modelIdle); |
| 141 | + if (recomputedAfterUndo !== "idle") failures.push("undo_merge recompute should return idle when authoritative merge record is cleared."); |
| 142 | + |
| 143 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 144 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 145 | + generatedAt: new Date().toISOString(), |
| 146 | + failures, |
| 147 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 148 | + states: { |
| 149 | + initialState, |
| 150 | + previewState, |
| 151 | + confirmState, |
| 152 | + applyState, |
| 153 | + recomputedAfterUndo |
| 154 | + } |
| 155 | + }, null, 2)} |
| 156 | +`, "utf8"); |
| 157 | + |
| 158 | + console.log(`v2 deterministic-state-transitions results: ${resultsPath}`); |
| 159 | + assert.equal(failures.length, 0, `V2 deterministic-state-transitions failures: ${failures.join(" | ")}`); |
| 160 | + return { failures }; |
| 161 | +} |
| 162 | + |
| 163 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 164 | + try { |
| 165 | + const summary = run(); |
| 166 | + console.log(JSON.stringify(summary, null, 2)); |
| 167 | + } catch (error) { |
| 168 | + console.error(error); |
| 169 | + process.exitCode = 1; |
| 170 | + } |
| 171 | +} |
0 commit comments