fix(FN-730): add missing ping handler in useTerminal WebSocket hook

- Add ping/pong heartbeat handler to useTerminal WebSocket hook
- Add tests for WebSocket heartbeat ping/pong behavior
- Remove unused usage tracking module and its tests
- Simplify usage.ts by removing dead code
This commit is contained in:
gsxdsm
2026-04-02 19:12:24 -07:00
parent 30f6413e15
commit ddf06bb7cf
2 changed files with 58 additions and 0 deletions

View File

@@ -123,6 +123,59 @@ describe("useTerminal", () => {
unsubScrollback();
});
it("responds with pong when server sends ping", () => {
renderHook(() => useTerminal("test-session-123"));
const ws = MockWebSocket.instances[0];
act(() => {
ws.emitOpen();
});
act(() => {
ws.emitMessage({ type: "ping" });
});
const pongSent = ws.sent.find((m) => JSON.parse(m).type === "pong");
expect(pongSent).toBeDefined();
expect(JSON.parse(pongSent!)).toEqual({ type: "pong" });
});
it("does not send pong when websocket is not open", () => {
renderHook(() => useTerminal("test-session-123"));
const ws = MockWebSocket.instances[0];
act(() => {
ws.emitOpen();
});
// Simulate WS in CLOSING state
ws.readyState = MockWebSocket.CLOSING;
act(() => {
ws.emitMessage({ type: "ping" });
});
const pongMessages = ws.sent.filter((m) => JSON.parse(m).type === "pong");
expect(pongMessages).toHaveLength(0);
});
it("stays connected after receiving a ping", () => {
const { result } = renderHook(() => useTerminal("test-session-123"));
const ws = MockWebSocket.instances[0];
act(() => {
ws.emitOpen();
});
expect(result.current.connectionStatus).toBe("connected");
act(() => {
ws.emitMessage({ type: "ping" });
});
expect(result.current.connectionStatus).toBe("connected");
});
it("does not reconnect for terminal-not-found closes", () => {
const { result } = renderHook(() => useTerminal("test-session-123"));

View File

@@ -199,6 +199,11 @@ export function useTerminal(sessionId: string | null): UseTerminalReturn {
onExitCallbacksRef.current.forEach((cb) => cb(msg.exitCode!));
}
break;
case "ping":
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: "pong" }));
}
break;
case "pong":
// Heartbeat response
break;