/* * Exercises the admin-server WebSocket path that the UI uses, which the plain * HTTP smoke test never touches. * * mockttp's admin server wraps each admin client's connection with * Ws.createWebSocketStream(). Under bun's built-in `ws` shim that throws * "Not supported yet in Bun" and kills the whole server the moment the UI opens a * session - so a binary can pass every HTTP check and still die on first real use. * * Run with node (this is a *client*; the binary under test is the server). * Usage: node ws-check.js [adminServerUrl] */ const mockttp = require('mockttp'); const adminServerUrl = process.argv[2] || 'http://127.0.0.1:45456'; // The admin server runs corsOptions.strict, so it requires an Origin from // MOCKTTP_ALLOWED_ORIGINS - the same one the browser UI sends. const origin = process.argv[3] || 'http://localhost:8099'; (async () => { const server = mockttp.getRemote({ adminServerUrl, client: { headers: { origin } } }); // start() opens the admin WebSocket + subscription stream. This is the call // that used to crash the server process. await server.start(); console.log(` admin session started, mock server on port ${server.port}`); await server.forGet('/ws-check').thenReply(200, 'ws-check-ok'); const res = await fetch(`http://127.0.0.1:${server.port}/ws-check`); const body = await res.text(); if (body !== 'ws-check-ok') { throw new Error(`unexpected proxied response: ${JSON.stringify(body)}`); } console.log(' proxied request through the mock server OK'); // Confirm the subscription stream is live, not just connected. Subscribing is // async on a remote client, so settle before generating traffic or we race it. const seen = new Promise((resolve) => server.on('request', (r) => resolve(r.path))); await new Promise((r) => setTimeout(r, 1000)); await fetch(`http://127.0.0.1:${server.port}/ws-check`); const path = await Promise.race([ seen, new Promise((_, rej) => setTimeout(() => rej(new Error('no event over subscription stream')), 10000)) ]); console.log(` received '${path}' event over the subscription stream`); await server.stop(); console.log(' admin session stopped cleanly'); })().catch((e) => { console.error('!! ws-check FAILED:', e.message); process.exit(1); });