Files
road-compiler/test/index.js
T
que01andClaude Opus 5 335c8ea68a feat: override which turn arrow a lane shows
The arrow on a lane came from the OSM turn:lanes tag with no way to change it,
and a lane the tag skipped got no arrow at all. lane-maneuver is a v1 override
that sets it.

v1 rather than a v2 constraint, because "which arrow" is a discrete attribute of
a lane with no station to anchor to, while every v2 anchor is positional. The
existing road override already covers OSM-derived values this way — laneCount,
width, sidewalks — and this is the same kind of thing. Arrow *placement* is the
positional half and stays out; it also still has an open question about whether
a group is a lane or a single arrow.

Three behaviours, each measured against compiled output: an override replaces
the tag's maneuver, it adds arrows to a lane the tag never marked, and '' removes
one the tag asked for. The empty string is therefore distinguished from "no
override" rather than falling through to the tag. An empty override set leaves
the arrows byte-identical and the fixtures untouched.

Validation lists the accepted maneuvers and rejects anything else, including
correctly-spelled components in the wrong order. It cannot be left to
normalizeManeuver(), which returns unrecognised input unchanged — a typo would
otherwise reach the template lookup and silently produce no arrow.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-31 18:00:53 +08:00

71 lines
3.4 KiB
JavaScript

'use strict';
const assert = require('assert/strict');
const fs = require('fs');
const path = require('path');
const compiler = require('../src');
assert.equal(typeof compiler.laneGeometry.laneCenterline, 'function');
assert.equal(typeof compiler.gaodeReference.convertGeoJson, 'function');
assert.equal(typeof compiler.turnLaneArrows.buildCustomTurnLaneArrows, 'function');
assert.equal(typeof compiler.complexJunction.buildComplexJunctionGeometry, 'function');
assert.equal(typeof compiler.nativeRoad.compileRoadModel, 'function');
assert.equal(typeof compiler.check.checkOutput, 'function');
assert.equal(typeof compiler.layerManifest.manifestForArea, 'function');
const manifest = compiler.layerManifest.manifestForArea('fixture');
assert.equal(manifest.contract, 'native-road-package/v1.1');
assert.equal(manifest.layers.length, 12);
assert.deepEqual(
manifest.layers.filter((layer) => layer.role === 'semantic').map((layer) => layer.source),
['lane_centerlines', 'connectors'],
);
assert.equal(manifest.layers.find((layer) => layer.source === 'center_lines').splitBy.cases[0].match, 'white');
const fixture = path.join(__dirname, 'fixtures', 'fengshu-er-road.osm');
assert.ok(fs.existsSync(fixture));
assert.throws(() => compiler.compiler.validateInput({ id: 'area' }), /RoadCompilerInput/);
const model = compiler.nativeRoad.compileRoadModel(fs.readFileSync(fixture, 'utf8'), {
schema: 'native-road-overrides/v1',
overrides: [],
});
assert.equal(model.roads.length, 24);
// --- lane maneuver override ---------------------------------------------------
//
// The arrow a lane shows comes from the OSM `turn:lanes` tag, so overriding it is
// a v1 override like `laneCount`, not a v2 positional constraint: "which arrow"
// has no station to anchor to.
{
const { validateOverrides } = require('../src/compile/native-road');
const laneModel = {
roads: [{ id: 'road:way/1:forward', sourceRoadId: 'way/1', segmentId: 'segment:way/1/0', laneCount: 2 }],
endpoints: [],
connections: [],
};
const wrap = (item) => ({ schema: 'native-road-overrides/v1', overrides: [item] });
const maneuver = (value, laneId = 'lane:road:way/1:forward:1') => ({
id: 'm',
kind: 'lane-maneuver',
laneId,
maneuver: value,
});
assert.doesNotThrow(() => validateOverrides(wrap(maneuver('through;left')), laneModel));
// '' is not "no override": it removes an arrow the tag asked for.
assert.doesNotThrow(() => validateOverrides(wrap(maneuver('')), laneModel));
assert.throws(() => validateOverrides(wrap(maneuver('u-turn')), laneModel), /Invalid lane maneuver/);
// normalizeManeuver() returns unknown input unchanged, so the override layer is
// the only thing standing between a typo and a silently missing arrow.
assert.throws(() => validateOverrides(wrap(maneuver('left;through')), laneModel), /Invalid lane maneuver/);
assert.throws(() => validateOverrides(wrap(maneuver(3)), laneModel), /Invalid lane maneuver/);
assert.throws(
() => validateOverrides(wrap(maneuver('left', 'lane:road:way/1:forward:9')), laneModel),
/Unknown lane maneuver override target/,
);
// An unknown target retires like any other stale override rather than throwing.
const stale = validateOverrides(wrap(maneuver('left', 'lane:road:way/9:forward:1')), laneModel, {
skipStaleTargets: true,
});
assert.equal(stale.stale.length, 1);
}
console.log('road compiler package tests passed');