Files
crawler-plugin/admin-frontend-vue/tests/ops-cell-must-wrap.test.ts
T

208 lines
9.9 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,
// 京东分类管理(V204):改名/删除两颗 btn-sm(两字 53×2+gap+左右 padding 24)。
'src/pages/jd/JdCategoryManagerPage.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/records/WorkbenchClientPackagePage.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; 被当成了真声明 ⇒ 这条看守对"只写在注释里"的修复恒绿')
})