diff --git a/frontend-vue/src/pages/brand/components/BrandShopDataCrawlTab.vue b/frontend-vue/src/pages/brand/components/BrandShopDataCrawlTab.vue index 59eaa91c0..a94326fe4 100644 --- a/frontend-vue/src/pages/brand/components/BrandShopDataCrawlTab.vue +++ b/frontend-vue/src/pages/brand/components/BrandShopDataCrawlTab.vue @@ -142,6 +142,7 @@ import { createCategorizedTimers } from '@/shared/utils/categorized-timers' import { saveUrlWithProgress } from '@/shared/utils/download-progress' import { taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text' import { checkQueuePayload } from '@/shared/dispatch-guard' +import { dispatchedPruneNotice, pruneRows } from '@/shared/utils/dispatch-row-prune' import { passGuard } from '@/shared/dispatch-guard-ui' import { mergeHistoryItems } from '@/shared/merge-history-items' import { @@ -577,9 +578,14 @@ function startQueue() { const runnable = matchedRunnableItems.value if (!runnable.length) { ElMessage.warning('请先匹配可用店铺'); return } autoQueueEnabled.value = true - pendingQueue.value = mergeItems(pendingQueue.value, runnable) + // 派发即裁剪:整批行留在匹配表里的话,再点一次「启动」就把同一批店铺再抓一遍 + // (代理按提取次数计费)。查 ASIN 页 10-04 已按同一形修过,这页当时漏了。 + const { remaining, removed } = pruneRows(matchedItems.value, runnable, rowKey) + matchedItems.value = remaining + saveMatched() + pendingQueue.value = mergeItems(pendingQueue.value, removed) saveQueueState() - queueStatus.value = `已加入 ${runnable.length} 条店铺` + queueStatus.value = dispatchedPruneNotice(removed.length, remaining.length) void processQueue() } diff --git a/frontend-vue/tests/dispatch-loop-must-prune.test.ts b/frontend-vue/tests/dispatch-loop-must-prune.test.ts new file mode 100644 index 000000000..5281cf14c --- /dev/null +++ b/frontend-vue/tests/dispatch-loop-must-prune.test.ts @@ -0,0 +1,146 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import { readFileSync, readdirSync } from 'node:fs' +import { fileURLToPath } from 'node:url' +import { dirname, join } from 'node:path' + +/** + * 「浏览器侧派发循环必须裁剪已派发行」的棘轮守卫(2026-10-07 架构三期,第 4 项)。 + * + * 这五页的自动接续跑在浏览器标签页里(`autoQueueEnabled` + while 循环 + 逐行 createTask/enqueue_json)。 + * 服务端那套兜底拉取(`/api/tasks/pull-pending`)今天对它们不开:module-types 只配了 + * SIMILAR_ASIN 与 COLLECT_DATA,两个开关默认 false,而且这几页每次启动带的 ziniao_version / + * 国家序列 / 定时点从没落库,服务端拿不到"下一家该带什么参数"。所以"标签页关掉就断"是现状, + * 而**在标签页活着的时候把同一批店铺买两遍**是今天唯一能确定修掉的那一半。 + * + * 计费口径:代理按提取次数计费,重复派发就是重复花钱(见 dispatch-row-prune.ts 开头那段)。 + * + * 为什么不能只 grep 整份文件里有没有 removeMatchedRow:店铺数据抓取页就有一个 + * `removeMatchedRow()`(用户手点「移除」用),派发路径一次都没调它 —— 文件级 grep 会把这页 + * 判成合规。所以判据落在**函数体**上:要么"启动那支"(写了 autoQueueEnabled.value = true)里 + * 接了 pruneRows,要么"派发那支"(写了 enqueue_json / createXxxTask)里逐行删。 + */ + +const PAGES_DIR = join(dirname(fileURLToPath(import.meta.url)), '..', 'src', 'pages', 'brand', 'components') + +function sourceOf(page: string) { + return readFileSync(join(PAGES_DIR, page), 'utf8') +} + +/** 按顶层 function 声明切函数体(形状抄 query-asin-dispatch-prune.test.ts 的 functionBody,改成一次切完)。 */ +function functionBodies(source: string) { + const starts: number[] = [] + const re = /^(?:async )?function [A-Za-z_$][\w$]*\(/gm + let m: RegExpExecArray | null + while ((m = re.exec(source)) !== null) starts.push(m.index) + return starts.map((start, i) => source.slice(start, i + 1 < starts.length ? starts[i + 1] : source.length)) +} + +const DISPATCH = /enqueue_json|create[A-Za-z]*Task\(/ +const STOPS = /autoQueueEnabled\.value = true/ +const PRUNES = /pruneRows\(|removeMatchedRowsLocally\(/ + +interface PageVerdict { + page: string + hasQueueSwitch: boolean + dispatches: boolean + /** 派发那支或启动那支里出现了裁剪 ⇒ 才算受保护 */ + pruned: boolean +} + +function judge(page: string, source: string): PageVerdict { + const bodies = functionBodies(source) + const hasQueueSwitch = source.includes('autoQueueEnabled = ref(false)') + const dispatches = bodies.some((body) => DISPATCH.test(body)) + const protectedByStart = bodies.some((body) => STOPS.test(body) && /pruneRows\(/.test(body)) + const protectedByLoop = bodies.some((body) => DISPATCH.test(body) && PRUNES.test(body)) + return { page, hasQueueSwitch, dispatches, pruned: protectedByStart || protectedByLoop } +} + +const GUARDED_PAGES = [ + 'BrandProductRiskTab.vue', + 'BrandQueryAsinTab.vue', + 'BrandShopDataCrawlTab.vue', + 'BrandShopMatchTab.vue', + 'BrandWithdrawTab.vue', +] + +test('判据不是恒绿:一份"有开关、有派发、不裁剪"的样本必须被判成裸奔', () => { + const naked = ` +const autoQueueEnabled = ref(false) +async function startQueue() { + autoQueueEnabled.value = true + await processQueue() +} +async function processQueue() { + const api = getPywebviewApi() + await api.enqueue_json(payload) +}` + const verdict = judge('naked.vue', naked) + assert.equal(verdict.hasQueueSwitch, true, '样本没被认成"有自动接续开关"') + assert.equal(verdict.dispatches, true, '样本没被认成"会派发"') + assert.equal(verdict.pruned, false, '样本没裁剪却被判合规 ⇒ 这条守卫等于没写') + + // 反向:同一份样本只在"用户手点移除"那支里删行,不许算合规(店铺数据抓取页的真实形状) + const manualOnly = ` +const autoQueueEnabled = ref(false) +async function startQueue() { + autoQueueEnabled.value = true + await processQueue() +} +async function processQueue() { + await api.enqueue_json(payload) +} +function removeMatchedRow(item) { + removeMatchedRowsLocally([item]) +}` + assert.equal(judge('manual-only.vue', manualOnly).pruned, false, + '把删除挂在用户手点的按钮上不该免掉派发路径的裁剪') + + // 再反向:真接了 pruneRows 的必须判绿,否则下一支断言是恒红 + const wired = ` +const autoQueueEnabled = ref(false) +async function startQueue() { + autoQueueEnabled.value = true + const { remaining, removed } = pruneRows(matchedItems.value, runnable, rowKey) + matchedItems.value = remaining + await processQueue() +} +async function processQueue() { + await api.enqueue_json(payload) +}` + assert.equal(judge('wired.vue', wired).pruned, true, '合规形状被判红 ⇒ 下面的断言会把人逼着改判据') +}) + +test('五页里带自动接续开关的恰好是这五页,一页都不许多', () => { + const found = readdirSync(PAGES_DIR) + .filter((name) => /^Brand.*Tab\.vue$/.test(name)) + .map((name) => judge(name, sourceOf(name))) + .filter((v) => v.hasQueueSwitch && v.dispatches) + .map((v) => v.page) + .sort() + // 名单漂了(新页接了自动接续、或某页把开关拆了)要在这里先响,别等到下一支按老名单跑 + assert.deepEqual(found, GUARDED_PAGES, '受本页看守的页面名单变了') +}) + +test('每一页的派发路径都必须裁掉已交出的行', () => { + const naked = GUARDED_PAGES + .map((name) => judge(name, sourceOf(name))) + .filter((v) => !v.pruned) + .map((v) => v.page) + assert.deepEqual(naked, [], '这些页再点一次启动会把同一批店铺再买一遍') +}) + +test('店铺数据抓取页:裁剪必须写在启动那支里、且在 processQueue 之前', () => { + const bodies = functionBodies(sourceOf('BrandShopDataCrawlTab.vue')) + const starter = bodies.find((body) => body.startsWith('function startQueue()')) + assert.ok(starter, '没找到 startQueue —— 启动入口改名了,本页看守不到它') + assert.match( + starter, + /const \{ remaining, removed \} = pruneRows\(matchedItems\.value, runnable, rowKey\)[\s\S]{0,120}?matchedItems\.value = remaining\b[\s\S]{0,240}?saveMatched\(\)[\s\S]{0,240}?void processQueue\(\)/, + '匹配表必须被换成 pruneRows 的 remaining、要持久化、且这些都发生在 processQueue 之前', + ) + assert.ok(starter.includes('dispatchedPruneNotice('), '派发后没给"再点启动只跑剩余"的统一提示') + assert.ok(!starter.includes('已加入 ${runnable.length} 条店铺'), + '旧文案还在:说明队列里合进去的是 runnable(含未派发行),而不是被裁出来的 removed') +})