|
| 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 fixturesRoot = path.join(repoRoot, "tests", "fixtures", "v2-tools"); |
| 11 | +const toolsRoot = path.join(repoRoot, "tools"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-tool-action-results.json"); |
| 13 | + |
| 14 | +const FLOWS = [ |
| 15 | + { |
| 16 | + sourceToolId: "asset-browser-v2", |
| 17 | + targetToolId: "svg-asset-studio-v2", |
| 18 | + buttonId: "assetBrowserV2OpenSvgAssetStudioV2Button", |
| 19 | + targetUrlSnippet: "../svg-asset-studio-v2/index.html" |
| 20 | + }, |
| 21 | + { |
| 22 | + sourceToolId: "palette-manager-v2", |
| 23 | + targetToolId: "vector-map-editor-v2", |
| 24 | + buttonId: "paletteManagerOpenVectorMapEditorV2Button", |
| 25 | + targetUrlSnippet: "../vector-map-editor-v2/index.html" |
| 26 | + }, |
| 27 | + { |
| 28 | + sourceToolId: "tilemap-studio-v2", |
| 29 | + targetToolId: "asset-browser-v2", |
| 30 | + buttonId: "tilemapV2OpenAssetBrowserV2Button", |
| 31 | + targetUrlSnippet: "../asset-browser-v2/index.html" |
| 32 | + } |
| 33 | +]; |
| 34 | + |
| 35 | +class MemorySessionStorage { |
| 36 | + constructor() { |
| 37 | + this.values = new Map(); |
| 38 | + } |
| 39 | + |
| 40 | + setItem(key, value) { |
| 41 | + this.values.set(String(key), String(value)); |
| 42 | + } |
| 43 | + |
| 44 | + getItem(key) { |
| 45 | + if (!this.values.has(String(key))) { |
| 46 | + return null; |
| 47 | + } |
| 48 | + return this.values.get(String(key)); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function readText(filePath) { |
| 53 | + return fs.readFileSync(filePath, "utf8"); |
| 54 | +} |
| 55 | + |
| 56 | +function readJson(filePath) { |
| 57 | + return JSON.parse(readText(filePath)); |
| 58 | +} |
| 59 | + |
| 60 | +function checkJsSyntax(jsPath) { |
| 61 | + try { |
| 62 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 63 | + cwd: repoRoot, |
| 64 | + stdio: ["ignore", "pipe", "pipe"] |
| 65 | + }); |
| 66 | + return { syntaxValid: true, syntaxError: "" }; |
| 67 | + } catch (error) { |
| 68 | + return { |
| 69 | + syntaxValid: false, |
| 70 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 71 | + }; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function generateHostContextId(sourceToolId) { |
| 76 | + const randomPart = Math.random().toString(36).slice(2, 10); |
| 77 | + return `${sourceToolId}-action-${Date.now()}-${randomPart}`; |
| 78 | +} |
| 79 | + |
| 80 | +function validateFlow(flow) { |
| 81 | + const sourceFixturePath = path.join(fixturesRoot, `${flow.sourceToolId}.json`); |
| 82 | + const sourceHtmlPath = path.join(toolsRoot, flow.sourceToolId, "index.html"); |
| 83 | + const sourceJsPath = path.join(toolsRoot, flow.sourceToolId, "index.js"); |
| 84 | + const targetHtmlPath = path.join(toolsRoot, flow.targetToolId, "index.html"); |
| 85 | + const targetJsPath = path.join(toolsRoot, flow.targetToolId, "index.js"); |
| 86 | + const failures = []; |
| 87 | + |
| 88 | + const sourceFixtureExists = fs.existsSync(sourceFixturePath); |
| 89 | + const sourceHtmlExists = fs.existsSync(sourceHtmlPath); |
| 90 | + const sourceJsExists = fs.existsSync(sourceJsPath); |
| 91 | + const targetHtmlExists = fs.existsSync(targetHtmlPath); |
| 92 | + const targetJsExists = fs.existsSync(targetJsPath); |
| 93 | + |
| 94 | + let fixtureValid = false; |
| 95 | + let sourceSessionContext = null; |
| 96 | + if (!sourceFixtureExists) { |
| 97 | + failures.push("Source fixture is missing."); |
| 98 | + } else { |
| 99 | + try { |
| 100 | + const fixture = readJson(sourceFixturePath); |
| 101 | + fixtureValid = true; |
| 102 | + sourceSessionContext = fixture.sessionContext; |
| 103 | + } catch { |
| 104 | + fixtureValid = false; |
| 105 | + } |
| 106 | + if (!fixtureValid) failures.push("Source fixture JSON is invalid."); |
| 107 | + if (fixtureValid && (!sourceSessionContext || typeof sourceSessionContext !== "object" || Array.isArray(sourceSessionContext))) { |
| 108 | + failures.push("Source fixture sessionContext is missing or invalid."); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + const hostContextId = generateHostContextId(flow.sourceToolId); |
| 113 | + const sessionStorageLike = new MemorySessionStorage(); |
| 114 | + if (sourceSessionContext) { |
| 115 | + sessionStorageLike.setItem(hostContextId, JSON.stringify(sourceSessionContext)); |
| 116 | + } |
| 117 | + |
| 118 | + const actionUrl = new URL(`tools/${flow.targetToolId}/index.html`, "http://localhost/"); |
| 119 | + actionUrl.searchParams.set("hostContextId", hostContextId); |
| 120 | + const builtUrl = actionUrl.pathname.slice(1) + actionUrl.search; |
| 121 | + const parsedHostContextId = actionUrl.searchParams.get("hostContextId"); |
| 122 | + const storedValue = sessionStorageLike.getItem(hostContextId); |
| 123 | + const expectedStoredValue = sourceSessionContext ? JSON.stringify(sourceSessionContext) : ""; |
| 124 | + |
| 125 | + const sourceHtmlText = sourceHtmlExists ? readText(sourceHtmlPath) : ""; |
| 126 | + const sourceJsText = sourceJsExists ? readText(sourceJsPath) : ""; |
| 127 | + const { syntaxValid: sourceSyntaxValid, syntaxError: sourceSyntaxError } = checkJsSyntax(sourceJsPath); |
| 128 | + const { syntaxValid: targetSyntaxValid, syntaxError: targetSyntaxError } = checkJsSyntax(targetJsPath); |
| 129 | + |
| 130 | + if (!sourceHtmlExists) failures.push("Source tool index.html is missing."); |
| 131 | + if (!sourceJsExists) failures.push("Source tool index.js is missing."); |
| 132 | + if (!targetHtmlExists) failures.push("Target tool index.html is missing."); |
| 133 | + if (!targetJsExists) failures.push("Target tool index.js is missing."); |
| 134 | + if (!sourceHtmlText.includes(`id="${flow.buttonId}"`)) failures.push("Required launch action control is missing in source tool HTML."); |
| 135 | + if (!sourceJsText.includes(flow.targetUrlSnippet)) failures.push("Source tool JS does not construct the required target route."); |
| 136 | + if (!sourceJsText.includes('searchParams.set("hostContextId", this.urlState.hostContextId);')) failures.push("Source tool JS does not preserve hostContextId in action URL."); |
| 137 | + if (!builtUrl.startsWith(`tools/${flow.targetToolId}/index.html?hostContextId=`)) failures.push("Built action URL does not target required V2 route."); |
| 138 | + if (parsedHostContextId !== hostContextId) failures.push("Action URL hostContextId was not preserved."); |
| 139 | + if (!storedValue) failures.push("Session storage entry for hostContextId is missing in simulated action flow."); |
| 140 | + if (storedValue !== expectedStoredValue) failures.push("Session payload was mutated during simulated action flow."); |
| 141 | + if (!sourceSyntaxValid) failures.push("Source tool index.js failed syntax check."); |
| 142 | + if (!targetSyntaxValid) failures.push("Target tool index.js failed syntax check."); |
| 143 | + |
| 144 | + return { |
| 145 | + sourceTool: flow.sourceToolId, |
| 146 | + targetTool: flow.targetToolId, |
| 147 | + buttonId: flow.buttonId, |
| 148 | + sourceFixturePath: path.relative(repoRoot, sourceFixturePath).replace(/\\/g, "/"), |
| 149 | + sourceHtmlPath: path.relative(repoRoot, sourceHtmlPath).replace(/\\/g, "/"), |
| 150 | + sourceJsPath: path.relative(repoRoot, sourceJsPath).replace(/\\/g, "/"), |
| 151 | + targetHtmlPath: path.relative(repoRoot, targetHtmlPath).replace(/\\/g, "/"), |
| 152 | + targetJsPath: path.relative(repoRoot, targetJsPath).replace(/\\/g, "/"), |
| 153 | + sourceFixtureExists, |
| 154 | + fixtureValid, |
| 155 | + hostContextId, |
| 156 | + builtUrl, |
| 157 | + parsedHostContextId, |
| 158 | + storageEntryExists: Boolean(storedValue), |
| 159 | + sourceSyntaxValid, |
| 160 | + sourceSyntaxError, |
| 161 | + targetSyntaxValid, |
| 162 | + targetSyntaxError, |
| 163 | + failures |
| 164 | + }; |
| 165 | +} |
| 166 | + |
| 167 | +export function run() { |
| 168 | + const rows = FLOWS.map(validateFlow); |
| 169 | + const failures = rows.flatMap((row) => row.failures.map((entry) => `${row.sourceTool}->${row.targetTool}: ${entry}`)); |
| 170 | + |
| 171 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 172 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 173 | + generatedAt: new Date().toISOString(), |
| 174 | + flowCount: rows.length, |
| 175 | + failures, |
| 176 | + rows |
| 177 | + }, null, 2)}\n`, "utf8"); |
| 178 | + |
| 179 | + console.log(`v2 tool action results: ${resultsPath}`); |
| 180 | + assert.equal(failures.length, 0, `V2 tool action failures: ${failures.join(" | ")}`); |
| 181 | + return { flowCount: rows.length, failures, rows }; |
| 182 | +} |
| 183 | + |
| 184 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 185 | + try { |
| 186 | + const summary = run(); |
| 187 | + console.log(JSON.stringify(summary, null, 2)); |
| 188 | + } catch (error) { |
| 189 | + console.error(error); |
| 190 | + process.exitCode = 1; |
| 191 | + } |
| 192 | +} |
0 commit comments