fixed build libs

This commit is contained in:
Boki 2025-06-25 08:29:53 -04:00
parent b03231b849
commit 42baadae38
26 changed files with 981 additions and 541 deletions

View file

@ -1,206 +0,0 @@
import { beforeEach, describe, expect, it, mock } from 'bun:test';
import { SimpleEventBus } from './simple-event-bus';
import type { EventHandler, EventSubscription } from './types';
describe('EventBus', () => {
let eventBus: SimpleEventBus;
beforeEach(() => {
eventBus = new SimpleEventBus();
});
describe('subscribe', () => {
it('should subscribe to events', () => {
const handler: EventHandler = mock(async () => {});
const subscription = eventBus.subscribe('test-event', handler);
expect(subscription).toBeDefined();
expect(subscription.id).toBeDefined();
expect(subscription.event).toBe('test-event');
});
it('should allow multiple subscribers to same event', () => {
const handler1 = mock(async () => {});
const handler2 = mock(async () => {});
const sub1 = eventBus.subscribe('event', handler1);
const sub2 = eventBus.subscribe('event', handler2);
expect(sub1.id).not.toBe(sub2.id);
});
it('should support pattern subscriptions', () => {
const handler = mock(async () => {});
const subscription = eventBus.subscribe('user.*', handler);
expect(subscription.event).toBe('user.*');
});
});
describe('unsubscribe', () => {
it('should unsubscribe by subscription object', () => {
const handler = mock(async () => {});
const subscription = eventBus.subscribe('event', handler);
const result = eventBus.unsubscribe(subscription);
expect(result).toBe(true);
});
it('should unsubscribe by id', () => {
const handler = mock(async () => {});
const subscription = eventBus.subscribe('event', handler);
const result = eventBus.unsubscribe(subscription.id);
expect(result).toBe(true);
});
it('should return false for non-existent subscription', () => {
const result = eventBus.unsubscribe('non-existent-id');
expect(result).toBe(false);
});
});
describe('publish', () => {
it('should publish events to subscribers', async () => {
const handler = mock(async (data: any) => {});
eventBus.subscribe('test-event', handler);
await eventBus.publish('test-event', { message: 'hello' });
expect(handler).toHaveBeenCalledWith({ message: 'hello' }, 'test-event');
});
it('should publish to multiple subscribers', async () => {
const handler1 = mock(async () => {});
const handler2 = mock(async () => {});
eventBus.subscribe('event', handler1);
eventBus.subscribe('event', handler2);
await eventBus.publish('event', { data: 'test' });
expect(handler1).toHaveBeenCalledWith({ data: 'test' }, 'event');
expect(handler2).toHaveBeenCalledWith({ data: 'test' }, 'event');
});
it('should match pattern subscriptions', async () => {
const handler = mock(async () => {});
eventBus.subscribe('user.*', handler);
await eventBus.publish('user.created', { id: 1 });
await eventBus.publish('user.updated', { id: 2 });
await eventBus.publish('order.created', { id: 3 });
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenCalledWith({ id: 1 }, 'user.created');
expect(handler).toHaveBeenCalledWith({ id: 2 }, 'user.updated');
});
it('should handle errors in handlers gracefully', async () => {
const errorHandler = mock(async () => {
throw new Error('Handler error');
});
const successHandler = mock(async () => {});
eventBus.subscribe('event', errorHandler);
eventBus.subscribe('event', successHandler);
await eventBus.publish('event', {});
expect(successHandler).toHaveBeenCalled();
});
});
describe('publishSync', () => {
it('should publish synchronously', () => {
const results: any[] = [];
const handler = (data: any) => {
results.push(data);
};
eventBus.subscribe('sync-event', handler as any);
eventBus.publishSync('sync-event', { value: 42 });
expect(results).toEqual([{ value: 42 }]);
});
});
describe('once', () => {
it('should subscribe for single event', async () => {
const handler = mock(async () => {});
eventBus.once('once-event', handler);
await eventBus.publish('once-event', { first: true });
await eventBus.publish('once-event', { second: true });
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith({ first: true }, 'once-event');
});
});
describe('off', () => {
it('should remove all handlers for event', async () => {
const handler1 = mock(async () => {});
const handler2 = mock(async () => {});
eventBus.subscribe('event', handler1);
eventBus.subscribe('event', handler2);
eventBus.off('event');
await eventBus.publish('event', {});
expect(handler1).not.toHaveBeenCalled();
expect(handler2).not.toHaveBeenCalled();
});
it('should remove specific handler', async () => {
const handler1 = mock(async () => {});
const handler2 = mock(async () => {});
eventBus.subscribe('event', handler1);
eventBus.subscribe('event', handler2);
eventBus.off('event', handler1);
await eventBus.publish('event', {});
expect(handler1).not.toHaveBeenCalled();
expect(handler2).toHaveBeenCalled();
});
});
describe('hasSubscribers', () => {
it('should check for subscribers', () => {
expect(eventBus.hasSubscribers('event')).toBe(false);
const sub = eventBus.subscribe('event', async () => {});
expect(eventBus.hasSubscribers('event')).toBe(true);
eventBus.unsubscribe(sub);
expect(eventBus.hasSubscribers('event')).toBe(false);
});
});
describe('clear', () => {
it('should clear all subscriptions', async () => {
const handler = mock(async () => {});
eventBus.subscribe('event1', handler);
eventBus.subscribe('event2', handler);
eventBus.clear();
await eventBus.publish('event1', {});
await eventBus.publish('event2', {});
expect(handler).not.toHaveBeenCalled();
});
});
});