Files
crawler-plugin/frontend-vue/tests/jd-category-cache.test.ts
T
huangzd1997 ce3502be9a fix(jd-collect): 清缓存失败如实回 false,静默棘轮 blocks 按三支逐条登记
jd-category-cache.ts 的三支 try/catch(读/写/清)兜法都是把结果交给调用方,没有 console 糊过去;清缓存原先是空 catch(被 catch-silence-ratchet 判「新增静默 catch」),改成 return false 后静默数不动、只进 blocks 324→327。
2026-11-19 22:30:55 +08:00

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.equal(clearCategoryCache(broken), false, '清不掉要如实回 false,不许吞成"清过了"')
})
test('清缓存后读不到', () => {
const storage = memoryStorage()
saveCategoryCache(storage, {
shopName: 'A', shopId: '', categories: [{ label: '其他', value: '1' }], fetchedAt: '',
})
assert.ok(loadCategoryCache(storage))
assert.equal(clearCategoryCache(storage), true)
assert.equal(loadCategoryCache(storage), null)
})
test('预选值只保留缓存里还存在的 id(换店后旧 id 必须丢)', () => {
const categories = [{ label: '其他', value: '1024347575' }]
assert.deepEqual(pruneSelectedCategoryValues(['1024347575', '999'], categories), ['1024347575'])
assert.deepEqual(pruneSelectedCategoryValues([], categories), [])
})