ab2605d1d1
代理按提取次数计费:这页把整批匹配行合进 pendingQueue 后从不清空匹配表,再点一次 「启动」就把同一批店铺再抓一遍——真花钱。查 ASIN/跟价/删除品牌/提款在 10-04 已按同一形 修过,这页当时漏了。 - BrandShopDataCrawlTab.startQueue 走共享的 pruneRows:匹配表换成 remaining、 removed 进 pendingQueue、随即 saveMatched()+saveQueueState(),并把提示统一成 dispatchedPruneNotice(说清"再点启动只跑剩余"); - 新增 tests/dispatch-loop-must-prune.test.ts:按函数体判「有自动接续开关 + 有派发」 的页面是否裁行,五页全覆盖,另配三份合成夹具(裸奔必须被判不安全、只有人工删行的 不许算合规、接好线的必须绿),判据自身不会恒绿。 「派发循环整体下沉服务端」这条前提经核不成立:拉取开关默认关是设计使然,且每轮派发参数 (紫鸟版本/国家序列/定时点)从不落库,服务端自行重派会花掉用户的次数,故留在客户端。 前端 node --test 全量 414 例 / 42 红:本批新增的 dispatch-loop-must-prune.test.ts 单跑 4 例全绿; 那 42 红是本机 node_modules 为空(缺 axios/vue),在干净 HEAD 上同样红。
147 lines
6.7 KiB
TypeScript
147 lines
6.7 KiB
TypeScript
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')
|
|
})
|