| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import assert from 'node:assert/strict'
- import { test } from 'node:test'
- import { normalizeEntityDetail, entityDetailView } from '../src/modules/cockpit/domain/entityDetail.js'
- import { DICT_TYPES, resetDictLabelsForTest, setDictLabels } from '../src/modules/cockpit/domain/dictLabels.js'
- const RISK_LEVEL = { 0: '重大风险', 1: '较大风险', 2: '一般风险', 3: '低风险' }
- const RISK_COLOR = { 0: '红', 1: '橙', 2: '黄', 3: '蓝' }
- function useDictionaries() {
- resetDictLabelsForTest()
- setDictLabels(DICT_TYPES.riskLevel, RISK_LEVEL)
- setDictLabels(DICT_TYPES.riskColor, RISK_COLOR)
- }
- function targetView(attributes = {}, source = {}) {
- const raw = {
- category: 'PROTECTION_TARGET',
- id: 'r1',
- name: '污水处理厂防护目标',
- status: 1,
- attributes: { riskLevel: 1, riskColor: 1, riskScore: 72.5, assessmentTime: '2026-11-13T17:57:10', ...attributes },
- ...source,
- }
- return entityDetailView(normalizeEntityDetail({ kind: 'emergency', id: 'r1', category: 'PROTECTION_TARGET' }, raw))
- }
- const businessRows = (view) => view.sections.find((group) => group.title === '业务详情').fields
- const rowOf = (view, label) => businessRows(view).find((row) => row.label === label)
- test('风险等级按 risk_level 字典显示中文,徽标抬头写「风险等级」并按风险颜色着色', () => {
- useDictionaries()
- const view = targetView()
- assert.equal(view.status, '较大风险')
- assert.equal(view.statusCaption, '风险等级')
- assert.equal(view.tone, 'risk-2')
- })
- test('不再出现「未定义(原值:N)」', () => {
- useDictionaries()
- for (const code of [0, 1, 2, 3]) {
- const view = targetView({ riskLevel: code, riskColor: code }, { status: code })
- assert.ok(!JSON.stringify(view.sections).includes('未定义'), 'code ' + code + ' 仍出现了未定义')
- assert.ok(!view.status.includes('未定义'))
- }
- })
- test('风险等级与风险颜色不再进业务明细(改由抬头徽标承担)', () => {
- useDictionaries()
- const view = targetView()
- assert.equal(rowOf(view, '风险等级'), undefined)
- assert.equal(rowOf(view, '风险颜色'), undefined)
- assert.equal(rowOf(view, '风险分值').value, '72.5 分')
- })
- test('「风险等级」在整份视图里只出现一次(徽标),摘要不再重复', () => {
- useDictionaries()
- const view = targetView()
- const seen = []
- for (const item of view.summary) if (item.label === '风险等级') seen.push('summary')
- if (view.statusCaption === '风险等级') seen.push('badge')
- for (const group of view.sections) for (const item of group.fields) if (item.label === '风险等级') seen.push('section')
- assert.deepEqual(seen, ['badge'])
- })
- test('风险因素 JSON 串排成键值网格', () => {
- useDictionaries()
- const view = targetView({ riskFactors: '{"管材":"钢管","埋深":"1.8 米"}' })
- const row = rowOf(view, '风险因素')
- assert.deepEqual(row.pairs, [['管材', '钢管'], ['埋深', '1.8 米']])
- assert.equal(row.wide, true)
- })
- test('风险因素本身就是对象时同样成网格', () => {
- useDictionaries()
- const view = targetView({ riskFactors: { 周边: '学校', 距离: '30 米' } })
- assert.deepEqual(rowOf(view, '风险因素').pairs, [['周边', '学校'], ['距离', '30 米']])
- })
- test('风险因素不是合法 JSON 时原样展示,不当成网格', () => {
- useDictionaries()
- const view = targetView({ riskFactors: '管材钢管,埋深不明' })
- const row = rowOf(view, '风险因素')
- assert.equal(row.pairs, undefined)
- assert.equal(row.value, '管材钢管,埋深不明')
- })
- test('关联管网下发 name+code 时显示「名称(编码)」并给出聚焦入口', () => {
- useDictionaries()
- const view = targetView({ network: { code: 'PS-N-001', name: '滨江污水干管' } })
- const row = rowOf(view, '关联管网')
- assert.equal(row.value, '滨江污水干管(PS-N-001)')
- assert.deepEqual(row.action, { kind: 'focus-network', code: 'PS-N-001' })
- })
- test('关联管网只有内部 id 时显示「未提供」,不把 id 渲染出来', () => {
- useDictionaries()
- const withId = targetView({ networkId: 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4' })
- assert.equal(rowOf(withId, '关联管网').value, '未提供')
- assert.equal(rowOf(withId, '关联管网').action, undefined)
- assert.ok(!JSON.stringify(withId.sections).includes('a1b2c3d4e5f6'))
- })
- test('字典没取到时回原码,而不是编一个「未定义」', () => {
- resetDictLabelsForTest()
- const view = targetView()
- assert.equal(view.status, '1')
- assert.equal(view.tone, 'risk-2')
- const row = rowOf(view, '风险分值')
- assert.equal(row.value, '72.5 分')
- })
- test('后端已发布的中文风险等级原样透传', () => {
- useDictionaries()
- const view = targetView({ riskLevel: '待核查' }, { status: '待核查' })
- assert.equal(view.status, '待核查')
- })
- test('历史 high/medium/low 仍能翻译(风险评估服务的老形状,不是数字码)', () => {
- useDictionaries()
- assert.equal(targetView({}, { status: 'high' }).status, '高风险')
- })
|