125 lines
6.6 KiB
TypeScript
125 lines
6.6 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { existsSync, readFileSync } from 'node:fs'
|
||
import { join } from 'node:path'
|
||
|
||
/** v1.4 AC-1 用户端入口契约(蓝图 docs/ITERATION_PLAN.md §3.2 / §4 Phase 5)。
|
||
*
|
||
* 「京东采集」以工具台卡片 + 路由 + endpoints 组三件接入用户端:
|
||
* 2026-11-15 用户拍板——卡片不再挂在亚马逊工具台(pages/amazon/tool-catalog.ts,
|
||
* 不含京东的绊线在 amazon-tool-catalog.test.ts),收敛到首页一级「京东」工作台
|
||
* 自己的目录(pages/jd/tool-catalog.ts,显隐收口在工具台内部)。
|
||
* 路由 /jd-collect,endpoints.ts 有 jdCollect 组。Pending 期整组 skip;
|
||
* Phase 5 落地后去掉 skip 跑绿。
|
||
*/
|
||
|
||
const ROUTER = join(process.cwd(), 'src', 'router', 'index.ts')
|
||
const JD_TOOL_CATALOG = join(process.cwd(), 'src', 'pages', 'jd', 'tool-catalog.ts')
|
||
const ENDPOINTS = join(process.cwd(), 'src', 'shared', 'api', 'endpoints.ts')
|
||
|
||
function readIfExists(path: string, what: string): string {
|
||
assert.ok(existsSync(path), `${what} 尚未落地(蓝图 §4 Phase 5):${path}`)
|
||
return readFileSync(path, 'utf8')
|
||
}
|
||
|
||
test('路由注册京东采集页', () => {
|
||
assert.ok(readIfExists(ROUTER, 'router/index.ts').includes('/jd-collect'),
|
||
'router 缺 /jd-collect 路由')
|
||
})
|
||
|
||
test('京东工作台卡片声明采集入口并指向采集页', () => {
|
||
const catalog = readIfExists(JD_TOOL_CATALOG, 'jd/tool-catalog.ts')
|
||
assert.ok(catalog.includes('获取店铺数据'),
|
||
'京东工作台缺「获取店铺数据」卡')
|
||
assert.ok(catalog.includes('/jd-collect'),
|
||
'「获取店铺数据」卡必须带 href 指向 /jd-collect')
|
||
})
|
||
|
||
test('endpoints 注册 jdCollect 端点组', () => {
|
||
assert.ok(readIfExists(ENDPOINTS, 'endpoints.ts').includes('jdCollect'),
|
||
'endpoints.ts 缺 jdCollect 端点组(建单/心跳/回传三件)')
|
||
})
|
||
|
||
/** 2026-11-16 用户口径「客户端前端也要同步加上京东类目」:建单页多选末级类目随链下发。
|
||
* 三处同键契约(建单请求体 / 推送载荷 / 历史行带回)缺一处,客户端就按全店采集跑。 */
|
||
test('endpoints 注册采集类目 categories 下拉端点', () => {
|
||
const endpoints = readIfExists(ENDPOINTS, 'endpoints.ts')
|
||
assert.ok(endpoints.includes("categories: '/api/jd-collect/categories'"),
|
||
'endpoints.ts jdCollect 组缺 categories(建单页类目多选面板的选项源)')
|
||
})
|
||
|
||
test('建单请求体与推送载荷都带 categoryPaths', () => {
|
||
const api = readIfExists(
|
||
join(process.cwd(), 'src', 'shared', 'api', 'types', 'modules', 'jd-collect.ts'),
|
||
'jd-collect.ts',
|
||
)
|
||
const apiStart = api.indexOf('export function createJdCollectTask')
|
||
const apiEnd = api.indexOf('export interface JdCollectDashboardVo')
|
||
assert.ok(apiStart > 0 && apiEnd > apiStart, 'jd-collect.ts 缺 createJdCollectTask')
|
||
const apiBody = api.slice(apiStart, apiEnd)
|
||
assert.ok(apiBody.includes('categoryPaths'),
|
||
'createJdCollectTask 请求体必须带 categoryPaths(空数组=全店采集)')
|
||
|
||
const page = readIfExists(
|
||
join(process.cwd(), 'src', 'pages', 'jd', 'JdCollectTab.vue'),
|
||
'JdCollectTab.vue',
|
||
)
|
||
assert.ok(page.includes('maxProducts: 0, categoryPaths }'),
|
||
'推送载荷 data 必须带 categoryPaths(与后端 JdTaskPullSpiImplTest 键集断言同键)')
|
||
assert.ok(page.includes('createJdCollectTask(shop, tabs, 0, categories)'),
|
||
'startCollect 建单必须把选中的类目传给 createJdCollectTask')
|
||
})
|
||
|
||
test('重试/继续从历史行同源带回 categoryPaths', () => {
|
||
const page = readIfExists(
|
||
join(process.cwd(), 'src', 'pages', 'jd', 'JdCollectTab.vue'),
|
||
'JdCollectTab.vue',
|
||
)
|
||
const startAt = page.indexOf('async function retryTaskRecord')
|
||
assert.ok(startAt > 0, 'JdCollectTab.vue 缺 retryTaskRecord')
|
||
const body = page.slice(startAt, page.indexOf('/** 手动刷新'))
|
||
assert.ok(body.includes('item?.categoryPaths ?? []'),
|
||
'重试/继续重建推送载荷必须从历史行带回 categoryPaths(后端 history VO 同源字段),缺了客户端会按全店采集跑')
|
||
})
|
||
|
||
/** 2026-11-14 真机实录:建单请求体写成 `user_id` 时后端回一句「user_id 不合法」直接拒,
|
||
* 管理员点「一键获取店铺数据」永远失败——Java 侧 DTO 是 camelCase `userId`(无 snake 命名
|
||
* 策略),snake 键会被静默丢掉。进度接口的 `?user_id=` 是 @RequestParam,那个名字不改。
|
||
* 射程只圈 createJdCollectTask 函数段:面板段的 `params: { user_id }` 是 @RequestParam
|
||
* 形状(deleteJdCollectTask 等与外观专利同形),不是请求体,不该被这条守卫误伤。 */
|
||
test('建单请求体用 camelCase userId,不是 user_id', () => {
|
||
const api = readIfExists(
|
||
join(process.cwd(), 'src', 'shared', 'api', 'types', 'modules', 'jd-collect.ts'),
|
||
'jd-collect.ts',
|
||
)
|
||
const start = api.indexOf('createJdCollectTask')
|
||
const end = api.indexOf('export interface JdCollectDashboardVo')
|
||
assert.ok(start > 0 && end > start, 'jd-collect.ts 缺 createJdCollectTask 或面板段形状变了')
|
||
const body = api.slice(start, end)
|
||
assert.ok(/userId:\s*getCurrentUserId\(\)/.test(body),
|
||
'createJdCollectTask 请求体必须传 camelCase `userId`(传 user_id 会被后端丢掉⇒「user_id 不合法」)')
|
||
assert.ok(!/[^_a-z]user_id:\s*getCurrentUserId\(\)/.test(body),
|
||
'建单请求体不得再传 snake_case `user_id`')
|
||
})
|
||
|
||
/** 2026-11-14 真机实录(task 34399):页面建完单直接推客户端,任务还是 PENDING ⇒ 客户端
|
||
* 第一个分片被「任务不在运行中(PENDING),回传被拒」、心跳回 alive=false 让客户端收手,
|
||
* 采集白跑一轮。修法是建单后先激活(PENDING→RUNNING)再推队列——顺序反了等于没修。 */
|
||
test('建单后先激活再推客户端队列', () => {
|
||
const page = readIfExists(
|
||
join(process.cwd(), 'src', 'pages', 'jd', 'JdCollectTab.vue'),
|
||
'JdCollectTab.vue',
|
||
)
|
||
assert.ok(page.includes('activateJdCollectTask'),
|
||
'JdCollectTab.vue 必须调用 activateJdCollectTask(漏掉这一步客户端分片全被拒)')
|
||
const startAt = page.indexOf('async function startCollect')
|
||
assert.ok(startAt > 0, 'JdCollectTab.vue 缺 startCollect')
|
||
const body = page.slice(startAt)
|
||
const activateAt = body.indexOf('activateTask(vo.taskId)')
|
||
const pushAt = body.indexOf('pushToClientQueue(vo.taskId')
|
||
assert.ok(activateAt > 0, 'startCollect 必须先调用 activateTask(vo.taskId)')
|
||
assert.ok(pushAt > 0, 'startCollect 应继续推送本机队列')
|
||
assert.ok(activateAt < pushAt,
|
||
'激活必须排在推送之前(PENDING 状态下推给客户端=分片全被拒)')
|
||
})
|