import { describe, it, expect } from 'vitest';
import { computeCost, formatEur, getModelPrice } from '../ModelPricing';

describe('ModelPricing', () => {
    it('matches exact model id case-insensitively', () => {
        const price = getModelPrice('claude-sonnet-4-6');
        expect(price.inputPerMillionUsd).toBe(3);
        expect(price.outputPerMillionUsd).toBe(15);
    });

    it('falls back to longest substring match', () => {
        const price = getModelPrice('claude-sonnet-4-6-20251101');
        expect(price.inputPerMillionUsd).toBe(3);
    });

    it('returns Haiku pricing for haiku models', () => {
        const price = getModelPrice('claude-haiku-4-5-20251001');
        expect(price.inputPerMillionUsd).toBe(1);
        expect(price.outputPerMillionUsd).toBe(5);
    });

    it('falls back gracefully for unknown models', () => {
        const price = getModelPrice('some-future-model');
        expect(price.inputPerMillionUsd).toBeGreaterThan(0);
    });

    it('computes total cost with cache rates', () => {
        const cost = computeCost('claude-sonnet-4-6', 100_000, 10_000, 50_000, 0);
        // input: 100k * 3$/M = $0.30
        // output: 10k * 15$/M = $0.15
        // cache read: 50k * 0.3$/M = $0.015
        // total USD: 0.465, EUR: ~0.432
        expect(cost.totalUsd).toBeCloseTo(0.465, 2);
        expect(cost.totalEur).toBeCloseTo(0.432, 2);
    });

    // v2.10.0: formatEur uses Intl.NumberFormat('de-DE', currency: EUR)
    // with min 2 / max 4 fraction digits. The Euro sign is separated from
    // the amount by a non-breaking space (U+00A0), per German locale.
    it('formats sub-cent amounts with up to 4 digits', () => {
        // 0,005 rendered with min 2, max 4 -> "0,005"
        expect(formatEur(0.005)).toBe('0,005 €');
    });

    it('formats cent amounts with locale comma', () => {
        // 0,042 rendered with min 2, max 4 -> "0,042"
        expect(formatEur(0.042)).toBe('0,042 €');
    });

    it('formats euros above one with up to 4 digits', () => {
        // 1,234 rendered with min 2, max 4 -> "1,234"
        expect(formatEur(1.234)).toBe('1,234 €');
    });

    it('formats zero as 0,00 EUR', () => {
        expect(formatEur(0)).toBe('0,00 €');
    });

    it('formats common amounts correctly', () => {
        expect(formatEur(0.02)).toBe('0,02 €');
        expect(formatEur(0.84)).toBe('0,84 €');
    });
});
