entityDetailHazard.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /** 真实字典(线上 /dict/*,2026-12-01)。 */
  6. const DICTS = {
  7. [DICT_TYPES.hazardType]: [['0', '腐蚀'], ['1', '第三方破坏'], ['2', '占压'], ['3', '泄漏']],
  8. [DICT_TYPES.hazardLevel]: [['1', '一级隐患'], ['2', '二级隐患'], ['3', '三级隐患'], ['4', '四级隐患']],
  9. [DICT_TYPES.hazardSource]: [['0', '巡检'], ['1', '政府检查'], ['2', '企业上报'], ['3', '模型辨识']],
  10. }
  11. function useDictionaries() {
  12. resetDictLabelsForTest()
  13. for (const [type, rows] of Object.entries(DICTS)) {
  14. setDictLabels(type, rows.map(([dictValue, dictLabel]) => ({ dictValue, dictLabel })))
  15. }
  16. }
  17. /** 线上真实响应原文(YH-20261201134850,2026-12-01 实测)。 */
  18. const REAL_PAYLOAD = {
  19. requestId: 'ba80165ff557475baadeeed1671bf381',
  20. apiVersion: 'v1',
  21. readOnly: true,
  22. category: 'HAZARD',
  23. id: '156cdc681e9fa53932ba4c1efe277b9c',
  24. code: 'YH-20261201134850',
  25. name: 'test',
  26. status: '待核查',
  27. location: null,
  28. district: 1,
  29. districtName: '城北',
  30. coordinates: { longitude: 110.361206, latitude: 28.45557, endLongitude: null, endLatitude: null },
  31. attributes: {
  32. hazardType: '3',
  33. level: '2',
  34. source: '0',
  35. description: 'test',
  36. equipment: { code: '2607230190000022', name: '可燃气体-022' },
  37. workOrder: { code: 'WO202612011354251210' },
  38. reportTime: '2026-12-01T00:00:00',
  39. rectifyPlan: '',
  40. rectifyDeadline: '2026-12-04',
  41. rectifyPerson: '',
  42. images: '/hazard/2026/12/01/bbf71b8ebf6645b0ba3666cf13a37e83.png',
  43. },
  44. updatedAt: '2026-12-01T13:51:52+08:00',
  45. }
  46. function hazardView(attributes = {}, source = {}) {
  47. useDictionaries()
  48. return entityDetailView(normalizeEntityDetail(
  49. { kind: 'emergency', id: REAL_PAYLOAD.id, category: 'HAZARD' },
  50. { ...REAL_PAYLOAD, ...source, attributes: { ...REAL_PAYLOAD.attributes, ...attributes } },
  51. ))
  52. }
  53. const businessRows = (view) => view.sections.find((group) => group.title === '业务详情').fields
  54. const rowOf = (view, label) => businessRows(view).find((row) => row.label === label)
  55. test('隐患类型/等级/来源按业务库字典显示中文,不出现裸码', () => {
  56. const view = hazardView()
  57. assert.equal(rowOf(view, '隐患类型').value, '泄漏')
  58. assert.equal(rowOf(view, '隐患等级').value, '二级隐患')
  59. assert.equal(rowOf(view, '来源').value, '巡检')
  60. for (const label of ['隐患类型', '隐患等级', '来源']) {
  61. assert.ok(!/^[0-9]+$/.test(rowOf(view, label).value), label + ' 仍是裸码')
  62. }
  63. })
  64. test('字典没取到时回原码,不编「未知」', () => {
  65. resetDictLabelsForTest()
  66. const view = entityDetailView(normalizeEntityDetail(
  67. { kind: 'emergency', id: REAL_PAYLOAD.id, category: 'HAZARD' },
  68. REAL_PAYLOAD,
  69. ))
  70. assert.equal(rowOf(view, '隐患类型').value, '3')
  71. assert.equal(rowOf(view, '隐患等级').value, '2')
  72. })
  73. test('关联设备显示「名称(编码)」并给定位入口', () => {
  74. const row = rowOf(hazardView(), '关联设备')
  75. assert.equal(row.value, '可燃气体-022(2607230190000022)')
  76. assert.deepEqual(row.action, { kind: 'locate-equipment', code: '2607230190000022' })
  77. })
  78. test('关联工单显示工单号并可打开(后端只给 code,没有名称列)', () => {
  79. const row = rowOf(hazardView(), '关联工单')
  80. assert.equal(row.value, 'WO202612011354251210')
  81. assert.deepEqual(row.action, { kind: 'open-work-order', code: 'WO202612011354251210' })
  82. })
  83. test('没有关联管网时显示「未提供」,且不出现内部 id', () => {
  84. const view = hazardView()
  85. const row = rowOf(view, '关联管网')
  86. assert.equal(row.value, '未提供')
  87. assert.equal(row.action, undefined)
  88. assert.ok(!JSON.stringify(view.sections).includes('156cdc681e9fa539'))
  89. })
  90. test('隐患图片不再作为明细行,改为附件数据(缩略图由共享组件渲染)', () => {
  91. const view = hazardView()
  92. assert.equal(businessRows(view).some((row) => row.label === '隐患图片'), false)
  93. assert.equal(view.attachments.length, 1)
  94. assert.match(view.attachments[0].url, /bbf71b8ebf6645b0ba3666cf13a37e83\.png$/)
  95. })
  96. test('时间按粒度显示:00:00:00 占位只显示日期,整改期限同样只到日', () => {
  97. const view = hazardView()
  98. assert.equal(rowOf(view, '报告时间').value, '2026-12-01')
  99. assert.equal(rowOf(view, '整改期限').value, '2026-12-04')
  100. assert.ok(!businessRows(view).some((row) => String(row.value).includes('00:00:00')))
  101. })
  102. test('后端已发布的中文状态原样透传;有业务编码列时展现编码', () => {
  103. const view = hazardView()
  104. assert.equal(view.status, '待核查')
  105. assert.match(view.subtitle, /YH-20261201134850/)
  106. const basic = view.sections.find((group) => group.title === '基本信息').fields
  107. // 应急对象的编码行叫「对象编码」(规格 §7);危险源有业务编码列,所以这行存在。
  108. assert.ok(basic.some((row) => row.label === '对象编码' && row.value === 'YH-20261201134850'))
  109. })