import { describe, it, expect, beforeEach, vi } from 'vitest';
import { __resetPreviewEventBus, notifyPreviewUpdate, previewSubscriberCount, subscribePreviewUpdates, } from '../preview/event-bus.js';
beforeEach(() => {
    __resetPreviewEventBus();
});
describe('previewSubscriberCount', () => {
    it('returns 0 for a key with no subscribers', () => {
        expect(previewSubscriberCount('posts', 'doc-1')).toBe(0);
    });
    it('returns the number of subscribers for that doc', () => {
        subscribePreviewUpdates('posts', 'doc-1', () => undefined);
        subscribePreviewUpdates('posts', 'doc-1', () => undefined);
        subscribePreviewUpdates('posts', 'doc-2', () => undefined);
        expect(previewSubscriberCount('posts', 'doc-1')).toBe(2);
        expect(previewSubscriberCount('posts', 'doc-2')).toBe(1);
    });
});
describe('subscribePreviewUpdates + notifyPreviewUpdate', () => {
    it('delivers a payload to a subscriber', () => {
        const listener = vi.fn();
        subscribePreviewUpdates('posts', 'doc-1', listener);
        notifyPreviewUpdate('posts', 'doc-1', { title: 'Hello' });
        expect(listener).toHaveBeenCalledTimes(1);
        expect(listener.mock.calls[0][0]).toMatchObject({
            collection: 'posts',
            documentId: 'doc-1',
            payload: { title: 'Hello' },
        });
        expect(listener.mock.calls[0][0].at).toBeTypeOf('number');
    });
    it('delivers to every subscriber for the same doc', () => {
        const a = vi.fn();
        const b = vi.fn();
        subscribePreviewUpdates('posts', 'doc-1', a);
        subscribePreviewUpdates('posts', 'doc-1', b);
        notifyPreviewUpdate('posts', 'doc-1', { v: 1 });
        expect(a).toHaveBeenCalledTimes(1);
        expect(b).toHaveBeenCalledTimes(1);
    });
    it('does not deliver to subscribers on a different doc', () => {
        const a = vi.fn();
        const b = vi.fn();
        subscribePreviewUpdates('posts', 'doc-1', a);
        subscribePreviewUpdates('posts', 'doc-2', b);
        notifyPreviewUpdate('posts', 'doc-1', { v: 1 });
        expect(a).toHaveBeenCalledTimes(1);
        expect(b).not.toHaveBeenCalled();
    });
    it('does not deliver across collections', () => {
        const a = vi.fn();
        const b = vi.fn();
        subscribePreviewUpdates('posts', 'doc-1', a);
        subscribePreviewUpdates('pages', 'doc-1', b);
        notifyPreviewUpdate('posts', 'doc-1', { v: 1 });
        expect(a).toHaveBeenCalledTimes(1);
        expect(b).not.toHaveBeenCalled();
    });
    it('removes a subscriber via the returned unsubscribe handle', () => {
        const listener = vi.fn();
        const unsubscribe = subscribePreviewUpdates('posts', 'doc-1', listener);
        notifyPreviewUpdate('posts', 'doc-1', { v: 1 });
        unsubscribe();
        notifyPreviewUpdate('posts', 'doc-1', { v: 2 });
        expect(listener).toHaveBeenCalledTimes(1);
        expect(previewSubscriberCount('posts', 'doc-1')).toBe(0);
    });
    it('isolates throwing listeners — siblings still get called', () => {
        const error = new Error('boom');
        const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined);
        const ok = vi.fn();
        subscribePreviewUpdates('posts', 'doc-1', () => {
            throw error;
        });
        subscribePreviewUpdates('posts', 'doc-1', ok);
        expect(() => notifyPreviewUpdate('posts', 'doc-1', { v: 1 })).not.toThrow();
        expect(ok).toHaveBeenCalledTimes(1);
        consoleError.mockRestore();
    });
    it('skips notify when there are no subscribers', () => {
        expect(() => notifyPreviewUpdate('posts', 'doc-1', { v: 1 })).not.toThrow();
    });
    it('is safe to unsubscribe twice', () => {
        const unsubscribe = subscribePreviewUpdates('posts', 'doc-1', () => undefined);
        unsubscribe();
        expect(() => unsubscribe()).not.toThrow();
        expect(previewSubscriberCount('posts', 'doc-1')).toBe(0);
    });
});
//# sourceMappingURL=preview-event-bus.test.js.map