import { describe, it, expect, vi } from 'vitest';
import { EventBus } from '@domain/events/EventBus';

describe('EventBus', () => {
  it('dispatches events to subscribers', () => {
    const bus = new EventBus();
    const handler = vi.fn();
    bus.on('timer-completed', handler);
    bus.emit({ type: 'timer-completed', enclosId: 1 });
    expect(handler).toHaveBeenCalledWith({ type: 'timer-completed', enclosId: 1 });
  });

  it('ignores unsubscribed event types', () => {
    const bus = new EventBus();
    const handler = vi.fn();
    bus.on('timer-completed', handler);
    bus.emit({ type: 'accouplement-registered' });
    expect(handler).not.toHaveBeenCalled();
  });

  it('supports multiple handlers for same event', () => {
    const bus = new EventBus();
    const h1 = vi.fn();
    const h2 = vi.fn();
    bus.on('timer-completed', h1);
    bus.on('timer-completed', h2);
    bus.emit({ type: 'timer-completed' });
    expect(h1).toHaveBeenCalled();
    expect(h2).toHaveBeenCalled();
  });

  it('off removes handler', () => {
    const bus = new EventBus();
    const handler = vi.fn();
    bus.on('timer-completed', handler);
    bus.off('timer-completed', handler);
    bus.emit({ type: 'timer-completed' });
    expect(handler).not.toHaveBeenCalled();
  });

  it('emit with no handlers does not throw', () => {
    const bus = new EventBus();
    expect(() => bus.emit({ type: 'timer-completed' })).not.toThrow();
  });
});
