|
| 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-cross-tool-merge-block-results.json"); |
| 12 | + |
| 13 | +function checkSyntax(filePath) { |
| 14 | + try { |
| 15 | + execFileSync(process.execPath, ["--check", filePath], { |
| 16 | + cwd: repoRoot, |
| 17 | + stdio: ["ignore", "pipe", "pipe"] |
| 18 | + }); |
| 19 | + return { ok: true, error: "" }; |
| 20 | + } catch (error) { |
| 21 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function mergeUiState({ canPreviewMerge, previewExists, previewFresh, previewConfirmed, conflictCount }) { |
| 26 | + const hasConflicts = previewExists && conflictCount > 0; |
| 27 | + const confirmDisabled = !(previewExists && !previewConfirmed && previewFresh && !hasConflicts); |
| 28 | + const applyDisabled = !(previewExists && previewConfirmed && previewFresh && !hasConflicts); |
| 29 | + return { previewEnabled: canPreviewMerge, confirmDisabled, applyDisabled }; |
| 30 | +} |
| 31 | + |
| 32 | +function evaluateCrossTool(leftToolId, rightToolId) { |
| 33 | + if (leftToolId && rightToolId && leftToolId !== rightToolId) { |
| 34 | + return { |
| 35 | + blocked: true, |
| 36 | + rawJsonRendered: false, |
| 37 | + message: [ |
| 38 | + "Cross-tool merge is not supported. Select two sessions with the same toolId.", |
| 39 | + `Session A toolId: ${leftToolId}`, |
| 40 | + `Session B toolId: ${rightToolId}` |
| 41 | + ].join("\n") |
| 42 | + }; |
| 43 | + } |
| 44 | + return { blocked: false, rawJsonRendered: true, message: "" }; |
| 45 | +} |
| 46 | + |
| 47 | +export function run() { |
| 48 | + const failures = []; |
| 49 | + const jsExists = fs.existsSync(jsPath); |
| 50 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 51 | + const jsSyntax = checkSyntax(jsPath); |
| 52 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2CrossToolMergeBlock.test.mjs")); |
| 53 | + |
| 54 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 55 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 56 | + if (!testSyntax.ok) failures.push("tests/runtime/V2CrossToolMergeBlock.test.mjs failed syntax check."); |
| 57 | + |
| 58 | + const requiredTokens = [ |
| 59 | + "Cross-tool merge is not supported. Select two sessions with the same toolId.", |
| 60 | + "Session A toolId:", |
| 61 | + "Session B toolId:", |
| 62 | + "if (leftToolId && rightToolId && leftToolId !== rightToolId) {", |
| 63 | + "this.pendingMergePreview = null;", |
| 64 | + "this.updateMergeSelectionFeedbackAndState();" |
| 65 | + ]; |
| 66 | + requiredTokens.forEach((token) => { |
| 67 | + if (!js.includes(token)) failures.push(`Missing cross-tool merge block token/text: ${token}`); |
| 68 | + }); |
| 69 | + |
| 70 | + const crossTool = evaluateCrossTool("palette-manager-v2", "asset-browser-v2"); |
| 71 | + if (!crossTool.blocked) failures.push("Cross-tool merge should be blocked."); |
| 72 | + if (crossTool.rawJsonRendered) failures.push("Cross-tool merge block should not render raw merge JSON."); |
| 73 | + if (!crossTool.message.includes("Session A toolId: palette-manager-v2")) failures.push("Cross-tool block must show Session A toolId."); |
| 74 | + if (!crossTool.message.includes("Session B toolId: asset-browser-v2")) failures.push("Cross-tool block must show Session B toolId."); |
| 75 | + const crossToolState = mergeUiState({ canPreviewMerge: true, previewExists: false, previewFresh: false, previewConfirmed: false, conflictCount: 0 }); |
| 76 | + if (!crossToolState.previewEnabled) failures.push("Preview button should remain enabled for cross-tool validation message."); |
| 77 | + if (!crossToolState.confirmDisabled || !crossToolState.applyDisabled) failures.push("Confirm/Apply should remain disabled after cross-tool block."); |
| 78 | + |
| 79 | + const sameToolConflict = evaluateCrossTool("asset-browser-v2", "asset-browser-v2"); |
| 80 | + if (sameToolConflict.blocked) failures.push("Same-tool merge should not be blocked by cross-tool guard."); |
| 81 | + if (!sameToolConflict.rawJsonRendered) failures.push("Same-tool preview should continue to render merge output."); |
| 82 | + const sameToolConflictState = mergeUiState({ canPreviewMerge: true, previewExists: true, previewFresh: true, previewConfirmed: false, conflictCount: 2 }); |
| 83 | + if (sameToolConflictState.confirmDisabled !== true || sameToolConflictState.applyDisabled !== true) { |
| 84 | + failures.push("Same-tool conflict preview should keep Confirm/Apply disabled."); |
| 85 | + } |
| 86 | + const sameToolConflictFreeState = mergeUiState({ canPreviewMerge: true, previewExists: true, previewFresh: true, previewConfirmed: false, conflictCount: 0 }); |
| 87 | + if (sameToolConflictFreeState.confirmDisabled) failures.push("Same-tool conflict-free preview should enable Confirm."); |
| 88 | + |
| 89 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 90 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 91 | + generatedAt: new Date().toISOString(), |
| 92 | + failures, |
| 93 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 94 | + scenarios: { crossTool, crossToolState, sameToolConflict, sameToolConflictState, sameToolConflictFreeState } |
| 95 | + }, null, 2)}\n`, "utf8"); |
| 96 | + |
| 97 | + console.log(`v2 cross-tool-merge-block results: ${resultsPath}`); |
| 98 | + assert.equal(failures.length, 0, `V2 cross-tool-merge-block failures: ${failures.join(" | ")}`); |
| 99 | + return { failures }; |
| 100 | +} |
| 101 | + |
| 102 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 103 | + try { |
| 104 | + const summary = run(); |
| 105 | + console.log(JSON.stringify(summary, null, 2)); |
| 106 | + } catch (error) { |
| 107 | + console.error(error); |
| 108 | + process.exitCode = 1; |
| 109 | + } |
| 110 | +} |
| 111 | + |
0 commit comments