import { describe, expect, it } from 'bun:test';
import { EventEmitterWakePublisher } from '../src/event-stream/event-emitter-wake-publisher.js';
describe('EventEmitterWakePublisher', () => {
    it('should publish wakes to subscribers', () => {
        const publisher = new EventEmitterWakePublisher();
        const wakes = [];
        publisher.subscribe((wake) => { wakes.push(wake); });
        publisher.publish({ tenant: 'did:test:alice', seq: '1' });
        expect(wakes).toEqual([{ tenant: 'did:test:alice', seq: '1' }]);
    });
    it('should unsubscribe listeners', () => {
        const publisher = new EventEmitterWakePublisher();
        let calls = 0;
        const unsubscribe = publisher.subscribe(() => { calls++; });
        unsubscribe();
        publisher.publish({ tenant: 'did:test:alice', seq: '1' });
        expect(calls).toBe(0);
    });
    it('should clear listeners', () => {
        const publisher = new EventEmitterWakePublisher();
        let calls = 0;
        publisher.subscribe(() => { calls++; });
        publisher.clear();
        publisher.publish({ tenant: 'did:test:alice', seq: '1' });
        expect(calls).toBe(0);
    });
    it('should isolate listener failures', () => {
        const publisher = new EventEmitterWakePublisher();
        let calls = 0;
        publisher.subscribe(() => { throw new Error('listener failed'); });
        publisher.subscribe(() => { calls++; });
        expect(() => {
            publisher.publish({ tenant: 'did:test:alice', seq: '1' });
        }).not.toThrow();
        expect(calls).toBe(1);
    });
    it('should publish to the current listener snapshot', () => {
        const publisher = new EventEmitterWakePublisher();
        const calls = [];
        let unsubscribeSecond = () => { };
        publisher.subscribe(() => {
            calls.push('first');
            unsubscribeSecond();
        });
        unsubscribeSecond = publisher.subscribe(() => { calls.push('second'); });
        publisher.publish({ tenant: 'did:test:alice', seq: '1' });
        publisher.publish({ tenant: 'did:test:alice', seq: '2' });
        expect(calls).toEqual(['first', 'second', 'first']);
    });
});
//# sourceMappingURL=event-emitter-wake-publisher.spec.js.map