|
| 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 testPath = path.join(repoRoot, "tests", "runtime", "V2DiffViewerSummaryCounts.test.mjs"); |
| 13 | +const resultsPath = path.join(repoRoot, "tmp", "v2-diff-viewer-summary-counts-results.json"); |
| 14 | + |
| 15 | +function checkSyntax(filePath) { |
| 16 | + try { |
| 17 | + execFileSync(process.execPath, ["--check", filePath], { |
| 18 | + cwd: repoRoot, |
| 19 | + stdio: ["ignore", "pipe", "pipe"] |
| 20 | + }); |
| 21 | + return { ok: true, error: "" }; |
| 22 | + } catch (error) { |
| 23 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function buildDiffSummary(diff) { |
| 28 | + const addedCount = Object.keys(diff.added).length; |
| 29 | + const removedCount = Object.keys(diff.removed).length; |
| 30 | + const changedCount = Object.keys(diff.changed).length; |
| 31 | + if (addedCount === 0 && removedCount === 0 && changedCount === 0) { |
| 32 | + return "No differences (added: 0, removed: 0, changed: 0)"; |
| 33 | + } |
| 34 | + return `Differences detected (added: ${addedCount}, removed: ${removedCount}, changed: ${changedCount})`; |
| 35 | +} |
| 36 | + |
| 37 | +export function run() { |
| 38 | + const failures = []; |
| 39 | + const htmlExists = fs.existsSync(htmlPath); |
| 40 | + const jsExists = fs.existsSync(jsPath); |
| 41 | + const html = htmlExists ? fs.readFileSync(htmlPath, "utf8") : ""; |
| 42 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 43 | + const jsSyntax = checkSyntax(jsPath); |
| 44 | + const testSyntax = checkSyntax(testPath); |
| 45 | + |
| 46 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 47 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 48 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 49 | + if (!testSyntax.ok) failures.push("tests/runtime/V2DiffViewerSummaryCounts.test.mjs failed syntax check."); |
| 50 | + |
| 51 | + if (!html.includes('id="workspaceV2DiffSummary"')) { |
| 52 | + failures.push("Missing Diff summary node in Workspace V2 Diff Viewer."); |
| 53 | + } |
| 54 | + if (!js.includes("this.diffSummaryNode = document.getElementById(\"workspaceV2DiffSummary\");")) { |
| 55 | + failures.push("Diff summary node is not wired in JS."); |
| 56 | + } |
| 57 | + if (!js.includes("No differences (added: 0, removed: 0, changed: 0)")) { |
| 58 | + failures.push("Missing no-difference summary text."); |
| 59 | + } |
| 60 | + if (!js.includes("Differences detected (added: ${addedCount}, removed: ${removedCount}, changed: ${changedCount})")) { |
| 61 | + failures.push("Missing diff-exists summary text."); |
| 62 | + } |
| 63 | + if (!js.includes("this.diffSummaryNode.textContent = \"\";")) { |
| 64 | + failures.push("Diff summary clear path missing for stale summary reset."); |
| 65 | + } |
| 66 | + |
| 67 | + const noDiffSummary = buildDiffSummary({ added: {}, removed: {}, changed: {} }); |
| 68 | + const withDiffSummary = buildDiffSummary({ |
| 69 | + added: { "a.b": 1, "a.c": 2 }, |
| 70 | + removed: { old: true }, |
| 71 | + changed: { "x.y": { from: 1, to: 2 } } |
| 72 | + }); |
| 73 | + if (noDiffSummary !== "No differences (added: 0, removed: 0, changed: 0)") { |
| 74 | + failures.push("No-diff summary formatting mismatch."); |
| 75 | + } |
| 76 | + if (withDiffSummary !== "Differences detected (added: 2, removed: 1, changed: 1)") { |
| 77 | + failures.push("Diff summary count formatting mismatch."); |
| 78 | + } |
| 79 | + |
| 80 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 81 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 82 | + generatedAt: new Date().toISOString(), |
| 83 | + failures, |
| 84 | + checks: { |
| 85 | + htmlExists, |
| 86 | + jsExists, |
| 87 | + jsSyntax, |
| 88 | + testSyntax |
| 89 | + }, |
| 90 | + scenarios: { |
| 91 | + noDiffSummary, |
| 92 | + withDiffSummary |
| 93 | + } |
| 94 | + }, null, 2)} |
| 95 | +`, "utf8"); |
| 96 | + |
| 97 | + console.log(`v2 diff-viewer summary-counts results: ${resultsPath}`); |
| 98 | + assert.equal(failures.length, 0, `V2 diff-viewer summary-counts 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 | +} |
0 commit comments