import { describe, it, expect } from 'vitest';
import {
  orderedPoolsFromSelection,
  detectPresetIds,
  applyPoolsToForgeFields,
  DEFAULT_PRESET_IDS,
} from './poolPresets';

describe('poolPresets', () => {
  it('builds ordered failover list from preset ids', () => {
    const pools = orderedPoolsFromSelection([...DEFAULT_PRESET_IDS], 'x');
    expect(pools).toHaveLength(3);
    expect(pools[0].host).toBe('pool.supportxmr.com');
    expect(pools[1].host).toBe('gulf.moneroocean.stream');
    expect(pools[2].host).toBe('xmr.herominers.com');
  });

  it('dedupes duplicate preset picks', () => {
    const pools = orderedPoolsFromSelection(['supportxmr-tls', 'supportxmr-tls'], 'x');
    expect(pools).toHaveLength(1);
  });

  it('detects preset ids from primary and backups', () => {
    const ids = detectPresetIds('pool.supportxmr.com', 443, true, [
      { host: 'gulf.moneroocean.stream', port: 20128, tls: true },
    ]);
    expect(ids).toContain('supportxmr-tls');
    expect(ids).toContain('moneroocean-tls');
  });

  it('maps pools to forge primary and backup fields', () => {
    const fields = applyPoolsToForgeFields(
      orderedPoolsFromSelection(['2miners', 'xmrpool'], 'pw')
    );
    expect(fields.pool_host).toBe('xmr.2miners.com');
    expect(fields.pool_port).toBe(2222);
    expect(fields.backup_pools).toHaveLength(1);
    expect(fields.backup_pools![0].host).toBe('pool.xmrpool.eu');
  });
});
