removed old tests, created new ones and format

This commit is contained in:
Boki 2025-06-25 07:46:59 -04:00
parent 7579afa3c3
commit b03231b849
57 changed files with 4092 additions and 5901 deletions

View file

@ -0,0 +1,150 @@
import type { EventHandler, EventSubscription } from './types';
/**
* Simple in-memory event bus for testing
*/
export class SimpleEventBus {
private subscriptions = new Map<string, Set<EventSubscription>>();
private subscriptionById = new Map<string, EventSubscription>();
private nextId = 1;
subscribe(event: string, handler: EventHandler): EventSubscription {
const subscription: EventSubscription = {
id: `sub-${this.nextId++}`,
event,
handler,
pattern: event.includes('*'),
};
if (!this.subscriptions.has(event)) {
this.subscriptions.set(event, new Set());
}
this.subscriptions.get(event)!.add(subscription);
this.subscriptionById.set(subscription.id, subscription);
return subscription;
}
unsubscribe(idOrSubscription: string | EventSubscription): boolean {
const id = typeof idOrSubscription === 'string' ? idOrSubscription : idOrSubscription.id;
const subscription = this.subscriptionById.get(id);
if (!subscription) {
return false;
}
const eventSubs = this.subscriptions.get(subscription.event);
if (eventSubs) {
eventSubs.delete(subscription);
if (eventSubs.size === 0) {
this.subscriptions.delete(subscription.event);
}
}
this.subscriptionById.delete(id);
return true;
}
async publish(event: string, data: any): Promise<void> {
const handlers: EventHandler[] = [];
// Direct matches
const directSubs = this.subscriptions.get(event);
if (directSubs) {
handlers.push(...Array.from(directSubs).map(s => s.handler));
}
// Pattern matches
for (const [pattern, subs] of this.subscriptions) {
if (pattern.includes('*') && this.matchPattern(pattern, event)) {
handlers.push(...Array.from(subs).map(s => s.handler));
}
}
// Execute all handlers
await Promise.all(
handlers.map(handler =>
handler(data, event).catch(err => {
// Silently catch errors
})
)
);
}
publishSync(event: string, data: any): void {
const handlers: EventHandler[] = [];
// Direct matches
const directSubs = this.subscriptions.get(event);
if (directSubs) {
handlers.push(...Array.from(directSubs).map(s => s.handler));
}
// Pattern matches
for (const [pattern, subs] of this.subscriptions) {
if (pattern.includes('*') && this.matchPattern(pattern, event)) {
handlers.push(...Array.from(subs).map(s => s.handler));
}
}
// Execute all handlers synchronously
handlers.forEach(handler => {
try {
handler(data, event);
} catch {
// Silently catch errors
}
});
}
once(event: string, handler: EventHandler): EventSubscription {
const wrappedHandler: EventHandler = async (data, evt) => {
await handler(data, evt);
this.unsubscribe(subscription.id);
};
const subscription = this.subscribe(event, wrappedHandler);
return subscription;
}
off(event: string, handler?: EventHandler): void {
if (!handler) {
// Remove all handlers for this event
const subs = this.subscriptions.get(event);
if (subs) {
for (const sub of subs) {
this.subscriptionById.delete(sub.id);
}
this.subscriptions.delete(event);
}
} else {
// Remove specific handler
const subs = this.subscriptions.get(event);
if (subs) {
const toRemove = Array.from(subs).filter(s => s.handler === handler);
toRemove.forEach(sub => {
subs.delete(sub);
this.subscriptionById.delete(sub.id);
});
if (subs.size === 0) {
this.subscriptions.delete(event);
}
}
}
}
hasSubscribers(event: string): boolean {
return this.subscriptions.has(event) && this.subscriptions.get(event)!.size > 0;
}
clear(): void {
this.subscriptions.clear();
this.subscriptionById.clear();
}
private matchPattern(pattern: string, event: string): boolean {
// Simple pattern matching: user.* matches user.created, user.updated, etc.
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
return regex.test(event);
}
}