|
| 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", "V2SessionLibrarySaveGuard.test.mjs"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-session-library-save-guard-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 canSaveSession({ hasActiveSession, newId, idValid, idExists }) { |
| 27 | + return Boolean(Boolean(newId) && idValid && !idExists && hasActiveSession); |
| 28 | +} |
| 29 | + |
| 30 | +export function run() { |
| 31 | + const failures = []; |
| 32 | + const jsExists = fs.existsSync(jsPath); |
| 33 | + const js = jsExists ? fs.readFileSync(jsPath, "utf8") : ""; |
| 34 | + const jsSyntax = checkSyntax(jsPath); |
| 35 | + const testSyntax = checkSyntax(testPath); |
| 36 | + |
| 37 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 38 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 39 | + if (!testSyntax.ok) failures.push("tests/runtime/V2SessionLibrarySaveGuard.test.mjs failed syntax check."); |
| 40 | + |
| 41 | + const requiredTokens = [ |
| 42 | + "hasActiveWorkspaceSessionForSave()", |
| 43 | + "isValidNewSessionId(sessionId)", |
| 44 | + "savedSessionIdExists(sessionId)", |
| 45 | + "libraryCanSave", |
| 46 | + "this.saveSessionButton.disabled = !model.libraryCanSave;", |
| 47 | + "That session ID already exists. Use the saved session card to Load, Overwrite, or Delete it.", |
| 48 | + "loadSavedSessionById(sessionId)", |
| 49 | + "this.loadNamedSession();", |
| 50 | + "this.syncDiffAndMergeSelectionSlotsFromContextId(sessionId.trim());" |
| 51 | + ]; |
| 52 | + requiredTokens.forEach((token) => { |
| 53 | + if (!js.includes(token)) failures.push(`Missing required save-guard token: ${token}`); |
| 54 | + }); |
| 55 | + |
| 56 | + const scenarios = { |
| 57 | + emptyIdDisabled: canSaveSession({ hasActiveSession: true, newId: "", idValid: false, idExists: false }), |
| 58 | + invalidIdDisabled: canSaveSession({ hasActiveSession: true, newId: "bad id", idValid: false, idExists: false }), |
| 59 | + duplicateIdDisabled: canSaveSession({ hasActiveSession: true, newId: "session-a", idValid: true, idExists: true }), |
| 60 | + noActiveSessionDisabled: canSaveSession({ hasActiveSession: false, newId: "session-a", idValid: true, idExists: false }), |
| 61 | + validEnabled: canSaveSession({ hasActiveSession: true, newId: "session-a", idValid: true, idExists: false }) |
| 62 | + }; |
| 63 | + |
| 64 | + if (scenarios.emptyIdDisabled) failures.push("Save should be disabled for empty ID."); |
| 65 | + if (scenarios.invalidIdDisabled) failures.push("Save should be disabled for invalid ID."); |
| 66 | + if (scenarios.duplicateIdDisabled) failures.push("Save should be disabled for duplicate ID."); |
| 67 | + if (scenarios.noActiveSessionDisabled) failures.push("Save should be disabled without active session."); |
| 68 | + if (!scenarios.validEnabled) failures.push("Save should be enabled only for valid new ID with active session."); |
| 69 | + |
| 70 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 71 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 72 | + generatedAt: new Date().toISOString(), |
| 73 | + failures, |
| 74 | + checks: { jsExists, jsSyntax, testSyntax }, |
| 75 | + scenarios |
| 76 | + }, null, 2)} |
| 77 | +`, "utf8"); |
| 78 | + |
| 79 | + console.log(`v2 session-library-save-guard results: ${resultsPath}`); |
| 80 | + assert.equal(failures.length, 0, `V2 session-library-save-guard failures: ${failures.join(" | ")}`); |
| 81 | + return { failures }; |
| 82 | +} |
| 83 | + |
| 84 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 85 | + try { |
| 86 | + const summary = run(); |
| 87 | + console.log(JSON.stringify(summary, null, 2)); |
| 88 | + } catch (error) { |
| 89 | + console.error(error); |
| 90 | + process.exitCode = 1; |
| 91 | + } |
| 92 | +} |
0 commit comments