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' /** 真实字典(线上 /dict/*,2026-12-01)。 */ const DICTS = { [DICT_TYPES.hazardType]: [['0', '腐蚀'], ['1', '第三方破坏'], ['2', '占压'], ['3', '泄漏']], [DICT_TYPES.hazardLevel]: [['1', '一级隐患'], ['2', '二级隐患'], ['3', '三级隐患'], ['4', '四级隐患']], [DICT_TYPES.hazardSource]: [['0', '巡检'], ['1', '政府检查'], ['2', '企业上报'], ['3', '模型辨识']], } function useDictionaries() { resetDictLabelsForTest() for (const [type, rows] of Object.entries(DICTS)) { setDictLabels(type, rows.map(([dictValue, dictLabel]) => ({ dictValue, dictLabel }))) } } /** 线上真实响应原文(YH-20261201134850,2026-12-01 实测)。 */ const REAL_PAYLOAD = { requestId: 'ba80165ff557475baadeeed1671bf381', apiVersion: 'v1', readOnly: true, category: 'HAZARD', id: '156cdc681e9fa53932ba4c1efe277b9c', code: 'YH-20261201134850', name: 'test', status: '待核查', location: null, district: 1, districtName: '城北', coordinates: { longitude: 110.361206, latitude: 28.45557, endLongitude: null, endLatitude: null }, attributes: { hazardType: '3', level: '2', source: '0', description: 'test', equipment: { code: '2607230190000022', name: '可燃气体-022' }, workOrder: { code: 'WO202612011354251210' }, reportTime: '2026-12-01T00:00:00', rectifyPlan: '', rectifyDeadline: '2026-12-04', rectifyPerson: '', images: '/hazard/2026/12/01/bbf71b8ebf6645b0ba3666cf13a37e83.png', }, updatedAt: '2026-12-01T13:51:52+08:00', } function hazardView(attributes = {}, source = {}) { useDictionaries() return entityDetailView(normalizeEntityDetail( { kind: 'emergency', id: REAL_PAYLOAD.id, category: 'HAZARD' }, { ...REAL_PAYLOAD, ...source, attributes: { ...REAL_PAYLOAD.attributes, ...attributes } }, )) } const businessRows = (view) => view.sections.find((group) => group.title === '业务详情').fields const rowOf = (view, label) => businessRows(view).find((row) => row.label === label) test('隐患类型/等级/来源按业务库字典显示中文,不出现裸码', () => { const view = hazardView() assert.equal(rowOf(view, '隐患类型').value, '泄漏') assert.equal(rowOf(view, '隐患等级').value, '二级隐患') assert.equal(rowOf(view, '来源').value, '巡检') for (const label of ['隐患类型', '隐患等级', '来源']) { assert.ok(!/^[0-9]+$/.test(rowOf(view, label).value), label + ' 仍是裸码') } }) test('字典没取到时回原码,不编「未知」', () => { resetDictLabelsForTest() const view = entityDetailView(normalizeEntityDetail( { kind: 'emergency', id: REAL_PAYLOAD.id, category: 'HAZARD' }, REAL_PAYLOAD, )) assert.equal(rowOf(view, '隐患类型').value, '3') assert.equal(rowOf(view, '隐患等级').value, '2') }) test('关联设备显示「名称(编码)」并给定位入口', () => { const row = rowOf(hazardView(), '关联设备') assert.equal(row.value, '可燃气体-022(2607230190000022)') assert.deepEqual(row.action, { kind: 'locate-equipment', code: '2607230190000022' }) }) test('关联工单显示工单号并可打开(后端只给 code,没有名称列)', () => { const row = rowOf(hazardView(), '关联工单') assert.equal(row.value, 'WO202612011354251210') assert.deepEqual(row.action, { kind: 'open-work-order', code: 'WO202612011354251210' }) }) test('没有关联管网时显示「未提供」,且不出现内部 id', () => { const view = hazardView() const row = rowOf(view, '关联管网') assert.equal(row.value, '未提供') assert.equal(row.action, undefined) assert.ok(!JSON.stringify(view.sections).includes('156cdc681e9fa539')) }) test('隐患图片不再作为明细行,改为附件数据(缩略图由共享组件渲染)', () => { const view = hazardView() assert.equal(businessRows(view).some((row) => row.label === '隐患图片'), false) assert.equal(view.attachments.length, 1) assert.match(view.attachments[0].url, /bbf71b8ebf6645b0ba3666cf13a37e83\.png$/) }) test('时间按粒度显示:00:00:00 占位只显示日期,整改期限同样只到日', () => { const view = hazardView() assert.equal(rowOf(view, '报告时间').value, '2026-12-01') assert.equal(rowOf(view, '整改期限').value, '2026-12-04') assert.ok(!businessRows(view).some((row) => String(row.value).includes('00:00:00'))) }) test('后端已发布的中文状态原样透传;有业务编码列时展现编码', () => { const view = hazardView() assert.equal(view.status, '待核查') assert.match(view.subtitle, /YH-20261201134850/) const basic = view.sections.find((group) => group.title === '基本信息').fields // 应急对象的编码行叫「对象编码」(规格 §7);危险源有业务编码列,所以这行存在。 assert.ok(basic.some((row) => row.label === '对象编码' && row.value === 'YH-20261201134850')) })