Files
crawler-plugin/frontend-vue/tests/query-asin-dispatch-prune.test.ts
huangzd1997 eea617b3e6 feat(采集页省钱线): 派发即裁剪已匹配行、候选勾选记忆、摘掉跟价旧逐行派发
代理按「提取次数」计费而不是按流量,所以把同一批店铺重复派发一次就是真花钱。三处改动的
共同口径是「用户多点一次启动,不再多买一轮」。纯逻辑各抽成一个可单测的模块,页面只接线。

- dispatch-row-prune:查 ASIN 页匹配好的店铺交给后台执行后从结果表里移除。删行本身一句 filter,
  两个坑要钉住——行键口径必须与该页 rowKeyForMatch 一致(名字带两端空格的行删不掉,就留着
  下次再买一轮);历史行明细靠本地匹配表回退(withQueryAsinFallback 读 matchedItems),直接删行
  等于把已派发店铺的历史明细一起删掉 ⇒ 移除的行进一份上限 50 的归档,继续当回退数据源。
- candidate-selection-memory:候选店铺每天重拉时保住勾选。记的是店铺名不是行 id(候选行会被
  删了再加、id 会变,店铺名才是人眼认的那个身份);键按 uid + 页签隔离;selection-change 只带
  当前渲染窗口里的行,整份覆盖会把别的页刚记住的勾选一起抹掉 ⇒ 只按本窗口可见行重算;
  店铺已被删除时不得恢复勾选(否则匹配的是不存在的店)。
- 跟价页摘掉 processMatchedQueue 这条旧逐行派发路径:入口只有 if (autoQueueEnabled),而本页
  从来没把它置 true ⇒ 永远进不去。留着不是无害死代码——它是一套能重复买提取的并行派发路径,
  谁接错一次就再花钱。用例反向钉「不得回流」,不给死代码补测试。

验证:frontend-vue 定向 29/0(dispatch-row-prune 7、candidate-selection-memory 5、
wiring 10、query-asin-dispatch-prune 5、price-track-dead-queue-removed 反向钉 2),
全量 710/0(含本会话另加的到期弹窗 9 支,那 9 支随上一笔走)。接线由 wiring 用例钉住
"纯模块真被这两个页面 import",不是只测纯函数。
2026-10-05 11:51:53 +08:00

88 lines
4.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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\]\)/,
'跟价循环体不再裁剪已派发行',
)
})