File size: 5,414 Bytes
3201ca6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | import { renderHook } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { useBashCommandRunner } from "#/hooks/use-bash-command-runner";
class MockWebSocket {
static readonly CONNECTING = 0;
static readonly OPEN = 1;
static readonly CLOSING = 2;
static readonly CLOSED = 3;
static instance: MockWebSocket | null = null;
readonly url: string;
readonly sent: string[] = [];
readyState = MockWebSocket.CONNECTING;
onopen: (() => void) | null = null;
onmessage: ((event: MessageEvent) => void) | null = null;
onclose: (() => void) | null = null;
onerror: (() => void) | null = null;
constructor(url: string) {
this.url = url;
MockWebSocket.instance = this;
}
send(data: string) {
if (this.readyState !== MockWebSocket.OPEN) {
throw new DOMException("WebSocket is not open", "InvalidStateError");
}
this.sent.push(data);
}
open() {
this.readyState = MockWebSocket.OPEN;
this.onopen?.();
}
receive(data: unknown) {
this.onmessage?.(
new MessageEvent("message", { data: JSON.stringify(data) }),
);
}
close() {
this.readyState = MockWebSocket.CLOSED;
}
}
describe("useBashCommandRunner", () => {
afterEach(() => {
MockWebSocket.instance = null;
vi.unstubAllGlobals();
});
it("sends auth before queued commands without putting the key in the URL", async () => {
vi.stubGlobal("WebSocket", MockWebSocket);
const sessionApiKey = `sk-oh-${"b".repeat(64)}`;
const { result, unmount } = renderHook(() =>
useBashCommandRunner(
"https://runtime.example.com/api/conversations/conv-1",
sessionApiKey,
true,
),
);
const socket = MockWebSocket.instance!;
socket.readyState = MockWebSocket.OPEN;
const command = result.current("pwd", "/workspace", 30);
expect(socket.sent).toEqual([]);
socket.open();
expect(socket.url).not.toContain(sessionApiKey);
expect(socket.url).not.toContain("session_api_key");
expect(socket.sent).toEqual([
JSON.stringify({ type: "auth", session_api_key: sessionApiKey }),
JSON.stringify({ command: "pwd", cwd: "/workspace", timeout: 30 }),
]);
socket.receive({ kind: "BashCommand", id: "command-1" });
socket.receive({
kind: "BashOutput",
command_id: "command-1",
stdout: "/workspace\n",
stderr: "",
exit_code: 0,
});
await expect(command).resolves.toEqual({
exit_code: 0,
stdout: "/workspace\n",
stderr: "",
});
unmount();
});
it("sends queued commands without an auth frame when no key is configured", async () => {
vi.stubGlobal("WebSocket", MockWebSocket);
const { result, unmount } = renderHook(() =>
useBashCommandRunner(
"http://runtime.example.com/api/conversations/conv-1",
null,
true,
),
);
const socket = MockWebSocket.instance!;
const command = result.current("git status", "/workspace", 10);
expect(socket.sent).toEqual([]);
socket.open();
expect(socket.sent).toEqual([
JSON.stringify({
command: "git status",
cwd: "/workspace",
timeout: 10,
}),
]);
socket.receive({ kind: "BashCommand", id: "command-1" });
socket.receive({
kind: "BashOutput",
command_id: "command-1",
stdout: "",
stderr: "",
exit_code: 0,
});
await expect(command).resolves.toEqual({
exit_code: 0,
stdout: "",
stderr: "",
});
unmount();
});
it("closes a handshake stuck in CONNECTING at the timeout", () => {
// Arrange: the server never completes the 101 upgrade. Left alone, this
// socket would hold the browser's per-host handshake lock and block the
// conversation's events socket indefinitely.
vi.stubGlobal("WebSocket", MockWebSocket);
vi.useFakeTimers();
try {
const { unmount } = renderHook(() =>
useBashCommandRunner(
"http://runtime.example.com/api/conversations/conv-1",
null,
true,
),
);
const socket = MockWebSocket.instance!;
const closeSpy = vi.spyOn(socket, "close");
// Act/Assert: untouched just before the timeout, closed right at it.
vi.advanceTimersByTime(9_999);
expect(closeSpy).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(closeSpy).toHaveBeenCalledOnce();
expect(socket.readyState).toBe(MockWebSocket.CLOSED);
unmount();
} finally {
vi.useRealTimers();
}
});
it("does not close a socket that finished its handshake in time", () => {
vi.stubGlobal("WebSocket", MockWebSocket);
vi.useFakeTimers();
try {
const { unmount } = renderHook(() =>
useBashCommandRunner(
"http://runtime.example.com/api/conversations/conv-1",
null,
true,
),
);
const socket = MockWebSocket.instance!;
const closeSpy = vi.spyOn(socket, "close");
// Act: the handshake completes, then the watchdog window elapses.
socket.open();
vi.advanceTimersByTime(60_000);
// Assert: the cleared watchdog never touched the healthy socket.
expect(closeSpy).not.toHaveBeenCalled();
expect(socket.readyState).toBe(MockWebSocket.OPEN);
unmount();
} finally {
vi.useRealTimers();
}
});
});
|