|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { parseNativeAiCreditsUsageRecord, parseTokenUsageHeader, type TokenUsageRecord } from '../parser' |
| 4 | +import { CostCenterAggregator } from './costCenterAggregator' |
| 5 | +import { DailyUsageAggregator } from './dailyUsageAggregator' |
| 6 | +import { ModelUsageAggregator } from './modelUsageAggregator' |
| 7 | +import { OrganizationAggregator } from './organizationAggregator' |
| 8 | +import { ProductUsageAggregator } from './productUsageAggregator' |
| 9 | +import { UserUsageAggregator } from './userUsageAggregator' |
| 10 | + |
| 11 | +const NATIVE_AI_CREDITS_HEADER = [ |
| 12 | + 'date', |
| 13 | + 'username', |
| 14 | + 'product', |
| 15 | + 'sku', |
| 16 | + 'model', |
| 17 | + 'quantity', |
| 18 | + 'unit_type', |
| 19 | + 'applied_cost_per_quantity', |
| 20 | + 'gross_amount', |
| 21 | + 'discount_amount', |
| 22 | + 'net_amount', |
| 23 | + 'total_monthly_quota', |
| 24 | + 'organization', |
| 25 | + 'cost_center_name', |
| 26 | + 'aic_quantity', |
| 27 | + 'aic_gross_amount', |
| 28 | +].join(',') |
| 29 | + |
| 30 | +const NATIVE_AI_CREDITS_PARSED_HEADER = parseTokenUsageHeader(NATIVE_AI_CREDITS_HEADER) |
| 31 | + |
| 32 | +function nativeRecord(values: string[]): TokenUsageRecord { |
| 33 | + return parseNativeAiCreditsUsageRecord(values.join(','), NATIVE_AI_CREDITS_PARSED_HEADER) |
| 34 | +} |
| 35 | + |
| 36 | +function nativeRecords(): TokenUsageRecord[] { |
| 37 | + return [ |
| 38 | + nativeRecord([ |
| 39 | + '5/29/26', |
| 40 | + 'mona', |
| 41 | + 'copilot', |
| 42 | + 'copilot_ai_credit', |
| 43 | + 'GPT-5.2', |
| 44 | + '10', |
| 45 | + 'ai-credits', |
| 46 | + '0.01', |
| 47 | + '100', |
| 48 | + '20', |
| 49 | + '80', |
| 50 | + '3900', |
| 51 | + 'example-org', |
| 52 | + 'Cost Center A', |
| 53 | + '999', |
| 54 | + '999', |
| 55 | + ]), |
| 56 | + nativeRecord([ |
| 57 | + '05/29/2026', |
| 58 | + 'hubot', |
| 59 | + 'spark', |
| 60 | + 'spark_ai_credit', |
| 61 | + 'GPT-5.2', |
| 62 | + '25', |
| 63 | + 'ai-credits', |
| 64 | + '0.01', |
| 65 | + '250', |
| 66 | + '50', |
| 67 | + '200', |
| 68 | + '7000', |
| 69 | + 'octodemo', |
| 70 | + 'Cost Center B', |
| 71 | + '', |
| 72 | + '', |
| 73 | + ]), |
| 74 | + nativeRecord([ |
| 75 | + '2026-05-30', |
| 76 | + 'octocat', |
| 77 | + 'copilot', |
| 78 | + 'coding_agent_ai_credit', |
| 79 | + 'Copilot Coding Agent: Claude Sonnet 4.6', |
| 80 | + '40', |
| 81 | + 'ai-credits', |
| 82 | + '0.01', |
| 83 | + '400', |
| 84 | + '75', |
| 85 | + '325', |
| 86 | + '7000', |
| 87 | + 'example-org', |
| 88 | + 'Cost Center A', |
| 89 | + '4000', |
| 90 | + '4000', |
| 91 | + ]), |
| 92 | + ] |
| 93 | +} |
| 94 | + |
| 95 | +function aggregate(records: TokenUsageRecord[]) { |
| 96 | + const daily = new DailyUsageAggregator('native-ai-credits') |
| 97 | + const users = new UserUsageAggregator('native-ai-credits') |
| 98 | + const organizations = new OrganizationAggregator('native-ai-credits') |
| 99 | + const costCenters = new CostCenterAggregator('native-ai-credits') |
| 100 | + const models = new ModelUsageAggregator('native-ai-credits') |
| 101 | + const products = new ProductUsageAggregator('native-ai-credits') |
| 102 | + const aggregators = [daily, users, organizations, costCenters, models, products] |
| 103 | + |
| 104 | + records.forEach((record) => { |
| 105 | + aggregators.forEach((aggregator) => aggregator.accumulate(record)) |
| 106 | + }) |
| 107 | + |
| 108 | + return { |
| 109 | + daily: daily.result(), |
| 110 | + users: users.result(), |
| 111 | + organizations: organizations.result(), |
| 112 | + costCenters: costCenters.result(), |
| 113 | + models: models.result(), |
| 114 | + products: products.result(), |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +describe('native AI Credits direct aggregator usage', () => { |
| 119 | + it('keeps native usage test-only while mapping canonical AIC metrics into existing daily and model fields', () => { |
| 120 | + const result = aggregate(nativeRecords()) |
| 121 | + |
| 122 | + expect(result.daily.dailyData).toEqual([ |
| 123 | + expect.objectContaining({ |
| 124 | + date: '2026-05-29', |
| 125 | + requests: 0, |
| 126 | + grossAmount: 0, |
| 127 | + discountAmount: 0, |
| 128 | + netAmount: 0, |
| 129 | + aicQuantity: 35, |
| 130 | + aicGrossAmount: 350, |
| 131 | + aicNetAmount: 280, |
| 132 | + }), |
| 133 | + expect.objectContaining({ |
| 134 | + date: '2026-05-30', |
| 135 | + requests: 0, |
| 136 | + grossAmount: 0, |
| 137 | + discountAmount: 0, |
| 138 | + netAmount: 0, |
| 139 | + aicQuantity: 40, |
| 140 | + aicGrossAmount: 400, |
| 141 | + aicNetAmount: 325, |
| 142 | + }), |
| 143 | + ]) |
| 144 | + |
| 145 | + expect(result.models.totalsByModel['GPT-5.2']).toEqual({ |
| 146 | + requests: 0, |
| 147 | + grossAmount: 0, |
| 148 | + discountAmount: 0, |
| 149 | + netAmount: 0, |
| 150 | + aicQuantity: 35, |
| 151 | + aicGrossAmount: 350, |
| 152 | + aicNetAmount: 280, |
| 153 | + }) |
| 154 | + expect(result.models.totalsByModel['Copilot Coding Agent: Claude Sonnet 4.6']).toEqual({ |
| 155 | + requests: 0, |
| 156 | + grossAmount: 0, |
| 157 | + discountAmount: 0, |
| 158 | + netAmount: 0, |
| 159 | + aicQuantity: 40, |
| 160 | + aicGrossAmount: 400, |
| 161 | + aicNetAmount: 325, |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + it('maps native actual costs into product, user, organization, and cost-center aic fields', () => { |
| 166 | + const result = aggregate(nativeRecords()) |
| 167 | + |
| 168 | + const copilot = result.products.products.find((product) => product.product === 'Copilot') |
| 169 | + expect(copilot?.totals).toEqual({ |
| 170 | + requests: 0, |
| 171 | + grossAmount: 0, |
| 172 | + netAmount: 0, |
| 173 | + aicQuantity: 10, |
| 174 | + aicGrossAmount: 100, |
| 175 | + aicNetAmount: 80, |
| 176 | + }) |
| 177 | + expect(copilot?.models['GPT-5.2']).toEqual({ |
| 178 | + requests: 0, |
| 179 | + grossAmount: 0, |
| 180 | + netAmount: 0, |
| 181 | + aicQuantity: 10, |
| 182 | + aicGrossAmount: 100, |
| 183 | + aicNetAmount: 80, |
| 184 | + }) |
| 185 | + |
| 186 | + const mona = result.users.users.find((user) => user.username === 'mona') |
| 187 | + expect(mona?.totals).toEqual(expect.objectContaining({ |
| 188 | + requests: 0, |
| 189 | + grossAmount: 0, |
| 190 | + discountAmount: 0, |
| 191 | + netAmount: 0, |
| 192 | + aicQuantity: 10, |
| 193 | + aicGrossAmount: 100, |
| 194 | + aicNetAmount: 80, |
| 195 | + })) |
| 196 | + expect(mona?.daily['2026-05-29']).toEqual(expect.objectContaining({ |
| 197 | + requests: 0, |
| 198 | + grossAmount: 0, |
| 199 | + discountAmount: 0, |
| 200 | + netAmount: 0, |
| 201 | + aicQuantity: 10, |
| 202 | + aicGrossAmount: 100, |
| 203 | + aicNetAmount: 80, |
| 204 | + })) |
| 205 | + |
| 206 | + const exampleOrg = result.organizations.organizations.find((organization) => organization.organization === 'example-org') |
| 207 | + expect(exampleOrg?.totals).toEqual({ |
| 208 | + requests: 0, |
| 209 | + grossAmount: 0, |
| 210 | + discountAmount: 0, |
| 211 | + netAmount: 0, |
| 212 | + aicQuantity: 50, |
| 213 | + aicGrossAmount: 500, |
| 214 | + aicNetAmount: 405, |
| 215 | + }) |
| 216 | + expect(exampleOrg?.totalsByUser.mona).toEqual({ |
| 217 | + requests: 0, |
| 218 | + grossAmount: 0, |
| 219 | + netAmount: 0, |
| 220 | + aicQuantity: 10, |
| 221 | + aicGrossAmount: 100, |
| 222 | + aicNetAmount: 80, |
| 223 | + }) |
| 224 | + |
| 225 | + const costCenter = result.costCenters.costCenters.find((entry) => entry.costCenterName === 'Cost Center A') |
| 226 | + expect(costCenter?.totals).toEqual({ |
| 227 | + requests: 0, |
| 228 | + grossAmount: 0, |
| 229 | + discountAmount: 0, |
| 230 | + netAmount: 0, |
| 231 | + aicQuantity: 50, |
| 232 | + aicGrossAmount: 500, |
| 233 | + aicNetAmount: 405, |
| 234 | + }) |
| 235 | + expect(costCenter?.totalsByUser.octocat).toEqual({ |
| 236 | + requests: 0, |
| 237 | + grossAmount: 0, |
| 238 | + netAmount: 0, |
| 239 | + aicQuantity: 40, |
| 240 | + aicGrossAmount: 400, |
| 241 | + aicNetAmount: 325, |
| 242 | + }) |
| 243 | + }) |
| 244 | +}) |
0 commit comments