feat(frontend): 客户端损耗保护弹窗与共享密钥提示
- 30 秒轮询损耗状态;暂停时红色弹窗/横幅并提供「继续任务」 - 共享密钥黄色提示条,按类型去重,不重复刷屏 - 通知铃铛登记 cost_guard / secret_shared 场景
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
/**
|
||||
* 代理/LLM 损耗保护(2026-10-24 需求):
|
||||
* 「异常要立刻弹窗警告在客户端界面上,并暂停任务;排查后手动点击接着跑」。
|
||||
*
|
||||
* 分两层钉:
|
||||
* ① 行为层:暂停键只随 pausedAt 变化、用量行含当日与窗口两段、共享提示按键去重;
|
||||
* ② 源码层:全局挂载、30 秒轮询、暂停弹窗带「继续任务」、resume 走独立接口。
|
||||
*/
|
||||
|
||||
const COMPONENT = 'src/shared/components/CostGuardAlert.vue'
|
||||
const APP = 'src/App.vue'
|
||||
const ENDPOINTS = 'src/shared/api/endpoints.ts'
|
||||
const NOTIFICATION_TYPES = 'src/shared/api/types/modules/notification.ts'
|
||||
|
||||
function read(rel: string): string {
|
||||
return readFileSync(join(process.cwd(), rel), 'utf8')
|
||||
}
|
||||
|
||||
const component = read(COMPONENT)
|
||||
const app = read(APP)
|
||||
|
||||
test('用量行同时给出当日累计与最近窗口增量', async () => {
|
||||
const { usageLine, formatCount } = await import('../src/shared/utils/cost-guard-alert.ts')
|
||||
const status = {
|
||||
paused: true,
|
||||
reason: '代理提取异常',
|
||||
pausedAt: '2026-10-24T10:00:00',
|
||||
today: { proxy: 12345, llmCalls: 678, llmTokens: 900000 },
|
||||
window: { seconds: 120, proxy: 600, llmCalls: 180, llmTokens: 600000 },
|
||||
thresholds: {
|
||||
proxyWindow: 600,
|
||||
llmCallsWindow: 180,
|
||||
llmTokensWindow: 600000,
|
||||
proxyDaily: 8000,
|
||||
llmCallsDaily: 6000,
|
||||
llmTokensDaily: 20000000,
|
||||
sharedAccounts: 3,
|
||||
},
|
||||
sharedWarnings: [],
|
||||
}
|
||||
|
||||
const line = usageLine(status as never)
|
||||
assert.match(line, /今日代理提取 12,345 次/)
|
||||
assert.match(line, /最近 120 秒:代理 600 次/)
|
||||
assert.match(line, /LLM 180 次 \/ 600,000 tokens/)
|
||||
assert.equal(formatCount(undefined), '0')
|
||||
})
|
||||
|
||||
test('同一个暂停事件只弹一次,pausedAt 变化才重新弹', async () => {
|
||||
const { pausedPromptKey } = await import('../src/shared/utils/cost-guard-alert.ts')
|
||||
const base = { paused: true, reason: 'r', pausedAt: '2026-10-24T10:00:00' }
|
||||
const next = { paused: true, reason: 'r', pausedAt: '2026-10-24T11:00:00' }
|
||||
|
||||
assert.equal(pausedPromptKey(base as never), pausedPromptKey({ ...base } as never))
|
||||
assert.notEqual(pausedPromptKey(base as never), pausedPromptKey(next as never))
|
||||
assert.equal(pausedPromptKey({ paused: false, reason: 'r', pausedAt: null } as never), '')
|
||||
})
|
||||
|
||||
test('共享密钥提示按键去重,不按文案去重', async () => {
|
||||
const { sharedWarningKey } = await import('../src/shared/utils/cost-guard-alert.ts')
|
||||
|
||||
assert.equal(
|
||||
sharedWarningKey({ type: 'proxy', moduleLabel: '代理设置', accounts: 3, threshold: 3, message: 'a' }),
|
||||
sharedWarningKey({ type: 'proxy', moduleLabel: '代理设置', accounts: 3, threshold: 3, message: 'b' }),
|
||||
)
|
||||
assert.notEqual(
|
||||
sharedWarningKey({ type: 'llm', moduleLabel: '代理设置', accounts: 3, threshold: 3, message: 'a' }),
|
||||
sharedWarningKey({ type: 'proxy', moduleLabel: '代理设置', accounts: 3, threshold: 3, message: 'a' }),
|
||||
)
|
||||
})
|
||||
|
||||
test('全局挂载并在暂停时提供「继续任务」', () => {
|
||||
assert.ok(app.includes("import CostGuardAlert from '@/shared/components/CostGuardAlert.vue'"))
|
||||
assert.ok(app.includes('<CostGuardAlert />'), '组件没挂到 App.vue,异常时界面不会弹')
|
||||
|
||||
assert.ok(component.includes('setInterval'), '缺少轮询,用户停在页面上等不到暂停提示')
|
||||
assert.ok(component.includes('COST_GUARD_POLL_MS'))
|
||||
assert.ok(component.includes('fetchCostGuardStatus'))
|
||||
assert.ok(component.includes('resumeCostGuard'))
|
||||
assert.ok(component.includes('ElMessageBox.confirm'))
|
||||
assert.ok(component.includes('继续任务'))
|
||||
assert.ok(component.includes('已完成的任务与数据不受影响'))
|
||||
})
|
||||
|
||||
test('接口与通知场景已登记', () => {
|
||||
const endpoints = read(ENDPOINTS)
|
||||
assert.ok(endpoints.includes("status: '/api/user/cost-guard/status'"))
|
||||
assert.ok(endpoints.includes("resume: '/api/user/cost-guard/resume'"))
|
||||
|
||||
const notification = read(NOTIFICATION_TYPES)
|
||||
assert.ok(notification.includes("| 'cost_guard'"))
|
||||
assert.ok(notification.includes("| 'secret_shared'"))
|
||||
})
|
||||
Reference in New Issue
Block a user