import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { dirname, join } from 'node:path' /** * 「派发即移除」接线守卫(2026-10-04 A2)。 * * 代理按提取次数计费:点两次启动就把同一批店铺跑两遍是真花钱。查 ASIN 页把整批匹配行 * 合进 pendingQueue 后从不清空匹配表,第二次点击会再合一遍;跟价页则是在循环体里 * 逐行裁剪(这里把它钉成对照组,谁删了谁报错)。 * * 删行还有一个反向坑:查 ASIN 的历史卡片 ASIN 明细靠本地匹配表回退 * (fallbackQueryAsinsForShop 读 matchedItems),只删行不加归档等于把已派发店铺的历史 * 明细静默删掉 ⇒ 守卫同时要求归档进回退候选源 + 进持久化。 */ const PAGES_DIR = join(dirname(fileURLToPath(import.meta.url)), '../src/pages/brand/components') function sourceOf(page: string) { return readFileSync(join(PAGES_DIR, page), 'utf8') } const QUERY_ASIN = sourceOf('BrandQueryAsinTab.vue') const PRICE_TRACK = sourceOf('BrandPriceTrackTab.vue') /** 取函数体:从声明行到下一个顶层 function/async function 之前 */ function functionBody(source: string, header: string) { const start = source.indexOf(header) assert.ok(start >= 0, `未找到 ${header}`) const rest = source.slice(start + header.length) const next = rest.search(/\n(?:async )?function /) return next < 0 ? rest : rest.slice(0, next) } test('查ASIN 派发时必须把已交出的行从匹配表裁掉', () => { const body = functionBody(QUERY_ASIN, 'async function pushToPythonQueue()') assert.ok(body.includes('pruneRows('), 'pushToPythonQueue 未调用 pruneRows') // 必须把匹配表换成 pruneRows 算出来的 remaining(写成 matchedItems.value = matchedItems.value // 也算「有赋值」,所以要把变量名钉住),且发生在 processQueue 之前 assert.match( body, /const \{ remaining, removed \} = pruneRows\([\s\S]{0,160}?matchedItems\.value = remaining\b[\s\S]{0,400}?await processQueue\(\)/, '匹配表必须被替换成 pruneRows 的 remaining,且在 processQueue 之前', ) assert.ok(/saveMatchedItems\(\)/.test(body), '裁剪后未持久化匹配表') }) test('查ASIN 被裁掉的行要进归档并继续当 ASIN 回退数据源', () => { const body = functionBody(QUERY_ASIN, 'async function pushToPythonQueue()') assert.ok(body.includes('mergeDispatchedArchive('), '未把移除行并入已派发归档') const fallback = functionBody(QUERY_ASIN, 'function fallbackQueryAsinsForShop(') assert.match( fallback, /\.\.\.matchedItems\.value[\s\S]{0,80}?\.\.\.dispatchedArchive\.value/, '回退候选源里没有归档,隐藏=静默删除历史 ASIN 明细', ) }) test('查ASIN 归档必须随队列状态一起持久化并在重载后恢复', () => { const save = functionBody(QUERY_ASIN, 'function saveQueueState()') assert.ok(save.includes('dispatchedArchive'), 'saveQueueState 未写入归档') const load = functionBody(QUERY_ASIN, 'function loadQueueState()') // 只写「= 什么东西 ;」不够:类型注解 QueryAsinShopQueueItem[] 里就有 [], // 断言必须抓到 Array.isArray 这条兜底分支本身 assert.match( load, /dispatchedArchive\.value = Array\.isArray\(parsed\.dispatchedArchive\)[\s\S]{0,120}?:\s*\[\];/, 'loadQueueState 未对归档做类型兜底', ) }) test('查ASIN 派发后的提示要说清「再点启动只跑剩余」', () => { const body = functionBody(QUERY_ASIN, 'async function pushToPythonQueue()') assert.ok(body.includes('dispatchedPruneNotice('), '未使用统一提示语') }) test('对照组:跟价循环每派发一行就裁一行', () => { // 这条是现状钉桩,不是新增要求:runLoopExecution 已实现派发即移除, // 一旦有人把它删了,跟价会立刻退化成「再点启动再买一轮」。 assert.match( PRICE_TRACK, /enqueueCreatedTask\(api,[\s\S]{0,200}?removeMatchedRowsLocally\(\[row\]\)/, '跟价循环体不再裁剪已派发行', ) })