Files
crawler-plugin/admin-frontend-vue/tests/mount/user-secret-status-band.mount.test.ts
T
huangzd1997 7299166e96 style(admin): 全站原生下拉统一换 el-select——弹层圆角与亚马逊面一致(2026-11-17 用户口径「样式咋不是圆角 和亚马逊不统一 其他地方也要看看」)
范围:src 里原生 <select> 由 36 处清零(含全站共享分页条),15 个页面 + 1 个组件。
换法统一:class="admin-filter"(main.css 皮肤 42px/9px/#f8fbfd/focus ring)+
aria-label 定位 + el-option 显式 label;「全部 XX」空值行与 value 形状逐字保留
('' / null / String(id) / '1'/'0' / `${source}|${deviceId}` 等,下游查询参数与后端枚举不动)。

- OldPagination:每页条数下拉换 el-select(局部 import,挂载测试单 mount 也能渲染),壳压回分页条 30px/8px 档
- account 三页(菜单类型/密钥状态/分组)+ GroupsPage/UsersPage 清死 select 规则
- asin 三页(国家/弹窗分组):弹窗分组用 placeholder+clearable+:value-on-clear 钉回 null,守 number|null 契约
- patent 页(国家/分组)
- records 两页(来源/用户/终端覆盖):终端覆盖 value 仍 source|deviceId
- tasks 四页 + 任务看板(业务/状态/类型/来源/店铺/分组/场景):BrandGating/DuplicateCheck/KeywordUsage 三页本地是 34px 紧凑档,
  故把 el-select 壳压回本地 34px/7px 与同行输入框齐高(否则高 8px 错位)

测试:mount 全量 21 文件 164 passed;node --test 1924 例 1922 passed(2 红为在册:
date-range-order 环境红、company-config-entry-super-only 由 ee613db0 引入与本次无关);
vue-tsc rc=0。新增 account-select-el / records-select-el 两支换壳契约(值形状 + 空值行 + 点开→点选项)。
2026-11-17 13:49:39 +08:00

282 lines
14 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 密钥管理列表页「检测结果分档」的界面侧判据(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 { ElOption, ElSelect } from 'element-plus'
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'
/* 筛选下拉 2026-11-17 起是 el-select(原生 select 的弹层是浏览器直角原生物,用户要求与亚马逊统一)。
页面模板不带局部 import、靠 main.ts 全局注册,挂载测试不跑 main.ts ⇒ 必须在这里显式注册,
否则拿到的是未解析的空壳,`findAllComponents(ElSelect)` 一条也找不到。 */
const EL_PLUGINS = [ElSelect, ElOption]
/** 按 aria-label 找下拉:EP 把该属性落在内层 combobox input 上,比按节点顺序取更稳。 */
function selectByLabel(wrapper: VueWrapper<Element>, label: string) {
const found = wrapper.findAllComponents(ElSelect).find((s) => s.find(`[aria-label="${label}"]`).exists())
expect(found, `应存在 aria-label="${label}" 的下拉`).toBeTruthy()
return found!
}
/** 下拉里每一档的文案(读 el-option 的 label prop,不依赖插槽文本)。 */
function optionLabels(wrapper: VueWrapper<Element>, label: string): string[] {
return selectByLabel(wrapper, label).findAllComponents(ElOption).map((o) => o.props('label') as string)
}
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<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-02T15:51:07',
updatedAt: '2026-11-02T15:51:07',
...over,
}
}
function row(over: Record<string, unknown> = {}) {
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<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 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<Element>, 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<string, unknown>) {
secretsApi.fetchUserSecretList.mockResolvedValue({ items: [rowValue], total: 1, page: 1, pageSize: 15, groupOptions: [] })
const wrapper = mount(UserSecretsPage, { global: { plugins: [pinia, ...EL_PLUGINS] } })
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 = optionLabels(wrapper, '筛选密钥状态')
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, ...EL_PLUGINS] } })
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('部分配置本次无法判定')
})
})