|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { |
| 4 | + InvalidReportError, |
| 5 | + UnsupportedNativeAiCreditsReportError, |
| 6 | + UnsupportedReportVersionError, |
| 7 | + parseTokenUsageHeader, |
| 8 | + parseTokenUsageRecord, |
| 9 | +} from './parser' |
| 10 | +import { |
| 11 | + detectReportFormat, |
| 12 | + selectUsageReportAdapter, |
| 13 | + validateUsageReportFirstRecord, |
| 14 | + validateUsageReportHeader, |
| 15 | +} from './reportAdapters' |
| 16 | + |
| 17 | +const TRANSITION_PERIOD_HEADER = [ |
| 18 | + 'date', |
| 19 | + 'username', |
| 20 | + 'product', |
| 21 | + 'sku', |
| 22 | + 'model', |
| 23 | + 'quantity', |
| 24 | + 'unit_type', |
| 25 | + 'applied_cost_per_quantity', |
| 26 | + 'gross_amount', |
| 27 | + 'discount_amount', |
| 28 | + 'net_amount', |
| 29 | + 'exceeds_quota', |
| 30 | + 'total_monthly_quota', |
| 31 | + 'organization', |
| 32 | + 'cost_center_name', |
| 33 | + 'aic_quantity', |
| 34 | + 'aic_gross_amount', |
| 35 | +].join(',') |
| 36 | + |
| 37 | +const HEADER_WITHOUT_EXCEEDS_QUOTA = [ |
| 38 | + 'date', |
| 39 | + 'username', |
| 40 | + 'product', |
| 41 | + 'sku', |
| 42 | + 'model', |
| 43 | + 'quantity', |
| 44 | + 'unit_type', |
| 45 | + 'applied_cost_per_quantity', |
| 46 | + 'gross_amount', |
| 47 | + 'discount_amount', |
| 48 | + 'net_amount', |
| 49 | + 'total_monthly_quota', |
| 50 | + 'organization', |
| 51 | + 'cost_center_name', |
| 52 | + 'aic_quantity', |
| 53 | + 'aic_gross_amount', |
| 54 | +].join(',') |
| 55 | + |
| 56 | +function buildRow(values: string[]): string { |
| 57 | + return values.join(',') |
| 58 | +} |
| 59 | + |
| 60 | +describe('usage report adapters', () => { |
| 61 | + it('detects and selects the Transition Period Billing Preview adapter for current preview reports', () => { |
| 62 | + const header = parseTokenUsageHeader(TRANSITION_PERIOD_HEADER) |
| 63 | + const record = parseTokenUsageRecord( |
| 64 | + buildRow([ |
| 65 | + '2026-05-29', |
| 66 | + 'mona', |
| 67 | + 'copilot', |
| 68 | + 'copilot_premium_request', |
| 69 | + 'Auto: Claude Haiku 4.5', |
| 70 | + '2', |
| 71 | + 'requests', |
| 72 | + '0.04', |
| 73 | + '0.08', |
| 74 | + '0', |
| 75 | + '0.08', |
| 76 | + 'False', |
| 77 | + '300', |
| 78 | + 'example-org', |
| 79 | + 'Cost Center A', |
| 80 | + '20', |
| 81 | + '0.20', |
| 82 | + ]), |
| 83 | + header, |
| 84 | + ) |
| 85 | + |
| 86 | + expect(detectReportFormat(header, record)).toBe('transition-period-billing-preview') |
| 87 | + expect(selectUsageReportAdapter(header, record).metadata).toMatchObject({ |
| 88 | + format: 'transition-period-billing-preview', |
| 89 | + supported: true, |
| 90 | + }) |
| 91 | + expect(() => validateUsageReportFirstRecord(header, record)).not.toThrow() |
| 92 | + }) |
| 93 | + |
| 94 | + it('keeps missing-exceeds premium request rows on the Transition Period Billing Preview adapter', () => { |
| 95 | + const header = parseTokenUsageHeader(HEADER_WITHOUT_EXCEEDS_QUOTA) |
| 96 | + const record = parseTokenUsageRecord( |
| 97 | + buildRow([ |
| 98 | + '2026-05-29', |
| 99 | + 'mona', |
| 100 | + 'copilot', |
| 101 | + 'copilot_premium_request', |
| 102 | + 'Auto: Claude Haiku 4.5', |
| 103 | + '2', |
| 104 | + 'requests', |
| 105 | + '0.04', |
| 106 | + '0.08', |
| 107 | + '0', |
| 108 | + '0.08', |
| 109 | + '300', |
| 110 | + 'example-org', |
| 111 | + 'Cost Center A', |
| 112 | + '20', |
| 113 | + '0.20', |
| 114 | + ]), |
| 115 | + header, |
| 116 | + ) |
| 117 | + |
| 118 | + expect(detectReportFormat(header, record)).toBe('transition-period-billing-preview') |
| 119 | + expect(selectUsageReportAdapter(header, record).metadata.format).toBe('transition-period-billing-preview') |
| 120 | + expect(() => validateUsageReportFirstRecord(header, record)).not.toThrow() |
| 121 | + }) |
| 122 | + |
| 123 | + it('detects native AI Credits reports and routes them to an unsupported adapter', () => { |
| 124 | + const header = parseTokenUsageHeader(HEADER_WITHOUT_EXCEEDS_QUOTA) |
| 125 | + const record = parseTokenUsageRecord( |
| 126 | + buildRow([ |
| 127 | + '2026-06-01', |
| 128 | + 'mona', |
| 129 | + 'copilot', |
| 130 | + 'copilot_ai_credit', |
| 131 | + 'Auto: Claude Haiku 4.5', |
| 132 | + '96.9990345', |
| 133 | + 'ai-credits', |
| 134 | + '0.01', |
| 135 | + '0.969990345', |
| 136 | + '0', |
| 137 | + '0.969990345', |
| 138 | + '3900', |
| 139 | + 'example-org', |
| 140 | + '', |
| 141 | + '96.9990345', |
| 142 | + '0.969990345', |
| 143 | + ]), |
| 144 | + header, |
| 145 | + ) |
| 146 | + |
| 147 | + expect(detectReportFormat(header, record)).toBe('native-ai-credits') |
| 148 | + expect(selectUsageReportAdapter(header, record).metadata).toMatchObject({ |
| 149 | + format: 'native-ai-credits', |
| 150 | + supported: false, |
| 151 | + }) |
| 152 | + expect(() => validateUsageReportFirstRecord(header, record)).toThrow(UnsupportedNativeAiCreditsReportError) |
| 153 | + }) |
| 154 | + |
| 155 | + it('fails clearly for malformed billing headers before adapter selection', () => { |
| 156 | + const header = parseTokenUsageHeader('foo,bar,baz') |
| 157 | + |
| 158 | + expect(() => validateUsageReportHeader(header)).toThrow(InvalidReportError) |
| 159 | + }) |
| 160 | + |
| 161 | + it('fails clearly for pre-AIC report headers before adapter selection', () => { |
| 162 | + const header = parseTokenUsageHeader([ |
| 163 | + 'date', |
| 164 | + 'username', |
| 165 | + 'product', |
| 166 | + 'sku', |
| 167 | + 'model', |
| 168 | + 'quantity', |
| 169 | + 'unit_type', |
| 170 | + 'applied_cost_per_quantity', |
| 171 | + 'gross_amount', |
| 172 | + 'discount_amount', |
| 173 | + 'net_amount', |
| 174 | + 'exceeds_quota', |
| 175 | + 'total_monthly_quota', |
| 176 | + 'organization', |
| 177 | + 'cost_center_name', |
| 178 | + ].join(',')) |
| 179 | + |
| 180 | + expect(() => validateUsageReportHeader(header)).toThrow(UnsupportedReportVersionError) |
| 181 | + }) |
| 182 | +}) |
0 commit comments