|
| 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 assetManagerPath = path.join(repoRoot, "tools", "asset-manager-v2", "index.js"); |
| 11 | +const workspacePath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "pr_11_314_asset_manager_workspace_persistence_results.json"); |
| 13 | + |
| 14 | +function readText(filePath) { |
| 15 | + return fs.readFileSync(filePath, "utf8"); |
| 16 | +} |
| 17 | + |
| 18 | +function checkSyntax(filePath) { |
| 19 | + execFileSync(process.execPath, ["--check", filePath], { |
| 20 | + cwd: repoRoot, |
| 21 | + stdio: ["ignore", "pipe", "pipe"] |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +function simulatePersistence(hostContextId, sessionContext, limitBytes) { |
| 26 | + if (!hostContextId) return { ok: false, message: "No hostContextId is available for Workspace V2 persistence." }; |
| 27 | + if (!sessionContext || typeof sessionContext !== "object" || Array.isArray(sessionContext)) { |
| 28 | + return { ok: false, message: "Session context is invalid for Workspace V2 persistence." }; |
| 29 | + } |
| 30 | + if (!sessionContext.payloadJson || typeof sessionContext.payloadJson !== "object" || Array.isArray(sessionContext.payloadJson)) { |
| 31 | + return { ok: false, message: "payloadJson is invalid for Workspace V2 persistence." }; |
| 32 | + } |
| 33 | + if (!sessionContext.payloadJson.assetCatalog || typeof sessionContext.payloadJson.assetCatalog !== "object" || Array.isArray(sessionContext.payloadJson.assetCatalog)) { |
| 34 | + return { ok: false, message: "payloadJson.assetCatalog is invalid for Workspace V2 persistence." }; |
| 35 | + } |
| 36 | + const payload = { |
| 37 | + version: "v2", |
| 38 | + toolId: "asset-manager-v2", |
| 39 | + payloadJson: sessionContext.payloadJson |
| 40 | + }; |
| 41 | + const serialized = JSON.stringify(payload); |
| 42 | + if (serialized.length > limitBytes) { |
| 43 | + return { ok: false, message: `Session size exceeds allowed limit for Workspace V2 persistence. Payload is ${serialized.length} bytes and limit is ${limitBytes} bytes.` }; |
| 44 | + } |
| 45 | + return { ok: true, payload: JSON.parse(serialized) }; |
| 46 | +} |
| 47 | + |
| 48 | +export function run() { |
| 49 | + checkSyntax(assetManagerPath); |
| 50 | + checkSyntax(workspacePath); |
| 51 | + |
| 52 | + const assetSource = readText(assetManagerPath); |
| 53 | + const workspaceSource = readText(workspacePath); |
| 54 | + |
| 55 | + const validSession = { |
| 56 | + version: "v2", |
| 57 | + toolId: "asset-manager-v2", |
| 58 | + payloadJson: { |
| 59 | + assetCatalog: { |
| 60 | + name: "Catalog", |
| 61 | + entries: [ |
| 62 | + { id: "a1", label: "Ship", kind: "svg", path: "assets/ship.svg" } |
| 63 | + ] |
| 64 | + } |
| 65 | + } |
| 66 | + }; |
| 67 | + const invalidSession = { |
| 68 | + version: "v2", |
| 69 | + toolId: "asset-manager-v2", |
| 70 | + payloadJson: {} |
| 71 | + }; |
| 72 | + |
| 73 | + const validWrite = simulatePersistence("asset-manager-v2-123", validSession, 1024 * 1024); |
| 74 | + const invalidWrite = simulatePersistence("asset-manager-v2-123", invalidSession, 1024 * 1024); |
| 75 | + |
| 76 | + const summary = { |
| 77 | + generatedAt: new Date().toISOString(), |
| 78 | + checks: { |
| 79 | + noDeferredWorkspaceWriteMessage: !assetSource.includes("Workspace writes are deferred for this isolated V2 entry."), |
| 80 | + hasValidSessionPersistenceMethod: assetSource.includes("persistValidSessionForWorkspace(sessionContext, assetCatalog)") && |
| 81 | + assetSource.includes("window.sessionStorage.setItem(this.urlState.hostContextId, serializedPayload);"), |
| 82 | + hasWorkspacePersistenceReadout: assetSource.includes("Workspace session context was read and persisted for Workspace V2 export."), |
| 83 | + hasWorkspaceHostContextRestore: workspaceSource.includes("restoreActiveSessionFromHostContextIdUrl()") && |
| 84 | + workspaceSource.includes("this.restoreActiveSessionFromHostContextIdUrl();") && |
| 85 | + workspaceSource.includes("if (parsed.value.toolId !== \"asset-manager-v2\")") && |
| 86 | + workspaceSource.includes("this.syncWorkspaceManifestTextarea();"), |
| 87 | + validWriteAllowed: validWrite.ok === true && validWrite.payload?.payloadJson?.assetCatalog?.name === "Catalog", |
| 88 | + invalidWriteBlocked: invalidWrite.ok === false |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 93 | + fs.writeFileSync(resultsPath, `${JSON.stringify(summary, null, 2)}\n`, "utf8"); |
| 94 | + |
| 95 | + for (const [key, value] of Object.entries(summary.checks)) { |
| 96 | + assert.equal(value, true, `${key} failed`); |
| 97 | + } |
| 98 | + |
| 99 | + return summary; |
| 100 | +} |
| 101 | + |
| 102 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 103 | + try { |
| 104 | + const summary = run(); |
| 105 | + console.log(JSON.stringify(summary, null, 2)); |
| 106 | + } catch (error) { |
| 107 | + console.error(error); |
| 108 | + process.exitCode = 1; |
| 109 | + } |
| 110 | +} |
0 commit comments