import assert from 'node:assert/strict'; import test from 'node:test'; import { buildBillingBreakdown, reconcileDisplayAmounts, } from '../../src/components/table/usage-logs/billingBreakdown.js'; const near = (actual, expected) => assert.ok( Math.abs(actual - expected) < 1e-6, `expected ${actual} ≈ ${expected}`, ); const keys = (result) => result.items.map((item) => item.key); test('splits cached input out of prompt tokens for OpenAI-style logs', () => { const result = buildBillingBreakdown( { prompt_tokens: 88630, completion_tokens: 153, quota: 1716 }, { model_ratio: 0.575, completion_ratio: 4, cache_tokens: 87168, cache_ratio: 0.02, group_ratio: 0.782, model_price: -1, }, ); assert.deepEqual(keys(result), ['inputUncached', 'cacheRead', 'output']); assert.equal(result.items[0].qty, 1462); near(result.items[0].quota, 1462 * 0.575 * 0.782); near(result.items[1].quota, 87168 * 0.02 * 0.575 * 0.782); near(result.items[2].quota, 153 * 4 * 0.575 * 0.782); near(result.estimated, 3817.36 * 0.575 * 0.782); assert.equal(result.actual, 1716); }); test('keeps Claude prompt tokens intact and prices each cache bucket', () => { const result = buildBillingBreakdown( { prompt_tokens: 100, completion_tokens: 10, quota: 2138 }, { claude: true, model_ratio: 1.5, completion_ratio: 5, group_ratio: 1, model_price: -1, cache_tokens: 1000, cache_ratio: 0.1, cache_creation_tokens: 700, cache_creation_ratio: 1.25, cache_creation_tokens_5m: 200, cache_creation_ratio_5m: 1.25, cache_creation_tokens_1h: 400, cache_creation_ratio_1h: 2, cache_creation_tokens_unlabeled: 100, cache_creation_ratio_unlabeled: 1.25, }, ); assert.deepEqual(keys(result), [ 'inputUncached', 'cacheRead', 'cacheCreation5m', 'cacheCreation1h', 'cacheCreationUnlabeled', 'output', ]); assert.equal(result.items[0].qty, 100); near(result.estimated, (100 + 100 + 250 + 800 + 125 + 50) * 1.5); // 分项之和 2137.5,四舍五入后实扣 2138 near(result.actual - result.estimated, 0.5); }); test('per-call pricing yields a single line', () => { const result = buildBillingBreakdown( { prompt_tokens: 0, completion_tokens: 0, quota: 20000 }, { model_price: 0.05, group_ratio: 0.8, model_ratio: 0 }, { quotaPerUnit: 500000 }, ); assert.deepEqual(keys(result), ['perCall']); near(result.items[0].quota, 20000); near(result.actual - result.estimated, 0); }); test('audio logs price text and audio separately', () => { const result = buildBillingBreakdown( { prompt_tokens: 30, completion_tokens: 20, quota: 1040 }, { audio: true, model_ratio: 2, completion_ratio: 2, group_ratio: 1, model_price: -1, audio_ratio: 10, audio_completion_ratio: 2, text_input: 10, text_output: 5, audio_input: 20, audio_output: 15, }, ); assert.deepEqual(keys(result), [ 'textInput', 'textOutput', 'audioInput', 'audioOutput', ]); near(result.estimated, (10 + 5 * 2 + 20 * 10 + 15 * 10 * 2) * 2); }); test('tool calls are priced by group ratio only', () => { const result = buildBillingBreakdown( { prompt_tokens: 1000, completion_tokens: 0, quota: 32000 }, { model_ratio: 1, completion_ratio: 1, group_ratio: 2, model_price: -1, web_search: true, web_search_call_count: 3, web_search_price: 10, }, ); assert.deepEqual(keys(result), ['input', 'webSearch']); near(result.items[1].quota, (10 / 1000) * 3 * 2 * 500000); }); test('returns null when the log carries no pricing info', () => { assert.equal(buildBillingBreakdown({ quota: 10 }, {}), null); assert.equal(buildBillingBreakdown({ quota: 10 }, null), null); }); test('per-call pricing still carries tool charges', () => { const result = buildBillingBreakdown( { prompt_tokens: 0, completion_tokens: 0, quota: 30000 }, { model_price: 0.05, group_ratio: 0.8, image_generation_call: true, image_generation_call_price: 0.025, }, ); assert.deepEqual(keys(result), ['perCall', 'imageGeneration']); near(result.estimated, 30000); }); test('per-second video tasks are priced by duration, not per call', () => { const result = buildBillingBreakdown( { quota: 200000 }, { model_price: 0, group_ratio: 0.8, video_second_unit_price: 0.1, video_second_duration: 5, video_second_group_ratio: 0.8, }, ); assert.deepEqual(keys(result), ['videoSecond']); assert.equal(result.items[0].qty, 5); near(result.estimated, 200000); }); test('hides the breakdown when it cannot explain the actual charge', () => { // 任务按时长 / 分辨率系数计价:日志只有 model_price,系数不在结构化字段里 assert.equal( buildBillingBreakdown( { quota: 150000 }, { model_price: 0.1, group_ratio: 1 }, ), null, ); // token 兜底预扣:model_price 为 0,实际扣费来自估算 assert.equal( buildBillingBreakdown( { quota: 8000 }, { model_price: 0, group_ratio: 1, video_token_fallback: true }, ), null, ); }); test('tolerance is one quota unit, not a share of the charge', () => { // 100 万额度的请求里有 9500 个单位对不上:占比不到 1%,但绝不是「舍入」 assert.equal( buildBillingBreakdown( { prompt_tokens: 990500, completion_tokens: 0, quota: 1000000 }, { model_price: -1, model_ratio: 1, completion_ratio: 1, group_ratio: 1 }, ), null, ); // 差 2 个单位同样不展示;差 1 个单位以内才算取整 const base = { model_price: -1, model_ratio: 1, completion_ratio: 1 }; assert.equal( buildBillingBreakdown( { prompt_tokens: 1000, quota: 1002 }, { ...base, group_ratio: 1 }, ), null, ); const withinOne = buildBillingBreakdown( { prompt_tokens: 1000, quota: 1001 }, { ...base, group_ratio: 1 }, ); near(withinOne.actual - withinOne.estimated, 1); }); test('never presents a whole charge as an adjustment', () => { // 按次价 0 却有实扣:分项全是 0 元,这笔钱没有被任何一行解释 assert.equal( buildBillingBreakdown({ quota: 1 }, { model_price: 0, group_ratio: 1 }), null, ); // model_price 为 null 等于没有这个字段,不能当成「按次 0 元」 assert.equal( buildBillingBreakdown({ quota: 0 }, { model_price: null, group_ratio: 1 }), null, ); const tokenBilled = buildBillingBreakdown( { prompt_tokens: 1000, quota: 1000 }, { model_price: null, model_ratio: 1, completion_ratio: 1, group_ratio: 1 }, ); assert.deepEqual(keys(tokenBilled), ['input']); }); test('cost-basis video fields do not turn other logs into per-second billing', () => { const perSecond = { model_price: 0, group_ratio: 0.8, video_second_unit_price: 0.1, video_second_duration: 5, }; // 即使金额恰好等于 单价 × 时长 × 分组倍率,这两类也不是按秒结算的一整笔 assert.equal( buildBillingBreakdown( { quota: 200000 }, { ...perSecond, video_token_fallback: true }, ), null, ); assert.equal( buildBillingBreakdown( { quota: 200000 }, { ...perSecond, quota_adjustment: true }, ), null, ); // 字段只是成本基准、实扣是别的算法算出来的 assert.equal(buildBillingBreakdown({ quota: 150000 }, perSecond), null); assert.deepEqual(keys(buildBillingBreakdown({ quota: 200000 }, perSecond)), [ 'videoSecond', ]); }); test('zero model price with a model ratio is still token billing', () => { const result = buildBillingBreakdown( { prompt_tokens: 1000, completion_tokens: 0, quota: 1000 }, { model_price: 0, model_ratio: 1, completion_ratio: 1, group_ratio: 1 }, ); assert.deepEqual(keys(result), ['input']); near(result.estimated, 1000); }); test('a minimum charge is within tolerance', () => { // 10 tokens × 0.02 = 0.2 个额度单位,结算按最低 1 个单位扣 const result = buildBillingBreakdown( { prompt_tokens: 10, completion_tokens: 0, quota: 1 }, { model_price: -1, model_ratio: 0.02, completion_ratio: 1, group_ratio: 1 }, ); assert.deepEqual(keys(result), ['input']); near(result.actual - result.estimated, 0.8); }); test('both web-search charges get their own line', () => { // 模型名以 search-preview 结尾却由 Claude 渠道承接:OpenAI 口径 1 次 + Claude 口径 2 次 const result = buildBillingBreakdown( { prompt_tokens: 1000, quota: 11750 }, { model_price: -1, model_ratio: 1, completion_ratio: 1, group_ratio: 0.5, web_search: true, web_search_call_count: 1, web_search_price: 25, claude_web_search_call_count: 2, claude_web_search_price: 10, }, ); assert.deepEqual(keys(result), ['input', 'webSearch', 'claudeWebSearch']); near(result.items[2].quota, (10 / 1000) * 2 * 0.5 * 500000); near(result.estimated, 11750); }); // --------------------------------------------------------------------------- // reconcileDisplayAmounts:界面上逐项相加必须恰好等于合计 // --------------------------------------------------------------------------- const usd = { digits: 6, toDisplay: (quota) => (quota / 500000) * 1 }; const units = (value, digits) => Math.round(value * 10 ** digits); const addsUp = (result, digits) => result.rows.reduce((sum, row) => sum + units(row.amount, digits), 0) + units(result.adjustment, digits) === units(result.total, digits); test('displayed rows plus the adjustment equal the displayed total', () => { // 截图那条记录:三行展示金额之和是 0.003433,实扣 0.003432 const breakdown = buildBillingBreakdown( { prompt_tokens: 88630, completion_tokens: 153, quota: 1716 }, { model_ratio: 0.575, completion_ratio: 4, cache_tokens: 87168, cache_ratio: 0.02, group_ratio: 0.782, model_price: -1, }, ); const result = reconcileDisplayAmounts(breakdown, usd); assert.deepEqual( result.rows.map((row) => row.amount), [0.001315, 0.001568, 0.00055], ); assert.equal(result.total, 0.003432); assert.equal(result.adjustment, -0.000001); assert.ok(addsUp(result, 6)); }); test('no adjustment when the rows already add up', () => { const breakdown = buildBillingBreakdown( { quota: 20000 }, { model_price: 0.05, group_ratio: 0.8 }, ); const result = reconcileDisplayAmounts(breakdown, usd); assert.equal(result.adjustment, 0); assert.equal(result.total, 0.04); }); test('a minimum charge lands in the adjustment', () => { // 0.2 个额度单位的用量按最低 1 个单位扣:行金额展示为 0,差额全在调整项 const breakdown = buildBillingBreakdown( { prompt_tokens: 10, quota: 1 }, { model_price: -1, model_ratio: 0.02, completion_ratio: 1, group_ratio: 1 }, ); const result = reconcileDisplayAmounts(breakdown, usd); assert.equal(result.rows[0].amount, 0); assert.equal(result.adjustment, 0.000002); assert.ok(addsUp(result, 6)); }); test('reconciles in quota units and with a currency rate', () => { const breakdown = buildBillingBreakdown( { prompt_tokens: 88630, completion_tokens: 153, quota: 1716 }, { model_ratio: 0.575, completion_ratio: 4, cache_tokens: 87168, cache_ratio: 0.02, group_ratio: 0.782, model_price: -1, }, ); // 按额度展示:两位小数,合计是整数个额度单位 const inUnits = reconcileDisplayAmounts(breakdown, { digits: 2, toDisplay: (quota) => quota, }); assert.deepEqual( inUnits.rows.map((row) => row.amount), [657.39, 783.9, 275.19], ); assert.equal(inUnits.total, 1716); assert.equal(inUnits.adjustment, -0.48); assert.ok(addsUp(inUnits, 2)); // 人民币展示(汇率 7.3) const cny = reconcileDisplayAmounts(breakdown, { digits: 6, toDisplay: (quota) => (quota / 500000) * 7.3, }); assert.equal(cny.total, 0.025054); assert.ok(addsUp(cny, 6)); }); test('a positive charge never displays as zero', () => { // 展示汇率极小时实扣会被舍成 0:与页面其余位置一样按最小展示单位显示 const breakdown = buildBillingBreakdown( { prompt_tokens: 1000, quota: 1000 }, { model_price: -1, model_ratio: 1, completion_ratio: 1, group_ratio: 1 }, ); const result = reconcileDisplayAmounts(breakdown, { digits: 6, toDisplay: (quota) => (quota / 500000) * 0.0001, }); assert.equal(result.total, 0.000001); assert.ok(addsUp(result, 6)); }); test('rows always add up across random logs', () => { // 固定种子的线性同余:失败时可复现 let seed = 20261124; const random = () => { seed = (seed * 1664525 + 1013904223) % 4294967296; return seed / 4294967296; }; const pick = (list) => list[Math.floor(random() * list.length)]; let checked = 0; for (let i = 0; i < 2000; i += 1) { const cache = Math.floor(random() * 50000); const prompt = cache + Math.floor(random() * 5000); const completion = Math.floor(random() * 3000); const other = { model_price: -1, model_ratio: pick([0.0375, 0.25, 0.575, 1.5, 7.5]), completion_ratio: pick([1, 2, 4, 5]), cache_tokens: cache, cache_ratio: pick([0.02, 0.1, 0.5]), group_ratio: pick([0.5, 0.782, 1, 1.3]), }; const weighted = (prompt - cache + cache * other.cache_ratio + completion * other.completion_ratio) * other.model_ratio * other.group_ratio; // 与后端一致:四舍五入到 1 个额度单位,有费用至少扣 1 个单位 const quota = weighted > 0 ? Math.max(1, Math.round(weighted)) : 0; const breakdown = buildBillingBreakdown( { prompt_tokens: prompt, completion_tokens: completion, quota }, other, ); if (!breakdown) continue; checked += 1; for (const display of [ usd, { digits: 6, toDisplay: (q) => (q / 500000) * 7.3 }, { digits: 2, toDisplay: (q) => q }, ]) { const result = reconcileDisplayAmounts(breakdown, display); assert.ok(addsUp(result, display.digits), `第 ${i} 条对不平`); // 调整项只应是取整量级:不超过「行数 × 半个最小展示单位 + 1 个额度单位」 const bound = (result.rows.length * 0.5 + 0.5) / 10 ** display.digits + Math.abs(display.toDisplay(1)); assert.ok( Math.abs(result.adjustment) <= bound + 1e-12, `第 ${i} 条调整项过大`, ); } } assert.ok(checked > 1500, `随机样本大多应可拆分,实际 ${checked}`); });