Cover renderer stale-error isolation and generic error fallback

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 07:32:54 +00:00
1 parent 9598488fe9
commit 124cd5ea0d
1 file changed
+65
+65
View File
@@ -69,6 +69,10 @@ class MockWebSocket {
this.emit("error", { message });
}
emitErrorEvent(event: unknown) {
this.emit("error", event);
}
emitMessage(data: unknown) {
this.emit("message", { data });
}
@@ -442,6 +446,19 @@ describe("wsNativeApi", () => {
await expect(request).rejects.toThrow("websocket errored (forced-socket-error)");
});
it("falls back to generic message when websocket errors without message", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4473");
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
const api = getOrCreateWsNativeApi();
const request = api.todos.list();
const socket = MockWebSocket.instances[0];
await waitForCondition(() => (socket?.sentMessages.length ?? 0) > 0);
socket?.emitErrorEvent({});
await expect(request).rejects.toThrow("websocket errored.");
});
it("includes close reason details when pending request disconnects", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4450");
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
@@ -834,6 +851,54 @@ describe("wsNativeApi", () => {
await expect(secondRequest).resolves.toEqual([]);
});
it("ignores stale error events from prior sockets after reconnect", async () => {
setWindowSearch("?ws=ws%3A%2F%2F127.0.0.1%3A4474");
const { getOrCreateWsNativeApi } = await import("./wsNativeApi");
const api = getOrCreateWsNativeApi();
const firstRequest = api.todos.list();
const firstSocket = await waitForSocket();
await waitForCondition(() => firstSocket.sentMessages.length > 0);
const firstEnvelope = JSON.parse(firstSocket.sentMessages[0] ?? "{}") as {
id: string;
};
firstSocket.emitMessage(
JSON.stringify({
type: "response",
id: firstEnvelope.id,
ok: true,
result: [],
}),
);
await expect(firstRequest).resolves.toEqual([]);
firstSocket.closeWith({
code: WS_CLOSE_CODES.unauthorized,
reason: WS_CLOSE_REASONS.unauthorized,
});
const secondRequest = api.todos.list();
await waitForCondition(() => MockWebSocket.instances.length >= 2);
const secondSocket = MockWebSocket.instances[1];
await waitForCondition(() => (secondSocket?.sentMessages.length ?? 0) > 0);
const secondEnvelope = JSON.parse(secondSocket?.sentMessages[0] ?? "{}") as {
id: string;
};
firstSocket.emitError("stale-socket-error");
secondSocket?.emitMessage(
JSON.stringify({
type: "response",
id: secondEnvelope.id,
ok: true,
result: [],
}),
);
await expect(secondRequest).resolves.toEqual([]);
});
it("rejects requests when runtime does not respond before timeout", async () => {
vi.useFakeTimers();
try {