import test from 'node:test' import assert from 'node:assert/strict' import { readSource } from './helpers.ts' import { appendCategoryChildren, isCategoryLoadMoreNode, makeCategoryLoadMoreNode, productCategoryNodeMeta, productCategorySourceLabel, type ProductCategoryNode, } from '../src/pages/asin/product-category-model.ts' /** 对齐 admin.js:5998-6012/6077-6120:树节点带来源/排序/备注、按需懒加载 + 加载更多(已加载 x/y)。 */ function node(id: number, overrides: Partial = {}): ProductCategoryNode { return { id, parentId: null, name: `类目${id}`, categoryKey: `k${id}`, sortOrder: id, description: '', isBuiltin: false, childCount: 0, level: 0, path: `类目${id}`, children: [], ...overrides, } } test('align_category_source_label', () => { assert.equal(productCategorySourceLabel({ isBuiltin: true } as ProductCategoryNode), '内置') assert.equal(productCategorySourceLabel({ isBuiltin: false } as ProductCategoryNode), '自定义') assert.equal(productCategorySourceLabel(null), '自定义') }) test('align_category_node_meta_contains_source_and_sort', () => { assert.equal(productCategoryNodeMeta({ isBuiltin: true, sortOrder: 3 } as ProductCategoryNode), '内置 · 排序 3') assert.equal(productCategoryNodeMeta({ isBuiltin: false, sortOrder: null } as ProductCategoryNode), '自定义 · 排序 0') }) test('align_category_load_more_node_factory', () => { const more = makeCategoryLoadMoreNode(5, 20, 45) assert.equal(isCategoryLoadMoreNode(more), true) assert.equal(more.parentId, 5) assert.equal(more.loadedCount, 20) assert.equal(more.totalCount, 45) assert.equal(isCategoryLoadMoreNode(node(1)), false) }) test('align_category_append_children_keeps_more_until_done', () => { const current = [node(1), makeCategoryLoadMoreNode(null, 20, 45)] const next = appendCategoryChildren(current, [node(2), node(3)], null, 40, 45, true) assert.deepEqual(next.filter((n) => !isCategoryLoadMoreNode(n)).map((n) => n.id), [1, 2, 3]) const tail = next[next.length - 1] assert.equal(isCategoryLoadMoreNode(tail), true) assert.equal((tail as ReturnType).loadedCount, 40) }) test('align_category_append_children_drops_more_when_complete', () => { const current = [node(1), makeCategoryLoadMoreNode(9, 20, 22)] const next = appendCategoryChildren(current, [node(2), node(3)], 9, 22, 22, false) assert.deepEqual(next.map((n) => n.id), [1, 2, 3]) assert.equal(next.some((n) => isCategoryLoadMoreNode(n)), false) }) test('align_category_children_api_wiring', () => { const api = readSource('src/pages/asin/product-category-api.ts') assert.match(api, /children/, '懒加载走 children 端点') assert.match(api, /parent_id|parentId/, '带父级参数') assert.match(api, /page_size|pageSize/, '分页参数') }) test('align_category_page_tree_and_search_wiring', () => { const page = readSource('src/pages/asin/ProductCategoryPage.vue') assert.doesNotMatch(page, /default-expand-all/, '树不再全量展开,改按需懒加载') assert.match(page, /加载更多/, '节点带加载更多') assert.match(page, /已加载/, '显示已加载 x/y') assert.match(page, /productCategorySourceLabel/, '节点展示来源') assert.match(page, /排序/, '节点展示排序') assert.doesNotMatch(page, /新增根类目/, '按钮名对齐参考“新增类目”') assert.match(page, /新增类目/, '顶部新增类目按钮') assert.match(page, /搜索类目名称、编码或备注/, '搜索占位对齐参考') assert.match(page, /来源/, '搜索表格补来源列') assert.match(page, /childCount/, '删除按钮按有子禁用') }) /** * 列宽算术闸门(2026-10-06 控制方圈出:层级路径整列只剩一个字、备注只剩一个破折号)。 * `.table-scroll table` 是 table-layout:fixed —— 首行 th 定宽后,列宽之和一旦超过表格实宽, * 浏览器把各列等比压缩,而没写宽度的 auto 列第一个被压到 0px。旧写法(类目 34% + 两个 auto 列 + * 700px 固定列)在表格宽度 < 1061px 时就是这么塌的;min-width 当时是 860px,比列宽之和还小, * 等于下限自己把塌缩区放进了合法区间。 * * 闸门只认三条,其中真正承重的是前两条(浏览器实测,同一份 CSS 与真实 thead 生成的复现页): * 旧写法在表格宽度 1224px 时把 层级路径 与 备注 量成 0px,而 8 列全显式定宽后在 1920/1440/1100/900 * 四档没有一列低于 90px —— 也就是"把 min-width 抬到 1224"单独并不能救旧写法,auto 列才是塌的那一根。 * 第三条(min-width >= 列宽之和)管的是成对维护:加一列却忘了抬下限会直接红。 * 求和从 markup 现算,不是抄常量——把某列从 220px 改成 400px 而忘了同步会直接红。 * (注:单元格左右各 12px padding 是加在列宽之外的,所以渲染出的表格自然宽度是"列宽之和 + 24×8 = 1416px", * 与这里比对的 1224 不是同一个数,别混。) */ function categoryHeaderColumns(page: string): Array<{ label: string; px: number | null }> { const theadBlocks = page.match(/[\s\S]*?<\/thead>/g) ?? [] assert.equal(theadBlocks.length, 1, '类目页应只有一个 thead(本闸门按单个表头求列宽之和)') const columns: Array<{ label: string; px: number | null }> = [] for (const cell of theadBlocks[0].matchAll(/]*>([^<]*)<\/th>/g)) { const style = cell[0].match(/style="([^"]*)"/)?.[1] ?? '' const px = /width:\s*(\d+)px/.exec(style)?.[1] columns.push({ label: cell[1].trim(), px: px === undefined ? null : Number(px) }) } return columns } function categoryTableMinWidth(page: string): number | null { const block = page.match(/\.category-table-scroll\s*>\s*table\s*\{([^}]*)\}/)?.[1] ?? '' const px = /min-width:\s*(\d+)px/.exec(block)?.[1] return px === undefined ? null : Number(px) } test('align_category_fixed_layout_never_leaves_a_column_to_be_squeezed', () => { const page = readSource('src/pages/asin/ProductCategoryPage.vue') // 前提也要钉:这条闸门只在 fixed 布局下成立,改成 auto 布局时它该喊,而不是安静地继续绿。 // 窗口用 [^}] 而不是定长字符数:本页 CSS 注释里就写着 "table-layout:fixed 下……" 这句话, // 定长窗口能跨过 } 把注释喂成"前提成立"(变异自证时实测过一次 SURVIVED)。 assert.match(page, /\.table-scroll\s+table\s*\{[^}]*table-layout:\s*fixed/, '本闸门的前提:表格是 table-layout:fixed') const columns = categoryHeaderColumns(page) assert.equal(columns.length, 8, `表头应有 8 列(与 对齐),实际 ${columns.length}:${JSON.stringify(columns)}`) const unwidthed = columns.filter((c) => c.px === null).map((c) => c.label) assert.deepEqual(unwidthed, [], 'fixed 布局下没写宽度的列会在容器变窄时被第一个压到 0px:' + unwidthed.join('/')) assert.equal(/width:\s*\d+%/.test(page.match(/[\s\S]*?<\/thead>/)?.[0] ?? ''), false, '表头不许用百分比:百分比与 px 混排时列宽之和随表格宽度变,下限就无从比对') const sum = columns.reduce((acc, c) => acc + (c.px ?? 0), 0) const minWidth = categoryTableMinWidth(page) assert.notEqual(minWidth, null, '.category-table-scroll > table 必须有 px 下限,否则塌缩区又合法了') assert.ok((minWidth ?? 0) >= sum, `min-width(${minWidth}px) 必须 >= 8 列宽度之和(${sum}px):否则表格落在 [min-width, 列宽和] 这一段时列被等比压缩`) // 控制方圈出的那两列是这次的直接症状:它们是文字列,宽度下限给死,别被"重新调配比"顺手砍回去。 for (const label of ['层级路径', '备注']) { const col = columns.find((c) => c.label === label) assert.ok(col && (col.px ?? 0) >= 120, `${label} 列宽不得低于 120px,实际 ${col?.px ?? '未定义'}px`) } })