internalIdGuard.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * 主键不上屏守卫(票 10)。
  3. *
  4. * 规格 §5:任何渲染层不得输出 32 位十六进制、`risk` 加数字、或纯数字类型 id 作为编码。
  5. * 这里按「最终渲染出来的文本」断言,而不是按字段名 —— 字段名会变、会被误标
  6. * (防护目标的 code 曾经直接塞 riskId,设备也曾直接用主键当名称),
  7. * 只有「用户在屏幕上看到的字」是最终判据。
  8. */
  9. import assert from 'node:assert/strict'
  10. import { test } from 'node:test'
  11. import { normalizeEntityDetail, entityDetailView } from '../src/modules/cockpit/domain/entityDetail.js'
  12. import { looksLikeInternalId } from '../src/modules/cockpit/domain/internalId.js'
  13. const HEX = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4'
  14. const HEX2 = '38d27d089bcc218124b0d6158e4a58fd'
  15. const UUID = '8e420978-3ac4-91c1-0871-82a319d6f019'
  16. /** 详情面板里真正会被组件渲染出来的文本(与 EntityDetailSections.vue 的取数一致)。 */
  17. function renderedTexts(view) {
  18. const out = []
  19. const push = (value) => {
  20. if (typeof value === 'string' && value.trim()) out.push(value)
  21. }
  22. push(view.title)
  23. push(view.subtitle)
  24. push(view.statusText)
  25. push(view.categoryText)
  26. push(view.levelLabel)
  27. for (const item of view.summary || []) {
  28. push(item.value)
  29. for (const pair of item.pairs || []) { push(pair[0]); push(pair[1]) }
  30. }
  31. for (const group of view.sections || []) {
  32. for (const field of group.fields || []) {
  33. push(field.value)
  34. push(field.note)
  35. for (const pair of field.pairs || []) { push(pair[0]); push(pair[1]) }
  36. }
  37. }
  38. for (const person of view.people || []) { push(person.role); push(person.name) }
  39. for (const device of view.devices || []) {
  40. push(device.name); push(device.code); push(device.type); push(device.location); push(device.status)
  41. }
  42. for (const attachment of view.attachments || []) { push(attachment.name); push(attachment.url) }
  43. for (const stage of view.stage || []) { push(stage.label); push(stage.value) }
  44. return out
  45. }
  46. const ASSET_CASES = [
  47. {
  48. category: 'PIPE_NETWORK',
  49. raw: { id: HEX, code: 'TCRQ202609080001', name: '太常片区燃气', attributes: { networkType: '0', material: HEX2, lengthMeters: 12.5 } },
  50. idValues: [HEX, HEX2],
  51. expect: [['材质', '未提供']],
  52. },
  53. {
  54. category: 'WATER_PLANT',
  55. raw: { id: HEX, code: 'WP-1', name: '一水厂', attributes: { treatmentProcess: HEX2 } },
  56. idValues: [HEX, HEX2],
  57. expect: [['处理工艺', '未提供']],
  58. },
  59. {
  60. category: 'PUMP_STATION',
  61. raw: { id: HEX, code: 'PS-1', name: '城南泵站', attributes: { principal: HEX2 } },
  62. idValues: [HEX, HEX2],
  63. expect: [['负责人', '未提供']],
  64. },
  65. {
  66. category: 'WATER_SOURCE',
  67. raw: { id: HEX, code: 'WS-1', name: '沅江水源地', attributes: { unit: HEX2 } },
  68. idValues: [HEX, HEX2],
  69. expect: [['所属单位', '未提供']],
  70. },
  71. {
  72. category: 'DRAINAGE_GATE',
  73. raw: { id: HEX, code: 'DG-1', name: '东堤闸门', attributes: { managementUnit: HEX2 } },
  74. idValues: [HEX, HEX2],
  75. expect: [['管理单位', '未提供']],
  76. },
  77. {
  78. category: 'DRAINAGE_STORAGE',
  79. raw: { id: HEX, code: 'DS-1', name: '调蓄池', attributes: { managementUnit: HEX2 } },
  80. idValues: [HEX, HEX2],
  81. expect: [['管理单位', '未提供']],
  82. },
  83. {
  84. category: 'WATER_USER',
  85. raw: { id: HEX, code: 'WU-1', name: '某用水户', attributes: { meterNumber: HEX2 } },
  86. idValues: [HEX, HEX2],
  87. expect: [['水表编号', '未提供']],
  88. },
  89. {
  90. category: 'WATER_FIRE_HYDRANT',
  91. raw: { id: HEX, code: 'FH-1', name: '中山路消火栓', attributes: { model: HEX2 } },
  92. idValues: [HEX, HEX2],
  93. expect: [['型号', '未提供']],
  94. },
  95. {
  96. category: 'MANHOLE',
  97. raw: { id: HEX, code: 'YL-YJ-002', name: '窨井002', attributes: { material: HEX2 } },
  98. idValues: [HEX, HEX2],
  99. expect: [['材质', '未提供']],
  100. },
  101. {
  102. // 应急资源:编码、名称、类型名三个位置都喂主键形状,答案只能是不显示。
  103. category: 'EMERGENCY_RESOURCE',
  104. raw: {
  105. id: HEX,
  106. code: HEX2,
  107. name: UUID,
  108. attributes: {
  109. deviceTypeName: '窨井盖监测终端',
  110. deviceModel: 'MJ-01',
  111. equipmentTypeId: 22,
  112. latestImage: '/profile/upload/2026/10/22/x.png',
  113. },
  114. },
  115. idValues: [HEX, HEX2, UUID],
  116. expect: [['设备类型', '窨井盖监测终端'], ['型号', 'MJ-01']],
  117. absentLabels: ['设备类型 id', 'equipmentTypeId'],
  118. },
  119. {
  120. // 危险源:三个关联对象只给业务引用,内部 id 不出现在渲染文本里。
  121. category: 'HAZARD',
  122. raw: {
  123. id: HEX,
  124. code: 'YH-20261201134850',
  125. name: '燃气泄漏',
  126. attributes: {
  127. hazardType: '3',
  128. equipmentId: HEX2,
  129. networkId: UUID,
  130. workOrderId: 320,
  131. equipment: { code: '2607230190000022', name: '可燃气体-022' },
  132. network: { code: 'TCRQ202609080001', name: '太常片区燃气' },
  133. workOrder: { code: 'WO202612011354251210' },
  134. },
  135. },
  136. idValues: [HEX, HEX2, UUID, '320'],
  137. expect: [
  138. ['关联管网', '太常片区燃气(TCRQ202609080001)'],
  139. ['关联设备', '可燃气体-022(2607230190000022)'],
  140. ['关联工单', 'WO202612011354251210'],
  141. ],
  142. },
  143. {
  144. // 防护目标:riskId 是主键,历史上被当成 code 发出来;这里连 code 位置也喂它。
  145. category: 'PROTECTION_TARGET',
  146. raw: {
  147. id: 'risk001',
  148. code: 'risk001',
  149. name: '阳光小学',
  150. attributes: { riskLevel: '1', riskColor: '1', networkId: 'net001' },
  151. },
  152. idValues: ['risk001', 'net001'],
  153. expect: [['关联管网', '未提供']],
  154. },
  155. ]
  156. function assetView({ category, raw }) {
  157. const kind = ['HAZARD', 'PROTECTION_TARGET', 'EMERGENCY_RESOURCE'].includes(category) ? 'emergency' : 'facility'
  158. return entityDetailView(normalizeEntityDetail({ kind, id: raw.id, category }, raw))
  159. }
  160. function fieldValue(view, label) {
  161. for (const group of view.sections || []) {
  162. const found = (group.fields || []).find((item) => item.label === label)
  163. if (found) return found.value
  164. }
  165. return undefined
  166. }
  167. test('详情面板:任何类别都不把主键形状的值渲成文本', () => {
  168. for (const item of ASSET_CASES) {
  169. const view = assetView(item)
  170. const texts = renderedTexts(view)
  171. for (const value of texts) {
  172. assert.equal(looksLikeInternalId(value), false, `${item.category} 把主键形状的值渲染出来了:${value}`)
  173. }
  174. for (const id of item.idValues) {
  175. assert.equal(texts.includes(id), false, `${item.category} 把内部 id 渲染出来了:${id}`)
  176. }
  177. for (const [label, expected] of item.expect || []) {
  178. assert.equal(fieldValue(view, label), expected, `${item.category} 的「${label}」不对`)
  179. }
  180. for (const label of item.absentLabels || []) {
  181. assert.equal(fieldValue(view, label), undefined, `${item.category} 不该有「${label}」这一行`)
  182. }
  183. }
  184. })
  185. test('业务引用:名称形如主键时只留下编码,不拼成「主键(编码)」', () => {
  186. // 现网确有 42 台设备的名称就是 32 位主键:拼进「名称(编码)」同样是把主键印上屏。
  187. const view = assetView({
  188. category: 'HAZARD',
  189. raw: {
  190. id: 'h-ref',
  191. code: 'YH-20261201134850',
  192. name: '燃气泄漏',
  193. attributes: {
  194. equipment: { code: '2607230190000022', name: HEX },
  195. network: { code: 'TCRQ202609080001', name: HEX2 },
  196. workOrder: { code: HEX },
  197. },
  198. },
  199. })
  200. assert.equal(fieldValue(view, '关联设备'), '2607230190000022')
  201. assert.equal(fieldValue(view, '关联管网'), 'TCRQ202609080001')
  202. assert.equal(fieldValue(view, '关联工单'), '未提供')
  203. for (const value of renderedTexts(view)) {
  204. assert.equal(looksLikeInternalId(value), false, `引用里混进了主键形状的值:${value}`)
  205. assert.equal(value.includes(HEX), false, '引用文案里出现了内部主键')
  206. }
  207. })
  208. test('应急资源:设备类型显示类型表的名字,设备图片走附件区,不再有 id 行', () => {
  209. const view = assetView(ASSET_CASES.find((item) => item.category === 'EMERGENCY_RESOURCE'))
  210. assert.equal(fieldValue(view, '设备类型'), '窨井盖监测终端')
  211. assert.deepEqual(view.attachments.map((item) => item.url), ['/profile/upload/2026/10/22/x.png'])
  212. // 抬头:主键形状的编码与名称都不显示,回落到「未提供」。
  213. assert.equal(view.title, '应急对象名称未提供')
  214. assert.match(view.subtitle, /编码未提供/)
  215. })
  216. test('预警详情与工单详情同样不渲主键', () => {
  217. const event = entityDetailView(normalizeEntityDetail({ kind: 'event', id: HEX, family: 'WARNING' }, {
  218. basicInfo: { warningNo: 'YJ202611241647341367', warningName: '测试预警', status: 'PROCESSING', eventTime: '2026-11-24T16:47:34.000+08:00' },
  219. alarmList: [{ alarmId: HEX2, alarmNo: 'BJ20261124001', alarmName: '压力报警', status: 'HANDLED' }],
  220. }))
  221. const order = entityDetailView(normalizeEntityDetail({ kind: 'work-order', id: '279' }, {
  222. order: { orderId: 279, orderNo: 'WO-2026120100001', orderStatus: 3, orderLevel: 2, orderDesc: '测试压力故障' },
  223. timelineEntries: [{ stateCode: 3, stateName: '处理中', operationName: '现场处置', operatedAt: '2026-12-01T09:30:00+08:00' }],
  224. }))
  225. for (const [name, view] of [['预警详情', event], ['工单详情', order]]) {
  226. const texts = renderedTexts(view)
  227. for (const value of texts) {
  228. assert.equal(looksLikeInternalId(value), false, `${name} 把主键形状的值渲染出来了:${value}`)
  229. }
  230. assert.equal(texts.includes(HEX), false, `${name} 把内部 id 渲染出来了`)
  231. assert.equal(texts.includes('279'), false, `${name} 把工单主键渲染出来了`)
  232. }
  233. })
  234. test('内部主键识别只认主键形状,不吞业务编码与普通读数', () => {
  235. for (const value of [HEX, HEX.toUpperCase(), UUID, 'risk001', 'raw_P11920']) {
  236. assert.equal(looksLikeInternalId(value), true, `${value} 应判为主键`)
  237. }
  238. // 32 位纯数字更像业务编码,不能被当成主键吞掉。
  239. for (const value of ['2607230190000022', '12345678901234567890123456789012', 'TCRQ202609080001', 'WO202612011354251210', 'YH-20261201134850', '2', 320, '二级预警', '', null, undefined]) {
  240. assert.equal(looksLikeInternalId(value), false, `${value} 不该判成主键`)
  241. }
  242. })