import { describe, expect, it } from 'vitest';

import { connectorChannelForKind, runtimeBridgeEnabledForPath, visibleConnectorCards } from '@/lib/channel-connector-state';
import type { ChannelConnectorDescriptor, ChannelStatus } from '@/types';


function channel(overrides: Partial<ChannelStatus>): ChannelStatus {
  return {
    channel_id: 'weixin-main',
    kind: 'weixin',
    mode: 'polling',
    account_id: 'wx-main',
    display_name: 'Weixin Main',
    enabled: false,
    state: 'disabled',
    capabilities: [],
    connected_peers: 0,
    ...overrides,
  };
}


describe('connector channel cards', () => {
  it('does not let a disabled static channel block connector onboarding', () => {
    expect(connectorChannelForKind('weixin', [channel({})])).toBeUndefined();
  });

  it('selects a running connector channel', () => {
    const running = channel({ enabled: true, state: 'running' });

    expect(connectorChannelForKind('weixin', [running])).toEqual(running);
  });

  it('selects a running terminal channel', () => {
    const running = channel({
      channel_id: 'terminal-dev',
      kind: 'terminal',
      mode: 'websocket',
      display_name: 'Terminal Dev',
      enabled: true,
      state: 'running',
      connected_peers: 1,
    });

    expect(connectorChannelForKind('terminal', [running])).toEqual(running);
  });

  it('always includes terminal as a local connector card fallback', () => {
    const connectors: ChannelConnectorDescriptor[] = [{ kind: 'weixin', authType: 'qr' }, { kind: 'feishu', authType: 'plugin' }];

    expect(visibleConnectorCards(connectors).map((connector) => connector.kind)).toEqual(['weixin', 'feishu', 'terminal']);
  });
});


describe('app runtime bridge', () => {
  it('stays enabled on settings pages so the global connection status is accurate', () => {
    expect(runtimeBridgeEnabledForPath('/settings')).toBe(true);
  });
});
