import assert from 'node:assert/strict' import { test } from 'node:test' import { DICT_TYPES, dictLabel, hasDictLabels, resetDictLabelsForTest, setDictLabels, toDictLabels, } from '../src/modules/cockpit/domain/dictLabels.js' test('字典行归一化成 value→label,缺值行不收', () => { const map = toDictLabels([ { dictValue: '0', dictLabel: '重大风险' }, { dictValue: '1', dictLabel: '较大风险' }, { dictValue: '', dictLabel: '空值丢掉' }, { dict_value: '2', dict_label: '一般风险' }, ]) assert.deepEqual(map, { 0: '重大风险', 1: '较大风险', 2: '一般风险' }) assert.equal(toDictLabels([]), null) assert.equal(toDictLabels(null), null) }) test('注入后按类型翻译;没有这类字典或没有这个码都回空串', () => { resetDictLabelsForTest() assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '') setDictLabels(DICT_TYPES.riskLevel, [{ dictValue: '1', dictLabel: '较大风险' }]) assert.equal(dictLabel(DICT_TYPES.riskLevel, '1'), '较大风险') assert.equal(dictLabel(DICT_TYPES.riskLevel, 1), '较大风险') assert.equal(dictLabel(DICT_TYPES.riskLevel, '9'), '') assert.equal(dictLabel(DICT_TYPES.riskColor, '1'), '') assert.equal(hasDictLabels(DICT_TYPES.riskLevel), true) resetDictLabelsForTest() assert.equal(hasDictLabels(DICT_TYPES.riskLevel), false) }) test('空表不覆盖已有字典:接口返回空时继续用上一份', () => { resetDictLabelsForTest() setDictLabels(DICT_TYPES.riskColor, { 0: '红', 1: '橙' }) assert.equal(setDictLabels(DICT_TYPES.riskColor, []), false) assert.equal(dictLabel(DICT_TYPES.riskColor, '1'), '橙') }) test('未知字典类型不接收', () => { resetDictLabelsForTest() assert.equal(setDictLabels('pump_station_type', { 0: '供水泵站' }), false) assert.equal(hasDictLabels('pump_station_type'), false) })