Files
road-compiler/workbench/client/src/junction/structure.test.ts
T

156 lines
6.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type { GeoFeature, JunctionStructure, WorkbenchState } from '../types/state';
import { junctionDescriptors } from './hotspots';
import { localJunctionPreview, localJunctionState } from './local-state';
import {
junctionEditSelection,
junctionObjectFeatures,
junctionObjectKeyForFeature,
structureForJunction,
} from './structure';
const structure: JunctionStructure = {
schema: 'junction-structure/v1',
junctionKey: 'cluster:a',
junctionRef: { type: 'cluster', id: 'a' },
label: '路口 A',
center: [120, 30],
provenance: { nodeIds: ['1', '2'], roadIds: ['external'], segmentIds: ['external-segment'] },
capability: { editable: false, reason: 'complex-geometry-editing-not-implemented' },
shape: { kind: 't-junction', label: 'T 字', effectiveArmCount: 3, throughPairCount: 1 },
arms: ['a', 'b', 'c'].map((id) => ({ key: `arm:${id}`, label: `道路 ${id}`, directionality: 'two-way' })),
corners: [{ key: 'corner:a', label: '角部 A', fromArmKey: 'arm:a', toArmKey: 'arm:b' }],
facilities: [{ key: 'crossing:a', label: '过街 A', kind: 'crossing' }],
};
structure.facilities[0].provenance = {
facilityEvidence: 'source-crossing-node',
crossingNodeIds: ['crossing-node-a'],
};
const feature = (object: string, junction = 'cluster:a', arm?: string): GeoFeature => ({
type: 'Feature',
properties: { junction_key: junction, object_key: object, arm_key: arm },
geometry: {
type: 'Polygon',
coordinates: [
[
[120, 30],
[120.001, 30],
[120, 30.001],
[120, 30],
],
],
},
});
const road = (id: string, nodes: string[]) => ({
id,
sourceNodeIds: nodes,
segmentId: `${id}-segment`,
centerline: [],
osmWayIds: [],
tags: {},
highway: 'residential',
direction: 'forward',
widthMeters: 7,
laneCount: 2,
sidewalkLeft: false,
sidewalkRight: false,
});
const state: WorkbenchState = {
areaId: 'test',
compiled: {
model: { roads: [road('external', ['1', '3']), road('internal', ['1', '2'])], endpoints: [], connections: [] },
diagnostics: [],
},
overrides: { schema: 'native-road-overrides/v1', overrides: [] },
trafficSignals: { provenance: 'empty', assemblies: { type: 'FeatureCollection', features: [] } },
trafficRuntime: { signals: [] },
comparison: {},
junctionStructures: { 'cluster:a': structure },
junctionStructure: structure,
layers: {
nativeRoadSurface: {
type: 'FeatureCollection',
features: [feature('carriageway:a', 'cluster:a', 'arm:a'), feature('carriageway:b', 'cluster:b', 'arm:a')],
},
crosswalks: {
type: 'FeatureCollection',
features: [feature('crossing:a'), feature('crossing:a'), feature('crossing:b', 'cluster:b')],
},
nativeIntersectionSurface: { type: 'FeatureCollection', features: [feature('corner:a')] },
},
};
describe('authoritative junction structures', () => {
it('uses typed scopes without leaking the legacy first structure', () => {
expect(junctionEditSelection({ type: 'cluster', id: 'a' })).toEqual({ cluster: 'a' });
expect(junctionEditSelection({ type: 'node', id: 'a' })).toEqual({ junction: 'a' });
expect(structureForJunction(state, { type: 'cluster', id: 'a' })).toBe(structure);
expect(structureForJunction(state, { type: 'node', id: 'a' })).toBeNull();
expect(structureForJunction(state, { type: 'cluster', id: 'missing' })).toBeNull();
});
it('makes map and list select the same arm, corner and crossing group', () => {
expect(junctionObjectKeyForFeature(structure, feature('carriageway:a', 'cluster:a', 'arm:a').properties!)).toBe(
'arm:a',
);
expect(junctionObjectKeyForFeature(structure, feature('corner:a').properties!)).toBe('corner:a');
expect(junctionObjectKeyForFeature(structure, feature('crossing:a').properties!)).toBe('crossing:a');
expect(junctionObjectKeyForFeature(structure, feature('crossing:a', 'cluster:b').properties!)).toBeNull();
expect(junctionObjectFeatures(state, structure, 'arm:a')).toHaveLength(1);
expect(junctionObjectFeatures(state, structure, 'crossing:a')).toHaveLength(2);
expect(junctionObjectFeatures(state, structure, 'corner:a')).toHaveLength(1);
expect(junctionObjectFeatures(state, structure, 'deleted')).toEqual([]);
});
it('keeps facility evidence available to scoped structure consumers', () => {
expect(structure.facilities[0].provenance).toEqual({
facilityEvidence: 'source-crossing-node',
crossingNodeIds: ['crossing-node-a'],
});
});
it('scopes the canvas and preview using cluster ownership, retaining core features without node IDs', () => {
const descriptor = junctionDescriptors(state).find((item) => item.ref.type === 'cluster')!;
expect(descriptor.status).toBe('readonly');
expect(descriptor.approachRoadIds).toEqual(['external']);
const local = localJunctionState(state, descriptor);
expect(local.compiled.model.roads.map((item) => item.id)).toEqual(['external']);
expect(local.junctionStructure).toBe(structure);
expect(local.layers.nativeRoadSurface?.features).toHaveLength(1);
expect(local.layers.nativeIntersectionSurface?.features).toHaveLength(1);
const preview = localJunctionPreview(
{ roadSurface: state.layers.nativeRoadSurface },
descriptor,
state.compiled.model.roads,
);
expect(preview?.roadSurface?.features).toHaveLength(1);
});
it('resolves separately persisted signals through scoped provenance without parsing their IDs', () => {
const signal = { ...feature('unused'), properties: { signal_uid: 'raw:signal:uid' } };
const withSignal = {
...structure,
facilities: [
...structure.facilities,
{
key: 'cluster:a:signal:raw',
kind: 'signal',
label: '信号灯',
geometryRef: 'raw:signal:uid',
provenance: { featureIds: ['raw:signal:uid'] },
},
],
};
const signalState = {
...state,
trafficSignals: {
...state.trafficSignals,
assemblies: { ...state.trafficSignals.assemblies, features: [signal] },
},
};
expect(junctionObjectKeyForFeature(withSignal, signal.properties)).toBe('cluster:a:signal:raw');
expect(junctionObjectFeatures(signalState, withSignal, 'cluster:a:signal:raw')).toEqual([signal]);
expect(junctionObjectKeyForFeature(structure, signal.properties)).toBeNull();
});
});