Files
crawler-plugin/frontend-vue/tests/helpers/api-snapshot.ts
T
huangzd1997 e896e0bef4 task-26: 模块 API 快照契约测试基建
新增集中式 assertApiCall() helper(tests/helpers/api-snapshot.ts):
断言 url/method/params/data 快照,params/data 白名单外字段报错,
失败信息含实际与期望 URL 上下文;isApiModule() 覆盖 16 个已拆模块。

8 个测试:helper url/method/params/body、all_modules_covered、
fail_message、allowlist、regression_guard。
2026-08-31 20:05:53 +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',
] 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}`)
}
}