import { describe, it, expect } from 'vitest';
import { assignBadges, type ModelScore } from '../routing-scores.js';

function makeScore(partial: Partial<ModelScore> & { compositeId: string }): ModelScore {
  return {
    providerId: partial.compositeId.split('/')[0]!,
    model: partial.compositeId.split('/').slice(1).join('/'),
    codeScore: null,
    chatScore: null,
    evalScore: null,
    avgGenTps: null,
    avgLatencyMs: null,
    sampleCount: 0,
    healthy: true,
    badges: [],
    ...partial,
  };
}

describe('assignBadges', () => {
  it('awards best-code to the highest healthy code score', () => {
    const scores = [
      makeScore({ compositeId: 'a/m1', codeScore: 0.7 }),
      makeScore({ compositeId: 'a/m2', codeScore: 0.9 }),
      makeScore({ compositeId: 'a/m3', codeScore: 0.5 }),
    ];
    assignBadges(scores);
    expect(scores.find((s) => s.compositeId === 'a/m2')!.badges).toContain('best-code');
    expect(scores.find((s) => s.compositeId === 'a/m1')!.badges).not.toContain('best-code');
  });

  it('excludes unhealthy hosts from winning any badge', () => {
    const scores = [
      makeScore({ compositeId: 'a/m1', codeScore: 0.95, healthy: false }),
      makeScore({ compositeId: 'a/m2', codeScore: 0.6, healthy: true }),
    ];
    assignBadges(scores);
    expect(scores.find((s) => s.compositeId === 'a/m1')!.badges).toHaveLength(0);
    expect(scores.find((s) => s.compositeId === 'a/m2')!.badges).toContain('best-code');
  });

  it('awards best-fast by throughput independently of eval scores', () => {
    const scores = [
      makeScore({ compositeId: 'a/slow', codeScore: 0.9, avgGenTps: 10 }),
      makeScore({ compositeId: 'a/fast', codeScore: 0.4, avgGenTps: 80 }),
    ];
    assignBadges(scores);
    expect(scores.find((s) => s.compositeId === 'a/fast')!.badges).toContain('best-fast');
    expect(scores.find((s) => s.compositeId === 'a/slow')!.badges).toContain('best-code');
  });

  it('awards nothing for a category when no model has that metric', () => {
    const scores = [makeScore({ compositeId: 'a/m1', avgGenTps: 20 })];
    assignBadges(scores);
    expect(scores[0]!.badges).toEqual(['best-fast']);
  });
});
