Files
pubsub/tasks/007-websocket-client-tests.md
glm-5.1 1306716897 Decompose architecture into atomic, dependency-ordered tasks
19 tasks covering core testing, Redis hardening, WebSocket client/server
adapters, Worker adapter, and final review gates. Iroh adapters are tracked
as a deferred placeholder blocked on the @alkdev/iroh fork.

Phases: core validation → Redis hardening → review gate → WebSocket
adapters → review gate → Worker adapter → review gate → final validation.
2026-05-08 05:50:43 +00:00

52 lines
2.3 KiB
Markdown

---
id: websocket-client-tests
name: Write tests for WebSocket client event target adapter
status: pending
depends_on: [websocket-client-adapter]
scope: moderate
risk: medium
impact: component
level: implementation
---
## Description
Write tests for `createWebSocketClientEventTarget`. Since WebSocket is a browser API, we need to mock `WebSocket` in tests. Use a lightweight mock or `vitest` mocking to simulate WebSocket behavior.
Test scenarios from the architecture doc:
1. Send path — dispatchEvent serializes envelope and calls ws.send
2. Receive path — ws.onmessage parses envelope, creates CustomEvent, dispatches to listeners
3. Topic scoping — type:id topics correctly formed from envelope
4. Subscription forwarding — addEventListener sends __subscribe on first listener for a topic
5. Subscription dedup — multiple addEventListener for the same topic sends only one __subscribe
6. Unsubscription forwarding — removeEventListener sends __unsubscribe when no listeners remain
7. Connection close — ws.onclose behavior
8. Reconnection — new connection + new event target + re-subscribe restores subscriptions
9. Multiple listeners — multiple addEventListener on same topic receives events correctly
## Acceptance Criteria
- [ ] `test/event-target-websocket-client.test.ts` exists and passes
- [ ] Mock strategy for WebSocket is established (vitest mock, mock WebSocket class, or similar)
- [ ] Test: dispatchEvent sends JSON-stringified envelope via ws.send
- [ ] Test: addEventListener with first listener for a topic sends `__subscribe` control event
- [ ] Test: multiple addEventListener for same topic sends only one `__subscribe`
- [ ] Test: removeEventListener with last listener sends `__unsubscribe`
- [ ] Test: removeEventListener with remaining listeners does NOT send `__unsubscribe`
- [ ] Test: malformed JSON from server is silently ignored
- [ ] Test: control events received from server are silently ignored
- [ ] Test: ws.onmessage correctly dispatches to local listeners with type:id topic
- [ ] Test: multiple listeners on same topic all receive events
## References
- docs/architecture/event-targets/websocket-client.md (Test Plan section)
- src/event-target-websocket-client.ts
## Notes
> To be filled by implementation agent
## Summary
> To be filled on completion