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(component.includes('ElMessage.warning'), '共用密钥提示应通过 ElMessage 通知弹出') assert.ok(!component.includes('cost-guard-alert--shared'), '黄色常驻横幅已下线,样式类不得回流') assert.ok(!component.includes('密钥共用提示'), '界面模板不应再渲染「密钥共用提示」横幅') assert.ok(component.includes("warning.type !== 'proxy'"), 'LLM 密钥共用可接受,弹窗只放行代理 IP 类型') }) test('代理 IP 共用弹窗是精简口语文案,不搬运后端长口径', async () => { const { proxySharedToastText } = await import('../src/shared/utils/cost-guard-alert.ts') const text = proxySharedToastText({ type: 'proxy', moduleLabel: '代理设置', accounts: 3, threshold: 3, message: 'x' }) assert.match(text, /被 3 个账号共用/) assert.match(text, /密钥设置/) assert.ok(!text.includes('阈值'), '不应出现「阈值」这类技术词') assert.ok(!text.includes('归因'), '不应出现后端长口径的书面语') assert.ok(component.includes('proxySharedToastText'), '弹窗必须用前端精简文案,而不是后端 message 原文') }) test('全局挂载并在暂停时提供「继续任务」', () => { assert.ok(app.includes("import CostGuardAlert from '@/shared/components/CostGuardAlert.vue'")) assert.ok(app.includes(''), '组件没挂到 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'")) })