f6aa04d0f2
平台自费池由单个 planId/userId 改为 planId:userId 列表逐套餐探测, 套餐余量或账户余额低于阈值时推管理员预警;用户可在密钥设置里为自己 配置多个套餐,巡检跟随密钥检查按用户查余量并推站内通知,欠费状态在 客户端右上角常驻提示(V160 新建用户级套餐表)。 同时把测试里写死的真实代理 key 换成占位串,避免凭证随仓库扩散。
100 lines
4.9 KiB
TypeScript
100 lines
4.9 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
|
|
/**
|
|
* 客户端右上角代理余量常驻警告(2026-10-21 需求):
|
|
* 「如果欠费了就一直在客户端右上角有个弹窗警告」。
|
|
*
|
|
* 分两层钉:
|
|
* ① 行为层:warning 判定必须只认「用户自己的套餐 + low」,平台套餐/查询失败一律不弹;
|
|
* ② 源码层:只查用户套餐、定时轮询、无关闭件、挂在 App.vue——缺一条就是静默不弹或误报。
|
|
*/
|
|
|
|
const COMPONENT = 'src/shared/components/ProxyBalanceAlert.vue'
|
|
const APP = 'src/App.vue'
|
|
|
|
function read(rel: string): string {
|
|
return readFileSync(join(process.cwd(), rel), 'utf8')
|
|
}
|
|
|
|
function occurrences(text: string, token: string): number {
|
|
if (!token) return 0
|
|
let count = 0
|
|
let index = text.indexOf(token)
|
|
while (index !== -1) {
|
|
count += 1
|
|
index = text.indexOf(token, index + token.length)
|
|
}
|
|
return count
|
|
}
|
|
|
|
const source = read(COMPONENT)
|
|
const template = source.slice(source.indexOf('<template>'), source.lastIndexOf('</template>'))
|
|
const script = source.slice(source.indexOf('<script setup'), source.indexOf('</script>'))
|
|
|
|
test('判定口径:只有用户自己的套餐 low=true 才弹,平台套餐/未低/查询失败都不弹', async () => {
|
|
const { proxyBalanceWarningReason, PROXY_BALANCE_FALLBACK_REASON } =
|
|
await import('../src/shared/utils/proxy-balance-alert.ts')
|
|
|
|
assert.equal(
|
|
proxyBalanceWarningReason({ available: true, scope: 'user', low: true, lowReason: '账户余额已耗尽' } as never),
|
|
'账户余额已耗尽',
|
|
)
|
|
assert.equal(
|
|
proxyBalanceWarningReason({ available: true, scope: 'user', low: true, lowReason: ' ' } as never),
|
|
PROXY_BALANCE_FALLBACK_REASON,
|
|
'服务端没给原因时也要有兜底文案,不能弹一个空壳',
|
|
)
|
|
|
|
for (const balance of [
|
|
null,
|
|
undefined,
|
|
{ available: false, scope: 'user', low: true, lowReason: '账户余额已耗尽' },
|
|
{ available: true, scope: 'platform', low: true, lowReason: '平台套餐不足' },
|
|
{ available: true, scope: 'user', low: false, lowReason: null },
|
|
{ available: true, scope: 'user', low: null, lowReason: '账户余额已耗尽' },
|
|
]) {
|
|
assert.equal(proxyBalanceWarningReason(balance as never), '', `不该弹:${JSON.stringify(balance)}`)
|
|
}
|
|
})
|
|
|
|
test('先查套餐配置再查余量:没填套餐的用户不白打供应商接口', () => {
|
|
const planIndex = script.indexOf('await fetchProxyPlan()')
|
|
const balanceIndex = script.indexOf('await fetchProxyBalanceSafely()')
|
|
assert.ok(planIndex !== -1, '没读用户套餐配置')
|
|
assert.ok(balanceIndex !== -1, '没查代理余量')
|
|
assert.ok(planIndex < balanceIndex, '必须先确认 configured 再查余量')
|
|
assert.match(script, /if \(!plan\?\.configured\)/, '没配套餐时必须直接收起警告')
|
|
})
|
|
|
|
test('页面常开也会更新:10 分钟轮询 + 回到前台立即刷新,卸载清计时器与监听', () => {
|
|
assert.match(script, /window\.setInterval\(\(\) => void refresh\(\), PROXY_BALANCE_POLL_MS\)/, '没有按固定间隔轮询')
|
|
assert.match(script, /document\.addEventListener\('visibilitychange', refreshWhenVisible\)/, '回到前台没刷新')
|
|
assert.match(script, /window\.addEventListener\('focus', refreshWhenVisible\)/, '窗口获得焦点没刷新')
|
|
assert.match(script, /window\.clearInterval\(timer\)/, '卸载没清计时器')
|
|
assert.match(script, /document\.removeEventListener\('visibilitychange', refreshWhenVisible\)/, '卸载没摘监听')
|
|
assert.equal(occurrences(script, 'warning.value ='), 3, '只允许登录态/未配置/查询结果三处赋值,失败分支不得赋值')
|
|
})
|
|
|
|
test('只能自动消失不能手动关闭:模板里没有任何可点项或关闭开关', () => {
|
|
assert.equal(occurrences(template, '<button'), 0, '常驻警告不放关闭按钮')
|
|
assert.equal(occurrences(template, '@click'), 0, '没有任何点击交互')
|
|
assert.equal(occurrences(source, 'el-dialog'), 0, '对话框会带来关闭件')
|
|
assert.doesNotMatch(source, /show-close|close-on-click-modal|close-on-press-escape|:closable/, '存在关闭开关')
|
|
})
|
|
|
|
test('位置在右上角且避开 56px 顶栏,余量恢复后由轮询自动收起', () => {
|
|
assert.match(source, /position: fixed;/, '必须是全局浮层')
|
|
assert.match(source, /top: 68px;/, '要落在顶栏下方,不能盖住铃铛/用户菜单')
|
|
assert.match(source, /right: 16px;/, '必须在右上角')
|
|
assert.match(template, /v-if="warning"/, '没有内容时不该占位渲染')
|
|
})
|
|
|
|
test('挂在 App.vue:桌面壳与网页共用同一个根组件,不挂就整站看不见', () => {
|
|
const app = read(APP).replace(/<!--[\s\S]*?-->/g, '')
|
|
assert.match(app, /import ProxyBalanceAlert from '@\/shared\/components\/ProxyBalanceAlert\.vue'/, 'App.vue 没引组件')
|
|
assert.match(app, /<ProxyBalanceAlert\s*\/>/, 'App.vue 没渲染组件(注释掉等于没挂)')
|
|
})
|