Files
crawler-plugin/admin-frontend-vue/tests/ops-cell-must-wrap.test.ts
T
huangzd1997 c216cd93fb fix(后管表格): 操作列给 px 下限并统一可换行,店铺密钥整表 1538→1227px
三张截图同一个结构缺陷的两个方向:操作列 <td> 上直接 display:flex + overflow:hidden +
white-space:nowrap,而列宽是声明死的,超宽时的失败模式是"按钮静默消失"——DOM 里在、
事件也在,就是看不见点不到。
  - 写死 px(店铺密钥线上 160,三颗按钮实测需 215.5)⇒ 从右裁,删除整颗没了;
  - 写百分比(最低价 ASIN 9%,两颗需 114)⇒ 百分比跟容器一起缩,且 .asin-col-actions
    带 justify-content:flex-end 把溢出翻到左侧,于是"配置"被吃掉而"删除"看着完好。

flex-wrap 单独不够:900px 容器下 9% 只有 56.7px,连一颗 53px 按钮加 24px padding 都装不下,
换行之后照样裁。所以列必须有 px 下限。OPS_MIN_COL_PX 是 browser-use 在 100% 缩放下渲染
.btn.btn-sm 量的 offsetWidth(两字 53/四字 80/五字 93.5)+ gap 8 + td 左右 padding 24,
三元文案按较长分支计,不是按字数估的。

改动落在 18 处操作列里不达标的 6 处:UserSecrets 250→315、RecordsDigitalHuman 240→290、
DedupeRegistry 无声明→140、QueryAsin 与 SkipPrice 9%→140 并补 flex-wrap、
ShopKeys 270→245。另把店铺密钥整表 1538→1227px(代理 190→90、紫鸟令牌 220→160、
白名单状态 150→110 等按实测内容宽收窄):那页 100% 缩放下内容区只有 ~1280,
"操作"整列要横向滚动才看到,这才是"没显示全"的主账。

