| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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'
- /**
- * 真实契约回归(2026-12-01 线上 8301 实测)。
- *
- * 这份 fixture 是从线上响应原样抄下来的,用来钉住「后端实际长什么样」:
- * · 防护目标顶层 code 是 null(riskId 不再当编码);
- * · network 在 attributes 里,给 { code, name };
- * · riskLevel / riskColor 是**字符串**码值;status 也是字符串;
- * · riskFactors 是 **JSON 字符串**,不是对象。
- * 合成数据再过一遍看不出这些形状差异,所以单独留一份。
- */
- const RISK_LEVEL = [
- { dictValue: '0', dictLabel: '重大风险' }, { dictValue: '1', dictLabel: '较大风险' },
- { dictValue: '2', dictLabel: '一般风险' }, { dictValue: '3', dictLabel: '低风险' },
- ]
- const RISK_COLOR = [
- { dictValue: '0', dictLabel: '红' }, { dictValue: '1', dictLabel: '橙' },
- { dictValue: '2', dictLabel: '黄' }, { dictValue: '3', dictLabel: '蓝' },
- ]
- const REAL_PAYLOAD = {
- requestId: '176382a308a74519a53d195d1f265786',
- apiVersion: 'v1',
- readOnly: true,
- category: 'PROTECTION_TARGET',
- id: 'risk001',
- code: null,
- name: '郑东新区CBD商业中心、河南省政府、郑州国际会展中心',
- status: '1',
- location: null,
- district: null,
- districtName: null,
- coordinates: null,
- attributes: {
- network: { code: 'TCRQ202609080001', name: '太常片区燃气' },
- riskLevel: '1',
- riskScore: 72.5,
- riskColor: '1',
- assessmentModel: 'Kent Muhlbauer风险评估模型V2.1',
- assessmentTime: '2025-01-15T10:00:00',
- riskFactors: '{"管材":"钢管","压力":"高压1.6MPa","管龄":"14年","防腐状况":"局部腐蚀","土壤腐蚀性":"中等"}',
- hazardSources: '周边地铁1号线、地下综合管廊施工',
- remark: '高后果区(HCAs)管线,需加强巡检',
- },
- updatedAt: '2025-01-15T10:00:00+08:00',
- }
- function realView(overrides = {}) {
- resetDictLabelsForTest()
- setDictLabels(DICT_TYPES.riskLevel, RISK_LEVEL)
- setDictLabels(DICT_TYPES.riskColor, RISK_COLOR)
- const payload = { ...REAL_PAYLOAD, ...overrides }
- return entityDetailView(normalizeEntityDetail(
- { kind: 'emergency', id: 'risk001', category: 'PROTECTION_TARGET' },
- payload,
- ))
- }
- const businessRows = (view) => view.sections.find((group) => group.title === '业务详情').fields
- test('真实响应:风险等级与颜色按字典翻成中文,抬头写「风险等级」', () => {
- const view = realView()
- assert.equal(view.status, '较大风险')
- assert.equal(view.statusCaption, '风险等级')
- assert.equal(view.tone, 'risk-2')
- assert.ok(!JSON.stringify(view).includes('未定义'))
- })
- test('真实响应:code 为 null 时不生成编码字段,抬头只有「应急对象」', () => {
- const view = realView()
- assert.equal(view.subtitle, '应急对象')
- const basic = view.sections.find((group) => group.title === '基本信息').fields
- assert.equal(basic.some((row) => row.label.includes('编码')), false)
- })
- test('真实响应:关联管网按 { code, name } 显示并给聚焦入口', () => {
- const view = realView()
- const row = businessRows(view).find((item) => item.label === '关联管网')
- assert.equal(row.value, '太常片区燃气(TCRQ202609080001)')
- assert.deepEqual(row.action, { kind: 'focus-network', code: 'TCRQ202609080001' })
- })
- test('真实响应:riskFactors 是 JSON 字符串,排成键值网格', () => {
- const row = businessRows(realView()).find((item) => item.label === '风险因素')
- assert.equal(row.pairs.length, 5)
- assert.deepEqual(row.pairs[0], ['管材', '钢管'])
- })
- test('真实响应:风险等级与风险颜色不出现在业务明细里', () => {
- const labels = businessRows(realView()).map((item) => item.label)
- assert.equal(labels.includes('风险等级'), false)
- assert.equal(labels.includes('风险颜色'), false)
- })
- test('真实响应缺 network 时显示「未提供」,不出现空括号或 id', () => {
- const view = realView({ attributes: { ...REAL_PAYLOAD.attributes, network: undefined } })
- const row = businessRows(view).find((item) => item.label === '关联管网')
- assert.equal(row.value, '未提供')
- assert.equal(row.action, undefined)
- })
|