Files
crawler-plugin/frontend-vue/tests/candidate-selection-memory-wiring.test.ts
T
huangzd1997 47f24162d5 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

91 lines
4.3 KiB
TypeScript
Raw 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 C 档)。
* 纯逻辑在 candidate-selection-memory.test.ts 里钉;这里只守两件"接错就完全不生效"的事:
* ①el-table 上真有 ref、恢复真的由分页变化驱动(只挂 onMounted 的话翻页不恢复、重新加载不恢复);
* ②写入记忆的入口是 selection-change,且恢复期间不重算(半程写入会把还没勾上的名字抹掉)。
*/
const PAGES_DIR = join(dirname(fileURLToPath(import.meta.url)), '../src/pages/brand/components')
const PAGES = [
{ file: 'BrandQueryAsinTab.vue', tab: 'query-asin', nameField: 'shop_name' },
{ file: 'BrandPriceTrackTab.vue', tab: 'price-track', nameField: 'shopName' },
]
function blockBetween(source: string, from: string, to: string) {
const start = source.indexOf(from)
assert.ok(start >= 0, `未找到 ${from}`)
const end = source.indexOf(to, start)
assert.ok(end > start, `${from} 之后没找到 ${to}`)
return source.slice(start, end)
}
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)
}
for (const { file, tab, nameField } of PAGES) {
const source = readFileSync(join(PAGES_DIR, file), 'utf8')
test(`${file} 勾选记忆接线到位`, () => {
assert.match(source, /ref="candidateTableRef"/, '候选表没有 ref,恢复勾选无处可调')
assert.ok(source.includes('toggleRowSelection'), '没有调用 toggleRowSelection 恢复勾选')
assert.ok(source.includes('selectionMemoryKey'), '没有用共享的记忆键')
assert.match(
source,
new RegExp(`selectionMemoryKey\\(\\s*uidForStorage\\(\\)\\s*,\\s*["']${tab}["']\\s*\\)`),
`记忆键没带本页第 ${tab} 的 tab`,
)
assert.match(
source,
new RegExp(`function candidateNameOf[\\s\\S]{0,90}?row\\.${nameField}`),
`店铺名取值口径不对(应为 row.${nameField})`,
)
})
test(`${file} 恢复由分页变化驱动,且 onMounted 先读记忆`, () => {
assert.match(source, /watch\(\s*\n?\s*pagedCandidates/, '翻页后不重新恢复勾选')
const mount = blockBetween(source, 'onMounted(', 'onUnmounted(')
const memIdx = mount.indexOf('loadSelectionMemory()')
const candIdx = mount.search(/loadCandidates\(/)
assert.ok(memIdx >= 0, 'onMounted 没有读记忆')
assert.ok(candIdx < 0 || memIdx < candIdx, '读记忆晚于加载候选,首屏恢复不出来')
})
test(`${file} 勾选变化写记忆:按本窗口重算,恢复期间不重算`, () => {
const body = functionBody(source, 'function onSelectionChange(')
assert.ok(body.includes('reconcileRememberedNames('), 'selection-change 没有把勾选写进记忆')
assert.match(body, /if \(\s*restoringSelection\s*\)\s*return/, '恢复过程中仍在重写记忆,半程会抹掉未恢复的名字')
assert.match(body, /reconcileRememberedNames\([\s\S]{0,260}?saveSelectionMemory\(\)/, '记忆没落盘')
})
test(`${file} 记忆的读写真的落到 localStorage`, () => {
const save = functionBody(source, 'function saveSelectionMemory(')
assert.match(
save,
/(setStorageJson|localStorage\.setItem)\(\s*\n?\s*selectionMemoryStorageKey\(\)/,
'saveSelectionMemory 只改了内存,重启就忘',
)
const load = functionBody(source, 'function loadSelectionMemory(')
assert.match(load, /getItem\(\s*\n?\s*selectionMemoryStorageKey\(\)/, 'loadSelectionMemory 没读盘')
assert.match(load, /parseRememberedNames\(/, '读回来的没经脏数据收口')
})
test(`${file} 有清除入口,且清的是记忆本身`, () => {
const clear = functionBody(source, 'function clearCandidateSelectionMemory(')
assert.match(clear, /rememberedCandidates\.value = \[\]/, '没清内存里的名单')
assert.ok(clear.includes('saveSelectionMemory()'), '没写回 localStorage,下次启动又恢复')
assert.match(source, /@click="clearCandidateSelectionMemory/, '界面上没有清除勾选记忆入口')
})
}