Files
crawler-plugin/admin-frontend-vue/tests/align-product-category.test.ts
T
huangzd1997 b7acfc0eef feat(菜单权限): 每家公司一套菜单树(V149)+ 公司 tab 只在注册表页渲染
本批两件事,加上随本批一起入仓的并行会话在制品:

一、菜单权限按公司隔离(批次 6)
- V149 给 columns 加 company_id:先可空加列(末位、ALGORITHM=INSTANT)→ 回填公司 1 →
  MODIFY 收紧 NOT NULL;三段式缺一即撞 1048。同时建 (company_id, column_key) 复合键、
  把公司 2 的 16 笔授权按 column_key 改指向它自己的副本,三类 menu_type 全复制,
  admin_companies 作为角色内生闸门行不复制(D3)。
- 第三道归属门 PermissionMenuService.resolveScopeCompanyId:站点超管可带 company_id
  读写任意公司,非超管锁本公司、越界 403。它注册进 CompanyIdParamGoesThroughReadScopeTest
  的 SCOPE_DOORS,四条判据与 readScope / readCompanyIdOrOwn 共用——护栏在代码出口,不靠清单。
- 旁路种子 PermissionMenuSchemaInitializer 只种公司 1:31 段种子逐条钉公司,
  按 column_key 点查的 14 段全部带 company_id,否则摘了 DEFAULT 就是跨公司串写。
- CompanyService.create 同一次写入里给新公司复制模板树(D4)。

二、公司 tab 收口(批次 b)
tab 条挂在壳层,对 22 页全画;改为按正列表 companyTabAppliesTo(menuKey) 判定,
只有注册表页渲染。护栏钉在用户看见的那个出口。

三、并行会话在制品(控制方「全部提交」,未单独拆分——见下)
- V150 店铺密钥 biz_shop_key 按公司(readCompanyIdOrOwn + writeCompanyId 两道门)
- V151 品牌库/商品类目推翻为全平台共享:只退唯一键、company_id DEFAULT 保留为
  NOT NULL 承重约束(摘了每条 1364);删掉 ProductCategoryControllerCompanyScopeTest
  那支"按公司隔离"的旧护栏,readCompanyId 方法一并移除
- invalidasin 字段与外观专利合批的连带改动

为什么不拆笔:后端 3891 例 / 0 红、前端 1731 例 / 0 红这两个读数是"合起来的树"的读数,
拆成多笔会让每一笔落到从未一起验过的组合上,而这一版要直接上两台。

回滚脚本在 backend-java/docs/rollback-V149/V150/V151-*.sql,不放进 Flyway 扫描路径。
设计记录:docs/ops/menu-company-tree-design-2026-10-06.md
2026-10-06 17:17:14 +08:00

148 lines
7.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 { 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> = {}): 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<typeof makeCategoryLoadMoreNode>).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(/<thead>[\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\b[^>]*>([^<]*)<\/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 列(与 <td colspan="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(/<thead>[\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`)
}
})