import { AdapterRegistry } from '../../../../../src/modules/desktop_op/runtime/adapters/registry.js';
import type { AppAdapter } from '../../../../../src/modules/desktop_op/runtime/adapters/adapter.js';

function makeAdapter(appId: string): AppAdapter {
  return {
    appId,
    supportedActions: ['send-text'],
    async findWindow() { return null; },
    async preFlightCheck() {},
    async buildSteps() { return []; },
    async verifyResult() { return true; },
    queueKey() { return 'k'; },
  };
}

describe('AdapterRegistry', () => {
  let reg: AdapterRegistry;
  beforeEach(() => { reg = new AdapterRegistry(); });

  it('register + get round-trips an adapter', () => {
    reg.register(makeAdapter('weixin'));
    const a = reg.get('weixin');
    expect(a.appId).toBe('weixin');
  });

  it('get throws app-not-allowed for unknown appId', () => {
    expect(() => reg.get('cmd')).toThrow('app-not-allowed: cmd');
  });

  it('register throws on duplicate appId', () => {
    reg.register(makeAdapter('weixin'));
    expect(() => reg.register(makeAdapter('weixin'))).toThrow('adapter-already-registered: weixin');
  });

  it('register throws when appId is empty', () => {
    const bad = { ...makeAdapter('weixin'), appId: '' };
    expect(() => reg.register(bad as any)).toThrow('adapter-missing-appId');
  });

  it('listAppIds returns all registered appIds', () => {
    reg.register(makeAdapter('weixin'));
    reg.register(makeAdapter('excel'));
    expect(reg.listAppIds().sort()).toEqual(['excel', 'weixin']);
  });

  it('clear empties the registry', () => {
    reg.register(makeAdapter('weixin'));
    reg.clear();
    expect(reg.listAppIds()).toEqual([]);
  });
});
