ClawWork icon indicating copy to clipboard operation
ClawWork copied to clipboard

[Enhancement] Add unit tests for GatewayClient reconnect and backoff logic

Open samzong opened this issue 8 months ago • 2 comments

What problem are you trying to solve?

GatewayClient manages the WebSocket connection lifecycle including exponential backoff reconnection, challenge/response handshake, a 15-second pending request timeout, and close-rejects-all-pending behavior. None of this is unit tested. The existing tests only mock the client at a high level. A regression in reconnect logic would break the entire app.

Where

  • Source: packages/desktop/src/main/ws/gateway-client.ts
  • Key functions: scheduleReconnect() (exponential backoff), pending request timeout (REQ_TIMEOUT_MS = 15_000), and the close handler that rejects all pending requests

What needs to be done

  1. Create packages/desktop/test/gateway-client.test.ts
  2. Extract the backoff calculation into a pure function and test it:
    • Verify exponential growth: attempt 1 → base delay, attempt 2 → 2x, etc.
    • Verify it caps at a maximum delay
  3. Test pending request timeout:
    • A request that does not receive a response within 15s should reject
    • Use vi.useFakeTimers() to advance time
  4. Test close-rejects-all-pending:
    • When the connection closes, all pending requests should reject with a connection error
  5. Consider using mock-socket or a lightweight WS test harness for full lifecycle tests
  6. Run pnpm check to verify

Why does this matter?

The WebSocket connection is the only transport between the app and the gateway. If reconnect logic breaks, the app is dead. Tests here are a safety net for the most critical transport layer.

Primary area

Settings or gateway connection

samzong avatar Apr 01 '26 15:04 samzong

Hi @samzong, Thanks for opening an issue! We will look into it as soon as possible.

Details

Instructions for interacting with me using comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the gh-ci-bot repository.

github-actions[bot] avatar Apr 01 '26 15:04 github-actions[bot]

Hi, I’d like to take this on. I’ll keep the change focused on the GatewayClient reconnect/backoff test coverage described here, including the timeout and pending-request cleanup paths, and I’ll run the full pnpm check verification before opening the PR.

bferanmi806-sketch avatar Aug 05 '26 12:08 bferanmi806-sketch