import { describe, it, expect, beforeEach, vi } from 'vitest';
import { setActiveEditor, getActiveEditor, clearActiveEditorIf, subscribeActiveEditor, } from '../../lib/active-editor.js';
function fakeEditor() {
    return {};
}
beforeEach(() => {
    setActiveEditor(null);
});
describe('active-editor registry', () => {
    it('stores and returns the active editor', () => {
        const a = fakeEditor();
        expect(getActiveEditor()).toBeNull();
        setActiveEditor(a);
        expect(getActiveEditor()).toBe(a);
    });
    it('replaces the active editor', () => {
        const a = fakeEditor();
        const b = fakeEditor();
        setActiveEditor(a);
        setActiveEditor(b);
        expect(getActiveEditor()).toBe(b);
    });
    it('accepts null to clear the active editor', () => {
        setActiveEditor(fakeEditor());
        setActiveEditor(null);
        expect(getActiveEditor()).toBeNull();
    });
    it('notifies subscribers when the active editor changes', () => {
        const listener = vi.fn();
        const unsubscribe = subscribeActiveEditor(listener);
        setActiveEditor(fakeEditor());
        expect(listener).toHaveBeenCalledTimes(1);
        setActiveEditor(fakeEditor());
        expect(listener).toHaveBeenCalledTimes(2);
        unsubscribe();
        setActiveEditor(fakeEditor());
        expect(listener).toHaveBeenCalledTimes(2);
    });
    it('skips notifications when the same editor is set twice', () => {
        const listener = vi.fn();
        const a = fakeEditor();
        subscribeActiveEditor(listener);
        setActiveEditor(a);
        setActiveEditor(a);
        expect(listener).toHaveBeenCalledTimes(1);
    });
    it('clearActiveEditorIf clears only when matched', () => {
        const a = fakeEditor();
        const b = fakeEditor();
        setActiveEditor(a);
        clearActiveEditorIf(b);
        expect(getActiveEditor()).toBe(a);
        clearActiveEditorIf(a);
        expect(getActiveEditor()).toBeNull();
    });
});
//# sourceMappingURL=active-editor.test.js.map