/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ // 缓存创建按上游实际返回分三桶结算:明确的 5m、明确的 1h,以及上游只给总量没给分档、 // 按「默认档位」计价的未分档桶。界面上少露一桶,用户拿 token × 倍率就加不出账单金额。 // // 这些源码断言钉的是「哪些字段进了界面、各自的闸门是什么」——组件要真渲染得拉整套 // Semi + JSX 工具链,而本仓库的前端测试跑的是裸 node --test。 import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import test from 'node:test'; const read = (relative) => readFileSync(new URL(relative, import.meta.url), 'utf8').replace(/\r\n/g, '\n'); const table = read('../src/components/table/usage-logs/UsageLogsTable.jsx'); const columnDefs = read( '../src/components/table/usage-logs/UsageLogsColumnDefs.jsx', ); const render = read('../src/helpers/render.jsx'); test('计费详情把未分档桶的 token 与倍率都露出来', () => { assert.match(table, /other\.cache_creation_tokens_unlabeled/); assert.match(table, /other\.cache_creation_ratio_unlabeled/); assert.match(table, /t\('未分档缓存创建'\)/); assert.match(table, /t\('未分档缓存倍率'\)/); }); test('未分档行只在与分档并存时单独成行', () => { // 没有分档时「缓存创建 / 缓存创建倍率」说的就是这一桶,再列一遍是重复 assert.match( table, /const showUnlabeledCacheCreation\s*=\s*\n?\s*hasSplitCacheCreation && unlabeledCacheCreation > 0;/, ); }); test('缓存倍率的闸门是缓存命中,不是缓存创建', () => { // cache_ratio 是读倍率。闸门里混入写入 token 会让「只写不读」的请求挂一个 // 没有对应 token 的缓存倍率,看着像漏算了一笔钱。 assert.match( table, /other\.cache_ratio != null &&\s*\n?\s*other\.cache_tokens > 0 && \{/, ); // 旧的三项相加闸门必须已经消失 assert.doesNotMatch(table, /const cacheTokens\s*=/); }); test('明细列的倍率文案把未分档项带上', () => { assert.match( render, /if \(shouldShowCacheCreationUnlabeled\) \{[\s\S]*?未分档缓存创建: \{\{cacheCreationRatioUnlabeled\}\}/, ); // 只在有分档时才单列,与计费详情同一套口径 assert.match( render, /const shouldShowCacheCreationUnlabeled\s*=\s*\n?\s*hasSplitCacheCreation && cacheCreationTokensUnlabeled > 0;/, ); }); test('renderModelPriceSimple 的未分档实参落在 1h 与 image 之间', () => { // 这是个 18 个位置参数的函数,插在中间的两个参数一旦错位, // 未分档 token 会被当成 image 开关,整列文案静默跑偏。 const signature = render.match( /export function renderModelPriceSimple\(([\s\S]*?)\n\) \{/, ); assert.ok(signature, 'renderModelPriceSimple 签名未找到'); const params = signature[1] .split(',') .map((p) => p.trim().split('=')[0].trim()) .filter(Boolean); assert.deepEqual(params.slice(10, 15), [ 'cacheCreationTokens1h', 'cacheCreationRatio1h', 'cacheCreationTokensUnlabeled', 'cacheCreationRatioUnlabeled', 'image', ]); // Claude 分支按同样的顺序传实参 assert.match( columnDefs, /other\.cache_creation_tokens_1h \|\| 0,[\s\S]{0,160}?other\.cache_creation_tokens_unlabeled \|\| 0,\s*\n\s*other\.cache_creation_ratio_unlabeled \|\|/, ); // 非 Claude 分支没有缓存创建,三桶全部补 0/1,补少了同样错位 assert.match( columnDefs, /other\.cache_ratio \|\| 1\.0,\s*(?:\n\s*0,\s*\n\s*1\.0,){4}\s*\n\s*false,/, ); });