|
| 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-session-migration-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 | +function readText(filePath) { |
| 23 | + return fs.readFileSync(filePath, "utf8"); |
| 24 | +} |
| 25 | + |
| 26 | +function readJson(filePath) { |
| 27 | + return JSON.parse(readText(filePath)); |
| 28 | +} |
| 29 | + |
| 30 | +function cloneJson(value) { |
| 31 | + return JSON.parse(JSON.stringify(value)); |
| 32 | +} |
| 33 | + |
| 34 | +function checkJsSyntax(jsPath) { |
| 35 | + try { |
| 36 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 37 | + cwd: repoRoot, |
| 38 | + stdio: ["ignore", "pipe", "pipe"] |
| 39 | + }); |
| 40 | + return { syntaxValid: true, syntaxError: "" }; |
| 41 | + } catch (error) { |
| 42 | + return { |
| 43 | + syntaxValid: false, |
| 44 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 45 | + }; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function handleSessionVersion(payload) { |
| 50 | + if (payload && payload.version === "v2") return { ok: true, payload }; |
| 51 | + return { |
| 52 | + ok: false, |
| 53 | + error: "Unsupported session version", |
| 54 | + code: "UNSUPPORTED_VERSION" |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function classifyVersionState(payload) { |
| 59 | + const versionResult = handleSessionVersion(payload); |
| 60 | + if (!versionResult.ok) { |
| 61 | + return { state: "INVALID", result: versionResult }; |
| 62 | + } |
| 63 | + return { state: "VALID", result: versionResult }; |
| 64 | +} |
| 65 | + |
| 66 | +function validateTool(toolId) { |
| 67 | + const fixturePath = path.join(fixturesRoot, `${toolId}.json`); |
| 68 | + const jsPath = path.join(toolsRoot, toolId, "index.js"); |
| 69 | + const failures = []; |
| 70 | + const fixtureExists = fs.existsSync(fixturePath); |
| 71 | + const jsExists = fs.existsSync(jsPath); |
| 72 | + const jsText = jsExists ? readText(jsPath) : ""; |
| 73 | + const { syntaxValid, syntaxError } = checkJsSyntax(jsPath); |
| 74 | + |
| 75 | + const hasHookMethod = jsText.includes("handleSessionVersion(payload)"); |
| 76 | + const hasHookPattern = jsText.includes('if (payload && payload.version === "v2") return { ok: true, payload };') && |
| 77 | + jsText.includes('code: "UNSUPPORTED_VERSION"'); |
| 78 | + const hasHookEnforcement = jsText.includes("const versionCheck = this.handleSessionVersion(sessionContext);") && |
| 79 | + jsText.includes("if (!versionCheck.ok)"); |
| 80 | + |
| 81 | + if (!jsExists) failures.push("Missing tool index.js."); |
| 82 | + if (!syntaxValid) failures.push("Tool index.js failed syntax check."); |
| 83 | + if (!hasHookMethod) failures.push("Missing handleSessionVersion(payload) hook method."); |
| 84 | + if (!hasHookPattern) failures.push("Missing expected hook return pattern for v2/unsupported versions."); |
| 85 | + if (!hasHookEnforcement) failures.push("Tool does not enforce handleSessionVersion(sessionContext) before render."); |
| 86 | + |
| 87 | + let fixtureValid = false; |
| 88 | + let sessionContext = null; |
| 89 | + if (!fixtureExists) { |
| 90 | + failures.push("Missing fixture file."); |
| 91 | + } else { |
| 92 | + try { |
| 93 | + const fixture = readJson(fixturePath); |
| 94 | + fixtureValid = true; |
| 95 | + sessionContext = fixture.sessionContext; |
| 96 | + } catch { |
| 97 | + fixtureValid = false; |
| 98 | + } |
| 99 | + } |
| 100 | + if (!fixtureValid) failures.push("Fixture JSON is invalid."); |
| 101 | + |
| 102 | + let validVersionCase = { state: "INVALID", result: { ok: false, error: "fixture missing", code: "UNSUPPORTED_VERSION" } }; |
| 103 | + let wrongVersionCase = { state: "INVALID", result: { ok: false, error: "fixture missing", code: "UNSUPPORTED_VERSION" } }; |
| 104 | + let missingVersionCase = { state: "INVALID", result: { ok: false, error: "fixture missing", code: "UNSUPPORTED_VERSION" } }; |
| 105 | + |
| 106 | + if (sessionContext) { |
| 107 | + validVersionCase = classifyVersionState(sessionContext); |
| 108 | + const wrongVersionContext = cloneJson(sessionContext); |
| 109 | + wrongVersionContext.version = "v3"; |
| 110 | + wrongVersionCase = classifyVersionState(wrongVersionContext); |
| 111 | + const missingVersionContext = cloneJson(sessionContext); |
| 112 | + delete missingVersionContext.version; |
| 113 | + missingVersionCase = classifyVersionState(missingVersionContext); |
| 114 | + } |
| 115 | + |
| 116 | + if (validVersionCase.state !== "VALID") failures.push(`Expected VALID for version v2, got ${validVersionCase.state}.`); |
| 117 | + if (wrongVersionCase.state !== "INVALID") failures.push(`Expected INVALID for version v3, got ${wrongVersionCase.state}.`); |
| 118 | + if (missingVersionCase.state !== "INVALID") failures.push(`Expected INVALID for missing version, got ${missingVersionCase.state}.`); |
| 119 | + if (wrongVersionCase.result.code !== "UNSUPPORTED_VERSION") failures.push("Wrong-version case did not return code UNSUPPORTED_VERSION."); |
| 120 | + if (missingVersionCase.result.code !== "UNSUPPORTED_VERSION") failures.push("Missing-version case did not return code UNSUPPORTED_VERSION."); |
| 121 | + if (wrongVersionCase.result.error !== "Unsupported session version") failures.push("Wrong-version case did not return error 'Unsupported session version'."); |
| 122 | + if (missingVersionCase.result.error !== "Unsupported session version") failures.push("Missing-version case did not return error 'Unsupported session version'."); |
| 123 | + |
| 124 | + return { |
| 125 | + tool: toolId, |
| 126 | + fixturePath: path.relative(repoRoot, fixturePath).replace(/\\/g, "/"), |
| 127 | + jsPath: path.relative(repoRoot, jsPath).replace(/\\/g, "/"), |
| 128 | + jsExists, |
| 129 | + fixtureExists, |
| 130 | + fixtureValid, |
| 131 | + syntaxValid, |
| 132 | + syntaxError, |
| 133 | + hasHookMethod, |
| 134 | + hasHookPattern, |
| 135 | + hasHookEnforcement, |
| 136 | + cases: { |
| 137 | + versionV2: validVersionCase, |
| 138 | + versionV3: wrongVersionCase, |
| 139 | + missingVersion: missingVersionCase |
| 140 | + }, |
| 141 | + failures |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +export function run() { |
| 146 | + const rows = TOOLS.map(validateTool); |
| 147 | + const failures = rows.flatMap((row) => row.failures.map((entry) => `${row.tool}: ${entry}`)); |
| 148 | + |
| 149 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 150 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 151 | + generatedAt: new Date().toISOString(), |
| 152 | + failures, |
| 153 | + rows |
| 154 | + }, null, 2)}\n`, "utf8"); |
| 155 | + |
| 156 | + console.log(`v2 session migration results: ${resultsPath}`); |
| 157 | + assert.equal(failures.length, 0, `V2 session migration failures: ${failures.join(" | ")}`); |
| 158 | + return { failures, rows }; |
| 159 | +} |
| 160 | + |
| 161 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 162 | + try { |
| 163 | + const summary = run(); |
| 164 | + console.log(JSON.stringify(summary, null, 2)); |
| 165 | + } catch (error) { |
| 166 | + console.error(error); |
| 167 | + process.exitCode = 1; |
| 168 | + } |
| 169 | +} |
0 commit comments