feat(phase-1): 京东数据库 UI 改造蓝图 Phase 1——类目计数聚合纯函数+首屏默认档 50
This commit is contained in:
@@ -4,11 +4,13 @@ import {
|
||||
JD_CATEGORY_TREE,
|
||||
jdCategoryAllNodes,
|
||||
jdCategoryChildren,
|
||||
jdCategoryCountTree,
|
||||
jdCategoryLeaves,
|
||||
jdCategoryNamesUnder,
|
||||
jdCategoryRoots,
|
||||
jdCategorySearchLeaves,
|
||||
jdCategorySearchNodes,
|
||||
type JdCategoryCountedNode,
|
||||
} from '../src/pages/jd/jd-category-tree.ts'
|
||||
|
||||
/** 京麦授权类目真树(2026-11-16 真机取证 getVenderLicenseCategory 快照;与用户端
|
||||
@@ -132,3 +134,132 @@ test('子树展开集合:含自身+全部后代名、去重、不越支;
|
||||
const women = jdCategoryNamesUnder(['服饰内衣', '女装'])!
|
||||
assert.equal(new Set(women).size, women.length, '展开集合内部去重')
|
||||
})
|
||||
|
||||
/** v1.6 蓝图 Phase 1 备料:/categories 计数行 → 树形计数聚合(侧栏徽标的数据源)。 */
|
||||
/** 按名称路径取计数节点(与 jdCategoryChildren 同构的测试侧寻路)。 */
|
||||
const nodeAt = (roots: JdCategoryCountedNode[], path: string[]): JdCategoryCountedNode | null => {
|
||||
let level = roots
|
||||
let hit: JdCategoryCountedNode | null = null
|
||||
for (const name of path) {
|
||||
hit = level.find((node) => node.name === name) ?? null
|
||||
if (!hit) return null
|
||||
level = hit.children
|
||||
}
|
||||
return hit
|
||||
}
|
||||
|
||||
test('计数聚合——节点徽标=自身名+子树叶和,与 jdCategoryNamesUnder 展开集合同源', () => {
|
||||
const tree = jdCategoryCountTree([
|
||||
{ name: '艺考形体服', productCount: 5 },
|
||||
{ name: '啦啦队服', productCount: 3 },
|
||||
{ name: '拉丁舞服', productCount: 2 },
|
||||
{ name: '连衣裙', productCount: 7 },
|
||||
{ name: '短袖连衣裙', productCount: 4 },
|
||||
])
|
||||
assert.equal(tree.extra.length, 0, '树内名字不进兜底组')
|
||||
assert.equal(tree.extraTotal, 0)
|
||||
|
||||
const roots = tree.roots
|
||||
assert.equal(roots.length, 3, '一级节点全量保留(无词条的一级也在)')
|
||||
assert.deepEqual(
|
||||
roots.map((node) => node.name),
|
||||
['服饰内衣', '母婴', '运动户外'],
|
||||
)
|
||||
assert.equal(roots.find((node) => node.name === '母婴')!.count, 0, '无词条的一级归 0 不缺节点')
|
||||
|
||||
const sport = nodeAt(roots, ['运动户外'])!
|
||||
assert.equal(sport.count, 10, '运动户外=艺考5+啦啦3+拉丁2')
|
||||
assert.equal(sport.leaf, false)
|
||||
const dance = nodeAt(roots, ['运动户外', '舞蹈体操鞋服/装备'])!
|
||||
assert.equal(dance.count, 10, '二级下钻同和')
|
||||
const yikao = nodeAt(roots, ['运动户外', '舞蹈体操鞋服/装备', '艺考形体服'])!
|
||||
assert.equal(yikao.count, 5, '末级叶子=该名计数')
|
||||
assert.equal(yikao.leaf, true, 'children 空=末级')
|
||||
assert.deepEqual(yikao.children, [], '叶子 children 是空数组(UI 直接遍历不炸)')
|
||||
|
||||
const dress = nodeAt(roots, ['服饰内衣', '女装', '连衣裙'])!
|
||||
assert.equal(dress.count, 11, '连衣裙=自身名7+短袖4(长袖/无袖无词条归 0)')
|
||||
assert.equal(nodeAt(roots, ['服饰内衣', '女装'])!.count, 11, '女装只有连衣裙支有词条')
|
||||
assert.equal(roots.find((node) => node.name === '服饰内衣')!.count, 11, '一级汇总到根')
|
||||
|
||||
const dressLeaves = nodeAt(roots, ['服饰内衣', '女装', '连衣裙'])!.children.map((node) => node.name)
|
||||
assert.deepEqual(dressLeaves, ['短袖连衣裙', '长袖连衣裙', '无袖连衣裙'], '子节点形状与静态树逐字同')
|
||||
})
|
||||
|
||||
test('计数聚合——父级名本身是落库末级名:计入自身节点(快照层级可能比实际浅)', () => {
|
||||
const tree = jdCategoryCountTree([
|
||||
{ name: '女装', productCount: 6 },
|
||||
{ name: '短袖连衣裙', productCount: 1 },
|
||||
])
|
||||
const roots = tree.roots
|
||||
assert.equal(nodeAt(roots, ['服饰内衣', '女装'])!.count, 7, '女装徽标=自身6+子树1')
|
||||
assert.equal(roots.find((node) => node.name === '服饰内衣')!.count, 7)
|
||||
assert.equal(tree.extra.length, 0, '非叶名也是合法落库名,不进兜底组')
|
||||
})
|
||||
|
||||
test('计数聚合——同名跨支叶:两个分支都计入(点哪支都等值命中同一批行),父层去重不翻倍', () => {
|
||||
const tree = jdCategoryCountTree([{ name: '短裤', productCount: 9 }])
|
||||
const roots = tree.roots
|
||||
assert.equal(nodeAt(roots, ['服饰内衣', '女装'])!.count, 9, '女装支的短裤叶如实计入')
|
||||
assert.equal(nodeAt(roots, ['服饰内衣', '男装'])!.count, 9, '男装支的同名叶同计数(过滤语义非双计)')
|
||||
assert.equal(roots.find((node) => node.name === '服饰内衣')!.count, 9,
|
||||
'父层子树按名字去重求和:展开集合里短裤只出现一次,徽标不得翻倍成 18')
|
||||
assert.equal(tree.extra.length, 0)
|
||||
})
|
||||
|
||||
test('计数聚合——树外兜底组:树上任何节点名都对不上的历史行单独归组', () => {
|
||||
const tree = jdCategoryCountTree([
|
||||
{ name: '已下架历史类目', productCount: 12 },
|
||||
{ name: '另一个树外名', productCount: 8 },
|
||||
{ name: '艺考形体服', productCount: 5 },
|
||||
])
|
||||
assert.deepEqual(
|
||||
tree.extra.map((row) => row.name),
|
||||
['已下架历史类目', '另一个树外名'],
|
||||
'兜底组按入参顺序保留',
|
||||
)
|
||||
assert.deepEqual(
|
||||
tree.extra.map((row) => row.productCount),
|
||||
[12, 8],
|
||||
)
|
||||
assert.equal(tree.extraTotal, 20, '兜底组和=徽标直接用')
|
||||
const sport = nodeAt(tree.roots, ['运动户外', '舞蹈体操鞋服/装备', '艺考形体服'])!
|
||||
assert.equal(sport.count, 5, '树内行照常聚合,兜底不影响树形')
|
||||
})
|
||||
|
||||
test('计数聚合——脏值归一:空名跳过、非有限/负数归 0、前后空白截掉、同名多行求和', () => {
|
||||
const tree = jdCategoryCountTree([
|
||||
{ name: '', productCount: 5 },
|
||||
{ name: ' ', productCount: 7 },
|
||||
{ name: '短裤', productCount: Number.NaN },
|
||||
{ name: '唐装', productCount: -3 },
|
||||
{ name: ' 女装 ', productCount: 2 },
|
||||
{ name: '女装', productCount: 3 },
|
||||
{ name: '艺考形体服', productCount: '4' as unknown as number },
|
||||
] as { name: string; productCount: number }[])
|
||||
const roots = tree.roots
|
||||
assert.equal(nodeAt(roots, ['服饰内衣', '女装'])!.count, 5, 'trim 后同名求和 2+3')
|
||||
assert.equal(nodeAt(roots, ['服饰内衣', '男装'])!.count, 0, 'NaN/负数/空名都归 0 不记账')
|
||||
assert.equal(tree.extra.length, 0, '空名词条不进兜底组')
|
||||
assert.equal(tree.extraTotal, 0)
|
||||
})
|
||||
|
||||
test('计数聚合——空输入:全树 0、兜底组空(结构仍完整可渲染)', () => {
|
||||
const tree = jdCategoryCountTree([])
|
||||
assert.equal(tree.roots.length, 3)
|
||||
assert.equal(tree.extra.length, 0)
|
||||
assert.equal(tree.extraTotal, 0)
|
||||
const total = tree.roots.reduce((sum, node) => sum + node.count, 0)
|
||||
assert.equal(total, 0)
|
||||
// 结构与静态树同形:一级孩子数与 jdCategoryChildren 逐一对齐。
|
||||
for (const root of tree.roots) {
|
||||
assert.deepEqual(
|
||||
root.children.map((node) => node.name),
|
||||
jdCategoryChildren([root.name]).map((node) => node.name),
|
||||
)
|
||||
}
|
||||
// 非数组入参按空处理(运行期 resilience,JS 调用方传 null 不炸)。
|
||||
const junk = jdCategoryCountTree(null as unknown as [])
|
||||
assert.equal(junk.roots.length, 3)
|
||||
assert.equal(junk.extraTotal, 0)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user