feat(core-testing): RecordingEventBus
Adds RecordingEventBus implementing IEventBus for use in unit tests. Validates payloads via the descriptor schema, records all publish calls, and delivers events to subscribed handlers synchronously in subscription order. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import type { z } from "zod";
|
||||
import type { EventDescriptor, EventHandler, IEventBus } from "@repo/core-events";
|
||||
|
||||
export class RecordingEventBus implements IEventBus {
|
||||
readonly published: { name: string; payload: unknown }[] = [];
|
||||
private readonly handlers = new Map<string, EventHandler<unknown>[]>();
|
||||
|
||||
async publish<T>(
|
||||
descriptor: EventDescriptor<string, z.ZodType<T>>,
|
||||
payload: T,
|
||||
): Promise<void> {
|
||||
descriptor.schema.parse(payload);
|
||||
this.published.push({ name: descriptor.name, payload });
|
||||
for (const h of this.handlers.get(descriptor.name) ?? []) await h(payload);
|
||||
}
|
||||
|
||||
subscribe<T>(
|
||||
descriptor: EventDescriptor<string, z.ZodType<T>>,
|
||||
_consumerFeature: string,
|
||||
handler: EventHandler<T>,
|
||||
): void {
|
||||
const arr = this.handlers.get(descriptor.name) ?? [];
|
||||
arr.push(handler as EventHandler<unknown>);
|
||||
this.handlers.set(descriptor.name, arr);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user