import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { resolve, dirname } from 'node:path' import { RISK_LISTING_FILTER_KEY, productRiskFilterIssues, shopMatchFilterIssues, } from '../src/pages/brand/components/listingFilters.ts' import { checkQueuePayload, type GuardResult } from '../src/shared/dispatch-guard.ts' /** * 定时匹配(shop-match-run)入队前的闸。 * * 2026-10-19 给产品风险补那道闸时,这一支被记在"同源没改"的欠账里。复算后拆开处理: * - 缺筛选值这一半确实同源:match_action.py 拿到 '' 会去页面上找 value='' 的选项, * 必然 0 行,而它判空的写法与 approve.py 逐字同形(`len(sku_ls) == 0` 直接 return), * 整单看着跑完、实际一行没匹配 ⇒ 照样拦。 * - 「在售要签字」这一半不成立:shopMatchListingFilter 的默认值就是 Active, * 这一支的主用途正是给在售商品匹配价格。给它套产品风险那份判据,等于每天默认 * 发起都要签一次字——只会教会用户闭眼点。所以这里只接缺键那道。 * * .vue 组件无法在本套里挂载,接线按源码形状钉(同 enqueue-rejection-wiring 的约定)。 */ const here = dirname(fileURLToPath(import.meta.url)) const shopMatchTab = readFileSync( resolve(here, '../src/pages/brand/components/BrandShopMatchTab.vue'), 'utf8', ) function codes(result: GuardResult) { return result.issues.map((issue) => issue.code) } /** 与组件里 shopMatchPayloadGuard 同一份选项,改了组件不改这里 ⇒ 下面的用例判红 */ function guard(payload: unknown) { return checkQueuePayload(payload, { expectedType: 'shop-match-run', requiredDataKeys: ['taskId'], nonEmptyArrayKeys: ['items', 'country_codes'], extraIssues: (data) => shopMatchFilterIssues(data[RISK_LISTING_FILTER_KEY]), }) } function runPayload(data: Record) { return { type: 'shop-match-run', ts: 1, data: { taskId: 31500, items: [{ shopName: '蔡建芳' }], country_codes: ['DE'], [RISK_LISTING_FILTER_KEY]: 'Active', ...data, }, } } test('test_定时匹配_缺筛选值必须拦住', () => { const issues = shopMatchFilterIssues(undefined) assert.equal(issues.length, 1) assert.equal(issues[0].severity, 'block', "空值到了 Python 端会 0 行假成功,不能放行") assert.equal(issues[0].code, 'shop-match.filter-missing') assert.match(issues[0].message, /risk_listing_filter/) for (const bad of ['', ' ', null, 7]) { assert.equal(shopMatchFilterIssues(bad)[0].severity, 'block', `空值 ${String(bad)} 同样要拦`) } }) test('test_定时匹配_在售是默认值不许弹窗', () => { assert.deepEqual(shopMatchFilterIssues('Active'), [], '这一支的默认与主用途就是在售;套上产品风险那份 confirm 会让每次发起都弹一次字') const result = guard(runPayload({})) assert.equal(result.ok, true) assert.equal(result.needsConfirm, false) assert.deepEqual(result.issues, []) }) test('test_定时匹配_整条载荷缺键时结构校验也救不回来', () => { const withoutFilter = runPayload({}) delete (withoutFilter.data as Record)[RISK_LISTING_FILTER_KEY] const result = guard(withoutFilter) assert.equal(result.ok, false, '只查 taskId/items 的结构校验拦不住缺筛选值,必须靠 extraIssues') assert.ok(codes(result).includes('shop-match.filter-missing')) for (const blank of ['', ' ']) { assert.equal(guard(runPayload({ [RISK_LISTING_FILTER_KEY]: blank })).ok, false, `空串 ${JSON.stringify(blank)} 与缺键同形:键在但值为空,Python 端照样拿它去 search('')`) } }) test('test_定时匹配_空店铺与空国家照样拦下', () => { assert.equal(guard(runPayload({ items: [] })).ok, false) assert.equal(guard(runPayload({ country_codes: [] })).ok, false) assert.ok(codes(guard(runPayload({ items: [], country_codes: [] }))).includes('payload.empty-array')) }) test('test_定时匹配_任务号缺失与NaN都拦下', () => { const noTaskId = runPayload({}) delete (noTaskId.data as Record).taskId assert.equal(guard(noTaskId).ok, false) assert.equal(guard(runPayload({ taskId: Number.NaN })).ok, false, 'NaN 序列化后是 null,Python 端拿到空任务号会一直卡在执行中') }) test('test_两支共用的缺键判据各认各的出处', () => { assert.equal(shopMatchFilterIssues('')[0].code, 'shop-match.filter-missing') assert.equal(productRiskFilterIssues('')[0].code, 'product-risk.filter-missing', '文案与问题码按支分开:产品风险那条被日志/用例认过,不能因为复用而改名') // 产品风险仍然对在售要签字,定时匹配不签——这份差异就是本次复算的结论 assert.equal(productRiskFilterIssues('Active')[0].severity, 'confirm') assert.deepEqual(shopMatchFilterIssues('Active'), []) }) function bodyOf(source: string, startMarker: string, endMarker: string) { const start = source.indexOf(startMarker) assert.notEqual(start, -1, `组件里找不到 ${startMarker}`) const end = source.indexOf(endMarker, start) assert.notEqual(end, -1, `组件里找不到 ${endMarker}`) return source.slice(start, end) } test('test_定时匹配_手动启动口先过闸再入队', () => { const fn = bodyOf(shopMatchTab, 'async function processMatchedQueue', 'async function batchDeleteHistory') const guardAt = fn.indexOf('passGuard(shopMatchPayloadGuard(payload))') assert.notEqual(guardAt, -1, '手动启动口没过闸') const enqueueAt = fn.indexOf('api.enqueue_json(payload)') assert.notEqual(enqueueAt, -1, '找不到推送语句') assert.ok(guardAt < enqueueAt, '闸必须跑在推送之前') assert.match(fn.slice(guardAt, enqueueAt), /break/, '店铺列表共用同一份筛选值与国家,拦下一条就该整批停手,不该逐店再弹一次') }) test('test_定时匹配_定时口无人签字只认阻断项', () => { const fn = bodyOf(shopMatchTab, 'async function dispatchScheduledTask', 'function scheduledTimestamp') const guardAt = fn.indexOf('shopMatchPayloadGuard(payload)') assert.notEqual(guardAt, -1, '定时执行口没过闸') const enqueueAt = fn.indexOf('api.enqueue_json(payload)') assert.ok(guardAt < enqueueAt, '闸必须跑在推送之前') assert.doesNotMatch(fn, /passGuard\(/, '定时口没有人在屏幕前,弹窗会把这一轮卡住;只按 guard.ok 抛错,交给外层如实报错') assert.match(fn, /if \(!guard\.ok\) throw/, '不通过要抛错,让这一轮留下失败原因而不是静默跳过') })