'use strict'; const assert = require('assert/strict'); const { buildJunctionStructure } = require('../src/compile/junction-structure'); const { CN_URBAN_DEFAULT_PROFILE, cloneProfile, evaluateRange, validateStandardsProfile, } = require('../src/compile/junction-standards'); const arm = (key, label, directionality = 'two-way') => ({ key, label, heading: key === 'south' ? 180 : key === 'east' ? 90 : key === 'west' ? 270 : 0, directionality, carriageway: { laneCount: directionality === 'one-way' ? 1 : 2, effectiveWidthMeters: directionality === 'one-way' ? 3.5 : 7, }, geometryRef: `road-surface:${key}`, }); const cross = buildJunctionStructure({ junctionKey: 'junction:fixture-cross', label: '科技园东路与科技园一路', shape: { kind: 'cross', label: '十字路口', effectiveArmCount: 4, throughPairCount: 2 }, arms: [arm('north', '科技园一路'), arm('east', '科技园东路'), arm('south', '科技园一路'), arm('west', '科技园东路')], corners: [ { key: 'corner:north-east', fromArmKey: 'north', toArmKey: 'east', radiusMeters: 12 }, { key: 'corner:east-south', fromArmKey: 'east', toArmKey: 'south', radiusMeters: 12 }, ], facilities: [ { key: 'crossing:north', kind: 'crossing', label: '北侧人行横道', armKey: 'north' }, { key: 'stop-line:north', kind: 'stop-line', label: '北侧停止线', armKey: 'north', relatedFacilityKey: 'crossing:north', }, ], }); assert.equal(cross.schema, 'junction-structure/v1'); assert.equal(cross.shape.kind, 'cross'); assert.equal(cross.shape.effectiveArmCount, 4); assert.equal(cross.arms[0].label, '科技园一路'); assert.equal(cross.facilities[1].relatedFacilityKey, 'crossing:north'); assert.equal(cross.arms[0].overrideState.mode, 'global'); const localArm = buildJunctionStructure({ junctionKey: 'junction:t', shape: { kind: 't-junction', label: 'T 字路口', effectiveArmCount: 3, throughPairCount: 1 }, arms: [arm('north', '道路 1'), arm('east', '道路 2'), arm('west', '道路 3', 'one-way')], }); assert.equal(localArm.shape.kind, 't-junction'); assert.equal(localArm.arms.length, 3); assert.equal(localArm.arms[2].directionality, 'one-way'); assert.equal(localArm.arms[2].carriageway.laneCount, 1); for (const [kind, label, headings] of [ ['offset-cross', '偏置十字路口', ['north', 'east', 'south', 'west']], ['multi-arm', '5 臂非规则路口', ['north', 'east', 'south', 'west', 'north-east']], ]) { const structure = buildJunctionStructure({ junctionKey: `junction:${kind}`, shape: { kind, label, effectiveArmCount: headings.length, throughPairCount: kind === 'offset-cross' ? 2 : 1 }, arms: headings.map((heading, index) => arm(`${heading}-${index}`, `道路 ${index + 1}`)), }); assert.equal(structure.shape.kind, kind); assert.equal(structure.shape.effectiveArmCount, headings.length); } assert.throws( () => buildJunctionStructure({ shape: { kind: 't-junction', effectiveArmCount: 4 }, arms: [arm('north', '道路 1')] }), /effective arm count must match/, ); assert.throws( () => buildJunctionStructure({ shape: { kind: 'cross', effectiveArmCount: 1 }, arms: [arm('north', '道路 1'), arm('north', '道路 2')], }), /Duplicate junction road arm key/, ); assert.throws( () => buildJunctionStructure({ shape: { kind: 't-junction', effectiveArmCount: 1 }, arms: [arm('north', '道路 1')], corners: [{ fromArmKey: 'north', toArmKey: 'missing' }], }), /unknown road arm/, ); validateStandardsProfile(CN_URBAN_DEFAULT_PROFILE); assert.equal(CN_URBAN_DEFAULT_PROFILE.id, 'cn-urban-default'); assert.ok(CN_URBAN_DEFAULT_PROFILE.sources.every((source) => source.title && source.edition && source.clause)); assert.throws(() => (CN_URBAN_DEFAULT_PROFILE.rules[0].recommended.min = 99), /read only/); assert.deepEqual( CN_URBAN_DEFAULT_PROFILE.rules.map((rule) => rule.id), [ 'effective-carriageway-width', 'crossing-covers-carriageway', 'stop-line-crossing-clearance', 'corner-radius-preserves-pedestrian-space', 'pedestrian-continuity', 'junction-polygon-validity', ], ); assert.equal(evaluateRange(7, CN_URBAN_DEFAULT_PROFILE.rules[0]), 'pass'); assert.equal(evaluateRange(5, CN_URBAN_DEFAULT_PROFILE.rules[0]), 'warning'); assert.equal(evaluateRange(-1, CN_URBAN_DEFAULT_PROFILE.rules[0]), 'blocking'); const copy = cloneProfile(CN_URBAN_DEFAULT_PROFILE); copy.rules[0].recommended.min = 99; assert.equal(CN_URBAN_DEFAULT_PROFILE.rules[0].recommended.min, 6); assert.throws( () => validateStandardsProfile({ ...copy, rules: [{ ...copy.rules[0] }, { ...copy.rules[0] }] }), /Duplicate/, ); console.log('junction structure and standards tests passed');