|
| 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 toolsRoot = path.join(repoRoot, "tools"); |
| 11 | +const fixturesRoot = path.join(repoRoot, "tests", "fixtures", "v2-tools"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-cross-tool-results.json"); |
| 13 | + |
| 14 | +const FLOWS = [ |
| 15 | + { |
| 16 | + sourceTool: "asset-browser-v2", |
| 17 | + targetTool: "svg-asset-studio-v2" |
| 18 | + }, |
| 19 | + { |
| 20 | + sourceTool: "palette-manager-v2", |
| 21 | + targetTool: "vector-map-editor-v2" |
| 22 | + }, |
| 23 | + { |
| 24 | + sourceTool: "tilemap-studio-v2", |
| 25 | + targetTool: "asset-browser-v2" |
| 26 | + } |
| 27 | +]; |
| 28 | + |
| 29 | +function checkJsSyntax(jsPath) { |
| 30 | + try { |
| 31 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 32 | + cwd: repoRoot, |
| 33 | + stdio: ["ignore", "pipe", "pipe"] |
| 34 | + }); |
| 35 | + return { syntaxValid: true, syntaxError: "" }; |
| 36 | + } catch (error) { |
| 37 | + return { |
| 38 | + syntaxValid: false, |
| 39 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 40 | + }; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function buildTargetUrl(targetTool, hostContextId) { |
| 45 | + return `tools/${targetTool}/index.html?hostContextId=${encodeURIComponent(hostContextId)}`; |
| 46 | +} |
| 47 | + |
| 48 | +function validateFlow(flow) { |
| 49 | + const sourceFixturePath = path.join(fixturesRoot, `${flow.sourceTool}.json`); |
| 50 | + const sourceFixtureExists = fs.existsSync(sourceFixturePath); |
| 51 | + const failures = []; |
| 52 | + |
| 53 | + let sourceFixtureValid = false; |
| 54 | + let sourceHostContextId = ""; |
| 55 | + let fixtureMutationDetected = false; |
| 56 | + let launchUrl = ""; |
| 57 | + |
| 58 | + if (!sourceFixtureExists) { |
| 59 | + failures.push(`Missing source fixture: tests/fixtures/v2-tools/${flow.sourceTool}.json`); |
| 60 | + } else { |
| 61 | + const beforeText = fs.readFileSync(sourceFixturePath, "utf8"); |
| 62 | + let sourceFixture = null; |
| 63 | + try { |
| 64 | + sourceFixture = JSON.parse(beforeText); |
| 65 | + sourceFixtureValid = true; |
| 66 | + } catch { |
| 67 | + sourceFixtureValid = false; |
| 68 | + } |
| 69 | + if (!sourceFixtureValid) { |
| 70 | + failures.push(`Invalid source fixture JSON: tests/fixtures/v2-tools/${flow.sourceTool}.json`); |
| 71 | + } else { |
| 72 | + sourceHostContextId = typeof sourceFixture.hostContextId === "string" ? sourceFixture.hostContextId.trim() : ""; |
| 73 | + if (!sourceHostContextId) { |
| 74 | + failures.push("Source fixture hostContextId is missing or empty."); |
| 75 | + } |
| 76 | + launchUrl = buildTargetUrl(flow.targetTool, sourceHostContextId); |
| 77 | + const afterText = fs.readFileSync(sourceFixturePath, "utf8"); |
| 78 | + fixtureMutationDetected = beforeText !== afterText; |
| 79 | + if (fixtureMutationDetected) { |
| 80 | + failures.push("Source fixture was unexpectedly mutated during flow simulation."); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const targetHtmlPath = path.join(toolsRoot, flow.targetTool, "index.html"); |
| 86 | + const targetJsPath = path.join(toolsRoot, flow.targetTool, "index.js"); |
| 87 | + const targetRouteExists = fs.existsSync(targetHtmlPath); |
| 88 | + if (!targetRouteExists) { |
| 89 | + failures.push(`Missing target route: tools/${flow.targetTool}/index.html`); |
| 90 | + } |
| 91 | + const hostContextIdPreserved = sourceHostContextId |
| 92 | + ? launchUrl.includes(`hostContextId=${encodeURIComponent(sourceHostContextId)}`) |
| 93 | + : false; |
| 94 | + if (sourceHostContextId && !hostContextIdPreserved) { |
| 95 | + failures.push("hostContextId was not preserved in constructed target URL."); |
| 96 | + } |
| 97 | + |
| 98 | + const { syntaxValid, syntaxError } = checkJsSyntax(targetJsPath); |
| 99 | + if (!syntaxValid) { |
| 100 | + failures.push(`Target index.js failed syntax check: tools/${flow.targetTool}/index.js`); |
| 101 | + } |
| 102 | + |
| 103 | + return { |
| 104 | + flow: `${flow.sourceTool} -> ${flow.targetTool}`, |
| 105 | + sourceTool: flow.sourceTool, |
| 106 | + targetTool: flow.targetTool, |
| 107 | + sourceFixturePath: path.relative(repoRoot, sourceFixturePath).replace(/\\/g, "/"), |
| 108 | + sourceFixtureExists, |
| 109 | + sourceFixtureValid, |
| 110 | + sourceHostContextId, |
| 111 | + fixtureMutationDetected, |
| 112 | + launchUrl, |
| 113 | + targetRoutePath: path.relative(repoRoot, targetHtmlPath).replace(/\\/g, "/"), |
| 114 | + targetRouteExists, |
| 115 | + hostContextIdPreserved, |
| 116 | + targetSyntaxPath: path.relative(repoRoot, targetJsPath).replace(/\\/g, "/"), |
| 117 | + targetSyntaxValid: syntaxValid, |
| 118 | + targetSyntaxError: syntaxError, |
| 119 | + failures |
| 120 | + }; |
| 121 | +} |
| 122 | + |
| 123 | +export function run() { |
| 124 | + const rows = FLOWS.map(validateFlow); |
| 125 | + const failures = rows.flatMap((row) => row.failures.map((entry) => `${row.flow}: ${entry}`)); |
| 126 | + |
| 127 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 128 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 129 | + generatedAt: new Date().toISOString(), |
| 130 | + flowCount: rows.length, |
| 131 | + failures, |
| 132 | + rows |
| 133 | + }, null, 2)}\n`, "utf8"); |
| 134 | + |
| 135 | + console.log(`v2 cross-tool flow results: ${resultsPath}`); |
| 136 | + assert.equal(failures.length, 0, `V2 cross-tool flow failures: ${failures.join(" | ")}`); |
| 137 | + return { flowCount: rows.length, failures, rows }; |
| 138 | +} |
| 139 | + |
| 140 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 141 | + try { |
| 142 | + const summary = run(); |
| 143 | + console.log(JSON.stringify(summary, null, 2)); |
| 144 | + } catch (error) { |
| 145 | + console.error(error); |
| 146 | + process.exitCode = 1; |
| 147 | + } |
| 148 | +} |
0 commit comments