62 lines
3.1 KiB
Markdown
62 lines
3.1 KiB
Markdown
---
|
|
id: websocket-server-tests
|
|
name: Write tests for WebSocket server event target adapter
|
|
status: completed
|
|
depends_on: [websocket-server-adapter, websocket-client-tests]
|
|
scope: moderate
|
|
risk: medium
|
|
impact: component
|
|
level: implementation
|
|
---
|
|
|
|
## Description
|
|
|
|
Write tests for `createWebSocketServerEventTarget`. This requires mocking multiple WebSocket connections and simulating the fan-out behavior.
|
|
|
|
Test scenarios from the architecture doc:
|
|
1. Topic-based fan-out — dispatchEvent sends only to connections subscribed to that event type
|
|
2. Subscription protocol — `__subscribe`/`__unsubscribe` control events correctly update the subscription map
|
|
3. Incoming aggregation — messages from any spoke dispatch to local listeners
|
|
4. Connection add/remove — new connections are tracked, disconnections clean up all subscriptions
|
|
5. Backpressure disconnect — slow consumers exceeding threshold are disconnected
|
|
6. Backpressure callback — `onBackpressure` is called before disconnecting
|
|
7. Direct messaging — events dispatched to `"direct:${spokeId}"` reach only the target spoke
|
|
8. Mixed topology — server adapter and client adapters can communicate bidirectionally
|
|
|
|
## Acceptance Criteria
|
|
|
|
- [ ] `test/event-target-websocket-server.test.ts` exists and passes
|
|
- [ ] Mock strategy for multiple WebSocket connections established
|
|
- [ ] Test: `addConnection`/`removeConnection` track connections correctly
|
|
- [ ] Test: `__subscribe` control event adds connection to topic's subscriber set
|
|
- [ ] Test: `__unsubscribe` control event removes connection from topic's subscriber set
|
|
- [ ] Test: `dispatchEvent` sends only to connections subscribed to that topic
|
|
- [ ] Test: connections NOT subscribed to a topic do NOT receive events for that topic
|
|
- [ ] Test: malformed JSON from spokes is silently ignored
|
|
- [ ] Test: invalid `__subscribe` with empty/malformed topic is ignored
|
|
- [ ] Test: backpressure threshold disconnect — connections exceeding `maxBufferedAmount` are closed with code 1013
|
|
- [ ] Test: `onBackpressure` callback fires before disconnect
|
|
- [ ] Test: `onConnection` callback receives spoke target and WebSocket
|
|
- [ ] Test: `onDisconnection` callback fires on connection close
|
|
- [ ] Test: `dispatchEvent` always returns `true`
|
|
- [ ] Test: send failure (ws.send throws) removes connection and fires `onDisconnection`
|
|
|
|
## References
|
|
|
|
- docs/architecture/event-targets/websocket-server.md (Test Plan section)
|
|
- src/event-target-websocket-server.ts
|
|
|
|
## Notes
|
|
|
|
Completed as part of the websocket-server-adapter task. 46 test cases covering all acceptance criteria.
|
|
|
|
## Summary
|
|
|
|
Tests were written alongside the adapter implementation. 46 tests pass, covering:
|
|
- Connection lifecycle (addConnection, removeConnection, automatic cleanup on close)
|
|
- Subscription protocol (__subscribe, __unsubscribe, idempotency, invalid topics)
|
|
- Topic-based fan-out (subscribed connections receive events, unsubscribed don't)
|
|
- Local listeners (addEventListener, removeEventListener, aggregation from spokes)
|
|
- Per-connection spoke targets (spoke.addEventListener, spoke.dispatchEvent)
|
|
- Error handling (malformed JSON, send failures, backpressure)
|
|
- Callbacks (onConnection, onDisconnection, onBackpressure) |