| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- import assert from 'node:assert/strict'
- import { test } from 'node:test'
- import { DICT_TYPES, dictLabel, resetDictLabelsForTest } from '../src/modules/cockpit/domain/dictLabels.js'
- import { DICT_SNAPSHOT_VERSION, parseDictSnapshot } from '../src/modules/cockpit/domain/dictSnapshot.js'
- import {
- DICT_STORAGE_KEY,
- loadDictSnapshot,
- loadDictionaries,
- saveDictSnapshot,
- } from '../src/modules/cockpit/services/cockpitDict.js'
- /** 最小 localStorage 替身:只实现 getItem/setItem。 */
- function memoryStorage(seed = {}) {
- const data = { ...seed }
- return {
- getItem: (key) => (Object.hasOwn(data, key) ? data[key] : null),
- setItem: (key, value) => { data[key] = String(value) },
- data,
- }
- }
- const snapshotOf = (types) => JSON.stringify({ version: DICT_SNAPSHOT_VERSION, savedAt: 1, types })
- test('启动先读快照顶上:接口还没回来时就能出中文', async () => {
- resetDictLabelsForTest()
- const storage = memoryStorage({
- [DICT_STORAGE_KEY]: snapshotOf({ [DICT_TYPES.riskLevel]: { 1: '较大风险' } }),
- })
- const snapshot = loadDictSnapshot({ storage })
- assert.equal(snapshot.savedAt, 1)
- assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '较大风险')
- })
- test('接口刷新成功后覆盖内存并写回快照', async () => {
- resetDictLabelsForTest()
- const storage = memoryStorage()
- const result = await loadDictionaries({
- storage,
- now: () => 42,
- // 真实形状:fetchSection 的 toData 已经把行数组转成了 { 码: 标签 } 表。
- fetchDict: async (type) => (type === DICT_TYPES.riskLevel
- ? { state: 'success', data: { 2: '一般风险' } }
- : { state: 'failed', data: null }),
- })
- assert.deepEqual(result.refreshed, [DICT_TYPES.riskLevel])
- assert.equal(dictLabel(DICT_TYPES.riskLevel, '2'), '一般风险')
- const saved = parseDictSnapshot(storage.getItem(DICT_STORAGE_KEY))
- assert.equal(saved.savedAt, 42)
- assert.deepEqual(saved.types[DICT_TYPES.riskLevel], { 2: '一般风险' })
- })
- test('接口失败时保留快照里的字典,不改写、不清空', async () => {
- resetDictLabelsForTest()
- const storage = memoryStorage({
- [DICT_STORAGE_KEY]: snapshotOf({ [DICT_TYPES.riskColor]: { 1: '橙' } }),
- })
- const before = storage.getItem(DICT_STORAGE_KEY)
- const result = await loadDictionaries({
- storage,
- fetchDict: async () => { throw new Error('network down') },
- })
- assert.deepEqual(result.refreshed, [])
- assert.equal(dictLabel(DICT_TYPES.riskColor, '1'), '橙')
- assert.equal(storage.getItem(DICT_STORAGE_KEY), before)
- })
- test('快照损坏或版本不符时当作没有快照,界面回原码(字典为空)', () => {
- resetDictLabelsForTest()
- const broken = memoryStorage({ [DICT_STORAGE_KEY]: '{oops' })
- assert.equal(loadDictSnapshot({ storage: broken }), null)
- const stale = memoryStorage({
- [DICT_STORAGE_KEY]: JSON.stringify({ version: DICT_SNAPSHOT_VERSION - 1, types: { [DICT_TYPES.riskLevel]: { 1: '较大风险' } } }),
- })
- assert.equal(loadDictSnapshot({ storage: stale }), null)
- assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '')
- })
- test('取数层直接给行数组时也能接住(两种形状都不丢)', async () => {
- resetDictLabelsForTest()
- const result = await loadDictionaries({
- storage: null,
- fetchDict: async (type) => (type === DICT_TYPES.riskLevel
- ? { state: 'success', data: [{ dictValue: '0', dictLabel: '重大风险' }] }
- : { state: 'failed', data: null }),
- })
- assert.deepEqual(result.refreshed, [DICT_TYPES.riskLevel])
- assert.equal(dictLabel(DICT_TYPES.riskLevel, '0'), '重大风险')
- })
- test('localStorage 不可用(隐私模式)时退化为内存,不抛错', async () => {
- resetDictLabelsForTest()
- const result = await loadDictionaries({
- storage: null,
- fetchDict: async (type) => (type === DICT_TYPES.riskLevel
- ? { state: 'success', data: { 3: '低风险' } }
- : { state: 'failed', data: null }),
- })
- assert.deepEqual(result.refreshed, [DICT_TYPES.riskLevel])
- assert.equal(dictLabel(DICT_TYPES.riskLevel, '3'), '低风险')
- assert.equal(saveDictSnapshot({ storage: null }), null)
- })
|