|
| 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 fixturePath = path.join(repoRoot, "tests", "fixtures", "v2-tools", "asset-browser-v2.json"); |
| 11 | +const workspaceHtmlPath = path.join(repoRoot, "tools", "workspace-v2", "index.html"); |
| 12 | +const workspaceJsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 13 | +const resultsPath = path.join(repoRoot, "tmp", "v2-import-export-results.json"); |
| 14 | + |
| 15 | +class MemorySessionStorage { |
| 16 | + constructor() { |
| 17 | + this.values = new Map(); |
| 18 | + } |
| 19 | + |
| 20 | + setItem(key, value) { |
| 21 | + this.values.set(String(key), String(value)); |
| 22 | + } |
| 23 | + |
| 24 | + getItem(key) { |
| 25 | + if (!this.values.has(String(key))) { |
| 26 | + return null; |
| 27 | + } |
| 28 | + return this.values.get(String(key)); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function readText(filePath) { |
| 33 | + return fs.readFileSync(filePath, "utf8"); |
| 34 | +} |
| 35 | + |
| 36 | +function readJson(filePath) { |
| 37 | + return JSON.parse(readText(filePath)); |
| 38 | +} |
| 39 | + |
| 40 | +function checkJsSyntax(jsPath) { |
| 41 | + try { |
| 42 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 43 | + cwd: repoRoot, |
| 44 | + stdio: ["ignore", "pipe", "pipe"] |
| 45 | + }); |
| 46 | + return { syntaxValid: true, syntaxError: "" }; |
| 47 | + } catch (error) { |
| 48 | + return { |
| 49 | + syntaxValid: false, |
| 50 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 51 | + }; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function generateHostContextId(toolId) { |
| 56 | + const randomPart = Math.random().toString(36).slice(2, 10); |
| 57 | + return `${toolId}-import-export-${Date.now()}-${randomPart}`; |
| 58 | +} |
| 59 | + |
| 60 | +function simulateImport(toolId, rawJson, sessionStorageLike) { |
| 61 | + const parsed = JSON.parse(rawJson); |
| 62 | + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { |
| 63 | + throw new Error("Imported JSON is invalid. Expected an object session payload."); |
| 64 | + } |
| 65 | + const hostContextId = generateHostContextId(toolId); |
| 66 | + sessionStorageLike.setItem(hostContextId, JSON.stringify(parsed)); |
| 67 | + return { hostContextId, parsed }; |
| 68 | +} |
| 69 | + |
| 70 | +function simulateExport(currentSessionPayload) { |
| 71 | + return JSON.stringify(currentSessionPayload, null, 2); |
| 72 | +} |
| 73 | + |
| 74 | +export function run() { |
| 75 | + const failures = []; |
| 76 | + const fixtureExists = fs.existsSync(fixturePath); |
| 77 | + const workspaceHtmlExists = fs.existsSync(workspaceHtmlPath); |
| 78 | + const workspaceJsExists = fs.existsSync(workspaceJsPath); |
| 79 | + const workspaceHtml = workspaceHtmlExists ? readText(workspaceHtmlPath) : ""; |
| 80 | + const workspaceJs = workspaceJsExists ? readText(workspaceJsPath) : ""; |
| 81 | + const { syntaxValid, syntaxError } = checkJsSyntax(workspaceJsPath); |
| 82 | + |
| 83 | + if (!fixtureExists) { |
| 84 | + failures.push("Fixture file missing."); |
| 85 | + } |
| 86 | + if (!workspaceHtmlExists) { |
| 87 | + failures.push("workspace-v2/index.html missing."); |
| 88 | + } |
| 89 | + if (!workspaceJsExists) { |
| 90 | + failures.push("workspace-v2/index.js missing."); |
| 91 | + } |
| 92 | + |
| 93 | + let fixture = null; |
| 94 | + let fixtureSessionContext = null; |
| 95 | + let fixtureRawSessionJson = ""; |
| 96 | + if (fixtureExists) { |
| 97 | + try { |
| 98 | + fixture = readJson(fixturePath); |
| 99 | + fixtureSessionContext = fixture.sessionContext; |
| 100 | + fixtureRawSessionJson = JSON.stringify(fixtureSessionContext, null, 2); |
| 101 | + } catch { |
| 102 | + failures.push("Fixture JSON failed to parse."); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (!fixtureSessionContext || typeof fixtureSessionContext !== "object" || Array.isArray(fixtureSessionContext)) { |
| 107 | + failures.push("Fixture sessionContext missing/invalid."); |
| 108 | + } |
| 109 | + |
| 110 | + const sessionStorageLike = new MemorySessionStorage(); |
| 111 | + let importResult = null; |
| 112 | + try { |
| 113 | + importResult = simulateImport("asset-browser-v2", fixtureRawSessionJson, sessionStorageLike); |
| 114 | + } catch (error) { |
| 115 | + failures.push(`Import simulation failed: ${error instanceof Error ? error.message : "unknown error"}`); |
| 116 | + } |
| 117 | + |
| 118 | + let exportedJson = ""; |
| 119 | + let exportedParsed = null; |
| 120 | + if (importResult) { |
| 121 | + exportedJson = simulateExport(importResult.parsed); |
| 122 | + try { |
| 123 | + exportedParsed = JSON.parse(exportedJson); |
| 124 | + } catch { |
| 125 | + failures.push("Exported JSON failed to parse."); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + const storedPayload = importResult ? sessionStorageLike.getItem(importResult.hostContextId) : null; |
| 130 | + const storedParsed = storedPayload ? JSON.parse(storedPayload) : null; |
| 131 | + const importHostContextAssigned = Boolean(importResult && typeof importResult.hostContextId === "string" && importResult.hostContextId.trim()); |
| 132 | + const storageEntryCreated = Boolean(storedPayload); |
| 133 | + const exportMatchesInput = Boolean(exportedParsed && fixtureSessionContext && JSON.stringify(exportedParsed) === JSON.stringify(fixtureSessionContext)); |
| 134 | + |
| 135 | + if (!importHostContextAssigned) { |
| 136 | + failures.push("Import did not assign hostContextId."); |
| 137 | + } |
| 138 | + if (!storageEntryCreated) { |
| 139 | + failures.push("Import did not create sessionStorage entry."); |
| 140 | + } |
| 141 | + if (storedParsed && fixtureSessionContext && JSON.stringify(storedParsed) !== JSON.stringify(fixtureSessionContext)) { |
| 142 | + failures.push("Stored session payload does not match imported JSON."); |
| 143 | + } |
| 144 | + if (!exportMatchesInput) { |
| 145 | + failures.push("Exported JSON does not match imported input payload."); |
| 146 | + } |
| 147 | + |
| 148 | + if (!workspaceHtml.includes('id="workspaceV2ImportJson"')) { |
| 149 | + failures.push("Import textarea is missing in workspace-v2 HTML."); |
| 150 | + } |
| 151 | + if (!workspaceHtml.includes('id="workspaceV2ImportButton"')) { |
| 152 | + failures.push("Import button is missing in workspace-v2 HTML."); |
| 153 | + } |
| 154 | + if (!workspaceHtml.includes('id="workspaceV2ExportButton"')) { |
| 155 | + failures.push("Export button is missing in workspace-v2 HTML."); |
| 156 | + } |
| 157 | + if (!workspaceJs.includes("importSessionJson()")) { |
| 158 | + failures.push("workspace-v2 JS does not expose importSessionJson handler."); |
| 159 | + } |
| 160 | + if (!workspaceJs.includes("exportCurrentSessionJson()")) { |
| 161 | + failures.push("workspace-v2 JS does not expose exportCurrentSessionJson handler."); |
| 162 | + } |
| 163 | + if (!workspaceJs.includes("JSON.parse(rawJson)")) { |
| 164 | + failures.push("workspace-v2 JS does not parse imported JSON."); |
| 165 | + } |
| 166 | + if (!workspaceJs.includes("sessionStorage.setItem(hostContextId, JSON.stringify(parsed));")) { |
| 167 | + failures.push("workspace-v2 JS does not store imported payload by generated hostContextId."); |
| 168 | + } |
| 169 | + if (!workspaceJs.includes("Imported JSON is invalid")) { |
| 170 | + failures.push("workspace-v2 JS does not expose explicit invalid JSON error messaging."); |
| 171 | + } |
| 172 | + if (!syntaxValid) { |
| 173 | + failures.push("workspace-v2/index.js failed syntax check."); |
| 174 | + } |
| 175 | + |
| 176 | + const summary = { |
| 177 | + generatedAt: new Date().toISOString(), |
| 178 | + fixturePath: path.relative(repoRoot, fixturePath).replace(/\\/g, "/"), |
| 179 | + workspaceHtmlPath: path.relative(repoRoot, workspaceHtmlPath).replace(/\\/g, "/"), |
| 180 | + workspaceJsPath: path.relative(repoRoot, workspaceJsPath).replace(/\\/g, "/"), |
| 181 | + fixtureExists, |
| 182 | + workspaceHtmlExists, |
| 183 | + workspaceJsExists, |
| 184 | + importHostContextAssigned, |
| 185 | + storageEntryCreated, |
| 186 | + exportMatchesInput, |
| 187 | + syntaxValid, |
| 188 | + syntaxError, |
| 189 | + hostContextId: importResult ? importResult.hostContextId : "", |
| 190 | + failures |
| 191 | + }; |
| 192 | + |
| 193 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 194 | + fs.writeFileSync(resultsPath, `${JSON.stringify(summary, null, 2)}\n`, "utf8"); |
| 195 | + |
| 196 | + console.log(`v2 import/export results: ${resultsPath}`); |
| 197 | + assert.equal(failures.length, 0, `V2 import/export failures: ${failures.join(" | ")}`); |
| 198 | + return summary; |
| 199 | +} |
| 200 | + |
| 201 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 202 | + try { |
| 203 | + const summary = run(); |
| 204 | + console.log(JSON.stringify(summary, null, 2)); |
| 205 | + } catch (error) { |
| 206 | + console.error(error); |
| 207 | + process.exitCode = 1; |
| 208 | + } |
| 209 | +} |
0 commit comments