|
| 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 workspaceJsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 11 | +const testPath = path.join(repoRoot, "tests", "runtime", "V2CurrentSessionExport.test.mjs"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-current-session-export-results.json"); |
| 13 | + |
| 14 | +function checkSyntax(filePath) { |
| 15 | + try { |
| 16 | + execFileSync(process.execPath, ["--check", filePath], { |
| 17 | + cwd: repoRoot, |
| 18 | + stdio: ["ignore", "pipe", "pipe"] |
| 19 | + }); |
| 20 | + return { ok: true, error: "" }; |
| 21 | + } catch (error) { |
| 22 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function simulateExport(activePayload, selectedToolId, currentHostContextId) { |
| 27 | + if (!activePayload || typeof activePayload !== "object" || Array.isArray(activePayload)) { |
| 28 | + return { |
| 29 | + ok: false, |
| 30 | + status: "No active Workspace V2 session is available to export.", |
| 31 | + filename: "", |
| 32 | + serialized: "" |
| 33 | + }; |
| 34 | + } |
| 35 | + const payloadToolId = typeof activePayload.toolId === "string" ? activePayload.toolId.trim() : ""; |
| 36 | + const filenameToolId = payloadToolId || selectedToolId || "workspace-v2"; |
| 37 | + const filenameSessionId = typeof currentHostContextId === "string" && currentHostContextId.trim() |
| 38 | + ? currentHostContextId.trim() |
| 39 | + : "session"; |
| 40 | + return { |
| 41 | + ok: true, |
| 42 | + status: "Exported current workspace session JSON.", |
| 43 | + filename: `${filenameToolId}-${filenameSessionId}.json`, |
| 44 | + serialized: JSON.stringify(activePayload, null, 2) |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +export function run() { |
| 49 | + const failures = []; |
| 50 | + const workspaceJsExists = fs.existsSync(workspaceJsPath); |
| 51 | + const workspaceJs = workspaceJsExists ? fs.readFileSync(workspaceJsPath, "utf8") : ""; |
| 52 | + const workspaceJsSyntax = checkSyntax(workspaceJsPath); |
| 53 | + const testSyntax = checkSyntax(testPath); |
| 54 | + |
| 55 | + if (!workspaceJsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 56 | + if (!workspaceJsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 57 | + if (!testSyntax.ok) failures.push("tests/runtime/V2CurrentSessionExport.test.mjs failed syntax check."); |
| 58 | + |
| 59 | + const requiredTokens = [ |
| 60 | + "readActiveSessionPayloadForLibraryActions()", |
| 61 | + "No active Workspace V2 session is available to export.", |
| 62 | + "Exported current workspace session JSON.", |
| 63 | + "URL.createObjectURL", |
| 64 | + "downloadLink.download" |
| 65 | + ]; |
| 66 | + requiredTokens.forEach((token) => { |
| 67 | + if (!workspaceJs.includes(token)) { |
| 68 | + failures.push(`Missing export token: ${token}`); |
| 69 | + } |
| 70 | + }); |
| 71 | + if (workspaceJs.includes("No current session payload to export. Load fixture or import JSON first.")) { |
| 72 | + failures.push("Legacy export-empty message is still present."); |
| 73 | + } |
| 74 | + |
| 75 | + const activePayload = { |
| 76 | + version: "v2", |
| 77 | + toolId: "palette-manager-v2", |
| 78 | + payloadJson: { swatches: ["#000000", "#ffffff"] } |
| 79 | + }; |
| 80 | + const activeResult = simulateExport(activePayload, "asset-browser-v2", "palette-manager-v2-1234567890123-abcd1234"); |
| 81 | + if (!activeResult.ok) failures.push("Active session export should be allowed."); |
| 82 | + if (activeResult.status !== "Exported current workspace session JSON.") { |
| 83 | + failures.push("Active session export status mismatch."); |
| 84 | + } |
| 85 | + if (activeResult.filename !== "palette-manager-v2-palette-manager-v2-1234567890123-abcd1234.json") { |
| 86 | + failures.push("Export filename should include tool/session identity."); |
| 87 | + } |
| 88 | + const parsedActive = activeResult.serialized ? JSON.parse(activeResult.serialized) : null; |
| 89 | + if (JSON.stringify(parsedActive) !== JSON.stringify(activePayload)) { |
| 90 | + failures.push("Exported JSON does not preserve active session payload exactly."); |
| 91 | + } |
| 92 | + |
| 93 | + const noActiveResult = simulateExport(null, "asset-browser-v2", ""); |
| 94 | + if (noActiveResult.ok) failures.push("Export should be blocked when no active session exists."); |
| 95 | + if (noActiveResult.status !== "No active Workspace V2 session is available to export.") { |
| 96 | + failures.push("No-active export status mismatch."); |
| 97 | + } |
| 98 | + |
| 99 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 100 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 101 | + generatedAt: new Date().toISOString(), |
| 102 | + failures, |
| 103 | + checks: { |
| 104 | + workspaceJsExists, |
| 105 | + workspaceJsSyntax, |
| 106 | + testSyntax |
| 107 | + }, |
| 108 | + scenarios: { |
| 109 | + activeResult, |
| 110 | + noActiveResult |
| 111 | + } |
| 112 | + }, null, 2)}\n`, "utf8"); |
| 113 | + |
| 114 | + console.log(`v2 current-session export results: ${resultsPath}`); |
| 115 | + assert.equal(failures.length, 0, `V2 current-session export failures: ${failures.join(" | ")}`); |
| 116 | + return { failures }; |
| 117 | +} |
| 118 | + |
| 119 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 120 | + try { |
| 121 | + const summary = run(); |
| 122 | + console.log(JSON.stringify(summary, null, 2)); |
| 123 | + } catch (error) { |
| 124 | + console.error(error); |
| 125 | + process.exitCode = 1; |
| 126 | + } |
| 127 | +} |
0 commit comments