a10aa6bda3
现状纠偏(写死免得下次重研):桌面壳加载的是 frontend-vue 的 Vue SPA(app_client/blueprints/main.py:52 供产物、 config.py:96 WEB_HOME_URL 直载),所谓"客户端密钥设置"不在 Python 里;而密钥明文只在后端(前端手里只有掩码), 所以余额必须由后端查、前端不自打上游——本面只消费 GET /api/user-secrets/llm-balance。 - 新建 SecretBalanceRow.vue:根元素就是 <div class="secret-meta">,保住既有挂载测试按 .secret-meta 取数的断言不破; LLM 卡插一行、代理卡那 8 行 div 换成同一组件 ⇒ 两卡同形自动达成。面板 763→789 行,在 800 报警线内, 可读棘轮基线不用动(该文件本就未点名)。 - 新建 shared/utils/llm-balance-text.ts(零依赖纯函数,node --test 直测)+ shared/composables/useLlmBalance.ts (按 moduleKey 存 items、三态文案、available=false 原样透出后端 message)。不引任何汇率乘算:绊线 tests/llm-balance-text.test.ts 里钉住源码不得出现 7.2/per_usd 类字样——换算只在上游 credits.per_usd 里。 - loadLlmBalance() 移出 if (props.showProxy):原形状下 LLM 卡永远空白。刷新时机=挂载、保存该模块后、清空后、 「检测已存值」后;⚠ 刻意不在「检测输入框未保存值」后刷——checkMyApiSecret(moduleKey,value) 探的是没落库的那把 key,而余额读服务端已存密钥,刷出来是上一把 key 的钱挂在新渠道名下=串档形状(同一格绊线盯着这条)。 - 不动 ProxyBalanceAlert.vue / utils/proxy-balance-alert.ts:没要求告警,引阈值就是自造口径。 - 复用 jikip 那套 VO 的 surplus/scope/low 语义不 borrow 给 LLM(铁律 25):types/modules/user-secret.ts 里 新类型与 :48-59 的 jikip VO 并置但互不相通。 测试:tests/llm-balance-text.test.ts 10 例(含源码绊线:面板行数<800、useLlmBalance 无汇率乘算、检测输入值路径 不触发 loadLlmBalance)+ tests/mount/密钥面板余额显示.mount.test.ts 11 例(两 LLM 卡各有 .secret-meta;龙抬头行 含 ¥+「龙抬头」、贞贞行含「不提供余额查询」且不含 ¥;两卡夹具给不同数字并交叉断言不互串;代理余量文案未变); endpoints.test.ts 金样加一行;两支既有挂载夹具补 fetchLlmBalanceSafely 默认值。 catch-silence 棘轮:useLlmBalance.ts 那一枚 catch 的**首条语句是界面可见出口**(清空该 moduleKey 的展示), console.warn 留在其后——按"首条即 log 才算静默"的口径它不该入账,实测入账了就把 blocks 311→312 如实登记并写明 理由,不删那个 catch(删了就是把失败读成"没有这一项")。vue-tsc rc=0、node --test 1018 例、vitest 66 例。
141 lines
6.0 KiB
TypeScript
141 lines
6.0 KiB
TypeScript
/**
|
||
* LLM 余额显示口径的纯函数判据(B83:龙抬头余额要显示人民币)。
|
||
*
|
||
* 靶子是「两套口径挑了哪一套」:上游同一份响应里美元串与 ⚡(人民币)数字并存,
|
||
* 挑错那一套不会报错、只会安静地差 7.2 倍。所以除了正向读数,这里还钉两条反向的:
|
||
* ① 美元形状不许被渲染成 `¥…`;② 源码里不许出现汇率乘算(自己乘一次=自造一套口径)。
|
||
*
|
||
* 跑法:`cd crawler-plugin/frontend-vue && npm test`
|
||
*/
|
||
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readFileSync } from 'node:fs'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
import {
|
||
formatYuan,
|
||
llmBalanceText,
|
||
llmBalanceUnavailableText,
|
||
llmBalanceUpdatedAt,
|
||
} from '../src/shared/utils/llm-balance-text.ts'
|
||
import type { UserLlmBalance } from '../src/shared/api/types/modules/user-secret.ts'
|
||
|
||
function item(over: Partial<UserLlmBalance> = {}): UserLlmBalance {
|
||
return {
|
||
moduleKey: 'appearance-patent',
|
||
moduleLabel: '外观专利密钥',
|
||
channel: 'longtaitou',
|
||
channelLabel: '龙抬头',
|
||
available: true,
|
||
balance: '88.888882',
|
||
availableBalance: '85.288882',
|
||
frozen: '3.6',
|
||
unit: 'CREDIT',
|
||
message: null,
|
||
checkedAt: '2026-11-05T10:30:00',
|
||
...over,
|
||
}
|
||
}
|
||
|
||
test('人民币读数只吃 credits 的数值并保留两位', () => {
|
||
assert.equal(formatYuan('88.888882'), '¥88.89')
|
||
assert.equal(formatYuan('0'), '¥0.00')
|
||
assert.equal(formatYuan(null), '')
|
||
assert.equal(formatYuan(''), '')
|
||
})
|
||
|
||
test('美元形状绝不渲染成人民币', () => {
|
||
// 顶层那串是美元:解析不出数字时原样回显,不套 ¥ 符号——套了就等于宣称它是人民币
|
||
assert.equal(formatYuan('$12.345678'), '$12.345678')
|
||
assert.ok(!formatYuan('$12.345678').startsWith('¥'))
|
||
})
|
||
|
||
test('有数时的整行读数带渠道、总额与可用', () => {
|
||
const text = llmBalanceText(item())
|
||
assert.match(text, /龙抬头余额(人民币)/)
|
||
assert.match(text, /¥88\.89/)
|
||
assert.match(text, /可用 ¥85\.29/)
|
||
assert.match(text, /冻结 ¥3\.60/)
|
||
// 美元那两个数一旦混进来,读数就会差 7.2 倍还看着正常
|
||
assert.ok(!text.includes('12.34'), `读数里不该出现美元串:${text}`)
|
||
assert.ok(!text.includes('11.84'), `读数里不该出现美元串:${text}`)
|
||
})
|
||
|
||
test('缺可用与冻结时不补零', () => {
|
||
const text = llmBalanceText(item({ availableBalance: null, frozen: null }))
|
||
assert.match(text, /总 ¥88\.89/)
|
||
assert.ok(!text.includes('可用 ¥0'), `缺字段不许读成 0:${text}`)
|
||
assert.ok(!text.includes('冻结 ¥0'), `缺字段不许读成 0:${text}`)
|
||
})
|
||
|
||
test('贞贞那一档说不提供查询,且一个金额都不给', () => {
|
||
const unsupported = item({
|
||
available: false,
|
||
channel: 't8star',
|
||
channelLabel: '贞贞',
|
||
balance: null,
|
||
availableBalance: null,
|
||
frozen: null,
|
||
unit: null,
|
||
message: '贞贞渠道不提供余额查询;用量请看 ai.t8star.org 自己的控制台。',
|
||
})
|
||
assert.equal(llmBalanceText(unsupported), '')
|
||
const text = llmBalanceUnavailableText(unsupported)
|
||
assert.match(text, /不提供余额查询/)
|
||
assert.ok(!text.includes('¥'), `查不到时不许出现金额:${text}`)
|
||
assert.ok(!text.includes('0.00'), `查不到不许读成欠费:${text}`)
|
||
})
|
||
|
||
test('后端没给原因时才兜底,且兜底句说明不是欠费', () => {
|
||
const text = llmBalanceUnavailableText(item({ available: false, message: null }))
|
||
assert.match(text, /龙抬头余额这次没查到/)
|
||
assert.match(text, /不是欠费/)
|
||
})
|
||
|
||
test('整格都没取到时用本地渠道名兜底(服务端连渠道都没回)', () => {
|
||
assert.match(llmBalanceUnavailableText(null, '贞贞'), /贞贞余额这次没查到/)
|
||
// item 在的时候一律以服务端回传的渠道名为准:库里那把 key 是哪家,钱就是哪家的
|
||
assert.match(llmBalanceUnavailableText(item({ available: false, message: '密钥失效' })), /龙抬头余额:密钥失效/)
|
||
})
|
||
|
||
test('原因已以渠道名开头时不叠第二遍前缀', () => {
|
||
const text = llmBalanceUnavailableText(item({
|
||
available: false, balance: null, message: '龙抬头没有返回人民币(⚡)口径的余额块',
|
||
}))
|
||
assert.equal(text, '龙抬头没有返回人民币(⚡)口径的余额块')
|
||
})
|
||
|
||
test('取数时刻只到分钟,脏值不显示', () => {
|
||
assert.equal(llmBalanceUpdatedAt(item()), '10:30')
|
||
assert.equal(llmBalanceUpdatedAt(item({ checkedAt: null })), '')
|
||
assert.equal(llmBalanceUpdatedAt(item({ checkedAt: '不是时间' })), '')
|
||
})
|
||
|
||
test('两张卡各回各的钱,不互串', () => {
|
||
const ap = llmBalanceText(item({ balance: '88.888882', availableBalance: '85.288882' }))
|
||
const sa = llmBalanceText(item({
|
||
moduleKey: 'similar-asin', balance: '12.5', availableBalance: '10', frozen: null,
|
||
}))
|
||
assert.match(ap, /¥88\.89/)
|
||
assert.match(sa, /¥12\.50/)
|
||
assert.ok(!sa.includes('88.89'), 'A 卡的数不许出现在 B 卡')
|
||
assert.ok(!ap.includes('12.50'), 'B 卡的数不许出现在 A 卡')
|
||
})
|
||
|
||
test('源码绊线:不许自己乘汇率,也不许在面板里再造一套余额判定', () => {
|
||
const read = (path: string) => readFileSync(
|
||
fileURLToPath(new URL(path, import.meta.url)), 'utf8',
|
||
)
|
||
const textModule = read('../src/shared/utils/llm-balance-text.ts')
|
||
const composable = read('../src/shared/composables/useLlmBalance.ts')
|
||
|
||
// 7.2 是上游 credits.per_usd 的当前值,把它写进代码=自造第二套口径(上游一改就静默错)
|
||
for (const source of [textModule, composable]) {
|
||
assert.doesNotMatch(source, /7\.2/, '余额换算必须只用 credits 块,不许出现汇率字面量')
|
||
assert.doesNotMatch(source, /perUsd|per_usd/, '前端不参与口径换算,per_usd 只该在后端判定')
|
||
}
|
||
// 三态判定只有一处:面板按 proxyMetaText/llmMetaText 走,组件不自己判
|
||
const row = read('../src/shared/components/SecretBalanceRow.vue')
|
||
assert.doesNotMatch(row, /available/, '行组件只渲染调用方算好的文案,不再判一次 available')
|
||
})
|