| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- /**
- * 主键不上屏守卫(票 10)。
- *
- * 规格 §5:任何渲染层不得输出 32 位十六进制、`risk` 加数字、或纯数字类型 id 作为编码。
- * 这里按「最终渲染出来的文本」断言,而不是按字段名 —— 字段名会变、会被误标
- * (防护目标的 code 曾经直接塞 riskId,设备也曾直接用主键当名称),
- * 只有「用户在屏幕上看到的字」是最终判据。
- */
- import assert from 'node:assert/strict'
- import { test } from 'node:test'
- import { normalizeEntityDetail, entityDetailView } from '../src/modules/cockpit/domain/entityDetail.js'
- import { looksLikeInternalId } from '../src/modules/cockpit/domain/internalId.js'
- const HEX = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4'
- const HEX2 = '38d27d089bcc218124b0d6158e4a58fd'
- const UUID = '8e420978-3ac4-91c1-0871-82a319d6f019'
- /** 详情面板里真正会被组件渲染出来的文本(与 EntityDetailSections.vue 的取数一致)。 */
- function renderedTexts(view) {
- const out = []
- const push = (value) => {
- if (typeof value === 'string' && value.trim()) out.push(value)
- }
- push(view.title)
- push(view.subtitle)
- push(view.statusText)
- push(view.categoryText)
- push(view.levelLabel)
- for (const item of view.summary || []) {
- push(item.value)
- for (const pair of item.pairs || []) { push(pair[0]); push(pair[1]) }
- }
- for (const group of view.sections || []) {
- for (const field of group.fields || []) {
- push(field.value)
- push(field.note)
- for (const pair of field.pairs || []) { push(pair[0]); push(pair[1]) }
- }
- }
- for (const person of view.people || []) { push(person.role); push(person.name) }
- for (const device of view.devices || []) {
- push(device.name); push(device.code); push(device.type); push(device.location); push(device.status)
- }
- for (const attachment of view.attachments || []) { push(attachment.name); push(attachment.url) }
- for (const stage of view.stage || []) { push(stage.label); push(stage.value) }
- return out
- }
- const ASSET_CASES = [
- {
- category: 'PIPE_NETWORK',
- raw: { id: HEX, code: 'TCRQ202609080001', name: '太常片区燃气', attributes: { networkType: '0', material: HEX2, lengthMeters: 12.5 } },
- idValues: [HEX, HEX2],
- expect: [['材质', '未提供']],
- },
- {
- category: 'WATER_PLANT',
- raw: { id: HEX, code: 'WP-1', name: '一水厂', attributes: { treatmentProcess: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['处理工艺', '未提供']],
- },
- {
- category: 'PUMP_STATION',
- raw: { id: HEX, code: 'PS-1', name: '城南泵站', attributes: { principal: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['负责人', '未提供']],
- },
- {
- category: 'WATER_SOURCE',
- raw: { id: HEX, code: 'WS-1', name: '沅江水源地', attributes: { unit: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['所属单位', '未提供']],
- },
- {
- category: 'DRAINAGE_GATE',
- raw: { id: HEX, code: 'DG-1', name: '东堤闸门', attributes: { managementUnit: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['管理单位', '未提供']],
- },
- {
- category: 'DRAINAGE_STORAGE',
- raw: { id: HEX, code: 'DS-1', name: '调蓄池', attributes: { managementUnit: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['管理单位', '未提供']],
- },
- {
- category: 'WATER_USER',
- raw: { id: HEX, code: 'WU-1', name: '某用水户', attributes: { meterNumber: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['水表编号', '未提供']],
- },
- {
- category: 'WATER_FIRE_HYDRANT',
- raw: { id: HEX, code: 'FH-1', name: '中山路消火栓', attributes: { model: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['型号', '未提供']],
- },
- {
- category: 'MANHOLE',
- raw: { id: HEX, code: 'YL-YJ-002', name: '窨井002', attributes: { material: HEX2 } },
- idValues: [HEX, HEX2],
- expect: [['材质', '未提供']],
- },
- {
- // 应急资源:编码、名称、类型名三个位置都喂主键形状,答案只能是不显示。
- category: 'EMERGENCY_RESOURCE',
- raw: {
- id: HEX,
- code: HEX2,
- name: UUID,
- attributes: {
- deviceTypeName: '窨井盖监测终端',
- deviceModel: 'MJ-01',
- equipmentTypeId: 22,
- latestImage: '/profile/upload/2026/10/22/x.png',
- },
- },
- idValues: [HEX, HEX2, UUID],
- expect: [['设备类型', '窨井盖监测终端'], ['型号', 'MJ-01']],
- absentLabels: ['设备类型 id', 'equipmentTypeId'],
- },
- {
- // 危险源:三个关联对象只给业务引用,内部 id 不出现在渲染文本里。
- category: 'HAZARD',
- raw: {
- id: HEX,
- code: 'YH-20261201134850',
- name: '燃气泄漏',
- attributes: {
- hazardType: '3',
- equipmentId: HEX2,
- networkId: UUID,
- workOrderId: 320,
- equipment: { code: '2607230190000022', name: '可燃气体-022' },
- network: { code: 'TCRQ202609080001', name: '太常片区燃气' },
- workOrder: { code: 'WO202612011354251210' },
- },
- },
- idValues: [HEX, HEX2, UUID, '320'],
- expect: [
- ['关联管网', '太常片区燃气(TCRQ202609080001)'],
- ['关联设备', '可燃气体-022(2607230190000022)'],
- ['关联工单', 'WO202612011354251210'],
- ],
- },
- {
- // 防护目标:riskId 是主键,历史上被当成 code 发出来;这里连 code 位置也喂它。
- category: 'PROTECTION_TARGET',
- raw: {
- id: 'risk001',
- code: 'risk001',
- name: '阳光小学',
- attributes: { riskLevel: '1', riskColor: '1', networkId: 'net001' },
- },
- idValues: ['risk001', 'net001'],
- expect: [['关联管网', '未提供']],
- },
- ]
- function assetView({ category, raw }) {
- const kind = ['HAZARD', 'PROTECTION_TARGET', 'EMERGENCY_RESOURCE'].includes(category) ? 'emergency' : 'facility'
- return entityDetailView(normalizeEntityDetail({ kind, id: raw.id, category }, raw))
- }
- function fieldValue(view, label) {
- for (const group of view.sections || []) {
- const found = (group.fields || []).find((item) => item.label === label)
- if (found) return found.value
- }
- return undefined
- }
- test('详情面板:任何类别都不把主键形状的值渲成文本', () => {
- for (const item of ASSET_CASES) {
- const view = assetView(item)
- const texts = renderedTexts(view)
- for (const value of texts) {
- assert.equal(looksLikeInternalId(value), false, `${item.category} 把主键形状的值渲染出来了:${value}`)
- }
- for (const id of item.idValues) {
- assert.equal(texts.includes(id), false, `${item.category} 把内部 id 渲染出来了:${id}`)
- }
- for (const [label, expected] of item.expect || []) {
- assert.equal(fieldValue(view, label), expected, `${item.category} 的「${label}」不对`)
- }
- for (const label of item.absentLabels || []) {
- assert.equal(fieldValue(view, label), undefined, `${item.category} 不该有「${label}」这一行`)
- }
- }
- })
- test('业务引用:名称形如主键时只留下编码,不拼成「主键(编码)」', () => {
- // 现网确有 42 台设备的名称就是 32 位主键:拼进「名称(编码)」同样是把主键印上屏。
- const view = assetView({
- category: 'HAZARD',
- raw: {
- id: 'h-ref',
- code: 'YH-20261201134850',
- name: '燃气泄漏',
- attributes: {
- equipment: { code: '2607230190000022', name: HEX },
- network: { code: 'TCRQ202609080001', name: HEX2 },
- workOrder: { code: HEX },
- },
- },
- })
- assert.equal(fieldValue(view, '关联设备'), '2607230190000022')
- assert.equal(fieldValue(view, '关联管网'), 'TCRQ202609080001')
- assert.equal(fieldValue(view, '关联工单'), '未提供')
- for (const value of renderedTexts(view)) {
- assert.equal(looksLikeInternalId(value), false, `引用里混进了主键形状的值:${value}`)
- assert.equal(value.includes(HEX), false, '引用文案里出现了内部主键')
- }
- })
- test('应急资源:设备类型显示类型表的名字,设备图片走附件区,不再有 id 行', () => {
- const view = assetView(ASSET_CASES.find((item) => item.category === 'EMERGENCY_RESOURCE'))
- assert.equal(fieldValue(view, '设备类型'), '窨井盖监测终端')
- assert.deepEqual(view.attachments.map((item) => item.url), ['/profile/upload/2026/10/22/x.png'])
- // 抬头:主键形状的编码与名称都不显示,回落到「未提供」。
- assert.equal(view.title, '应急对象名称未提供')
- assert.match(view.subtitle, /编码未提供/)
- })
- test('预警详情与工单详情同样不渲主键', () => {
- const event = entityDetailView(normalizeEntityDetail({ kind: 'event', id: HEX, family: 'WARNING' }, {
- basicInfo: { warningNo: 'YJ202611241647341367', warningName: '测试预警', status: 'PROCESSING', eventTime: '2026-11-24T16:47:34.000+08:00' },
- alarmList: [{ alarmId: HEX2, alarmNo: 'BJ20261124001', alarmName: '压力报警', status: 'HANDLED' }],
- }))
- const order = entityDetailView(normalizeEntityDetail({ kind: 'work-order', id: '279' }, {
- order: { orderId: 279, orderNo: 'WO-2026120100001', orderStatus: 3, orderLevel: 2, orderDesc: '测试压力故障' },
- timelineEntries: [{ stateCode: 3, stateName: '处理中', operationName: '现场处置', operatedAt: '2026-12-01T09:30:00+08:00' }],
- }))
- for (const [name, view] of [['预警详情', event], ['工单详情', order]]) {
- const texts = renderedTexts(view)
- for (const value of texts) {
- assert.equal(looksLikeInternalId(value), false, `${name} 把主键形状的值渲染出来了:${value}`)
- }
- assert.equal(texts.includes(HEX), false, `${name} 把内部 id 渲染出来了`)
- assert.equal(texts.includes('279'), false, `${name} 把工单主键渲染出来了`)
- }
- })
- test('内部主键识别只认主键形状,不吞业务编码与普通读数', () => {
- for (const value of [HEX, HEX.toUpperCase(), UUID, 'risk001', 'raw_P11920']) {
- assert.equal(looksLikeInternalId(value), true, `${value} 应判为主键`)
- }
- // 32 位纯数字更像业务编码,不能被当成主键吞掉。
- for (const value of ['2607230190000022', '12345678901234567890123456789012', 'TCRQ202609080001', 'WO202612011354251210', 'YH-20261201134850', '2', 320, '二级预警', '', null, undefined]) {
- assert.equal(looksLikeInternalId(value), false, `${value} 不该判成主键`)
- }
- })
|