import { describe, expect, it } from 'vitest';
import { agentNeedsAttention, agentSubnet, filterFleetAgents } from './fleetFilters';
import type { Agent } from '../types';

const base = (over: Partial<Agent>): Agent => ({
  id: '1',
  name: 'w1',
  wallet: '',
  ip: '192.168.1.10',
  version: '1',
  status: 'online',
  cpu_cores: 4,
  memory_gb: 8,
  last_seen: '',
  created_at: '',
  hashrate_15s: 0,
  hashrate_1m: 0,
  hashrate_15m: 5000,
  shares_total: 0,
  shares_good: 0,
  shares_bad: 0,
  cpu_usage_pct: 0,
  memory_usage_pct: 0,
  uptime_seconds: 0,
  tags: ['lab'],
  ...over,
});

const DEFAULT = {
  search: '',
  tag: '',
  subnet: '',
  hashrateMin: 0,
  needsAttention: false,
};

describe('fleetFilters', () => {
  it('filters by tag and subnet', () => {
    const agents = [base({}), base({ id: '2', ip: '10.0.0.2', tags: [] })];
    expect(filterFleetAgents(agents, { ...DEFAULT, tag: 'lab' }).length).toBe(1);
    expect(filterFleetAgents(agents, { ...DEFAULT, subnet: '192.168.1.x' }).length).toBe(1);
  });

  it('flags offline as needs attention', () => {
    expect(agentNeedsAttention(base({ status: 'offline' }))).toBe(true);
  });
});

describe('agentSubnet', () => {
  it('masks last octet', () => {
    expect(agentSubnet('192.168.5.22')).toBe('192.168.5.x');
  });
});
