import { describe, it, expect, vi } from 'vitest';
import { ProviderHolder } from '@main/ai/ProviderHolder.js';
import { LocalOllamaProvider } from '@main/ai/LocalOllamaProvider.js';

describe('ProviderHolder', () => {
  it('replace() fires listener and get() returns new instance', () => {
    const a = new LocalOllamaProvider({ endpoint: 'http://a:11434', model: 'm1' });
    const b = new LocalOllamaProvider({ endpoint: 'http://b:11434', model: 'm2' });
    const holder = new ProviderHolder(a);
    const listener = vi.fn();
    holder.onReplace(listener);
    expect(holder.get()).toBe(a);
    holder.replace(b);
    expect(holder.get()).toBe(b);
    expect(listener).toHaveBeenCalledWith(b);
  });

  it('multiple listeners all fire on replace()', () => {
    const a = new LocalOllamaProvider({ model: 'm1' });
    const b = new LocalOllamaProvider({ model: 'm2' });
    const holder = new ProviderHolder(a);
    const l1 = vi.fn();
    const l2 = vi.fn();
    holder.onReplace(l1);
    holder.onReplace(l2);
    holder.replace(b);
    expect(l1).toHaveBeenCalledWith(b);
    expect(l2).toHaveBeenCalledWith(b);
  });
});
