| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import test from 'node:test'
- import assert from 'node:assert/strict'
- import {
- calculateRiskStatistics,
- getRiskLevel,
- groupRiskLabelCandidates,
- normalizeRiskPipeline,
- sortRiskLabelCandidates
- } from '../src/utils/riskFourColorMapModel.js'
- test('four-color risk thresholds preserve their exact boundary levels', () => {
- assert.deepEqual(
- [34.9, 35, 54.9, 55, 74.9, 75].map(getRiskLevel),
- ['low', 'general', 'general', 'larger', 'larger', 'major']
- )
- })
- test('risk normalization maps GeoJSON geometry and color without changing risk semantics', () => {
- const pipeline = normalizeRiskPipeline(
- { networkId: 'gas-1', riskScore: 75, riskFactors: ['腐蚀'] },
- {
- properties: { networkCode: 'G-1', networkName: '测试管段' },
- geometry: { type: 'MultiLineString', coordinates: [[[110, 28], [111, 29]], [[112, 30], [113, 31]]] }
- }
- )
- assert.equal(pipeline.riskLevel, 'major')
- assert.equal(pipeline.riskLevelName, '重大风险')
- assert.equal(pipeline.color, '#f56c6c')
- assert.equal(pipeline.lineWidth, 6)
- assert.equal(pipeline.code, 'G-1')
- assert.equal(pipeline.name, '测试管段')
- assert.deepEqual(pipeline.path, [[110, 28], [111, 29], [112, 30], [113, 31]])
- })
- test('risk statistics calculate counts, percentages, and average in one pass', () => {
- const statistics = calculateRiskStatistics([
- { riskLevel: 'major', riskScore: 80 },
- { riskLevel: 'larger', riskScore: 60 },
- { riskLevel: 'low', riskScore: 20 }
- ])
- assert.deepEqual(statistics.counts, { major: 1, larger: 1, general: 0, low: 1 })
- assert.deepEqual(statistics.percentages, { major: '33.3', larger: '33.3', general: '0.0', low: '33.3' })
- assert.equal(statistics.average, '53.3')
- })
- test('empty risk data produces zero counts and percentages', () => {
- const statistics = calculateRiskStatistics([])
- assert.deepEqual(statistics.counts, { major: 0, larger: 0, general: 0, low: 0 })
- assert.deepEqual(statistics.percentages, { major: '0.0', larger: '0.0', general: '0.0', low: '0.0' })
- assert.equal(statistics.average, '0.0')
- })
- test('map labels are limited to one per screen cell and prioritize higher risks', () => {
- const pipelines = [
- { riskLevel: 'low', name: '低风险', path: [[0, 0], [1, 1]] },
- { riskLevel: 'major', name: '重大风险', path: [[2, 2], [3, 3]] },
- { riskLevel: 'general', name: '一般风险', path: [[300, 300], [301, 301]] }
- ]
- const candidates = sortRiskLabelCandidates(pipelines)
- const groups = groupRiskLabelCandidates(
- candidates,
- ['major', 'general', 'low'],
- (_pipeline, midpoint) => ({ x: midpoint[0], y: midpoint[1] }),
- { x: 120, y: 44 }
- )
- assert.equal(groups.length, 2)
- assert.equal(groups[0].pipeline.name, '重大风险')
- assert.equal(groups[0].count, 2)
- assert.equal(groups[1].pipeline.name, '一般风险')
- })
- test('large sets of overlapping risk points produce only one label per screen cell', () => {
- const pipelines = Array.from({ length: 10000 }, (_, index) => ({
- riskLevel: 'low',
- name: `管段-${index}`,
- path: [[110, 28], [110, 28]]
- }))
- const groups = groupRiskLabelCandidates(
- sortRiskLabelCandidates(pipelines),
- ['low'],
- () => ({ x: 80, y: 30 }),
- { x: 120, y: 44 }
- )
- assert.equal(groups.length, 1)
- assert.equal(groups[0].count, 10000)
- })
|