f6aa04d0f2
平台自费池由单个 planId/userId 改为 planId:userId 列表逐套餐探测, 套餐余量或账户余额低于阈值时推管理员预警;用户可在密钥设置里为自己 配置多个套餐,巡检跟随密钥检查按用户查余量并推站内通知,欠费状态在 客户端右上角常驻提示(V160 新建用户级套餐表)。 同时把测试里写死的真实代理 key 换成占位串,避免凭证随仓库扩散。
142 lines
4.9 KiB
TypeScript
142 lines
4.9 KiB
TypeScript
/**
|
|
* 代理套餐低余量常驻警告的组件挂载用例:判的是运行时行为,不是读源码形状。
|
|
*
|
|
* 需求原话:「如果欠费了就一直在客户端右上角有个弹窗警告」。
|
|
* 三组靶子:
|
|
* ① 用户自己的套餐 low=true 时,右上角必须出现警告且文案带原因;
|
|
* ② 未配套餐/余量恢复/查询失败时不许误弹,恢复后由轮询自动收起;
|
|
* ③ 失败不能把上一轮的警告清掉(网络抖一下不该骗用户说欠费已解决)。
|
|
*
|
|
* 跑法:`npx vitest run tests/mount/proxy-balance-alert.mount.test.ts`
|
|
*/
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
|
|
|
import ProxyBalanceAlert from '@/shared/components/ProxyBalanceAlert.vue'
|
|
import { AUTH_TOKEN_KEY } from '@/shared/auth/session-keys'
|
|
import { PROXY_PLAN_CHANGED_EVENT } from '@/shared/utils/proxy-balance-alert'
|
|
|
|
const api = vi.hoisted(() => ({
|
|
fetchProxyPlan: vi.fn(),
|
|
fetchProxyBalanceSafely: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/shared/api/types/modules/user-secret', () => ({
|
|
fetchProxyPlan: api.fetchProxyPlan,
|
|
}))
|
|
vi.mock('@/shared/utils/api-secret-store', () => ({
|
|
fetchProxyBalanceSafely: api.fetchProxyBalanceSafely,
|
|
}))
|
|
|
|
let wrapper: VueWrapper | null = null
|
|
|
|
function plan(configured: boolean) {
|
|
return {
|
|
planId: configured ? '20261010593613392231' : null,
|
|
jikipUserId: configured ? '626710989815' : null,
|
|
configured,
|
|
}
|
|
}
|
|
|
|
function balance(over: Record<string, unknown> = {}) {
|
|
return {
|
|
available: true,
|
|
surplus: null,
|
|
balance: '10',
|
|
message: null,
|
|
scope: 'user',
|
|
low: true,
|
|
lowReason: '账户余额已耗尽',
|
|
...over,
|
|
}
|
|
}
|
|
|
|
async function mountAlert() {
|
|
localStorage.setItem(AUTH_TOKEN_KEY, 'token')
|
|
wrapper = mount(ProxyBalanceAlert)
|
|
await flushPromises()
|
|
return wrapper
|
|
}
|
|
|
|
beforeEach(() => {
|
|
localStorage.clear()
|
|
api.fetchProxyPlan.mockReset()
|
|
api.fetchProxyBalanceSafely.mockReset()
|
|
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
})
|
|
|
|
afterEach(() => {
|
|
wrapper?.unmount()
|
|
wrapper = null
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
describe('ProxyBalanceAlert:低余量常驻警告', () => {
|
|
it('用户自己的套餐低余量:右上角常驻警告,文案带服务端给的原因', async () => {
|
|
api.fetchProxyPlan.mockResolvedValue(plan(true))
|
|
api.fetchProxyBalanceSafely.mockResolvedValue(balance())
|
|
await mountAlert()
|
|
|
|
const alert = wrapper!.find('.proxy-balance-alert')
|
|
expect(alert.exists()).toBe(true)
|
|
expect(alert.text()).toContain('代理套餐余额不足')
|
|
expect(alert.text()).toContain('账户余额已耗尽')
|
|
// 没有关闭件:用户点不掉,只能等充值后自动收起
|
|
expect(alert.find('button').exists()).toBe(false)
|
|
})
|
|
|
|
it('没配套餐:直接收起且一次都不查余量,不白打供应商接口', async () => {
|
|
api.fetchProxyPlan.mockResolvedValue(plan(false))
|
|
await mountAlert()
|
|
|
|
expect(wrapper!.find('.proxy-balance-alert').exists()).toBe(false)
|
|
expect(api.fetchProxyBalanceSafely).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('平台套餐/未低余量:普通用户不该看到平台侧的告警', async () => {
|
|
api.fetchProxyPlan.mockResolvedValue(plan(true))
|
|
api.fetchProxyBalanceSafely.mockResolvedValue(balance({ scope: 'platform', low: true }))
|
|
await mountAlert()
|
|
|
|
expect(wrapper!.find('.proxy-balance-alert').exists()).toBe(false)
|
|
})
|
|
|
|
it('余量恢复后回到前台立即刷新:警告自动消失,无需用户手动关闭', async () => {
|
|
api.fetchProxyPlan.mockResolvedValue(plan(true))
|
|
api.fetchProxyBalanceSafely.mockResolvedValue(balance())
|
|
await mountAlert()
|
|
expect(wrapper!.find('.proxy-balance-alert').exists()).toBe(true)
|
|
|
|
api.fetchProxyBalanceSafely.mockResolvedValue(balance({ low: false, lowReason: null }))
|
|
window.dispatchEvent(new Event('focus'))
|
|
await flushPromises()
|
|
|
|
expect(wrapper!.find('.proxy-balance-alert').exists()).toBe(false)
|
|
})
|
|
|
|
it('设置面板保存套餐后广播事件:警告立即按新配置刷新,不用等下一轮轮询', async () => {
|
|
api.fetchProxyPlan.mockResolvedValue(plan(false))
|
|
await mountAlert()
|
|
expect(wrapper!.find('.proxy-balance-alert').exists()).toBe(false)
|
|
|
|
api.fetchProxyPlan.mockResolvedValue(plan(true))
|
|
api.fetchProxyBalanceSafely.mockResolvedValue(balance())
|
|
window.dispatchEvent(new Event(PROXY_PLAN_CHANGED_EVENT))
|
|
await flushPromises()
|
|
|
|
expect(wrapper!.find('.proxy-balance-alert').exists()).toBe(true)
|
|
})
|
|
|
|
it('查询失败不清掉已有警告:网络抖动不能骗用户说欠费已解决', async () => {
|
|
api.fetchProxyPlan.mockResolvedValue(plan(true))
|
|
api.fetchProxyBalanceSafely.mockResolvedValue(balance())
|
|
await mountAlert()
|
|
|
|
api.fetchProxyBalanceSafely.mockRejectedValue(new Error('502 Bad Gateway'))
|
|
window.dispatchEvent(new Event('focus'))
|
|
await flushPromises()
|
|
|
|
expect(wrapper!.find('.proxy-balance-alert').exists()).toBe(true)
|
|
})
|
|
})
|