|
| 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 resultsPath = path.join(repoRoot, "tmp", "v2-back-nav-results.json"); |
| 12 | + |
| 13 | +const FLOWS = [ |
| 14 | + { |
| 15 | + first: "workspace-v2", |
| 16 | + second: "asset-browser-v2", |
| 17 | + third: "svg-asset-studio-v2", |
| 18 | + secondActionQueryFrom: "workspace-v2", |
| 19 | + thirdActionQueryFrom: "asset-browser-v2" |
| 20 | + }, |
| 21 | + { |
| 22 | + first: "workspace-v2", |
| 23 | + second: "palette-manager-v2", |
| 24 | + third: "vector-map-editor-v2", |
| 25 | + secondActionQueryFrom: "workspace-v2", |
| 26 | + thirdActionQueryFrom: "palette-manager-v2" |
| 27 | + }, |
| 28 | + { |
| 29 | + first: "workspace-v2", |
| 30 | + second: "tilemap-studio-v2", |
| 31 | + third: "asset-browser-v2", |
| 32 | + secondActionQueryFrom: "workspace-v2", |
| 33 | + thirdActionQueryFrom: "tilemap-studio-v2" |
| 34 | + } |
| 35 | +]; |
| 36 | + |
| 37 | +function readText(filePath) { |
| 38 | + return fs.readFileSync(filePath, "utf8"); |
| 39 | +} |
| 40 | + |
| 41 | +function checkJsSyntax(jsPath) { |
| 42 | + try { |
| 43 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 44 | + cwd: repoRoot, |
| 45 | + stdio: ["ignore", "pipe", "pipe"] |
| 46 | + }); |
| 47 | + return { syntaxValid: true, syntaxError: "" }; |
| 48 | + } catch (error) { |
| 49 | + return { |
| 50 | + syntaxValid: false, |
| 51 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 52 | + }; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function generateHostContextId(flow) { |
| 57 | + const randomPart = Math.random().toString(36).slice(2, 10); |
| 58 | + return `${flow.second}-back-nav-${Date.now()}-${randomPart}`; |
| 59 | +} |
| 60 | + |
| 61 | +function buildToolUrl(toolId, hostContextId, fromToolId) { |
| 62 | + const url = new URL(`tools/${toolId}/index.html`, "http://localhost/"); |
| 63 | + url.searchParams.set("hostContextId", hostContextId); |
| 64 | + if (fromToolId) { |
| 65 | + url.searchParams.set("fromTool", fromToolId); |
| 66 | + } |
| 67 | + return url; |
| 68 | +} |
| 69 | + |
| 70 | +function validateFlow(flow) { |
| 71 | + const hostContextId = generateHostContextId(flow); |
| 72 | + const secondUrl = buildToolUrl(flow.second, hostContextId, flow.secondActionQueryFrom); |
| 73 | + const thirdUrl = buildToolUrl(flow.third, hostContextId, flow.thirdActionQueryFrom); |
| 74 | + const thirdBackUrl = buildToolUrl(flow.thirdActionQueryFrom, hostContextId, ""); |
| 75 | + const secondBackUrl = buildToolUrl(flow.secondActionQueryFrom, hostContextId, ""); |
| 76 | + |
| 77 | + const secondHtmlPath = path.join(toolsRoot, flow.second, "index.html"); |
| 78 | + const secondJsPath = path.join(toolsRoot, flow.second, "index.js"); |
| 79 | + const thirdHtmlPath = path.join(toolsRoot, flow.third, "index.html"); |
| 80 | + const thirdJsPath = path.join(toolsRoot, flow.third, "index.js"); |
| 81 | + const workspaceHtmlPath = path.join(toolsRoot, "workspace-v2", "index.html"); |
| 82 | + const workspaceJsPath = path.join(toolsRoot, "workspace-v2", "index.js"); |
| 83 | + const failures = []; |
| 84 | + |
| 85 | + const secondHtmlExists = fs.existsSync(secondHtmlPath); |
| 86 | + const secondJsExists = fs.existsSync(secondJsPath); |
| 87 | + const thirdHtmlExists = fs.existsSync(thirdHtmlPath); |
| 88 | + const thirdJsExists = fs.existsSync(thirdJsPath); |
| 89 | + const workspaceHtmlExists = fs.existsSync(workspaceHtmlPath); |
| 90 | + const workspaceJsExists = fs.existsSync(workspaceJsPath); |
| 91 | + const secondHtml = secondHtmlExists ? readText(secondHtmlPath) : ""; |
| 92 | + const secondJs = secondJsExists ? readText(secondJsPath) : ""; |
| 93 | + const thirdHtml = thirdHtmlExists ? readText(thirdHtmlPath) : ""; |
| 94 | + const thirdJs = thirdJsExists ? readText(thirdJsPath) : ""; |
| 95 | + const workspaceJs = workspaceJsExists ? readText(workspaceJsPath) : ""; |
| 96 | + |
| 97 | + const { syntaxValid: secondSyntaxValid, syntaxError: secondSyntaxError } = checkJsSyntax(secondJsPath); |
| 98 | + const { syntaxValid: thirdSyntaxValid, syntaxError: thirdSyntaxError } = checkJsSyntax(thirdJsPath); |
| 99 | + const { syntaxValid: workspaceSyntaxValid, syntaxError: workspaceSyntaxError } = checkJsSyntax(workspaceJsPath); |
| 100 | + |
| 101 | + if (!secondHtmlExists) failures.push("Second tool index.html is missing."); |
| 102 | + if (!secondJsExists) failures.push("Second tool index.js is missing."); |
| 103 | + if (!thirdHtmlExists) failures.push("Third tool index.html is missing."); |
| 104 | + if (!thirdJsExists) failures.push("Third tool index.js is missing."); |
| 105 | + if (!workspaceHtmlExists) failures.push("Workspace V2 index.html is missing."); |
| 106 | + if (!workspaceJsExists) failures.push("Workspace V2 index.js is missing."); |
| 107 | + |
| 108 | + if (!workspaceJs.includes('searchParams.set("fromTool", "workspace-v2");')) failures.push("Workspace V2 launch does not set fromTool=workspace-v2."); |
| 109 | + if (!secondJs.includes('searchParams.set("fromTool"')) failures.push("Second tool action flow does not set fromTool."); |
| 110 | + if (!thirdJs.includes('fromTool: typeof urlStateParams.get("fromTool")')) failures.push("Third tool does not parse fromTool from URL."); |
| 111 | + if (!thirdJs.includes("goBack()")) failures.push("Third tool does not expose goBack navigation handler."); |
| 112 | + |
| 113 | + if (!secondHtml.includes("Breadcrumb")) failures.push("Second tool breadcrumb UI is missing."); |
| 114 | + if (!thirdHtml.includes("Breadcrumb")) failures.push("Third tool breadcrumb UI is missing."); |
| 115 | + if (!secondHtml.includes("BackButton")) failures.push("Second tool back action UI is missing."); |
| 116 | + if (!thirdHtml.includes("BackButton")) failures.push("Third tool back action UI is missing."); |
| 117 | + |
| 118 | + if (secondUrl.searchParams.get("hostContextId") !== hostContextId) failures.push("Second tool launch URL did not preserve hostContextId."); |
| 119 | + if (thirdUrl.searchParams.get("hostContextId") !== hostContextId) failures.push("Third tool launch URL did not preserve hostContextId."); |
| 120 | + if (thirdBackUrl.searchParams.get("hostContextId") !== hostContextId) failures.push("Third tool back URL did not preserve hostContextId."); |
| 121 | + if (secondBackUrl.searchParams.get("hostContextId") !== hostContextId) failures.push("Second tool back URL did not preserve hostContextId."); |
| 122 | + |
| 123 | + if (!thirdBackUrl.pathname.endsWith(`/tools/${flow.thirdActionQueryFrom}/index.html`)) failures.push("Third tool back URL target is incorrect."); |
| 124 | + if (!secondBackUrl.pathname.endsWith(`/tools/${flow.secondActionQueryFrom}/index.html`)) failures.push("Second tool back URL target is incorrect."); |
| 125 | + |
| 126 | + if (!secondSyntaxValid) failures.push("Second tool JS failed syntax check."); |
| 127 | + if (!thirdSyntaxValid) failures.push("Third tool JS failed syntax check."); |
| 128 | + if (!workspaceSyntaxValid) failures.push("Workspace V2 JS failed syntax check."); |
| 129 | + |
| 130 | + return { |
| 131 | + flow: `${flow.first}->${flow.second}->${flow.third}`, |
| 132 | + hostContextId, |
| 133 | + secondUrl: secondUrl.pathname.slice(1) + secondUrl.search, |
| 134 | + thirdUrl: thirdUrl.pathname.slice(1) + thirdUrl.search, |
| 135 | + thirdBackUrl: thirdBackUrl.pathname.slice(1) + thirdBackUrl.search, |
| 136 | + secondBackUrl: secondBackUrl.pathname.slice(1) + secondBackUrl.search, |
| 137 | + secondSyntaxValid, |
| 138 | + secondSyntaxError, |
| 139 | + thirdSyntaxValid, |
| 140 | + thirdSyntaxError, |
| 141 | + workspaceSyntaxValid, |
| 142 | + workspaceSyntaxError, |
| 143 | + failures |
| 144 | + }; |
| 145 | +} |
| 146 | + |
| 147 | +export function run() { |
| 148 | + const rows = FLOWS.map(validateFlow); |
| 149 | + const failures = rows.flatMap((row) => row.failures.map((entry) => `${row.flow}: ${entry}`)); |
| 150 | + |
| 151 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 152 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 153 | + generatedAt: new Date().toISOString(), |
| 154 | + flowCount: rows.length, |
| 155 | + failures, |
| 156 | + rows |
| 157 | + }, null, 2)}\n`, "utf8"); |
| 158 | + |
| 159 | + console.log(`v2 back nav results: ${resultsPath}`); |
| 160 | + assert.equal(failures.length, 0, `V2 back nav failures: ${failures.join(" | ")}`); |
| 161 | + return { flowCount: rows.length, failures, rows }; |
| 162 | +} |
| 163 | + |
| 164 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 165 | + try { |
| 166 | + const summary = run(); |
| 167 | + console.log(JSON.stringify(summary, null, 2)); |
| 168 | + } catch (error) { |
| 169 | + console.error(error); |
| 170 | + process.exitCode = 1; |
| 171 | + } |
| 172 | +} |
0 commit comments