/** * A stand-in gateway for scripts/e2e-alerts.sh: connects to /ws/edge/v1 with * an edge JWT, says hello, then reports one printer through a sequence of * states, one telemetry frame each, and waits for the backend to take them. * * Usage: bun scripts/fake-gateway.ts STATE[,STATE...] * A state is a Bambu gcode_state word (IDLE, RUNNING, PAUSE, FAILED). */ const [url, jwt, printerId, states] = process.argv.slice(2); if (!url || !jwt || !printerId || !states) { console.error("usage: fake-gateway.ts STATE[,STATE...]"); process.exit(2); } const ws = new WebSocket(url, { headers: { authorization: `Bearer ${jwt}` } } as never); ws.onerror = () => { console.error("socket error"); process.exit(1); }; ws.onopen = async () => { ws.send(JSON.stringify({ type: "hello", gateway_id: "fake-gw" })); for (const state of states.split(",")) { ws.send( JSON.stringify({ type: "telemetry", printers: [{ printer_id: printerId, name: "Fake", state, percent: 10, print_error: 0 }], }), ); // Telemetry is persisted in order per socket; a pause keeps the frames // from being coalesced into one observation. await Bun.sleep(400); } await Bun.sleep(500); ws.close(); process.exit(0); };