|
| 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-result-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 previewSummary(preview) { |
| 27 | + const addedCount = Object.keys(preview.changes.added).length; |
| 28 | + const updatedCount = Object.keys(preview.changes.updated).length; |
| 29 | + const unchangedCount = Object.keys(preview.changes.unchanged).length; |
| 30 | + const conflictCount = preview.conflictCount; |
| 31 | + return [ |
| 32 | + "Merge Preview Summary", |
| 33 | + `Source Session ID: ${preview.source.id}`, |
| 34 | + `Target Session ID: ${preview.target.id}`, |
| 35 | + `Tool ID: ${preview.selectedToolId}`, |
| 36 | + `Added: ${addedCount}`, |
| 37 | + `Updated: ${updatedCount}`, |
| 38 | + `Unchanged: ${unchangedCount}`, |
| 39 | + `Conflicts: ${conflictCount}` |
| 40 | + ].join("\n"); |
| 41 | +} |
| 42 | + |
| 43 | +function applySummary(applied) { |
| 44 | + return [ |
| 45 | + "Merge Apply Summary", |
| 46 | + `Merged Session ID: ${applied.hostContextId}`, |
| 47 | + `Tool ID: ${applied.toolId}`, |
| 48 | + `Timestamp: ${applied.timestamp}`, |
| 49 | + `Added: ${applied.addedCount}`, |
| 50 | + `Updated: ${applied.updatedCount}`, |
| 51 | + `Unchanged: ${applied.unchangedCount}` |
| 52 | + ].join("\n"); |
| 53 | +} |
| 54 | + |
| 55 | +export function run() { |
| 56 | + const failures = []; |
| 57 | + const htmlExists = fs.existsSync(htmlPath); |
| 58 | + const jsExists = fs.existsSync(jsPath); |
| 59 | + const html = htmlExists ? fs.readFileSync(htmlPath, "utf8") : ""; |
| 60 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 61 | + const jsSyntax = checkSyntax(jsPath); |
| 62 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2MergeResultSummary.test.mjs")); |
| 63 | + |
| 64 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 65 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 66 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 67 | + if (!testSyntax.ok) failures.push("tests/runtime/V2MergeResultSummary.test.mjs failed syntax check."); |
| 68 | + |
| 69 | + const requiredHtml = [ |
| 70 | + "id=\"workspaceV2MergeResultSummary\"", |
| 71 | + "No merge summary yet.", |
| 72 | + "id=\"workspaceV2MergeOutput\"" |
| 73 | + ]; |
| 74 | + requiredHtml.forEach((token) => { |
| 75 | + if (!html.includes(token)) failures.push(`Missing merge summary/output HTML token: ${token}`); |
| 76 | + }); |
| 77 | + |
| 78 | + const requiredJs = [ |
| 79 | + "setMergeResultSummary(message)", |
| 80 | + "setMergePreviewSummary(preview)", |
| 81 | + "setMergeApplySummary(hostContextId, toolId, timestamp, changes)", |
| 82 | + "Merge Preview Summary", |
| 83 | + "Preview confirmed. Apply Merge is ready.", |
| 84 | + "Merge Apply Summary", |
| 85 | + "Removed Session ID:", |
| 86 | + "Preview summary is stale because Session A or Session B changed. Run Preview Merge again.", |
| 87 | + "this.setMergeResultSummary(\"Merge preview blocked. Session A and Session B selections are missing.\");" |
| 88 | + ]; |
| 89 | + requiredJs.forEach((token) => { |
| 90 | + if (!js.includes(token)) failures.push(`Missing merge summary JS token/text: ${token}`); |
| 91 | + }); |
| 92 | + |
| 93 | + const preview = { |
| 94 | + source: { id: "history:asset-browser-v2-a1" }, |
| 95 | + target: { id: "history:asset-browser-v2-b2" }, |
| 96 | + selectedToolId: "asset-browser-v2", |
| 97 | + changes: { |
| 98 | + added: { "payload.layers.2": {} }, |
| 99 | + updated: { "payload.zoom": { from: 1, to: 2 } }, |
| 100 | + unchanged: { "payload.toolId": "asset-browser-v2" } |
| 101 | + }, |
| 102 | + conflictCount: 0 |
| 103 | + }; |
| 104 | + const previewSummaryText = previewSummary(preview); |
| 105 | + if (!previewSummaryText.includes("Merge Preview Summary")) failures.push("Preview summary missing heading."); |
| 106 | + if (!previewSummaryText.includes("Source Session ID: history:asset-browser-v2-a1")) failures.push("Preview summary missing source id."); |
| 107 | + if (!previewSummaryText.includes("Target Session ID: history:asset-browser-v2-b2")) failures.push("Preview summary missing target id."); |
| 108 | + if (!previewSummaryText.includes("Tool ID: asset-browser-v2")) failures.push("Preview summary missing tool id."); |
| 109 | + if (!previewSummaryText.includes("Added: 1") || !previewSummaryText.includes("Updated: 1") || !previewSummaryText.includes("Unchanged: 1")) { |
| 110 | + failures.push("Preview summary missing added/updated/unchanged counts."); |
| 111 | + } |
| 112 | + |
| 113 | + const confirmSummaryText = "Preview confirmed. Apply Merge is ready."; |
| 114 | + if (confirmSummaryText !== "Preview confirmed. Apply Merge is ready.") failures.push("Confirm summary mismatch."); |
| 115 | + |
| 116 | + const applied = { |
| 117 | + hostContextId: "asset-browser-v2-merged-1777777777777-abc123xy", |
| 118 | + toolId: "asset-browser-v2", |
| 119 | + timestamp: "2026-05-02T00:00:00.000Z", |
| 120 | + addedCount: 2, |
| 121 | + updatedCount: 1, |
| 122 | + unchangedCount: 3 |
| 123 | + }; |
| 124 | + const applySummaryText = applySummary(applied); |
| 125 | + if (!applySummaryText.includes("Merge Apply Summary")) failures.push("Apply summary missing heading."); |
| 126 | + if (!applySummaryText.includes(`Merged Session ID: ${applied.hostContextId}`)) failures.push("Apply summary missing merged session id."); |
| 127 | + if (!applySummaryText.includes(`Timestamp: ${applied.timestamp}`)) failures.push("Apply summary missing timestamp."); |
| 128 | + |
| 129 | + const undoSummaryText = `Last merged session removed.\nRemoved Session ID: ${applied.hostContextId}`; |
| 130 | + if (!undoSummaryText.includes("Last merged session removed.")) failures.push("Undo summary missing success message."); |
| 131 | + if (!undoSummaryText.includes(`Removed Session ID: ${applied.hostContextId}`)) failures.push("Undo summary missing removed session id."); |
| 132 | + |
| 133 | + const blockedReason = "Merge preview blocked. Session A and Session B selections are missing."; |
| 134 | + const staleSummary = "Preview summary is stale because Session A or Session B changed. Run Preview Merge again."; |
| 135 | + if (!blockedReason.startsWith("Merge preview blocked.")) failures.push("Blocked preview reason mismatch."); |
| 136 | + if (!staleSummary.startsWith("Preview summary is stale")) failures.push("Stale preview summary mismatch."); |
| 137 | + |
| 138 | + const rawPreviewJson = JSON.stringify({ |
| 139 | + source: preview.source, |
| 140 | + target: preview.target, |
| 141 | + changes: preview.changes, |
| 142 | + conflicts: {}, |
| 143 | + conflictCount: 0, |
| 144 | + confirmed: false, |
| 145 | + canApply: true |
| 146 | + }, null, 2); |
| 147 | + if (!rawPreviewJson.includes("\"source\"") || !rawPreviewJson.includes("\"target\"") || !rawPreviewJson.includes("\"changes\"")) { |
| 148 | + failures.push("Raw preview JSON is not available after summary updates."); |
| 149 | + } |
| 150 | + |
| 151 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 152 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 153 | + generatedAt: new Date().toISOString(), |
| 154 | + failures, |
| 155 | + checks: { htmlExists, jsExists, jsSyntax, testSyntax }, |
| 156 | + samples: { |
| 157 | + previewSummaryText, |
| 158 | + confirmSummaryText, |
| 159 | + applySummaryText, |
| 160 | + undoSummaryText, |
| 161 | + blockedReason, |
| 162 | + staleSummary, |
| 163 | + rawPreviewJsonVisible: Boolean(rawPreviewJson) |
| 164 | + } |
| 165 | + }, null, 2)}\n`, "utf8"); |
| 166 | + |
| 167 | + console.log(`v2 merge-result-summary results: ${resultsPath}`); |
| 168 | + assert.equal(failures.length, 0, `V2 merge-result-summary failures: ${failures.join(" | ")}`); |
| 169 | + return { failures }; |
| 170 | +} |
| 171 | + |
| 172 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 173 | + try { |
| 174 | + const summary = run(); |
| 175 | + console.log(JSON.stringify(summary, null, 2)); |
| 176 | + } catch (error) { |
| 177 | + console.error(error); |
| 178 | + process.exitCode = 1; |
| 179 | + } |
| 180 | +} |
0 commit comments