protectionTargetRealContract.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import assert from 'node:assert/strict'
  2. import { test } from 'node:test'
  3. import { normalizeEntityDetail, entityDetailView } from '../src/modules/cockpit/domain/entityDetail.js'
  4. import { DICT_TYPES, resetDictLabelsForTest, setDictLabels } from '../src/modules/cockpit/domain/dictLabels.js'
  5. /**
  6. * 真实契约回归(2026-12-01 线上 8301 实测)。
  7. *
  8. * 这份 fixture 是从线上响应原样抄下来的,用来钉住「后端实际长什么样」:
  9. * · 防护目标顶层 code 是 null(riskId 不再当编码);
  10. * · network 在 attributes 里,给 { code, name };
  11. * · riskLevel / riskColor 是**字符串**码值;status 也是字符串;
  12. * · riskFactors 是 **JSON 字符串**,不是对象。
  13. * 合成数据再过一遍看不出这些形状差异,所以单独留一份。
  14. */
  15. const RISK_LEVEL = [
  16. { dictValue: '0', dictLabel: '重大风险' }, { dictValue: '1', dictLabel: '较大风险' },
  17. { dictValue: '2', dictLabel: '一般风险' }, { dictValue: '3', dictLabel: '低风险' },
  18. ]
  19. const RISK_COLOR = [
  20. { dictValue: '0', dictLabel: '红' }, { dictValue: '1', dictLabel: '橙' },
  21. { dictValue: '2', dictLabel: '黄' }, { dictValue: '3', dictLabel: '蓝' },
  22. ]
  23. const REAL_PAYLOAD = {
  24. requestId: '176382a308a74519a53d195d1f265786',
  25. apiVersion: 'v1',
  26. readOnly: true,
  27. category: 'PROTECTION_TARGET',
  28. id: 'risk001',
  29. code: null,
  30. name: '郑东新区CBD商业中心、河南省政府、郑州国际会展中心',
  31. status: '1',
  32. location: null,
  33. district: null,
  34. districtName: null,
  35. coordinates: null,
  36. attributes: {
  37. network: { code: 'TCRQ202609080001', name: '太常片区燃气' },
  38. riskLevel: '1',
  39. riskScore: 72.5,
  40. riskColor: '1',
  41. assessmentModel: 'Kent Muhlbauer风险评估模型V2.1',
  42. assessmentTime: '2025-01-15T10:00:00',
  43. riskFactors: '{"管材":"钢管","压力":"高压1.6MPa","管龄":"14年","防腐状况":"局部腐蚀","土壤腐蚀性":"中等"}',
  44. hazardSources: '周边地铁1号线、地下综合管廊施工',
  45. remark: '高后果区(HCAs)管线,需加强巡检',
  46. },
  47. updatedAt: '2025-01-15T10:00:00+08:00',
  48. }
  49. function realView(overrides = {}) {
  50. resetDictLabelsForTest()
  51. setDictLabels(DICT_TYPES.riskLevel, RISK_LEVEL)
  52. setDictLabels(DICT_TYPES.riskColor, RISK_COLOR)
  53. const payload = { ...REAL_PAYLOAD, ...overrides }
  54. return entityDetailView(normalizeEntityDetail(
  55. { kind: 'emergency', id: 'risk001', category: 'PROTECTION_TARGET' },
  56. payload,
  57. ))
  58. }
  59. const businessRows = (view) => view.sections.find((group) => group.title === '业务详情').fields
  60. test('真实响应:风险等级与颜色按字典翻成中文,抬头写「风险等级」', () => {
  61. const view = realView()
  62. assert.equal(view.status, '较大风险')
  63. assert.equal(view.statusCaption, '风险等级')
  64. assert.equal(view.tone, 'risk-2')
  65. assert.ok(!JSON.stringify(view).includes('未定义'))
  66. })
  67. test('真实响应:code 为 null 时不生成编码字段,且基本信息整栏已删', () => {
  68. const view = realView()
  69. assert.equal(view.subtitle, '应急对象')
  70. assert.equal(view.sections.some((group) => group.title === '基本信息'), false)
  71. assert.equal(JSON.stringify(view.sections).includes('对象编码'), false)
  72. })
  73. test('真实响应:关联管网按 { code, name } 显示并给聚焦入口', () => {
  74. const view = realView()
  75. const row = businessRows(view).find((item) => item.label === '关联管网')
  76. assert.equal(row.value, '太常片区燃气(TCRQ202609080001)')
  77. assert.deepEqual(row.action, { kind: 'focus-network', code: 'TCRQ202609080001' })
  78. })
  79. test('真实响应:riskFactors 是 JSON 字符串,排成键值网格', () => {
  80. const row = businessRows(realView()).find((item) => item.label === '风险因素')
  81. assert.equal(row.pairs.length, 5)
  82. assert.deepEqual(row.pairs[0], ['管材', '钢管'])
  83. })
  84. test('真实响应:风险等级与风险颜色不出现在业务明细里', () => {
  85. const labels = businessRows(realView()).map((item) => item.label)
  86. assert.equal(labels.includes('风险等级'), false)
  87. assert.equal(labels.includes('风险颜色'), false)
  88. })
  89. test('真实响应缺 network 时显示「未提供」,不出现空括号或 id', () => {
  90. const view = realView({ attributes: { ...REAL_PAYLOAD.attributes, network: undefined } })
  91. const row = businessRows(view).find((item) => item.label === '关联管网')
  92. assert.equal(row.value, '未提供')
  93. assert.equal(row.action, undefined)
  94. })