|
| 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 htmlPath = path.join(repoRoot, "tools", "workspace-v2", "index.html"); |
| 11 | +const jsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 12 | +const testPath = path.join(repoRoot, "tests", "runtime", "V2WorkspaceDefaultToolInitialization.test.mjs"); |
| 13 | +const resultsPath = path.join(repoRoot, "tmp", "v2-workspace-default-tool-init-results.json"); |
| 14 | + |
| 15 | +function checkSyntax(filePath) { |
| 16 | + try { |
| 17 | + execFileSync(process.execPath, ["--check", filePath], { |
| 18 | + cwd: repoRoot, |
| 19 | + stdio: ["ignore", "pipe", "pipe"] |
| 20 | + }); |
| 21 | + return { ok: true, error: "" }; |
| 22 | + } catch (error) { |
| 23 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function simulateInitialization(selectedToolId, existingPayload, existingHostContextId) { |
| 28 | + if (existingPayload && existingHostContextId) { |
| 29 | + return { initialized: false, blocked: false, payload: existingPayload, hostContextId: existingHostContextId }; |
| 30 | + } |
| 31 | + if (!selectedToolId) { |
| 32 | + return { initialized: false, blocked: true, message: "Workspace V2 initialization blocked: default tool is missing." }; |
| 33 | + } |
| 34 | + const payload = { version: "v2", toolId: selectedToolId, payloadJson: {} }; |
| 35 | + return { initialized: true, blocked: false, payload, hostContextId: `${selectedToolId}-1234567890123-abcd1234` }; |
| 36 | +} |
| 37 | + |
| 38 | +export function run() { |
| 39 | + const failures = []; |
| 40 | + const htmlExists = fs.existsSync(htmlPath); |
| 41 | + const jsExists = fs.existsSync(jsPath); |
| 42 | + const html = htmlExists ? fs.readFileSync(htmlPath, "utf8") : ""; |
| 43 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 44 | + const jsSyntax = checkSyntax(jsPath); |
| 45 | + const testSyntax = checkSyntax(testPath); |
| 46 | + |
| 47 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 48 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 49 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 50 | + if (!testSyntax.ok) failures.push("tests/runtime/V2WorkspaceDefaultToolInitialization.test.mjs failed syntax check."); |
| 51 | + |
| 52 | + const requiredHtmlTokens = [ |
| 53 | + '<option value="palette-manager-v2" selected>Palette Manager V2</option>' |
| 54 | + ]; |
| 55 | + requiredHtmlTokens.forEach((token) => { |
| 56 | + if (!html.includes(token)) failures.push(`Missing required default tool HTML token: ${token}`); |
| 57 | + }); |
| 58 | + |
| 59 | + const requiredJsTokens = [ |
| 60 | + "this.applyDefaultWorkspaceToolSelection();", |
| 61 | + "this.initializeWorkspaceProducerSession();", |
| 62 | + "applyDefaultWorkspaceToolSelection()", |
| 63 | + 'const defaultToolId = "palette-manager-v2";', |
| 64 | + "initializeWorkspaceProducerSession()", |
| 65 | + "payloadJson: {}", |
| 66 | + 'this.setCurrentSessionPayload(initialPayload, "workspace-v2-init");', |
| 67 | + "Session is active for Save Session." |
| 68 | + ]; |
| 69 | + requiredJsTokens.forEach((token) => { |
| 70 | + if (!js.includes(token)) failures.push(`Missing required initialization token: ${token}`); |
| 71 | + }); |
| 72 | + |
| 73 | + const noExisting = simulateInitialization("palette-manager-v2", null, ""); |
| 74 | + if (!noExisting.initialized) failures.push("Initialization should create an active session when none exists."); |
| 75 | + if (noExisting.payload?.toolId !== "palette-manager-v2") failures.push("Initialized payload should use Palette Manager toolId."); |
| 76 | + if (!noExisting.payload || noExisting.payload.version !== "v2") failures.push("Initialized payload should be versioned as v2."); |
| 77 | + |
| 78 | + const existing = simulateInitialization("palette-manager-v2", { version: "v2", toolId: "palette-manager-v2", payloadJson: {} }, "palette-manager-v2-1"); |
| 79 | + if (existing.initialized) failures.push("Initialization should not recreate session when active session already exists."); |
| 80 | + |
| 81 | + const blocked = simulateInitialization("", null, ""); |
| 82 | + if (!blocked.blocked) failures.push("Initialization should block when no default tool is available."); |
| 83 | + |
| 84 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 85 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 86 | + generatedAt: new Date().toISOString(), |
| 87 | + failures, |
| 88 | + checks: { htmlExists, jsExists, jsSyntax, testSyntax }, |
| 89 | + scenarios: { noExisting, existing, blocked } |
| 90 | + }, null, 2)} |
| 91 | +`, "utf8"); |
| 92 | + |
| 93 | + console.log(`v2 workspace-default-tool-init results: ${resultsPath}`); |
| 94 | + assert.equal(failures.length, 0, `V2 workspace-default-tool-init failures: ${failures.join(" | ")}`); |
| 95 | + return { failures }; |
| 96 | +} |
| 97 | + |
| 98 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 99 | + try { |
| 100 | + const summary = run(); |
| 101 | + console.log(JSON.stringify(summary, null, 2)); |
| 102 | + } catch (error) { |
| 103 | + console.error(error); |
| 104 | + process.exitCode = 1; |
| 105 | + } |
| 106 | +} |
0 commit comments