import { describe, expect, it } from 'vitest';
import { formatLanEndpoint, lanEndpointCandidates } from './endpointHelpers';
import type { ServerInfo } from '../types';

describe('formatLanEndpoint', () => {
  it('strips scheme and path from host', () => {
    expect(formatLanEndpoint('https://192.168.1.5:8989/extra', 8989)).toBe('http://192.168.1.5:8989');
  });

  it('builds http URL from bare IP', () => {
    expect(formatLanEndpoint('10.0.0.2', 7777)).toBe('http://10.0.0.2:7777');
  });
});

describe('lanEndpointCandidates', () => {
  it('dedupes suggested URL and local IPs', () => {
    const info: ServerInfo = {
      port: 8989,
      host: 'localhost',
      local_ips: ['192.168.1.10', '192.168.1.10'],
      suggested_url: 'http://192.168.1.10:8989',
      dashboard_url: 'http://192.168.1.10:8989',
      websocket_url: 'ws://192.168.1.10:8989/ws/agent',
    };
    const urls = lanEndpointCandidates(info);
    expect(urls).toEqual(['http://192.168.1.10:8989']);
  });

  it('honours port override', () => {
    const info: ServerInfo = {
      port: 8989,
      host: 'localhost',
      local_ips: ['10.0.0.1'],
      suggested_url: '',
      dashboard_url: '',
      websocket_url: '',
    };
    expect(lanEndpointCandidates(info, 9000)).toEqual(['http://10.0.0.1:9000']);
  });
});
