eea617b3e6
代理按「提取次数」计费而不是按流量,所以把同一批店铺重复派发一次就是真花钱。三处改动的 共同口径是「用户多点一次启动,不再多买一轮」。纯逻辑各抽成一个可单测的模块,页面只接线。 - 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",不是只测纯函数。
101 lines
3.8 KiB
TypeScript
101 lines
3.8 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import {
|
||
DISPATCH_ARCHIVE_LIMIT,
|
||
dispatchedPruneNotice,
|
||
mergeDispatchedArchive,
|
||
pruneRows,
|
||
} from '../src/shared/utils/dispatch-row-prune.ts'
|
||
|
||
interface Row {
|
||
shopName: string
|
||
shopId?: number
|
||
asins?: string[]
|
||
}
|
||
|
||
const keyOf = (row: Row) => `${(row.shopName || '').trim()}::${row.shopId || ''}`
|
||
|
||
/** 三行 A/B/C(A 与 B 各带 ASIN,用来验归档后仍查得到明细) */
|
||
function rows(): Row[] {
|
||
return [
|
||
{ shopName: ' 店铺A ', shopId: 1, asins: ['B0A'] },
|
||
{ shopName: '店铺B', shopId: 2, asins: ['B0B'] },
|
||
{ shopName: '店铺C', shopId: 3, asins: [] },
|
||
]
|
||
}
|
||
|
||
test('裁剪按行键移除,保留原顺序与未派发的行', () => {
|
||
const base = rows()
|
||
const { remaining, removed } = pruneRows(base, [base[0]], keyOf)
|
||
|
||
assert.deepEqual(remaining.map(keyOf), ['店铺B::2', '店铺C::3'])
|
||
assert.deepEqual(removed.map(keyOf), ['店铺A::1'])
|
||
assert.deepEqual(removed.map((r) => r.shopName), [' 店铺A '], '返回的应是原始行对象本体,不是重写过的')
|
||
})
|
||
|
||
test('裁剪不修改入参数组(页面 ref 直接赋值时要能安全比较)', () => {
|
||
const base = rows()
|
||
const snapshot = base.map(keyOf)
|
||
|
||
pruneRows(base, [base[0], base[1]], keyOf)
|
||
|
||
assert.deepEqual(base.map(keyOf), snapshot, 'pruneRows 原地改了入参:Vue 的引用比较会失效')
|
||
})
|
||
|
||
test('派发键含空白时按 trim 匹配:同一店铺不因两端空格漏掉而留双行', () => {
|
||
const base: Row[] = [{ shopName: '店铺A', shopId: 1 }]
|
||
const dispatched: Row[] = [{ shopName: ' 店铺A ', shopId: 1 }]
|
||
|
||
const { remaining } = pruneRows(base, dispatched, keyOf)
|
||
|
||
assert.deepEqual(remaining, [], 'trim 口径不一致 ⇒ 再点启动会把这家店再派发一次(再买一轮提取)')
|
||
})
|
||
|
||
test('派发清单为空时不动任何行', () => {
|
||
const base = rows()
|
||
|
||
const { remaining, removed } = pruneRows(base, [], keyOf)
|
||
|
||
assert.deepEqual(remaining.map(keyOf), base.map(keyOf))
|
||
assert.deepEqual(removed, [])
|
||
})
|
||
|
||
test('归档按行键去重并按上限截断(localStorage 不能被历史 ASIN 撑爆)', () => {
|
||
const base = rows()
|
||
const first = mergeDispatchedArchive([], base, keyOf)
|
||
assert.deepEqual(first.map(keyOf), ['店铺A::1', '店铺B::2', '店铺C::3'])
|
||
assert.equal(first.length, 3)
|
||
|
||
// 重复派发同一行不累积
|
||
const again = mergeDispatchedArchive(first, [base[0]], keyOf)
|
||
assert.equal(again.length, 3, '同一店铺重复派发应在归档里覆盖而不是追加')
|
||
|
||
const many: Row[] = Array.from({ length: DISPATCH_ARCHIVE_LIMIT + 20 }, (_, i) => ({
|
||
shopName: `店${i}`,
|
||
shopId: i,
|
||
asins: [`B0${i}`],
|
||
}))
|
||
const capped = mergeDispatchedArchive([], many, keyOf)
|
||
assert.equal(capped.length, DISPATCH_ARCHIVE_LIMIT, '归档必须有上限')
|
||
// 保留最近的:最后一条一定在
|
||
assert.equal(capped[capped.length - 1].shopName, `店${many.length - 1}`)
|
||
})
|
||
|
||
test('归档覆盖同一行时保留最新明细(ASIN 可能被重新匹配改过)', () => {
|
||
const oldRow: Row = { shopName: '店铺A', shopId: 1, asins: ['旧ASIN'] }
|
||
const newRow: Row = { shopName: '店铺A', shopId: 1, asins: ['新ASIN'] }
|
||
|
||
const merged = mergeDispatchedArchive([oldRow], [newRow], keyOf)
|
||
|
||
assert.deepEqual(merged.map((r) => r.asins), [['新ASIN']])
|
||
})
|
||
|
||
test('派发提示要说清"再点启动只跑剩余",且剩余为 0 时不谎称还有', () => {
|
||
// 数字必须真从入参来:写成 /剩余|5/ 这类或关系,把剩余数硬编码成 0 也照样绿
|
||
assert.match(dispatchedPruneNotice(3, 5), /已派发 3 家/)
|
||
assert.match(dispatchedPruneNotice(3, 5), /还剩 5 家/)
|
||
assert.match(dispatchedPruneNotice(3, 5), /只跑剩余/)
|
||
assert.doesNotMatch(dispatchedPruneNotice(3, 0), /\d 家未派发|还剩 \d 家/)
|
||
assert.match(dispatchedPruneNotice(3, 0), /已清空/)
|
||
})
|