Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

test(companion): cover the drain answer on a socket that is no longer open

biocherry1f4c15810c32
diff --git a/companion/test/e2e/phone.test.ts b/companion/test/e2e/phone.test.ts
new file mode 100644
index 0000000000..b6d88405ac
--- /dev/null
+++ b/companion/test/e2e/phone.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { webSocketTransport } from "../../src/e2e/phone.ts";
+
+/** Stands in for the built-in WebSocket client; only what the transport reads. */
+const fakeSocket = (readyState: number, bufferedAmount: number) =>
+ ({ readyState, bufferedAmount, send: () => {}, close: () => {} }) as unknown as WebSocket;
+
+describe("the built-in WebSocket transport", () => {
+ it("answers a drain wait on a later turn when the socket is no longer open", () => {
+ // A closing socket never drains. Answering synchronously here re-enters
+ // MuxStream.flush, which finds the session congested again and waits again, until the
+ // stack overflows; the close event that would end the session also never gets a turn.
+ vi.useFakeTimers();
+ try {
+ const transport = webSocketTransport(fakeSocket(WebSocket.CLOSING, 4 * 1024 * 1024));
+ const listener = vi.fn();
+
+ transport.onceDrained(listener);
+
+ expect(listener).not.toHaveBeenCalled();
+ vi.advanceTimersByTime(50);
+ expect(listener).toHaveBeenCalled();
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+});