feat(用户端): 密钥设置面板 LLM 两卡加人民币余额行,与代理余量同形
现状纠偏(写死免得下次重研):桌面壳加载的是 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 例。
This commit is contained in:
@@ -39,6 +39,7 @@ const store = vi.hoisted(() => ({
|
||||
saveApiSecret: vi.fn(),
|
||||
subscribeApiSecrets: vi.fn(),
|
||||
fetchProxyBalanceSafely: vi.fn(),
|
||||
fetchLlmBalanceSafely: vi.fn(),
|
||||
}))
|
||||
const secretApi = vi.hoisted(() => ({
|
||||
putMyApiSecret: vi.fn(),
|
||||
@@ -63,6 +64,7 @@ vi.mock('@/shared/utils/api-secret-store', () => ({
|
||||
saveApiSecret: store.saveApiSecret,
|
||||
subscribeApiSecrets: store.subscribeApiSecrets,
|
||||
fetchProxyBalanceSafely: store.fetchProxyBalanceSafely,
|
||||
fetchLlmBalanceSafely: store.fetchLlmBalanceSafely,
|
||||
}))
|
||||
vi.mock('@/shared/api/types/modules/user-secret', () => ({
|
||||
putMyApiSecret: secretApi.putMyApiSecret,
|
||||
@@ -169,6 +171,8 @@ beforeEach(() => {
|
||||
store.fetchProxyBalanceSafely.mockResolvedValue({
|
||||
available: true, surplus: '12', balance: '34', message: null, scope: 'user', low: false, lowReason: null,
|
||||
})
|
||||
// 本文件的靶子是掩码输入的显隐;LLM 余额回空数组即可,不参与任何断言
|
||||
store.fetchLlmBalanceSafely.mockResolvedValue([])
|
||||
secretApi.putMyApiSecret.mockResolvedValue({})
|
||||
secretApi.fetchProxyPlan.mockResolvedValue({ planId: null, jikipUserId: null, configured: false })
|
||||
bridge.readConfig.mockResolvedValue(qingguoConfig())
|
||||
|
||||
@@ -0,0 +1,290 @@
|
||||
/**
|
||||
* 密钥设置面板「余额行」的挂载用例(B83:龙抬头余额显示人民币,形状与代理余量统一)。
|
||||
*
|
||||
* 五组靶子:
|
||||
* ① 两张 LLM 卡各有一行 `.secret-meta`(改回「余额只属于代理卡」即红);
|
||||
* ② 龙抬头那一格给的是**人民币**读数(¥),且美元那两个数一个都不出现;
|
||||
* ③ 贞贞那一格说「不提供余额查询」,一个金额都不给(不借别家的数、也不显示 0);
|
||||
* ④ 两卡各回各的钱,交叉不串;代理卡余量文案在抽组件后**逐字未变**;
|
||||
* ⑤ 刷新时机:只有读服务端已存密钥的那几次会刷;「检测输入值(未保存)」不刷(那时刷的是上一把 key 的钱)。
|
||||
*
|
||||
* ⚠ 用例里的数字一律假值,真实密钥与真实余额不得进任何跟踪件。
|
||||
*
|
||||
* 跑法:`npx vitest run tests/mount/密钥面板余额显示.mount.test.ts`
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||||
|
||||
import ApiSecretSettingsPanel from '@/shared/components/ApiSecretSettingsPanel.vue'
|
||||
|
||||
const UID = '1301'
|
||||
|
||||
const message = vi.hoisted(() => ({
|
||||
error: vi.fn(),
|
||||
warning: vi.fn(),
|
||||
success: vi.fn(),
|
||||
info: vi.fn(),
|
||||
}))
|
||||
const store = vi.hoisted(() => ({
|
||||
checkApiSecret: vi.fn(),
|
||||
clearApiSecret: vi.fn(),
|
||||
getServerProxySnapshot: vi.fn(),
|
||||
getStoredApiSecretSnapshot: vi.fn(),
|
||||
listApiSecretModules: vi.fn(),
|
||||
loadApiSecrets: vi.fn(),
|
||||
saveApiSecret: vi.fn(),
|
||||
subscribeApiSecrets: vi.fn(),
|
||||
fetchProxyBalanceSafely: vi.fn(),
|
||||
fetchLlmBalanceSafely: vi.fn(),
|
||||
}))
|
||||
const secretApi = vi.hoisted(() => ({
|
||||
putMyApiSecret: vi.fn(),
|
||||
deleteMyApiSecret: vi.fn(),
|
||||
fetchProxyPlan: vi.fn(),
|
||||
saveProxyPlan: vi.fn(),
|
||||
clearProxyPlan: vi.fn(),
|
||||
}))
|
||||
const bridge = vi.hoisted(() => ({
|
||||
readConfig: vi.fn(),
|
||||
saveConfig: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('element-plus', () => ({ ElMessage: message }))
|
||||
vi.mock('@/shared/utils/api-secret-store', () => ({
|
||||
checkApiSecret: store.checkApiSecret,
|
||||
clearApiSecret: store.clearApiSecret,
|
||||
getServerProxySnapshot: store.getServerProxySnapshot,
|
||||
getStoredApiSecretSnapshot: store.getStoredApiSecretSnapshot,
|
||||
listApiSecretModules: store.listApiSecretModules,
|
||||
loadApiSecrets: store.loadApiSecrets,
|
||||
saveApiSecret: store.saveApiSecret,
|
||||
subscribeApiSecrets: store.subscribeApiSecrets,
|
||||
fetchProxyBalanceSafely: store.fetchProxyBalanceSafely,
|
||||
fetchLlmBalanceSafely: store.fetchLlmBalanceSafely,
|
||||
}))
|
||||
vi.mock('@/shared/api/types/modules/user-secret', () => ({
|
||||
putMyApiSecret: secretApi.putMyApiSecret,
|
||||
deleteMyApiSecret: secretApi.deleteMyApiSecret,
|
||||
fetchProxyPlan: secretApi.fetchProxyPlan,
|
||||
saveProxyPlan: secretApi.saveProxyPlan,
|
||||
clearProxyPlan: secretApi.clearProxyPlan,
|
||||
}))
|
||||
vi.mock('@/shared/bridges/pywebview', () => ({
|
||||
getPywebviewApi: () => ({ read_config: bridge.readConfig, save_config: bridge.saveConfig }),
|
||||
}))
|
||||
|
||||
function emptySnapshot() {
|
||||
return {
|
||||
value: '', masked: '', channel: '', exists: false, locked: false,
|
||||
checkStatus: 'unknown', checkCode: '', checkMessage: '',
|
||||
checkLatencyMs: null, checkedAt: null, updatedAt: null,
|
||||
}
|
||||
}
|
||||
|
||||
function llmItem(over: Record<string, unknown> = {}) {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
async function mountPanel() {
|
||||
wrapper = mount(ApiSecretSettingsPanel)
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
return wrapper!
|
||||
}
|
||||
|
||||
/** 前两张是 LLM 卡(按 modules 清单顺序),最后一张固定是代理卡。 */
|
||||
function llmCards(panel: VueWrapper) {
|
||||
return panel.findAll('.secret-card').slice(0, 2)
|
||||
}
|
||||
|
||||
function proxyCard(panel: VueWrapper) {
|
||||
const cards = panel.findAll('.secret-card')
|
||||
return cards[cards.length - 1]
|
||||
}
|
||||
|
||||
function cardByModule(panel: VueWrapper, moduleKey: string) {
|
||||
const index = ['appearance-patent', 'similar-asin'].indexOf(moduleKey)
|
||||
return llmCards(panel)[index]
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
localStorage.setItem('uid', UID)
|
||||
vi.clearAllMocks()
|
||||
store.listApiSecretModules.mockReturnValue([
|
||||
{ moduleKey: 'appearance-patent', moduleLabel: '外观专利密钥' },
|
||||
{ moduleKey: 'similar-asin', moduleLabel: '货源查询密钥' },
|
||||
])
|
||||
store.getStoredApiSecretSnapshot.mockReturnValue(emptySnapshot())
|
||||
store.getServerProxySnapshot.mockReturnValue(null)
|
||||
store.subscribeApiSecrets.mockReturnValue(() => undefined)
|
||||
store.loadApiSecrets.mockResolvedValue('complete')
|
||||
store.checkApiSecret.mockResolvedValue({ moduleKey: 'appearance-patent', checkStatus: 'passed', checkCode: 'ok', checkMessage: '连通正常', checkLatencyMs: 120, checkedAt: null, viaProxy: false })
|
||||
store.fetchProxyBalanceSafely.mockResolvedValue({
|
||||
available: true, surplus: '12', balance: '34', message: null, scope: 'user', low: false, lowReason: null,
|
||||
})
|
||||
// AP=龙抬头有钱、SA=贞贞查不了:这正是现网两把密钥最常见的一有一无形状
|
||||
store.fetchLlmBalanceSafely.mockResolvedValue([
|
||||
llmItem(),
|
||||
llmItem({
|
||||
moduleKey: 'similar-asin', moduleLabel: '货源查询密钥',
|
||||
channel: 't8star', channelLabel: '贞贞',
|
||||
available: false, balance: null, availableBalance: null, frozen: null, unit: null,
|
||||
message: '贞贞渠道不提供余额查询;用量请看 ai.t8star.org 自己的控制台。',
|
||||
}),
|
||||
])
|
||||
bridge.readConfig.mockResolvedValue({ proxy_users: {} })
|
||||
bridge.saveConfig.mockResolvedValue(undefined)
|
||||
secretApi.putMyApiSecret.mockResolvedValue({})
|
||||
secretApi.deleteMyApiSecret.mockResolvedValue({})
|
||||
secretApi.fetchProxyPlan.mockResolvedValue({ planId: null, jikipUserId: null, configured: false })
|
||||
secretApi.saveProxyPlan.mockResolvedValue({})
|
||||
secretApi.clearProxyPlan.mockResolvedValue({})
|
||||
})
|
||||
|
||||
let wrapper: VueWrapper | null = null
|
||||
|
||||
afterEach(() => {
|
||||
wrapper?.unmount()
|
||||
wrapper = null
|
||||
})
|
||||
|
||||
describe('余额行落在两张 LLM 卡上', () => {
|
||||
it('两张卡各有一行 secret-meta(余额不是代理卡的专属)', async () => {
|
||||
const panel = await mountPanel()
|
||||
|
||||
expect(llmCards(panel)).toHaveLength(2)
|
||||
for (const card of llmCards(panel)) {
|
||||
expect(card.find('.secret-meta').exists(), 'LLM 卡必须有余额行').toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
it('龙抬头那一格给人民币读数,美元那两条数字一个都不出现', async () => {
|
||||
const panel = await mountPanel()
|
||||
const meta = cardByModule(panel, 'appearance-patent').find('.secret-meta').text()
|
||||
|
||||
expect(meta).toContain('龙抬头余额(人民币)')
|
||||
expect(meta).toContain('¥88.89')
|
||||
expect(meta).toContain('可用 ¥85.29')
|
||||
expect(meta).toContain('更新于 10:30')
|
||||
expect(meta, '美元串进了读数就是差 7.2 倍的假数').not.toContain('12.34')
|
||||
expect(meta).not.toContain('$')
|
||||
})
|
||||
|
||||
it('贞贞那一格说不提供查询,且一个金额都不给', async () => {
|
||||
const panel = await mountPanel()
|
||||
const meta = cardByModule(panel, 'similar-asin').find('.secret-meta').text()
|
||||
|
||||
expect(meta).toContain('不提供余额查询')
|
||||
expect(meta).not.toContain('¥')
|
||||
expect(meta, '查不到不许读成欠费').not.toContain('0.00')
|
||||
})
|
||||
|
||||
it('两卡各回各的钱,A 的数字不许出现在 B 行', async () => {
|
||||
store.fetchLlmBalanceSafely.mockResolvedValue([
|
||||
llmItem({ balance: '88.888882', availableBalance: '85.288882' }),
|
||||
llmItem({
|
||||
moduleKey: 'similar-asin', moduleLabel: '货源查询密钥',
|
||||
balance: '12.5', availableBalance: '10', frozen: null,
|
||||
}),
|
||||
])
|
||||
const panel = await mountPanel()
|
||||
|
||||
expect(cardByModule(panel, 'appearance-patent').find('.secret-meta').text()).toContain('¥88.89')
|
||||
expect(cardByModule(panel, 'similar-asin').find('.secret-meta').text()).toContain('¥12.50')
|
||||
expect(cardByModule(panel, 'similar-asin').find('.secret-meta').text()).not.toContain('88.89')
|
||||
})
|
||||
|
||||
it('抽组件之后代理余量那一行逐字未变', async () => {
|
||||
const panel = await mountPanel()
|
||||
|
||||
// 本夹具的本机配置没有青果账密 ⇒ 供应商走默认的极客(`密钥面板青果检测` 那份才是青果形状)
|
||||
expect(proxyCard(panel).find('.secret-meta').text()).toContain('极客余量(你的套餐)')
|
||||
expect(proxyCard(panel).find('.secret-meta').text()).toContain('套餐IP 12')
|
||||
})
|
||||
})
|
||||
|
||||
describe('刷新时机:只读服务端已存密钥的那几次', () => {
|
||||
it('打开面板刷一次,两张卡一起要(不带 moduleKey)', async () => {
|
||||
await mountPanel()
|
||||
|
||||
expect(store.fetchLlmBalanceSafely).toHaveBeenCalledTimes(1)
|
||||
expect(store.fetchLlmBalanceSafely).toHaveBeenCalledWith(undefined)
|
||||
})
|
||||
|
||||
it('浏览器形态(showProxy=false)也要刷——余额不属代理卡', async () => {
|
||||
const panel = mount(ApiSecretSettingsPanel, { props: { showProxy: false } })
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
|
||||
expect(store.fetchLlmBalanceSafely).toHaveBeenCalledTimes(1)
|
||||
expect(llmCards(panel)[0].find('.secret-meta').text()).toContain('龙抬头余额')
|
||||
})
|
||||
|
||||
it('点某一格的刷新只刷那一格(带 moduleKey,不动另一格)', async () => {
|
||||
const panel = await mountPanel()
|
||||
store.fetchLlmBalanceSafely.mockClear()
|
||||
|
||||
await cardByModule(panel, 'similar-asin').find('.meta-refresh').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(store.fetchLlmBalanceSafely).toHaveBeenCalledTimes(1)
|
||||
expect(store.fetchLlmBalanceSafely).toHaveBeenCalledWith('similar-asin')
|
||||
})
|
||||
|
||||
it('检测输入值(未保存)不刷余额:那时库里还是上一把 key', async () => {
|
||||
const panel = await mountPanel()
|
||||
const card = cardByModule(panel, 'appearance-patent')
|
||||
await card.find('.secret-input').setValue('sk-刚粘贴还没保存')
|
||||
await flushPromises()
|
||||
store.fetchLlmBalanceSafely.mockClear()
|
||||
|
||||
await card.find('.check-btn').trigger('click')
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
|
||||
expect(store.checkApiSecret).toHaveBeenCalledWith('appearance-patent', 'sk-刚粘贴还没保存', expect.anything())
|
||||
expect(store.fetchLlmBalanceSafely, '刷了就是拿上一把 key 的钱挂在这一行').not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('检测配置密钥(已存值)顺带刷那一格', async () => {
|
||||
const panel = await mountPanel()
|
||||
const card = cardByModule(panel, 'appearance-patent')
|
||||
store.fetchLlmBalanceSafely.mockClear()
|
||||
|
||||
await card.find('.check-btn').trigger('click')
|
||||
await flushPromises()
|
||||
await flushPromises()
|
||||
|
||||
expect(store.checkApiSecret).toHaveBeenCalledWith('appearance-patent', undefined, expect.anything())
|
||||
expect(store.fetchLlmBalanceSafely).toHaveBeenCalledTimes(1)
|
||||
expect(store.fetchLlmBalanceSafely).toHaveBeenCalledWith('appearance-patent')
|
||||
})
|
||||
|
||||
it('接口整个失败时清空数字、只留「没查到」(不许留着上一把 key 的钱)', async () => {
|
||||
store.fetchLlmBalanceSafely.mockResolvedValue([llmItem()])
|
||||
const panel = await mountPanel()
|
||||
expect(cardByModule(panel, 'appearance-patent').find('.secret-meta').text()).toContain('¥88.89')
|
||||
|
||||
store.fetchLlmBalanceSafely.mockRejectedValue(new Error('网络断了'))
|
||||
await cardByModule(panel, 'appearance-patent').find('.meta-refresh').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
const meta = cardByModule(panel, 'appearance-patent').find('.secret-meta').text()
|
||||
expect(meta).not.toContain('88.89')
|
||||
expect(meta).toContain('没查到')
|
||||
})
|
||||
})
|
||||
@@ -42,6 +42,7 @@ const store = vi.hoisted(() => ({
|
||||
saveApiSecret: vi.fn(),
|
||||
subscribeApiSecrets: vi.fn(),
|
||||
fetchProxyBalanceSafely: vi.fn(),
|
||||
fetchLlmBalanceSafely: vi.fn(),
|
||||
}))
|
||||
const secretApi = vi.hoisted(() => ({
|
||||
putMyApiSecret: vi.fn(),
|
||||
@@ -66,6 +67,7 @@ vi.mock('@/shared/utils/api-secret-store', () => ({
|
||||
saveApiSecret: store.saveApiSecret,
|
||||
subscribeApiSecrets: store.subscribeApiSecrets,
|
||||
fetchProxyBalanceSafely: store.fetchProxyBalanceSafely,
|
||||
fetchLlmBalanceSafely: store.fetchLlmBalanceSafely,
|
||||
}))
|
||||
vi.mock('@/shared/api/types/modules/user-secret', () => ({
|
||||
putMyApiSecret: secretApi.putMyApiSecret,
|
||||
@@ -148,6 +150,8 @@ beforeEach(() => {
|
||||
store.fetchProxyBalanceSafely.mockResolvedValue({
|
||||
available: true, surplus: '12', balance: '34', message: null, scope: 'user', low: false, lowReason: null,
|
||||
})
|
||||
// 本文件的靶子是代理那一张卡;LLM 余额回空数组即可(面板按「没查到」显示,不参与这里的任何断言)
|
||||
store.fetchLlmBalanceSafely.mockResolvedValue([])
|
||||
secretApi.fetchProxyPlan.mockResolvedValue({ planId: null, jikipUserId: null, configured: false })
|
||||
bridge.readConfig.mockResolvedValue(qingguoConfig())
|
||||
bridge.saveConfig.mockResolvedValue(undefined)
|
||||
|
||||
Reference in New Issue
Block a user