import assert from "node:assert/strict"; import { startFakeDecoder } from "./browser_fake_decoder.js"; // A user running AMMC themselves on another port: type the new WebSocket URL in Settings -> // Connection, press Save, then Connect now - the decoder must come online on that port. export async function runDecoderUrlSmoke({ evaluate, navigate, savedState, waitFor, base }) { const decoder = await startFakeDecoder(); const before = await savedState(); try { await navigate("settings", "settingsForm"); await evaluate(`document.querySelector('[data-settings-tab="connection"]').click()`); const url = `ws://127.0.0.1:${decoder.port}`; await evaluate(`(() => { const input = document.querySelector('#settingsForm [name="wsUrl"]'); input.value = ${JSON.stringify(url)}; input.dispatchEvent(new Event("input", { bubbles: true })); input.dispatchEvent(new Event("change", { bubbles: true })); document.querySelector('#settingsForm button[type="submit"]').click(); })()`); await waitFor(async () => (await savedState()).settings.wsUrl === url, "the new WebSocket URL is saved"); await evaluate(`document.getElementById("settingsConnect").click()`); await waitFor(async () => evaluate('document.getElementById("connectionBadge")?.classList.contains("badge-online")'), "decoder online on the new port"); assert.equal(await evaluate(`document.querySelector('#settingsForm [name="wsUrl"]').value`), url, "the field still shows the new URL after the re-render"); console.log("PASS typing a new decoder WebSocket URL, Save and Connect now connects on that port"); // Typing a URL and pressing Connect now without Save: the typed URL is saved and used. const second = await startFakeDecoder(); try { const secondUrl = `ws://127.0.0.1:${second.port}`; await evaluate(`(() => { const input = document.querySelector('#settingsForm [name="wsUrl"]'); input.value = ${JSON.stringify(secondUrl)}; document.getElementById("settingsConnect").click(); })()`); await waitFor(async () => (await savedState()).settings.wsUrl === secondUrl, "the typed URL is saved by Connect now"); await waitFor(async () => second.totalConnections() > 0, "connected to the typed port"); console.log("PASS Connect now uses the URL in the field even without pressing Save"); } finally { await second.close(); } // A full browser storage (big installation: results, training ledger, logo) made every // saveState() throw QuotaExceededError - and Connect saves before it connects, so the // decoder never came online. The local copy is only a cache of the server state. const third = await startFakeDecoder(); try { const thirdUrl = `ws://127.0.0.1:${third.port}`; await evaluate(`(() => { window.__realSetItem = Storage.prototype.setItem; Storage.prototype.setItem = function (key, value) { if (key === "rc_timing_control_v1") throw new DOMException("Setting the value of '" + key + "' exceeded the quota.", "QuotaExceededError"); return window.__realSetItem.call(this, key, value); }; const input = document.querySelector('#settingsForm [name="wsUrl"]'); input.value = ${JSON.stringify(thirdUrl)}; document.getElementById("settingsConnect").click(); })()`); await waitFor(async () => third.totalConnections() > 0, "connects although the browser storage is full"); await waitFor(async () => (await savedState()).settings.wsUrl === thirdUrl, "and still saves to the server"); assert.equal(await evaluate(`localStorage.getItem("rc_timing_control_v1")`), null, "the stale local copy is dropped"); console.log("PASS a full browser storage no longer stops the decoder from connecting or saving to the server"); } finally { await evaluate(`window.__realSetItem && (Storage.prototype.setItem = window.__realSetItem)`); await third.close(); } } finally { const current = await savedState(); await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...current, settings: { ...current.settings, wsUrl: before.settings.wsUrl, autoReconnect: false } }) }); await decoder.close(); } }