import { describe, expect, it } from 'vitest';
import { pickBestServerUrl, suggestWorkerName, applySmartForgeDefaults } from './forgeSmartDefaults';
import type { BuildRequest } from '../types';

describe('forgeSmartDefaults', () => {
  it('suggests next worker-N name', () => {
    expect(suggestWorkerName([{ worker_name: 'worker-1' } as any])).toBe('worker-2');
  });

  it('picks LAN url over localhost', () => {
    expect(
      pickBestServerUrl('http://localhost:8989', ['http://192.168.1.5:8989'])
    ).toBe('http://192.168.1.5:8989');
  });

  it('fills worker and process name', () => {
    const form = applySmartForgeDefaults(
      { worker_name: '', server_url: '' } as BuildRequest,
      { endpointCandidates: ['http://10.0.0.2:8989'] }
    );
    expect(form.worker_name).toMatch(/^worker-/);
    expect(form.process_name).toBeTruthy();
    expect(form.server_url).toBe('http://10.0.0.2:8989');
    expect(form.mining_mode).toBe('idle');
  });

  it('fills backup C2 URLs from other LAN candidates', () => {
    const form = applySmartForgeDefaults(
      { worker_name: 'w1', server_url: 'http://192.168.1.5:8989' } as BuildRequest,
      {
        endpointCandidates: [
          'http://192.168.1.5:8989',
          'http://10.0.0.2:8989',
          'http://10.0.0.2:8989',
        ],
      }
    );
    expect(form.backup_server_urls).toEqual(['http://10.0.0.2:8989']);
  });
});
