import { describe, expect, it } from 'vitest';
import { FORGE_BUILD_DEFAULTS, forgeDefaultsFromServer } from './forgeDefaults';
import { mockServerConfig, mockServerInfo } from '../test/fixtures';

describe('FORGE_BUILD_DEFAULTS', () => {
  it('sets safe production defaults for a new forge form', () => {
    expect(FORGE_BUILD_DEFAULTS.threads).toBe(4);
    expect(FORGE_BUILD_DEFAULTS.thread_mode).toBe('percent');
    expect(FORGE_BUILD_DEFAULTS.stealth_mode).toBe(true);
    expect(FORGE_BUILD_DEFAULTS.persistence).toBe(true);
    expect(FORGE_BUILD_DEFAULTS.fusion_enabled).toBe(false);
    expect(FORGE_BUILD_DEFAULTS.ai_enabled).toBe(false);
    expect(FORGE_BUILD_DEFAULTS.auto_spread).toBe(false);
    expect(FORGE_BUILD_DEFAULTS.remote_aggressive).toBe(false);
  });

  it('omits per-build identity fields (filled by server merge)', () => {
    const keys = Object.keys(FORGE_BUILD_DEFAULTS);
    expect(keys).not.toContain('worker_name');
    expect(keys).not.toContain('server_url');
    expect(keys).not.toContain('wallet');
    expect(keys).not.toContain('pool_host');
    expect(keys).not.toContain('pool_port');
    expect(keys).not.toContain('pool_tls');
    expect(keys).not.toContain('pool_pass');
  });
});

describe('forgeDefaultsFromServer', () => {
  it('merges server config pool/wallet with build defaults', () => {
    const config = mockServerConfig();
    const result = forgeDefaultsFromServer(config, mockServerInfo);
    expect(result.wallet).toBe(config.wallet.address);
    expect(result.pool_host).toBe(config.pool.host);
    expect(result.pool_port).toBe(config.pool.port);
    expect(result.pool_tls).toBe(config.pool.use_tls);
    expect(result.pool_pass).toBe('x');
    expect(result.worker_name).toBe('');
    expect(result.stealth_mode).toBe(FORGE_BUILD_DEFAULTS.stealth_mode);
  });

  it('prefers trimmed public_url over suggested_url', () => {
    const config = mockServerConfig({
      server: { public_url: '  https://tunnel.example.com  ' },
    });
    const result = forgeDefaultsFromServer(config, mockServerInfo);
    expect(result.server_url).toBe('https://tunnel.example.com');
  });

  it('falls back to suggested LAN URL when public_url is blank', () => {
    const config = mockServerConfig({
      server: { public_url: '   ' },
    });
    const result = forgeDefaultsFromServer(config, mockServerInfo);
    expect(result.server_url).toBe(mockServerInfo.suggested_url);
  });

  it('reflects obfuscate and sign defaults from server config', () => {
    const config = mockServerConfig({
      server: { obfuscate_default: true, sign_enabled: true },
    });
    const result = forgeDefaultsFromServer(config, mockServerInfo);
    expect(result.obfuscate).toBe(true);
    expect(result.sign_build).toBe(true);
  });

  it('uses custom pool password when configured', () => {
    const config = mockServerConfig({
      pool: { password: 'worker-pass' },
    });
    const result = forgeDefaultsFromServer(config, mockServerInfo);
    expect(result.pool_pass).toBe('worker-pass');
  });
});
