191 lines
8.0 KiB
TypeScript
191 lines
8.0 KiB
TypeScript
/**
|
||
* 京东数据库页纯逻辑契约(v1.4 Phase 5)—— node --test 直跑(纯函数零依赖)。
|
||
*
|
||
* 不用 readFileSync/readSource/globSync 读点:source-pin-ratchet 对台账外新文件带读点即红,
|
||
* 本文件只 import 被测纯函数。
|
||
*
|
||
* 覆盖:
|
||
* 1. **snake 参数形状**:toJdProductListParams 产出 `page`/`page_size`(默认 20、上限 100)、
|
||
* `shop_name`/`product_code`/`title_keyword`/`sale_status`/`group_id`/`synced_from`/`synced_to`
|
||
* (ISO yyyy-MM-dd;空白不下发;groupId 空串→不带、有值→snake group_id);分页钳制(铁律 15)。
|
||
* 2. **详情解析**:parseJdProductDetail 解析 attrGroups/images(camel 为正 snake 容错、缺 url 弃行),
|
||
* attrGroups 解析不出时 attrJsonRaw 原文兜底;列表行解析缺 id 弃行。
|
||
*
|
||
* 跑法:`node --test tests/jd-product-group.test.ts`
|
||
*/
|
||
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
|
||
import {
|
||
JD_PRODUCT_MAX_PAGE,
|
||
createJdProductFilterState,
|
||
toJdProductExportParams,
|
||
toJdProductListParams,
|
||
} from '../src/pages/jd/jd-product-filter.ts'
|
||
import {
|
||
jdPriceRange,
|
||
parseJdProductDetail,
|
||
parseJdProductPage,
|
||
toJdProductItem,
|
||
} from '../src/pages/jd/jd-product-model.ts'
|
||
|
||
test('京东列表参数——snake 形状:groupId 空→不带、有值→group_id,日期空白/非法不下发', () => {
|
||
const full = toJdProductListParams(
|
||
{
|
||
shopName: ' 旗舰自营店 ',
|
||
productCode: ' JD-10086 ',
|
||
titleKeyword: ' 保温杯 ',
|
||
saleStatus: '上架中',
|
||
groupId: ' 7 ',
|
||
syncedFrom: '2026-11-01',
|
||
syncedTo: '2026-11-09',
|
||
},
|
||
2,
|
||
20,
|
||
)
|
||
assert.deepEqual(full, {
|
||
page: 2,
|
||
page_size: 20,
|
||
shop_name: '旗舰自营店',
|
||
product_code: 'JD-10086',
|
||
title_keyword: '保温杯',
|
||
sale_status: '上架中',
|
||
group_id: 7,
|
||
synced_from: '2026-11-01',
|
||
synced_to: '2026-11-09',
|
||
})
|
||
|
||
// 全空态只有分页两项;脏值(非数字分组、非法日期)同罪不下发。
|
||
const blank = toJdProductListParams(createJdProductFilterState(), 1, 20)
|
||
assert.deepEqual(blank, { page: 1, page_size: 20 })
|
||
assert.equal('group_id' in blank, false, 'groupId 空串不带 group_id')
|
||
const dirty = toJdProductListParams(
|
||
{ shopName: ' ', productCode: '', titleKeyword: '', saleStatus: '', groupId: 'abc', syncedFrom: '2026/11/01', syncedTo: 'not-a-date' },
|
||
1,
|
||
20,
|
||
)
|
||
assert.deepEqual(dirty, { page: 1, page_size: 20 }, '脏值不下发')
|
||
|
||
// 导出与列表同一过滤面单出口(无分页)。
|
||
const state = { shopName: '旗舰自营店', productCode: '', titleKeyword: '', saleStatus: '已下架', groupId: '7', syncedFrom: '2026-11-01', syncedTo: '' }
|
||
const list = toJdProductListParams(state, 3, 20)
|
||
const exp = toJdProductExportParams(state)
|
||
for (const key of Object.keys(exp) as Array<keyof typeof exp>) {
|
||
assert.equal((list as Record<string, unknown>)[key], exp[key], `导出与列表的 ${key} 必须同源`)
|
||
}
|
||
assert.equal('page' in exp, false, '导出参数无分页')
|
||
assert.deepEqual(exp, { shop_name: '旗舰自营店', sale_status: '已下架', group_id: 7, synced_from: '2026-11-01' })
|
||
})
|
||
|
||
test('京东列表参数——page 钳制防 (page-1)*page_size 溢出(铁律 15)', () => {
|
||
const low = toJdProductListParams(createJdProductFilterState(), 0, 0)
|
||
assert.equal(low.page, 1, 'page<1 归 1')
|
||
assert.equal(low.page_size, 20, 'page_size 非法回默认 20')
|
||
const big = toJdProductListParams(createJdProductFilterState(), 3, 500)
|
||
assert.equal(big.page_size, 100, 'page_size 上限 100')
|
||
|
||
const extreme = toJdProductListParams(createJdProductFilterState(), Number.MAX_SAFE_INTEGER, 100)
|
||
assert.equal(extreme.page, JD_PRODUCT_MAX_PAGE)
|
||
const offset = (extreme.page - 1) * extreme.page_size
|
||
assert.equal(Number.isSafeInteger(offset), true, `(page-1)*page_size=${offset} 必须留在安全整数域`)
|
||
assert.equal(offset, 999_900)
|
||
})
|
||
|
||
test('详情解析——attrGroups/images 结构化解析(snake 容错、缺 url 弃行)', () => {
|
||
const detail = parseJdProductDetail({
|
||
code: 0,
|
||
message: 'ok',
|
||
data: {
|
||
id: 42,
|
||
title: '304不锈钢保温杯',
|
||
sale_status: '上架中',
|
||
attrGroups: [
|
||
{
|
||
groupName: '规格',
|
||
attrs: [
|
||
{ key: 'capacity', label: '容量', value: '500ml' },
|
||
{ key: 'material', label: '', value: '304不锈钢' },
|
||
],
|
||
},
|
||
{ group_name: '其他', attrs: [{ key: 'color', label: '颜色', value: '白色' }] },
|
||
],
|
||
images: [
|
||
{ imageType: 'MAIN', attrName: '', sortNo: 1, url: 'https://img.example/main.jpg', width: 800, height: 800 },
|
||
{ image_type: 'ATTR', attrName: '白色', sortNo: 2, url: 'https://img.example/white.jpg' },
|
||
{ imageType: 'ATTR', attrName: '', sortNo: 3, url: 'https://img.example/attr2.jpg' },
|
||
{ imageType: 'DETAIL', sortNo: 4, url: '' }, // 缺 url 弃行
|
||
],
|
||
},
|
||
})
|
||
|
||
assert.equal(detail.item.id, 42)
|
||
assert.equal(detail.item.title, '304不锈钢保温杯')
|
||
assert.equal(detail.item.saleStatus, '上架中', 'snake 容错')
|
||
assert.equal(detail.attrGroups.length, 2, '两个属性块')
|
||
assert.equal(detail.attrGroups[0].groupName, '规格')
|
||
assert.equal(detail.attrGroups[0].attrs[0].label, '容量')
|
||
assert.equal(detail.attrGroups[1].groupName, '其他', 'group_name snake 容错')
|
||
assert.equal(detail.images.length, 3, '缺 url 的 DETAIL 图弃行')
|
||
assert.equal(detail.images[1].imageType, 'ATTR', 'image_type snake 容错')
|
||
assert.equal(detail.images[1].attrName, '白色')
|
||
assert.equal(detail.images[1].sortNo, 2)
|
||
assert.equal(detail.images[2].attrName, '', 'attrName 缺省空串')
|
||
assert.equal(detail.attrJsonRaw, '', '结构化解析成功时不需要原文兜底')
|
||
})
|
||
|
||
test('详情解析——attrGroups 缺失时 attrJsonRaw 原文兜底;信封外裸负载同口径', () => {
|
||
const raw = '{"规格":{"容量":"500ml"}}'
|
||
const detail = parseJdProductDetail({ id: 7, attrJsonRaw: raw })
|
||
assert.equal(detail.item.id, 7)
|
||
assert.deepEqual(detail.attrGroups, [], 'attrGroups 解析不出为空')
|
||
assert.equal(detail.attrJsonRaw, raw, '原文兜底字段原样透传(界面展示,不静默吞)')
|
||
assert.deepEqual(detail.images, [])
|
||
|
||
// 非法负载不抛:整包垃圾回空详情(id 0),attrJsonRaw 留空。
|
||
const junk = parseJdProductDetail('not-an-object')
|
||
assert.equal(junk.item.id, 0)
|
||
assert.equal(junk.attrJsonRaw, '')
|
||
})
|
||
|
||
test('列表行解析——camel 为正 snake 容错、缺 id 弃行、价格区间拼装', () => {
|
||
const page = parseJdProductPage({
|
||
success: true,
|
||
data: {
|
||
items: [
|
||
{
|
||
id: 1,
|
||
shopId: 3,
|
||
shopName: '旗舰自营店',
|
||
productCode: 'JD-10086',
|
||
priceMin: '12.90',
|
||
priceMax: '15.90',
|
||
stockTotal: 500,
|
||
saleStatus: '上架中',
|
||
groupId: 7,
|
||
groupName: '甲组',
|
||
syncedAt: '2026-11-12 09:00:00',
|
||
},
|
||
{ id: 'not-a-number', title: '缺 id 弃行' },
|
||
{ shop_name: 'snake 行也缺 id,弃' },
|
||
],
|
||
total: 1,
|
||
page: 1,
|
||
page_size: 20,
|
||
},
|
||
})
|
||
assert.equal(page.items.length, 1, '三条原始行里两条缺有效 id,只留 1 行')
|
||
const row = page.items[0]
|
||
assert.equal(row.shopName, '旗舰自营店')
|
||
assert.equal(row.groupId, 7)
|
||
assert.equal(row.groupName, '甲组')
|
||
assert.equal(jdPriceRange(row), '12.90 ~ 15.90', 'min/max 都有给区间')
|
||
assert.equal(jdPriceRange({ priceMin: '12.90', priceMax: '' }), '12.90', '只空一侧给单值')
|
||
assert.equal(jdPriceRange({ priceMin: '', priceMax: '' }), '', '全空为空串(界面展示 -)')
|
||
|
||
// snake 行同口径 + 缺省回默认。
|
||
const snakePage = parseJdProductPage({ data: { items: [{ id: 9, shop_name: 'B店', main_image_url: 'https://img/x.jpg' }], total: 1, page: 2, page_size: 5 } })
|
||
const snakeRow = toJdProductItem({ id: 9, shop_name: 'B店', main_image_url: 'https://img/x.jpg' })
|
||
assert.equal(snakeRow?.mainImageUrl, 'https://img/x.jpg')
|
||
assert.equal(snakePage.pageSize, 5)
|
||
})
|