|
| 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 workspaceJsPath = path.join(repoRoot, "tools", "workspace-v2", "index.js"); |
| 11 | +const workspaceHtmlPath = path.join(repoRoot, "tools", "workspace-v2", "index.html"); |
| 12 | +const resultsPath = path.join(repoRoot, "tmp", "v2-reset-state-results.json"); |
| 13 | + |
| 14 | +class MemoryStorage { |
| 15 | + constructor() { |
| 16 | + this.values = new Map(); |
| 17 | + } |
| 18 | + |
| 19 | + getItem(key) { |
| 20 | + if (!this.values.has(String(key))) { |
| 21 | + return null; |
| 22 | + } |
| 23 | + return this.values.get(String(key)); |
| 24 | + } |
| 25 | + |
| 26 | + setItem(key, value) { |
| 27 | + this.values.set(String(key), String(value)); |
| 28 | + } |
| 29 | + |
| 30 | + removeItem(key) { |
| 31 | + this.values.delete(String(key)); |
| 32 | + } |
| 33 | + |
| 34 | + clear() { |
| 35 | + this.values.clear(); |
| 36 | + } |
| 37 | + |
| 38 | + keys() { |
| 39 | + return Array.from(this.values.keys()).sort((left, right) => left.localeCompare(right)); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function readText(filePath) { |
| 44 | + return fs.readFileSync(filePath, "utf8"); |
| 45 | +} |
| 46 | + |
| 47 | +function checkJsSyntax(jsPath) { |
| 48 | + try { |
| 49 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 50 | + cwd: repoRoot, |
| 51 | + stdio: ["ignore", "pipe", "pipe"] |
| 52 | + }); |
| 53 | + return { syntaxValid: true, syntaxError: "" }; |
| 54 | + } catch (error) { |
| 55 | + return { |
| 56 | + syntaxValid: false, |
| 57 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 58 | + }; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function diagnosticsActiveState(currentUrl, currentHostContextId, currentSessionPayload, sessionStorageLike) { |
| 63 | + const params = new URL(currentUrl).searchParams; |
| 64 | + const urlHostContextId = typeof params.get("hostContextId") === "string" ? params.get("hostContextId").trim() : ""; |
| 65 | + const activeHostContextId = urlHostContextId || (typeof currentHostContextId === "string" ? currentHostContextId.trim() : ""); |
| 66 | + if (activeHostContextId) { |
| 67 | + const stored = sessionStorageLike.getItem(activeHostContextId); |
| 68 | + if (!stored) { |
| 69 | + return "EMPTY"; |
| 70 | + } |
| 71 | + try { |
| 72 | + const parsed = JSON.parse(stored); |
| 73 | + if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { |
| 74 | + return "VALID"; |
| 75 | + } |
| 76 | + return "INVALID"; |
| 77 | + } catch { |
| 78 | + return "INVALID"; |
| 79 | + } |
| 80 | + } |
| 81 | + if (currentSessionPayload && typeof currentSessionPayload === "object" && !Array.isArray(currentSessionPayload)) { |
| 82 | + return "VALID"; |
| 83 | + } |
| 84 | + return "EMPTY"; |
| 85 | +} |
| 86 | + |
| 87 | +function runResetSimulation() { |
| 88 | + const sessionStorageLike = new MemoryStorage(); |
| 89 | + const localStorageLike = new MemoryStorage(); |
| 90 | + const state = { |
| 91 | + currentUrl: "https://example.test/tools/workspace-v2/index.html?hostContextId=reset-host-1&panel=diagnostics", |
| 92 | + currentHostContextId: "reset-host-1", |
| 93 | + currentSessionPayload: { |
| 94 | + toolId: "tilemap-studio-v2", |
| 95 | + payloadJson: { |
| 96 | + tileMapDocument: { |
| 97 | + map: { name: "Reset Fixture", width: 2, height: 2 }, |
| 98 | + layers: [{ name: "Ground", kind: "tiles", data: [[1, 1], [1, 1]] }] |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + sessionStorageLike.setItem("reset-host-1", JSON.stringify(state.currentSessionPayload)); |
| 105 | + sessionStorageLike.setItem("unrelated", "{\"hello\":\"world\"}"); |
| 106 | + localStorageLike.setItem("v2-session-library", JSON.stringify({ sample: state.currentSessionPayload })); |
| 107 | + localStorageLike.setItem("v2-error-logs", JSON.stringify([{ tool: "tilemap-studio-v2", type: "INVALID", message: "broken", details: {}, timestamp: new Date().toISOString() }])); |
| 108 | + localStorageLike.setItem("other-key", "leave-me"); |
| 109 | + |
| 110 | + const before = { |
| 111 | + url: state.currentUrl, |
| 112 | + sessionStorageKeys: sessionStorageLike.keys(), |
| 113 | + localStorageKeys: localStorageLike.keys(), |
| 114 | + activeState: diagnosticsActiveState(state.currentUrl, state.currentHostContextId, state.currentSessionPayload, sessionStorageLike) |
| 115 | + }; |
| 116 | + |
| 117 | + const clearSessionStorage = () => { |
| 118 | + sessionStorageLike.clear(); |
| 119 | + state.currentHostContextId = ""; |
| 120 | + }; |
| 121 | + const clearSavedSessions = () => { |
| 122 | + localStorageLike.removeItem("v2-session-library"); |
| 123 | + }; |
| 124 | + const clearErrorLogs = () => { |
| 125 | + localStorageLike.removeItem("v2-error-logs"); |
| 126 | + }; |
| 127 | + const resetUrlState = () => { |
| 128 | + const current = new URL(state.currentUrl); |
| 129 | + state.currentUrl = `${current.origin}${current.pathname}${current.hash || ""}`; |
| 130 | + state.currentHostContextId = ""; |
| 131 | + }; |
| 132 | + const fullReset = () => { |
| 133 | + clearSessionStorage(); |
| 134 | + clearSavedSessions(); |
| 135 | + clearErrorLogs(); |
| 136 | + resetUrlState(); |
| 137 | + state.currentSessionPayload = null; |
| 138 | + }; |
| 139 | + |
| 140 | + fullReset(); |
| 141 | + const afterFirst = { |
| 142 | + url: state.currentUrl, |
| 143 | + sessionStorageKeys: sessionStorageLike.keys(), |
| 144 | + localStorageKeys: localStorageLike.keys(), |
| 145 | + activeState: diagnosticsActiveState(state.currentUrl, state.currentHostContextId, state.currentSessionPayload, sessionStorageLike) |
| 146 | + }; |
| 147 | + |
| 148 | + fullReset(); |
| 149 | + const afterSecond = { |
| 150 | + url: state.currentUrl, |
| 151 | + sessionStorageKeys: sessionStorageLike.keys(), |
| 152 | + localStorageKeys: localStorageLike.keys(), |
| 153 | + activeState: diagnosticsActiveState(state.currentUrl, state.currentHostContextId, state.currentSessionPayload, sessionStorageLike) |
| 154 | + }; |
| 155 | + |
| 156 | + return { before, afterFirst, afterSecond }; |
| 157 | +} |
| 158 | + |
| 159 | +export function run() { |
| 160 | + const failures = []; |
| 161 | + const workspaceJsExists = fs.existsSync(workspaceJsPath); |
| 162 | + const workspaceHtmlExists = fs.existsSync(workspaceHtmlPath); |
| 163 | + const workspaceJsText = workspaceJsExists ? readText(workspaceJsPath) : ""; |
| 164 | + const workspaceHtmlText = workspaceHtmlExists ? readText(workspaceHtmlPath) : ""; |
| 165 | + const { syntaxValid, syntaxError } = checkJsSyntax(workspaceJsPath); |
| 166 | + |
| 167 | + const htmlHasResetControls = workspaceHtmlText.includes("workspaceV2ClearSessionStorageButton") && |
| 168 | + workspaceHtmlText.includes("workspaceV2ClearSavedSessionsButton") && |
| 169 | + workspaceHtmlText.includes("workspaceV2ResetClearErrorLogsButton") && |
| 170 | + workspaceHtmlText.includes("workspaceV2FullResetButton"); |
| 171 | + |
| 172 | + const jsHasClearSessionStorage = workspaceJsText.includes("clearSessionStorage(emitStatus = true)"); |
| 173 | + const jsHasClearSavedSessions = workspaceJsText.includes("clearSavedSessions(emitStatus = true)"); |
| 174 | + const jsHasClearErrorLogs = workspaceJsText.includes("clearErrorLogs(emitStatus = true)"); |
| 175 | + const jsHasResetUrlState = workspaceJsText.includes("resetUrlState(emitStatus = true)"); |
| 176 | + const jsHasFullReset = workspaceJsText.includes("fullReset()"); |
| 177 | + const jsRemovesSessionLibrary = workspaceJsText.includes("localStorage.removeItem(this.libraryStorageKey)"); |
| 178 | + const jsRemovesErrorLogs = workspaceJsText.includes("localStorage.removeItem(this.errorLogsStorageKey)"); |
| 179 | + const jsClearsSessionStorage = workspaceJsText.includes("sessionStorage.clear()"); |
| 180 | + |
| 181 | + if (!workspaceJsExists) failures.push("Missing tools/workspace-v2/index.js."); |
| 182 | + if (!workspaceHtmlExists) failures.push("Missing tools/workspace-v2/index.html."); |
| 183 | + if (!syntaxValid) failures.push("tools/workspace-v2/index.js failed syntax check."); |
| 184 | + if (!htmlHasResetControls) failures.push("Reset controls markup is missing required buttons."); |
| 185 | + if (!jsHasClearSessionStorage) failures.push("Missing clearSessionStorage(emitStatus = true)."); |
| 186 | + if (!jsHasClearSavedSessions) failures.push("Missing clearSavedSessions(emitStatus = true)."); |
| 187 | + if (!jsHasClearErrorLogs) failures.push("Missing clearErrorLogs(emitStatus = true)."); |
| 188 | + if (!jsHasResetUrlState) failures.push("Missing resetUrlState(emitStatus = true)."); |
| 189 | + if (!jsHasFullReset) failures.push("Missing fullReset()."); |
| 190 | + if (!jsRemovesSessionLibrary) failures.push("Expected localStorage removal of v2-session-library."); |
| 191 | + if (!jsRemovesErrorLogs) failures.push("Expected localStorage removal of v2-error-logs."); |
| 192 | + if (!jsClearsSessionStorage) failures.push("Expected sessionStorage.clear() usage."); |
| 193 | + |
| 194 | + const simulation = runResetSimulation(); |
| 195 | + if (!simulation.before.url.includes("hostContextId=")) { |
| 196 | + failures.push("Simulation precondition missing hostContextId in URL."); |
| 197 | + } |
| 198 | + if (simulation.afterFirst.sessionStorageKeys.length !== 0) { |
| 199 | + failures.push(`Expected empty sessionStorage after reset, got ${simulation.afterFirst.sessionStorageKeys.length} keys.`); |
| 200 | + } |
| 201 | + if (simulation.afterFirst.localStorageKeys.includes("v2-session-library")) { |
| 202 | + failures.push("v2-session-library was not removed by reset."); |
| 203 | + } |
| 204 | + if (simulation.afterFirst.localStorageKeys.includes("v2-error-logs")) { |
| 205 | + failures.push("v2-error-logs was not removed by reset."); |
| 206 | + } |
| 207 | + if (simulation.afterFirst.url.includes("hostContextId=")) { |
| 208 | + failures.push("URL reset did not remove hostContextId query param."); |
| 209 | + } |
| 210 | + if (simulation.afterFirst.activeState !== "EMPTY") { |
| 211 | + failures.push(`Expected EMPTY state after reset, got ${simulation.afterFirst.activeState}.`); |
| 212 | + } |
| 213 | + if (simulation.afterSecond.activeState !== "EMPTY") { |
| 214 | + failures.push(`Expected EMPTY state after second reset, got ${simulation.afterSecond.activeState}.`); |
| 215 | + } |
| 216 | + |
| 217 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 218 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 219 | + generatedAt: new Date().toISOString(), |
| 220 | + failures, |
| 221 | + workspaceChecks: { |
| 222 | + workspaceJsExists, |
| 223 | + workspaceHtmlExists, |
| 224 | + syntaxValid, |
| 225 | + syntaxError, |
| 226 | + htmlHasResetControls, |
| 227 | + jsHasClearSessionStorage, |
| 228 | + jsHasClearSavedSessions, |
| 229 | + jsHasClearErrorLogs, |
| 230 | + jsHasResetUrlState, |
| 231 | + jsHasFullReset, |
| 232 | + jsRemovesSessionLibrary, |
| 233 | + jsRemovesErrorLogs, |
| 234 | + jsClearsSessionStorage |
| 235 | + }, |
| 236 | + simulation |
| 237 | + }, null, 2)}\n`, "utf8"); |
| 238 | + |
| 239 | + console.log(`v2 reset state results: ${resultsPath}`); |
| 240 | + assert.equal(failures.length, 0, `V2 reset state failures: ${failures.join(" | ")}`); |
| 241 | + return { failures, simulation }; |
| 242 | +} |
| 243 | + |
| 244 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 245 | + try { |
| 246 | + const summary = run(); |
| 247 | + console.log(JSON.stringify(summary, null, 2)); |
| 248 | + } catch (error) { |
| 249 | + console.error(error); |
| 250 | + process.exitCode = 1; |
| 251 | + } |
| 252 | +} |
0 commit comments