|
| 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 jsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 11 | +const testPath = path.join(repoRoot, "tests", "runtime", "V2SessionLibraryCardOverwrite.test.mjs"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-session-library-card-overwrite-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 overwriteSavedSession(library, sessionId, activePayload) { |
| 27 | + const next = JSON.parse(JSON.stringify(library)); |
| 28 | + if (!sessionId || !Object.prototype.hasOwnProperty.call(next, sessionId)) { |
| 29 | + return { ok: false, message: "Saved session not found. Use Save Session to create it first.", library: next }; |
| 30 | + } |
| 31 | + if (!activePayload || typeof activePayload !== "object" || Array.isArray(activePayload)) { |
| 32 | + return { ok: false, message: "No active Workspace V2 session is available to overwrite from.", library: next }; |
| 33 | + } |
| 34 | + next[sessionId] = JSON.parse(JSON.stringify(activePayload)); |
| 35 | + return { ok: true, message: "Saved session updated.", library: next }; |
| 36 | +} |
| 37 | + |
| 38 | +export function run() { |
| 39 | + const failures = []; |
| 40 | + const jsExists = fs.existsSync(jsPath); |
| 41 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 42 | + const jsSyntax = checkSyntax(jsPath); |
| 43 | + const testSyntax = checkSyntax(testPath); |
| 44 | + |
| 45 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 46 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 47 | + if (!testSyntax.ok) failures.push("tests/runtime/V2SessionLibraryCardOverwrite.test.mjs failed syntax check."); |
| 48 | + |
| 49 | + const requiredTokens = [ |
| 50 | + "overwriteButton.textContent = \"Overwrite\";", |
| 51 | + "overwriteButton.disabled = !canOverwriteFromActiveSession;", |
| 52 | + "overwriteSavedSessionById(sessionId)", |
| 53 | + "No active Workspace V2 session is available to overwrite from.", |
| 54 | + "Saved session updated." |
| 55 | + ]; |
| 56 | + requiredTokens.forEach((token) => { |
| 57 | + if (!js.includes(token)) failures.push(`Missing overwrite-card token/text: ${token}`); |
| 58 | + }); |
| 59 | + |
| 60 | + const initialLibrary = { |
| 61 | + "saved-1": { version: "v2", toolId: "palette-manager-v2", payloadJson: { original: true } }, |
| 62 | + "saved-2": { version: "v2", toolId: "asset-browser-v2", payloadJson: { untouched: true } } |
| 63 | + }; |
| 64 | + const activePayload = { version: "v2", toolId: "palette-manager-v2", payloadJson: { updated: true } }; |
| 65 | + |
| 66 | + const noActive = overwriteSavedSession(initialLibrary, "saved-1", null); |
| 67 | + if (noActive.ok || noActive.message !== "No active Workspace V2 session is available to overwrite from.") { |
| 68 | + failures.push("Overwrite should be blocked without active session."); |
| 69 | + } |
| 70 | + |
| 71 | + const missingSaved = overwriteSavedSession(initialLibrary, "missing-id", activePayload); |
| 72 | + if (missingSaved.ok || missingSaved.message !== "Saved session not found. Use Save Session to create it first.") { |
| 73 | + failures.push("Overwrite should require an existing saved session ID."); |
| 74 | + } |
| 75 | + |
| 76 | + const overwritten = overwriteSavedSession(initialLibrary, "saved-1", activePayload); |
| 77 | + if (!overwritten.ok || overwritten.message !== "Saved session updated.") { |
| 78 | + failures.push("Overwrite should succeed and return update confirmation."); |
| 79 | + } |
| 80 | + if (!overwritten.library["saved-1"] || overwritten.library["saved-1"].payloadJson?.updated !== true) { |
| 81 | + failures.push("Overwrite should replace saved payload with active payload."); |
| 82 | + } |
| 83 | + if (Object.keys(overwritten.library).length !== Object.keys(initialLibrary).length) { |
| 84 | + failures.push("Overwrite should not create a new saved session entry."); |
| 85 | + } |
| 86 | + if (overwritten.library["saved-2"].payloadJson?.untouched !== true) { |
| 87 | + failures.push("Overwrite should not mutate other saved session entries."); |
| 88 | + } |
| 89 | + |
| 90 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 91 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 92 | + generatedAt: new Date().toISOString(), |
| 93 | + failures, |
| 94 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 95 | + scenarios: { noActive, missingSaved, overwritten } |
| 96 | + }, null, 2)} |
| 97 | +`, "utf8"); |
| 98 | + |
| 99 | + console.log(`v2 session-library-card-overwrite results: ${resultsPath}`); |
| 100 | + assert.equal(failures.length, 0, `V2 session-library-card-overwrite failures: ${failures.join(" | ")}`); |
| 101 | + return { failures }; |
| 102 | +} |
| 103 | + |
| 104 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 105 | + try { |
| 106 | + const summary = run(); |
| 107 | + console.log(JSON.stringify(summary, null, 2)); |
| 108 | + } catch (error) { |
| 109 | + console.error(error); |
| 110 | + process.exitCode = 1; |
| 111 | + } |
| 112 | +} |
0 commit comments