/** * 密钥管理列表页「检测结果分档」的界面侧判据(2026-11-02)。 * * 现网证据:库里 uid=1 的 proxy 行是 `check_status=error / check_code=channel_busy`, * 数据是对的、界面不认识这个码,于是渲染成「无法判定」——管理员分不清 * 「被自己的采集占着(等 3 分钟再点)」「密钥坏了」「欠费」这三种完全不同的处置。 * * 这一支跑真组件+真 pinia store,API 用假数据顶掉,逐档核对: * ① 药丸文字与配色类名(`is-retry` 是新档,既不红也不黄); * ② 悬停详情里能看到后端 `check_message` 原文(措辞权威在后端,界面只搬运); * ③ 一格上的两颗药丸分别取 `channel`(货源)与 `check_status`/`check_code`(状态)—— * 把状态当渠道渲染(或反过来)会在这里红。 * * 夹具里的凭据一律假值(fakekey/fakepwd/sk-fake),本仓有尺子按长度+高熵认凭据。 * * 跑法:`npx vitest run tests/mount/user-secret-status-band.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) /** 后端那句人话(CHANNEL_BUSY_MESSAGE 的形状):措辞权威在后端,界面只原样透出。 */ const BUSY_MESSAGE = '通道正被你自己的采集占着,等约 3 分钟释放后再点检测' const BALANCE_MESSAGE = '代理服务商余额不足,请充值后重试' const MASKED_PROXY = 'https://overseas.proxy.qg.net' function moduleCell(moduleKey: string, over: Record = {}) { 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-02T15:51:07', updatedAt: '2026-11-02T15:51:07', ...over, } } function row(over: Record = {}) { return { userId: 7, username: 'alice', groups: [], leaderUsername: '', similarAsin: moduleCell('similar-asin', { moduleLabel: '货源查询密钥', channel: 'longtaitou' }), appearancePatent: moduleCell('appearance-patent', { moduleLabel: '外观专利密钥', channel: 't8star' }), proxy: moduleCell('proxy', { moduleLabel: '代理设置', channel: 'qingguo', masked: MASKED_PROXY }), status: 'passed', statusMessage: '三项均检测通过', createdAt: '2026-11-01T09:00:00', updatedAt: '2026-11-02T15:51:07', ...over, } } /** 表头文字定位列(不靠列序数:加列就会全体错位),返回该格里的药丸。 */ function pills(wrapper: VueWrapper, 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 wrapper.findAll('tbody tr')[0].findAll('td')[index].findAll('.wh-pill').map((pill) => ({ text: pill.text().trim(), classes: pill.classes(), title: pill.attributes('title') ?? '', })) } function cellText(wrapper: VueWrapper, header: string): string { const headers = wrapper.findAll('thead th').map((th) => th.text().trim()) const index = headers.findIndex((label) => label.includes(header)) return wrapper.findAll('tbody tr')[0].findAll('td')[index].text() } describe('密钥管理页:检测结果按 check_code 分档渲染', () => { 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(rowValue: Record) { secretsApi.fetchUserSecretList.mockResolvedValue({ items: [rowValue], total: 1, page: 1, pageSize: 15, groupOptions: [] }) const wrapper = mount(UserSecretsPage, { global: { plugins: [pinia] } }) await flushPromises() return wrapper } /** 现网 uid=1 那一行的形状:error + channel_busy。 */ function busyRow() { return row({ status: 'error', statusMessage: '存在无法判定的检测结果', proxy: moduleCell('proxy', { moduleLabel: '代理设置', channel: 'qingguo', masked: MASKED_PROXY, checkStatus: 'error', checkCode: 'channel_busy', checkMessage: BUSY_MESSAGE, }), }) } it('channel_busy 渲染成「无可用通道」+独立配色,不再掉进「无法判定」', async () => { const wrapper = await mountWith(busyRow()) const proxyPills = pills(wrapper, '代理设置') const statusPill = proxyPills[proxyPills.length - 1] expect(statusPill.text).toBe('无可用通道') expect(statusPill.classes).toContain('is-retry') expect(cellText(wrapper, '代理设置')).not.toContain('无法判定') // 供应商那颗药丸还在原位:改的是状态档,不是把货源档挤掉 expect(proxyPills[0].text).toBe('青果') expect(proxyPills[0].classes).toContain('is-source') }) it('整行只剩「等一等」那一族时,行级状态列补成「可重试」', async () => { const wrapper = await mountWith(busyRow()) const rowPill = pills(wrapper, '状态')[0] expect(rowPill.text).toBe('可重试') expect(rowPill.classes).toContain('is-retry') expect(rowPill.text).not.toBe('无法判定') }) it('混进一格「不会自己好」的问题时,行级不许谎报可重试', async () => { const wrapper = await mountWith({ ...busyRow(), appearancePatent: moduleCell('appearance-patent', { moduleLabel: '外观专利密钥', channel: 't8star', checkStatus: 'error', checkCode: 'tunnel_unverified', checkMessage: '这条链接里没有可用于隧道验连的账密,请重新填写', }), }) expect(pills(wrapper, '状态')[0].text).toBe('无法判定') expect(pills(wrapper, '外观专利密钥').at(-1)!.text).toBe('链接缺账密') }) it('悬停能看到后端 check_message 原文:单格与行级 tooltip 都带上', async () => { const wrapper = await mountWith(busyRow()) const proxyStatusTitle = pills(wrapper, '代理设置').at(-1)!.title expect(proxyStatusTitle).toContain(BUSY_MESSAGE) expect(proxyStatusTitle).toContain(MASKED_PROXY, '脱敏值仍然在同一条 tooltip 里') const rowTitle = pills(wrapper, '状态')[0].title expect(rowTitle).toContain('存在无法判定的检测结果', '后端行级摘要不动') expect(rowTitle).toContain(`代理设置:${BUSY_MESSAGE}`, '运维判「是欠费还是被自己占着」就靠这句') }) it('欠费(现网 21 行)保留自己那一档,不被新码表挤去「无法判定」', async () => { const wrapper = await mountWith(row({ status: 'failed', statusMessage: '存在检测失败的配置', proxy: moduleCell('proxy', { moduleLabel: '代理设置', channel: 'qingguo', masked: MASKED_PROXY, checkStatus: 'failed', checkCode: 'insufficient_balance', checkMessage: BALANCE_MESSAGE, }), })) const balancePill = pills(wrapper, '代理设置').at(-1)! expect(balancePill.text).toBe('欠费') expect(balancePill.classes).toContain('is-warn') expect(pills(wrapper, '状态')[0].text).toBe('检测失败', '行级优先级是后端的:failed 永远压过 error') expect(pills(wrapper, '代理设置').at(-1)!.title).toContain(BALANCE_MESSAGE) }) it('密钥坏了与查不出是两档:invalid_key 红、空码/未知码才是「无法判定」', async () => { const wrapper = await mountWith(row({ status: 'error', similarAsin: moduleCell('similar-asin', { moduleLabel: '货源查询密钥', channel: 'longtaitou', checkStatus: 'failed', checkCode: 'invalid_key', checkMessage: '密钥无效(401)' }), appearancePatent: moduleCell('appearance-patent', { moduleLabel: '外观专利密钥', channel: 't8star', checkStatus: 'error', checkCode: '', checkMessage: '' }), proxy: moduleCell('proxy', { moduleLabel: '代理设置', channel: 'qingguo', masked: MASKED_PROXY, checkStatus: 'error', checkCode: 'a_code_not_registered', checkMessage: '' }), })) expect(pills(wrapper, '货源查询密钥').at(-1)!.text).toBe('密钥失效') expect(pills(wrapper, '货源查询密钥').at(-1)!.classes).toContain('is-blocked') expect(pills(wrapper, '外观专利密钥').at(-1)!.text).toBe('无法判定') expect(pills(wrapper, '代理设置').at(-1)!.text).toBe('无法判定') }) it('没配与没检测不许冒充「无法判定」', async () => { const wrapper = await mountWith(row({ status: 'unknown', statusMessage: '存在尚未检测的配置', similarAsin: moduleCell('similar-asin', { moduleLabel: '货源查询密钥', exists: false, checkStatus: 'unknown', checkCode: '', checkMessage: '' }), appearancePatent: moduleCell('appearance-patent', { moduleLabel: '外观专利密钥', channel: 't8star', checkStatus: 'unknown', checkCode: '', checkMessage: '' }), })) expect(pills(wrapper, '货源查询密钥').at(-1)!.text).toBe('未配置') expect(cellText(wrapper, '货源查询密钥')).toContain('未配置') expect(pills(wrapper, '外观专利密钥').at(-1)!.text).toBe('未检测') expect(pills(wrapper, '状态')[0].text).toBe('未检测') }) it('一格上的两颗药丸不同源:货源取 channel、状态取 check_status/check_code', async () => { // 把代理供应商值塞进 LLM 行的 channel:货源那颗药丸必须认不出(走「—」), // 状态那颗仍按 check_code 出「鉴权被拒」——两值互不串门,才是「不同源」的正证。 const wrapper = await mountWith(row({ status: 'failed', similarAsin: moduleCell('similar-asin', { moduleLabel: '货源查询密钥', channel: 'qingguo', checkStatus: 'failed', checkCode: 'forbidden', checkMessage: '密钥被拒绝(403)' }), appearancePatent: moduleCell('appearance-patent', { moduleLabel: '外观专利密钥', channel: 't8star', checkStatus: 'passed', checkCode: 'ok' }), })) const similarPills = pills(wrapper, '货源查询密钥') expect(similarPills[0].text).toBe('—') expect(similarPills[1].text).toBe('鉴权被拒') // 反向:把状态词塞进渠道位也不会被认成货源 const llmPills = pills(wrapper, '外观专利密钥') expect(llmPills[0].text).toBe('贞贞') expect(llmPills[1].text).toBe('通过') expect(llmPills[0].classes).toContain('is-source') expect(llmPills[1].classes).toContain('is-allowed') }) it('筛选项把 error 这一档的两个可能读法都点名(选了要能筛到无可用通道的行)', async () => { const wrapper = await mountWith(busyRow()) const options = wrapper.findAll('.secrets-filter-row select')[0].findAll('option').map((option) => option.text().trim()) expect(options).toContain('无法判定/可重试') expect(options).not.toContain('无法判定') }) it('立即检测的 toast 用后端那句原文,不再硬写「无法判定」', async () => { secretsApi.fetchUserSecretList.mockResolvedValue({ items: [busyRow()], total: 1, page: 1, pageSize: 15, groupOptions: [] }) secretsApi.checkUserSecret.mockResolvedValue([ { moduleKey: 'proxy', checkStatus: 'error', checkCode: 'channel_busy', checkMessage: BUSY_MESSAGE, checkLatencyMs: 30, checkedAt: '2026-11-02T15:51:07', viaProxy: false }, ]) const wrapper = mount(UserSecretsPage, { global: { plugins: [pinia] } }) await flushPromises() const checkButton = wrapper.findAll('button').find((button) => button.text().trim() === '立即检测') expect(checkButton, '行内应有「立即检测」入口').toBeDefined() await checkButton!.trigger('click') await flushPromises() // ElMessage 真渲染进 body:读的是那一句后端原文,不是界面自己拼的「部分配置本次无法判定」 const shown = document.body.textContent ?? '' expect(shown).toContain(BUSY_MESSAGE) expect(shown).not.toContain('部分配置本次无法判定') }) })