4f759b996c
店内分类只能靠客户端起 Chrome 现拉(一次十几秒),页面每次重开都空着逼用户再点一次同步是白成本。新增 pages/jd/jd-category-cache.ts(零依赖纯逻辑):同步成功后把 shopName/shopId/categories/fetchedAt 落 localStorage(键 jd_shop_category_cache_v1),页面挂载即回填选项集,说明行标注「X 同步,点同步分类可刷新」。只回填选项集、不回填选中值——选中值决定这一单采哪些分类,跨次沿用它等于静默改采集范围。「同步分类」仍走现拉并覆盖缓存(商家在京麦改了分类可刷回来)。脏 JSON/空分类/配额失败一律读成 null 或静默跳过,不拿半张表填下拉。 判据:新 tests/jd-category-cache.test.ts 6 支(原样读回/脏 JSON 与空分类读 null/脏条目与重复 id 过滤/storage 不可用不抛/清缓存/预选值按现有 id 裁剪);jd-collect-tab/jd-console 既有 19 支全绿;vue-tsc --noEmit rc=0。
111 lines
3.7 KiB
TypeScript
111 lines
3.7 KiB
TypeScript
import { test } from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import {
|
|
JD_CATEGORY_CACHE_KEY,
|
|
clearCategoryCache,
|
|
loadCategoryCache,
|
|
pruneSelectedCategoryValues,
|
|
saveCategoryCache,
|
|
} from '../src/pages/jd/jd-category-cache.ts'
|
|
|
|
/** 2026-11-19 客户口径「同步后就记住呀 别每次都同步」:
|
|
* 店内分类现拉要起 Chrome 十几秒,页面重开后应当直接回填上次结果。 */
|
|
|
|
function memoryStorage(initial: Record<string, string> = {}) {
|
|
const data = { ...initial }
|
|
return {
|
|
data,
|
|
getItem: (key: string) => (key in data ? data[key] : null),
|
|
setItem: (key: string, value: string) => {
|
|
data[key] = value
|
|
},
|
|
removeItem: (key: string) => {
|
|
delete data[key]
|
|
},
|
|
}
|
|
}
|
|
|
|
test('保存后能原样读回(含店铺名与时间)', () => {
|
|
const storage = memoryStorage()
|
|
const categories = [
|
|
{ label: '服饰内衣', value: '1024347001' },
|
|
{ label: '其他', value: '1024347575' },
|
|
]
|
|
assert.equal(saveCategoryCache(storage, {
|
|
shopName: '花屿巷旗舰店', shopId: '165515933', categories, fetchedAt: '2026-11-19T13:31:00.000Z',
|
|
}), true)
|
|
|
|
const cache = loadCategoryCache(storage)
|
|
assert.ok(cache)
|
|
assert.equal(cache?.shopName, '花屿巷旗舰店')
|
|
assert.equal(cache?.shopId, '165515933')
|
|
assert.deepEqual(cache?.categories, categories)
|
|
assert.equal(cache?.fetchedAt, '2026-11-19T13:31:00.000Z')
|
|
})
|
|
|
|
test('没有缓存、脏 JSON、空分类都读成 null(不拿半张表去填下拉)', () => {
|
|
assert.equal(loadCategoryCache(memoryStorage()), null)
|
|
assert.equal(loadCategoryCache(memoryStorage({ [JD_CATEGORY_CACHE_KEY]: '{not json' })), null)
|
|
assert.equal(loadCategoryCache(memoryStorage({ [JD_CATEGORY_CACHE_KEY]: '[]' })), null)
|
|
assert.equal(loadCategoryCache(memoryStorage({
|
|
[JD_CATEGORY_CACHE_KEY]: JSON.stringify({ categories: [] }),
|
|
})), null)
|
|
assert.equal(loadCategoryCache(null), null)
|
|
})
|
|
|
|
test('脏条目被过滤、重复 id 只留第一条', () => {
|
|
const storage = memoryStorage({
|
|
[JD_CATEGORY_CACHE_KEY]: JSON.stringify({
|
|
shopName: 'A',
|
|
categories: [
|
|
{ label: '其他', value: '1024347575' },
|
|
{ label: '其他', value: '1024347575' },
|
|
{ label: '', value: 'x' },
|
|
{ label: '缺 id' },
|
|
null,
|
|
{ label: '女装', value: '1024347001' },
|
|
],
|
|
}),
|
|
})
|
|
const cache = loadCategoryCache(storage)
|
|
assert.deepEqual(cache?.categories, [
|
|
{ label: '其他', value: '1024347575' },
|
|
{ label: '女装', value: '1024347001' },
|
|
])
|
|
})
|
|
|
|
test('storage 不可用(隐私模式/配额)只影响缓存,不抛异常', () => {
|
|
const broken = {
|
|
getItem: () => {
|
|
throw new Error('denied')
|
|
},
|
|
setItem: () => {
|
|
throw new Error('denied')
|
|
},
|
|
removeItem: () => {
|
|
throw new Error('denied')
|
|
},
|
|
}
|
|
assert.equal(loadCategoryCache(broken), null)
|
|
assert.equal(saveCategoryCache(broken, {
|
|
shopName: 'A', shopId: '', categories: [{ label: '其他', value: '1' }], fetchedAt: '',
|
|
}), false)
|
|
assert.doesNotThrow(() => clearCategoryCache(broken))
|
|
})
|
|
|
|
test('清缓存后读不到', () => {
|
|
const storage = memoryStorage()
|
|
saveCategoryCache(storage, {
|
|
shopName: 'A', shopId: '', categories: [{ label: '其他', value: '1' }], fetchedAt: '',
|
|
})
|
|
assert.ok(loadCategoryCache(storage))
|
|
clearCategoryCache(storage)
|
|
assert.equal(loadCategoryCache(storage), null)
|
|
})
|
|
|
|
test('预选值只保留缓存里还存在的 id(换店后旧 id 必须丢)', () => {
|
|
const categories = [{ label: '其他', value: '1024347575' }]
|
|
assert.deepEqual(pruneSelectedCategoryValues(['1024347575', '999'], categories), ['1024347575'])
|
|
assert.deepEqual(pruneSelectedCategoryValues([], categories), [])
|
|
})
|