|
| 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 resultsPath = path.join(repoRoot, "tmp", "v2-saved-session-row-actions-results.json"); |
| 13 | + |
| 14 | +function readText(filePath) { |
| 15 | + return fs.readFileSync(filePath, "utf8"); |
| 16 | +} |
| 17 | + |
| 18 | +function checkSyntax(filePath) { |
| 19 | + try { |
| 20 | + execFileSync(process.execPath, ["--check", filePath], { |
| 21 | + cwd: repoRoot, |
| 22 | + stdio: ["ignore", "pipe", "pipe"] |
| 23 | + }); |
| 24 | + return { ok: true, error: "" }; |
| 25 | + } catch (error) { |
| 26 | + return { ok: false, error: (error?.stderr || error?.stdout || error?.message || "").toString().trim() }; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function simulateDeleteSavedRow(sessionId, library, history, storage) { |
| 31 | + const nextLibrary = { ...library }; |
| 32 | + const nextHistory = [...history]; |
| 33 | + const nextStorage = { ...storage }; |
| 34 | + if (Object.prototype.hasOwnProperty.call(nextLibrary, sessionId)) { |
| 35 | + delete nextLibrary[sessionId]; |
| 36 | + return { message: "Saved session deleted.", nextLibrary, nextHistory, nextStorage }; |
| 37 | + } |
| 38 | + return { message: "Saved session not found.", nextLibrary, nextHistory, nextStorage }; |
| 39 | +} |
| 40 | + |
| 41 | +export function run() { |
| 42 | + const failures = []; |
| 43 | + const htmlExists = fs.existsSync(htmlPath); |
| 44 | + const jsExists = fs.existsSync(jsPath); |
| 45 | + const html = htmlExists ? readText(htmlPath) : ""; |
| 46 | + const js = jsExists ? readText(jsPath) : ""; |
| 47 | + const jsSyntax = checkSyntax(jsPath); |
| 48 | + const testSyntax = checkSyntax(path.join(repoRoot, "tests", "runtime", "V2SavedSessionRowActions.test.mjs")); |
| 49 | + |
| 50 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 51 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 52 | + if (!jsSyntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 53 | + if (!testSyntax.ok) failures.push("tests/runtime/V2SavedSessionRowActions.test.mjs failed syntax check."); |
| 54 | + |
| 55 | + if (!html.includes("Saved sessions are stored in Session Library. Recent sessions are temporary.")) { |
| 56 | + failures.push("Helper text is not updated to saved-vs-recent clarification."); |
| 57 | + } |
| 58 | + |
| 59 | + const requiredRowTokens = [ |
| 60 | + "copyIdButton.textContent = \"Copy ID\";", |
| 61 | + "useInLibraryButton.textContent = \"Use in Library\";", |
| 62 | + "loadButton.textContent = \"Load\";", |
| 63 | + "deleteSavedButton.textContent = \"Delete Saved\";", |
| 64 | + "copySavedSessionIdToClipboard(sessionId)", |
| 65 | + "useSavedSessionIdInLibraryInput(sessionId)", |
| 66 | + "loadSavedSessionById(sessionId)", |
| 67 | + "deleteSavedSessionById(sessionId)" |
| 68 | + ]; |
| 69 | + requiredRowTokens.forEach((token) => { |
| 70 | + if (!js.includes(token)) { |
| 71 | + failures.push(`Missing required saved-row action token: ${token}`); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + if (!js.includes("await navigator.clipboard.writeText(sessionId.trim());")) { |
| 76 | + failures.push("Row Copy ID does not copy exact saved session ID."); |
| 77 | + } |
| 78 | + if (!js.includes("this.sessionNameNode.value = sessionId.trim();")) { |
| 79 | + failures.push("Use in Library / row actions are not populating Session ID textbox."); |
| 80 | + } |
| 81 | + if (!js.includes("this.loadNamedSession();")) { |
| 82 | + failures.push("Row Load is not wired to library load action."); |
| 83 | + } |
| 84 | + if (!js.includes("this.deleteNamedSession();")) { |
| 85 | + failures.push("Row Delete Saved is not wired to library delete action."); |
| 86 | + } |
| 87 | + if (!js.includes("this.renderSessionLibrary();")) { |
| 88 | + failures.push("Session Library refresh after row actions is missing."); |
| 89 | + } |
| 90 | + if (!js.includes("this.renderSessionDiffInputs();") || !js.includes("this.renderSessionMergeInputs();")) { |
| 91 | + failures.push("Diff/Merge selector recompute hooks are missing."); |
| 92 | + } |
| 93 | + |
| 94 | + const library = { |
| 95 | + "asset-browser-v2-1777676088718-3eff5h3y": { version: "v2", toolId: "asset-browser-v2", payloadJson: { a: 1 } }, |
| 96 | + "palette-manager-v2-1777676088720-abcd1234": { version: "v2", toolId: "palette-manager-v2", payloadJson: { b: 2 } } |
| 97 | + }; |
| 98 | + const history = [{ hostContextId: "asset-browser-v2-1777676088718-3eff5h3y", tool: "asset-browser-v2", timestamp: "2026-05-01T00:00:00.000Z", payload: { version: "v2", toolId: "asset-browser-v2" } }]; |
| 99 | + const storage = { "asset-browser-v2-1777676088718-3eff5h3y": "{\"version\":\"v2\",\"toolId\":\"asset-browser-v2\"}" }; |
| 100 | + |
| 101 | + const deleted = simulateDeleteSavedRow("asset-browser-v2-1777676088718-3eff5h3y", library, history, storage); |
| 102 | + if (deleted.message !== "Saved session deleted.") { |
| 103 | + failures.push("Row Delete Saved should delete saved library entry."); |
| 104 | + } |
| 105 | + if (Object.prototype.hasOwnProperty.call(deleted.nextLibrary, "asset-browser-v2-1777676088718-3eff5h3y")) { |
| 106 | + failures.push("Row Delete Saved did not remove saved entry."); |
| 107 | + } |
| 108 | + if (deleted.nextHistory.length !== history.length) { |
| 109 | + failures.push("Row Delete Saved must not remove Recent Sessions."); |
| 110 | + } |
| 111 | + if (!Object.prototype.hasOwnProperty.call(deleted.nextStorage, "asset-browser-v2-1777676088718-3eff5h3y")) { |
| 112 | + failures.push("Row Delete Saved must not remove sessionStorage payload."); |
| 113 | + } |
| 114 | + |
| 115 | + const textboxActionsStillPresent = [ |
| 116 | + "saveNamedSession(overwriteExisting)", |
| 117 | + "loadNamedSession()", |
| 118 | + "deleteNamedSession()", |
| 119 | + "Saved session already exists. Use Overwrite Session.", |
| 120 | + "Saved session not found.", |
| 121 | + "Saved session loaded.", |
| 122 | + "Saved session deleted." |
| 123 | + ]; |
| 124 | + textboxActionsStillPresent.forEach((token) => { |
| 125 | + if (!js.includes(token)) { |
| 126 | + failures.push(`Textbox Session Library action token missing: ${token}`); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 131 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 132 | + generatedAt: new Date().toISOString(), |
| 133 | + failures, |
| 134 | + checks: { |
| 135 | + htmlExists, |
| 136 | + jsExists, |
| 137 | + jsSyntax, |
| 138 | + testSyntax |
| 139 | + }, |
| 140 | + scenarios: { |
| 141 | + deleted |
| 142 | + } |
| 143 | + }, null, 2)}\n`, "utf8"); |
| 144 | + |
| 145 | + console.log(`v2 saved-session-row-actions results: ${resultsPath}`); |
| 146 | + assert.equal(failures.length, 0, `V2 saved-session-row-actions failures: ${failures.join(" | ")}`); |
| 147 | + return { failures }; |
| 148 | +} |
| 149 | + |
| 150 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 151 | + try { |
| 152 | + const summary = run(); |
| 153 | + console.log(JSON.stringify(summary, null, 2)); |
| 154 | + } catch (error) { |
| 155 | + console.error(error); |
| 156 | + process.exitCode = 1; |
| 157 | + } |
| 158 | +} |
0 commit comments