|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockRecordUsed, mockRecordCostCharged } = vi.hoisted(() => ({ |
| 7 | + mockRecordUsed: vi.fn(), |
| 8 | + mockRecordCostCharged: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/monitoring/metrics', () => ({ |
| 12 | + hostedKeyMetrics: { |
| 13 | + recordUsed: mockRecordUsed, |
| 14 | + recordCostCharged: mockRecordCostCharged, |
| 15 | + }, |
| 16 | +})) |
| 17 | + |
| 18 | +import { |
| 19 | + calculateHostedCost, |
| 20 | + classifyHostedKeyFailure, |
| 21 | + emitHostedKeyUsage, |
| 22 | +} from '@/lib/api-key/hosted-cost' |
| 23 | + |
| 24 | +describe('calculateHostedCost (tool pricing)', () => { |
| 25 | + it('per_request returns the flat fee', () => { |
| 26 | + expect(calculateHostedCost({ type: 'per_request', cost: 0.005 }, {}, {})).toEqual({ |
| 27 | + cost: 0.005, |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('custom returns a numeric getCost result', () => { |
| 32 | + const pricing = { type: 'custom' as const, getCost: () => 0.42 } |
| 33 | + expect(calculateHostedCost(pricing, {}, {})).toEqual({ cost: 0.42 }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('custom passes through a structured getCost result with metadata', () => { |
| 37 | + const pricing = { |
| 38 | + type: 'custom' as const, |
| 39 | + getCost: () => ({ cost: 1.5, metadata: { units: 3 } }), |
| 40 | + } |
| 41 | + expect(calculateHostedCost(pricing, {}, {})).toEqual({ cost: 1.5, metadata: { units: 3 } }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('forwards params and response to custom getCost', () => { |
| 45 | + const getCost = vi.fn(() => 1) |
| 46 | + const params = { a: 1 } |
| 47 | + const response = { b: 2 } |
| 48 | + calculateHostedCost({ type: 'custom', getCost }, params, response) |
| 49 | + expect(getCost).toHaveBeenCalledWith(params, response) |
| 50 | + }) |
| 51 | +}) |
| 52 | + |
| 53 | +describe('classifyHostedKeyFailure', () => { |
| 54 | + it('classifies structured SDK errors by status', () => { |
| 55 | + expect(classifyHostedKeyFailure({ status: 429 })).toBe('rate_limited') |
| 56 | + expect(classifyHostedKeyFailure({ status: 503 })).toBe('rate_limited') |
| 57 | + expect(classifyHostedKeyFailure({ status: 401 })).toBe('auth') |
| 58 | + expect(classifyHostedKeyFailure({ status: 403, message: 'quota exceeded' })).toBe( |
| 59 | + 'rate_limited' |
| 60 | + ) |
| 61 | + expect(classifyHostedKeyFailure({ status: 500 })).toBe('other') |
| 62 | + }) |
| 63 | + |
| 64 | + it('classifies message-embedded status (provider errors with no .status)', () => { |
| 65 | + // Regression: the previous `\bunauthor\b` regex never matched "Unauthorized". |
| 66 | + expect(classifyHostedKeyFailure(new Error('Unauthorized'))).toBe('auth') |
| 67 | + expect(classifyHostedKeyFailure(new Error('OpenAI API error (401): bad key'))).toBe('auth') |
| 68 | + expect(classifyHostedKeyFailure(new Error('Forbidden'))).toBe('auth') |
| 69 | + expect(classifyHostedKeyFailure(new Error('Invalid API key provided'))).toBe('auth') |
| 70 | + expect(classifyHostedKeyFailure(new Error('API error (429): rate limit'))).toBe('rate_limited') |
| 71 | + expect(classifyHostedKeyFailure(new Error('Internal Server Error (500)'))).toBe('other') |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('emitHostedKeyUsage', () => { |
| 76 | + beforeEach(() => { |
| 77 | + vi.clearAllMocks() |
| 78 | + }) |
| 79 | + |
| 80 | + it('records both usage and cost with the provider/tool/key labels', () => { |
| 81 | + emitHostedKeyUsage({ |
| 82 | + provider: 'openai', |
| 83 | + tool: 'gpt-4o', |
| 84 | + key: 'OPENAI_API_KEY_2', |
| 85 | + costTotal: 0.03, |
| 86 | + }) |
| 87 | + |
| 88 | + expect(mockRecordUsed).toHaveBeenCalledWith({ |
| 89 | + provider: 'openai', |
| 90 | + tool: 'gpt-4o', |
| 91 | + key: 'OPENAI_API_KEY_2', |
| 92 | + }) |
| 93 | + expect(mockRecordCostCharged).toHaveBeenCalledWith(0.03, { provider: 'openai', tool: 'gpt-4o' }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments