import { test } from 'node:test' import assert from 'node:assert/strict' 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 快照;与用户端 * frontend-vue 侧同数据两份正本,改树两边同轮同步)。判据钉的是取证读数: * 3 个一级 / 250 个末级 / 最深 4 级 / 4 组同名跨支——改树先对取证数据(京麦面板 * 或 getVenderLicenseCategory 重抓),不得手改数字凑测试。 */ test('树形状=京麦授权类目真机读数:3 个一级、250 个末级、最深 4 级', () => { const roots = jdCategoryRoots() assert.deepEqual( roots.map((node) => node.name), ['服饰内衣', '母婴', '运动户外'], '一级就 3 个(京麦面板 2026-11-16 真机:该店铺授权经营类目,不得加回京东公开全量)', ) assert.deepEqual( roots.map((node) => node.value), ['1315', '1319', '1318'], '一级类目 id 与取证响应逐字同', ) const leaves = jdCategoryLeaves() assert.equal(leaves.length, 250, '末级节点 250 个(取证读数)') const maxDepth = Math.max(...leaves.map((leaf) => leaf.path.length)) assert.equal(maxDepth, 4, '树最深 4 级') }) test('服饰内衣二级与京麦面板逐字同源;逐级下钻到真机样本叶', () => { assert.deepEqual( jdCategoryChildren(['服饰内衣']).map((node) => node.name), ['服饰配件', '内衣', '女装', '男装', '定制服装/工装'], '服饰内衣二级 5 个(京麦面板逐字)', ) assert.deepEqual( jdCategoryChildren(['服饰内衣', '服饰配件']).map((node) => node.name), ['腰带/腰链', '耳罩/耳包', '领带/领结/领带夹', '男士丝巾/围巾', '女士丝巾/围巾/披肩', '鸭舌帽', '礼帽', '手套', '围巾/手套/帽子套装', '毛线帽', '遮阳帽', '棒球帽', '渔夫帽'], '服饰配件三级 13 个(京麦面板 2026-11-16 截图逐字前 6 +取证快照全量)', ) assert.deepEqual( jdCategoryChildren(['服饰内衣', '服饰配件', '腰带/腰链']), [], '三级叶子 children 空(末级,可选中)', ) assert.deepEqual( jdCategoryChildren(['不存在的一级']), [], '未知路径回落空子级(面板该栏缺省)', ) }) test('落库末级名真机样本都在树内(category_path 等值过滤成立的前提)', () => { const names = new Set(jdCategoryLeaves().map((leaf) => leaf.name)) for (const sample of ['艺考形体服', '啦啦队服', '拉丁舞服']) { assert.ok(names.has(sample), `真机样本「${sample}」必须是树内末级(契约 E3 口径)`) } const yikao = jdCategoryLeaves().find((leaf) => leaf.name === '艺考形体服') assert.ok(yikao, '艺考形体服在树内') assert.equal(yikao!.path.length, 3, '艺考形体服是 level 3 节点(取证读数)') }) test('同名跨支叶子如实登记:4 组重名(同名行等值过滤一起命中,B112-05 口径)', () => { const counts = new Map() for (const leaf of jdCategoryLeaves()) { counts.set(leaf.name, (counts.get(leaf.name) ?? 0) + 1) } const dups = [...counts.entries()].filter(([, count]) => count > 1) assert.deepEqual( dups.map(([name]) => name).sort(), ['唐装', '吊带/背心', '短裤', '赠品'].sort(), '重名集与取证读数逐字同(改树后这里红=重名集变了,须复核登记)。注:「汉服」两处节点中' + '一处带四级子级是非叶(点它会下钻),不构成末级重名。', ) }) test('搜索:按名称或路径段命中末级;空词返回全量', () => { assert.equal(jdCategorySearchLeaves('').length, 250, '空词=全量末级') const byName = jdCategorySearchLeaves('艺考') assert.equal(byName.length, 1) assert.equal(byName[0]!.name, '艺考形体服') const byPath = jdCategorySearchLeaves('服饰配件') assert.ok(byPath.length >= 13, '按路径段命中服饰配件全部末级') assert.ok(byPath.every((leaf) => leaf.path.includes('服饰配件'))) assert.deepEqual(jdCategorySearchLeaves('不存在词xyz'), []) }) test('树数据快照不可空、value 全非空(取证 id 留档完整)', () => { assert.ok(JD_CATEGORY_TREE.length > 0) for (const leaf of jdCategoryLeaves()) { assert.ok(leaf.value, `叶子 ${leaf.name} 必须带京麦类目 id`) } }) /** 2026-11-18 客户口径「一级/二级类目也要能选来搜索」:全层级节点+子树展开集合。 */ test('全节点搜索:一级/二级/末级都在结果里,leaf 标记如实', () => { assert.equal(jdCategorySearchNodes('').length, jdCategoryAllNodes().length, '空词=全量节点') assert.ok(jdCategoryAllNodes().length > 250, '全节点数必须大于末级 250(含非末级)') const byName = jdCategorySearchNodes('女装') const women = byName.find((node) => node.name === '女装') assert.ok(women, '「女装」这个二级节点必须可搜到(旧版只铺末级,选不了它)') assert.equal(women!.leaf, false, '女装有子级=非末级') assert.deepEqual(women!.path, ['服饰内衣', '女装'], '路径从一级起(同名跨支靠它区分)') assert.ok(byName.some((node) => node.leaf), '末级命中照旧在') }) test('子树展开集合:含自身+全部后代名、去重、不越支;路径失效回 null', () => { const dress = jdCategoryNamesUnder(['服饰内衣', '女装', '连衣裙']) assert.ok(dress, '连衣裙路径有效') assert.ok(dress!.includes('连衣裙'), '含节点自身名(实际末级可能就在这一级)') assert.ok(dress!.includes('短袖连衣裙') && dress!.includes('长袖连衣裙') && dress!.includes('无袖连衣裙')) assert.ok(!dress!.includes('女装'), '只展开本节点子树,不带上父级') const top = jdCategoryNamesUnder(['运动户外']) assert.ok(top!.includes('艺考形体服') && top!.includes('啦啦队服'), '一级展开覆盖全支') assert.ok(!top!.includes('女装'), '不越支') const hanfu = jdCategoryNamesUnder(['服饰内衣', '女装', '汉服']) assert.ok(hanfu!.includes('汉服') && hanfu!.includes('汉服套装'), '非叶「汉服」展开含四级') assert.equal(jdCategoryNamesUnder(['不存在', '的路径']), null, '路径失效回 null(调用方按单名兜底)') assert.equal(jdCategoryNamesUnder([]), null, '空路径回 null') // 去重:同名跨支在**同一子树内**出现两次时集合不重复(唐装/短裤等在女装支内只有一个)。 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) })