|
| 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-url-state-results.json"); |
| 12 | + |
| 13 | +const REQUIRED_V2_TOOLS = [ |
| 14 | + "asset-browser-v2", |
| 15 | + "palette-manager-v2", |
| 16 | + "svg-asset-studio-v2", |
| 17 | + "tilemap-studio-v2", |
| 18 | + "vector-map-editor-v2" |
| 19 | +]; |
| 20 | + |
| 21 | +function readText(filePath) { |
| 22 | + return fs.readFileSync(filePath, "utf8"); |
| 23 | +} |
| 24 | + |
| 25 | +function checkJsSyntax(jsPath) { |
| 26 | + try { |
| 27 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 28 | + cwd: repoRoot, |
| 29 | + stdio: ["ignore", "pipe", "pipe"] |
| 30 | + }); |
| 31 | + return { syntaxValid: true, syntaxError: "" }; |
| 32 | + } catch (error) { |
| 33 | + return { |
| 34 | + syntaxValid: false, |
| 35 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 36 | + }; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function validateTool(toolId) { |
| 41 | + const htmlPath = path.join(toolsRoot, toolId, "index.html"); |
| 42 | + const jsPath = path.join(toolsRoot, toolId, "index.js"); |
| 43 | + const htmlExists = fs.existsSync(htmlPath); |
| 44 | + const jsExists = fs.existsSync(jsPath); |
| 45 | + const jsText = jsExists ? readText(jsPath) : ""; |
| 46 | + |
| 47 | + const baseUrlPath = `tools/${toolId}/index.html?hostContextId=test-id`; |
| 48 | + const deepLinkUrlPath = `tools/${toolId}/index.html?hostContextId=test-id&view=test-view&selection=test-selection&zoom=2&panel=inspector`; |
| 49 | + const parsedBaseUrl = new URL(baseUrlPath, "http://localhost/"); |
| 50 | + const parsedDeepLinkUrl = new URL(deepLinkUrlPath, "http://localhost/"); |
| 51 | + |
| 52 | + const baseHostContextId = parsedBaseUrl.searchParams.get("hostContextId"); |
| 53 | + const deepHostContextId = parsedDeepLinkUrl.searchParams.get("hostContextId"); |
| 54 | + const deepView = parsedDeepLinkUrl.searchParams.get("view"); |
| 55 | + const deepSelection = parsedDeepLinkUrl.searchParams.get("selection"); |
| 56 | + const deepZoom = parsedDeepLinkUrl.searchParams.get("zoom"); |
| 57 | + const deepPanel = parsedDeepLinkUrl.searchParams.get("panel"); |
| 58 | + |
| 59 | + const hasHostContextIdParser = jsText.includes('get("hostContextId")'); |
| 60 | + const hasViewParser = jsText.includes('get("view")'); |
| 61 | + const hasSelectionParser = jsText.includes('get("selection")'); |
| 62 | + const hasZoomParser = jsText.includes('get("zoom")'); |
| 63 | + const hasPanelParser = jsText.includes('get("panel")'); |
| 64 | + const hasUrlStateObject = jsText.includes("this.urlState"); |
| 65 | + const { syntaxValid, syntaxError } = checkJsSyntax(jsPath); |
| 66 | + |
| 67 | + const failures = []; |
| 68 | + if (!htmlExists) failures.push("Missing tool index.html route target."); |
| 69 | + if (!jsExists) failures.push("Missing tool index.js runtime."); |
| 70 | + if (baseHostContextId !== "test-id") failures.push("Base URL hostContextId parsing failed."); |
| 71 | + if (deepHostContextId !== "test-id") failures.push("Deep-link URL hostContextId parsing failed."); |
| 72 | + if (deepView !== "test-view") failures.push("Deep-link view param parsing failed."); |
| 73 | + if (deepSelection !== "test-selection") failures.push("Deep-link selection param parsing failed."); |
| 74 | + if (deepZoom !== "2") failures.push("Deep-link zoom param parsing failed."); |
| 75 | + if (deepPanel !== "inspector") failures.push("Deep-link panel param parsing failed."); |
| 76 | + if (!hasHostContextIdParser) failures.push('Tool JS does not parse hostContextId from URL query.'); |
| 77 | + if (!hasViewParser) failures.push('Tool JS does not parse optional "view" URL param.'); |
| 78 | + if (!hasSelectionParser) failures.push('Tool JS does not parse optional "selection" URL param.'); |
| 79 | + if (!hasZoomParser) failures.push('Tool JS does not parse optional "zoom" URL param.'); |
| 80 | + if (!hasPanelParser) failures.push('Tool JS does not parse optional "panel" URL param.'); |
| 81 | + if (!hasUrlStateObject) failures.push("Tool JS does not retain parsed URL state."); |
| 82 | + if (!syntaxValid) failures.push("Tool JS failed syntax check."); |
| 83 | + |
| 84 | + return { |
| 85 | + tool: toolId, |
| 86 | + routePath: path.relative(repoRoot, htmlPath).replace(/\\/g, "/"), |
| 87 | + jsPath: path.relative(repoRoot, jsPath).replace(/\\/g, "/"), |
| 88 | + htmlExists, |
| 89 | + jsExists, |
| 90 | + baseUrlPath, |
| 91 | + deepLinkUrlPath, |
| 92 | + baseHostContextId, |
| 93 | + deepHostContextId, |
| 94 | + deepView, |
| 95 | + deepSelection, |
| 96 | + deepZoom, |
| 97 | + deepPanel, |
| 98 | + hasHostContextIdParser, |
| 99 | + hasViewParser, |
| 100 | + hasSelectionParser, |
| 101 | + hasZoomParser, |
| 102 | + hasPanelParser, |
| 103 | + hasUrlStateObject, |
| 104 | + syntaxValid, |
| 105 | + syntaxError, |
| 106 | + failures |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +export function run() { |
| 111 | + const rows = REQUIRED_V2_TOOLS.map(validateTool); |
| 112 | + const failures = rows.flatMap((row) => row.failures.map((entry) => `${row.tool}: ${entry}`)); |
| 113 | + |
| 114 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 115 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 116 | + generatedAt: new Date().toISOString(), |
| 117 | + toolCount: rows.length, |
| 118 | + failures, |
| 119 | + rows |
| 120 | + }, null, 2)}\n`, "utf8"); |
| 121 | + |
| 122 | + console.log(`v2 url state results: ${resultsPath}`); |
| 123 | + assert.equal(failures.length, 0, `V2 URL state failures: ${failures.join(" | ")}`); |
| 124 | + return { toolCount: rows.length, failures, rows }; |
| 125 | +} |
| 126 | + |
| 127 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 128 | + try { |
| 129 | + const summary = run(); |
| 130 | + console.log(JSON.stringify(summary, null, 2)); |
| 131 | + } catch (error) { |
| 132 | + console.error(error); |
| 133 | + process.exitCode = 1; |
| 134 | + } |
| 135 | +} |
0 commit comments