Files
crawler-plugin/frontend-vue/tests/candidate-selection-memory-wiring.test.ts
T
huangzd1997 3e797ab159 feat(用户端): 摘掉跟价/查ASIN 勾选区的「清除勾选记忆」入口
代收在飞件落地(原作者那一路已不在跑,内容一字未改)。记忆的唯一出口改回"取消勾选本身"——
onSelectionChange 按当前窗口重算并写回 localStorage,红字清除按钮是第二条出口、两条会打架。
尺子随之从"必须有清除入口"反向钉成"勾选区不留清除入口与那段文案"。

⚠ 这条改动其实已随 app-web-20261017-161525 上线(迭代蓝图 §6 记的"整棵上线"带上的未提交件),
本笔只是把提交坐标补到现网实际行为之后,不是新发布。验证依据=同一轮闸门用户端 20 格全绿。
2026-10-18 09:52:54 +08:00

90 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} 勾选区不留红字清除入口`, () => {
// 记忆的唯一出口是取消勾选本身:onSelectionChange 按本窗口重算并写回(上一条用例钉的就是这条)
assert.ok(!source.includes('clearCandidateSelectionMemory'), '勾选区又长出了清除勾选记忆入口')
assert.ok(!source.includes('清除勾选记忆'), '勾选区又出现了红字提示文案')
})
}