#!/usr/bin/env node "use strict"; const assert = require("assert"); const fs = require("fs"); const os = require("os"); const path = require("path"); const { SCHEMA, buildNativeTrafficSimulation } = require("./lib/native-preview-traffic-simulation"); const areaDir = fs.mkdtempSync(path.join(os.tmpdir(), "native-preview-traffic-")); const nativeRoadDir = path.join(areaDir, "native-road"); const layersDir = path.join(nativeRoadDir, "layers"); fs.mkdirSync(layersDir, { recursive: true }); const roads = [ { id: "road:a", centerline: [[120, 30], [120.001, 30]], sourceNodeIds: ["a", "b"] }, { id: "road:b", centerline: [[120.001, 30], [120.001, 30.001]], sourceNodeIds: ["b", "c"] }, { id: "road:c", centerline: [[120.001, 30.001], [120, 30.001]], sourceNodeIds: ["c", "d"] }, ]; const movements = [ ["road:a", "road:b", "left"], ["road:b", "road:c", "through"], ["road:c", "road:a", "right"], ].map(([fromRoadId, toRoadId, turn], index) => ({ id: `movement:cluster:test:${index}`, connectorId: `connector:${index}`, fromRoadId, toRoadId, fromLaneId: `lane:${fromRoadId}:1`, toLaneId: `lane:${toRoadId}:1`, turn, geometryPublished: true, })); fs.writeFileSync(path.join(nativeRoadDir, "compiled.json"), JSON.stringify({ model: { roads }, movements })); fs.writeFileSync(path.join(nativeRoadDir, "traffic-signals.json"), JSON.stringify({ signals: [ { id: "osm-signal-1", phaseGroup: 0, approachId: "way:a", sourceWayId: "a", stopLongitude: 120.0009, stopLatitude: 30, faceHeadingDegrees: 270, enabled: true }, // A closer but perpendicular signal must not win over the direction match. { id: "perpendicular-signal", phaseGroup: 1, approachId: "way:wrong", sourceWayId: "wrong", stopLongitude: 120.0009, stopLatitude: 30, faceHeadingDegrees: 0, enabled: true }, ] })); fs.writeFileSync(path.join(layersDir, "connectors.geojson"), JSON.stringify({ type: "FeatureCollection", features: movements.map((movement, index) => ({ type: "Feature", properties: { movement_id: movement.id }, geometry: { type: "LineString", coordinates: [[[120.001, 30], [120.001, 30.0001]], [[120.001, 30.001], [120.0009, 30.001]], [[120, 30.001], [120, 30.0009]]][index] } })) })); fs.writeFileSync(path.join(layersDir, "vehicle_stop_lines.geojson"), JSON.stringify({ type: "FeatureCollection", features: [{ type: "Feature", properties: { road_id: "road:a" }, geometry: { type: "Polygon", coordinates: [] } }] })); fs.writeFileSync(path.join(layersDir, "lane_centerlines.geojson"), JSON.stringify({ type: "FeatureCollection", features: roads.map((road) => ({ type: "Feature", properties: { road_id: road.id, lane_index: 1 }, geometry: { type: "LineString", coordinates: road.centerline } })) })); const descriptor = buildNativeTrafficSimulation({ id: "fixture", outputs: { areaDir } }, { nativePackage: { rootDir: nativeRoadDir } }); assert.equal(descriptor.schema, SCHEMA); assert.equal(descriptor.coordinateSystem.route, "WGS84"); assert.equal(descriptor.signalPlan.cycleSeconds, 60); assert.equal(descriptor.signalPlan.initialAllRedSeconds, 5, "all movements start red while the preview initializes"); assert.deepEqual(descriptor.signalPlan.groups[0], [ { from: 0, to: 25, state: "green" }, { from: 25, to: 28, state: "yellow" }, { from: 28, to: 60, state: "red" }, ]); assert.deepEqual(descriptor.signalPlan.groups[1], [ { from: 0, to: 32, state: "red" }, { from: 32, to: 57, state: "green" }, { from: 57, to: 60, state: "yellow" }, ]); assert.ok(descriptor.routes.length >= 1); assert.ok(descriptor.routes.some((route) => route.connectors.length === 3)); assert.ok(descriptor.routes.some((route) => route.stops.some((stop) => stop.signalId === "osm-signal-1"))); assert.ok(descriptor.participants.some((participant) => participant.kind === "vehicle")); assert.ok( descriptor.participants.every((participant) => participant.stops.every((stop) => stop.signalId !== "perpendicular-signal")), "a perpendicular signal must never control a route, even when it shares the stop point", ); for (const participant of descriptor.participants.filter((entry) => entry.kind === "vehicle" || entry.kind === "motorcycle")) { const signalStop = participant.stops.find((stop) => stop.stopReason === "traffic-signal"); if (!signalStop) continue; assert.ok(Number.isFinite(signalStop.stopLineDistance), "signal-controlled stops retain their geometric stop-line distance"); assert.ok( Math.abs(signalStop.stopLineDistance - signalStop.distance - (participant.vehicleLengthMeters / 2 + 1)) < 0.02, "the model origin stops half a vehicle length plus a safety margin before the line", ); } assert.ok(descriptor.participants.some((participant) => participant.kind === "motorcycle")); assert.ok(descriptor.participants.filter((participant) => participant.kind === "vehicle").every((participant) => participant.approachLaneId), "vehicle queues need stable lane identities so adjacent lanes do not become one queue"); assert.ok(descriptor.participants.filter((participant) => participant.kind === "pedestrian").every((participant) => participant.crossingId), "crossing participants need a shared crossing identity for vehicle yield checks"); assert.ok(descriptor.participants.filter((participant) => participant.kind === "vehicle").every((participant) => Array.isArray(participant.crossingConflicts)), "vehicles must carry geometry-based crossing conflict points for turn yielding"); assert.ok(descriptor.participants.every((participant) => participant.modelName.startsWith("v2x-vehicles/"))); const previewDescriptor = buildNativeTrafficSimulation( { id: "fixture", outputs: { areaDir } }, { nativePackage: { rootDir: nativeRoadDir }, modelPrefix: "_preview/" }, ); assert.ok(previewDescriptor.participants.every((participant) => participant.modelName.startsWith("_preview/v2x-vehicles/"))); assert.ok(!descriptor.diagnostics.some((entry) => entry.reason === "no_native_cruise_route")); assert.equal(descriptor.migration.legacyDependency, false); assert.equal(descriptor.source.length, 5); fs.rmSync(areaDir, { recursive: true, force: true }); console.log("Native preview traffic tests passed.");