fix(#338): Add tests verifying WebSocket timer cleanup on error

- Add test for clearTimeout when workspace membership query throws
- Add test for clearTimeout on successful connection
- Verify timer leak prevention in catch block

Refs #338

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-05 18:50:19 -06:00
parent a42f88d64c
commit a22fadae7e

View File

@@ -124,6 +124,52 @@ describe("WebSocketGateway", () => {
expect(mockClient.disconnect).toHaveBeenCalled();
});
it("should clear timeout when workspace membership query throws error", async () => {
const clearTimeoutSpy = vi.spyOn(global, "clearTimeout");
const mockSessionData = {
user: { id: "user-123", email: "test@example.com" },
session: { id: "session-123" },
};
vi.spyOn(authService, "verifySession").mockResolvedValue(mockSessionData);
vi.spyOn(prismaService.workspaceMember, "findFirst").mockRejectedValue(
new Error("Database connection failed")
);
await gateway.handleConnection(mockClient);
// Verify clearTimeout was called (timer cleanup on error)
expect(clearTimeoutSpy).toHaveBeenCalled();
expect(mockClient.disconnect).toHaveBeenCalled();
clearTimeoutSpy.mockRestore();
});
it("should clear timeout on successful connection", async () => {
const clearTimeoutSpy = vi.spyOn(global, "clearTimeout");
const mockSessionData = {
user: { id: "user-123", email: "test@example.com" },
session: { id: "session-123" },
};
vi.spyOn(authService, "verifySession").mockResolvedValue(mockSessionData);
vi.spyOn(prismaService.workspaceMember, "findFirst").mockResolvedValue({
userId: "user-123",
workspaceId: "workspace-456",
role: "MEMBER",
} as never);
await gateway.handleConnection(mockClient);
// Verify clearTimeout was called (timer cleanup on success)
expect(clearTimeoutSpy).toHaveBeenCalled();
expect(mockClient.disconnect).not.toHaveBeenCalled();
clearTimeoutSpy.mockRestore();
});
it("should have connection timeout mechanism in place", () => {
// This test verifies that the gateway has a CONNECTION_TIMEOUT_MS constant
// The actual timeout is tested indirectly through authentication failure tests