t52-facility-relations.mjs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import assert from 'node:assert/strict'
  2. import { planPipeEnhancement } from '../../../src/modules/cockpit/domain/pipeEnhancement.js'
  3. import { planEnhancementDevices } from '../../../src/modules/cockpit/domain/enhancementDevices.js'
  4. import { fetchDeviceFacilityRelations, relationIndex } from '../../../src/modules/cockpit/services/deviceFacilityRelations.js'
  5. const pipe = { code: 'P', networkId: 'raw_P11920', industry: 'WATER_SUPPLY', positions: [[110, 28], [110.001, 28]] }
  6. const plan = planPipeEnhancement({ selected: { kind: 'pipe', code: 'P' }, visiblePipes: [pipe] })
  7. const device = { code: 'D-1', industry: 'WATER_SUPPLY', longitude: 110.0005, latitude: 28, coordinate: { renderable: true } }
  8. const hit = planEnhancementDevices(plan, [device], {
  9. facilityRelations: new Map([['D-1', { deviceCode: 'D-1', facilityCode: 'raw_P11920', facilityKind: 'PIPE', relationType: 'GAS_PIPE_POINT_DEVICE' }]]),
  10. })
  11. assert.equal(hit.devices[0].placement, 'linked')
  12. assert.equal(hit.devices[0].placementText, '真实设施关联 · 位置示意')
  13. assert.equal(hit.devices[0].relationType, 'GAS_PIPE_POINT_DEVICE')
  14. assert.equal(hit.devices[0].pipeCode, 'P')
  15. const miss = planEnhancementDevices(plan, [device], {
  16. facilityRelations: new Map([['D-1', { deviceCode: 'D-1', facilityCode: 'not-a-pipe', facilityKind: 'MANHOLE', relationType: 'MANHOLE_DEVICE' }]]),
  17. })
  18. assert.equal(miss.devices[0].placement, 'nearby', '设施编码对不上时必须降级为邻近示意')
  19. assert.equal(miss.devices[0].placementText, '邻近设备 · 示意安装')
  20. const empty = planEnhancementDevices(plan, [device], { facilityRelations: new Map() })
  21. assert.equal(empty.devices[0].placement, 'nearby')
  22. assert.equal(empty.devices[0].relationType, '')
  23. const section = await fetchDeviceFacilityRelations({
  24. config: { dataSource: 'real', api: { baseUrl: 'http://test.invalid/api/cockpit/v1' } },
  25. fetchImpl: async () => new Response(JSON.stringify({ code: 200, data: { relations: [
  26. { deviceCode: 'D-1', facilityCode: 'raw_P11920', facilityKind: 'PIPE', relationType: 'GAS_PIPE_POINT_DEVICE' },
  27. { deviceCode: null, facilityCode: 'raw_X', facilityKind: 'MANHOLE', relationType: 'MANHOLE_DEVICE' },
  28. { deviceCode: '', facilityCode: 'raw_Y', facilityKind: 'MANHOLE', relationType: 'MANHOLE_DEVICE' },
  29. { deviceCode: 'D-2', facilityCode: '', facilityKind: 'MANHOLE', relationType: 'MANHOLE_DEVICE' },
  30. ], statistics: { skippedManholeFacilityMissing: 4, deviceCodeUnresolved: 2 } } })),
  31. })
  32. assert.equal(section.state, 'success')
  33. const index = relationIndex(section)
  34. assert.equal(index.size, 1, '只有 deviceCode 与 facilityCode 都齐全的行可回联')
  35. assert.equal(index.get('D-1').facilityCode, 'raw_P11920')
  36. const failed = await fetchDeviceFacilityRelations({
  37. config: { dataSource: 'real', api: { baseUrl: 'http://test.invalid/api/cockpit/v1' } },
  38. fetchImpl: async () => new Response(JSON.stringify({ code: 500, msg: 'boom' })),
  39. })
  40. assert.equal(relationIndex(failed).size, 0)
  41. console.log('PASS 真实设施关联优先/设施编码不匹配降级/空索引降级/只回联齐全行/取数失败降级')