护栏扩到 5 条。一个真发现:旧护栏只扫 .ops-cell,扫不到 .asin-col-actions——这就是
6d835e94 声称"扫过 8 个页面"却把最低价 ASIN 两页整个留在盲区的原因。现补上双向登记表
相等断言(新增页面不登记就红)。过程中护栏抓到我自己写的缺陷:<th[^>]*> 会连 <thead>
一起吃掉,再被新加的 HTML 注释里的"操作"两字喂饱,把"序号 52px"当成操作列宽度报出来;
已改为排除 <thead> + 解析前剥注释,并补一条内存反例自证(专喂"注释里写 flex-wrap 但代码
没设"和"百分比列"两种形状),防止尺子变恒绿。

验证:干净 worktree(9d762c55 + 只本批 7 个文件,git status 恰好 7 行 M)实跑前端 1765 例
0 红、vue-tsc rc=0、vite build rc=0;产物正反锚点已核 ShopKeys chunk 245px→1/旧值
270px→0、SkipPrice chunk 140px→1/旧值 9%→0。真浏览器复验 1280/1440/1516 三种容器下
三页 hScroll=0 且操作列不裁,900px 极端容器下仍不裁、只有文本列走 ellipsis(都有 title)。
主树混合态另跑 1769 例 0 红,差的 4 例是并行会话未提交的测试,与本批无关。
2026-10-08 20:59:49 +08:00

205 lines
9.7 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 { existsSync, readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'
import { readSource } from './helpers.ts'
/**
* 操作列的按钮一旦超过列宽,会**整颗消失**而不是换行:这些页的 td 是 `overflow:hidden` +
* `table-layout:fixed`,而 `.ops-cell` / `.asin-col-actions` 是 nowrap flex。2026-10-08 店铺密钥
* 那单"没有删除功能"就是这么来的——按钮在线上包的 DOM 里、事件也在,只是被裁到看不见点不到
* (列给 160px、实测需 215.5px)。
*
* 两条失败方向都见过:
* - 列宽写死 px(店铺密钥 160)⇒ 从**右**裁,最后一颗(删除)整颗没了;
* - 列宽写百分比(最低价 ASIN `9%`)⇒ 百分比跟着容器一起缩,且 `.asin-col-actions` 带
* `justify-content: flex-end`,溢出翻到**左**侧,于是"配置"被吃掉、"删除"看着完好。
* 所以光补 `flex-wrap` 不够:900px 容器下 9% 只有 56.7px,连一颗 53px 的按钮加 24px padding
* 都装不下,wrap 之后照样裁。列必须有 **px 下限**。
*
* 下面 OPS_MIN_COL_PX 是实测不是估算:browser-use 在 100% 缩放下渲染 `.btn.btn-sm` 量的
* offsetWidth(两字 53 / 四字 80 / 五字 93.5),加 `gap: 8px` 与 td 左右 padding 各 12px。
* 三元文案按**较长**分支计(`检测中…` vs `检测白名单` 取后者)。
*/
const SRC = 'src/pages'
/** 每页操作列「单行装下全部按钮」所需的列最小宽(px,实测算出)。 */
const OPS_MIN_COL_PX: Record<string, number> = {
'src/pages/account/CompaniesPage.vue': 77,
'src/pages/account/GroupsPage.vue': 138,
'src/pages/account/MenusPage.vue': 138,
'src/pages/account/UserSecretsPage.vue': 314,
'src/pages/account/UsersPage.vue': 138,
'src/pages/asin/AsinInvalidPage.vue': 138,
'src/pages/asin/DedupeRegistryPage.vue': 138,
'src/pages/asin/ProductCategoryPage.vue': 138,
'src/pages/asin/QueryAsinPage.vue': 138,
'src/pages/asin/SkipPricePage.vue': 138,
'src/pages/records/DeviceLogsPage.vue': 138,
'src/pages/records/RecordsDigitalHumanVersionPage.vue': 287,
'src/pages/records/RecordsSoftwareVersionPage.vue': 77,
'src/pages/records/RecordsTutorialPage.vue': 77,
'src/pages/shop/ShopKeysPage.vue': 240,
'src/pages/shop/ShopManagePage.vue': 199,
}
/** 操作列的 flex 容器类名。漏一个就是把一整族页面留在盲区里(`.asin-col-actions` 曾这么漏过两页)。 */
const OPS_CELL_CLASSES = ['ops-cell', 'asin-col-actions']
function vueFiles(dir: string): string[] {
const out: string[] = []
for (const name of readdirSync(dir)) {
const p = join(dir, name)
if (statSync(p).isDirectory()) out.push(...vueFiles(p))
else if (name.endsWith('.vue')) out.push(p)
}
return out
}
/** 相对工程根的正斜杠路径。 */
function rel(file: string): string {
return file.replace(/\\/g, '/')
}
interface Cell {
file: string
buttons: number
wrapped: boolean
}
/** 只做解析,不碰磁盘:读源码这一步留在用例体内(见 source-pin-ratchet 对"隔层 helper"的口径)。 */
function opsCells(sources: Array<{ file: string; source: string }>): Cell[] {
const cells: Cell[] = []
for (const { file, source } of sources) {
// 注释先剥掉:CSS 注释里的 `flex-wrap: wrap;` 会把"这条页已经能换行"判成恒真。
const markup = stripHtmlComments(source)
let fileCells = 0
for (const cls of OPS_CELL_CLASSES) {
const block = new RegExp('\\.' + cls + '[ \\t]*\\{([^}]*)\\}').exec(source)
if (!block) continue
const td = new RegExp('<td[^>]*class="[^"]*' + cls + '[^"]*"[^>]*>([\\s\\S]*?)<\\/td>').exec(markup)
const buttons = td ? (td[1].match(/<button/g) || []).length : 0
// 行锚 + 分号:不锚就会被块内注释里的散文喂饱(本仓踩过一次)。
const wrapped = /\n[ \t]*flex-wrap:[ \t]*wrap[ \t]*;/.test(stripCssComments(block[1]))
cells.push({ file: rel(file), buttons, wrapped })
fileCells += 1
}
if (fileCells === 0 && OPS_MIN_COL_PX[rel(file)]) {
// 登记表里有这页、扫描却认不出它的 flex 容器 ⇒ 形状与源码脱节,单独报出来。
cells.push({ file: rel(file), buttons: -1, wrapped: false })
}
}
return cells
}
function stripHtmlComments(s: string): string {
return s.replace(/<!--[\s\S]*?-->/g, '')
}
function stripCssComments(s: string): string {
return s.replace(/\/\*[\s\S]*?\*\//g, '')
}
/**
* 抓「操作」列的 <th> 宽度声明:返回 null = 压根没声明,返回 '9%' 这类 = 非 px。
* 两处坑都踩过:`<th[^>]*>` 会连 `<thead>` 一起吃掉;HTML 注释里出现"操作"就会把
* 匹配范围截在第一个真 <th> 之前,于是把"序号 52px"当成操作列宽度报出来。
*/
function opsColDecl(source: string): string | null {
const cleaned = stripHtmlComments(source)
const m = /<th(?![\w-])([^>]*)>(?:(?!<\/th>)[\s\S])*?操作(?:(?!<\/th>)[\s\S])*?<\/th>/.exec(cleaned)
if (!m) return '__no_header__'
const style = /style="[^"]*width:\s*([^;"]+)/.exec(m[1])
return style ? style[1].trim() : null
}
function sourcesOfAll(): Array<{ file: string; source: string }> {
return vueFiles(SRC).map((file) => ({ file, source: readSource(file) }))
}
test('ops_ruler_actually_sees_the_family', () => {
assert.ok(existsSync(SRC), `扫不到 ${SRC}:目录挪了就是整面恒绿`)
const cells = opsCells(sourcesOfAll())
assert.ok(cells.length >= Object.keys(OPS_MIN_COL_PX).length,
`只认出 ${cells.length} 个操作列容器,登记表有 ${Object.keys(OPS_MIN_COL_PX).length} 页`
+ ' ⇒ 扫描形状与源码脱节,下面的断言已经失明')
const multi = cells.filter((c) => c.buttons >= 2)
assert.ok(multi.length >= 13,
`只认出 ${multi.length} 页操作列有 ≥2 颗按钮(今天 13 页)⇒ 按钮计数读不到,无从判定`)
})
test('ops_cell_registry_matches_scanned_files_both_ways', () => {
const scanned = new Set(opsCells(sourcesOfAll()).map((c) => c.file))
const registered = new Set(Object.keys(OPS_MIN_COL_PX))
const unregistered = [...scanned].filter((f) => !registered.has(f))
const vanished = [...registered].filter((f) => !scanned.has(f))
assert.deepEqual(unregistered, [],
'这些页有操作列但没登记列宽下限 ⇒ 新增页面必须显式登记,否则这条看守对它无效')
assert.deepEqual(vanished, [],
'登记表里这些页已经扫不到了 ⇒ 页面删了要同步删表项,不许留死条目')
})
test('every_multi_button_ops_cell_wraps', () => {
const offenders = opsCells(sourcesOfAll())
.filter((c) => c.buttons >= 2 && !c.wrapped)
.map((c) => `${c.file}(${c.buttons} 颗按钮)`)
assert.deepEqual(offenders, [],
'这些页的操作列是 nowrap flex + td overflow:hidden:按钮总宽一旦超过列宽,'
+ '最后一颗会整颗被裁掉(看不见也点不到),补一行 flex-wrap: wrap 即可')
})
test('ops_column_width_is_px_and_wide_enough', () => { const bad: string[] = []
for (const [file, minPx] of Object.entries(OPS_MIN_COL_PX)) {
const decl = opsColDecl(readSource(file))
if (decl === '__no_header__') { bad.push(`${file} 找不到「操作」列表头`); continue }
if (decl === null) { bad.push(`${file} 操作列 <th> 没声明宽度(fixed layout 下拿剩余空间,可被压到 0)`); continue }
const px = /^(\d+(?:\.\d+)?)px$/.exec(decl)
if (!px) { bad.push(`${file} 操作列声明 ${decl}:百分比会跟着容器缩,缩到装不下按钮就从左侧静默裁切`); continue }
if (parseFloat(px[1]) < minPx) {
bad.push(`${file} 操作列 ${px[1]}px < 实测需 ${minPx}px(按钮占宽 + gap + 左右 padding 24)`)
}
}
assert.deepEqual(bad, [], '操作列必须是 px 且 ≥ 实测需求,否则超宽时的失败模式是"按钮静默消失"')
})
/**
* 尺子自己得可证伪:下面四份内存假源码各对应一种"会判错"的形状,
* 判不出来就是恒绿——本仓在"注释喂饱判据"和"`<th` 连 `<thead>` 一起吃"上各踩过一次。
*/
test('ruler_is_falsifiable_on_in_memory_counterexamples', () => {
const withCommentTrap = [
'<thead>',
' <!-- 刚性列(序号/两个时间/操作)给准 px -->',
' <tr>',
' <th style="width: 52px">序号</th>',
' <th style="width: 245px">操作</th>',
' </tr>',
'</thead>',
].join('\n')
assert.equal(opsColDecl(withCommentTrap), '245px',
'注释里的"操作"把匹配截在第一个真 <th> 之前 ⇒ 会把"序号"的宽度当成操作列宽度')
assert.equal(opsColDecl('<tr><th>操作</th></tr>'), null,
'<th> 没写 style 时必须报 null,不能悄悄放过')
assert.equal(opsColDecl('<tr><th style="width: 9%">操作</th></tr>'), '9%',
'百分比必须原样报出来,由调用方判成违规')
assert.equal(opsColDecl('<tr><th>别的列</th></tr>'), '__no_header__',
'找不到「操作」表头要显式报,不能回 null 与"没声明"混成一类')
const wrapOnlyInComment = [
'<td class="ops-cell"><button class="btn"></button><button class="btn"></button></td>',
'<style>',
'.ops-cell {',
' display: flex;',
' /* 这里写了 flex-wrap: wrap; 但代码其实没设 */',
'}',
'</style>',
].join('\n')
const cells = opsCells([{ file: 'src/pages/asin/TrapPage.vue', source: wrapOnlyInComment }])
assert.equal(cells.length, 1, '假源码没被认出容器 ⇒ 扫描形状与源码脱节')
assert.equal(cells[0].buttons, 2, `按钮数应认出 2,实际 ${cells[0].buttons}`)
assert.equal(cells[0].wrapped, false,
'CSS 注释里的 flex-wrap: wrap; 被当成了真声明 ⇒ 这条看守对"只写在注释里"的修复恒绿')
})