Files
crawler-plugin/frontend-vue/tests/helpers/api-snapshot.ts
T
huangzd1997 82a782550e feat(密钥): 用户 API 密钥服务端化——V115 按账号绑定存储 + 后台密钥管理页 + 桌面端全站拦截与配置引导
- 新增 usersecret 模块:外观专利/货源查询密钥从本地 localStorage 迁移至 biz_user_api_secret(AES 加密、按 uid 绑定)
- 后台「密钥管理」页:脱敏展示、立即检测、清空;每日 04:30 分布式锁定时巡检
- 桌面端:密钥设置面板走服务端、未配齐引导 /setup-secrets、代理余量展示
- 删除专利汇令牌全链路与密钥保留时长选择器
2026-09-13 10:03:59 +08:00

59 lines
2.0 KiB
TypeScript

import assert from 'node:assert/strict'
export interface ApiCallSnapshot {
url?: string
method?: string
params?: Record<string, unknown>
data?: unknown
}
const ALLOWED_SECTIONS = ['url', 'method', 'params', 'data'] as const
const apiModules = [
'dedupe', 'split', 'convert', 'publish', 'similar-asin', 'appearance-patent',
'delete-brand', 'price-track', 'shop-match', 'query-asin', 'withdraw',
'collect-data', 'image-video', 'brand', 'permission', 'digital-human', 'user-secret',
] as const
export function isApiModule(name: string): boolean {
return (apiModules as readonly string[]).includes(name)
}
export function assertApiCall(calls: ApiCallSnapshot[], index: number, expected: ApiCallSnapshot) {
const actual = calls[index]
assert.ok(actual, `第 ${index} 次请求不存在(共 ${calls.length} 次)`)
const context = `第 ${index} 次请求实际=${JSON.stringify(actual)} 期望=${JSON.stringify(expected)}`
for (const section of ALLOWED_SECTIONS) {
assert.ok(
!(section in expected) || section in actual,
`快照缺少 section: ${section}${context}`,
)
}
if (expected.url !== undefined) {
assert.equal(actual.url, expected.url, `URL 快照不匹配。${context}`)
}
if (expected.method !== undefined) {
assert.equal(actual.method, expected.method, `method 快照不匹配。${context}`)
}
if (expected.params !== undefined) {
const actualParams = actual.params ?? {}
for (const key of Object.keys(actualParams)) {
assert.ok(
key in expected.params,
`params 出现白名单外字段: ${key}${context}`,
)
}
assert.deepEqual(actualParams, expected.params, `params 快照不匹配。${context}`)
}
if (expected.data !== undefined) {
const actualData = (actual.data ?? {}) as Record<string, unknown>
for (const key of Object.keys(actualData)) {
assert.ok(
key in expected.data,
`data 出现白名单外字段: ${key}${context}`,
)
}
assert.deepEqual(actualData, expected.data, `data 快照不匹配。${context}`)
}
}