--- id: core-pubsub-tests name: Write tests for createPubSub, EventEnvelope, and in-process event target status: completed depends_on: [] scope: moderate risk: low impact: component level: implementation --- ## Description The core `createPubSub` factory, `EventEnvelope` type, and the in-process (default `EventTarget`) path have no tests. These are the foundation of the entire package — every adapter builds on `createPubSub`. Write comprehensive tests that verify the core contract. The architecture specifies these behaviors: - `createPubSub()` with no config uses `new EventTarget()` (in-process) - `createPubSub({ eventTarget })` uses a custom event target - `publish(type, id, payload)` dispatches a `CustomEvent` with type `"type:id"` and `detail` as `EventEnvelope` - `subscribe(type, id)` returns a `Repeater` (async iterable) - `publish` throws on event types starting with `__` (reserved for adapter control) - Topic scoping uses the `type:id` convention - `subscribe` cleanup: breaking out of the `for await` loop removes the listener ## Acceptance Criteria - [ ] `test/create_pubsub.test.ts` exists and passes - [ ] Test: `publish` dispatches event with correct `type:id` topic - [ ] Test: `publish` throws on `__`-prefixed event types - [ ] Test: `subscribe` returns async iterable that yields `EventEnvelope` objects - [ ] Test: `subscribe` envelope has correct `type`, `id`, `payload` fields - [ ] Test: subscriber receives events only for the subscribed topic (type:id matching) - [ ] Test: multiple subscribers on the same topic all receive events - [ ] Test: subscriber cleanup — breaking out of `for await` loop removes the listener - [ ] Test: `createPubSub` with custom `eventTarget` dispatches to that target - [ ] Test: `createPubSub` without `eventTarget` uses `new EventTarget()` (in-process) ## References - docs/architecture/api-surface.md - src/create_pubsub.ts - src/types.ts ## Notes Used subscribe-based testing approach since `createPubSub` doesn't expose the internal `target`. Custom eventTarget tests use `vi.spyOn` on the provided target's `dispatchEvent` method. ## Summary Implemented comprehensive tests for createPubSub, EventEnvelope, and in-process event target. - Created: test/create_pubsub.test.ts - Tests: 11, all passing - Coverage: publish dispatches correct type:id topic, publish throws on __-prefixed types, subscribe yields EventEnvelope objects, envelope has correct type/id/payload, topic scoping (type:id matching), multiple subscribers receive events, subscriber cleanup on break, custom eventTarget dispatches to provided target, default in-process EventTarget works