import { test } from 'node:test' import assert from 'node:assert/strict' import { JD_CATEGORY_TREE, jdCategoryAllNodes, jdCategoryChildren, jdCategoryLeaves, jdCategoryNamesUnder, jdCategoryRoots, jdCategorySearchLeaves, jdCategorySearchNodes, } 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, '展开集合内部去重') })