2aa05398f9
- 新纯函数 jd-category-hierarchy.ts:京麦授权类目真树(GET /api/jd-shop-category-tree, 按平台 node_id 并集)× /categories 末级名计数,名称精确等值挂接;计数按去重名字集合 求和(同名跨支不重复计)、零计数分支剪枝、树外历史名进「平台树外」兜底组; 树空=退化末级平铺(上一窗行为逐字相同),没有内置快照——第二份树不再存在 - 新 jd-shop-category-api.ts:只剩服务端真树一条路,失败/空静默回空数组 - 侧栏层级渲染:展开/收起、点父级=整棵子树末级名集合过滤(11-18 口径恢复)、 挂载即展开选中行祖先链;同名跨支按路径核选中态 - filter/api:categoryPath(只喂回显展开)+categoryNames(选中那一刻展开好的过滤集合) 复活,下发走后端既有 category IN 多值;清选时两者一起清,不留幽灵谓词 - 判据:jd-category-hierarchy.test.ts(挂接/去重/剪枝/兜底/平铺退化)+ sidebar/database-page mount(真树层级、父级点选下发集合、树空平铺、回显展开)
182 lines
8.8 KiB
TypeScript
182 lines
8.8 KiB
TypeScript
/**
|
||
* 商品目录层级纯函数契约(2026-11-26 客户口径「怎么都没有分类一级二级分类」「管理员要
|
||
* 分类时候那么多怎么看」)——node --test 直跑(纯函数零依赖)。
|
||
*
|
||
* 覆盖:
|
||
* 1. `parseJdCategoryNodeTree` 脏值归一(空名节点丢自身保子级、value 缺失空串、超深截断);
|
||
* 2. `buildJdCatalogTree` 三个口径:计数按**去重名字集合**求和(同名跨支不重复计)、
|
||
* 零计数分支剪枝(但保底「树外兜底」)、树空退化为末级平铺(上一窗行为逐字不变);
|
||
* 3. `flattenJdCatalogRows` 折叠语义(折叠节点的子孙不出现);
|
||
* 4. `findJdCatalogPath` / `jdCatalogPathIndex` / `findJdCatalogPathByKey` 的定位口径
|
||
* (点行下发 path、挂载展开祖先链都靠它)。
|
||
*/
|
||
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
|
||
import {
|
||
buildJdCatalogTree,
|
||
findJdCatalogPath,
|
||
findJdCatalogPathByKey,
|
||
flattenJdCatalogRows,
|
||
jdCatalogPathIndex,
|
||
parseJdCategoryNodeTree,
|
||
type JdCategoryNode,
|
||
} from '../src/pages/jd/jd-category-hierarchy.ts'
|
||
|
||
/** 本店真机形状的缩影(生产实测:商品末级名 100% 精确命中树节点名)。 */
|
||
const TREE: JdCategoryNode[] = [
|
||
{
|
||
name: '厨房卫浴',
|
||
value: '1',
|
||
children: [
|
||
{
|
||
name: '厨卫挂件',
|
||
value: '11',
|
||
children: [
|
||
{ name: '其他收纳架', value: '111' },
|
||
{ name: '牙刷架/牙具座', value: '112' },
|
||
],
|
||
},
|
||
{ name: '沐浴桶', value: '12', children: [{ name: '泡脚桶/袋/盆', value: '121' }] },
|
||
],
|
||
},
|
||
{
|
||
name: '家居饰品',
|
||
value: '2',
|
||
children: [
|
||
{ name: '摆件', value: '21', children: [{ name: '桌面摆件', value: '211' }] },
|
||
],
|
||
},
|
||
// 零计数整支:授权树里本店可卖、但这批采集没采到的类目。
|
||
{
|
||
name: '服饰内衣',
|
||
value: '3',
|
||
children: [{ name: '女装', value: '31', children: [{ name: '连衣裙', value: '311' }] }],
|
||
},
|
||
]
|
||
|
||
test('parseJdCategoryNodeTree:空名节点丢自身保子级、value 缺失按空串、超深截断', () => {
|
||
const parsed = parseJdCategoryNodeTree([
|
||
{ name: ' ', value: '9', children: [{ name: '子级', value: '91' }] },
|
||
{ value: '8', children: [{ name: '孤儿', value: '81' }] },
|
||
{ name: '有值', value: 7 },
|
||
'not-an-object',
|
||
])
|
||
assert.deepEqual(parsed.map((node) => node.name), ['子级', '孤儿', '有值'])
|
||
assert.equal(parsed[2].value, '7', '数字 value 归一成字符串')
|
||
assert.equal(parseJdCategoryNodeTree([{ name: 'x' }])[0].value, '', 'value 缺失按空串')
|
||
assert.deepEqual(parseJdCategoryNodeTree('bad'), [], '非数组回空')
|
||
|
||
// 深度护栏:9 层嵌套只保留前 9 层(depth > 8 起截断),不抛栈。
|
||
let deep: unknown = { name: 'L9', value: '9' }
|
||
for (let level = 8; level >= 1; level -= 1) deep = { name: `L${level}`, value: `${level}`, children: [deep] }
|
||
let node: JdCategoryNode | undefined = parseJdCategoryNodeTree([deep])[0]
|
||
let depth = 0
|
||
while (node) {
|
||
depth += 1
|
||
node = node.children?.[0]
|
||
}
|
||
assert.ok(depth >= 1 && depth <= 9, `深度应被截断在 9 层内,实际 ${depth}`)
|
||
})
|
||
|
||
test('buildJdCatalogTree:父级徽标=子树命中行数,末级=自身名行数,总数=全部行求和', () => {
|
||
const catalog = buildJdCatalogTree(TREE, [
|
||
{ name: '其他收纳架', productCount: 17 },
|
||
{ name: '牙刷架/牙具座', productCount: 2 },
|
||
{ name: '泡脚桶/袋/盆', productCount: 32 },
|
||
{ name: '桌面摆件', productCount: 9 },
|
||
])
|
||
const kitchen = catalog.roots.find((row) => row.name === '厨房卫浴')!
|
||
assert.equal(kitchen.productCount, 51, '一级=51(17+2+32)')
|
||
assert.equal(kitchen.isLeaf, false)
|
||
assert.deepEqual(kitchen.names.slice().sort(),
|
||
['厨房卫浴', '厨卫挂件', '其他收纳架', '牙刷架/牙具座', '沐浴桶', '泡脚桶/袋/盆'].sort(),
|
||
'父级点选下发的名字集合=整棵子树的节点名(主体是末级,父级名带上兜底)')
|
||
const hook = kitchen.children.find((row) => row.name === '厨卫挂件')!
|
||
assert.equal(hook.productCount, 19, '二级=19(17+2)')
|
||
assert.equal(hook.depth, 1)
|
||
const leaf = hook.children.find((row) => row.name === '其他收纳架')!
|
||
assert.equal(leaf.productCount, 17)
|
||
assert.equal(leaf.isLeaf, true, '无子级=末级(单名过滤)')
|
||
assert.equal(leaf.depth, 2)
|
||
assert.deepEqual(leaf.names, ['其他收纳架'])
|
||
assert.equal(catalog.totalCount, 60, '「全部商品」=全部计数行求和')
|
||
assert.deepEqual(catalog.orphans, [], '全部命中树,无兜底行')
|
||
})
|
||
|
||
test('buildJdCatalogTree:零计数分支剪掉(授权树≠有货树),整树都被剪时回空根', () => {
|
||
const catalog = buildJdCatalogTree(TREE, [{ name: '桌面摆件', productCount: 9 }])
|
||
assert.deepEqual(catalog.roots.map((row) => row.name), ['家居饰品'],
|
||
'「厨房卫浴」「服饰内衣」两支全 0 ⇒ 不出现(否则四百多节点铺满侧栏,反而看不清)')
|
||
assert.equal(catalog.roots[0].productCount, 9)
|
||
assert.deepEqual(buildJdCatalogTree(TREE, []).roots, [], '一行都没有 ⇒ 空根(侧栏走空态)')
|
||
})
|
||
|
||
test('buildJdCatalogTree:同名跨支按去重名字集合求和,父级徽标不重复计', () => {
|
||
const tree: JdCategoryNode[] = [
|
||
{ name: '内衣', value: 'a', children: [{ name: '吊带/背心', value: 'a1' }] },
|
||
{ name: '女装', value: 'b', children: [{ name: '吊带/背心', value: 'b1' }] },
|
||
]
|
||
const catalog = buildJdCatalogTree(tree, [{ name: '吊带/背心', productCount: 5 }])
|
||
assert.deepEqual(catalog.roots.map((row) => row.productCount), [5, 5],
|
||
'两个一级各自 5(同一批行按名字命中两支),不是各 2.5 也不是 10')
|
||
assert.equal(catalog.totalCount, 5, '「全部商品」只算一次')
|
||
})
|
||
|
||
test('buildJdCatalogTree:树外的历史末级名进兜底组(不藏、按行数降序)', () => {
|
||
const catalog = buildJdCatalogTree(TREE, [
|
||
{ name: '其他收纳架', productCount: 17 },
|
||
{ name: '已下架历史类目', productCount: 12 },
|
||
{ name: '另一个历史类目', productCount: 3 },
|
||
])
|
||
assert.deepEqual(catalog.orphans.map((row) => row.name), ['已下架历史类目', '另一个历史类目'],
|
||
'树里搜不到的名字单列一组,按行数降序——藏了这些商品就点不到')
|
||
assert.equal(catalog.totalCount, 32, '兜底行的行数照样进「全部商品」')
|
||
})
|
||
|
||
test('buildJdCatalogTree:树为空 ⇒ 退化为末级平铺(上一窗行为逐字不变)', () => {
|
||
const catalog = buildJdCatalogTree([], [
|
||
{ name: '连衣裙', productCount: 7 },
|
||
{ name: ' ', productCount: 4 },
|
||
{ name: '短裤', productCount: -1 },
|
||
])
|
||
assert.deepEqual(catalog.roots.map((row) => [row.name, row.productCount, row.depth, row.isLeaf]), [
|
||
['连衣裙', 7, 0, true],
|
||
], '空名/非正计数不进列表;平铺行 depth 0、按末级处理')
|
||
assert.deepEqual(catalog.orphans, [], '没有树就没有「树外」这个概念')
|
||
assert.equal(catalog.totalCount, 7)
|
||
})
|
||
|
||
test('flattenJdCatalogRows:折叠节点的子孙不进列表;展开哪个只影响那个分支', () => {
|
||
const catalog = buildJdCatalogTree(TREE, [
|
||
{ name: '其他收纳架', productCount: 17 },
|
||
{ name: '桌面摆件', productCount: 9 },
|
||
])
|
||
assert.deepEqual(flattenJdCatalogRows(catalog.roots, []).map((row) => row.name),
|
||
['厨房卫浴', '家居饰品'], '默认全收起:先看清有哪些大类')
|
||
|
||
const kitchen = catalog.roots.find((row) => row.name === '厨房卫浴')!
|
||
const opened = flattenJdCatalogRows(catalog.roots, [kitchen.key])
|
||
assert.deepEqual(opened.map((row) => row.name), ['厨房卫浴', '厨卫挂件', '家居饰品'],
|
||
'只展开一级 ⇒ 出它的二级,别的分支不动')
|
||
|
||
const fully = flattenJdCatalogRows(catalog.roots, [kitchen.key, '11'])
|
||
assert.deepEqual(fully.map((row) => row.name), ['厨房卫浴', '厨卫挂件', '其他收纳架', '家居饰品'])
|
||
})
|
||
|
||
test('路径定位:点行下发一级到本行的名称链;挂载按选中行展开祖先', () => {
|
||
const catalog = buildJdCatalogTree(TREE, [
|
||
{ name: '其他收纳架', productCount: 17 },
|
||
{ name: '桌面摆件', productCount: 9 },
|
||
])
|
||
const leaf = findJdCatalogPathByKey(catalog.roots, '111')
|
||
assert.deepEqual(leaf.map((row) => row.name), ['厨房卫浴', '厨卫挂件', '其他收纳架'])
|
||
assert.deepEqual(findJdCatalogPath(catalog.roots, '桌面摆件').map((row) => row.name),
|
||
['家居饰品', '摆件', '桌面摆件'])
|
||
assert.deepEqual(findJdCatalogPath(catalog.roots, '不存在的类目'), [],
|
||
'选中名不在树里(树外兜底行)⇒ 空链,侧栏不展开任何分支')
|
||
assert.deepEqual(findJdCatalogPathByKey(catalog.roots, ''), [])
|
||
|
||
const index = jdCatalogPathIndex(catalog.roots)
|
||
assert.deepEqual(index.get('21'), ['家居饰品', '摆件'], '索引供点行时算 path')
|
||
}) |