|
| 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 workspaceJsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-session-producer-results.json"); |
| 13 | + |
| 14 | +const TOOLS = [ |
| 15 | + "asset-browser-v2", |
| 16 | + "palette-manager-v2", |
| 17 | + "svg-asset-studio-v2", |
| 18 | + "tilemap-studio-v2", |
| 19 | + "vector-map-editor-v2" |
| 20 | +]; |
| 21 | + |
| 22 | +class MemorySessionStorage { |
| 23 | + constructor() { |
| 24 | + this.values = new Map(); |
| 25 | + } |
| 26 | + |
| 27 | + setItem(key, value) { |
| 28 | + this.values.set(String(key), String(value)); |
| 29 | + } |
| 30 | + |
| 31 | + getItem(key) { |
| 32 | + if (!this.values.has(String(key))) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + return this.values.get(String(key)); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function readText(filePath) { |
| 40 | + return fs.readFileSync(filePath, "utf8"); |
| 41 | +} |
| 42 | + |
| 43 | +function readJson(filePath) { |
| 44 | + return JSON.parse(readText(filePath)); |
| 45 | +} |
| 46 | + |
| 47 | +function checkJsSyntax(jsPath) { |
| 48 | + try { |
| 49 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 50 | + cwd: repoRoot, |
| 51 | + stdio: ["ignore", "pipe", "pipe"] |
| 52 | + }); |
| 53 | + return { syntaxValid: true, syntaxError: "" }; |
| 54 | + } catch (error) { |
| 55 | + return { |
| 56 | + syntaxValid: false, |
| 57 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 58 | + }; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function generateHostContextId(toolId) { |
| 63 | + const randomPart = Math.random().toString(36).slice(2, 10); |
| 64 | + return `${toolId}-producer-${Date.now()}-${randomPart}`; |
| 65 | +} |
| 66 | + |
| 67 | +function buildToolUrl(toolId, hostContextId) { |
| 68 | + return `tools/${toolId}/index.html?hostContextId=${encodeURIComponent(hostContextId)}`; |
| 69 | +} |
| 70 | + |
| 71 | +function validateTool(toolId) { |
| 72 | + const fixturePath = path.join(fixturesRoot, `${toolId}.json`); |
| 73 | + const toolJsPath = path.join(repoRoot, "tools", toolId, "index.js"); |
| 74 | + const toolHtmlPath = path.join(repoRoot, "tools", toolId, "index.html"); |
| 75 | + const failures = []; |
| 76 | + const fixtureExists = fs.existsSync(fixturePath); |
| 77 | + const toolJsExists = fs.existsSync(toolJsPath); |
| 78 | + const toolHtmlExists = fs.existsSync(toolHtmlPath); |
| 79 | + let fixtureValid = false; |
| 80 | + let fixtureSessionContext = null; |
| 81 | + |
| 82 | + if (!fixtureExists) { |
| 83 | + failures.push("Fixture file is missing."); |
| 84 | + } else { |
| 85 | + try { |
| 86 | + const fixture = readJson(fixturePath); |
| 87 | + fixtureValid = true; |
| 88 | + fixtureSessionContext = fixture.sessionContext; |
| 89 | + } catch { |
| 90 | + fixtureValid = false; |
| 91 | + } |
| 92 | + if (!fixtureValid) failures.push("Fixture JSON is invalid."); |
| 93 | + if (fixtureValid && (!fixtureSessionContext || typeof fixtureSessionContext !== "object" || Array.isArray(fixtureSessionContext))) { |
| 94 | + failures.push("Fixture sessionContext is missing or invalid."); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const hostContextId = generateHostContextId(toolId); |
| 99 | + const sessionStorageLike = new MemorySessionStorage(); |
| 100 | + if (fixtureSessionContext) { |
| 101 | + sessionStorageLike.setItem(hostContextId, JSON.stringify(fixtureSessionContext)); |
| 102 | + } |
| 103 | + |
| 104 | + const launchUrl = buildToolUrl(toolId, hostContextId); |
| 105 | + const parsedLaunchUrl = new URL(launchUrl, "http://localhost/"); |
| 106 | + const parsedHostContextId = parsedLaunchUrl.searchParams.get("hostContextId"); |
| 107 | + const storedValue = sessionStorageLike.getItem(hostContextId); |
| 108 | + const storedPayloadParseable = (() => { |
| 109 | + if (!storedValue) return false; |
| 110 | + try { |
| 111 | + const parsed = JSON.parse(storedValue); |
| 112 | + return Boolean(parsed && typeof parsed === "object" && !Array.isArray(parsed)); |
| 113 | + } catch { |
| 114 | + return false; |
| 115 | + } |
| 116 | + })(); |
| 117 | + |
| 118 | + const { syntaxValid: toolSyntaxValid, syntaxError: toolSyntaxError } = checkJsSyntax(toolJsPath); |
| 119 | + |
| 120 | + if (!toolHtmlExists) failures.push("Target tool index.html is missing."); |
| 121 | + if (!toolJsExists) failures.push("Target tool index.js is missing."); |
| 122 | + if (!launchUrl.includes(`tools/${toolId}/index.html?hostContextId=`)) failures.push("Launch URL does not match expected V2 path format."); |
| 123 | + if (parsedHostContextId !== hostContextId) failures.push("Launch URL hostContextId does not match generated value."); |
| 124 | + if (!storedValue) failures.push("Session storage entry was not written."); |
| 125 | + if (!storedPayloadParseable) failures.push("Session storage entry is not valid serialized JSON payload."); |
| 126 | + if (!toolSyntaxValid) failures.push("Target tool index.js failed syntax check."); |
| 127 | + |
| 128 | + return { |
| 129 | + tool: toolId, |
| 130 | + fixturePath: path.relative(repoRoot, fixturePath).replace(/\\/g, "/"), |
| 131 | + toolHtmlPath: path.relative(repoRoot, toolHtmlPath).replace(/\\/g, "/"), |
| 132 | + toolJsPath: path.relative(repoRoot, toolJsPath).replace(/\\/g, "/"), |
| 133 | + fixtureExists, |
| 134 | + fixtureValid, |
| 135 | + hostContextId, |
| 136 | + launchUrl, |
| 137 | + parsedHostContextId, |
| 138 | + storageEntryExists: Boolean(storedValue), |
| 139 | + storedPayloadParseable, |
| 140 | + toolSyntaxValid, |
| 141 | + toolSyntaxError, |
| 142 | + failures |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +export function run() { |
| 147 | + const workspaceJsText = fs.existsSync(workspaceJsPath) ? readText(workspaceJsPath) : ""; |
| 148 | + const { syntaxValid: workspaceSyntaxValid, syntaxError: workspaceSyntaxError } = checkJsSyntax(workspaceJsPath); |
| 149 | + const producerChecks = { |
| 150 | + workspaceJsExists: fs.existsSync(workspaceJsPath), |
| 151 | + usesSessionStorageSetItem: workspaceJsText.includes("sessionStorage.setItem(hostContextId, JSON.stringify(payload));"), |
| 152 | + setsHostContextIdInUrl: workspaceJsText.includes('toolUrl.searchParams.set("hostContextId", hostContextId);'), |
| 153 | + hasFixtureLoaderPath: workspaceJsText.includes("../../tests/fixtures/v2-tools/"), |
| 154 | + workspaceSyntaxValid, |
| 155 | + workspaceSyntaxError |
| 156 | + }; |
| 157 | + |
| 158 | + const rows = TOOLS.map(validateTool); |
| 159 | + const failures = []; |
| 160 | + if (!producerChecks.workspaceJsExists) failures.push("workspace-v2/index.js is missing."); |
| 161 | + if (!producerChecks.usesSessionStorageSetItem) failures.push("workspace-v2/index.js does not write session storage with hostContextId key."); |
| 162 | + if (!producerChecks.setsHostContextIdInUrl) failures.push("workspace-v2/index.js does not add hostContextId query parameter."); |
| 163 | + if (!producerChecks.hasFixtureLoaderPath) failures.push("workspace-v2/index.js does not reference v2 fixture path."); |
| 164 | + if (!producerChecks.workspaceSyntaxValid) failures.push("workspace-v2/index.js failed syntax check."); |
| 165 | + rows.forEach((row) => { |
| 166 | + row.failures.forEach((entry) => failures.push(`${row.tool}: ${entry}`)); |
| 167 | + }); |
| 168 | + |
| 169 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 170 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 171 | + generatedAt: new Date().toISOString(), |
| 172 | + toolCount: rows.length, |
| 173 | + producerChecks, |
| 174 | + failures, |
| 175 | + rows |
| 176 | + }, null, 2)}\n`, "utf8"); |
| 177 | + |
| 178 | + console.log(`v2 session producer results: ${resultsPath}`); |
| 179 | + assert.equal(failures.length, 0, `V2 session producer failures: ${failures.join(" | ")}`); |
| 180 | + return { toolCount: rows.length, producerChecks, failures, rows }; |
| 181 | +} |
| 182 | + |
| 183 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 184 | + try { |
| 185 | + const summary = run(); |
| 186 | + console.log(JSON.stringify(summary, null, 2)); |
| 187 | + } catch (error) { |
| 188 | + console.error(error); |
| 189 | + process.exitCode = 1; |
| 190 | + } |
| 191 | +} |
0 commit comments