121 lines
6.7 KiB
TypeScript
121 lines
6.7 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { adminPages } from '../src/router/routes.ts'
|
||
import {
|
||
jdPriceRange,
|
||
jdSkuDimensions,
|
||
jdSkuSummary,
|
||
parseJdProductDetail,
|
||
toJdProductItem,
|
||
} from '../src/pages/jd/jd-product-model.ts'
|
||
|
||
/** v1.4「京东数据库」后管页契约(蓝图 docs/ITERATION_PLAN.md §3.4 / §4 Phase 5)。
|
||
*
|
||
* 只用 import 断言 routes 注册表——readSource 台账(source-pin-ratchet)钉住
|
||
* tests/ 里 readFileSync/globSync 读取点逐文件只许减,新读源码文件会撞棘轮;
|
||
* V196 迁移 / company-scope / operation-guides 的接线由既有护栏在 Phase 5 收口时
|
||
* 自动覆盖(company-menu-route 把 V196 加进迁移清单、align-operation-guide、
|
||
* company-tab-wiring),本文件不重复 fs 断言。
|
||
* Pending 期整组 skip;Phase 5 落地后去掉 skip 跑绿。
|
||
*/
|
||
|
||
const MENU_KEY = 'admin_jd_database'
|
||
const ROUTE = 'jd-center/database'
|
||
|
||
test('routes 注册表出现京东数据库页面且公司隔离', () => {
|
||
const page = adminPages.find((item) => item.menuKey === MENU_KEY)
|
||
assert.ok(page, `routes.ts 缺 menuKey=${MENU_KEY} 的注册行`)
|
||
assert.equal(page.path, ROUTE, 'route_path 必须与 V196 菜单一侧一致')
|
||
assert.equal(page.title, '京东数据库', '菜单标题以「京东数据库」为准')
|
||
assert.equal(page.companyScoped, true, '京东数据库按公司隔离(AC-6)')
|
||
})
|
||
|
||
/** 详情面的 SKU 矩阵解析(V198):后端下发 camelCase,snake 容错;坏行不炸整块。 */
|
||
test('详情负载的 skus 归一化成矩阵行:skuId 为身份、销售属性维度按首次出现排序', () => {
|
||
const detail = parseJdProductDetail({
|
||
id: 1,
|
||
productCode: '10036442942137',
|
||
skus: [
|
||
{ skuId: '10238877506570', skuName: '上衣 S', skuSeq: '1', price: 162.0, stock: 200, sortNo: 0,
|
||
saleAttrs: [{ name: '颜色', value: '上衣' }, { name: '尺码', value: 'S' }] },
|
||
{ skuId: '10238877506580', sku_name: '半裙 S', sku_seq: '3', price: '168.00', stock: 80, sort_no: 2,
|
||
sale_attrs: [{ name: '颜色', value: '半裙' }, { name: '尺码', value: 'S' }] },
|
||
],
|
||
})
|
||
|
||
assert.equal(detail.skus.length, 2)
|
||
assert.equal(detail.skus[0].skuId, '10238877506570')
|
||
assert.equal(detail.skus[0].price, '162', 'BigDecimal 序列化成 number 也要能读')
|
||
assert.equal(detail.skus[1].price, '168', '字符串价去尾零')
|
||
assert.equal(detail.skus[1].sortNo, 2, 'snake 容错')
|
||
assert.deepEqual(jdSkuDimensions(detail.skus), ['颜色', '尺码'], '维度名按首次出现顺序')
|
||
assert.equal(jdSkuSummary(detail.skus), '2 个 SKU · ¥162 ~ ¥168 · 库存 280')
|
||
})
|
||
|
||
test('缺 skuId 的行弃掉、空 skus 回空数组(旧数据不渲染空表)', () => {
|
||
const detail = parseJdProductDetail({
|
||
id: 2,
|
||
skus: [{ skuName: '没有 skuId' }, { skuId: 'A1', price: 1, stock: 0 }],
|
||
})
|
||
assert.equal(detail.skus.length, 1)
|
||
assert.equal(jdSkuSummary(detail.skus), '1 个 SKU · ¥1 · 库存 0')
|
||
assert.equal(parseJdProductDetail({ id: 3 }).skus.length, 0)
|
||
assert.equal(jdSkuSummary([]), '')
|
||
})
|
||
|
||
/** 列表行价格(2026-11-16 修「采集到了却全显示 -」):后端 BigDecimal 序列化成 JSON number,
|
||
* 解析层必须数值/字符串都收——text() 只认字符串=价格在解析层被丢成空串的生产实录
|
||
* (生产库 273/273 行 price_min 全有值、界面价格列全「-」,用户报「价格没采集到」的真因)。 */
|
||
test('列表行价格:BigDecimal 的 JSON number 形状与字符串形状都归一成展示文本', () => {
|
||
const numeric = toJdProductItem({ id: 9, productCode: '10036368647085', priceMin: 107.64, priceMax: 109.26 })
|
||
assert.ok(numeric, '只缺价格之外的字段不弃行')
|
||
assert.equal(numeric.priceMin, '107.64', 'number 形状不许丢成空串')
|
||
assert.equal(jdPriceRange(numeric), '107.64 ~ 109.26', 'number 形状价格列不许显示 -')
|
||
|
||
const single = toJdProductItem({ id: 10, priceMin: 64.8, priceMax: 64.8 })
|
||
assert.ok(single)
|
||
assert.equal(jdPriceRange(single), '64.8', '单值价(min==max)不拼区间')
|
||
|
||
const stringShape = toJdProductItem({ id: 11, priceMin: '12.90', priceMax: '' })
|
||
assert.ok(stringShape)
|
||
assert.equal(stringShape.priceMin, '12.9', '字符串形状仍收(去尾零,与详情 SKU 价同口径)')
|
||
assert.equal(jdPriceRange(stringShape), '12.9')
|
||
|
||
const missing = toJdProductItem({ id: 12 })
|
||
assert.ok(missing)
|
||
assert.equal(jdPriceRange(missing), '', '两侧都空回空串(列表显示 -)')
|
||
})
|
||
|
||
/** 类目筛选(2026-11-15):京麦末级类目等值过滤,空串不下发、非空透传 category。 */
|
||
test('类目筛选参数序列化:空串不下发、非空下发 category(等值过滤)', async () => {
|
||
const { createJdProductFilterState, toJdProductListParams, jdProductFilterActive } =
|
||
await import('../src/pages/jd/jd-product-filter.ts')
|
||
const empty = createJdProductFilterState()
|
||
assert.equal('category' in toJdProductListParams(empty, 1, 20), false, '全部类目不下发 category')
|
||
assert.equal(jdProductFilterActive(empty), false)
|
||
|
||
const filtered = { ...empty, category: ' 服饰内衣 ' }
|
||
assert.equal(toJdProductListParams(filtered, 1, 20).category, '服饰内衣', '去空白透传(等值过滤)')
|
||
assert.equal(jdProductFilterActive(filtered), true, '选中类目算激活筛选')
|
||
})
|
||
|
||
/** 分类状态筛选(2026-11-15 客户口径「查询已分类的跟未分类,分类好的可以不显示」):
|
||
* 全部不下发、已分类下发 grouped、未分类下发 ungrouped(与店铺分类弹窗同一后端谓词)。 */
|
||
test('分类状态筛选参数序列化:全部不下发、已分类 grouped、未分类 ungrouped', async () => {
|
||
const { createJdProductFilterState, toJdProductListParams, jdProductFilterActive } =
|
||
await import('../src/pages/jd/jd-product-filter.ts')
|
||
const empty = createJdProductFilterState()
|
||
assert.equal('grouped' in toJdProductListParams(empty, 1, 20), false, '全部不下发 grouped')
|
||
assert.equal('ungrouped' in toJdProductListParams(empty, 1, 20), false, '全部不下发 ungrouped')
|
||
|
||
const grouped = { ...empty, groupScope: 'grouped' as const }
|
||
assert.equal(toJdProductListParams(grouped, 1, 20).grouped, true, '已分类档下发 grouped=true')
|
||
assert.equal('ungrouped' in toJdProductListParams(grouped, 1, 20), false, '已分类档不下发 ungrouped')
|
||
assert.equal(jdProductFilterActive(grouped), true, '选中已分类算激活筛选')
|
||
|
||
const ungrouped = { ...empty, groupScope: 'ungrouped' as const }
|
||
assert.equal(toJdProductListParams(ungrouped, 1, 20).ungrouped, true, '未分类档下发 ungrouped=true')
|
||
assert.equal('grouped' in toJdProductListParams(ungrouped, 1, 20), false, '未分类档不下发 grouped')
|
||
assert.equal(jdProductFilterActive(ungrouped), true, '选中未分类算激活筛选')
|
||
})
|