feat(admin-jd): 商品目录只渲染本店有商品的类目分支(去掉「树外兜底」退化解)

客户口径:「正确是一开始显示的那棵树,而不是什么树兜底」。授权树里零命中的分
支整支剪掉:

- 本店商品名挂得上树(花屿巷旗舰店)⇒ 只显示命中分支,别店的空类目不再占屏;
- 树与本店不同源(家居日用店看别店快照)⇒ 整树剪空,本店类目名平铺成一级列表,
  不再出现「授权类目树(本店无匹配)」行 + 说明框,也不再出现「树外兜底」分组;
- 树有命中时,挂不上树的本店历史名仍收在一组(改名「授权树外的类目」)。

- 纯函数 jdCategoryPruneZero(零命中整支剪掉、重算 leaf)
- 一级默认展开改挂剪枝后的根(空根不置位,等真根到达再补)
- 判据:jd-category-tree.test.ts +3(剪枝三态)、侧栏 mount 改写(平铺/剪支/真树)、
  page mount 组名同步;source-pin 台账按文件点名 17→20
- 后管 node --test 1982 绿、vitest 252 绿、vue-tsc rc=0,快检四面绿
This commit is contained in:
2026-11-24 13:50:53 +08:00
parent 10fb52d0e9
commit 0591d6ee6d
8 changed files with 129 additions and 107 deletions
@@ -2,7 +2,7 @@
"note": "readSource 钉子逐文件基线。两把尺子:pins=落在用例体内的读盘,reads=全文读点总数(含模块级夹具与 helper)。都只许减;pins 下降而 reads 没同步下降=把读盘挪出体内,不算改进。由 tests/source-pin-ratchet.test.ts 生成。",
"totals": {
"files": 265,
"cases": 1862,
"cases": 1865,
"pins": 463,
"reads": 664
},
@@ -318,7 +318,7 @@
"reads": 5
},
"jd-category-tree.test.ts": {
"cases": 17,
"cases": 20,
"pins": 0,
"reads": 0
},
@@ -7,6 +7,7 @@ import {
jdCategoryCountTree,
jdCategoryLeaves,
jdCategoryNamesUnder,
jdCategoryPruneZero,
jdCategoryRoots,
jdCategorySearchLeaves,
jdCategorySearchNodes,
@@ -262,8 +263,8 @@ test('计数聚合——treeMatched 三态:树内有命中/整树零命中
assert.equal(foreign.extraTotal, 264)
assert.equal(foreign.roots.every((root) => root.count === 0), true)
// 数据没到(空输入)同样是 false,但兜底为空——侧栏必须靠 extra 非空才判「不同源」,
// 否则加载中会闪一下「本店无匹配」。
// 数据没到(空输入)同样是 false,但兜底为空——侧栏靠剪枝后树为空 + extra 非空决定
// 怎么渲染(树空则本店类目平铺成一级列表),不靠 treeMatched 判「加载中」。
const empty = jdCategoryCountTree([])
assert.equal(empty.treeMatched, false)
assert.equal(empty.extra.length, 0)
@@ -346,3 +347,28 @@ test('计数聚合——传入服务端真树:家居类目挂得上树、不
assert.equal(snapshot.treeMatched, false)
assert.equal(snapshot.extraTotal, 264)
})
test('剪枝——零命中的别店分支整支剪掉,整树剪空(2026-11-24 客户口径「要的是类目树,不是树外兜底」)', () => {
const foreign = jdCategoryCountTree([
{ name: '化妆品收纳盒', productCount: 260 },
{ name: '其他收纳架', productCount: 4 },
])
assert.equal(jdCategoryPruneZero(foreign.roots).length, 0,
'家居类目套服饰快照树 ⇒ 整树剪空,调用方把 extra 平铺成一级列表(不再有「本店无匹配」档)')
})
test('剪枝——命中分支保留祖先链、重算 leaf,零命中兄弟支不出现', () => {
const counted = jdCategoryCountTree([{ name: '连衣裙', productCount: 7 }])
const pruned = jdCategoryPruneZero(counted.roots)
assert.deepEqual(pruned.map((node) => node.name), ['服饰内衣'], '只有服饰内衣这一支真有商品')
assert.equal(nodeAt(pruned, ['服饰内衣', '女装'])!.count, 7, '祖先链上徽标不变(子树去重和)')
const dress = nodeAt(pruned, ['服饰内衣', '女装', '连衣裙'])!
assert.equal(dress.leaf, true, '子级全被剪掉后重算 leaf(不再显示展开箭头)')
assert.equal(nodeAt(pruned, ['服饰内衣', '男装']), null, '零命中兄弟支剪掉')
assert.equal(nodeAt(pruned, ['母婴']), null, '整支零命中的一级剪掉')
})
test('剪枝——空输入/异常入参回空数组(渲染层不炸)', () => {
assert.deepEqual(jdCategoryPruneZero([]), [])
assert.deepEqual(jdCategoryPruneZero(null as unknown as JdCategoryCountedNode[]), [])
})
@@ -77,7 +77,8 @@ describe('商品目录 Tab(京麦授权类目树+计数徽标)', () => {
expect(all.text()).toContain('37', '全部商品徽标=5 条计数行求和(7+4+5+9+12)')
expect(rowByText(catalog, '服饰内衣').text()).toContain('20',
'服饰内衣根徽标=子树去重和(连衣裙7+短袖4+短裤9,短裤跨支同名只记一次)')
expect(rowByText(catalog, '母婴').text()).toContain('0', '无词条一级归 0 不缺节点')
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('母婴'))).toBe(false,
'零命中一级整支剪掉(2026-11-24:不显示本店没有的类目分支)')
expect(rowByText(catalog, '运动户外').text()).toContain('5')
wrapper.unmount()
})
@@ -193,7 +194,7 @@ describe('树外兜底组(树上对不上的历史行,单名等值出口)'
const wrapper = mountSidebar()
const catalog = wrapper.get('.jd-side-catalog')
const head = catalog.get('.jd-side-extra-head')
expect(head.text()).toContain('树外兜底')
expect(head.text()).toContain('授权树外的类目', '树有命中时这一组的名字如实点名来处')
expect(head.text()).toContain('12', '兜底组头徽标=extraTotal(历史行求和)')
const extra = rowByText(catalog, '已下架历史类目')
await extra.trigger('click')
@@ -218,9 +219,12 @@ describe('树外兜底组(树上对不上的历史行,单名等值出口)'
})
})
describe('内置授权树与本店不同源(2026-11-23:家居日用店打开服饰快照树)', () => {
/** 活样本:库里只有家居收纳类目(家居日用数据库店的 264 件),内置树(花屿巷旗舰店的
* 服饰授权快照)节点全 0——旧行为照样渲染整棵服饰树,界面看着像「这家店卖女装」。 */
describe('内置授权树与本店不同源(2026-11-24 客户口径:要的是类目树,不是「树外兜底」)', () => {
/** 活样本:库里只有家居收纳类目(家居日用数据库店的 474 件),内置树(花屿巷旗舰店的
* 服饰授权快照)节点全 0——旧行为照样渲染整棵服饰树(看着像「这家店卖女装」),
* 2026-11-23 一轮收成「本店无匹配」一行+全部落「树外兜底」(客户:「正确是一开始
* 显示的那棵树,而不是什么树兜底」)。现行口径:零命中分支整支剪掉,树空就把本店
* 类目平铺成一级列表——始终是一棵树,不出现别店类目、也不出现退化分组。 */
const FOREIGN_OPTIONS: JdProductCategoryOption[] = [
{ name: '化妆品收纳盒', productCount: 260 },
{ name: '其他收纳架', productCount: 4 },
@@ -244,44 +248,36 @@ describe('内置授权树与本店不同源(2026-11-23:家居日用店打开
return (catalog.get('.jd-side-tree').element as HTMLElement).style.display
}
it('整树零命中 ⇒ 树默认收起、只留一行「本店无匹配」,兜底行仍可选', async () => {
it('整树零命中 ⇒ 不渲染别店树,本店类目平铺成一级(无分组头、无「本店无匹配」行)', async () => {
const wrapper = mountForeign()
const catalog = wrapper.get('.jd-side-catalog')
const head = catalog.get('.jd-side-foreign-head')
expect(head.text()).toContain('本店无匹配', '点名「这棵树不适用本店」,不让人以为本店有服饰类目')
expect(treeDisplay(catalog)).toBe('none', '整棵树收起(别店快照不冒充本店类目)')
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(false, '收起态不摆展开/收起按钮')
expect(treeDisplay(catalog)).toBe('none', '别店快照的服饰分支一件商品都没有 ⇒ 整树剪空')
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(false, '没有树就不摆展开/收起')
expect(catalog.find('.jd-side-extra-head').exists()).toBe(false,
'本店类目就是整列内容,不再套「树外兜底」分组头')
expect(catalog.text()).not.toContain('服饰内衣', '别店类目一个都不露脸')
const extra = rowByText(catalog, '化妆品收纳盒')
expect(extra.classes()).not.toContain('depth-1', '平铺成本店一级类目(与树行同一层级)')
expect(extra.text()).toContain('260')
await extra.trigger('click')
await flushPromises()
expect(wrapper.emitted('select-category')).toEqual([[{ name: '化妆品收纳盒', path: [] }]],
'树外历史行照旧按单名等值出口,筛选不因收树失效')
'平铺行仍按单名等值出口(path 空数组=无路径序列化)')
wrapper.unmount()
})
it('点说明行 ⇒ 展开原树与提示文案;再点收起', async () => {
const wrapper = mountForeign()
const catalog = wrapper.get('.jd-side-catalog')
await catalog.get('.jd-side-foreign-head').trigger('click')
await flushPromises()
expect(treeDisplay(catalog)).not.toBe('none', '点开仍可查原树形状(只读参考)')
expect(catalog.get('.jd-side-foreign-tip').text()).toContain('固定快照')
expect(catalog.find('.jd-side-tree-ops').exists()).toBe(true, '点开后恢复展开/收起操作')
await catalog.get('.jd-side-foreign-head').trigger('click')
await flushPromises()
expect(treeDisplay(catalog)).toBe('none')
wrapper.unmount()
})
it('树内有命中时照旧全树渲染(不同源判据不误伤正常店)', () => {
it('树内有命中 ⇒ 只渲染命中分支(零命中的一级整支不出现)', () => {
const wrapper = mountSidebar()
const catalog = wrapper.get('.jd-side-catalog')
expect(catalog.find('.jd-side-foreign-head').exists()).toBe(false)
expect(treeDisplay(catalog)).not.toBe('none')
expect(rowByText(catalog, '服饰内衣').text()).toContain('20')
expect(catalog.findAll('.jd-side-row').some((node) => node.text().includes('母婴'))).toBe(false,
'母婴支零命中 ⇒ 剪掉(不拿别店的空类目占屏)')
wrapper.unmount()
})
it('传服务端真树(家居店):树上就是收纳类目、兜底清空、不同源判据不触发', () => {
it('传服务端真树(家居店):树上就是收纳类目、不同源判据不触发', () => {
const wrapper = mount(JdCategorySidebar, {
props: {
categoryOptions: FOREIGN_OPTIONS,
@@ -297,11 +293,9 @@ describe('内置授权树与本店不同源(2026-11-23:家居日用店打开
},
})
const catalog = wrapper.get('.jd-side-catalog')
expect(catalog.find('.jd-side-foreign-head').exists()).toBe(false,
'真树有命中 ⇒ 不再点名「本店无匹配」')
expect(treeDisplay(catalog)).not.toBe('none', '真树直接渲染,不收起')
expect(treeDisplay(catalog)).not.toBe('none', '真树直接渲染')
expect(rowByText(catalog, '收纳用品').text()).toContain('260', '计数徽标按真树聚合')
expect(catalog.find('.jd-side-extra').exists()).toBe(false, '所有类目都挂得上真树,兜底组不占位')
expect(catalog.find('.jd-side-extra').exists()).toBe(false, '所有类目都挂得上真树,无树外组')
expect(catalog.text()).not.toContain('服饰内衣', '内置快照树被服务端真树替换')
wrapper.unmount()
})
@@ -352,14 +352,14 @@ describe('侧栏商品目录(类目筛选迁移自级联面板)', () => {
wrapper.unmount()
})
it('树外兜底组(已落库但京麦树里没有的历史名):点行单名等值下发;「全部商品」行清选重查', async () => {
it('树外类目组(已落库但京麦树里没有的历史名):点行单名等值下发;「全部商品」行清选重查', async () => {
jdApi.fetchJdProductCategories.mockResolvedValue(CATEGORY_OPTIONS)
const wrapper = await mountWith({ items: [], total: 0, page: 1, pageSize: 50 })
const catalog = wrapper.get('.jd-side-catalog')
// 树外兜底段:树外历史行从兜底组搜出可选(path 空数组=页面按无路径序列化单名等值)。
// 授权树外的本店历史行收在单独一组(path 空数组=页面按无路径序列化单名等值)。
const extra = catalog.get('.jd-side-extra')
expect(extra.text()).toContain('树外兜底')
expect(extra.text()).toContain('授权树外的类目')
await extra.findAll('.jd-side-row').find((row) => row.text().includes('已下架历史类目'))!.trigger('click')
await flushPromises()
expect(jdApi.fetchJdProductList.mock.calls.at(-1)![0]).toMatchObject({ category: '已下架历史类目', categoryPath: [] })