| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import assert from 'node:assert/strict'
- import { test } from 'node:test'
- import { DICT_TYPES } from '../src/modules/cockpit/domain/dictLabels.js'
- import {
- DICT_SNAPSHOT_VERSION,
- buildDictSnapshot,
- parseDictSnapshot,
- } from '../src/modules/cockpit/domain/dictSnapshot.js'
- const sample = () => ({
- [DICT_TYPES.riskLevel]: { 0: '重大风险', 1: '较大风险' },
- [DICT_TYPES.riskColor]: { 0: '红', 1: '橙' },
- })
- test('快照带版本、保存时间与按类型的字典表', () => {
- const snapshot = buildDictSnapshot(sample(), 1700000000000)
- assert.equal(snapshot.version, DICT_SNAPSHOT_VERSION)
- assert.equal(snapshot.savedAt, 1700000000000)
- assert.deepEqual(snapshot.types[DICT_TYPES.riskLevel], { 0: '重大风险', 1: '较大风险' })
- })
- test('没有任何字典时不出空快照', () => {
- assert.equal(buildDictSnapshot({}), null)
- assert.equal(buildDictSnapshot(null), null)
- assert.equal(buildDictSnapshot({ [DICT_TYPES.riskLevel]: {} }), null)
- })
- test('往返:序列化后能解析回同一份字典', () => {
- const snapshot = buildDictSnapshot(sample(), 1)
- const parsed = parseDictSnapshot(JSON.stringify(snapshot))
- assert.deepEqual(parsed.types, snapshot.types)
- })
- test('版本不符、结构损坏、非法 JSON 都当作没有快照', () => {
- const snapshot = buildDictSnapshot(sample(), 1)
- assert.equal(parseDictSnapshot(JSON.stringify({ ...snapshot, version: 0 })), null)
- assert.equal(parseDictSnapshot('{not json'), null)
- assert.equal(parseDictSnapshot(JSON.stringify({ version: DICT_SNAPSHOT_VERSION })), null)
- assert.equal(parseDictSnapshot(JSON.stringify({ version: DICT_SNAPSHOT_VERSION, types: [] })), null)
- assert.equal(parseDictSnapshot(null), null)
- })
- test('快照里认不出的类型被丢弃,其余保留', () => {
- const parsed = parseDictSnapshot({
- version: DICT_SNAPSHOT_VERSION,
- savedAt: 5,
- types: { [DICT_TYPES.riskLevel]: { 0: '重大风险' }, pump_station_type: { 0: '供水泵站' } },
- })
- assert.deepEqual(Object.keys(parsed.types), [DICT_TYPES.riskLevel])
- })
|