227 lines
10 KiB
TypeScript
227 lines
10 KiB
TypeScript
/**
|
||
* 密钥管理列表页「余额两列」的界面侧判据(2026-11-05 B83)。
|
||
*
|
||
* 读数本身在 `user-secret-model.ts` 里被那颗纯函数尺子钉着;这一支管的是**接上去的形状**:
|
||
* ① 两列都在,且 `<th>` 数=每行 `<td>` 数=空态行的 colspan —— 加列忘改 colspan 是本仓常踩的残行,
|
||
* 历史上专门为此写过提交说明,所以这里把它钉成算术关系而不是逐个数字;
|
||
* ② 有数时 LLM 列给人民币、代理列给次数,两者不混;
|
||
* ③ 整轮没取到时两列都是 `—`,**不是 0**(一格 0 会被读成「这人欠费了」);
|
||
* ④ 非超管少一列「分组」时,上述三个数仍然彼此相等。
|
||
*
|
||
* 跑法:`npx vitest run tests/mount/user-secret-balance-columns.mount.test.ts`
|
||
*/
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||
import { createPinia, setActivePinia, type Pinia } from 'pinia'
|
||
|
||
import UserSecretsPage from '@/pages/account/UserSecretsPage.vue'
|
||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||
|
||
const secretsApi = vi.hoisted(() => ({
|
||
fetchUserSecretList: vi.fn(),
|
||
fetchAdminSecretBalances: vi.fn(),
|
||
checkUserSecret: vi.fn(),
|
||
deleteUserSecret: vi.fn(),
|
||
saveUserSecretModule: vi.fn(),
|
||
clearUserSecretModule: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/user-secrets', () => secretsApi)
|
||
const confirmBatch = vi.hoisted(() => vi.fn())
|
||
vi.mock('@/components/admin-confirm', () => ({ openAdminConfirm: confirmBatch }))
|
||
|
||
it('批量清空点名全部密钥与代理,只调用用户级清空接口', async () => {
|
||
const wrapper = await mountWith([row(7), row(8)])
|
||
confirmBatch.mockResolvedValue(true)
|
||
secretsApi.deleteUserSecret.mockResolvedValue(undefined)
|
||
await wrapper.get('thead .batch-delete-checkbox').setValue(true)
|
||
await wrapper.get('.batch-delete-actions .btn-danger').trigger('click')
|
||
await flushPromises()
|
||
expect(confirmBatch).toHaveBeenCalledWith(expect.objectContaining({ danger: true, message: expect.stringContaining('全部密钥与代理配置') }))
|
||
expect(secretsApi.deleteUserSecret.mock.calls).toEqual([[7], [8]])
|
||
expect(secretsApi.clearUserSecretModule).not.toHaveBeenCalled()
|
||
expect(wrapper.get('.batch-delete-actions .btn-danger').text()).toBe('批量清空(0)')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
function moduleCell(moduleKey: string, over: Record<string, unknown> = {}) {
|
||
return {
|
||
moduleKey, moduleLabel: moduleKey, channel: null,
|
||
masked: 'sk-f****9BgQ', full: '', exists: true, locked: false,
|
||
checkStatus: 'passed', checkCode: 'ok', checkMessage: '连通正常',
|
||
checkLatencyMs: 12, checkedAt: '2026-11-05T10:30:00', updatedAt: '2026-11-05T10:30:00',
|
||
...over,
|
||
}
|
||
}
|
||
|
||
function row(userId: number, over: Record<string, unknown> = {}) {
|
||
return {
|
||
userId,
|
||
username: `user${userId}`,
|
||
groups: [],
|
||
leaderUsername: '',
|
||
similarAsin: moduleCell('similar-asin', { moduleLabel: '货源查询密钥', channel: 'longtaitou' }),
|
||
appearancePatent: moduleCell('appearance-patent', { moduleLabel: '外观专利密钥', channel: 'longtaitou' }),
|
||
proxy: moduleCell('proxy', { moduleLabel: '代理设置', channel: 'jikip' }),
|
||
status: 'passed',
|
||
statusMessage: '三项均检测通过',
|
||
createdAt: '2026-11-01T09:00:00',
|
||
updatedAt: '2026-11-05T10:30:00',
|
||
...over,
|
||
}
|
||
}
|
||
|
||
function llmCells(userId: number) {
|
||
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',
|
||
},
|
||
{
|
||
moduleKey: 'similar-asin', 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',
|
||
},
|
||
].map((cell) => (userId === 7 ? cell : { ...cell, balance: '12.5', availableBalance: '10', frozen: null }))
|
||
}
|
||
|
||
function indexByHeader(wrapper: VueWrapper<Element>, header: string) {
|
||
const headers = wrapper.findAll('thead th').map((th) => th.text().trim())
|
||
const index = headers.findIndex((label) => label.includes(header))
|
||
expect(index, `表头里找不到「${header}」`).toBeGreaterThanOrEqual(0)
|
||
return index
|
||
}
|
||
|
||
function cellText(wrapper: VueWrapper<Element>, header: string, rowIndex = 0): string {
|
||
const index = indexByHeader(wrapper, header)
|
||
return wrapper.findAll('tbody tr')[rowIndex].findAll('td')[index].text().trim()
|
||
}
|
||
|
||
function cellTitle(wrapper: VueWrapper<Element>, header: string, rowIndex = 0): string {
|
||
const index = indexByHeader(wrapper, header)
|
||
const cell = wrapper.findAll('tbody tr')[rowIndex].findAll('td')[index]
|
||
return cell.find('.balance-line').attributes('title') ?? ''
|
||
}
|
||
|
||
let pinia: Pinia
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
|
||
useCompanyTabStore().setActiveId(1)
|
||
})
|
||
|
||
async function mountWith(rowValues: unknown[], balances?: unknown) {
|
||
secretsApi.fetchUserSecretList.mockResolvedValue({
|
||
items: rowValues, total: rowValues.length, page: 1, pageSize: 15, groupOptions: [],
|
||
})
|
||
secretsApi.fetchAdminSecretBalances.mockResolvedValue(
|
||
balances === undefined
|
||
? { items: rowValues.map((item: any) => ({ userId: item.userId, llm: llmCells(item.userId), proxy: { available: true, surplus: '1234', balance: '56', scope: 'user', message: null } })), deniedUserIds: [], timedOutUserIds: [] }
|
||
: balances,
|
||
)
|
||
const wrapper = mount(UserSecretsPage, { global: { plugins: [pinia] } })
|
||
await flushPromises()
|
||
await flushPromises()
|
||
return wrapper
|
||
}
|
||
|
||
describe('余额两列接在密钥管理列表上', () => {
|
||
it('表头数=行内格数=空态 colspan(加列忘改 colspan 直接红)', async () => {
|
||
const wrapper = await mountWith([row(7)])
|
||
const headerCount = wrapper.findAll('thead th').length
|
||
const cellCount = wrapper.findAll('tbody tr')[0].findAll('td').length
|
||
expect(cellCount, '每行的 td 数必须等于表头列数').toBe(headerCount)
|
||
expect(headerCount).toBe(13)
|
||
|
||
const emptyWrapper = await mountWith([])
|
||
const colspan = Number(emptyWrapper.find('tbody td').attributes('colspan'))
|
||
expect(colspan, '空态行的 colspan 必须跟着列数走').toBe(headerCount)
|
||
})
|
||
|
||
it('非超管少一列分组时,三个数仍然彼此相等', async () => {
|
||
useAdminSessionStore().user = { id: 2, username: 'leader', role: 'admin' }
|
||
const wrapper = await mountWith([row(7)])
|
||
const headerCount = wrapper.findAll('thead th').length
|
||
expect(headerCount).toBe(12)
|
||
expect(wrapper.findAll('tbody tr')[0].findAll('td').length).toBe(headerCount)
|
||
|
||
const emptyWrapper = await mountWith([])
|
||
expect(Number(emptyWrapper.find('tbody td').attributes('colspan'))).toBe(headerCount)
|
||
})
|
||
|
||
it('LLM 列给人民币读数,按渠道折成一行(同一个钱不排两遍)', async () => {
|
||
const wrapper = await mountWith([row(7)])
|
||
|
||
expect(cellText(wrapper, 'LLM 余额')).toBe('龙抬头 ¥88.89')
|
||
expect(cellTitle(wrapper, 'LLM 余额')).toContain('外观专利密钥')
|
||
expect(cellTitle(wrapper, 'LLM 余额')).toContain('货源查询密钥')
|
||
expect(cellTitle(wrapper, 'LLM 余额')).toContain('取数于 2026-11-05 10:30')
|
||
})
|
||
|
||
it('代理列给次数并说清是谁的池子,不套 ¥', async () => {
|
||
const wrapper = await mountWith([row(7)])
|
||
|
||
const text = cellText(wrapper, '代理余量')
|
||
expect(text).toContain('你的套餐')
|
||
expect(text).toContain('1234 次')
|
||
expect(text).not.toContain('¥')
|
||
})
|
||
|
||
it('两列各读各的:LLM 列里不出现次数、代理列里不出现金额', async () => {
|
||
const wrapper = await mountWith([row(7)])
|
||
|
||
expect(cellText(wrapper, 'LLM 余额')).not.toMatch(/\d+\s*次/)
|
||
expect(cellText(wrapper, '代理余量')).not.toContain('¥')
|
||
})
|
||
|
||
it('整轮没取到时两列都是 —,一格都不许是 0', async () => {
|
||
const wrapper = await mountWith([row(7)], { items: [], deniedUserIds: [7], timedOutUserIds: [] })
|
||
|
||
expect(cellText(wrapper, 'LLM 余额')).toBe('—')
|
||
expect(cellText(wrapper, '代理余量')).toBe('—')
|
||
expect(cellText(wrapper, 'LLM 余额')).not.toContain('0.00')
|
||
})
|
||
|
||
it('超预算的行说「本轮未应答」,不冒充欠费', async () => {
|
||
const wrapper = await mountWith([row(7)], {
|
||
items: [{ userId: 7, llm: [], proxy: { available: false, surplus: null, balance: null, scope: null, message: '上游未在本轮时间预算内应答,稍后重试;这不代表余量为 0' } }],
|
||
deniedUserIds: [],
|
||
timedOutUserIds: [7],
|
||
})
|
||
|
||
expect(cellText(wrapper, 'LLM 余额')).toBe('—')
|
||
expect(cellText(wrapper, '代理余量')).toBe('查不了')
|
||
expect(cellTitle(wrapper, '代理余量')).toContain('不代表余量为 0')
|
||
})
|
||
|
||
it('余额接口整个失败也不把整页判成加载失败', async () => {
|
||
secretsApi.fetchUserSecretList.mockResolvedValue({ items: [row(7)], total: 1, page: 1, pageSize: 15, groupOptions: [] })
|
||
secretsApi.fetchAdminSecretBalances.mockRejectedValue(new Error('上游 502'))
|
||
const wrapper = mount(UserSecretsPage, { global: { plugins: [pinia] } })
|
||
await flushPromises()
|
||
await flushPromises()
|
||
|
||
expect(wrapper.findAll('tbody tr')).toHaveLength(1)
|
||
expect(wrapper.find('.load-failure-bar').exists(), '列表主数据在,就不该把整页报成加载失败').toBe(false)
|
||
expect(cellText(wrapper, 'LLM 余额')).toBe('—')
|
||
})
|
||
|
||
it('点「刷新余额」重取一轮,不动列表请求', async () => {
|
||
const wrapper = await mountWith([row(7)])
|
||
const listCalls = secretsApi.fetchUserSecretList.mock.calls.length
|
||
|
||
await wrapper.findAll('.filter-actions button').at(-1)!.trigger('click')
|
||
await flushPromises()
|
||
|
||
expect(secretsApi.fetchAdminSecretBalances).toHaveBeenCalledTimes(2)
|
||
expect(secretsApi.fetchUserSecretList).toHaveBeenCalledTimes(listCalls)
|
||
})
|
||
})
|