|
| 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-recent-session-delete-results.json"); |
| 13 | + |
| 14 | +function readText(filePath) { |
| 15 | + return fs.readFileSync(filePath, "utf8"); |
| 16 | +} |
| 17 | + |
| 18 | +function checkSyntax(jsFilePath) { |
| 19 | + try { |
| 20 | + execFileSync(process.execPath, ["--check", jsFilePath], { |
| 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 resolvePersistedSelectionIds(entries, persisted) { |
| 31 | + if (!Array.isArray(entries) || entries.length < 2) return { leftId: "", rightId: "" }; |
| 32 | + if (!persisted.sessionA || !persisted.sessionB || persisted.sessionA === persisted.sessionB) return { leftId: "", rightId: "" }; |
| 33 | + const left = entries.find((entry) => entry.contextId === persisted.sessionA); |
| 34 | + const right = entries.find((entry) => entry.contextId === persisted.sessionB); |
| 35 | + if (!left || !right || left.id === right.id) return { leftId: "", rightId: "" }; |
| 36 | + return { leftId: left.id, rightId: right.id }; |
| 37 | +} |
| 38 | + |
| 39 | +export function run() { |
| 40 | + const failures = []; |
| 41 | + const htmlExists = fs.existsSync(htmlPath); |
| 42 | + const jsExists = fs.existsSync(jsPath); |
| 43 | + const html = htmlExists ? readText(htmlPath) : ""; |
| 44 | + const js = jsExists ? readText(jsPath) : ""; |
| 45 | + const syntax = checkSyntax(jsPath); |
| 46 | + |
| 47 | + const hasDeleteSavedLabel = html.includes("Delete Saved Session"); |
| 48 | + const hasRecentDeleteButton = js.includes("deleteRecentButton.textContent = \"Delete\";"); |
| 49 | + const hasRecentDeleteMethod = js.includes("deleteRecentSessionEntry(hostContextId)"); |
| 50 | + const hasRecentDeleteHistoryWrite = js.includes("const nextHistory = history.filter((entry) => entry.hostContextId !== sessionId);") && js.includes("this.writeSessionHistory(nextHistory);"); |
| 51 | + const hasRecentDeleteStorageClear = js.includes("sessionStorage.removeItem(sessionId);"); |
| 52 | + const hasRecentDeleteActiveClear = js.includes("if (this.currentHostContextId === sessionId)") && js.includes("this.currentHostContextId = \"\";"); |
| 53 | + const hasLibraryScopeMessage = js.includes("Session ID is not saved in Session Library. Use Delete on Recent Sessions to remove temporary sessions."); |
| 54 | + |
| 55 | + if (!htmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 56 | + if (!jsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 57 | + if (!syntax.ok) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 58 | + if (!hasDeleteSavedLabel) failures.push("Delete Saved Session label is missing."); |
| 59 | + if (!hasRecentDeleteButton) failures.push("Recent Session row Delete button is missing."); |
| 60 | + if (!hasRecentDeleteMethod) failures.push("deleteRecentSessionEntry(hostContextId) is missing."); |
| 61 | + if (!hasRecentDeleteHistoryWrite) failures.push("Recent delete does not remove the history entry."); |
| 62 | + if (!hasRecentDeleteStorageClear) failures.push("Recent delete does not remove matching sessionStorage payload."); |
| 63 | + if (!hasRecentDeleteActiveClear) failures.push("Recent delete does not clear active hostContextId when deleted."); |
| 64 | + if (!hasLibraryScopeMessage) failures.push("Missing explicit message for recent-only delete attempts in library delete flow."); |
| 65 | + |
| 66 | + const history = [ |
| 67 | + { hostContextId: "ctx-a", tool: "asset-browser-v2", timestamp: "2026-05-01T00:00:00.000Z", payload: { toolId: "asset-browser-v2" } }, |
| 68 | + { hostContextId: "ctx-b", tool: "asset-browser-v2", timestamp: "2026-05-01T00:01:00.000Z", payload: { toolId: "asset-browser-v2" } } |
| 69 | + ]; |
| 70 | + const sessionStorageMap = { "ctx-a": "{\"toolId\":\"asset-browser-v2\"}", "ctx-b": "{\"toolId\":\"asset-browser-v2\"}" }; |
| 71 | + const library = { "saved-1": { toolId: "asset-browser-v2" } }; |
| 72 | + let currentHostContextId = "ctx-a"; |
| 73 | + |
| 74 | + const nextHistory = history.filter((entry) => entry.hostContextId !== "ctx-a"); |
| 75 | + delete sessionStorageMap["ctx-a"]; |
| 76 | + if (currentHostContextId === "ctx-a") currentHostContextId = ""; |
| 77 | + |
| 78 | + if (nextHistory.some((entry) => entry.hostContextId === "ctx-a")) { |
| 79 | + failures.push("Delete Recent should remove the recent entry."); |
| 80 | + } |
| 81 | + if (Object.prototype.hasOwnProperty.call(sessionStorageMap, "ctx-a")) { |
| 82 | + failures.push("Delete Recent should remove matching sessionStorage payload."); |
| 83 | + } |
| 84 | + if (currentHostContextId !== "") { |
| 85 | + failures.push("Deleting active session should clear active state safely."); |
| 86 | + } |
| 87 | + |
| 88 | + const libraryDeleteAttemptId = "ctx-b"; |
| 89 | + const inLibrary = Object.prototype.hasOwnProperty.call(library, libraryDeleteAttemptId); |
| 90 | + const inRecent = nextHistory.some((entry) => entry.hostContextId === libraryDeleteAttemptId); |
| 91 | + if (inLibrary) { |
| 92 | + failures.push("Fixture error: recent-only id unexpectedly exists in library."); |
| 93 | + } |
| 94 | + if (!inRecent) { |
| 95 | + failures.push("Fixture error: recent-only id should exist in recent history."); |
| 96 | + } |
| 97 | + |
| 98 | + const entries = nextHistory.map((entry) => ({ |
| 99 | + id: `history:${entry.hostContextId}`, |
| 100 | + contextId: entry.hostContextId |
| 101 | + })); |
| 102 | + const persisted = { sessionA: "ctx-a", sessionB: "ctx-b" }; |
| 103 | + const restored = resolvePersistedSelectionIds(entries, persisted); |
| 104 | + if (restored.leftId || restored.rightId) { |
| 105 | + failures.push("Deleting a selected session should clear selections referencing it."); |
| 106 | + } |
| 107 | + |
| 108 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 109 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 110 | + generatedAt: new Date().toISOString(), |
| 111 | + failures, |
| 112 | + checks: { |
| 113 | + htmlExists, |
| 114 | + jsExists, |
| 115 | + syntax, |
| 116 | + hasDeleteSavedLabel, |
| 117 | + hasRecentDeleteButton, |
| 118 | + hasRecentDeleteMethod, |
| 119 | + hasRecentDeleteHistoryWrite, |
| 120 | + hasRecentDeleteStorageClear, |
| 121 | + hasRecentDeleteActiveClear, |
| 122 | + hasLibraryScopeMessage |
| 123 | + }, |
| 124 | + scenarios: { |
| 125 | + nextHistory, |
| 126 | + sessionStorageMap, |
| 127 | + currentHostContextId, |
| 128 | + libraryDeleteAttemptId, |
| 129 | + restored |
| 130 | + } |
| 131 | + }, null, 2)}\n`, "utf8"); |
| 132 | + |
| 133 | + console.log(`v2 recent-session delete results: ${resultsPath}`); |
| 134 | + assert.equal(failures.length, 0, `V2 recent-session delete failures: ${failures.join(" | ")}`); |
| 135 | + return { failures }; |
| 136 | +} |
| 137 | + |
| 138 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 139 | + try { |
| 140 | + const summary = run(); |
| 141 | + console.log(JSON.stringify(summary, null, 2)); |
| 142 | + } catch (error) { |
| 143 | + console.error(error); |
| 144 | + process.exitCode = 1; |
| 145 | + } |
| 146 | +} |
0 commit comments