Initial commit: AetherForge Linux (forge-mesh) v0.1.0-dev
Some checks failed
Test / test (push) Has been cancelled
Some checks failed
Test / test (push) Has been cancelled
This commit is contained in:
57
web/src/hooks/useFleetWebSocket.test.ts
Normal file
57
web/src/hooks/useFleetWebSocket.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it, beforeEach, afterEach, vi } from "vitest";
|
||||
import { renderHook, waitFor } from "@testing-library/react";
|
||||
import { useFleetWebSocket } from "./useFleetWebSocket";
|
||||
import { setStoredAuth } from "@/lib/auth";
|
||||
import {
|
||||
installMockWebSocket,
|
||||
MockWebSocket,
|
||||
uninstallMockWebSocket,
|
||||
} from "@/test/mockWebSocket";
|
||||
|
||||
describe("useFleetWebSocket", () => {
|
||||
beforeEach(() => {
|
||||
sessionStorage.clear();
|
||||
setStoredAuth({ username: "operator", password: "test-pass" });
|
||||
installMockWebSocket();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
uninstallMockWebSocket();
|
||||
});
|
||||
|
||||
it("connects and receives heartbeat updates", async () => {
|
||||
const onMessage = vi.fn();
|
||||
const { result } = renderHook(() =>
|
||||
useFleetWebSocket({ enabled: true, onMessage, reconnectIntervalMs: 60_000 }),
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.connectionState).toBe("connected");
|
||||
});
|
||||
|
||||
const ws = MockWebSocket.latest();
|
||||
expect(ws?.url).toContain("ticket=test-ws-ticket");
|
||||
|
||||
const heartbeat = {
|
||||
type: "heartbeat" as const,
|
||||
timestamp: new Date().toISOString(),
|
||||
payload: { online: 2 },
|
||||
};
|
||||
ws?.simulateMessage(heartbeat);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.lastMessage).toEqual(heartbeat);
|
||||
});
|
||||
expect(onMessage).toHaveBeenCalledWith(heartbeat);
|
||||
});
|
||||
|
||||
it("stays disconnected when unauthenticated", async () => {
|
||||
sessionStorage.clear();
|
||||
const { result } = renderHook(() => useFleetWebSocket({ enabled: true }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.connectionState).toBe("disconnected");
|
||||
});
|
||||
expect(MockWebSocket.instances).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user