riskFourColorMap.test.mjs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {
  4. calculateRiskStatistics,
  5. getRiskLevel,
  6. groupRiskLabelCandidates,
  7. normalizeRiskPipeline,
  8. sortRiskLabelCandidates
  9. } from '../src/utils/riskFourColorMapModel.js'
  10. test('four-color risk thresholds preserve their exact boundary levels', () => {
  11. assert.deepEqual(
  12. [34.9, 35, 54.9, 55, 74.9, 75].map(getRiskLevel),
  13. ['low', 'general', 'general', 'larger', 'larger', 'major']
  14. )
  15. })
  16. test('risk normalization maps GeoJSON geometry and color without changing risk semantics', () => {
  17. const pipeline = normalizeRiskPipeline(
  18. { networkId: 'gas-1', riskScore: 75, riskFactors: ['腐蚀'] },
  19. {
  20. properties: { networkCode: 'G-1', networkName: '测试管段' },
  21. geometry: { type: 'MultiLineString', coordinates: [[[110, 28], [111, 29]], [[112, 30], [113, 31]]] }
  22. }
  23. )
  24. assert.equal(pipeline.riskLevel, 'major')
  25. assert.equal(pipeline.riskLevelName, '重大风险')
  26. assert.equal(pipeline.color, '#f56c6c')
  27. assert.equal(pipeline.lineWidth, 6)
  28. assert.equal(pipeline.code, 'G-1')
  29. assert.equal(pipeline.name, '测试管段')
  30. assert.deepEqual(pipeline.path, [[110, 28], [111, 29], [112, 30], [113, 31]])
  31. })
  32. test('risk statistics calculate counts, percentages, and average in one pass', () => {
  33. const statistics = calculateRiskStatistics([
  34. { riskLevel: 'major', riskScore: 80 },
  35. { riskLevel: 'larger', riskScore: 60 },
  36. { riskLevel: 'low', riskScore: 20 }
  37. ])
  38. assert.deepEqual(statistics.counts, { major: 1, larger: 1, general: 0, low: 1 })
  39. assert.deepEqual(statistics.percentages, { major: '33.3', larger: '33.3', general: '0.0', low: '33.3' })
  40. assert.equal(statistics.average, '53.3')
  41. })
  42. test('empty risk data produces zero counts and percentages', () => {
  43. const statistics = calculateRiskStatistics([])
  44. assert.deepEqual(statistics.counts, { major: 0, larger: 0, general: 0, low: 0 })
  45. assert.deepEqual(statistics.percentages, { major: '0.0', larger: '0.0', general: '0.0', low: '0.0' })
  46. assert.equal(statistics.average, '0.0')
  47. })
  48. test('map labels are limited to one per screen cell and prioritize higher risks', () => {
  49. const pipelines = [
  50. { riskLevel: 'low', name: '低风险', path: [[0, 0], [1, 1]] },
  51. { riskLevel: 'major', name: '重大风险', path: [[2, 2], [3, 3]] },
  52. { riskLevel: 'general', name: '一般风险', path: [[300, 300], [301, 301]] }
  53. ]
  54. const candidates = sortRiskLabelCandidates(pipelines)
  55. const groups = groupRiskLabelCandidates(
  56. candidates,
  57. ['major', 'general', 'low'],
  58. (_pipeline, midpoint) => ({ x: midpoint[0], y: midpoint[1] }),
  59. { x: 120, y: 44 }
  60. )
  61. assert.equal(groups.length, 2)
  62. assert.equal(groups[0].pipeline.name, '重大风险')
  63. assert.equal(groups[0].count, 2)
  64. assert.equal(groups[1].pipeline.name, '一般风险')
  65. })
  66. test('large sets of overlapping risk points produce only one label per screen cell', () => {
  67. const pipelines = Array.from({ length: 10000 }, (_, index) => ({
  68. riskLevel: 'low',
  69. name: `管段-${index}`,
  70. path: [[110, 28], [110, 28]]
  71. }))
  72. const groups = groupRiskLabelCandidates(
  73. sortRiskLabelCandidates(pipelines),
  74. ['low'],
  75. () => ({ x: 80, y: 30 }),
  76. { x: 120, y: 44 }
  77. )
  78. assert.equal(groups.length, 1)
  79. assert.equal(groups[0].count, 10000)
  80. })