cockpitDict.test.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import { DICT_TYPES, dictLabel, resetDictLabelsForTest } from '../src/modules/cockpit/domain/dictLabels.js'
  4. import { DICT_SNAPSHOT_VERSION, parseDictSnapshot } from '../src/modules/cockpit/domain/dictSnapshot.js'
  5. import {
  6. DICT_STORAGE_KEY,
  7. loadDictSnapshot,
  8. loadDictionaries,
  9. saveDictSnapshot,
  10. } from '../src/modules/cockpit/services/cockpitDict.js'
  11. /** 最小 localStorage 替身:只实现 getItem/setItem。 */
  12. function memoryStorage(seed = {}) {
  13. const data = { ...seed }
  14. return {
  15. getItem: (key) => (Object.hasOwn(data, key) ? data[key] : null),
  16. setItem: (key, value) => { data[key] = String(value) },
  17. data,
  18. }
  19. }
  20. const snapshotOf = (types) => JSON.stringify({ version: DICT_SNAPSHOT_VERSION, savedAt: 1, types })
  21. test('启动先读快照顶上:接口还没回来时就能出中文', async () => {
  22. resetDictLabelsForTest()
  23. const storage = memoryStorage({
  24. [DICT_STORAGE_KEY]: snapshotOf({ [DICT_TYPES.riskLevel]: { 1: '较大风险' } }),
  25. })
  26. const snapshot = loadDictSnapshot({ storage })
  27. assert.equal(snapshot.savedAt, 1)
  28. assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '较大风险')
  29. })
  30. test('接口刷新成功后覆盖内存并写回快照', async () => {
  31. resetDictLabelsForTest()
  32. const storage = memoryStorage()
  33. const result = await loadDictionaries({
  34. storage,
  35. now: () => 42,
  36. fetchDict: async (type) => (type === DICT_TYPES.riskLevel
  37. ? { state: 'success', data: [{ dictValue: '2', dictLabel: '一般风险' }] }
  38. : { state: 'failed', data: null }),
  39. })
  40. assert.deepEqual(result.refreshed, [DICT_TYPES.riskLevel])
  41. assert.equal(dictLabel(DICT_TYPES.riskLevel, '2'), '一般风险')
  42. const saved = parseDictSnapshot(storage.getItem(DICT_STORAGE_KEY))
  43. assert.equal(saved.savedAt, 42)
  44. assert.deepEqual(saved.types[DICT_TYPES.riskLevel], { 2: '一般风险' })
  45. })
  46. test('接口失败时保留快照里的字典,不改写、不清空', async () => {
  47. resetDictLabelsForTest()
  48. const storage = memoryStorage({
  49. [DICT_STORAGE_KEY]: snapshotOf({ [DICT_TYPES.riskColor]: { 1: '橙' } }),
  50. })
  51. const before = storage.getItem(DICT_STORAGE_KEY)
  52. const result = await loadDictionaries({
  53. storage,
  54. fetchDict: async () => { throw new Error('network down') },
  55. })
  56. assert.deepEqual(result.refreshed, [])
  57. assert.equal(dictLabel(DICT_TYPES.riskColor, '1'), '橙')
  58. assert.equal(storage.getItem(DICT_STORAGE_KEY), before)
  59. })
  60. test('快照损坏或版本不符时当作没有快照,界面回原码(字典为空)', () => {
  61. resetDictLabelsForTest()
  62. const broken = memoryStorage({ [DICT_STORAGE_KEY]: '{oops' })
  63. assert.equal(loadDictSnapshot({ storage: broken }), null)
  64. const stale = memoryStorage({
  65. [DICT_STORAGE_KEY]: JSON.stringify({ version: DICT_SNAPSHOT_VERSION - 1, types: { [DICT_TYPES.riskLevel]: { 1: '较大风险' } } }),
  66. })
  67. assert.equal(loadDictSnapshot({ storage: stale }), null)
  68. assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '')
  69. })
  70. test('localStorage 不可用(隐私模式)时退化为内存,不抛错', async () => {
  71. resetDictLabelsForTest()
  72. const result = await loadDictionaries({
  73. storage: null,
  74. fetchDict: async (type) => (type === DICT_TYPES.riskLevel
  75. ? { state: 'success', data: [{ dictValue: '3', dictLabel: '低风险' }] }
  76. : { state: 'failed', data: null }),
  77. })
  78. assert.deepEqual(result.refreshed, [DICT_TYPES.riskLevel])
  79. assert.equal(dictLabel(DICT_TYPES.riskLevel, '3'), '低风险')
  80. assert.equal(saveDictSnapshot({ storage: null }), null)
  81. })