test(gateway): dedupe canvas ws connect assertions

This commit is contained in:
Peter Steinberger
2026-02-21 23:21:51 +00:00
parent 6471ff02dc
commit d325c01503

View File

@@ -59,6 +59,23 @@ async function expectWsRejected(
});
}
async function expectWsConnected(url: string): Promise<void> {
await new Promise<void>((resolve, reject) => {
const ws = new WebSocket(url);
const timer = setTimeout(() => reject(new Error("timeout")), WS_CONNECT_TIMEOUT_MS);
ws.once("open", () => {
clearTimeout(timer);
ws.terminate();
resolve();
});
ws.once("unexpected-response", (_req, res) => {
clearTimeout(timer);
reject(new Error(`unexpected response ${res.statusCode}`));
});
ws.once("error", reject);
});
}
function makeWsClient(params: {
connId: string;
clientIp: string;
@@ -243,20 +260,7 @@ describe("gateway canvas host auth", () => {
);
expect(scopedA2ui.status).toBe(200);
await new Promise<void>((resolve, reject) => {
const ws = new WebSocket(`ws://${host}:${listener.port}${activeWsPath}`);
const timer = setTimeout(() => reject(new Error("timeout")), WS_CONNECT_TIMEOUT_MS);
ws.once("open", () => {
clearTimeout(timer);
ws.terminate();
resolve();
});
ws.once("unexpected-response", (_req, res) => {
clearTimeout(timer);
reject(new Error(`unexpected response ${res.statusCode}`));
});
ws.once("error", reject);
});
await expectWsConnected(`ws://${host}:${listener.port}${activeWsPath}`);
clients.delete(activeNodeClient);
@@ -361,20 +365,7 @@ describe("gateway canvas host auth", () => {
const scopedCanvas = await fetch(`http://[::1]:${listener.port}${canvasPath}`);
expect(scopedCanvas.status).toBe(200);
await new Promise<void>((resolve, reject) => {
const ws = new WebSocket(`ws://[::1]:${listener.port}${wsPath}`);
const timer = setTimeout(() => reject(new Error("timeout")), WS_CONNECT_TIMEOUT_MS);
ws.once("open", () => {
clearTimeout(timer);
ws.terminate();
resolve();
});
ws.once("unexpected-response", (_req, res) => {
clearTimeout(timer);
reject(new Error(`unexpected response ${res.statusCode}`));
});
ws.once("error", reject);
});
await expectWsConnected(`ws://[::1]:${listener.port}${wsPath}`);
},
});
} catch (err) {