|
| 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-merge-conflict-summary-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 truncatePreview(value, maxLength) { |
| 27 | + const text = typeof value === "string" ? value : String(value); |
| 28 | + if (text.length <= maxLength) return text; |
| 29 | + return `${text.slice(0, maxLength)} ...truncated (${text.length - maxLength} more chars)`; |
| 30 | +} |
| 31 | + |
| 32 | +function conflictValuePreview(value) { |
| 33 | + if (value === undefined) return "undefined"; |
| 34 | + const json = JSON.stringify(value); |
| 35 | + return truncatePreview(json === undefined ? String(value) : json, 140); |
| 36 | +} |
| 37 | + |
| 38 | +function buildConflictSummary(conflicts) { |
| 39 | + const entries = Object.entries(conflicts || {}); |
| 40 | + if (entries.length === 0) return ""; |
| 41 | + const lines = [ |
| 42 | + "Conflict preview only. Apply is blocked until conflicts are resolved.", |
| 43 | + `Total conflicts: ${entries.length}` |
| 44 | + ]; |
| 45 | + entries.sort((a, b) => a[0].localeCompare(b[0])).forEach(([path, values]) => { |
| 46 | + lines.push(`- ${path}`); |
| 47 | + lines.push(` source: ${conflictValuePreview(values && Object.prototype.hasOwnProperty.call(values, "a") ? values.a : undefined)}`); |
| 48 | + lines.push(` target: ${conflictValuePreview(values && Object.prototype.hasOwnProperty.call(values, "b") ? values.b : undefined)}`); |
| 49 | + }); |
| 50 | + return lines.join("\n"); |
| 51 | +} |
| 52 | + |
| 53 | +function mergeUiState(conflictCount, fresh, confirmed) { |
| 54 | + const hasConflicts = conflictCount > 0; |
| 55 | + const confirmDisabled = !(fresh && !confirmed && !hasConflicts); |
| 56 | + const applyDisabled = !(fresh && confirmed && !hasConflicts); |
| 57 | + return { confirmDisabled, applyDisabled }; |
| 58 | +} |
| 59 | + |
| 60 | +export function run() { |
| 61 | + const failures = []; |
| 62 | + const htmlExists = fs.existsSync(htmlPath); |
| 63 | + const jsExists = fs.existsSync(jsPath); |
| 64 | + const html = htmlExists ? fs.readFileSync(htmlPath, "utf8") : ""; |
| 65 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 66 | + const jsSyntax = checkSyntax(jsPath); |
| 67 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2MergeConflictSummary.test.mjs")); |
| 68 | + |
| 69 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 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/V2MergeConflictSummary.test.mjs failed syntax check."); |
| 73 | + |
| 74 | + if (!html.includes("id=\"workspaceV2MergeConflictSummary\"")) failures.push("Missing merge conflict summary node."); |
| 75 | + if (!html.includes("id=\"workspaceV2MergeOutput\"")) failures.push("Missing raw merge JSON preview node."); |
| 76 | + if (!js.includes("renderMergeConflictSummary()")) failures.push("Missing renderMergeConflictSummary implementation."); |
| 77 | + if (!js.includes("Conflict preview only. Apply is blocked until conflicts are resolved.")) failures.push("Missing required conflict inline text."); |
| 78 | + if (!js.includes("Total conflicts:")) failures.push("Missing conflict count summary rendering."); |
| 79 | + if (!js.includes("source: ${this.conflictValuePreview")) failures.push("Missing source value preview rendering."); |
| 80 | + if (!js.includes("target: ${this.conflictValuePreview")) failures.push("Missing target value preview rendering."); |
| 81 | + |
| 82 | + const conflicts = { |
| 83 | + "payload.layers[1].tiles[3]": { a: 4, b: 9 }, |
| 84 | + "payload.palette.primary": { a: "#ff0000", b: "#00ff00" } |
| 85 | + }; |
| 86 | + const summary = buildConflictSummary(conflicts); |
| 87 | + if (!summary.includes("Total conflicts: 2")) failures.push("Conflict summary did not include total conflict count."); |
| 88 | + if (!summary.includes("- payload.layers[1].tiles[3]")) failures.push("Conflict summary missing path listing."); |
| 89 | + if (!summary.includes("source: 4")) failures.push("Conflict summary missing source value preview."); |
| 90 | + if (!summary.includes("target: 9")) failures.push("Conflict summary missing target value preview."); |
| 91 | + if (!summary.includes("- payload.palette.primary")) failures.push("Conflict summary missing second path."); |
| 92 | + |
| 93 | + const conflictState = mergeUiState(2, true, false); |
| 94 | + if (!conflictState.confirmDisabled || !conflictState.applyDisabled) { |
| 95 | + failures.push("Confirm/Apply should remain disabled for conflict preview."); |
| 96 | + } |
| 97 | + |
| 98 | + const conflictFreeState = mergeUiState(0, true, false); |
| 99 | + if (conflictFreeState.confirmDisabled) { |
| 100 | + failures.push("Conflict-free fresh preview should enable Confirm."); |
| 101 | + } |
| 102 | + |
| 103 | + const noConflictSummary = buildConflictSummary({}); |
| 104 | + if (noConflictSummary !== "") { |
| 105 | + failures.push("Conflict-free preview should not render conflict summary."); |
| 106 | + } |
| 107 | + |
| 108 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 109 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 110 | + generatedAt: new Date().toISOString(), |
| 111 | + failures, |
| 112 | + checks: { htmlExists, jsExists, jsSyntax, testSyntax }, |
| 113 | + sampleSummary: summary, |
| 114 | + states: { conflictState, conflictFreeState } |
| 115 | + }, null, 2)}\n`, "utf8"); |
| 116 | + |
| 117 | + console.log(`v2 merge-conflict-summary results: ${resultsPath}`); |
| 118 | + assert.equal(failures.length, 0, `V2 merge-conflict-summary failures: ${failures.join(" | ")}`); |
| 119 | + return { failures }; |
| 120 | +} |
| 121 | + |
| 122 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 123 | + try { |
| 124 | + const summary = run(); |
| 125 | + console.log(JSON.stringify(summary, null, 2)); |
| 126 | + } catch (error) { |
| 127 | + console.error(error); |
| 128 | + process.exitCode = 1; |
| 129 | + } |
| 130 | +} |
| 131 | + |
0 commit comments