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(); } } 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(); } }