mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-21 02:32:18 +00:00
Merge branch 'main' into feat/blender-camera-interchange-submit
This commit is contained in:
commit
68958bf34b
3290 files changed
+460820
-51773
No files matched your search
@@ -9,7 +9,7 @@ const source = readFileSync(new URL('../../index.html', import.meta.url), 'utf8'
|
||||
test('the standalone document hosts every view once and keeps element ids unique', async () => {
|
||||
const hosts = [...source.matchAll(/data-gev-view="([^"]+)"/g)].map((match) => match[1]);
|
||||
// Order is stacking order for fixed elements that share a z-index.
|
||||
assert.deepEqual(hosts, ['scene-chrome', 'cockpit', 'display-controls', 'command-dock', 'layer-panels', 'context', 'welcome', 'provider-settings', 'intel-hud']);
|
||||
assert.deepEqual(hosts, ['scene-chrome', 'cockpit', 'display-controls', 'command-dock', 'layer-panels', 'context', 'welcome', 'provider-settings', 'intel-hud', 'stories']);
|
||||
assert.deepEqual([...hosts].sort(), Object.keys(APPLICATION_VIEWS).sort());
|
||||
const html = await readApplicationHtml();
|
||||
const ids = [...html.matchAll(/\sid="([^"]+)"/g)].map((match) => match[1]);
|
||||
|
||||
@@ -0,0 +1,682 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { crc32, deflateRawSync } from 'node:zlib';
|
||||
import {
|
||||
buildPack,
|
||||
buildSchedule,
|
||||
entryStream,
|
||||
fillTimes,
|
||||
finishShape,
|
||||
frequencyDepartures,
|
||||
isRailRouteType,
|
||||
openZip,
|
||||
parseGtfsTime,
|
||||
parseRecord,
|
||||
projectStops,
|
||||
readCsv,
|
||||
resolveServiceDays,
|
||||
simplifyFlat,
|
||||
} from '../../scripts/build-rail-paths.mjs';
|
||||
import { createRailPath } from '../data/railPaths.js';
|
||||
import { createSchedule, tripStops } from '../data/railSchedule.js';
|
||||
|
||||
/** A minimal zip writer: stored or deflated entries, no zip64. */
|
||||
function makeZip(files) {
|
||||
const locals = [];
|
||||
const centrals = [];
|
||||
let offset = 0;
|
||||
for (const { name, text, deflate } of files) {
|
||||
const data = Buffer.from(text, 'utf8');
|
||||
const body = deflate ? deflateRawSync(data) : data;
|
||||
const nameBytes = Buffer.from(name, 'utf8');
|
||||
const local = Buffer.alloc(30);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt16LE(deflate ? 8 : 0, 8);
|
||||
local.writeUInt32LE(crc32(data), 14);
|
||||
local.writeUInt32LE(body.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(nameBytes.length, 26);
|
||||
const central = Buffer.alloc(46);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt16LE(deflate ? 8 : 0, 10);
|
||||
central.writeUInt32LE(crc32(data), 16);
|
||||
central.writeUInt32LE(body.length, 20);
|
||||
central.writeUInt32LE(data.length, 24);
|
||||
central.writeUInt16LE(nameBytes.length, 28);
|
||||
central.writeUInt32LE(offset, 42);
|
||||
locals.push(local, nameBytes, body);
|
||||
centrals.push(central, nameBytes);
|
||||
offset += local.length + nameBytes.length + body.length;
|
||||
}
|
||||
const cd = Buffer.concat(centrals);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(files.length, 8);
|
||||
end.writeUInt16LE(files.length, 10);
|
||||
end.writeUInt32LE(cd.length, 12);
|
||||
end.writeUInt32LE(offset, 16);
|
||||
return Buffer.concat([...locals, cd, end]);
|
||||
}
|
||||
|
||||
const D = 1 / 111_320;
|
||||
|
||||
/** Straight east along the equator in `step`-metre steps, as shapes.txt rows. */
|
||||
function shapeRows(id, steps, step) {
|
||||
const rows = [];
|
||||
for (let i = 0; i <= steps; i++)
|
||||
rows.push(`${id},0,${(i * step * D).toFixed(6)},${i * 10}`);
|
||||
// Out of order on purpose: the build sorts by sequence.
|
||||
return rows.reverse();
|
||||
}
|
||||
|
||||
async function withZip(t, files) {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-build-rail-paths-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const file = path.join(dir, 'feed.zip');
|
||||
await writeFile(file, makeZip(files));
|
||||
const zip = await openZip(file);
|
||||
t.after(() => zip.handle.close());
|
||||
return zip;
|
||||
}
|
||||
|
||||
test('rail route types are the tram, metro and railway families', () => {
|
||||
for (const type of [
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
' 2 ',
|
||||
'5',
|
||||
'7',
|
||||
'12',
|
||||
'100',
|
||||
'109',
|
||||
'401',
|
||||
'900',
|
||||
// Extended funicular, as basic 7 is.
|
||||
'1400',
|
||||
])
|
||||
assert.ok(isRailRouteType(type), type);
|
||||
for (const type of ['3', '4', '6', '11', '200', '700', '1000', '', 'x'])
|
||||
assert.equal(isRailRouteType(type), false, type);
|
||||
});
|
||||
|
||||
test('CSV records honour quotes and trim padded fields', () => {
|
||||
assert.deepEqual(parseRecord('a, b ,c\r'), ['a', 'b', 'c']);
|
||||
assert.deepEqual(parseRecord('1,"x, ""y""",z'), ['1', 'x, "y"', 'z']);
|
||||
});
|
||||
|
||||
test('the zip reader streams stored and deflated entries', async (t) => {
|
||||
const zip = await withZip(t, [
|
||||
{ name: 'stored.txt', text: 'h\n1\n' },
|
||||
{
|
||||
name: 'gtfs/routes.txt',
|
||||
text: 'route_id,route_long_name\nR1,"A, B\nC"\nR2,D\n',
|
||||
deflate: true,
|
||||
},
|
||||
]);
|
||||
assert.deepEqual([...zip.entries.keys()].sort(), [
|
||||
'gtfs/routes.txt',
|
||||
'stored.txt',
|
||||
]);
|
||||
const rows = [];
|
||||
const count = await readCsv(
|
||||
await entryStream(zip, zip.entries.get('gtfs/routes.txt')),
|
||||
(row) => rows.push(row),
|
||||
);
|
||||
assert.equal(count, 2);
|
||||
assert.deepEqual(rows, [
|
||||
{ route_id: 'R1', route_long_name: 'A, B\nC' },
|
||||
{ route_id: 'R2', route_long_name: 'D' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('a shape is sorted, rounded, simplified and measured; a chord is dropped', () => {
|
||||
const triples = [2, 0, 2000 * D, 0, 0, 0, 1, 0, 1000 * D, 1, 0, 1000 * D];
|
||||
const shape = finishShape(triples);
|
||||
assert.deepEqual(
|
||||
shape.flat.length,
|
||||
4,
|
||||
'collinear middle point simplified away',
|
||||
);
|
||||
assert.equal(shape.rawPoints, 3, 'duplicate point removed');
|
||||
// Haversine metres, a little under the 111,320 m/degree used to place it.
|
||||
assert.ok(Math.abs(shape.dist[1] - 2000) < 5, String(shape.dist[1]));
|
||||
// Sparse vertices on straight track (MBTA commuter lines run 2-3.3 km
|
||||
// between vertices) are track, not a chord.
|
||||
assert.equal(finishShape([0, 0, 0, 1, 0, 3000 * D]).dropped, undefined);
|
||||
const chord = finishShape([0, 0, 0, 1, 0, 6000 * D]);
|
||||
assert.match(chord.dropped, /^vertex gap 59\d\d m$/);
|
||||
assert.match(finishShape([0, 0, 0]).dropped, /fewer than two points/);
|
||||
});
|
||||
|
||||
test('simplification keeps every corner beyond the tolerance', () => {
|
||||
const flat = [
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1000 * D,
|
||||
0.5 * D,
|
||||
1500 * D,
|
||||
0,
|
||||
2000 * D,
|
||||
500 * D,
|
||||
2000 * D,
|
||||
];
|
||||
const simple = simplifyFlat(flat, 1);
|
||||
assert.deepEqual(simple, [0, 0, 0, 2000 * D, 500 * D, 2000 * D]);
|
||||
});
|
||||
|
||||
test('a pack keeps classified rail routes, dedupes shapes and maps trips', async (t) => {
|
||||
const zip = await withZip(t, [
|
||||
{
|
||||
name: 'routes.txt',
|
||||
text: 'route_id, route_type, route_long_name\nR1, 2, "Main, line"\nR2,2,Other\nB1,3,Bus\n',
|
||||
deflate: true,
|
||||
},
|
||||
{
|
||||
name: 'trips.txt',
|
||||
text: [
|
||||
'route_id,service_id,trip_id,shape_id',
|
||||
'R1,wk,t1,A',
|
||||
'R1,wk,t2,A2',
|
||||
'R1,wk,t3,CHORD',
|
||||
'R1,wk,t4,',
|
||||
'R2,wk,t5,A',
|
||||
'B1,wk,t6,BUS',
|
||||
].join('\n'),
|
||||
deflate: true,
|
||||
},
|
||||
{
|
||||
name: 'shapes.txt',
|
||||
text: [
|
||||
'shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence',
|
||||
...shapeRows('A', 20, 100),
|
||||
...shapeRows('A2', 20, 100),
|
||||
'CHORD,0,0,1',
|
||||
`CHORD,0,${(8000 * D).toFixed(6)},2`,
|
||||
...shapeRows('BUS', 5, 100),
|
||||
].join('\n'),
|
||||
deflate: true,
|
||||
},
|
||||
]);
|
||||
const feed = {
|
||||
id: 'test-feed',
|
||||
classify: (routeId) =>
|
||||
routeId === 'R1' || routeId === 'B1'
|
||||
? { category: 'local', mode: 'rail' }
|
||||
: null,
|
||||
};
|
||||
const { pack, stats } = await buildPack(feed, zip, {
|
||||
url: 'https://example.test/gtfs.zip',
|
||||
lastModified: null,
|
||||
});
|
||||
assert.equal(pack.version, 1);
|
||||
assert.equal(pack.feedId, 'test-feed');
|
||||
assert.equal(pack.shapes.length, 1, 'identical shapes stored once');
|
||||
assert.deepEqual(pack.shapes[0], [0, 0, 0, 0.01797]);
|
||||
assert.equal(pack.dist[0].length, 2);
|
||||
assert.deepEqual({ ...pack.trips }, { t1: 0, t2: 0 });
|
||||
assert.deepEqual({ ...pack.routes }, { R1: [0] });
|
||||
assert.equal(
|
||||
stats.railRoutes,
|
||||
1,
|
||||
'the bus route and the unclassified route are out',
|
||||
);
|
||||
assert.equal(stats.shapesDropped, 1);
|
||||
assert.equal(stats.shapelessTrips, 1);
|
||||
assert.equal(stats.tripsMapped, 2);
|
||||
assert.equal(JSON.parse(JSON.stringify(pack)).trips.t1, 0);
|
||||
});
|
||||
|
||||
test('GTFS times parse past midnight; blanks and junk are NaN', () => {
|
||||
assert.equal(parseGtfsTime('25:30:00'), 91_800);
|
||||
assert.equal(parseGtfsTime(' 7:05:09 '), 25_509);
|
||||
assert.ok(Number.isNaN(parseGtfsTime('')));
|
||||
assert.ok(Number.isNaN(parseGtfsTime('7:5:00')));
|
||||
});
|
||||
|
||||
test('calendars resolve to concrete service days with exceptions', () => {
|
||||
const day = (text) => Date.parse(`${text}T00:00:00Z`) / 86_400_000;
|
||||
const days = resolveServiceDays(
|
||||
[
|
||||
{
|
||||
service_id: 'WK',
|
||||
monday: '1',
|
||||
tuesday: '1',
|
||||
wednesday: '1',
|
||||
thursday: '1',
|
||||
friday: '1',
|
||||
saturday: '0',
|
||||
sunday: '0',
|
||||
start_date: '20260101',
|
||||
end_date: '20261105',
|
||||
},
|
||||
],
|
||||
[
|
||||
{ service_id: 'WK', date: '20261104', exception_type: '2' },
|
||||
{ service_id: 'X', date: '20261108', exception_type: '1' },
|
||||
{ service_id: 'Y', date: '20261108', exception_type: '2' },
|
||||
],
|
||||
day('2026-11-02'),
|
||||
day('2026-11-08'),
|
||||
);
|
||||
assert.deepEqual(days.get('WK'), [
|
||||
day('2026-11-02'),
|
||||
day('2026-11-03'),
|
||||
day('2026-11-05'),
|
||||
]);
|
||||
assert.deepEqual(days.get('X'), [day('2026-11-08')]);
|
||||
assert.equal(days.has('Y'), false);
|
||||
});
|
||||
|
||||
test('blank stop times are filled by distance between timed stops', () => {
|
||||
const arr = [0, NaN, NaN, 300, 400];
|
||||
const dep = [0, NaN, NaN, NaN, 400];
|
||||
assert.equal(fillTimes(arr, dep, [0, 100, 200, 300, 350]), true);
|
||||
assert.deepEqual(arr, [0, 100, 200, 300, 400]);
|
||||
assert.deepEqual(dep, [0, 100, 200, 300, 400]);
|
||||
assert.equal(fillTimes([NaN, 5], [NaN, 5], [0, 1]), false);
|
||||
});
|
||||
|
||||
test('stops project onto a shape in order, binding a twice-passed stop to the first pass', () => {
|
||||
// Out along the equator and back 20 m north of it.
|
||||
const path = createRailPath([0, 0, 0, 2000 * D, 20 * D, 2000 * D, 20 * D, 0]);
|
||||
const { dist, maxOffsetM } = projectStops(path, [
|
||||
{ lat: 0, lon: 0 },
|
||||
// Nearer the return line (5 m) than the outbound (15 m).
|
||||
{ lat: 15 * D, lon: 1000 * D },
|
||||
{ lat: 20 * D, lon: 0 },
|
||||
]);
|
||||
assert.ok(Math.abs(dist[1] - 1000) < 2, String(dist[1]));
|
||||
assert.ok(Math.abs(dist[2] - 4020) < 2, String(dist[2]));
|
||||
assert.ok(maxOffsetM < 16);
|
||||
// Never backwards, even when a stop lies behind the previous one.
|
||||
const back = projectStops(path, [
|
||||
{ lat: 0, lon: 1500 * D },
|
||||
{ lat: 0, lon: 500 * D },
|
||||
]);
|
||||
assert.ok(back.dist[1] >= back.dist[0]);
|
||||
});
|
||||
|
||||
test('a stop offset is in great-circle metres, not the path projection', () => {
|
||||
// North along the meridian; the flat projection is true only at 30°N.
|
||||
const path = createRailPath([0, 0, 30, 0, 60, 0]);
|
||||
const eastAt60 =
|
||||
(500 / (6_371_008.8 * Math.cos(Math.PI / 3))) * (180 / Math.PI);
|
||||
const { maxOffsetM } = projectStops(path, [
|
||||
{ lat: 0, lon: 0 },
|
||||
{ lat: 60, lon: eastAt60 },
|
||||
]);
|
||||
// The projection alone reads 866 m (500 m × cos 30° / cos 60°).
|
||||
assert.ok(Math.abs(maxOffsetM - 500) < 5, String(maxOffsetM));
|
||||
});
|
||||
|
||||
const east = (m) => (m * D).toFixed(6);
|
||||
|
||||
function scheduleZip(t) {
|
||||
return withZip(t, [
|
||||
{
|
||||
name: 'agency.txt',
|
||||
text: 'agency_id,agency_name,agency_url,agency_timezone\nA,Test,https://example.test,America/New_York\n',
|
||||
},
|
||||
{
|
||||
name: 'calendar.txt',
|
||||
text: [
|
||||
'service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date',
|
||||
'WK,1,1,1,1,1,0,0,20260101,20261231',
|
||||
'WE,0,0,0,0,0,1,1,20260101,20261231',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'calendar_dates.txt',
|
||||
text: 'service_id,date,exception_type\nWK,20261106,2\nHOL,20261107,1\n',
|
||||
},
|
||||
{
|
||||
name: 'routes.txt',
|
||||
text: 'route_id,route_type,route_color\nR1,2,ff0000\nB1,3,\n',
|
||||
},
|
||||
{
|
||||
name: 'trips.txt',
|
||||
text: [
|
||||
'route_id,service_id,trip_id,shape_id,trip_headsign,trip_short_name,direction_id',
|
||||
'R1,WK,t1,S,East,101,0',
|
||||
'R1,WK,t2,S,East,103,0',
|
||||
'R1,WE,t3,S,East,105,0',
|
||||
'R1,HOL,t4,,East,107,1',
|
||||
'R1,OLD,t5,S,East,109,0',
|
||||
'B1,WK,b1,S,,,',
|
||||
].join('\n'),
|
||||
deflate: true,
|
||||
},
|
||||
{
|
||||
name: 'stops.txt',
|
||||
text: [
|
||||
'stop_id,stop_name,stop_lat,stop_lon,parent_station,stop_timezone,platform_code',
|
||||
'P,Parent Station,0,0.00001,,,',
|
||||
// A hair east of (0, 0), which the build reads as no coordinates.
|
||||
'A1,Alpha 1,0,0.000001,P,,1',
|
||||
'Bst,Bravo,0,' + east(1000) + ',,,',
|
||||
'C,Charlie,0,' + east(3000) + ',,America/Chicago,',
|
||||
'X,Unused,1,1,,,',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'stop_times.txt',
|
||||
text: [
|
||||
'trip_id,arrival_time,departure_time,stop_id,stop_sequence,pickup_type,drop_off_type',
|
||||
't1,08:30:00,08:30:00,C,9,,',
|
||||
't1,08:00:00,08:00:00,A1,1,,',
|
||||
't1,,,Bst,5,,',
|
||||
't2,09:00:00,09:00:00,A1,1,,',
|
||||
't2,,,Bst,5,,',
|
||||
't2,09:30:00,09:30:00,C,9,,',
|
||||
't3,23:50:00,23:50:00,A1,1,,',
|
||||
't3,,,Bst,5,,',
|
||||
't3,24:20:00,24:20:00,C,9,,',
|
||||
't4,10:00:00,10:00:00,A1,1,,',
|
||||
't4,10:30:00,10:30:00,C,2,1,1',
|
||||
't5,07:00:00,07:00:00,A1,1,,',
|
||||
't5,07:30:00,07:30:00,C,9,,',
|
||||
'b1,07:00:00,07:00:00,A1,1,,',
|
||||
'b1,07:30:00,07:30:00,X,2,,',
|
||||
].join('\n'),
|
||||
deflate: true,
|
||||
},
|
||||
{
|
||||
name: 'shapes.txt',
|
||||
text: [
|
||||
'shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence',
|
||||
...shapeRows('S', 40, 100),
|
||||
].join('\n'),
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
const railFeed = (extra = {}) => ({
|
||||
id: 'test-feed',
|
||||
classify: () => ({ category: 'local', mode: 'rail' }),
|
||||
...extra,
|
||||
});
|
||||
|
||||
test('a schedule pack resolves services, shares patterns and run times, and keeps served stops', async (t) => {
|
||||
const zip = await scheduleZip(t);
|
||||
const feed = railFeed();
|
||||
const { pack: paths } = await buildPack(feed, zip, { url: 'x' });
|
||||
const { schedule, stats } = await buildSchedule(
|
||||
feed,
|
||||
zip,
|
||||
{ url: 'x' },
|
||||
paths,
|
||||
{ from: '2026-11-05', days: 4 },
|
||||
);
|
||||
assert.equal(schedule.version, 1);
|
||||
assert.equal(schedule.timezone, 'America/New_York');
|
||||
assert.deepEqual(schedule.window, { from: '2026-11-05', to: '2026-11-08' });
|
||||
assert.equal(schedule.pathsBuiltAt, paths.builtAt);
|
||||
assert.deepEqual(Object.keys(schedule.trips).sort(), [
|
||||
't1',
|
||||
't2',
|
||||
't3',
|
||||
't4',
|
||||
]);
|
||||
const { t1, t2, t3, t4 } = schedule.trips;
|
||||
assert.equal(t1.pattern, t2.pattern);
|
||||
assert.equal(t1.pattern, t3.pattern);
|
||||
assert.notEqual(t1.pattern, t4.pattern);
|
||||
assert.equal(t1.profile, t2.profile, 'equal run times share one array');
|
||||
assert.equal(t1.profile, t3.profile);
|
||||
assert.deepEqual(schedule.profiles[t1.profile], [0, 0, 600, 600, 1800, 1800]);
|
||||
assert.equal(t1.startSec, 8 * 3600);
|
||||
assert.equal(t3.startSec, 23 * 3600 + 50 * 60);
|
||||
assert.deepEqual(
|
||||
{
|
||||
name: t1.name,
|
||||
headsign: t1.headsign,
|
||||
direction: t1.direction,
|
||||
route: t1.route,
|
||||
},
|
||||
{ name: '101', headsign: 'East', direction: 0, route: 'R1' },
|
||||
);
|
||||
// t3 ends past midnight, so the pack looks one day back.
|
||||
assert.equal(stats.lookbackDays, 1);
|
||||
assert.deepEqual(
|
||||
{ ...schedule.services },
|
||||
{
|
||||
WK: ['2026-11-04', '2026-11-05'],
|
||||
WE: ['2026-11-07', '2026-11-08'],
|
||||
HOL: ['2026-11-07'],
|
||||
},
|
||||
);
|
||||
assert.deepEqual(
|
||||
{ ...schedule.routes },
|
||||
{ R1: { type: 2, color: 'FF0000' } },
|
||||
);
|
||||
const ids = schedule.stops.map((stop) => stop.id);
|
||||
assert.deepEqual(ids.sort(), ['A1', 'Bst', 'C', 'P']);
|
||||
const byId = Object.fromEntries(
|
||||
schedule.stops.map((stop) => [stop.id, stop]),
|
||||
);
|
||||
assert.equal(schedule.stops[byId.A1.parent].id, 'P');
|
||||
assert.equal(byId.A1.platform, '1');
|
||||
assert.equal(byId.C.tz, 'America/Chicago');
|
||||
assert.equal(byId.Bst.tz, undefined);
|
||||
const shaped = schedule.patterns[t1.pattern];
|
||||
assert.equal(shaped.shape, 0);
|
||||
assert.deepEqual(shaped.seq, [1, 5, 9]);
|
||||
assert.ok(Math.abs(shaped.dist[1] - 1000) < 5, String(shaped.dist[1]));
|
||||
assert.ok(Math.abs(shaped.dist[2] - 3000) < 5, String(shaped.dist[2]));
|
||||
const bare = schedule.patterns[t4.pattern];
|
||||
assert.equal(
|
||||
bare.shape,
|
||||
null,
|
||||
'a position feed draws a shapeless trip between stops',
|
||||
);
|
||||
assert.deepEqual(bare.pass, [0, 1]);
|
||||
assert.ok(Math.abs(bare.dist[1] - 3000) < 5);
|
||||
assert.equal(stats.patternsShapeless, 1);
|
||||
|
||||
// The browser module reads it back: the 24:20:00 arrival lands in Chicago
|
||||
// time on the next day.
|
||||
const live = createSchedule(JSON.parse(JSON.stringify(schedule)));
|
||||
const stops = tripStops(
|
||||
live,
|
||||
{ tripId: 't3', serviceDate: '2026-11-07' },
|
||||
Date.parse('2026-11-08T04:55:00Z'),
|
||||
);
|
||||
assert.deepEqual(
|
||||
stops.map((stop) => [stop.stopId, stop.scheduledArrival, stop.state]),
|
||||
[
|
||||
['A1', '23:50', 'passed'],
|
||||
['Bst', '00:00', 'next'],
|
||||
['C', '23:20', 'upcoming'],
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
test('no line is invented where the GTFS has no shape', async (t) => {
|
||||
const zip = await scheduleZip(t);
|
||||
{
|
||||
const feed = railFeed();
|
||||
const { pack: paths } = await buildPack(feed, zip, { url: 'x' });
|
||||
assert.equal(paths.shapes.length, 1);
|
||||
assert.deepEqual(paths.shapeSources, ['operator']);
|
||||
const { schedule, stats } = await buildSchedule(
|
||||
feed,
|
||||
zip,
|
||||
{ url: 'x' },
|
||||
paths,
|
||||
{ from: '2026-11-05' },
|
||||
);
|
||||
const pattern = schedule.patterns[schedule.trips.t4.pattern];
|
||||
assert.equal(pattern.shape, null, 'the shapeless trip gets no shape');
|
||||
// Great-circle metres between the two stops, a little under 3 km.
|
||||
assert.equal(pattern.dist.length, 2);
|
||||
assert.ok(Math.abs(pattern.dist[1] - 3000) < 5, String(pattern.dist[1]));
|
||||
|
||||
assert.equal(stats.patternsShapeless, 1);
|
||||
assert.equal(paths.shapes.length, 1, 'the path pack is left as built');
|
||||
}
|
||||
const chord = [
|
||||
['shape_id', 'shape_pt_lat', 'shape_pt_lon', 'shape_pt_sequence'].join(),
|
||||
`S,0,0,1`,
|
||||
`S,0,${(6000 * D).toFixed(6)},2`,
|
||||
].join('\n');
|
||||
const sparse = await withZip(t, [
|
||||
{ name: 'routes.txt', text: 'route_id,route_type\nR1,2\n' },
|
||||
{
|
||||
name: 'trips.txt',
|
||||
text: 'route_id,service_id,trip_id,shape_id\nR1,WK,t1,S\n',
|
||||
},
|
||||
{ name: 'shapes.txt', text: chord },
|
||||
]);
|
||||
const { pack, stats } = await buildPack(
|
||||
railFeed({ positions: 'schedule' }),
|
||||
sparse,
|
||||
{ url: 'x' },
|
||||
);
|
||||
assert.equal(pack.shapes.length, 0, 'a chord is dropped on every feed');
|
||||
assert.equal(stats.shapesDropped, 1);
|
||||
});
|
||||
|
||||
test('frequency windows give one departure per headway before each end', () => {
|
||||
const h = (hours, minutes = 0) => hours * 3600 + minutes * 60;
|
||||
// Santiago's Tren Nos: back-to-back windows of different headways.
|
||||
assert.deepEqual(
|
||||
frequencyDepartures([h(6), h(6, 10), 600, h(6, 10), h(6, 42), 480]),
|
||||
[h(6), h(6, 10), h(6, 18), h(6, 26), h(6, 34)],
|
||||
);
|
||||
// Overlapping windows do not repeat a departure; malformed rows add none.
|
||||
assert.deepEqual(
|
||||
frequencyDepartures([
|
||||
...[h(7), h(7, 20), 600],
|
||||
...[h(7, 10), h(7, 11), 600],
|
||||
...[h(8), h(9), 0],
|
||||
...[Number.NaN, h(9), 600],
|
||||
...[h(9), h(8), 600],
|
||||
]),
|
||||
[h(7), h(7, 10)],
|
||||
);
|
||||
// One row is capped at a run a minute for a day.
|
||||
assert.equal(frequencyDepartures([0, 2 * 86_400, 1]).length, 1440);
|
||||
});
|
||||
|
||||
test('a frequency-based trip becomes one run per departure on its template', async (t) => {
|
||||
const zip = await withZip(t, [
|
||||
{
|
||||
name: 'agency.txt',
|
||||
text: 'agency_id,agency_name,agency_url,agency_timezone\nA,Test,https://example.test,America/Santiago\n',
|
||||
},
|
||||
{
|
||||
name: 'calendar.txt',
|
||||
text: [
|
||||
'service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date',
|
||||
'L,1,1,1,1,1,0,0,20261003,20261231',
|
||||
].join('\n'),
|
||||
},
|
||||
{ name: 'routes.txt', text: 'route_id,route_type\nMTN,0\nMTR,0\n' },
|
||||
{
|
||||
name: 'trips.txt',
|
||||
text: [
|
||||
'route_id,service_id,trip_id,shape_id,trip_headsign,direction_id',
|
||||
'MTN,L,MTN-I-L-M01,S,Nos,0',
|
||||
'MTN,L,MTN-I-L-M02,S,Nos,0',
|
||||
'MTR,L,MTR-I-L-1,S,Rancagua,0',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'frequencies.txt',
|
||||
text: [
|
||||
'trip_id,start_time,end_time,headway_secs,exact_times',
|
||||
'MTN-I-L-M01,06:00:00,06:10:00,600,0',
|
||||
'MTN-I-L-M02,06:10:00,06:30:00,480,0',
|
||||
'MTN-I-L-M02,23:58:00,24:10:00,1200,0',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'stops.txt',
|
||||
text: [
|
||||
'stop_id,stop_name,stop_lat,stop_lon',
|
||||
'A,Alpha,0,' + east(10),
|
||||
'B,Bravo,0,' + east(2000),
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'stop_times.txt',
|
||||
text: [
|
||||
'trip_id,arrival_time,departure_time,stop_id,stop_sequence',
|
||||
// Templates timed from 00:00:00, as the DTPM file has them.
|
||||
'MTN-I-L-M01,00:00:00,00:00:00,A,1',
|
||||
'MTN-I-L-M01,00:05:00,00:05:00,B,2',
|
||||
'MTN-I-L-M02,00:00:00,00:00:00,A,1',
|
||||
'MTN-I-L-M02,00:05:00,00:05:00,B,2',
|
||||
'MTR-I-L-1,07:00:00,07:00:00,A,1',
|
||||
'MTR-I-L-1,07:04:00,07:04:00,B,2',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'shapes.txt',
|
||||
text: [
|
||||
'shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence',
|
||||
...shapeRows('S', 25, 100),
|
||||
].join('\n'),
|
||||
},
|
||||
]);
|
||||
const feed = railFeed();
|
||||
const { pack: paths } = await buildPack(feed, zip, { url: 'x' });
|
||||
const { schedule, stats } = await buildSchedule(
|
||||
feed,
|
||||
zip,
|
||||
{ url: 'x' },
|
||||
paths,
|
||||
{ from: '2026-11-10', days: 1 },
|
||||
);
|
||||
|
||||
assert.deepEqual(Object.keys(schedule.trips).sort(), [
|
||||
'MTN-I-L-M01@06:00:00',
|
||||
'MTN-I-L-M02@06:10:00',
|
||||
'MTN-I-L-M02@06:18:00',
|
||||
'MTN-I-L-M02@06:26:00',
|
||||
'MTN-I-L-M02@23:58:00',
|
||||
'MTR-I-L-1',
|
||||
]);
|
||||
const runs = Object.entries(schedule.trips).filter(([id]) =>
|
||||
id.startsWith('MTN'),
|
||||
);
|
||||
assert.deepEqual(
|
||||
runs.map(([, trip]) => trip.startSec),
|
||||
[21_600, 22_200, 22_680, 23_160, 86_280],
|
||||
);
|
||||
// Every run shares its template's pattern and run times.
|
||||
assert.equal(new Set(runs.map(([, trip]) => trip.pattern)).size, 1);
|
||||
assert.equal(new Set(runs.map(([, trip]) => trip.profile)).size, 1);
|
||||
assert.deepEqual(schedule.profiles[runs[0][1].profile], [0, 0, 300, 300]);
|
||||
assert.equal(runs[0][1].headsign, 'Nos');
|
||||
assert.equal(schedule.trips['MTR-I-L-1'].startSec, 25_200);
|
||||
assert.equal(stats.frequencyTrips, 2);
|
||||
assert.equal(stats.frequencyRuns, 5);
|
||||
assert.equal(stats.tripsKept, 6);
|
||||
// The 23:58 run arrives after midnight: the pack looks a day back for it.
|
||||
assert.equal(stats.lookbackDays, 1);
|
||||
assert.deepEqual([...schedule.services.L], ['2026-11-09', '2026-11-10']);
|
||||
// The browser module times a run from its own start (Santiago is UTC-3
|
||||
// in November).
|
||||
const live = createSchedule(JSON.parse(JSON.stringify(schedule)));
|
||||
const stops = tripStops(
|
||||
live,
|
||||
{ tripId: 'MTN-I-L-M02@06:18:00', serviceDate: '2026-11-10' },
|
||||
Date.parse('2026-11-10T09:20:00Z'),
|
||||
);
|
||||
assert.deepEqual(
|
||||
stops.map((stop) => [stop.stopId, stop.scheduledArrival, stop.state]),
|
||||
[
|
||||
['A', '06:18', 'passed'],
|
||||
['B', '06:23', 'next'],
|
||||
],
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,305 @@
|
||||
// The pack build's download and command line: https on every hop, a byte
|
||||
// cap and a time limit; the redirect target recorded as the source; the
|
||||
// output folder the server reads; `--no-schedule` refused for a feed named
|
||||
// only through its schedule pack; and exit code 2 when a feed fails. No test
|
||||
// here touches the network: downloads are a stubbed fetch, builds read a
|
||||
// local zip.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import {
|
||||
FEED_FAILED_EXIT_CODE,
|
||||
buildRefusal,
|
||||
downloadStatic,
|
||||
packSource,
|
||||
parseBuildArgs,
|
||||
} from '../../scripts/build-rail-paths.mjs';
|
||||
import { getRegisteredRailFeed } from '../data/railFeeds.js';
|
||||
|
||||
const SCRIPT = fileURLToPath(
|
||||
new URL('../../scripts/build-rail-paths.mjs', import.meta.url),
|
||||
);
|
||||
|
||||
async function tempDir(t) {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-cli-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
return dir;
|
||||
}
|
||||
|
||||
/** A fetch stub answering each URL from `answers`, recording each call. */
|
||||
function stubFetch(t, answers) {
|
||||
const calls = [];
|
||||
t.mock.method(globalThis, 'fetch', async (url, init) => {
|
||||
calls.push({ url, init });
|
||||
const answer = answers(url, init);
|
||||
return typeof answer === 'function' ? answer() : answer;
|
||||
});
|
||||
return calls;
|
||||
}
|
||||
|
||||
const redirect = (location, status = 302) =>
|
||||
new Response(null, { status, headers: { location } });
|
||||
|
||||
test('a download follows https redirects hop by hop and records where it ended', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const calls = stubFetch(t, (url) =>
|
||||
url === 'https://example.test/gtfs.zip'
|
||||
? redirect('https://cdn.example.test/files/gtfs.zip')
|
||||
: new Response('zip bytes', {
|
||||
status: 200,
|
||||
headers: { 'last-modified': 'Thu, 05 Nov 2026 10:00:00 GMT' },
|
||||
}),
|
||||
);
|
||||
const dest = path.join(dir, 'gtfs.zip');
|
||||
const got = await downloadStatic('https://example.test/gtfs.zip', dest, {
|
||||
'Digitraffic-User': 'GodsEyeView/RailLayer',
|
||||
});
|
||||
assert.deepEqual(
|
||||
calls.map((call) => call.url),
|
||||
[
|
||||
'https://example.test/gtfs.zip',
|
||||
'https://cdn.example.test/files/gtfs.zip',
|
||||
],
|
||||
);
|
||||
for (const { init } of calls) {
|
||||
assert.equal(init.redirect, 'manual');
|
||||
assert.match(init.headers['User-Agent'], /gods-eye-view-rail-paths/);
|
||||
assert.equal(init.headers['Digitraffic-User'], 'GodsEyeView/RailLayer');
|
||||
}
|
||||
assert.equal(got.finalUrl, 'https://cdn.example.test/files/gtfs.zip');
|
||||
assert.equal(got.lastModified, 'Thu, 05 Nov 2026 10:00:00 GMT');
|
||||
assert.equal(got.bytes, 9);
|
||||
assert.equal(await readFile(dest, 'utf8'), 'zip bytes');
|
||||
// The packs record the URL the file came from, not the one first asked.
|
||||
const feed = getRegisteredRailFeed('mbta-rail');
|
||||
assert.deepEqual(packSource(feed, got), {
|
||||
url: 'https://cdn.example.test/files/gtfs.zip',
|
||||
lastModified: 'Thu, 05 Nov 2026 10:00:00 GMT',
|
||||
});
|
||||
assert.deepEqual(
|
||||
packSource(feed, {
|
||||
file: '/tmp/x.zip',
|
||||
localName: 'x.zip',
|
||||
lastModified: 'Wed, 04 Nov 2026 10:00:00 GMT',
|
||||
}),
|
||||
{
|
||||
url: feed.staticGtfsUrl,
|
||||
file: 'x.zip',
|
||||
lastModified: 'Wed, 04 Nov 2026 10:00:00 GMT',
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('a download is https only: plain http is refused before it is asked', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const calls = stubFetch(t, () =>
|
||||
redirect('http://example.test/gtfs.zip', 301),
|
||||
);
|
||||
await assert.rejects(
|
||||
downloadStatic('https://example.test/gtfs.zip', path.join(dir, 'a.zip')),
|
||||
/refusing non-https URL http:\/\/example\.test\/gtfs\.zip/,
|
||||
);
|
||||
assert.equal(calls.length, 1, 'the http hop is never contacted');
|
||||
await assert.rejects(
|
||||
downloadStatic('http://example.test/gtfs.zip', path.join(dir, 'b.zip')),
|
||||
/refusing non-https/,
|
||||
);
|
||||
assert.equal(calls.length, 1);
|
||||
});
|
||||
|
||||
test('a download stops at the redirect limit', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
let n = 0;
|
||||
const calls = stubFetch(t, () => redirect(`https://example.test/${++n}`));
|
||||
await assert.rejects(
|
||||
downloadStatic('https://example.test/0', path.join(dir, 'a.zip')),
|
||||
/too many redirects/,
|
||||
);
|
||||
assert.equal(calls.length, 6, 'the first ask and five redirects');
|
||||
});
|
||||
|
||||
test('a download over the byte cap is refused, declared or streamed', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
stubFetch(
|
||||
t,
|
||||
(url) => () =>
|
||||
url.endsWith('declared')
|
||||
? new Response('x'.repeat(10), {
|
||||
status: 200,
|
||||
headers: { 'content-length': '2000' },
|
||||
})
|
||||
: new Response(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
for (let i = 0; i < 4; i += 1)
|
||||
controller.enqueue(new Uint8Array(400));
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
await assert.rejects(
|
||||
downloadStatic(
|
||||
'https://example.test/declared',
|
||||
path.join(dir, 'a.zip'),
|
||||
null,
|
||||
{ maxBytes: 1000 },
|
||||
),
|
||||
/download is 2000 bytes/,
|
||||
);
|
||||
await assert.rejects(
|
||||
downloadStatic(
|
||||
'https://example.test/streamed',
|
||||
path.join(dir, 'b.zip'),
|
||||
null,
|
||||
{ maxBytes: 1000 },
|
||||
),
|
||||
/exceeds the size cap/,
|
||||
);
|
||||
// Under the cap it is written whole.
|
||||
const got = await downloadStatic(
|
||||
'https://example.test/streamed',
|
||||
path.join(dir, 'c.zip'),
|
||||
null,
|
||||
{ maxBytes: 1600 },
|
||||
);
|
||||
assert.equal(got.bytes, 1600);
|
||||
});
|
||||
|
||||
test('a download that does not finish in time is abandoned', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
stubFetch(
|
||||
t,
|
||||
(url, init) => () =>
|
||||
new Promise((resolve, reject) => {
|
||||
init.signal.addEventListener('abort', () => reject(init.signal.reason));
|
||||
}),
|
||||
);
|
||||
await assert.rejects(
|
||||
downloadStatic('https://example.test/slow', path.join(dir, 'a.zip'), null, {
|
||||
timeoutMs: 50,
|
||||
}),
|
||||
(error) => error?.name === 'TimeoutError',
|
||||
);
|
||||
});
|
||||
|
||||
test('the build writes where the server reads: RAIL_PATHS_DIR unless --out names a folder', () => {
|
||||
const served = path.join(os.tmpdir(), 'served-packs');
|
||||
assert.equal(
|
||||
parseBuildArgs(['--feed', 'a'], { RAIL_PATHS_DIR: served }).out,
|
||||
path.resolve(served),
|
||||
);
|
||||
const own = path.join(os.tmpdir(), 'own-packs');
|
||||
assert.equal(
|
||||
parseBuildArgs(['--feed', 'a', '--out', own], { RAIL_PATHS_DIR: served })
|
||||
.out,
|
||||
path.resolve(own),
|
||||
);
|
||||
assert.match(
|
||||
parseBuildArgs(['--feed', 'a'], {}).out,
|
||||
/[\\/]\.cache[\\/]rail-paths$/,
|
||||
);
|
||||
// The served folder is held to the same rule as --out.
|
||||
const src = fileURLToPath(new URL('../packs', import.meta.url));
|
||||
assert.throws(
|
||||
() => parseBuildArgs(['--feed', 'a'], { RAIL_PATHS_DIR: src }),
|
||||
/--out must not be inside src\//,
|
||||
);
|
||||
});
|
||||
|
||||
test('--no-schedule is refused for a feed named only through its schedule pack', () => {
|
||||
const ktmb = getRegisteredRailFeed('ktmb');
|
||||
const mbta = getRegisteredRailFeed('mbta-rail');
|
||||
assert.match(buildRefusal(ktmb, { schedule: false }), /ktmb.*schedule pack/);
|
||||
assert.match(
|
||||
buildRefusal(getRegisteredRailFeed('renfe-larga-distancia'), {
|
||||
schedule: false,
|
||||
}),
|
||||
/renfe-larga-distancia/,
|
||||
);
|
||||
assert.equal(buildRefusal(ktmb, { schedule: true }), null);
|
||||
assert.equal(buildRefusal(mbta, { schedule: false }), null);
|
||||
});
|
||||
|
||||
/** Run the build script with the given arguments and environment. */
|
||||
function runBuild(args, env = {}) {
|
||||
const base = { ...process.env };
|
||||
delete base.RAIL_PATHS_DIR;
|
||||
return spawnSync(process.execPath, [SCRIPT, ...args], {
|
||||
encoding: 'utf8',
|
||||
env: { ...base, ...env },
|
||||
timeout: 60_000,
|
||||
});
|
||||
}
|
||||
|
||||
test('a build in which a feed fails exits with code 2', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const broken = path.join(dir, 'broken.zip');
|
||||
await writeFile(broken, 'this is not a zip');
|
||||
const out = path.join(dir, 'packs');
|
||||
const run = runBuild([
|
||||
'--feed',
|
||||
'mbta-rail',
|
||||
'--local-gtfs',
|
||||
`mbta-rail=${broken}`,
|
||||
'--out',
|
||||
out,
|
||||
]);
|
||||
assert.equal(run.status, FEED_FAILED_EXIT_CODE, run.stderr);
|
||||
assert.match(run.stderr, /mbta-rail skipped/);
|
||||
assert.match(run.stdout, /done: 0 built, failed: mbta-rail/);
|
||||
});
|
||||
|
||||
test('a build with no --out writes to RAIL_PATHS_DIR', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const broken = path.join(dir, 'broken.zip');
|
||||
await writeFile(broken, 'this is not a zip');
|
||||
const served = path.join(dir, 'served');
|
||||
const run = runBuild(
|
||||
['--feed', 'mbta-rail', '--local-gtfs', `mbta-rail=${broken}`],
|
||||
{ RAIL_PATHS_DIR: served },
|
||||
);
|
||||
assert.equal(run.status, FEED_FAILED_EXIT_CODE, run.stderr);
|
||||
assert.ok(
|
||||
run.stdout.includes(`1 feeds → ${path.resolve(served)}`),
|
||||
run.stdout,
|
||||
);
|
||||
});
|
||||
|
||||
test('--no-schedule leaves a pack-routed feed and its schedule pack as they were', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const zip = path.join(dir, 'ktmb.zip');
|
||||
await writeFile(zip, 'not read: the build is refused first');
|
||||
const out = path.join(dir, 'packs');
|
||||
const runRefused = async () => {
|
||||
const run = runBuild([
|
||||
'--feed',
|
||||
'ktmb',
|
||||
'--no-schedule',
|
||||
'--local-gtfs',
|
||||
`ktmb=${zip}`,
|
||||
'--out',
|
||||
out,
|
||||
]);
|
||||
assert.equal(run.status, FEED_FAILED_EXIT_CODE, run.stderr);
|
||||
assert.match(
|
||||
run.stderr,
|
||||
/ktmb skipped: --no-schedule: ktmb names its trains only through its schedule pack/,
|
||||
);
|
||||
return run;
|
||||
};
|
||||
await runRefused();
|
||||
const schedule = path.join(out, 'ktmb.schedule.json');
|
||||
await writeFile(schedule, '{"version":1,"feedId":"ktmb"}');
|
||||
await runRefused();
|
||||
assert.equal(
|
||||
await readFile(schedule, 'utf8'),
|
||||
'{"version":1,"feedId":"ktmb"}',
|
||||
'the schedule pack stays',
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,182 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { crc32 } from 'node:zlib';
|
||||
import {
|
||||
createRouteNames,
|
||||
localStatic,
|
||||
openZip,
|
||||
parseLocalGtfs,
|
||||
readTripEnds,
|
||||
} from '../../scripts/build-rail-paths.mjs';
|
||||
import { classifyTrain } from '../data/railFeeds.js';
|
||||
|
||||
/** A minimal stored (uncompressed) zip. */
|
||||
function makeZip(files) {
|
||||
const locals = [];
|
||||
const centrals = [];
|
||||
let offset = 0;
|
||||
for (const [name, text] of Object.entries(files)) {
|
||||
const data = Buffer.from(text, 'utf8');
|
||||
const nameBytes = Buffer.from(name, 'utf8');
|
||||
const local = Buffer.alloc(30);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt32LE(crc32(data), 14);
|
||||
local.writeUInt32LE(data.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(nameBytes.length, 26);
|
||||
const central = Buffer.alloc(46);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt32LE(crc32(data), 16);
|
||||
central.writeUInt32LE(data.length, 20);
|
||||
central.writeUInt32LE(data.length, 24);
|
||||
central.writeUInt16LE(nameBytes.length, 28);
|
||||
central.writeUInt32LE(offset, 42);
|
||||
locals.push(local, nameBytes, data);
|
||||
centrals.push(central, nameBytes);
|
||||
offset += local.length + nameBytes.length + data.length;
|
||||
}
|
||||
const cd = Buffer.concat(centrals);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(locals.length / 3, 8);
|
||||
end.writeUInt16LE(locals.length / 3, 10);
|
||||
end.writeUInt32LE(cd.length, 12);
|
||||
end.writeUInt32LE(offset, 16);
|
||||
return Buffer.concat([...locals, cd, end]);
|
||||
}
|
||||
|
||||
async function tempDir(t) {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-local-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
return dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* A GTFS in a top-level folder: a train from Venezia into Austria, a
|
||||
* domestic train, a train inside Italy, and a bus.
|
||||
*/
|
||||
const GTFS_FILES = {
|
||||
'GTFS_2026/agency.txt':
|
||||
'agency_id,agency_name,agency_url,agency_timezone\n01,Example Rail,https://example.test,Europe/Vienna\n',
|
||||
'GTFS_2026/routes.txt': [
|
||||
'route_id,agency_id,route_short_name,route_type',
|
||||
'I3,01,I3,2',
|
||||
'A9,01,A9,2',
|
||||
'SV1,01,SV1,3',
|
||||
].join('\n'),
|
||||
'GTFS_2026/trips.txt': [
|
||||
'route_id,service_id,trip_id,trip_short_name',
|
||||
'I3,TA,it-domestic,R 134',
|
||||
'A9,TA,cross-border,EC 134',
|
||||
'A9,TA,at-domestic,RJ 650',
|
||||
'SV1,TA,bus,',
|
||||
].join('\n'),
|
||||
'GTFS_2026/stops.txt': [
|
||||
'stop_id,stop_name,stop_lat,stop_lon',
|
||||
'it:22021:1,Venezia Santa Lucia,45.441,12.321',
|
||||
'it:22021:2,Tarvisio Citta Boscoverde,46.507,13.583',
|
||||
'at:42:1:1,Villach Hauptbahnhof,46.618,13.848',
|
||||
'at:42:2:1,Klagenfurt Hauptbahnhof,46.616,14.312',
|
||||
'at:49:1:1,Wien Hauptbahnhof,48.185,16.376',
|
||||
].join('\n'),
|
||||
'GTFS_2026/stop_times.txt': [
|
||||
'trip_id,arrival_time,departure_time,stop_id,stop_sequence',
|
||||
'it-domestic,09:56:00,09:56:00,it:22021:1,1',
|
||||
'it-domestic,12:41:00,12:41:00,it:22021:2,2',
|
||||
'cross-border,12:57:00,12:57:00,it:22021:2,1',
|
||||
'cross-border,13:30:00,13:32:00,at:42:1:1,2',
|
||||
'cross-border,17:07:00,17:07:00,at:49:1:1,3',
|
||||
'at-domestic,08:00:00,08:00:00,at:42:2:1,1',
|
||||
'at-domestic,12:00:00,12:00:00,at:49:1:1,2',
|
||||
'bus,08:00:00,08:00:00,at:42:1:1,1',
|
||||
'bus,08:30:00,08:30:00,at:42:2:1,2',
|
||||
].join('\n'),
|
||||
};
|
||||
|
||||
/**
|
||||
* A feed whose category rule needs each trip's ends (`staticTripEnds:
|
||||
* true`): a trip is international when it stops in more than one country.
|
||||
*/
|
||||
const ENDS_FEED = Object.freeze({
|
||||
id: 'test-ends',
|
||||
staticTripEnds: true,
|
||||
staticTripRename: (trip, routeId, { ends } = {}) =>
|
||||
`${(ends?.countries ?? []).length > 1 ? 'INT' : 'DOM'}~${routeId}`,
|
||||
classify: (routeId) =>
|
||||
String(routeId).startsWith('INT~')
|
||||
? { category: 'international', mode: 'rail' }
|
||||
: String(routeId).startsWith('DOM~')
|
||||
? { category: 'national', mode: 'rail' }
|
||||
: null,
|
||||
});
|
||||
|
||||
test('--local-gtfs names a feed and a zip path, made absolute', () => {
|
||||
const parsed = parseLocalGtfs('test-ends=downloads/GTFS_2026.zip');
|
||||
assert.equal(parsed.id, 'test-ends');
|
||||
assert.ok(path.isAbsolute(parsed.file));
|
||||
assert.equal(path.basename(parsed.file), 'GTFS_2026.zip');
|
||||
// A Windows path keeps its drive letter.
|
||||
assert.equal(
|
||||
parseLocalGtfs('irish-rail=C:\\Users\\me\\Downloads\\gtfs.zip').file,
|
||||
path.resolve('C:\\Users\\me\\Downloads\\gtfs.zip'),
|
||||
);
|
||||
for (const bad of ['', 'test-ends', 'test-ends=', '=x.zip', 'Bad_Id=x.zip'])
|
||||
assert.throws(() => parseLocalGtfs(bad), /--local-gtfs/, bad);
|
||||
});
|
||||
|
||||
test('a local zip stands in for a download, by name only, and is left as it was', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const file = path.join(dir, 'GTFS_2026.zip');
|
||||
const bytes = makeZip(GTFS_FILES);
|
||||
await writeFile(file, bytes);
|
||||
const before = await stat(file);
|
||||
const got = await localStatic(file);
|
||||
assert.equal(got.file, file);
|
||||
assert.equal(got.localName, 'GTFS_2026.zip');
|
||||
assert.equal(got.lastModified, before.mtime.toUTCString());
|
||||
// Reading it through the build leaves the file unchanged.
|
||||
const zip = await openZip(got.file);
|
||||
await readTripEnds(ENDS_FEED, zip);
|
||||
await zip.handle.close();
|
||||
assert.deepEqual(await readFile(file), bytes);
|
||||
assert.equal((await stat(file)).mtimeMs, before.mtimeMs);
|
||||
await assert.rejects(localStatic(path.join(dir, 'missing.zip')));
|
||||
await assert.rejects(localStatic(dir), /not a file/);
|
||||
});
|
||||
|
||||
test('staticTripEnds gives each rail trip its ends and every country it stops in', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const file = path.join(dir, 'feed.zip');
|
||||
await writeFile(file, makeZip(GTFS_FILES));
|
||||
const zip = await openZip(file);
|
||||
t.after(() => zip.handle.close());
|
||||
const feed = ENDS_FEED;
|
||||
const ends = await readTripEnds(feed, zip);
|
||||
assert.deepEqual([...ends.keys()].sort(), [
|
||||
'at-domestic',
|
||||
'cross-border',
|
||||
'it-domestic',
|
||||
]);
|
||||
assert.deepEqual(ends.get('cross-border').countries, ['AT', 'IT']);
|
||||
assert.deepEqual(ends.get('at-domestic').countries, ['AT']);
|
||||
assert.deepEqual(ends.get('it-domestic').countries, ['IT']);
|
||||
assert.equal(ends.get('cross-border').from.name, 'Tarvisio Citta Boscoverde');
|
||||
assert.equal(ends.get('cross-border').to.name, 'Wien Hauptbahnhof');
|
||||
|
||||
const names = await createRouteNames(feed, zip);
|
||||
const route = names.route({ route_id: 'A9', route_type: '2' });
|
||||
const category = (tripId, shortName) =>
|
||||
classifyTrain(
|
||||
feed,
|
||||
names.trip({ trip_id: tripId, trip_short_name: shortName }, route),
|
||||
)?.category ?? null;
|
||||
assert.equal(category('cross-border', 'EC 134'), 'international');
|
||||
assert.equal(category('at-domestic', 'RJ 650'), 'national');
|
||||
assert.equal(names.route({ route_id: 'SV1', route_type: '3' }), null);
|
||||
});
|
||||
@@ -0,0 +1,143 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { crc32 } from 'node:zlib';
|
||||
import {
|
||||
buildPack,
|
||||
createRouteNames,
|
||||
createStateLookup,
|
||||
openZip,
|
||||
} from '../../scripts/build-rail-paths.mjs';
|
||||
import { getRegisteredRailFeed } from '../data/railFeeds.js';
|
||||
|
||||
/** A minimal stored (uncompressed) zip. */
|
||||
function makeZip(files) {
|
||||
const locals = [];
|
||||
const centrals = [];
|
||||
let offset = 0;
|
||||
for (const [name, text] of Object.entries(files)) {
|
||||
const data = Buffer.from(text, 'utf8');
|
||||
const nameBytes = Buffer.from(name, 'utf8');
|
||||
const local = Buffer.alloc(30);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt32LE(crc32(data), 14);
|
||||
local.writeUInt32LE(data.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(nameBytes.length, 26);
|
||||
const central = Buffer.alloc(46);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt32LE(crc32(data), 16);
|
||||
central.writeUInt32LE(data.length, 20);
|
||||
central.writeUInt32LE(data.length, 24);
|
||||
central.writeUInt16LE(nameBytes.length, 28);
|
||||
central.writeUInt32LE(offset, 42);
|
||||
locals.push(local, nameBytes, data);
|
||||
centrals.push(central, nameBytes);
|
||||
offset += local.length + nameBytes.length + data.length;
|
||||
}
|
||||
const cd = Buffer.concat(centrals);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(locals.length / 3, 8);
|
||||
end.writeUInt16LE(locals.length / 3, 10);
|
||||
end.writeUInt32LE(cd.length, 12);
|
||||
end.writeUInt32LE(offset, 16);
|
||||
return Buffer.concat([...locals, cd, end]);
|
||||
}
|
||||
|
||||
async function withZip(t, files) {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-routes-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const file = path.join(dir, 'feed.zip');
|
||||
await writeFile(file, makeZip(files));
|
||||
const zip = await openZip(file);
|
||||
t.after(() => zip.handle.close());
|
||||
return zip;
|
||||
}
|
||||
|
||||
test('states: Spanish provinces group into communities; abroad is abroad', async () => {
|
||||
const at = await createStateLookup();
|
||||
assert.deepEqual(at(40.4712, -3.683), { country: 'ES', state: 'ES-MD' });
|
||||
assert.deepEqual(at(38.8907, -6.9818), { country: 'ES', state: 'ES-EX' });
|
||||
assert.equal(at(41.3799, 2.141).state, 'ES-CT', 'Barcelona');
|
||||
assert.equal(at(36.5287, -6.2896).state, 'ES-AN', 'Cádiz, on the coast');
|
||||
assert.equal(at(43.3035, 5.3822).country, 'FR');
|
||||
assert.equal(at(Number.NaN, 0), null);
|
||||
assert.equal(at(0, -30), null, 'mid-ocean');
|
||||
});
|
||||
|
||||
test('Renfe routes are renamed with product and community scope', async (t) => {
|
||||
const zip = await withZip(t, {
|
||||
'stops.txt': [
|
||||
'stop_id,stop_name,stop_lat,stop_lon',
|
||||
'17000,Madrid-Chamartín,40.4711789,-3.6829524',
|
||||
'37606,Badajoz,38.8906960,-6.9817530',
|
||||
'10200,Guadalajara,40.6440,-3.1820',
|
||||
'18000,Madrid-Atocha,40.4064,-3.6906',
|
||||
].join('\n'),
|
||||
});
|
||||
const names = await createRouteNames(
|
||||
getRegisteredRailFeed('renfe-larga-distancia'),
|
||||
zip,
|
||||
);
|
||||
const route = (id, short, type = '2') =>
|
||||
names.route({ route_id: id, route_short_name: short, route_type: type });
|
||||
assert.equal(route('1700037606GL023', 'MD'), 'MD~X~1700037606GL023');
|
||||
assert.equal(
|
||||
route('1700018000GL001', 'REGIONAL'),
|
||||
'REGIONAL~S~1700018000GL001',
|
||||
);
|
||||
assert.equal(
|
||||
route('1700010200GL001', 'MD'),
|
||||
'MD~X~1700010200GL001',
|
||||
'Madrid to Castilla-La Mancha',
|
||||
);
|
||||
assert.equal(route('1700037606GL023', 'AUTOBUS'), null, 'an unknown product');
|
||||
assert.equal(route('1700037606GL023', 'MD', '3'), null, 'not rail');
|
||||
assert.equal(names.trip({ trip_id: 'x' }, 'MD~S~1'), 'MD~S~1');
|
||||
});
|
||||
|
||||
test('a renamed route keeps its shapes under the new id in the path pack', async (t) => {
|
||||
const zip = await withZip(t, {
|
||||
'routes.txt': [
|
||||
'route_id,agency_id,route_short_name,route_type',
|
||||
'114876,IFF:NS,Intercity,2',
|
||||
'9,GVB,Tram 9,0',
|
||||
].join('\n'),
|
||||
'trips.txt': [
|
||||
'route_id,service_id,trip_id,shape_id',
|
||||
'114876,s,338806760,sh1',
|
||||
'9,s,t9,sh2',
|
||||
].join('\n'),
|
||||
'shapes.txt': [
|
||||
'shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence',
|
||||
'sh1,52.0,5.0,1',
|
||||
'sh1,52.001,5.001,2',
|
||||
'sh1,52.002,5.002,3',
|
||||
'sh2,52.3,4.9,1',
|
||||
'sh2,52.301,4.901,2',
|
||||
].join('\n'),
|
||||
});
|
||||
// A rename that names the operator, as a feed that sorts its routes by
|
||||
// agency writes it; the classifier reads the renamed id.
|
||||
const feed = {
|
||||
id: 'test-feed',
|
||||
staticRouteRename: (route) =>
|
||||
route.route_type === '2'
|
||||
? `${route.agency_id.split(':').pop()}~${route.route_short_name}~${route.route_id}`
|
||||
: null,
|
||||
classify: (routeId) =>
|
||||
routeId?.startsWith('NS~')
|
||||
? { category: 'national', mode: 'rail' }
|
||||
: null,
|
||||
};
|
||||
const { pack } = await buildPack(feed, zip, { url: 'x' });
|
||||
assert.deepEqual(Object.keys(pack.routes), ['NS~Intercity~114876']);
|
||||
|
||||
assert.deepEqual(Object.keys(pack.trips), ['338806760']);
|
||||
});
|
||||
@@ -0,0 +1,162 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { crc32 } from 'node:zlib';
|
||||
import {
|
||||
buildPack,
|
||||
buildSchedule,
|
||||
createRouteNames,
|
||||
openZip,
|
||||
readTripEnds,
|
||||
} from '../../scripts/build-rail-paths.mjs';
|
||||
import { classifyTrain, getRegisteredRailFeed } from '../data/railFeeds.js';
|
||||
|
||||
/** A minimal stored (uncompressed) zip. */
|
||||
function makeZip(files) {
|
||||
const locals = [];
|
||||
const centrals = [];
|
||||
let offset = 0;
|
||||
for (const [name, text] of Object.entries(files)) {
|
||||
const data = Buffer.from(text, 'utf8');
|
||||
const nameBytes = Buffer.from(name, 'utf8');
|
||||
const local = Buffer.alloc(30);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt32LE(crc32(data), 14);
|
||||
local.writeUInt32LE(data.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(nameBytes.length, 26);
|
||||
const central = Buffer.alloc(46);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt32LE(crc32(data), 16);
|
||||
central.writeUInt32LE(data.length, 20);
|
||||
central.writeUInt32LE(data.length, 24);
|
||||
central.writeUInt16LE(nameBytes.length, 28);
|
||||
central.writeUInt32LE(offset, 42);
|
||||
locals.push(local, nameBytes, data);
|
||||
centrals.push(central, nameBytes);
|
||||
offset += local.length + nameBytes.length + data.length;
|
||||
}
|
||||
const cd = Buffer.concat(centrals);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(locals.length / 3, 8);
|
||||
end.writeUInt16LE(locals.length / 3, 10);
|
||||
end.writeUInt32LE(cd.length, 12);
|
||||
end.writeUInt32LE(offset, 16);
|
||||
return Buffer.concat([...locals, cd, end]);
|
||||
}
|
||||
|
||||
async function withZip(t, files) {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-ends-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const file = path.join(dir, 'feed.zip');
|
||||
await writeFile(file, makeZip(files));
|
||||
const zip = await openZip(file);
|
||||
t.after(() => zip.handle.close());
|
||||
return zip;
|
||||
}
|
||||
|
||||
/** A small KTMB-like GTFS: an ETS run KL–JB, an ETS run KL–Hat Yai, a bus. */
|
||||
const KTMB_FILES = {
|
||||
'agency.txt':
|
||||
'agency_id,agency_name,agency_url,agency_timezone\nktmb,KTMB,https://ktmb.com.my,Asia/Kuala_Lumpur\n',
|
||||
'routes.txt': [
|
||||
'route_id,route_short_name,route_type',
|
||||
'ETS,ETS,2',
|
||||
'KC05_KB18,Seremban,2',
|
||||
'B1,Bus,3',
|
||||
].join('\n'),
|
||||
'trips.txt': [
|
||||
'route_id,service_id,trip_id',
|
||||
'ETS,daily,9001',
|
||||
'ETS,daily,9002',
|
||||
'KC05_KB18,daily,2001',
|
||||
'B1,daily,b1',
|
||||
].join('\n'),
|
||||
'stops.txt': [
|
||||
'stop_id,stop_name,stop_lat,stop_lon',
|
||||
'19100,KL SENTRAL,3.134167,101.686111',
|
||||
'37500,JB SENTRAL,1.463147,103.764445',
|
||||
'22700,SEREMBAN,2.719169,101.940792',
|
||||
'99000,HAT YAI,7.0033,100.4687',
|
||||
'50600,BATU CAVES,3.237796,101.681215',
|
||||
].join('\n'),
|
||||
'stop_times.txt': [
|
||||
'trip_id,arrival_time,departure_time,stop_id,stop_sequence',
|
||||
// Out of order on purpose: the ends come from stop_sequence.
|
||||
'9001,08:40:00,08:42:00,22700,2',
|
||||
'9001,08:00:00,08:00:00,19100,1',
|
||||
'9001,12:00:00,12:00:00,37500,3',
|
||||
'9002,09:00:00,09:00:00,19100,1',
|
||||
'9002,17:00:00,17:00:00,99000,2',
|
||||
'2001,07:00:00,07:00:00,50600,1',
|
||||
'2001,07:30:00,07:30:00,19100,2',
|
||||
'b1,07:00:00,07:00:00,19100,1',
|
||||
'b1,07:30:00,07:30:00,22700,2',
|
||||
].join('\n'),
|
||||
'calendar.txt': [
|
||||
'service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date',
|
||||
'daily,1,1,1,1,1,1,1,20260101,20271231',
|
||||
].join('\n'),
|
||||
'calendar_dates.txt': [
|
||||
'service_id,date,exception_type',
|
||||
// Far outside any build window: read and ignored.
|
||||
'daily,20271225,2',
|
||||
'daily,20261110,2',
|
||||
].join('\n'),
|
||||
};
|
||||
|
||||
test('readTripEnds keeps rail trips only, ends by stop_sequence, with states', async (t) => {
|
||||
const zip = await withZip(t, KTMB_FILES);
|
||||
const ends = await readTripEnds(getRegisteredRailFeed('ktmb'), zip);
|
||||
assert.deepEqual([...ends.keys()].sort(), ['2001', '9001', '9002']);
|
||||
const ets = ends.get('9001');
|
||||
assert.equal(ets.from.name, 'KL SENTRAL');
|
||||
assert.equal(ets.to.name, 'JB SENTRAL');
|
||||
assert.deepEqual([ets.from.state, ets.to.state], ['MY-14', 'MY-01']);
|
||||
assert.deepEqual(ets.countries, ['MY']);
|
||||
assert.deepEqual(ends.get('9002').countries, ['MY', 'TH']);
|
||||
});
|
||||
|
||||
test('a feed with staticTripEnds names each trip by where it runs', async (t) => {
|
||||
const zip = await withZip(t, KTMB_FILES);
|
||||
const feed = getRegisteredRailFeed('ktmb');
|
||||
const names = await createRouteNames(feed, zip);
|
||||
const etsKl = names.trip(
|
||||
{ trip_id: '9001' },
|
||||
names.route({ route_id: 'ETS', route_type: '2' }),
|
||||
);
|
||||
assert.equal(etsKl, 'ETS~X~ETS');
|
||||
assert.equal(classifyTrain(feed, etsKl).category, 'national');
|
||||
const etsHatYai = names.trip({ trip_id: '9002' }, 'ETS');
|
||||
assert.equal(classifyTrain(feed, etsHatYai).category, 'international');
|
||||
assert.equal(
|
||||
classifyTrain(feed, names.trip({ trip_id: '2001' }, 'KC05_KB18')).category,
|
||||
'local',
|
||||
);
|
||||
assert.equal(names.route({ route_id: 'B1', route_type: '3' }), null);
|
||||
});
|
||||
|
||||
test('the packs carry the scoped route ids, and the realtime trip resolves to one', async (t) => {
|
||||
const zip = await withZip(t, KTMB_FILES);
|
||||
const feed = getRegisteredRailFeed('ktmb');
|
||||
const names = await createRouteNames(feed, zip);
|
||||
const { pack } = await buildPack(feed, zip, { url: 'x' }, names);
|
||||
const { schedule } = await buildSchedule(feed, zip, { url: 'x' }, pack, {
|
||||
names,
|
||||
from: '2026-11-09',
|
||||
days: 3,
|
||||
});
|
||||
assert.deepEqual(
|
||||
Object.fromEntries(
|
||||
Object.entries(schedule.trips).map(([id, trip]) => [id, trip.route]),
|
||||
),
|
||||
{ 9001: 'ETS~X~ETS', 9002: 'ETS~B~ETS', 2001: 'KC05_KB18~X~KC05_KB18' },
|
||||
);
|
||||
assert.deepEqual(schedule.services.daily, ['2026-11-09', '2026-11-11']);
|
||||
});
|
||||
@@ -0,0 +1,199 @@
|
||||
// Stops a GTFS gives no coordinates, or (0, 0) as some feeds do for foreign
|
||||
// stations their international trains serve: left out of their runs, never
|
||||
// placed by guesswork, so no line is ever drawn to (0, 0).
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { crc32, deflateRawSync } from 'node:zlib';
|
||||
import {
|
||||
buildPack,
|
||||
buildSchedule,
|
||||
hasStopCoordinates,
|
||||
openZip,
|
||||
readTripEnds,
|
||||
} from '../../scripts/build-rail-paths.mjs';
|
||||
|
||||
/** A minimal zip writer: deflated entries, no zip64. */
|
||||
function makeZip(files) {
|
||||
const locals = [];
|
||||
const centrals = [];
|
||||
let offset = 0;
|
||||
for (const { name, text } of files) {
|
||||
const data = Buffer.from(text, 'utf8');
|
||||
const body = deflateRawSync(data);
|
||||
const nameBytes = Buffer.from(name, 'utf8');
|
||||
const local = Buffer.alloc(30);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt16LE(8, 8);
|
||||
local.writeUInt32LE(crc32(data), 14);
|
||||
local.writeUInt32LE(body.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(nameBytes.length, 26);
|
||||
const central = Buffer.alloc(46);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt16LE(8, 10);
|
||||
central.writeUInt32LE(crc32(data), 16);
|
||||
central.writeUInt32LE(body.length, 20);
|
||||
central.writeUInt32LE(data.length, 24);
|
||||
central.writeUInt16LE(nameBytes.length, 28);
|
||||
central.writeUInt32LE(offset, 42);
|
||||
locals.push(local, nameBytes, body);
|
||||
centrals.push(central, nameBytes);
|
||||
offset += local.length + nameBytes.length + body.length;
|
||||
}
|
||||
const cd = Buffer.concat(centrals);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(files.length, 8);
|
||||
end.writeUInt16LE(files.length, 10);
|
||||
end.writeUInt32LE(cd.length, 12);
|
||||
end.writeUInt32LE(offset, 16);
|
||||
return Buffer.concat([...locals, cd, end]);
|
||||
}
|
||||
|
||||
// A line 20 km due east from 51° N 7° E (in Germany), x in metres.
|
||||
const LAT = 51;
|
||||
const LON = 7;
|
||||
const DLON = 1 / (111_320 * Math.cos((LAT * Math.PI) / 180));
|
||||
const lonAt = (x) => LON + x * DLON;
|
||||
const at = (x) => `${LAT.toFixed(6)},${lonAt(x).toFixed(6)}`;
|
||||
|
||||
async function feedZip(t) {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-unlocated-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const file = path.join(dir, 'feed.zip');
|
||||
await writeFile(
|
||||
file,
|
||||
makeZip([
|
||||
{
|
||||
name: 'agency.txt',
|
||||
text: 'agency_id,agency_name,agency_url,agency_timezone\nA,Test,https://example.test,Europe/Berlin\n',
|
||||
},
|
||||
{
|
||||
name: 'calendar.txt',
|
||||
text: 'service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date\nWK,1,1,1,1,1,1,1,20260101,20261231\n',
|
||||
},
|
||||
{ name: 'routes.txt', text: 'route_id,route_type\nR1,2\n' },
|
||||
{
|
||||
name: 'trips.txt',
|
||||
text: [
|
||||
'route_id,service_id,trip_id,shape_id',
|
||||
'R1,WK,t1,',
|
||||
'R1,WK,t2,',
|
||||
'R1,WK,t3,',
|
||||
'R1,WK,t4,',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'stops.txt',
|
||||
text: [
|
||||
'stop_id,stop_name,stop_lat,stop_lon,parent_station',
|
||||
`A,Alpha,${at(0)},`,
|
||||
`C,Charlie,${at(20_000)},`,
|
||||
// (0, 0) in the feed, parent and all.
|
||||
'PZ,Zeta (Westf.),0,0,',
|
||||
'Z,Zeta (Westf.),0,0,PZ',
|
||||
'Y,Ypsilon,0,0,',
|
||||
// Blank coordinates are none either.
|
||||
'Q,Quebec,,,',
|
||||
].join('\n'),
|
||||
},
|
||||
{
|
||||
name: 'stop_times.txt',
|
||||
text: [
|
||||
'trip_id,arrival_time,departure_time,stop_id,stop_sequence',
|
||||
't1,08:00:00,08:00:00,A,1',
|
||||
't1,08:10:00,08:10:00,Z,2',
|
||||
't1,08:20:00,08:20:00,C,3',
|
||||
't2,09:00:00,09:00:00,A,1',
|
||||
't2,09:10:00,09:10:00,Y,2',
|
||||
't2,09:20:00,09:20:00,C,3',
|
||||
't3,10:00:00,10:00:00,A,1',
|
||||
't3,10:10:00,10:10:00,Y,2',
|
||||
't4,11:00:00,11:00:00,A,1',
|
||||
't4,11:10:00,11:10:00,Q,2',
|
||||
't4,11:20:00,11:20:00,C,3',
|
||||
].join('\n'),
|
||||
},
|
||||
]),
|
||||
);
|
||||
const zip = await openZip(file);
|
||||
t.after(() => zip.handle.close());
|
||||
return zip;
|
||||
}
|
||||
|
||||
const feed = {
|
||||
id: 'test-feed',
|
||||
classify: () => ({ category: 'regional', mode: 'rail' }),
|
||||
};
|
||||
const options = { from: '2026-11-09', days: 2 };
|
||||
|
||||
/** Every vertex of every shape the pack holds, as [lat, lon]. */
|
||||
function vertices(pack) {
|
||||
const out = [];
|
||||
for (const flat of pack.shapes)
|
||||
for (let i = 0; i < flat.length; i += 2) out.push([flat[i], flat[i + 1]]);
|
||||
return out;
|
||||
}
|
||||
|
||||
const stopNames = (schedule, id) =>
|
||||
schedule.patterns[schedule.trips[id].pattern].stops.map(
|
||||
(k) => schedule.stops[k].name,
|
||||
);
|
||||
|
||||
test('(0, 0) and blank coordinates are no coordinates', () => {
|
||||
assert.equal(hasStopCoordinates(0, 0), false);
|
||||
assert.equal(hasStopCoordinates(-0, 0), false);
|
||||
assert.equal(hasStopCoordinates(Number.NaN, 7), false);
|
||||
assert.equal(hasStopCoordinates(undefined, undefined), false);
|
||||
assert.equal(hasStopCoordinates(0, 0.00001), true);
|
||||
assert.equal(hasStopCoordinates(51.68, 7.81), true);
|
||||
});
|
||||
|
||||
test('an unlocated stop is left out of its runs, never drawn to (0, 0)', async (t) => {
|
||||
const zip = await feedZip(t);
|
||||
const { pack } = await buildPack(feed, zip, { url: 'x' });
|
||||
const { schedule, stats } = await buildSchedule(
|
||||
feed,
|
||||
zip,
|
||||
{ url: 'x' },
|
||||
pack,
|
||||
options,
|
||||
);
|
||||
// t1, t2 and t4 keep their two located stops; t3 has only one left.
|
||||
assert.deepEqual(Object.keys(schedule.trips).sort(), ['t1', 't2', 't4']);
|
||||
for (const id of ['t1', 't2', 't4'])
|
||||
assert.deepEqual(stopNames(schedule, id), ['Alpha', 'Charlie']);
|
||||
// The stop's times go with it.
|
||||
const t1 = schedule.trips.t1;
|
||||
assert.deepEqual(schedule.profiles[t1.profile], [0, 0, 1200, 1200]);
|
||||
assert.deepEqual(schedule.patterns[t1.pattern].seq, [1, 3]);
|
||||
assert.equal(stats.tripsUnknownStop, 1);
|
||||
assert.equal(stats.tripsStopsLeftOut, 3);
|
||||
assert.equal(stats.stopsLeftOut, 3);
|
||||
// Nothing is guessed: no stop without coordinates, nor its parent, is in
|
||||
// the pack.
|
||||
for (const id of ['Z', 'PZ', 'Y', 'Q'])
|
||||
assert.equal(
|
||||
schedule.stops.find((s) => s.id === id),
|
||||
undefined,
|
||||
id,
|
||||
);
|
||||
assert.ok(schedule.stops.every((s) => hasStopCoordinates(s.lat, s.lon)));
|
||||
assert.ok(vertices(pack).every(([lat, lon]) => hasStopCoordinates(lat, lon)));
|
||||
});
|
||||
|
||||
test('a run ends at its last located stop when its last has no coordinates', async (t) => {
|
||||
const zip = await feedZip(t);
|
||||
const ends = await readTripEnds({}, zip);
|
||||
assert.equal(ends.get('t1').from.name, 'Alpha');
|
||||
assert.equal(ends.get('t1').to.name, 'Charlie');
|
||||
// t3: Alpha, then Ypsilon at (0, 0).
|
||||
assert.equal(ends.get('t3').to.name, 'Alpha');
|
||||
assert.deepEqual(ends.get('t3').countries, ['DE']);
|
||||
});
|
||||
@@ -0,0 +1,318 @@
|
||||
// How the path build writes a feed's packs: the path and schedule packs as
|
||||
// a pair, never one without the other it indexes; nothing over the size the
|
||||
// server serves; no collapse in trips without --force; one build per folder
|
||||
// at a time; and a command line that never starts a build of every feed by
|
||||
// itself.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import {
|
||||
BUILD_LOCK,
|
||||
FEED_FAILED_EXIT_CODE,
|
||||
MIN_TRIP_SHARE,
|
||||
acquireDirLock,
|
||||
buildExitCode,
|
||||
isInside,
|
||||
operatorShapeTrips,
|
||||
parseBuildArgs,
|
||||
scheduleTripsPerDay,
|
||||
tripFloorProblem,
|
||||
writeFeedPacks,
|
||||
} from '../../scripts/build-rail-paths.mjs';
|
||||
|
||||
async function tempDir(t) {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-write-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
return dir;
|
||||
}
|
||||
|
||||
/** A path pack with `trips` trips on one operator shape. */
|
||||
function pathPack(builtAt, trips, { shapes = 1, source = 'operator' } = {}) {
|
||||
return {
|
||||
version: 1,
|
||||
feedId: 'f',
|
||||
builtAt,
|
||||
source: { url: 'https://example.test/gtfs.zip' },
|
||||
shapes: Array.from({ length: shapes }, () => [0, 0, 0.001, 0.001]),
|
||||
dist: Array.from({ length: shapes }, () => [0, 157]),
|
||||
shapeSources: Array.from({ length: shapes }, () => source),
|
||||
trips: Object.fromEntries(
|
||||
Array.from({ length: trips }, (_, i) => [`t${i}`, 0]),
|
||||
),
|
||||
routes: { R: [0] },
|
||||
};
|
||||
}
|
||||
|
||||
/** A schedule pack with `trips` trips over `days` service days. */
|
||||
function schedulePack(pathsBuiltAt, trips, days = 4) {
|
||||
const from = '2026-11-09';
|
||||
const to = new Date(Date.parse(from) + (days - 1) * 86_400_000)
|
||||
.toISOString()
|
||||
.slice(0, 10);
|
||||
return {
|
||||
version: 1,
|
||||
feedId: 'f',
|
||||
builtAt: pathsBuiltAt,
|
||||
pathsBuiltAt,
|
||||
window: { from, to },
|
||||
trips: Object.fromEntries(
|
||||
Array.from({ length: trips }, (_, i) => [`t${i}`, { pattern: 0 }]),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
const read = async (dir, name) =>
|
||||
JSON.parse(await readFile(path.join(dir, name), 'utf8'));
|
||||
const tempFiles = async (dir) =>
|
||||
(await readdir(dir)).filter((name) => name.endsWith('.tmp'));
|
||||
|
||||
async function seed(dir, { trips = 10, scheduleTrips = 100 } = {}) {
|
||||
await writeFile(
|
||||
path.join(dir, 'f.json'),
|
||||
JSON.stringify(pathPack('old', trips)),
|
||||
);
|
||||
await writeFile(
|
||||
path.join(dir, 'f.schedule.json'),
|
||||
JSON.stringify(schedulePack('old', scheduleTrips)),
|
||||
);
|
||||
}
|
||||
|
||||
test('both packs are written as a pair, through temporary names of their own', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
await seed(dir);
|
||||
const written = await writeFeedPacks(dir, 'f', {
|
||||
pack: pathPack('new', 10),
|
||||
schedule: schedulePack('new', 100),
|
||||
});
|
||||
assert.equal(written.removedSchedule, false);
|
||||
assert.equal((await read(dir, 'f.json')).builtAt, 'new');
|
||||
assert.equal((await read(dir, 'f.schedule.json')).pathsBuiltAt, 'new');
|
||||
assert.equal(JSON.parse(written.scheduleText).pathsBuiltAt, 'new');
|
||||
assert.deepEqual(await tempFiles(dir), []);
|
||||
});
|
||||
|
||||
test('a schedule that cannot be written leaves the previous path pack in place', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
await seed(dir);
|
||||
const broken = schedulePack('new', 100);
|
||||
broken.trips.t0.startSec = 1n; // JSON cannot hold it.
|
||||
await assert.rejects(
|
||||
writeFeedPacks(dir, 'f', { pack: pathPack('new', 10), schedule: broken }),
|
||||
/BigInt/,
|
||||
);
|
||||
assert.equal((await read(dir, 'f.json')).builtAt, 'old');
|
||||
assert.equal((await read(dir, 'f.schedule.json')).pathsBuiltAt, 'old');
|
||||
assert.deepEqual(await tempFiles(dir), []);
|
||||
});
|
||||
|
||||
test('without a schedule the previous one, which indexes the old shapes, is removed', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
await seed(dir);
|
||||
const written = await writeFeedPacks(dir, 'f', { pack: pathPack('new', 10) });
|
||||
assert.equal(written.removedSchedule, true);
|
||||
assert.equal(written.scheduleText, null);
|
||||
assert.equal((await read(dir, 'f.json')).builtAt, 'new');
|
||||
assert.deepEqual((await readdir(dir)).sort(), ['f.json']);
|
||||
// Nothing to remove the next time.
|
||||
const again = await writeFeedPacks(dir, 'f', { pack: pathPack('newer', 10) });
|
||||
assert.equal(again.removedSchedule, false);
|
||||
});
|
||||
|
||||
test('a pack over the size the server serves is not written, and says what thins it', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
await seed(dir);
|
||||
const pack = pathPack('new', 10);
|
||||
const maxBytes = Buffer.byteLength(JSON.stringify(pack)) - 1;
|
||||
await assert.rejects(
|
||||
writeFeedPacks(dir, 'f', { pack, maxBytes }),
|
||||
(error) =>
|
||||
/over the .* the server serves/.test(error.message) &&
|
||||
/its shapes hold 2 points/.test(error.message) &&
|
||||
/staticSimplifyM/.test(error.message),
|
||||
);
|
||||
const schedule = schedulePack('new', 100);
|
||||
await assert.rejects(
|
||||
writeFeedPacks(dir, 'f', {
|
||||
pack: pathPack('new', 10, { shapes: 0 }),
|
||||
schedule,
|
||||
maxBytes: Buffer.byteLength(JSON.stringify(schedule)) - 1,
|
||||
}),
|
||||
/schedule is .* --days/,
|
||||
);
|
||||
assert.equal((await read(dir, 'f.json')).builtAt, 'old');
|
||||
assert.equal((await read(dir, 'f.schedule.json')).pathsBuiltAt, 'old');
|
||||
});
|
||||
|
||||
test('a collapse in trips keeps the previous packs unless --force', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
await seed(dir, { trips: 10, scheduleTrips: 100 });
|
||||
// The classifier kept nothing.
|
||||
await assert.rejects(
|
||||
writeFeedPacks(dir, 'f', {
|
||||
pack: pathPack('new', 0),
|
||||
schedule: schedulePack('new', 0),
|
||||
}),
|
||||
(error) =>
|
||||
/trips on operator shapes 0 against 10/.test(error.message) &&
|
||||
/schedule trips per day 0 against 25/.test(error.message) &&
|
||||
/--force/.test(error.message),
|
||||
);
|
||||
// The calendar went missing: the shapes are fine, the timetable is not.
|
||||
await assert.rejects(
|
||||
writeFeedPacks(dir, 'f', {
|
||||
pack: pathPack('new', 10),
|
||||
schedule: schedulePack('new', 4),
|
||||
}),
|
||||
/schedule trips per day 1 against 25/,
|
||||
);
|
||||
assert.equal((await read(dir, 'f.json')).builtAt, 'old');
|
||||
assert.equal((await read(dir, 'f.schedule.json')).pathsBuiltAt, 'old');
|
||||
// A shorter window is not a collapse: trips are compared per day.
|
||||
await writeFeedPacks(dir, 'f', {
|
||||
pack: pathPack('short', 10),
|
||||
schedule: schedulePack('short', 30, 1),
|
||||
});
|
||||
assert.equal((await read(dir, 'f.json')).builtAt, 'short');
|
||||
// --force writes what the floor refused.
|
||||
await writeFeedPacks(dir, 'f', {
|
||||
pack: pathPack('forced', 0),
|
||||
schedule: schedulePack('forced', 0),
|
||||
force: true,
|
||||
});
|
||||
assert.equal((await read(dir, 'f.json')).builtAt, 'forced');
|
||||
// A first build has nothing to fall from.
|
||||
const fresh = await tempDir(t);
|
||||
await writeFeedPacks(fresh, 'f', {
|
||||
pack: pathPack('first', 0),
|
||||
schedule: schedulePack('first', 0),
|
||||
});
|
||||
assert.equal((await read(fresh, 'f.json')).builtAt, 'first');
|
||||
});
|
||||
|
||||
test('the trip floor counts what the build makes alike each time', () => {
|
||||
// Only the operator's shapes count: a shape of any other source does not.
|
||||
|
||||
const pack = pathPack('x', 0, { shapes: 2 });
|
||||
pack.shapeSources = ['operator', 'straight'];
|
||||
pack.trips = { a: 0, b: 1, c: 1 };
|
||||
assert.equal(operatorShapeTrips(pack), 1);
|
||||
assert.equal(operatorShapeTrips(null), 0);
|
||||
assert.equal(scheduleTripsPerDay(schedulePack('x', 14, 7)), 2);
|
||||
assert.equal(MIN_TRIP_SHARE, 0.2);
|
||||
assert.equal(tripFloorProblem('trips', 2, 10), null);
|
||||
assert.match(tripFloorProblem('trips', 1, 10), /trips 1 against 10/);
|
||||
assert.equal(tripFloorProblem('trips', 0, 0), null);
|
||||
});
|
||||
|
||||
test('one build holds a folder’s lock; a second refuses, a dead one’s is taken over', async (t) => {
|
||||
const dir = await tempDir(t);
|
||||
const held = await acquireDirLock(dir, BUILD_LOCK, { what: 'test build' });
|
||||
const record = JSON.parse(await readFile(held.file, 'utf8'));
|
||||
assert.equal(record.pid, process.pid);
|
||||
await assert.rejects(
|
||||
acquireDirLock(dir, BUILD_LOCK, { what: 'test build' }),
|
||||
(error) =>
|
||||
/another test build holds/.test(error.message) &&
|
||||
error.message.includes(String(process.pid)),
|
||||
);
|
||||
await held.release();
|
||||
assert.deepEqual(await readdir(dir), []);
|
||||
|
||||
// A lock whose process is gone.
|
||||
const lockFile = path.join(dir, BUILD_LOCK);
|
||||
await writeFile(
|
||||
lockFile,
|
||||
JSON.stringify({ pid: 999_999, time: new Date().toISOString() }),
|
||||
);
|
||||
const taken = await acquireDirLock(dir, BUILD_LOCK, { alive: () => false });
|
||||
assert.equal(JSON.parse(await readFile(lockFile, 'utf8')).pid, process.pid);
|
||||
await taken.release();
|
||||
|
||||
// A live process's lock not refreshed for longer than staleMs.
|
||||
await writeFile(
|
||||
lockFile,
|
||||
JSON.stringify({ pid: 4242, time: '2026-01-01T00:00:00.000Z' }),
|
||||
);
|
||||
const old = await acquireDirLock(dir, BUILD_LOCK, { alive: () => true });
|
||||
await old.release();
|
||||
|
||||
// A live, fresh one is respected; release never removes another's lock.
|
||||
await writeFile(
|
||||
lockFile,
|
||||
JSON.stringify({ pid: 4242, time: new Date().toISOString() }),
|
||||
);
|
||||
await assert.rejects(
|
||||
acquireDirLock(dir, BUILD_LOCK, { alive: () => true }),
|
||||
/pid 4242/,
|
||||
);
|
||||
await old.release();
|
||||
assert.equal(JSON.parse(await readFile(lockFile, 'utf8')).pid, 4242);
|
||||
});
|
||||
|
||||
test('the command line names its feeds or says --all, and --help needs nothing else', () => {
|
||||
assert.throws(() => parseBuildArgs([]), /--feed <id>.*--all/);
|
||||
assert.equal(parseBuildArgs(['--all']).all, true);
|
||||
assert.deepEqual(parseBuildArgs(['--help']), { help: true });
|
||||
assert.deepEqual(parseBuildArgs(['--feed', 'x', '-h']), { help: true });
|
||||
assert.throws(() => parseBuildArgs(['--all', '--feed', 'x']), /not both/);
|
||||
assert.throws(
|
||||
() => parseBuildArgs(['--feed', 'x', '--bogus']),
|
||||
/Unknown argument --bogus/,
|
||||
);
|
||||
const named = parseBuildArgs(['--feed', 'a', '--feed', 'b', '--force']);
|
||||
assert.deepEqual(named.feeds, ['a', 'b']);
|
||||
assert.equal(named.force, true);
|
||||
assert.equal(parseBuildArgs(['--feed', 'a']).force, false);
|
||||
// Only registered feeds are built: there is no flag for any other.
|
||||
assert.throws(
|
||||
() =>
|
||||
parseBuildArgs([
|
||||
'--extra-feed',
|
||||
'test-extra=https://example.test/gtfs.zip',
|
||||
]),
|
||||
/Unknown argument --extra-feed/,
|
||||
);
|
||||
});
|
||||
|
||||
test('--out may not be src/ or public/ or inside them, but may sit beside them', () => {
|
||||
const root = path.resolve(
|
||||
path.dirname(fileURLToPath(import.meta.url)),
|
||||
'..',
|
||||
'..',
|
||||
);
|
||||
for (const inside of [
|
||||
'src',
|
||||
'public',
|
||||
path.join('src', 'data', 'packs'),
|
||||
// A name that only starts with two dots is still inside.
|
||||
path.join('src', '..cache'),
|
||||
path.join('public', '..x', 'y'),
|
||||
])
|
||||
assert.throws(
|
||||
() => parseBuildArgs(['--feed', 'a', '--out', path.join(root, inside)]),
|
||||
/--out must not be inside/,
|
||||
inside,
|
||||
);
|
||||
for (const outside of [
|
||||
path.join('.cache', 'rail-paths'),
|
||||
'src-packs',
|
||||
path.join('src', '..', 'packs'),
|
||||
])
|
||||
assert.equal(
|
||||
parseBuildArgs(['--feed', 'a', '--out', path.join(root, outside)]).out,
|
||||
path.resolve(root, outside),
|
||||
);
|
||||
assert.equal(isInside(root, root), true);
|
||||
assert.equal(isInside(path.join(root, 'src'), root), false);
|
||||
});
|
||||
|
||||
test('a build in which any feed fails exits with code 2', () => {
|
||||
assert.equal(FEED_FAILED_EXIT_CODE, 2);
|
||||
assert.equal(buildExitCode(0), 0);
|
||||
// One failure among feeds that built is still a failed build.
|
||||
assert.equal(buildExitCode(1), 2);
|
||||
assert.equal(buildExitCode(12), 2);
|
||||
});
|
||||
@@ -313,9 +313,9 @@ test('FIRMS retains a large successful source during partial failure and filters
|
||||
const first = json(await request());
|
||||
assert.equal(first.count, 130001);
|
||||
assert.equal(first.sources.filter((s) => s.ok).length, 1);
|
||||
assert.equal(calls, 4);
|
||||
assert.equal(calls, 3);
|
||||
assert.equal(json(await request()).count, 130001);
|
||||
assert.equal(calls, 4);
|
||||
assert.equal(calls, 3);
|
||||
assert.deepEqual(json(await request('/status')).transactions, {
|
||||
used: 3,
|
||||
limit: 5000,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { once } from 'node:events';
|
||||
import { gunzipSync } from 'node:zlib';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import * as providers from '../../server/providers/live.js';
|
||||
@@ -15,7 +16,7 @@ function install(plugin, preview = false) {
|
||||
},
|
||||
},
|
||||
});
|
||||
return async (route, url = '/', method = 'GET') => {
|
||||
return async (route, url = '/', method = 'GET', headers = {}) => {
|
||||
assert.ok(routes.has(route), `registered route: ${route}`);
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
@@ -32,7 +33,7 @@ function install(plugin, preview = false) {
|
||||
this.body = body;
|
||||
},
|
||||
};
|
||||
await routes.get(route)({ url, method }, response);
|
||||
await routes.get(route)({ url, method, headers }, response);
|
||||
return response;
|
||||
};
|
||||
}
|
||||
@@ -584,6 +585,168 @@ test('track backfill proxy returns 502 on an oversized upstream body and caches
|
||||
assert.equal(callCount, 1);
|
||||
});
|
||||
|
||||
test('the aircraft snapshot routes decline a non-GET before any upstream work', async (t) => {
|
||||
// A fetch that throws proves the refusal comes before any upstream call.
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
throw new Error(`upstream must not be reached: ${url}`);
|
||||
});
|
||||
const cases = [
|
||||
[install(providers.openSkyProxy()), '/api/flights'],
|
||||
[install(providers.adsbLolProxy()), '/api/military'],
|
||||
];
|
||||
for (const [request, route] of cases) {
|
||||
for (const method of ['POST', 'PUT', 'DELETE', 'PATCH']) {
|
||||
const response = await request(route, '/', method);
|
||||
assert.equal(response.statusCode, 405, `${method} ${route}`);
|
||||
// RFC 9110 section 15.5.6: a 405 names the methods it accepts.
|
||||
assert.equal(response.headers.allow, 'GET', `${method} ${route}`);
|
||||
assert.deepEqual(JSON.parse(response.body), {
|
||||
error: 'Method Not Allowed',
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('the snapshot method check hands each /track route on to its own handler', async () => {
|
||||
// The snapshot mount also matches /track, so it must hand /track on
|
||||
// before it checks the method.
|
||||
for (const [plugin, route] of [
|
||||
[providers.openSkyProxy(), '/api/flights'],
|
||||
[providers.adsbLolProxy(), '/api/military'],
|
||||
]) {
|
||||
const routes = new Map();
|
||||
plugin.configureServer({
|
||||
middlewares: { use: (path, handler) => routes.set(path, handler) },
|
||||
});
|
||||
const res = {
|
||||
statusCode: 200,
|
||||
writeHead(s) {
|
||||
this.statusCode = s;
|
||||
},
|
||||
end() {},
|
||||
};
|
||||
let handedOn = false;
|
||||
await routes.get(route)(
|
||||
{ url: '/track?icao24=abc123', method: 'POST' },
|
||||
res,
|
||||
() => {
|
||||
handedOn = true;
|
||||
},
|
||||
);
|
||||
assert.ok(handedOn, `${route}/track reaches next()`);
|
||||
assert.equal(
|
||||
res.statusCode,
|
||||
200,
|
||||
`${route}/track is not answered by the snapshot handler`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('the OpenSky snapshot is compressed for clients that ask, and an error body never inherits the cached frame', async (t) => {
|
||||
environment(t, {
|
||||
OPENSKY_AUTH_MODE: 'anon',
|
||||
OPENSKY_CLIENT_ID: undefined,
|
||||
OPENSKY_CLIENT_SECRET: undefined,
|
||||
OPENSKY_USERNAME: undefined,
|
||||
OPENSKY_PASSWORD: undefined,
|
||||
});
|
||||
let now = Date.now();
|
||||
t.mock.method(Date, 'now', () => now);
|
||||
t.mock.method(console, 'log', () => {});
|
||||
t.mock.method(console, 'error', () => {});
|
||||
|
||||
let upstreamStatus = 200;
|
||||
let snapshotAgeMs = 0;
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
if (url.includes('/lat/'))
|
||||
return Response.json({
|
||||
now: now / 1000,
|
||||
ac: Array.from({ length: 200 }, (_, index) => ({
|
||||
hex: `abc${index}`,
|
||||
lat: 30,
|
||||
lon: -97,
|
||||
alt_baro: 10000,
|
||||
})),
|
||||
});
|
||||
assert.ok(url.includes('/states/'), `unexpected upstream call: ${url}`);
|
||||
if (upstreamStatus !== 200)
|
||||
return new Response('{"error":"upstream"}', { status: upstreamStatus });
|
||||
const sourceSeconds = Math.floor((now - snapshotAgeMs) / 1000);
|
||||
// Healthy credits keep the 9 s base TTL, so the clock jumps below clear it.
|
||||
return Response.json(
|
||||
{
|
||||
time: sourceSeconds,
|
||||
states: Array.from({ length: 200 }, (_, index) => [
|
||||
`abc${index}`,
|
||||
'CALLSIGN',
|
||||
'United States',
|
||||
sourceSeconds,
|
||||
-97.7,
|
||||
30.1,
|
||||
10000,
|
||||
]),
|
||||
},
|
||||
{ headers: { 'x-rate-limit-remaining': '4000' } },
|
||||
);
|
||||
});
|
||||
|
||||
// A fresh module: the snapshot cache, its frame and the TTL are module state.
|
||||
const isolated = await import(
|
||||
`../../server/providers/aircraft/opensky.js?compression=${now}`
|
||||
);
|
||||
const states = install(isolated.openSkyProxy());
|
||||
const compressed = await states('/api/flights', '/', 'GET', {
|
||||
'accept-encoding': 'gzip, deflate, br',
|
||||
});
|
||||
assert.equal(compressed.statusCode, 200);
|
||||
assert.equal(compressed.headers['content-encoding'], 'gzip');
|
||||
assert.equal(compressed.headers.vary, 'Accept-Encoding');
|
||||
assert.ok(Buffer.isBuffer(compressed.body));
|
||||
|
||||
// Same cached snapshot, no Accept-Encoding: identity bytes, which the frame
|
||||
// above must decode to.
|
||||
const identity = await states('/api/flights');
|
||||
assert.equal(identity.headers['x-opensky-cache'], 'HIT');
|
||||
assert.equal(identity.headers['content-encoding'], undefined);
|
||||
assert.equal(identity.headers.vary, 'Accept-Encoding');
|
||||
assert.equal(gunzipSync(compressed.body).toString(), identity.body);
|
||||
|
||||
// `gzip;q=0` refuses gzip.
|
||||
const refused = await states('/api/flights', '/', 'GET', {
|
||||
'accept-encoding': 'gzip;q=0',
|
||||
});
|
||||
assert.equal(refused.headers['content-encoding'], undefined);
|
||||
assert.equal(refused.body, identity.body);
|
||||
|
||||
// An upstream rejection is not cached, so it must not carry the previous
|
||||
// snapshot's Content-Encoding.
|
||||
upstreamStatus = 403;
|
||||
now += 60000; // past the 9 s base TTL
|
||||
const rejected = await states('/api/flights', '/', 'GET', {
|
||||
'accept-encoding': 'gzip',
|
||||
});
|
||||
assert.equal(rejected.statusCode, 403);
|
||||
assert.equal(rejected.headers['content-encoding'], undefined);
|
||||
assert.ok(JSON.parse(rejected.body).error, 'a readable error body');
|
||||
|
||||
// A snapshot too old to serve falls back to adsb.lol, which negotiates the
|
||||
// same way.
|
||||
upstreamStatus = 200;
|
||||
snapshotAgeMs = 3600000;
|
||||
now += 60000;
|
||||
const regional = await states('/api/flights', '?lat=30&lon=-97', 'GET', {
|
||||
'accept-encoding': 'gzip',
|
||||
});
|
||||
assert.equal(regional.statusCode, 200);
|
||||
assert.equal(regional.headers['x-flight-source'], 'adsb.lol');
|
||||
assert.equal(regional.headers['content-encoding'], 'gzip');
|
||||
assert.equal(regional.headers.vary, 'Accept-Encoding');
|
||||
assert.equal(
|
||||
JSON.parse(gunzipSync(regional.body).toString()).states.length,
|
||||
200,
|
||||
);
|
||||
});
|
||||
|
||||
test('OpenSky Basic mode no longer sends username and password: it asks anonymously and warns once', async (t) => {
|
||||
environment(t, {
|
||||
OPENSKY_CLIENT_ID: undefined,
|
||||
|
||||
@@ -20,12 +20,9 @@ import {
|
||||
import { openAiRealtimeProxy } from 'gods-eye-view/server/providers/openai';
|
||||
import { keySetupEndpoint } from 'gods-eye-view/server/standalone/key-setup';
|
||||
import { realtimeInstructions } from '../../server/providers/openai/instructions.js';
|
||||
import {
|
||||
GEV_REALTIME_TOOLS,
|
||||
offeredTools,
|
||||
realtimeTools,
|
||||
} from '../../server/providers/openai/tools.js';
|
||||
import { GEV_REALTIME_TOOLS } from '../../server/providers/openai/tools.js';
|
||||
import { createDebugLogHandler } from '../../server/providers/openai/debug-log.js';
|
||||
import { realtimeSessionTools } from '../../server/providers/openai/realtime.js';
|
||||
import { attachVoiceResult } from '../voice/speech.js';
|
||||
import { sanitizeDebugValue } from '../voice/realtimeDiagnostics.js';
|
||||
import { standaloneVoiceTools } from '../../server/standalone/voiceTools.js';
|
||||
@@ -199,7 +196,10 @@ test('Realtime handler preserves tools and default instructions, isolates suppli
|
||||
sent.at(-1).session.instructions,
|
||||
realtimeInstructions(guidance),
|
||||
);
|
||||
assert.deepEqual(sent.at(-1).session.tools, realtimeTools());
|
||||
assert.deepEqual(
|
||||
sent.at(-1).session.tools,
|
||||
realtimeSessionTools(GEV_REALTIME_TOOLS),
|
||||
);
|
||||
}
|
||||
assert.notEqual(sent[0].session.instructions, sent[1].session.instructions);
|
||||
assert.equal(sent[0].session.instructions, sent[2].session.instructions);
|
||||
@@ -607,7 +607,10 @@ test('osm_query is offered only when an operator configured Overpass', async (t)
|
||||
assert.doesNotMatch(plain.instructions, /osm_query/);
|
||||
assert.ok(names(plain).includes('resolve_area'));
|
||||
assert.ok(names(plain).includes('find_imagery'));
|
||||
assert.deepEqual(configured.tools, GEV_REALTIME_TOOLS);
|
||||
assert.deepEqual(
|
||||
configured.tools,
|
||||
realtimeSessionTools(GEV_REALTIME_TOOLS, { overpass: true }),
|
||||
);
|
||||
assert.match(configured.instructions, /osm_query/);
|
||||
assert.equal(
|
||||
configured.instructions,
|
||||
@@ -656,7 +659,7 @@ test('Realtime sessions carry supplied tools, and the standalone voice adds the
|
||||
);
|
||||
assert.equal(response.status, 200);
|
||||
// Without an operator Overpass, osm_query is left out of the session.
|
||||
assert.deepEqual(sent.at(-1).session.tools, offeredTools(tools));
|
||||
assert.deepEqual(sent.at(-1).session.tools, realtimeSessionTools(tools));
|
||||
assert.ok(
|
||||
!sent.at(-1).session.tools.some(({ name }) => name === 'osm_query'),
|
||||
);
|
||||
@@ -740,6 +743,26 @@ test('key setup writes only the supplied application root, retains request guard
|
||||
assert.equal(existsSync(path.join(untouched, '.env')), false);
|
||||
});
|
||||
|
||||
test('Realtime sessions leave out the Unicode property patterns OpenAI refuses (#1618)', async (t) => {
|
||||
// OpenAI answers 400 invalid_function_parameters for a whole Realtime session
|
||||
// whose tool schema uses `\p{…}`, as resolve_area's area ids do, so voice
|
||||
// never connects. The session drops only those patterns, as typed chat does.
|
||||
env(t, 'OPENAI_API_KEY', 'fixture-upstream-secret');
|
||||
env(t, 'GEV_RATELIMIT_OPENAI_PER_MIN', undefined);
|
||||
const sent = [];
|
||||
t.mock.method(globalThis, 'fetch', async (_url, options) => {
|
||||
sent.push(JSON.parse(options.body).session);
|
||||
return Response.json({ value: 'fixture-ephemeral' });
|
||||
});
|
||||
const response = await request(
|
||||
install(openAiRealtimeProxy({})).get('/api/realtime/token'),
|
||||
);
|
||||
assert.equal(response.status, 200);
|
||||
const { tools } = sent.at(-1);
|
||||
assert.ok(tools.some(({ name }) => name === 'resolve_area'));
|
||||
assert.doesNotMatch(JSON.stringify(tools), /\\\\[pP]\{/);
|
||||
});
|
||||
|
||||
test('Realtime service configuration selects compatible endpoint/model without forwarding request model IDs or keys', async () => {
|
||||
const handler = install(
|
||||
openAiRealtimeProxy({
|
||||
@@ -757,7 +780,7 @@ test('Realtime service configuration selects compatible endpoint/model without f
|
||||
assert.equal(payload.session.model, 'configured-model');
|
||||
assert.deepEqual(
|
||||
payload.session.tools,
|
||||
realtimeTools({ overpass: false }),
|
||||
realtimeSessionTools(GEV_REALTIME_TOOLS),
|
||||
);
|
||||
return Response.json({ value: 'short-lived-fixture' });
|
||||
},
|
||||
|
||||
@@ -684,25 +684,34 @@ test('the disk cache survives a restart and stays within its bounds', async () =
|
||||
await createNominatimCache({ dir, now: time.now }).get('k0'),
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
fs.statSync(dir).mode & 0o777,
|
||||
0o700,
|
||||
'the query cache directory is private',
|
||||
);
|
||||
// The cache is private only through POSIX mode bits. Windows has none
|
||||
// (directories stat as 0o666) and the cache keeps the checkout's ACLs,
|
||||
// so these checks run, and privacy holds, only on POSIX systems.
|
||||
const posixModes = process.platform !== 'win32';
|
||||
if (posixModes) {
|
||||
assert.equal(
|
||||
fs.statSync(dir).mode & 0o777,
|
||||
0o700,
|
||||
'the query cache directory is private (POSIX modes)',
|
||||
);
|
||||
}
|
||||
const text = fs
|
||||
.readdirSync(dir)
|
||||
.filter((name) => name.endsWith('.json'))
|
||||
.map((name) => fs.readFileSync(path.join(dir, name), 'utf8'))
|
||||
.join('\n');
|
||||
assert.equal(text.includes('k4'), false, 'disk entries never store raw keys');
|
||||
for (const name of fs
|
||||
.readdirSync(dir)
|
||||
.filter((entry) => entry.endsWith('.json')))
|
||||
assert.equal(
|
||||
fs.statSync(path.join(dir, name)).mode & 0o777,
|
||||
0o600,
|
||||
`${name} is private`,
|
||||
);
|
||||
if (posixModes) {
|
||||
for (const name of fs
|
||||
.readdirSync(dir)
|
||||
.filter((entry) => entry.endsWith('.json'))) {
|
||||
assert.equal(
|
||||
fs.statSync(path.join(dir, name)).mode & 0o777,
|
||||
0o600,
|
||||
`${name} is private (POSIX modes)`,
|
||||
);
|
||||
}
|
||||
}
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
|
||||
@@ -11,7 +11,11 @@ import {
|
||||
_routeInflightCountForTest,
|
||||
_setRouteUpstreamIntervalForTest,
|
||||
} from '../../server/providers/places/routes.js';
|
||||
import { ROUTE_STEPS_MAX } from '../../src/data/routeSteps.js';
|
||||
import {
|
||||
ROUTE_STEPS_MAX,
|
||||
normalizeOsrmSteps,
|
||||
} from '../../src/data/routeSteps.js';
|
||||
import * as placePayloads from '../../src/data/placeProviderPayloads.js';
|
||||
import {
|
||||
projectNearbyPlaces,
|
||||
projectTextSearchPlaces,
|
||||
@@ -236,23 +240,108 @@ test('OSRM routing preserves aliases, cache, span guards and upstream failure be
|
||||
assert.equal((await request(route, url)).body.error, error);
|
||||
assert.equal(calls, 1);
|
||||
now += 600001;
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async () => new Response('offline', { status: 503 }),
|
||||
t.mock.method(globalThis, 'fetch', async () =>
|
||||
Response.json(
|
||||
{ code: 'NoRoute', message: 'Impossible route between points' },
|
||||
{ status: 400 },
|
||||
),
|
||||
);
|
||||
assert.deepEqual((await request(route, query)).body, {
|
||||
ok: false,
|
||||
error: 'no route found',
|
||||
});
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async () => new Response('x', { headers: { 'content-type': 'text/html' } }),
|
||||
const noRoute = await request(route, query);
|
||||
assert.equal(noRoute.statusCode, 200);
|
||||
assert.deepEqual(noRoute.body, { ok: false, error: 'no route found' });
|
||||
t.mock.method(globalThis, 'fetch', async () =>
|
||||
Response.json({ code: 'NoRoute', routes: [] }),
|
||||
);
|
||||
assert.equal((await request(route, query)).body.error, 'no route found');
|
||||
});
|
||||
|
||||
test('a routing outage is reported as one, not as "no route found"', async (t) => {
|
||||
const route = '/api/route';
|
||||
const query = '?profile=car&coords=-97,30;-97.01,30.01';
|
||||
for (const response of [
|
||||
() => new Response('offline', { status: 503 }),
|
||||
() => new Response('blocked', { status: 403 }),
|
||||
() => new Response('x', { headers: { 'content-type': 'text/html' } }),
|
||||
() =>
|
||||
new Response('<html>', {
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}),
|
||||
() => Promise.reject(new TypeError('fetch failed')),
|
||||
() => Promise.reject(new DOMException('aborted', 'AbortError')),
|
||||
]) {
|
||||
_resetRouteUpstreamForTest();
|
||||
const request = install(installRouteMiddleware);
|
||||
t.mock.method(globalThis, 'fetch', async () => response());
|
||||
const result = await request(route, query);
|
||||
assert.equal(result.statusCode, 502);
|
||||
assert.deepEqual(result.body, {
|
||||
ok: false,
|
||||
error: 'routing service unavailable',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('callers sharing one failed upstream call all hear the outage, and it is not cached', async (t) => {
|
||||
_resetRouteUpstreamForTest();
|
||||
_setRouteUpstreamIntervalForTest(0);
|
||||
t.after(() => _resetRouteUpstreamForTest());
|
||||
const request = install(installRouteMiddleware);
|
||||
let calls = 0;
|
||||
let release;
|
||||
const gate = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
t.mock.method(globalThis, 'fetch', async () => {
|
||||
calls += 1;
|
||||
await gate;
|
||||
return new Response('offline', { status: 503 });
|
||||
});
|
||||
const route = '/api/route';
|
||||
const query = '?profile=car&coords=-97,30;-97.01,30.01';
|
||||
const all = [
|
||||
request(route, `${query}&steps=1`),
|
||||
request(route, `${query}&steps=1`),
|
||||
request(route, query),
|
||||
];
|
||||
await Promise.resolve();
|
||||
release();
|
||||
for (const result of await Promise.all(all)) {
|
||||
assert.equal(result.statusCode, 502);
|
||||
assert.equal(result.body.error, 'routing service unavailable');
|
||||
}
|
||||
assert.equal(calls, 1);
|
||||
assert.equal((await request(route, query)).statusCode, 502);
|
||||
assert.equal(calls, 2, 'the next request asks the routing service again');
|
||||
});
|
||||
|
||||
test('a budget spent in the outbound queue is reported as busy, not as an outage', async (t) => {
|
||||
_resetRouteUpstreamForTest();
|
||||
// The second request waits 13 s for its slot, past the 12 s budget.
|
||||
_setRouteUpstreamIntervalForTest(13_000);
|
||||
t.after(() => _resetRouteUpstreamForTest());
|
||||
t.mock.timers.enable({ apis: ['setTimeout', 'Date'], now: Date.now() });
|
||||
const request = install(installRouteMiddleware);
|
||||
let calls = 0;
|
||||
t.mock.method(globalThis, 'fetch', async () => {
|
||||
calls += 1;
|
||||
return Response.json(osrmRouteWithSteps(2));
|
||||
});
|
||||
const first = request(
|
||||
'/api/route',
|
||||
'?profile=car&coords=-97,30;-97.01,30.01',
|
||||
);
|
||||
const second = request(
|
||||
'/api/route',
|
||||
'?profile=foot&coords=-97,30;-97.01,30.01',
|
||||
);
|
||||
assert.equal((await first).body.ok, true);
|
||||
t.mock.timers.tick(13_000);
|
||||
const result = await second;
|
||||
assert.equal(result.statusCode, 429);
|
||||
assert.match(result.body.error, /routing busy/);
|
||||
assert.equal(calls, 1, 'the spent request never reaches the routing service');
|
||||
});
|
||||
|
||||
/** One OSRM route with `count` maneuvers, for the step tests. */
|
||||
function osrmRouteWithSteps(count) {
|
||||
return {
|
||||
@@ -380,6 +469,10 @@ test('identical concurrent route requests make one upstream call', async (t) =>
|
||||
);
|
||||
});
|
||||
|
||||
test('the place payloads entry serves the same route steps the dev proxy does', () => {
|
||||
assert.equal(placePayloads.normalizeOsrmSteps, normalizeOsrmSteps);
|
||||
});
|
||||
|
||||
test('a pathological maneuver count is bounded before it reaches the browser', async (t) => {
|
||||
_resetRouteUpstreamForTest();
|
||||
const request = install(installRouteMiddleware);
|
||||
@@ -501,6 +594,25 @@ test('the upstream host is pinned: a redirect is refused, not followed', async (
|
||||
assert.deepEqual(seen, ['error'], 'a redirect would escape the pinned host');
|
||||
});
|
||||
|
||||
test('the routing proxy declines a non-GET before rate limiting or upstream', async (t) => {
|
||||
// A fetch that throws proves the refusal comes before any upstream call.
|
||||
const request = install(installRouteMiddleware);
|
||||
t.mock.method(globalThis, 'fetch', async () => {
|
||||
throw new Error('upstream must not be reached');
|
||||
});
|
||||
for (const method of ['POST', 'PUT', 'DELETE', 'PATCH']) {
|
||||
const response = await request(
|
||||
'/api/route',
|
||||
'?profile=car&coords=-97,30;-97.01,30.01',
|
||||
method,
|
||||
);
|
||||
assert.equal(response.statusCode, 405, method);
|
||||
// RFC 9110 section 15.5.6: a 405 names the methods it accepts.
|
||||
assert.equal(response.headers.allow, 'GET', method);
|
||||
assert.deepEqual(response.body, { error: 'Method Not Allowed' });
|
||||
}
|
||||
});
|
||||
|
||||
test('both paid Google routes refuse cross-site requests before calling Google', async (t) => {
|
||||
const plugin = googlePlacesContextProxy({ resolveApiKey: () => 'key' });
|
||||
const routes = new Map();
|
||||
|
||||
@@ -16,6 +16,8 @@ test('data providers have both hooks; credential editing stays development-only'
|
||||
assert.ok(plugins.some(({ name }) => name === 'gnss-integrity'));
|
||||
assert.ok(plugins.some(({ name }) => name === 'ghostmaps'));
|
||||
assert.ok(plugins.some(({ name }) => name === 'drone-airspace'));
|
||||
assert.ok(plugins.some(({ name }) => name === 'inaturalist'));
|
||||
assert.ok(plugins.some(({ name }) => name === 'acartia'));
|
||||
for (const plugin of plugins) {
|
||||
if (plugin.name === 'gev-key-setup') {
|
||||
assert.equal(plugin.configurePreviewServer, undefined);
|
||||
@@ -114,6 +116,8 @@ test('real dev and built-preview servers serve provider JSON and terminate unkno
|
||||
['/api/fire-perimeters/inciweb/publication/invalid', 400],
|
||||
['/api/gnss-integrity?lat=invalid', 400],
|
||||
['/api/ghostmaps/unknown', 404],
|
||||
['/api/inaturalist/unknown', 404],
|
||||
['/api/acartia/unknown', 404],
|
||||
['/api/terrain/heights?points=invalid', 400],
|
||||
['/api/overpass', 405],
|
||||
['/api/overpass/status', 200],
|
||||
@@ -125,6 +129,9 @@ test('real dev and built-preview servers serve provider JSON and terminate unkno
|
||||
['/api/mapillary/status', 200],
|
||||
['/api/mapillary/tiles/coverage/14/1/2', 503],
|
||||
['/api/radio/unknown', 404],
|
||||
['/api/library/catalog', 200],
|
||||
['/api/library/tile/nasa-black-marble/0/0/0', 404],
|
||||
['/api/library/tile/gvp-holocene-volcanoes/0/0/0', 404],
|
||||
['/api/setup/status', isPreview ? 404 : 200],
|
||||
['/api/setup/update', 404],
|
||||
['/api/does-not-exist', 404],
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
/**
|
||||
* Schedule packs are opt-in build output: the catalog lists a live feed
|
||||
* whose trains are named only through its pack (`routesFrom: 'schedule'`)
|
||||
* once the pack is built (as `hasSchedulePack`, the pack version or the
|
||||
* pack reader tells), every other feed always, and a failing probe never
|
||||
* hides a feed. Without any of them no such feed is listed.
|
||||
*/
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, rm, writeFile, mkdir, utimes } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
SCHEDULE_PACK_PROBE_TTL_MS,
|
||||
createRailService,
|
||||
} from '../sources/railService.js';
|
||||
import {
|
||||
getRegisteredRailFeed,
|
||||
publicRailCatalog,
|
||||
railFeedNeedsSchedulePack,
|
||||
} from '../data/railFeeds.js';
|
||||
import {
|
||||
createFsSchedulePackProbe,
|
||||
createFsSchedulePackVersion,
|
||||
} from '../../server/providers/rail.js';
|
||||
|
||||
const request = (path) => ({
|
||||
url: `https://example.test${path}`,
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
async function catalogIds(service) {
|
||||
const response = await service.handle(request('/api/rail/feeds'));
|
||||
assert.equal(response.status, 200);
|
||||
const { feeds } = await response.json();
|
||||
return new Set(feeds.map((feed) => feed.id));
|
||||
}
|
||||
|
||||
test('which feeds need their schedule pack', () => {
|
||||
// Live GTFS-RT whose vehicles carry no route.
|
||||
for (const id of ['renfe-larga-distancia', 'ktmb'])
|
||||
assert.equal(railFeedNeedsSchedulePack(getRegisteredRailFeed(id)), true);
|
||||
// Live, with routes of its own.
|
||||
assert.equal(
|
||||
railFeedNeedsSchedulePack(getRegisteredRailFeed('mbta-rail')),
|
||||
false,
|
||||
);
|
||||
// Irish Rail's train codes classify its trains without a pack.
|
||||
assert.equal(
|
||||
railFeedNeedsSchedulePack(getRegisteredRailFeed('irish-rail')),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('pack-routed feeds wait for their pack; other feeds stay', async (t) => {
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
throw new Error('no network in tests');
|
||||
},
|
||||
hasSchedulePack: async (feedId) => feedId === 'ktmb',
|
||||
});
|
||||
t.after(() => service.close());
|
||||
const ids = await catalogIds(service);
|
||||
assert.ok(ids.has('ktmb'), 'a built pack-routed feed is listed');
|
||||
assert.ok(!ids.has('renfe-larga-distancia'), 'an unbuilt one waits');
|
||||
assert.ok(ids.has('mbta-rail'), 'live feed is listed');
|
||||
assert.ok(ids.has('irish-rail'), 'Irish Rail is listed');
|
||||
});
|
||||
|
||||
test('with no way to tell, no feed that needs a pack is listed', async (t) => {
|
||||
let calls = 0;
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
calls += 1;
|
||||
throw new Error('no network in tests');
|
||||
},
|
||||
});
|
||||
t.after(() => service.close());
|
||||
const ids = await catalogIds(service);
|
||||
assert.ok(!ids.has('ktmb'), 'a pack-routed feed waits for its pack');
|
||||
assert.ok(!ids.has('renfe-larga-distancia'), 'all of them');
|
||||
assert.ok(ids.has('mbta-rail'), 'live feed is listed');
|
||||
const packless = publicRailCatalog().filter(
|
||||
(feed) => !railFeedNeedsSchedulePack(getRegisteredRailFeed(feed.id)),
|
||||
);
|
||||
assert.equal(ids.size, packless.length);
|
||||
// Nor are its trains or trip updates served, and the operator is not
|
||||
// contacted for them.
|
||||
for (const path of [
|
||||
'/api/rail/trains/renfe-larga-distancia',
|
||||
'/api/rail/trains/ktmb',
|
||||
'/api/rail/updates/renfe-larga-distancia',
|
||||
]) {
|
||||
const response = await service.handle(request(path));
|
||||
if (response.status === 404) continue; // no trip updates route
|
||||
assert.equal(response.status, 503, path);
|
||||
assert.equal(response.headers.get('cache-control'), 'no-store');
|
||||
assert.equal(response.headers.get('X-Rail-Backoff'), 'schedule');
|
||||
assert.ok(Number(response.headers.get('Retry-After')) > 0);
|
||||
assert.equal((await response.json()).error, 'Rail schedule unavailable');
|
||||
}
|
||||
assert.equal(calls, 0);
|
||||
});
|
||||
|
||||
test('a pack reader alone tells what is built, its answers kept for the TTL', async (t) => {
|
||||
let now = 1_000_000;
|
||||
const reads = [];
|
||||
let built = false;
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
throw new Error('no network in tests');
|
||||
},
|
||||
now: () => now,
|
||||
readSchedulePack: async (feedId) => {
|
||||
reads.push(feedId);
|
||||
if (feedId === 'ktmb') return '{}';
|
||||
if (feedId === 'renfe-larga-distancia' && built) {
|
||||
const error = new Error('pack is 30000000 bytes');
|
||||
error.code = 'PACK_TOO_LARGE';
|
||||
throw error;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
});
|
||||
t.after(() => service.close());
|
||||
let ids = await catalogIds(service);
|
||||
assert.ok(ids.has('ktmb'), 'a pack the reader returns is built');
|
||||
assert.ok(
|
||||
!ids.has('renfe-larga-distancia'),
|
||||
'a reader answering null: not built',
|
||||
);
|
||||
assert.ok(ids.has('mbta-rail'));
|
||||
const firstReads = reads.length;
|
||||
built = true;
|
||||
ids = await catalogIds(service);
|
||||
assert.ok(!ids.has('renfe-larga-distancia'), 'kept for the TTL');
|
||||
assert.equal(reads.length, firstReads);
|
||||
now += SCHEDULE_PACK_PROBE_TTL_MS;
|
||||
ids = await catalogIds(service);
|
||||
assert.ok(
|
||||
ids.has('renfe-larga-distancia'),
|
||||
'an oversized pack is built, and refused',
|
||||
);
|
||||
});
|
||||
|
||||
test('a pack version alone tells what is built, without reading packs', async (t) => {
|
||||
let reads = 0;
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
throw new Error('no network in tests');
|
||||
},
|
||||
readSchedulePack: async () => {
|
||||
reads += 1;
|
||||
return '{}';
|
||||
},
|
||||
schedulePackVersion: async (feedId) => (feedId === 'ktmb' ? 'v1' : null),
|
||||
});
|
||||
t.after(() => service.close());
|
||||
const ids = await catalogIds(service);
|
||||
assert.ok(ids.has('ktmb'));
|
||||
assert.ok(!ids.has('renfe-larga-distancia'));
|
||||
assert.equal(reads, 0);
|
||||
});
|
||||
|
||||
test('a failing probe lists the feed; answers are kept for the TTL', async (t) => {
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
let now = 1_000_000;
|
||||
let calls = 0;
|
||||
let built = false;
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
throw new Error('no network in tests');
|
||||
},
|
||||
now: () => now,
|
||||
hasSchedulePack: async (feedId) => {
|
||||
if (feedId === 'renfe-larga-distancia') {
|
||||
calls += 1;
|
||||
return built;
|
||||
}
|
||||
if (feedId === 'ktmb') throw new Error('EACCES');
|
||||
return false;
|
||||
},
|
||||
});
|
||||
t.after(() => service.close());
|
||||
let ids = await catalogIds(service);
|
||||
assert.ok(ids.has('ktmb'), 'probe failure shows the feed');
|
||||
assert.ok(!ids.has('renfe-larga-distancia'));
|
||||
// Built meanwhile: still hidden within the TTL, listed after it.
|
||||
built = true;
|
||||
ids = await catalogIds(service);
|
||||
assert.ok(!ids.has('renfe-larga-distancia'));
|
||||
assert.equal(calls, 1);
|
||||
now += SCHEDULE_PACK_PROBE_TTL_MS;
|
||||
ids = await catalogIds(service);
|
||||
assert.ok(ids.has('renfe-larga-distancia'));
|
||||
assert.equal(calls, 2);
|
||||
});
|
||||
|
||||
test('the fs probe sees only a schedule pack file', async (t) => {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'rail-probe-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
await writeFile(path.join(dir, 'ktmb.schedule.json'), '{}');
|
||||
await writeFile(path.join(dir, 'mbta-rail.json'), '{}');
|
||||
await mkdir(path.join(dir, 'mta-lirr.schedule.json'));
|
||||
const probe = createFsSchedulePackProbe(dir);
|
||||
assert.equal(await probe('ktmb'), true);
|
||||
assert.equal(
|
||||
await probe('mbta-rail'),
|
||||
false,
|
||||
'a path pack alone is not built',
|
||||
);
|
||||
assert.equal(await probe('mta-lirr'), false, 'a directory is not a pack');
|
||||
assert.equal(await probe('../ktmb'), false, 'ids never leave the dir');
|
||||
});
|
||||
|
||||
test('the fs pack version changes when the pack is rebuilt', async (t) => {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'rail-version-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const version = createFsSchedulePackVersion(dir);
|
||||
assert.equal(await version('ktmb'), null, 'no pack, no version');
|
||||
const file = path.join(dir, 'ktmb.schedule.json');
|
||||
await writeFile(file, '{}');
|
||||
await utimes(file, 1_000, 1_000);
|
||||
const first = await version('ktmb');
|
||||
assert.equal(typeof first, 'string');
|
||||
assert.equal(await version('ktmb'), first, 'stable while unchanged');
|
||||
await writeFile(file, '{"trips":{}}');
|
||||
await utimes(file, 2_000, 2_000);
|
||||
assert.notEqual(await version('ktmb'), first);
|
||||
assert.equal(await version('../ktmb'), null, 'ids never leave the dir');
|
||||
});
|
||||
@@ -0,0 +1,877 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { PbfWriter } from 'pbf';
|
||||
import { mkdtemp, rm, utimes, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { Writable } from 'node:stream';
|
||||
import { createRailService } from '../sources/railService.js';
|
||||
import { railEnabledFeeds } from '../data/railFeeds.js';
|
||||
|
||||
import {
|
||||
RAIL_PATH_PACK_MAX_BYTES,
|
||||
railAdmissionGlobalMax,
|
||||
railNaturalRequestsPerMinute,
|
||||
} from '../data/railProxy.js';
|
||||
import {
|
||||
createFsPathPackReader,
|
||||
createFsSchedulePackReader,
|
||||
createRailPackCache,
|
||||
sendRailResponse,
|
||||
} from '../../server/providers/rail.js';
|
||||
|
||||
const request = (path, method = 'GET') => ({
|
||||
url: `https://example.test${path}`,
|
||||
method,
|
||||
});
|
||||
|
||||
/** A full-dataset FeedMessage with one commuter train and one bus. */
|
||||
function mbtaBytes() {
|
||||
const writer = new PbfWriter();
|
||||
const header = new PbfWriter();
|
||||
header.writeStringField(1, '2.0');
|
||||
header.writeVarintField(3, Math.floor(Date.now() / 1000));
|
||||
writer.writeBytesField(1, header.finish());
|
||||
for (const [id, route] of [
|
||||
['cr', 'CR-Providence'],
|
||||
['bus', '1'],
|
||||
]) {
|
||||
const entity = new PbfWriter();
|
||||
entity.writeStringField(1, id);
|
||||
const vehicle = new PbfWriter();
|
||||
const trip = new PbfWriter();
|
||||
trip.writeStringField(5, route);
|
||||
vehicle.writeBytesField(1, trip.finish());
|
||||
const position = new PbfWriter();
|
||||
position.writeFloatField(1, 42.3);
|
||||
position.writeFloatField(2, -71.1);
|
||||
vehicle.writeBytesField(2, position.finish());
|
||||
const descriptor = new PbfWriter();
|
||||
descriptor.writeStringField(1, id);
|
||||
vehicle.writeBytesField(8, descriptor.finish());
|
||||
entity.writeBytesField(4, vehicle.finish());
|
||||
writer.writeBytesField(2, entity.finish());
|
||||
}
|
||||
return writer.finish();
|
||||
}
|
||||
|
||||
test('the service confines requests to registered feeds and GET', async (t) => {
|
||||
let calls = 0;
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
calls++;
|
||||
throw new Error('unexpected');
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
for (const path of [
|
||||
'/other',
|
||||
'/api/rail/trains/unknown',
|
||||
'/api/rail/trains/vre',
|
||||
'/api/rail/trains/https%3A%2F%2Fexample.test',
|
||||
'/api/rail/vehicles/mbta-rail',
|
||||
]) {
|
||||
assert.equal((await service.handle(request(path))).status, 404, path);
|
||||
}
|
||||
for (const method of ['POST', 'HEAD']) {
|
||||
const refused = await service.handle(request('/api/rail/feeds', method));
|
||||
assert.equal(refused.status, 405);
|
||||
// A refusal is never kept by a shared cache.
|
||||
assert.equal(refused.headers.get('cache-control'), 'no-store');
|
||||
assert.equal(refused.headers.get('allow'), 'GET');
|
||||
}
|
||||
const response = await service.handle(request('/api/rail/feeds'));
|
||||
assert.equal(response.status, 200);
|
||||
const { feeds } = await response.json();
|
||||
assert.ok(feeds.some((feed) => feed.id === 'mbta-rail'));
|
||||
assert.ok(feeds.every((feed) => feed.url === undefined));
|
||||
assert.equal(calls, 0);
|
||||
service.close();
|
||||
assert.equal((await service.handle(request('/api/rail/feeds'))).status, 503);
|
||||
});
|
||||
|
||||
test('a snapshot carries trains only and is served from cache while fresh', async (t) => {
|
||||
const urls = [];
|
||||
const service = createRailService({
|
||||
fetchImpl: async (url, init) => {
|
||||
urls.push(url);
|
||||
assert.equal(init.redirect, 'manual');
|
||||
assert.match(init.headers['User-Agent'], /rail-proxy/);
|
||||
return new Response(mbtaBytes(), { status: 200 });
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const first = await service.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(first.status, 200);
|
||||
assert.equal(first.headers.get('X-GEV-Cache'), 'MISS');
|
||||
const body = await first.json();
|
||||
assert.equal(body.count, 1);
|
||||
assert.equal(body.dropped, 1);
|
||||
assert.equal(body.trains[0].category, 'local');
|
||||
const second = await service.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(second.headers.get('X-GEV-Cache'), 'HIT');
|
||||
assert.deepEqual(urls, ['https://cdn.mbta.com/realtime/VehiclePositions.pb']);
|
||||
});
|
||||
|
||||
test('a redirect off the feed origin is refused before it is followed', async (t) => {
|
||||
const urls = [];
|
||||
const service = createRailService({
|
||||
fetchImpl: async (url) => {
|
||||
urls.push(url);
|
||||
return new Response(null, {
|
||||
status: 307,
|
||||
headers: { location: 'https://elsewhere.test/feed.pb' },
|
||||
});
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const response = await service.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(response.status, 504);
|
||||
assert.equal((await response.json()).feedId, 'mbta-rail');
|
||||
assert.ok(Number(response.headers.get('Retry-After')) >= 1);
|
||||
assert.deepEqual(urls, ['https://cdn.mbta.com/realtime/VehiclePositions.pb']);
|
||||
// The cooldown answers the next request without contacting the operator.
|
||||
const again = await service.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(again.status, 503);
|
||||
assert.equal(again.headers.get('X-Rail-Backoff'), 'cooldown');
|
||||
assert.equal(urls.length, 1);
|
||||
});
|
||||
|
||||
test('one document for positions and trip updates is fetched once per window', async (t) => {
|
||||
let now = 1_800_000_000_000;
|
||||
const urls = [];
|
||||
const service = createRailService({
|
||||
now: () => now,
|
||||
fetchImpl: async (url) => {
|
||||
urls.push(url);
|
||||
return new Response(mbtaBytes(), { status: 200 });
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const trains = '/api/rail/trains/mta-lirr';
|
||||
const updates = '/api/rail/updates/mta-lirr';
|
||||
// Both at once share one request; each route keeps its own snapshot.
|
||||
const [a, b] = await Promise.all([
|
||||
service.handle(request(trains)),
|
||||
service.handle(request(updates)),
|
||||
]);
|
||||
assert.equal(a.status, 200);
|
||||
assert.equal(b.status, 200);
|
||||
assert.equal(urls.length, 1);
|
||||
// Positions come round again (15 s); trip updates (30 s) read that body.
|
||||
now += 16_000;
|
||||
await service.handle(request(trains));
|
||||
assert.equal(urls.length, 2);
|
||||
now += 15_000;
|
||||
assert.equal((await service.handle(request(updates))).status, 200);
|
||||
assert.equal(urls.length, 2, 'the body positions fetched 15 s ago');
|
||||
// With nobody reading positions, trip updates fetch on their own window.
|
||||
now += 31_000;
|
||||
await service.handle(request(updates));
|
||||
assert.equal(urls.length, 3);
|
||||
assert.equal(new Set(urls).size, 1, 'the same URL each time');
|
||||
});
|
||||
|
||||
test('an upstream HTTP error answers 502 with a retry hint', async (t) => {
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => new Response('nope', { status: 500 }),
|
||||
});
|
||||
t.after(service.close);
|
||||
const response = await service.handle(request('/api/rail/trains/mta-lirr'));
|
||||
assert.equal(response.status, 502);
|
||||
const body = await response.json();
|
||||
assert.equal(body.error, 'Rail feed unavailable');
|
||||
assert.ok(body.retryInSec >= 1);
|
||||
});
|
||||
|
||||
test('path packs are served from the injected reader for enabled feeds', async (t) => {
|
||||
const asked = [];
|
||||
const pack = JSON.stringify({ version: 1, feedId: 'mta-lirr' });
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
throw new Error('a path request never reaches an operator');
|
||||
},
|
||||
readPathPack: async (feedId, options) => {
|
||||
asked.push([feedId, options.maxBytes]);
|
||||
return feedId === 'mta-lirr' ? pack : null;
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const response = await service.handle(request('/api/rail/paths/mta-lirr'));
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(response.headers.get('cache-control'), 'no-cache');
|
||||
assert.match(response.headers.get('content-type'), /application\/json/);
|
||||
assert.equal(await response.text(), pack);
|
||||
assert.deepEqual(asked, [['mta-lirr', RAIL_PATH_PACK_MAX_BYTES]]);
|
||||
const none = await service.handle(request('/api/rail/paths/mbta-rail'));
|
||||
assert.equal(none.status, 404);
|
||||
assert.equal(none.headers.get('cache-control'), 'no-store');
|
||||
for (const blocked of [
|
||||
'/api/rail/paths/vre',
|
||||
'/api/rail/paths/unknown',
|
||||
'/api/rail/paths/..%2Fsecret',
|
||||
])
|
||||
assert.equal((await service.handle(request(blocked))).status, 404, blocked);
|
||||
assert.equal(asked.length, 2, 'unregistered ids never reach the reader');
|
||||
assert.equal(
|
||||
(await service.handle(request('/api/rail/paths/mta-lirr', 'POST'))).status,
|
||||
405,
|
||||
);
|
||||
});
|
||||
|
||||
test('a missing reader or pack is a 404; an unreadable or oversized pack is a 503', async (t) => {
|
||||
const bare = createRailService();
|
||||
t.after(bare.close);
|
||||
assert.equal(
|
||||
(await bare.handle(request('/api/rail/paths/mta-lirr'))).status,
|
||||
404,
|
||||
);
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
const failing = createRailService({
|
||||
readPathPack: async () => {
|
||||
throw new Error('disk on fire');
|
||||
},
|
||||
});
|
||||
t.after(failing.close);
|
||||
// A reader that fails (EMFILE, EACCES, EIO) is not "no pack": asked again.
|
||||
for (const [kind, error] of [
|
||||
['paths', 'Rail paths unreadable'],
|
||||
['schedule', 'Rail schedule unreadable'],
|
||||
]) {
|
||||
const unreadable = createRailService({
|
||||
[kind === 'paths' ? 'readPathPack' : 'readSchedulePack']: async () => {
|
||||
const fault = new Error('too many open files');
|
||||
fault.code = 'EMFILE';
|
||||
throw fault;
|
||||
},
|
||||
});
|
||||
t.after(unreadable.close);
|
||||
const response = await unreadable.handle(
|
||||
request(`/api/rail/${kind}/mta-lirr`),
|
||||
);
|
||||
assert.equal(response.status, 503, kind);
|
||||
assert.equal(response.headers.get('cache-control'), 'no-store');
|
||||
assert.ok(Number(response.headers.get('Retry-After')) > 0);
|
||||
assert.equal((await response.json()).error, error);
|
||||
}
|
||||
assert.equal(
|
||||
(await failing.handle(request('/api/rail/paths/mta-lirr'))).status,
|
||||
503,
|
||||
);
|
||||
const none = createRailService({ readPathPack: async () => null });
|
||||
t.after(none.close);
|
||||
assert.equal(
|
||||
(await none.handle(request('/api/rail/paths/mta-lirr'))).status,
|
||||
404,
|
||||
);
|
||||
// Over the cap is a build fault, not an unbuilt pack: its own answer,
|
||||
// whether the service measures it or the reader refuses it unread.
|
||||
for (const readPathPack of [
|
||||
async () => new Uint8Array(RAIL_PATH_PACK_MAX_BYTES + 1),
|
||||
async () => {
|
||||
const error = new Error('pack is 30000000 bytes');
|
||||
error.code = 'PACK_TOO_LARGE';
|
||||
throw error;
|
||||
},
|
||||
]) {
|
||||
const huge = createRailService({ readPathPack });
|
||||
t.after(huge.close);
|
||||
const response = await huge.handle(request('/api/rail/paths/mta-lirr'));
|
||||
assert.equal(response.status, 503);
|
||||
assert.equal(response.headers.get('cache-control'), 'no-store');
|
||||
assert.equal((await response.json()).error, 'Rail paths too large');
|
||||
}
|
||||
// At the cap is served, as bytes.
|
||||
const full = createRailService({
|
||||
readPathPack: async () => new Uint8Array(RAIL_PATH_PACK_MAX_BYTES),
|
||||
});
|
||||
t.after(full.close);
|
||||
const served = await full.handle(request('/api/rail/paths/mta-lirr'));
|
||||
assert.equal(served.status, 200);
|
||||
assert.equal(
|
||||
(await served.arrayBuffer()).byteLength,
|
||||
RAIL_PATH_PACK_MAX_BYTES,
|
||||
);
|
||||
});
|
||||
|
||||
test('the fs reader reads <id>.json from its directory and nothing else', async (t) => {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-paths-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
await writeFile(path.join(dir, 'mta-lirr.json'), '{"version":1}');
|
||||
await writeFile(path.join(dir, 'grt-ion.json'), 'x'.repeat(64));
|
||||
const read = createFsPathPackReader(dir);
|
||||
const pack = await read('mta-lirr');
|
||||
assert.ok(pack instanceof Uint8Array, 'bytes, never decoded to text');
|
||||
assert.equal(Buffer.from(pack).toString('utf8'), '{"version":1}');
|
||||
assert.equal(await read('mbta-rail'), null, 'no file');
|
||||
assert.equal(await read('../mta-lirr'), null, 'not an id');
|
||||
assert.equal(await read('MTA-LIRR'), null);
|
||||
await assert.rejects(read('grt-ion', { maxBytes: 10 }), (error) => {
|
||||
assert.match(error.message, /64 bytes/);
|
||||
assert.equal(error.code, 'PACK_TOO_LARGE');
|
||||
return true;
|
||||
});
|
||||
assert.equal((await read('grt-ion', { maxBytes: 64 })).length, 64);
|
||||
});
|
||||
|
||||
test('the fs pack cache reads an unchanged pack once and a rebuilt one again', async (t) => {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-cache-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const file = path.join(dir, 'mta-lirr.json');
|
||||
await writeFile(file, '{"version":1}');
|
||||
const cache = createRailPackCache(40);
|
||||
const read = createFsPathPackReader(dir, { cache });
|
||||
const [first, again] = await Promise.all([
|
||||
read('mta-lirr'),
|
||||
read('mta-lirr'),
|
||||
]);
|
||||
assert.equal(first, again, 'one read shared by two at once');
|
||||
assert.equal(await read('mta-lirr'), first, 'kept while unchanged');
|
||||
assert.equal(cache.bytes(), 13);
|
||||
// A rebuild changes the size or the time: read again, the old copy gone.
|
||||
await writeFile(file, '{"version":22}');
|
||||
await utimes(file, new Date(), new Date(Date.now() + 5_000));
|
||||
const rebuilt = await read('mta-lirr');
|
||||
assert.notEqual(rebuilt, first);
|
||||
assert.equal(Buffer.from(rebuilt).toString('utf8'), '{"version":22}');
|
||||
assert.equal(cache.bytes(), 14);
|
||||
// The byte budget holds: the least recently read pack makes room.
|
||||
await writeFile(path.join(dir, 'mbta-rail.json'), 'x'.repeat(30));
|
||||
await read('mbta-rail');
|
||||
assert.equal(cache.bytes(), 30);
|
||||
// A pack over the whole byte budget is served but never kept.
|
||||
await writeFile(path.join(dir, 'grt-ion.json'), 'y'.repeat(41));
|
||||
assert.equal((await read('grt-ion')).length, 41);
|
||||
assert.equal(cache.bytes(), 30);
|
||||
});
|
||||
|
||||
test('a served pack is streamed to the Node response as bytes, unchanged', async () => {
|
||||
const bytes = new TextEncoder().encode('{"paths":"é"}');
|
||||
const service = createRailService({ readPathPack: async () => bytes });
|
||||
try {
|
||||
const response = await service.handle(request('/api/rail/paths/mta-lirr'));
|
||||
const chunks = [];
|
||||
const head = {};
|
||||
const res = new Writable({
|
||||
write(chunk, encoding, done) {
|
||||
chunks.push(chunk);
|
||||
done();
|
||||
},
|
||||
});
|
||||
res.writeHead = (status, headers) =>
|
||||
Object.assign(head, { status, headers });
|
||||
await sendRailResponse(response, res);
|
||||
assert.equal(head.status, 200);
|
||||
assert.equal(head.headers['cache-control'], 'no-cache');
|
||||
const out = Buffer.concat(chunks);
|
||||
assert.deepEqual(new Uint8Array(out), bytes);
|
||||
// The reader's own buffer goes out: no decode, no copy.
|
||||
assert.equal(chunks.length, 1);
|
||||
assert.equal(chunks[0].buffer, bytes.buffer);
|
||||
} finally {
|
||||
service.close();
|
||||
}
|
||||
});
|
||||
|
||||
/** A TripUpdates FeedMessage with one commuter-rail trip and one bus trip. */
|
||||
function tripUpdateBytes() {
|
||||
const writer = new PbfWriter();
|
||||
const header = new PbfWriter();
|
||||
header.writeStringField(1, '2.0');
|
||||
header.writeVarintField(3, Math.floor(Date.now() / 1000));
|
||||
writer.writeBytesField(1, header.finish());
|
||||
for (const [id, route] of [
|
||||
['cr-trip', 'CR-Providence'],
|
||||
['bus-trip', '1'],
|
||||
]) {
|
||||
const entity = new PbfWriter();
|
||||
entity.writeStringField(1, id);
|
||||
const update = new PbfWriter();
|
||||
const trip = new PbfWriter();
|
||||
trip.writeStringField(1, id);
|
||||
trip.writeStringField(5, route);
|
||||
update.writeBytesField(1, trip.finish());
|
||||
const stop = new PbfWriter();
|
||||
stop.writeStringField(4, 'place-sstat');
|
||||
const arrival = new PbfWriter();
|
||||
arrival.writeVarintField(1, 240);
|
||||
stop.writeBytesField(2, arrival.finish());
|
||||
update.writeBytesField(2, stop.finish());
|
||||
entity.writeBytesField(3, update.finish());
|
||||
writer.writeBytesField(2, entity.finish());
|
||||
}
|
||||
return writer.finish();
|
||||
}
|
||||
|
||||
test('trip updates come from their own URL, cache and filter', async (t) => {
|
||||
const urls = [];
|
||||
const service = createRailService({
|
||||
fetchImpl: async (url) => {
|
||||
urls.push(url);
|
||||
return new Response(
|
||||
url.endsWith('TripUpdates.pb') ? tripUpdateBytes() : mbtaBytes(),
|
||||
{ status: 200 },
|
||||
);
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const updates = await service.handle(request('/api/rail/updates/mbta-rail'));
|
||||
assert.equal(updates.status, 200);
|
||||
assert.equal(updates.headers.get('X-GEV-Cache'), 'MISS');
|
||||
assert.equal(updates.headers.get('cache-control'), 'public, max-age=30');
|
||||
const body = await updates.json();
|
||||
assert.equal(body.count, 1);
|
||||
assert.equal(body.dropped, 1, 'the bus trip never leaves the server');
|
||||
assert.deepEqual(
|
||||
{
|
||||
tripId: body.trips[0].tripId,
|
||||
routeId: body.trips[0].routeId,
|
||||
delaySec: body.trips[0].delaySec,
|
||||
stop: body.trips[0].stops[0],
|
||||
},
|
||||
{
|
||||
tripId: 'cr-trip',
|
||||
routeId: 'CR-Providence',
|
||||
delaySec: 240,
|
||||
stop: {
|
||||
stopId: 'place-sstat',
|
||||
stopSequence: null,
|
||||
arrival: { time: null, delay: 240 },
|
||||
departure: null,
|
||||
},
|
||||
},
|
||||
);
|
||||
// Positions are a separate cache entry: this is a MISS, not the updates.
|
||||
const trains = await service.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(trains.headers.get('X-GEV-Cache'), 'MISS');
|
||||
assert.equal((await trains.json()).trains[0].id, 'cr');
|
||||
const again = await service.handle(request('/api/rail/updates/mbta-rail'));
|
||||
assert.equal(again.headers.get('X-GEV-Cache'), 'HIT');
|
||||
assert.deepEqual(urls, [
|
||||
'https://cdn.mbta.com/realtime/TripUpdates.pb',
|
||||
'https://cdn.mbta.com/realtime/VehiclePositions.pb',
|
||||
]);
|
||||
assert.equal(
|
||||
(await service.handle(request('/api/rail/updates/capmetro-rail'))).status,
|
||||
404,
|
||||
'a feed without trip updates has no /updates route',
|
||||
);
|
||||
});
|
||||
|
||||
test('every train answer comes from the operator, never from a timetable', async (t) => {
|
||||
// A feed once answered `/trains` with an empty list for the timetable to
|
||||
// fill; every feed now reports its trains, so every answer is fetched.
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
const asked = [];
|
||||
const service = createRailService({
|
||||
fetchImpl: async (url) => {
|
||||
asked.push(url);
|
||||
return new Response('', { status: 503 });
|
||||
},
|
||||
hasSchedulePack: async () => true,
|
||||
});
|
||||
t.after(service.close);
|
||||
const feeds = railEnabledFeeds();
|
||||
for (const feed of feeds) {
|
||||
const response = await service.handle(
|
||||
request(`/api/rail/trains/${feed.id}`),
|
||||
);
|
||||
assert.equal(response.status, 502, feed.id);
|
||||
assert.equal(response.headers.get('X-GEV-Cache'), 'NONE', feed.id);
|
||||
}
|
||||
// Feeds that share one positions document are asked once per window.
|
||||
assert.equal(
|
||||
new Set(asked).size,
|
||||
new Set(feeds.map((feed) => feed.url)).size,
|
||||
);
|
||||
});
|
||||
|
||||
test('Irish Rail is fetched as XML and served as event-positioned trains', async (t) => {
|
||||
const xml = `<?xml version="1.0" encoding="utf-8"?>
|
||||
<ArrayOfObjTrainPositions xmlns="http://api.irishrail.ie/realtime/">
|
||||
<objTrainPositions>
|
||||
<TrainStatus>R</TrainStatus>
|
||||
<TrainLatitude>53.35</TrainLatitude>
|
||||
<TrainLongitude>-6.25</TrainLongitude>
|
||||
<TrainCode>A216</TrainCode>
|
||||
<TrainDate>06 Nov 2026</TrainDate>
|
||||
<PublicMessage>A216\\n14:00 - Dublin Heuston to Cork (9 mins late)\\nDeparted Thurles next stop Limerick Junction</PublicMessage>
|
||||
<Direction>To Cork</Direction>
|
||||
</objTrainPositions>
|
||||
</ArrayOfObjTrainPositions>`;
|
||||
const seen = [];
|
||||
const service = createRailService({
|
||||
fetchImpl: async (url, init) => {
|
||||
seen.push([url, init.headers.Accept]);
|
||||
return new Response(xml, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'text/xml; charset=utf-8' },
|
||||
});
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const response = await service.handle(request('/api/rail/trains/irish-rail'));
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(response.headers.get('cache-control'), 'public, max-age=30');
|
||||
const body = await response.json();
|
||||
assert.equal(body.positions, 'event');
|
||||
assert.equal(body.trains[0].id, 'A216');
|
||||
assert.equal(body.trains[0].category, 'national');
|
||||
assert.equal(body.trains[0].positionSource, 'event');
|
||||
// The message's next stop decides the status; it does not leave the server.
|
||||
assert.equal(body.trains[0].status, 'IN_TRANSIT_TO');
|
||||
assert.equal(body.trains[0].nextStop, undefined);
|
||||
// No schedule pack here: the train code alone classifies the train.
|
||||
assert.equal(body.count, 1);
|
||||
assert.deepEqual(seen, [
|
||||
[
|
||||
'https://api.irishrail.ie/realtime/realtime.asmx/getCurrentTrainsXML',
|
||||
'text/xml, application/xml;q=0.9',
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
test('an unreadable Irish Rail document is a 502 with backoff, not a crash', async (t) => {
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => new Response('<html>busy</html>', { status: 200 }),
|
||||
});
|
||||
t.after(service.close);
|
||||
const response = await service.handle(request('/api/rail/trains/irish-rail'));
|
||||
assert.equal(response.status, 502);
|
||||
const again = await service.handle(request('/api/rail/trains/irish-rail'));
|
||||
assert.equal(again.headers.get('X-Rail-Backoff'), 'cooldown');
|
||||
});
|
||||
|
||||
test('an answer names its upstream host, and a failed fetch is a logged 504', async (t) => {
|
||||
const warnings = [];
|
||||
const warn = console.warn;
|
||||
console.warn = (...args) => warnings.push(args.join(' '));
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
const service = createRailService({
|
||||
now: () => 1_800_000_000_000,
|
||||
fetchImpl: async () => new Response(mbtaBytes(), { status: 200 }),
|
||||
});
|
||||
t.after(service.close);
|
||||
const response = await service.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(response.headers.get('X-Rail-Upstream'), 'cdn.mbta.com');
|
||||
assert.equal(response.headers.get('X-Rail-Contact'), '1800000000000');
|
||||
assert.equal(response.headers.get('cache-control'), 'public, max-age=15');
|
||||
const failing = createRailService({
|
||||
fetchImpl: async (url) => {
|
||||
throw new TypeError(`fetch failed for ${url}`);
|
||||
},
|
||||
});
|
||||
t.after(failing.close);
|
||||
const failed = await failing.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(failed.status, 504);
|
||||
assert.equal((await failed.json()).error, 'Rail feed unavailable');
|
||||
assert.ok(
|
||||
warnings.some((line) => line.includes('trains:mbta-rail unavailable')),
|
||||
);
|
||||
});
|
||||
|
||||
test('a redirect on the feed origin is followed, one hop at a time', async (t) => {
|
||||
const urls = [];
|
||||
const service = createRailService({
|
||||
fetchImpl: async (url, init) => {
|
||||
urls.push(url);
|
||||
assert.equal(init.redirect, 'manual');
|
||||
if (urls.length === 1)
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: { location: '/realtime/VehiclePositions.pb?v=2' },
|
||||
});
|
||||
return new Response(mbtaBytes(), { status: 200 });
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const response = await service.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(response.status, 200);
|
||||
assert.deepEqual(urls, [
|
||||
'https://cdn.mbta.com/realtime/VehiclePositions.pb',
|
||||
'https://cdn.mbta.com/realtime/VehiclePositions.pb?v=2',
|
||||
]);
|
||||
assert.equal(response.headers.get('X-Rail-Upstream'), 'cdn.mbta.com');
|
||||
});
|
||||
|
||||
test('schedule packs are served from the injected reader', async (t) => {
|
||||
const asked = [];
|
||||
const pack = JSON.stringify({ version: 1, feedId: 'ktmb' });
|
||||
const service = createRailService({
|
||||
fetchImpl: async () => {
|
||||
throw new Error('a schedule request never reaches an operator');
|
||||
},
|
||||
readSchedulePack: async (feedId, options) => {
|
||||
asked.push([feedId, options.maxBytes]);
|
||||
return feedId === 'mbta-rail' ? null : pack;
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
for (const id of ['ktmb', 'irish-rail']) {
|
||||
const response = await service.handle(request(`/api/rail/schedule/${id}`));
|
||||
assert.equal(response.status, 200, id);
|
||||
assert.equal(response.headers.get('cache-control'), 'no-cache');
|
||||
assert.equal(await response.text(), pack);
|
||||
}
|
||||
const none = await service.handle(request('/api/rail/schedule/mbta-rail'));
|
||||
assert.equal(none.status, 404);
|
||||
assert.equal((await none.json()).error, 'No rail schedule');
|
||||
for (const blocked of [
|
||||
'/api/rail/schedule/vre',
|
||||
'/api/rail/schedule/unknown',
|
||||
'/api/rail/schedule/..%2Fktmb',
|
||||
])
|
||||
assert.equal((await service.handle(request(blocked))).status, 404, blocked);
|
||||
assert.deepEqual(asked, [
|
||||
['ktmb', RAIL_PATH_PACK_MAX_BYTES],
|
||||
['irish-rail', RAIL_PATH_PACK_MAX_BYTES],
|
||||
['mbta-rail', RAIL_PATH_PACK_MAX_BYTES],
|
||||
]);
|
||||
// No reader: 404; an oversized pack: 503, as for path packs.
|
||||
const bare = createRailService();
|
||||
t.after(bare.close);
|
||||
assert.equal(
|
||||
(await bare.handle(request('/api/rail/schedule/ktmb'))).status,
|
||||
404,
|
||||
);
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
const huge = createRailService({
|
||||
readSchedulePack: async () => new Uint8Array(RAIL_PATH_PACK_MAX_BYTES + 1),
|
||||
});
|
||||
t.after(huge.close);
|
||||
const refused = await huge.handle(request('/api/rail/schedule/ktmb'));
|
||||
assert.equal(refused.status, 503);
|
||||
assert.equal((await refused.json()).error, 'Rail schedule too large');
|
||||
// The path reader is not asked for a schedule, nor the reverse.
|
||||
const paths = createRailService({ readPathPack: async () => pack });
|
||||
t.after(paths.close);
|
||||
assert.equal(
|
||||
(await paths.handle(request('/api/rail/schedule/ktmb'))).status,
|
||||
404,
|
||||
);
|
||||
});
|
||||
|
||||
test('the fs schedule reader reads <id>.schedule.json beside the path packs', async (t) => {
|
||||
const dir = await mkdtemp(path.join(os.tmpdir(), 'gev-rail-schedule-test-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
await writeFile(path.join(dir, 'ktmb.schedule.json'), '{"version":1}');
|
||||
await writeFile(path.join(dir, 'ktmb.json'), '{"paths":true}');
|
||||
const read = createFsSchedulePackReader(dir);
|
||||
const text = async (pending) => Buffer.from(await pending).toString('utf8');
|
||||
assert.equal(await text(read('ktmb')), '{"version":1}');
|
||||
assert.equal(await read('irish-rail'), null);
|
||||
assert.equal(await read('../ktmb'), null);
|
||||
await assert.rejects(read('ktmb', { maxBytes: 3 }), /13 bytes/);
|
||||
assert.equal(
|
||||
await text(createFsPathPackReader(dir)('ktmb')),
|
||||
'{"paths":true}',
|
||||
);
|
||||
});
|
||||
|
||||
test('a viewer of every region at once is not refused by the global cap', async (t) => {
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
const service = createRailService({
|
||||
now: () => 1_800_000_000_000,
|
||||
fetchImpl: async () => new Response(mbtaBytes(), { status: 200 }),
|
||||
});
|
||||
t.after(service.close);
|
||||
const { feeds } = await (
|
||||
await service.handle(request('/api/rail/feeds'))
|
||||
).json();
|
||||
const backoffs = [];
|
||||
let requests = 0;
|
||||
for (const feed of feeds) {
|
||||
for (const route of feed.tripUpdates ? ['trains', 'updates'] : ['trains']) {
|
||||
requests += 1;
|
||||
const response = await service.handle(
|
||||
request(`/api/rail/${route}/${feed.id}`),
|
||||
);
|
||||
backoffs.push(response.headers.get('X-Rail-Backoff'));
|
||||
}
|
||||
}
|
||||
assert.ok(requests > 40, `${requests} resources, more than the old cap`);
|
||||
assert.ok(!backoffs.includes('rate-limited'), 'the global cap held back');
|
||||
assert.ok(railAdmissionGlobalMax() >= railNaturalRequestsPerMinute());
|
||||
});
|
||||
|
||||
/** Positions with a trip id but no route, as Renfe's long-distance feed sends. */
|
||||
function routelessBytes(trips) {
|
||||
const writer = new PbfWriter();
|
||||
const header = new PbfWriter();
|
||||
header.writeStringField(1, '2.0');
|
||||
header.writeVarintField(3, Math.floor(Date.now() / 1000) - 3600);
|
||||
writer.writeBytesField(1, header.finish());
|
||||
for (const tripId of trips) {
|
||||
const entity = new PbfWriter();
|
||||
entity.writeStringField(1, tripId);
|
||||
const vehicle = new PbfWriter();
|
||||
const trip = new PbfWriter();
|
||||
trip.writeStringField(1, tripId);
|
||||
vehicle.writeBytesField(1, trip.finish());
|
||||
const position = new PbfWriter();
|
||||
position.writeFloatField(1, 40.4);
|
||||
position.writeFloatField(2, -3.7);
|
||||
vehicle.writeBytesField(2, position.finish());
|
||||
entity.writeBytesField(4, vehicle.finish());
|
||||
writer.writeBytesField(2, entity.finish());
|
||||
}
|
||||
return writer.finish();
|
||||
}
|
||||
|
||||
test('routes missing from a feed are read from its schedule pack, once an hour', async (t) => {
|
||||
let now = 1_800_000_000_000;
|
||||
const reads = [];
|
||||
const service = createRailService({
|
||||
now: () => now,
|
||||
fetchImpl: async () =>
|
||||
new Response(routelessBytes(['0419212026-11-06', '9999912026-11-06']), {
|
||||
status: 200,
|
||||
}),
|
||||
readSchedulePack: async (feedId, options) => {
|
||||
reads.push([feedId, options.maxBytes]);
|
||||
return JSON.stringify({
|
||||
version: 1,
|
||||
feedId: 'renfe-larga-distancia',
|
||||
trips: {
|
||||
'0419212026-11-06': { route: 'ALVIA~X~1700037606GL023' },
|
||||
other: { route: 'MD~S~1' },
|
||||
},
|
||||
});
|
||||
},
|
||||
hasSchedulePack: async () => true,
|
||||
});
|
||||
t.after(service.close);
|
||||
const path = '/api/rail/trains/renfe-larga-distancia';
|
||||
const body = await (await service.handle(request(path))).json();
|
||||
assert.equal(body.count, 1);
|
||||
assert.equal(body.dropped, 1, 'a trip the pack does not know');
|
||||
assert.equal(body.trains[0].category, 'national');
|
||||
assert.equal(body.trains[0].timestampSource, 'fetch');
|
||||
now += 20_000;
|
||||
await service.handle(request(path));
|
||||
assert.deepEqual(reads, [
|
||||
['renfe-larga-distancia', RAIL_PATH_PACK_MAX_BYTES],
|
||||
]);
|
||||
now += 61 * 60_000;
|
||||
await service.handle(request(path));
|
||||
assert.equal(reads.length, 2, 're-read after an hour');
|
||||
});
|
||||
|
||||
test('a rebuilt schedule pack replaces the lookups within a minute', async (t) => {
|
||||
let now = 1_800_000_000_000;
|
||||
let version = 'built-1';
|
||||
let route = 'ALVIA~X~1';
|
||||
let reads = 0;
|
||||
let versionAsks = 0;
|
||||
const service = createRailService({
|
||||
now: () => now,
|
||||
fetchImpl: async () =>
|
||||
new Response(routelessBytes(['0419212026-11-06']), { status: 200 }),
|
||||
readSchedulePack: async () => {
|
||||
reads += 1;
|
||||
return JSON.stringify({
|
||||
version: 1,
|
||||
feedId: 'renfe-larga-distancia',
|
||||
trips: { '0419212026-11-06': { route } },
|
||||
});
|
||||
},
|
||||
hasSchedulePack: async () => true,
|
||||
schedulePackVersion: async () => {
|
||||
versionAsks += 1;
|
||||
return version;
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const path = '/api/rail/trains/renfe-larga-distancia';
|
||||
const routeNow = async () =>
|
||||
(await (await service.handle(request(path))).json()).trains[0].routeId;
|
||||
assert.equal(await routeNow(), 'ALVIA~X~1');
|
||||
assert.equal(reads, 1);
|
||||
// Unchanged: kept, and the version is not asked on every poll.
|
||||
now += 61_000;
|
||||
assert.equal(await routeNow(), 'ALVIA~X~1');
|
||||
assert.equal(reads, 1);
|
||||
const asks = versionAsks;
|
||||
now += 20_000;
|
||||
await routeNow();
|
||||
assert.equal(versionAsks, asks);
|
||||
// Rebuilt: the next check after the minute reads the new pack.
|
||||
version = 'built-2';
|
||||
route = 'AVE~X~2';
|
||||
now += 61_000;
|
||||
await routeNow();
|
||||
now += 16_000;
|
||||
assert.equal(await routeNow(), 'AVE~X~2');
|
||||
assert.equal(reads, 2, 'read once more, well inside the hour');
|
||||
});
|
||||
|
||||
test('without a schedule pack, a feed that needs it is unavailable and the pack retried later', async (t) => {
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
let now = 1_800_000_000_000;
|
||||
let reads = 0;
|
||||
const service = createRailService({
|
||||
now: () => now,
|
||||
fetchImpl: async () =>
|
||||
new Response(routelessBytes(['0419212026-11-06']), { status: 200 }),
|
||||
readSchedulePack: async () => {
|
||||
reads += 1;
|
||||
return null;
|
||||
},
|
||||
// Listed (the probe says built), but the pack is gone when read.
|
||||
hasSchedulePack: async () => true,
|
||||
});
|
||||
t.after(service.close);
|
||||
const path = '/api/rail/trains/renfe-larga-distancia';
|
||||
// Every train needs the pack for its route: not a healthy empty feed.
|
||||
const response = await service.handle(request(path));
|
||||
assert.equal(response.status, 503);
|
||||
assert.equal(response.headers.get('X-Rail-Backoff'), 'schedule');
|
||||
assert.equal(response.headers.get('cache-control'), 'no-store');
|
||||
assert.equal((await response.json()).error, 'Rail schedule unavailable');
|
||||
now += 20_000;
|
||||
await service.handle(request(path));
|
||||
assert.equal(reads, 1, 'not re-read on every poll');
|
||||
now += 6 * 60_000;
|
||||
await service.handle(request(path));
|
||||
assert.equal(reads, 2, 'retried after five minutes');
|
||||
// A feed that carries its own routes never asks for the pack.
|
||||
let asked = 0;
|
||||
const own = createRailService({
|
||||
fetchImpl: async () => new Response(mbtaBytes(), { status: 200 }),
|
||||
readSchedulePack: async () => {
|
||||
asked += 1;
|
||||
return null;
|
||||
},
|
||||
});
|
||||
t.after(own.close);
|
||||
await own.handle(request('/api/rail/trains/mbta-rail'));
|
||||
assert.equal(asked, 0);
|
||||
});
|
||||
@@ -0,0 +1,214 @@
|
||||
/**
|
||||
* The rail service under upstream faults and missing build output: an empty
|
||||
* or non-protobuf 200, a pack-routed feed without its pack, and a
|
||||
* downstream cache near the end of a window.
|
||||
|
||||
*/
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { PbfWriter } from 'pbf';
|
||||
import { createRailService } from '../sources/railService.js';
|
||||
import { RAIL_FEED_REGISTRY, getRailFeed } from '../data/railFeeds.js';
|
||||
|
||||
const request = (path) => ({
|
||||
url: `https://example.test${path}`,
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
function quietWarnings(t) {
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
}
|
||||
|
||||
/** A full-dataset VehiclePositions message: one MBTA commuter train. */
|
||||
function positionsBytes() {
|
||||
const writer = new PbfWriter();
|
||||
const header = new PbfWriter();
|
||||
header.writeStringField(1, '2.0');
|
||||
header.writeVarintField(3, 1_800_000_000);
|
||||
writer.writeBytesField(1, header.finish());
|
||||
const entity = new PbfWriter();
|
||||
entity.writeStringField(1, 'cr');
|
||||
const vehicle = new PbfWriter();
|
||||
const trip = new PbfWriter();
|
||||
trip.writeStringField(5, 'CR-Providence');
|
||||
vehicle.writeBytesField(1, trip.finish());
|
||||
const position = new PbfWriter();
|
||||
position.writeFloatField(1, 42.3);
|
||||
position.writeFloatField(2, -71.1);
|
||||
vehicle.writeBytesField(2, position.finish());
|
||||
const descriptor = new PbfWriter();
|
||||
descriptor.writeStringField(1, 'cr');
|
||||
vehicle.writeBytesField(8, descriptor.finish());
|
||||
entity.writeBytesField(4, vehicle.finish());
|
||||
writer.writeBytesField(2, entity.finish());
|
||||
return writer.finish();
|
||||
}
|
||||
|
||||
/** A full-dataset TripUpdates message with the given trip ids. */
|
||||
function tripUpdatesBytes(tripIds) {
|
||||
const writer = new PbfWriter();
|
||||
const header = new PbfWriter();
|
||||
header.writeStringField(1, '2.0');
|
||||
header.writeVarintField(3, 1_800_000_000);
|
||||
writer.writeBytesField(1, header.finish());
|
||||
for (const tripId of tripIds) {
|
||||
const descriptor = new PbfWriter();
|
||||
descriptor.writeStringField(1, tripId);
|
||||
descriptor.writeStringField(3, '20270115');
|
||||
const update = new PbfWriter();
|
||||
update.writeBytesField(1, descriptor.finish());
|
||||
update.writeVarintField(5, 60);
|
||||
const entity = new PbfWriter();
|
||||
entity.writeStringField(1, tripId);
|
||||
entity.writeBytesField(3, update.finish());
|
||||
writer.writeBytesField(2, entity.finish());
|
||||
}
|
||||
return writer.finish();
|
||||
}
|
||||
|
||||
test('an empty 200 or 204 never replaces the last good snapshot', async (t) => {
|
||||
quietWarnings(t);
|
||||
let now = 1_800_000_000_000;
|
||||
let answer = () => new Response(positionsBytes(), { status: 200 });
|
||||
let calls = 0;
|
||||
const service = createRailService({
|
||||
now: () => now,
|
||||
fetchImpl: async () => {
|
||||
calls += 1;
|
||||
return answer();
|
||||
},
|
||||
});
|
||||
t.after(service.close);
|
||||
const path = '/api/rail/trains/mbta-rail';
|
||||
const good = await service.handle(request(path));
|
||||
assert.equal(good.headers.get('X-GEV-Cache'), 'MISS');
|
||||
const goodBody = await good.text();
|
||||
for (const empty of [
|
||||
() => new Response(new Uint8Array(0), { status: 200 }),
|
||||
() => new Response(null, { status: 204 }),
|
||||
]) {
|
||||
answer = empty;
|
||||
now += 16_000;
|
||||
const stale = await service.handle(request(path));
|
||||
assert.equal(stale.status, 200);
|
||||
assert.equal(stale.headers.get('X-GEV-Cache'), 'STALE-ERROR');
|
||||
assert.equal(await stale.text(), goodBody, 'the good snapshot, kept');
|
||||
// The failure is on the ladder: the next ask does not reach upstream.
|
||||
const before = calls;
|
||||
const held = await service.handle(request(path));
|
||||
assert.equal(held.headers.get('X-GEV-Cache'), 'STALE-ERROR');
|
||||
assert.equal(calls, before);
|
||||
}
|
||||
// A healthy answer takes over again.
|
||||
answer = () => new Response(positionsBytes(), { status: 200 });
|
||||
now += 61_000;
|
||||
const back = await service.handle(request(path));
|
||||
assert.equal(back.headers.get('X-GEV-Cache'), 'MISS');
|
||||
assert.equal((await back.json()).count, 1);
|
||||
});
|
||||
|
||||
test('an error page served as 200 is an upstream fault (502), not a timeout', async (t) => {
|
||||
quietWarnings(t);
|
||||
for (const [route, body] of [
|
||||
['trains', '<!DOCTYPE html><html><body>Service Unavailable</body></html>'],
|
||||
['trains', '{"error":"quota exceeded"}'],
|
||||
['updates', '<html>maintenance</html>'],
|
||||
['updates', ''],
|
||||
]) {
|
||||
const service = createRailService({
|
||||
fetchImpl: async () =>
|
||||
new Response(body, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'text/html' },
|
||||
}),
|
||||
});
|
||||
const response = await service.handle(
|
||||
request(`/api/rail/${route}/mbta-rail`),
|
||||
);
|
||||
service.close();
|
||||
assert.equal(response.status, 502, `${route}: ${body}`);
|
||||
assert.equal((await response.json()).error, 'Rail feed unavailable');
|
||||
}
|
||||
});
|
||||
|
||||
test('a pack-routed feed names its trip updates from the pack, and is unavailable without it', async (t) => {
|
||||
quietWarnings(t);
|
||||
// Every pack-routed feed with trip updates (Renfe long distance): its
|
||||
// updates carry no route, so they are classified by the pack's trips.
|
||||
const feeds = RAIL_FEED_REGISTRY.filter(
|
||||
(feed) =>
|
||||
feed.routesFrom === 'schedule' && typeof feed.tripUpdatesUrl === 'string',
|
||||
);
|
||||
assert.ok(feeds.length >= 1, 'Renfe long distance at least');
|
||||
for (const registered of feeds) {
|
||||
const id = registered.id;
|
||||
if (!getRailFeed(id)) continue;
|
||||
const service = createRailService({
|
||||
fetchImpl: async () =>
|
||||
new Response(tripUpdatesBytes(['in-pack', 'elsewhere']), {
|
||||
status: 200,
|
||||
}),
|
||||
readSchedulePack: async (feedId) =>
|
||||
feedId === id
|
||||
? JSON.stringify({
|
||||
version: 1,
|
||||
feedId: id,
|
||||
trips: { 'in-pack': { route: 'ALVIA~X~1700037606GL023' } },
|
||||
})
|
||||
: null,
|
||||
hasSchedulePack: async () => true,
|
||||
});
|
||||
try {
|
||||
const response = await service.handle(request(`/api/rail/updates/${id}`));
|
||||
assert.equal(response.status, 200, id);
|
||||
const body = await response.json();
|
||||
assert.deepEqual(
|
||||
body.trips.map((trip) => [trip.tripId, trip.routeId]),
|
||||
[['in-pack', 'ALVIA~X~1700037606GL023']],
|
||||
id,
|
||||
);
|
||||
assert.equal(body.dropped, 1, `${id}: the trip the pack does not know`);
|
||||
assert.equal(body.entityCount, 2, id);
|
||||
assert.equal(body.skipped, undefined, 'every trip is decoded');
|
||||
} finally {
|
||||
service.close();
|
||||
}
|
||||
// Listed, but the pack is gone when read: unavailable, not empty.
|
||||
const bare = createRailService({
|
||||
fetchImpl: async () =>
|
||||
new Response(tripUpdatesBytes(['in-pack', 'elsewhere']), {
|
||||
status: 200,
|
||||
}),
|
||||
readSchedulePack: async () => null,
|
||||
hasSchedulePack: async () => true,
|
||||
});
|
||||
try {
|
||||
const response = await bare.handle(request(`/api/rail/updates/${id}`));
|
||||
assert.equal(response.status, 503, id);
|
||||
assert.equal((await response.json()).error, 'Rail schedule unavailable');
|
||||
} finally {
|
||||
bare.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('a HIT is cached downstream only for what is left of its window', async (t) => {
|
||||
let now = 1_800_000_000_000;
|
||||
const service = createRailService({
|
||||
now: () => now,
|
||||
fetchImpl: async () => new Response(positionsBytes(), { status: 200 }),
|
||||
});
|
||||
t.after(service.close);
|
||||
const path = '/api/rail/trains/mbta-rail';
|
||||
const maxAge = async () =>
|
||||
(await service.handle(request(path))).headers.get('cache-control');
|
||||
assert.equal(await maxAge(), 'public, max-age=15');
|
||||
now += 10_000;
|
||||
assert.equal(await maxAge(), 'public, max-age=5');
|
||||
now += 4_500;
|
||||
assert.equal(await maxAge(), 'public, max-age=1');
|
||||
});
|
||||
@@ -0,0 +1,337 @@
|
||||
// The schedule pack a feed's live trains are read with, and how packs are
|
||||
// served: a pack of another format or feed, or whose window has ended, is
|
||||
// refused; a pack-routed feed whose every record is dropped answers 503,
|
||||
// not an empty list; a train named by its number keeps its run while its
|
||||
// report says it is late; packs go out no-cache, with their version as
|
||||
// their ETag.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { Writable } from 'node:stream';
|
||||
import { PbfWriter } from 'pbf';
|
||||
import { createRailService } from '../sources/railService.js';
|
||||
import { railProxy } from '../../server/providers/rail.js';
|
||||
|
||||
/** 2027-01-15T08:00:00Z. */
|
||||
const NOW = 1_800_000_000_000;
|
||||
const MIN = 60_000;
|
||||
const LIVE_TRIP = '0419212026-11-06';
|
||||
|
||||
const request = (path, headers = undefined) => ({
|
||||
url: `https://example.test${path}`,
|
||||
method: 'GET',
|
||||
...(headers ? { headers } : {}),
|
||||
});
|
||||
|
||||
function quietWarnings(t) {
|
||||
const warn = console.warn;
|
||||
console.warn = () => {};
|
||||
t.after(() => {
|
||||
console.warn = warn;
|
||||
});
|
||||
}
|
||||
|
||||
/** Positions with a trip id but no route, as Renfe's long-distance feed sends. */
|
||||
function routelessBytes(trips) {
|
||||
const writer = new PbfWriter();
|
||||
const header = new PbfWriter();
|
||||
header.writeStringField(1, '2.0');
|
||||
header.writeVarintField(3, Math.floor(NOW / 1000));
|
||||
writer.writeBytesField(1, header.finish());
|
||||
for (const tripId of trips) {
|
||||
const entity = new PbfWriter();
|
||||
entity.writeStringField(1, tripId);
|
||||
const vehicle = new PbfWriter();
|
||||
const trip = new PbfWriter();
|
||||
trip.writeStringField(1, tripId);
|
||||
vehicle.writeBytesField(1, trip.finish());
|
||||
const position = new PbfWriter();
|
||||
position.writeFloatField(1, 40.4);
|
||||
position.writeFloatField(2, -3.7);
|
||||
vehicle.writeBytesField(2, position.finish());
|
||||
entity.writeBytesField(4, vehicle.finish());
|
||||
writer.writeBytesField(2, entity.finish());
|
||||
}
|
||||
return writer.finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* A Renfe long-distance schedule pack whose trips run on 15 January 2027
|
||||
* (Madrid, UTC+1 then) from `startSec` local for `durationSec`.
|
||||
*/
|
||||
function renfePack({
|
||||
version = 1,
|
||||
feedId = 'renfe-larga-distancia',
|
||||
trips = [LIVE_TRIP],
|
||||
startSec = 9 * 3600,
|
||||
durationSec = 3600,
|
||||
} = {}) {
|
||||
return JSON.stringify({
|
||||
version,
|
||||
feedId,
|
||||
timezone: 'Europe/Madrid',
|
||||
window: { from: '2027-01-14', to: '2027-01-15' },
|
||||
profiles: [[0, 0, durationSec, durationSec]],
|
||||
services: { S: ['2027-01-15'] },
|
||||
trips: Object.fromEntries(
|
||||
trips.map((tripId) => [
|
||||
tripId,
|
||||
{
|
||||
route: 'ALVIA~X~1700037606GL023',
|
||||
pattern: 0,
|
||||
profile: 0,
|
||||
startSec,
|
||||
service: 'S',
|
||||
},
|
||||
]),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
function renfeService(t, readSchedulePack, clock = () => NOW) {
|
||||
const service = createRailService({
|
||||
now: clock,
|
||||
fetchImpl: async () =>
|
||||
new Response(routelessBytes([LIVE_TRIP]), { status: 200 }),
|
||||
readSchedulePack,
|
||||
hasSchedulePack: async () => true,
|
||||
});
|
||||
t.after(service.close);
|
||||
return service;
|
||||
}
|
||||
|
||||
const TRAINS = '/api/rail/trains/renfe-larga-distancia';
|
||||
|
||||
async function assertScheduleUnavailable(response, label) {
|
||||
assert.equal(response.status, 503, label);
|
||||
assert.equal(response.headers.get('X-Rail-Backoff'), 'schedule', label);
|
||||
assert.equal(response.headers.get('cache-control'), 'no-store', label);
|
||||
assert.equal(
|
||||
(await response.json()).error,
|
||||
'Rail schedule unavailable',
|
||||
label,
|
||||
);
|
||||
}
|
||||
|
||||
test('a pack that names none of the live trips is unavailable, not an empty feed', async (t) => {
|
||||
quietWarnings(t);
|
||||
// A pack built for other days: Renfe's trip ids carry their date.
|
||||
const service = renfeService(t, async () =>
|
||||
renfePack({ trips: ['0419212026-11-05'] }),
|
||||
);
|
||||
await assertScheduleUnavailable(await service.handle(request(TRAINS)));
|
||||
});
|
||||
|
||||
test('a pack of another format, another feed or a window that has ended is refused', async (t) => {
|
||||
quietWarnings(t);
|
||||
// The same live train, the same trip in each pack: only the pack differs.
|
||||
const good = renfeService(t, async () => renfePack());
|
||||
const served = await (await good.handle(request(TRAINS))).json();
|
||||
assert.equal(served.count, 1);
|
||||
assert.equal(served.trains[0].routeId, 'ALVIA~X~1700037606GL023');
|
||||
for (const [label, pack] of [
|
||||
['format version 2', renfePack({ version: 2 })],
|
||||
['another feed', renfePack({ feedId: 'ktmb' })],
|
||||
// 04:00 to 05:00 UTC: over three hours before now.
|
||||
['window ended', renfePack({ startSec: 5 * 3600 })],
|
||||
]) {
|
||||
const service = renfeService(t, async () => pack);
|
||||
await assertScheduleUnavailable(
|
||||
await service.handle(request(TRAINS)),
|
||||
label,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('a kept pack is refused once its window ends, and read again later', async (t) => {
|
||||
quietWarnings(t);
|
||||
let now = NOW;
|
||||
let reads = 0;
|
||||
// Its one run: 07:30 to 08:20 UTC.
|
||||
const service = renfeService(
|
||||
t,
|
||||
async () => {
|
||||
reads += 1;
|
||||
return renfePack({ startSec: 8.5 * 3600, durationSec: 50 * 60 });
|
||||
},
|
||||
() => now,
|
||||
);
|
||||
assert.equal((await service.handle(request(TRAINS))).status, 200);
|
||||
now += 21 * MIN;
|
||||
await assertScheduleUnavailable(await service.handle(request(TRAINS)));
|
||||
assert.equal(reads, 1, 'refused as kept, not read again');
|
||||
now += 6 * MIN;
|
||||
await service.handle(request(TRAINS));
|
||||
assert.equal(reads, 2, 'read again after five minutes');
|
||||
});
|
||||
|
||||
/** Irish Rail's running trains, one train with its stated delay. */
|
||||
function irishXml(minutesLate) {
|
||||
return `<?xml version="1.0" encoding="utf-8"?>
|
||||
<ArrayOfObjTrainPositions xmlns="http://api.irishrail.ie/realtime/">
|
||||
<objTrainPositions>
|
||||
<TrainStatus>R</TrainStatus><TrainLatitude>52.6</TrainLatitude>
|
||||
<TrainLongitude>-6.4</TrainLongitude><TrainCode>A610</TrainCode>
|
||||
<TrainDate>15 Jan 2027</TrainDate>
|
||||
<PublicMessage>A610\\n07:00 - Dublin Connolly to Rosslare Europort (${minutesLate} mins late)\\nDeparted Enniscorthy next stop Wexford</PublicMessage>
|
||||
<Direction>Southbound</Direction>
|
||||
</objTrainPositions>
|
||||
</ArrayOfObjTrainPositions>`;
|
||||
}
|
||||
|
||||
/** A610's run ends 07:45 UTC (Dublin is on UTC in January); A700 runs later. */
|
||||
const IRISH_PACK = JSON.stringify({
|
||||
version: 1,
|
||||
feedId: 'irish-rail',
|
||||
timezone: 'Europe/Dublin',
|
||||
window: { from: '2027-01-14', to: '2027-01-15' },
|
||||
profiles: [[0, 0, 2700, 2700]],
|
||||
services: { S: ['2027-01-15'] },
|
||||
trips: {
|
||||
'6024_8435': {
|
||||
name: 'A610',
|
||||
route: 'A~X~DUB-EUROPORT-I',
|
||||
pattern: 0,
|
||||
profile: 0,
|
||||
startSec: 7 * 3600,
|
||||
service: 'S',
|
||||
},
|
||||
'6024_9000': {
|
||||
name: 'A700',
|
||||
route: 'A~X~DUB-EUROPORT-I',
|
||||
pattern: 0,
|
||||
profile: 0,
|
||||
startSec: 10 * 3600,
|
||||
service: 'S',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
test('a late Irish Rail train keeps its run past its timetabled end', async (t) => {
|
||||
// Now is 15 minutes after the run's timetabled end, past the 10 minutes
|
||||
// a run is looked for around its times.
|
||||
const tripOf = async (minutesLate) => {
|
||||
const service = createRailService({
|
||||
now: () => NOW,
|
||||
fetchImpl: async () =>
|
||||
new Response(irishXml(minutesLate), { status: 200 }),
|
||||
readSchedulePack: async () => IRISH_PACK,
|
||||
});
|
||||
try {
|
||||
const body = await (
|
||||
await service.handle(request('/api/rail/trains/irish-rail'))
|
||||
).json();
|
||||
return body.trains[0];
|
||||
} finally {
|
||||
service.close();
|
||||
}
|
||||
};
|
||||
const late = await tripOf(16);
|
||||
assert.equal(late.tripId, '6024_8435', '16 minutes late: still on its run');
|
||||
assert.equal(late.trainNumber, 'A610');
|
||||
assert.equal(late.delaySec, 960);
|
||||
const onTime = await tripOf(0);
|
||||
assert.equal(onTime.tripId, 'A610', 'on time: that run is over');
|
||||
assert.equal(onTime.trainNumber, undefined);
|
||||
});
|
||||
|
||||
test('packs are served no-cache with their version as ETag, and 304 when unchanged', async (t) => {
|
||||
let pathVersion = '1731340000000.5:120';
|
||||
let reads = 0;
|
||||
const service = createRailService({
|
||||
readPathPack: async () => {
|
||||
reads += 1;
|
||||
return '{"paths":1}';
|
||||
},
|
||||
pathPackVersion: async () => pathVersion,
|
||||
readSchedulePack: async () => '{"schedule":1}',
|
||||
schedulePackVersion: async () => 'built-7',
|
||||
});
|
||||
t.after(service.close);
|
||||
const path = '/api/rail/paths/mta-lirr';
|
||||
const first = await service.handle(request(path));
|
||||
assert.equal(first.status, 200);
|
||||
assert.equal(first.headers.get('cache-control'), 'no-cache');
|
||||
const etag = first.headers.get('etag');
|
||||
assert.equal(etag, `"${encodeURIComponent(pathVersion)}"`);
|
||||
assert.equal(await first.text(), '{"paths":1}');
|
||||
// Asked again with its tag: 304, and the pack is not read.
|
||||
for (const headers of [
|
||||
{ 'if-none-match': etag },
|
||||
new Headers({ 'If-None-Match': `W/${etag}, "other"` }),
|
||||
]) {
|
||||
const again = await service.handle(request(path, headers));
|
||||
assert.equal(again.status, 304);
|
||||
assert.equal(again.headers.get('etag'), etag);
|
||||
assert.equal(again.headers.get('cache-control'), 'no-cache');
|
||||
assert.equal(await again.text(), '');
|
||||
}
|
||||
assert.equal(reads, 1);
|
||||
// Rebuilt: the old tag gets the new pack.
|
||||
pathVersion = '1731340099000:140';
|
||||
const rebuilt = await service.handle(
|
||||
request(path, { 'if-none-match': etag }),
|
||||
);
|
||||
assert.equal(rebuilt.status, 200);
|
||||
assert.notEqual(rebuilt.headers.get('etag'), etag);
|
||||
assert.equal(reads, 2);
|
||||
// The schedule pack carries its own version.
|
||||
const schedule = await service.handle(request('/api/rail/schedule/mta-lirr'));
|
||||
assert.equal(schedule.headers.get('etag'), '"built-7"');
|
||||
assert.equal(
|
||||
(
|
||||
await service.handle(
|
||||
request('/api/rail/schedule/mta-lirr', {
|
||||
'if-none-match': '"built-7"',
|
||||
}),
|
||||
)
|
||||
).status,
|
||||
304,
|
||||
);
|
||||
// Without a version there is no tag: no-cache, read each time.
|
||||
const bare = createRailService({ readPathPack: async () => '{}' });
|
||||
t.after(bare.close);
|
||||
const plain = await bare.handle(request(path, { 'if-none-match': '*' }));
|
||||
assert.equal(plain.status, 200);
|
||||
assert.equal(plain.headers.get('etag'), null);
|
||||
assert.equal(plain.headers.get('cache-control'), 'no-cache');
|
||||
});
|
||||
|
||||
test('the development server passes If-None-Match to the pack routes', async (t) => {
|
||||
const plugin = railProxy({
|
||||
readPathPack: async () => '{"paths":1}',
|
||||
pathPackVersion: async () => 'v1',
|
||||
});
|
||||
let handler = null;
|
||||
const server = {
|
||||
middlewares: {
|
||||
use(prefix, fn) {
|
||||
assert.equal(prefix, '/api/rail');
|
||||
handler = fn;
|
||||
},
|
||||
},
|
||||
};
|
||||
plugin.configureServer(server);
|
||||
t.after(() => plugin.closeBundle());
|
||||
const answer = async (headers) => {
|
||||
const head = {};
|
||||
const chunks = [];
|
||||
const res = new Writable({
|
||||
write(chunk, encoding, done) {
|
||||
chunks.push(chunk);
|
||||
done();
|
||||
},
|
||||
});
|
||||
res.writeHead = (status, sent) => Object.assign(head, { status, sent });
|
||||
const ended = new Promise((resolve) => res.on('finish', resolve));
|
||||
await handler({ url: '/paths/mta-lirr', method: 'GET', headers }, res);
|
||||
await ended;
|
||||
return { ...head, body: Buffer.concat(chunks).toString('utf8') };
|
||||
};
|
||||
const first = await answer({});
|
||||
assert.equal(first.status, 200);
|
||||
assert.equal(first.sent.etag, '"v1"');
|
||||
assert.equal(first.body, '{"paths":1}');
|
||||
const again = await answer({ 'if-none-match': '"v1"' });
|
||||
assert.equal(again.status, 304);
|
||||
assert.equal(again.body, '');
|
||||
});
|
||||
@@ -0,0 +1,131 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { gunzipSync } from 'node:zlib';
|
||||
import {
|
||||
acceptsGzip,
|
||||
encodedBody,
|
||||
gzipBody,
|
||||
} from '../../server/providers/common/compress.js';
|
||||
|
||||
/** A request shaped like the one Node hands a middleware. */
|
||||
function request(acceptEncoding) {
|
||||
return {
|
||||
url: '/api/flights',
|
||||
method: 'GET',
|
||||
headers:
|
||||
acceptEncoding === undefined ? {} : { 'accept-encoding': acceptEncoding },
|
||||
};
|
||||
}
|
||||
|
||||
/** Repetitive JSON shaped like a snapshot body. */
|
||||
function snapshotBody(states = 200) {
|
||||
return JSON.stringify({
|
||||
time: 1700000000,
|
||||
states: Array.from({ length: states }, (_, index) => [
|
||||
`abc${index}`,
|
||||
'CALLSIGN',
|
||||
'United States',
|
||||
1700000000,
|
||||
30.1,
|
||||
-97.7,
|
||||
10000,
|
||||
]),
|
||||
});
|
||||
}
|
||||
|
||||
/** Seeded high-entropy bytes, so the "gzip made it bigger" check is stable. */
|
||||
function incompressibleBytes(length) {
|
||||
const bytes = Buffer.alloc(length);
|
||||
let state = 123456789;
|
||||
for (let index = 0; index < length; index += 1) {
|
||||
state = (state * 1103515245 + 12345) % 2147483648;
|
||||
bytes[index] = (state >>> 16) & 0xff;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
test('accept-encoding is read the way clients actually write it', () => {
|
||||
assert.equal(acceptsGzip(request('gzip')), true);
|
||||
assert.equal(acceptsGzip(request('gzip, deflate, br')), true);
|
||||
assert.equal(acceptsGzip(request('br;q=1.0, gzip;q=0.8')), true);
|
||||
assert.equal(acceptsGzip(request('GZIP')), true);
|
||||
assert.equal(acceptsGzip(request(' gzip ')), true);
|
||||
|
||||
assert.equal(acceptsGzip(request('br, deflate')), false);
|
||||
assert.equal(acceptsGzip(request('identity')), false);
|
||||
assert.equal(acceptsGzip(request('')), false);
|
||||
assert.equal(acceptsGzip(request(undefined)), false);
|
||||
assert.equal(acceptsGzip({}), false);
|
||||
assert.equal(acceptsGzip(undefined), false);
|
||||
|
||||
// `q=0` names gzip and refuses it (RFC 9110 section 12.4.2).
|
||||
assert.equal(acceptsGzip(request('gzip;q=0')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.000')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.001')), true);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1')), true);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1.000')), true);
|
||||
|
||||
// A q that is not a whole `qvalue` is unreadable and must not round into
|
||||
// acceptance.
|
||||
assert.equal(acceptsGzip(request('gzip;q=bogus')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1oops')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.5abc')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=2')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=1.5')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=0.0001')), false);
|
||||
assert.equal(acceptsGzip(request('gzip;q=')), false);
|
||||
|
||||
// `gzip` is not matched as a substring of a neighbouring token.
|
||||
assert.equal(acceptsGzip(request('x-gzip')), false);
|
||||
assert.equal(acceptsGzip(request('gzipped')), false);
|
||||
});
|
||||
|
||||
test('a body is only compressed when compression pays for itself', async () => {
|
||||
assert.equal(await gzipBody(''), null);
|
||||
assert.equal(
|
||||
await gzipBody('{"states":[]}'),
|
||||
null,
|
||||
'below the frame threshold',
|
||||
);
|
||||
assert.equal(await gzipBody(Buffer.alloc(1023, 'a')), null, 'one byte short');
|
||||
|
||||
const identity = snapshotBody();
|
||||
assert.ok(identity.length > 1024, 'fixture is worth compressing');
|
||||
const compressed = await gzipBody(identity);
|
||||
assert.ok(Buffer.isBuffer(compressed));
|
||||
assert.ok(compressed.byteLength < Buffer.byteLength(identity));
|
||||
assert.equal(gunzipSync(compressed).toString(), identity);
|
||||
|
||||
// Buffers are accepted as-is; data gzip cannot shrink stays uncompressed.
|
||||
assert.ok((await gzipBody(Buffer.from(identity))) !== null);
|
||||
assert.equal(await gzipBody(incompressibleBytes(4096)), null);
|
||||
});
|
||||
|
||||
test('the encoded body and the header that names it are chosen together', async () => {
|
||||
const identity = snapshotBody();
|
||||
const compressed = await gzipBody(identity);
|
||||
|
||||
const negotiated = encodedBody(request('gzip'), identity, compressed);
|
||||
assert.equal(negotiated.body, compressed);
|
||||
assert.equal(negotiated.headers['Content-Encoding'], 'gzip');
|
||||
assert.equal(negotiated.headers.Vary, 'Accept-Encoding');
|
||||
assert.equal(gunzipSync(negotiated.body).toString(), identity);
|
||||
|
||||
// Offered but refused: identity bytes and no Content-Encoding.
|
||||
const refused = encodedBody(request('br'), identity, compressed);
|
||||
assert.equal(refused.body, identity);
|
||||
assert.equal(refused.headers['Content-Encoding'], undefined);
|
||||
assert.equal(refused.headers.Vary, 'Accept-Encoding');
|
||||
|
||||
// Wanted but not available: same answer.
|
||||
const unavailable = encodedBody(request('gzip'), identity, null);
|
||||
assert.equal(unavailable.body, identity);
|
||||
assert.equal(unavailable.headers['Content-Encoding'], undefined);
|
||||
|
||||
// Vary is on both branches, so a shared cache cannot serve the wrong encoding.
|
||||
assert.equal(unavailable.headers.Vary, 'Accept-Encoding');
|
||||
assert.equal(
|
||||
encodedBody(request(undefined), identity, compressed).headers.Vary,
|
||||
'Accept-Encoding',
|
||||
);
|
||||
});
|
||||
@@ -303,6 +303,96 @@ test('Space Missions reads the active catalog through the real proxy while unkno
|
||||
assert.deepEqual(fetched, ['active'], 'rejected groups never reach upstream');
|
||||
});
|
||||
|
||||
test('SATCAT is proxied per core group for a day, separately from TLEs, and served stale on failure', async (t) => {
|
||||
isolateDisk(t);
|
||||
let now = Date.now();
|
||||
t.mock.method(Date, 'now', () => now);
|
||||
t.mock.method(console, 'warn', () => {});
|
||||
const csv =
|
||||
'OBJECT_NAME,OBJECT_ID,NORAD_CAT_ID,OBJECT_TYPE,OPS_STATUS_CODE,OWNER\nSL-16 R/B,1992-093B,22285,R/B,,CIS\n';
|
||||
const fetched = [];
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
const parsed = new URL(url);
|
||||
fetched.push(`${parsed.pathname}?${parsed.searchParams.get('GROUP')}`);
|
||||
return new Response(csv);
|
||||
});
|
||||
const written = [];
|
||||
t.mock.method(fsp, 'writeFile', async (file) => {
|
||||
written.push(String(file).split(/[\\/]/).pop());
|
||||
});
|
||||
const request = install(celestrakProxy());
|
||||
for (const group of ['/starlink', '/active', '/../visual', '/nope'])
|
||||
assert.equal(
|
||||
(await request('/api/celestrak-satcat', group)).status,
|
||||
400,
|
||||
group,
|
||||
);
|
||||
assert.deepEqual(fetched, [], 'rejected groups never reach upstream');
|
||||
|
||||
const first = await request('/api/celestrak-satcat', '/visual');
|
||||
assert.equal(first.status, 200);
|
||||
assert.equal(first.body, csv);
|
||||
assert.equal(first.headers['x-satcat-cache'], 'MISS');
|
||||
assert.deepEqual(fetched, ['/satcat/records.php?visual']);
|
||||
assert.deepEqual(
|
||||
written,
|
||||
['celestrak-satcat-visual.json'],
|
||||
'its own disk cache',
|
||||
);
|
||||
|
||||
now += 23 * 3600_000;
|
||||
assert.equal(
|
||||
(await request('/api/celestrak-satcat', '/visual')).headers[
|
||||
'x-satcat-cache'
|
||||
],
|
||||
'HIT',
|
||||
'a day-long cache, unlike the 6 h TLEs',
|
||||
);
|
||||
now += 2 * 3600_000;
|
||||
t.mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async () => new Response('<html>busy</html>'),
|
||||
);
|
||||
const stale = await request('/api/celestrak-satcat', '/visual');
|
||||
assert.equal(stale.body, csv, 'an error page never replaces good records');
|
||||
assert.equal(stale.headers['x-satcat-cache'], 'STALE-ERROR');
|
||||
});
|
||||
|
||||
test('a SATCAT failure never holds off the TLEs, but CelesTrak throttling holds off both', async (t) => {
|
||||
isolateDisk(t);
|
||||
t.mock.method(console, 'warn', () => {});
|
||||
const tle =
|
||||
'ISS (ZARYA)\n1 25544U 98067A 24001.00000000 .00016717 00000-0 10270-3 0 9001\n2 25544 51.6416 247.4627 0006703 130.5360 325.0288 15.50000000 04\n';
|
||||
let satcatAnswer = () => new Response('<html>not csv</html>');
|
||||
const fetched = [];
|
||||
t.mock.method(globalThis, 'fetch', async (url) => {
|
||||
const parsed = new URL(url);
|
||||
fetched.push(parsed.pathname);
|
||||
return parsed.pathname.startsWith('/satcat/')
|
||||
? satcatAnswer()
|
||||
: new Response(tle);
|
||||
});
|
||||
const request = install(celestrakProxy());
|
||||
assert.equal((await request('/api/celestrak-satcat', '/sar')).status, 502);
|
||||
const starlink = await request('/api/celestrak', '/starlink');
|
||||
assert.equal(starlink.status, 200, 'the TLE feed is not cooling down');
|
||||
assert.equal(starlink.body, tle);
|
||||
|
||||
// A 429 is CelesTrak throttling this client: every feed waits it out.
|
||||
satcatAnswer = () =>
|
||||
new Response('slow down', {
|
||||
status: 429,
|
||||
headers: { 'Retry-After': '120' },
|
||||
});
|
||||
const throttled = install(celestrakProxy());
|
||||
await throttled('/api/celestrak-satcat', '/visual');
|
||||
const before = fetched.length;
|
||||
const held = await throttled('/api/celestrak', '/geo');
|
||||
assert.equal(held.status, 502);
|
||||
assert.equal(fetched.length, before, 'nothing went upstream');
|
||||
});
|
||||
|
||||
test('CelesTrak admits a same-origin request behind the documented reverse proxy and refuses foreign origins', async (t) => {
|
||||
isolateDisk(t);
|
||||
let calls = 0;
|
||||
|
||||
@@ -21,7 +21,10 @@ export const ARRIVAL_MAX_ATTEMPTS = 2;
|
||||
* The settle deadline starts when moveEnd is forced, before its rotation
|
||||
* baseline; the reveal deadline restarts at the settle frame, which is where
|
||||
* the contact is hidden. `read` snapshots visibility and rotation error
|
||||
* against the current position.
|
||||
* against the current position. A hidden sample carrying `revealStartsNow`
|
||||
* (a frame drew the contact hidden and it was just shown again) restarts the
|
||||
* reveal's clock and deadline there: the frames spent drawing it hidden are
|
||||
* the probe's setup, reported as `hiddenMs`, not part of the reveal.
|
||||
*/
|
||||
export function observeArrivalRotation({
|
||||
scene,
|
||||
@@ -37,6 +40,9 @@ export function observeArrivalRotation({
|
||||
let phaseStartedAt = startedAt;
|
||||
let moveEnd = null;
|
||||
let moveEndMs = null;
|
||||
// Where the reveal is measured from: the settle frame, or the frame a
|
||||
// `revealStartsNow` sample marks.
|
||||
let revealFromMs = null;
|
||||
let revealFrames = 0;
|
||||
let done = false;
|
||||
let remove;
|
||||
@@ -85,12 +91,17 @@ export function observeArrivalRotation({
|
||||
if (moveEnd === null) {
|
||||
moveEnd = sample;
|
||||
moveEndMs = elapsedMs;
|
||||
revealFromMs = elapsedMs;
|
||||
phaseStartedAt = at;
|
||||
armDeadline();
|
||||
startReveal();
|
||||
} else {
|
||||
revealFrames++;
|
||||
if (sample.shown) {
|
||||
if (!sample.shown && sample.revealStartsNow) {
|
||||
revealFromMs = elapsedMs;
|
||||
phaseStartedAt = at;
|
||||
armDeadline();
|
||||
} else if (sample.shown) {
|
||||
// Keep the FIRST visible result, including a wrong angle. Never
|
||||
// wait for a subsequent tick to repair a failed reveal.
|
||||
finish({
|
||||
@@ -99,7 +110,8 @@ export function observeArrivalRotation({
|
||||
shown: sample.shown,
|
||||
flipFrame: revealFrames,
|
||||
moveEndMs,
|
||||
revealMs: elapsedMs - moveEndMs,
|
||||
revealMs: elapsedMs - revealFromMs,
|
||||
hiddenMs: revealFromMs - moveEndMs,
|
||||
elapsedMs,
|
||||
});
|
||||
return;
|
||||
|
||||
@@ -88,6 +88,45 @@ test('80 ms fleet scheduling passes after more than four fast rendered frames',
|
||||
assert.equal(f.resources.moveEnds, 1);
|
||||
});
|
||||
|
||||
test('a reveal is measured from the frame that drew the contact hidden', async () => {
|
||||
const f = fixture();
|
||||
f.frame(8, { shown: true, rotationError: 0 });
|
||||
// The hidden frame waits behind a slow swap; the probe then shows it again.
|
||||
f.frame(300, { shown: false, revealStartsNow: true });
|
||||
f.frame(390, { shown: true, rotationError: 0, revealStartsNow: false });
|
||||
const result = await f.observation;
|
||||
assert.equal(result.flipFrame, 2);
|
||||
assert.equal(result.moveEndMs, 8);
|
||||
assert.equal(result.hiddenMs, 292);
|
||||
assert.equal(result.revealMs, 90);
|
||||
assert.equal(result.elapsedMs, 390);
|
||||
assert.equal(arrivalRotationPassed(result), true);
|
||||
assert.equal(f.resources.timers, 0);
|
||||
});
|
||||
|
||||
test('the reveal deadline restarts at the frame that drew the contact hidden', async () => {
|
||||
const f = fixture();
|
||||
f.frame(8, { shown: true, rotationError: 0 });
|
||||
f.frame(400, { shown: false, revealStartsNow: true });
|
||||
// 500 ms after the settle frame, but only 200 ms into the reveal.
|
||||
f.expire(600);
|
||||
f.frame(850, { shown: true, rotationError: 0, revealStartsNow: false });
|
||||
const result = await f.observation;
|
||||
assert.equal(result.error, undefined);
|
||||
assert.equal(result.revealMs, 450);
|
||||
assert.equal(arrivalRotationPassed(result), true);
|
||||
});
|
||||
|
||||
test('a reveal without a hidden-frame mark is measured from the settle frame', async () => {
|
||||
const f = fixture();
|
||||
f.frame(8, { shown: true, rotationError: 0 });
|
||||
f.frame(16, { shown: false });
|
||||
f.frame(96, { shown: true, rotationError: 0 });
|
||||
const result = await f.observation;
|
||||
assert.equal(result.revealMs, 88);
|
||||
assert.equal(result.hiddenMs, 0);
|
||||
});
|
||||
|
||||
test('a wrong first visible angle fails even if the next frame corrects it', async () => {
|
||||
const f = fixture();
|
||||
f.frame(8, { shown: true, rotationError: 0 });
|
||||
|
||||
@@ -397,6 +397,28 @@ test('sensor sampler waits for postRender and reads framebuffer pixels; stopped
|
||||
}),
|
||||
}),
|
||||
};
|
||||
// The marker record (fresh with each state read, as the fixture's
|
||||
// wrappers are), and the billboard the draw port drew for the latest one
|
||||
// in the layer's still set.
|
||||
let marker = null;
|
||||
const billboard = {
|
||||
id: { set: 'transit:1:still', id: 'bus' },
|
||||
show: true,
|
||||
color: { alpha: 1 },
|
||||
rotation: 0,
|
||||
get position() {
|
||||
return marker.position;
|
||||
},
|
||||
};
|
||||
scene.primitives = {
|
||||
length: 1,
|
||||
get: () => ({
|
||||
drawSetId: 'transit:1:still',
|
||||
show: true,
|
||||
length: 1,
|
||||
get: () => billboard,
|
||||
}),
|
||||
};
|
||||
globalThis.window = {
|
||||
__godsEyeView: {
|
||||
viewer: { scene },
|
||||
@@ -406,22 +428,22 @@ test('sensor sampler waits for postRender and reads framebuffer pixels; stopped
|
||||
'transit',
|
||||
{
|
||||
module: {
|
||||
_transitStateForTest: () => ({
|
||||
_vehicles: new Map([
|
||||
[
|
||||
'bus',
|
||||
{
|
||||
key: 'bus',
|
||||
mode: 'bus',
|
||||
marker: {
|
||||
show: true,
|
||||
image: 'raster',
|
||||
position: { x: 0, y: 0, z: 0 },
|
||||
},
|
||||
},
|
||||
],
|
||||
]),
|
||||
}),
|
||||
_transitSetIdsForTest: () => [
|
||||
'transit:1:still',
|
||||
'transit:1:moving',
|
||||
],
|
||||
_transitStateForTest: () => {
|
||||
marker = {
|
||||
show: true,
|
||||
image: 'raster',
|
||||
position: { x: 0, y: 0, z: 0 },
|
||||
};
|
||||
return {
|
||||
_vehicles: new Map([
|
||||
['bus', { key: 'bus', mode: 'bus', marker }],
|
||||
]),
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
@@ -603,8 +625,30 @@ test('preset baseline clears a previous city selection before comparing fleet si
|
||||
_selectedKey: entry.key,
|
||||
_vehicles: new Map([[entry.key, entry]]),
|
||||
};
|
||||
// The billboard the draw port drew for the marker, styled as it is.
|
||||
const billboard = {
|
||||
id: { set: 'transit:1:still', id: entry.key },
|
||||
get width() {
|
||||
return entry.marker.width;
|
||||
},
|
||||
get color() {
|
||||
return entry.marker.color;
|
||||
},
|
||||
};
|
||||
globalThis.window = {
|
||||
__godsEyeView: {
|
||||
viewer: {
|
||||
scene: {
|
||||
primitives: {
|
||||
length: 1,
|
||||
get: () => ({
|
||||
drawSetId: 'transit:1:still',
|
||||
length: 1,
|
||||
get: () => billboard,
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
dataManager: {
|
||||
layers: new Map([
|
||||
[
|
||||
@@ -612,6 +656,7 @@ test('preset baseline clears a previous city selection before comparing fleet si
|
||||
{
|
||||
module: {
|
||||
_transitStateForTest: () => state,
|
||||
_transitSetIdsForTest: () => ['transit:1:still'],
|
||||
_transitPartsForTest: () => ({
|
||||
selection: {
|
||||
clearSelection() {
|
||||
@@ -673,12 +718,15 @@ test('public transit documentation uses courtesy attribution and the approved gr
|
||||
new URL('../../DATA_SOURCES.md', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
// The Transit layer's own rows; the Rail layer's rows for the same
|
||||
// operators quote their licences.
|
||||
const rows = data
|
||||
.split('\n')
|
||||
.filter((line) =>
|
||||
/^\| \*\*(MBTA|CapMetro|Metro Transit|OVapi|Entur|TransLink|HSL)/.test(
|
||||
line,
|
||||
),
|
||||
.filter(
|
||||
(line) =>
|
||||
/^\| \*\*(MBTA|CapMetro|Metro Transit|OVapi|Entur|TransLink|HSL)/.test(
|
||||
line,
|
||||
) && /^\| [^|]+\| Transit layer /.test(line),
|
||||
);
|
||||
assert.equal(rows.length, 7);
|
||||
for (const row of rows) {
|
||||
@@ -695,7 +743,7 @@ test('public transit documentation uses courtesy attribution and the approved gr
|
||||
);
|
||||
assert.ok(
|
||||
readme.includes(
|
||||
'Twenty-one layers and map sources. **Nineteen have a keyless path.**',
|
||||
'Twenty-three layers and map sources. **Twenty-one have a keyless path.**',
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
|
||||
@@ -145,7 +145,7 @@ test('root config retains existing named exports and standalone provider order',
|
||||
assert.equal(compatibility[name], value, name);
|
||||
const config = standaloneConfig({ mode: 'test' }, { nominatimStorageDir });
|
||||
const standaloneProviders = config.plugins
|
||||
.slice(4, -6)
|
||||
.slice(4, -7)
|
||||
.map((plugin) => plugin.name);
|
||||
assert.equal(
|
||||
standaloneProviders.filter((name) => name === 'gemini-live-proxy').length,
|
||||
@@ -161,10 +161,12 @@ test('root config retains existing named exports and standalone provider order',
|
||||
.localProviderPlugins({ nominatimStorageDir })
|
||||
.map((plugin) => plugin.name),
|
||||
);
|
||||
assert.equal(config.plugins.at(-7).name, 'gev-key-setup');
|
||||
assert.equal(config.plugins.at(-8).name, 'gev-key-setup');
|
||||
// Token fallback follows real providers and local MCP before the API 404.
|
||||
assert.equal(config.plugins.at(-6).name, 'local-mcp');
|
||||
assert.equal(config.plugins.at(-5).name, 'google-token-unavailable');
|
||||
assert.equal(config.plugins.at(-7).name, 'local-mcp');
|
||||
assert.equal(config.plugins.at(-6).name, 'google-token-unavailable');
|
||||
// Dev-only, under its own path.
|
||||
assert.equal(config.plugins.at(-5).name, 'conformance-references');
|
||||
assert.equal(config.plugins.at(-4).name, 'api-not-found');
|
||||
// Config-only, so it sits after the provider slice rather than in it.
|
||||
assert.equal(config.plugins.at(-3).name, 'gev-unwatched-runtime-dirs');
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
// The Wikidata places pack is written by scripts/wikidataPlaceTiles.mjs. Its
|
||||
// rows come from a Wikidata query that cannot be repeated offline, so this
|
||||
// checks the steps after it: the shipped rows, sorted and filtered as the
|
||||
// build does, give back every shipped file byte for byte, and hash to the
|
||||
// value the pack's README records.
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { readdir, readFile } from 'node:fs/promises';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
keepBuiltIfUnchanged,
|
||||
keepPackBuiltIfUnchanged,
|
||||
placeTileFiles,
|
||||
tileKey,
|
||||
withoutCities,
|
||||
withoutNearTwins,
|
||||
} from '../../scripts/wikidataPlaceTiles.mjs';
|
||||
|
||||
const PACK = new URL('../data/local_data/wikidata_places/', import.meta.url);
|
||||
const CITIES = new URL(
|
||||
'../data/local_data/wikidata_cities/cities.json',
|
||||
import.meta.url,
|
||||
);
|
||||
|
||||
test('a tile is named by its south-west corner', () => {
|
||||
assert.equal(tileKey(30.343, -97.557), '30_-100');
|
||||
assert.equal(tileKey(-0.5, 0), '-5_0');
|
||||
assert.equal(tileKey(10.682, -71.036), '10_-75');
|
||||
// In the tiles the lookups read: the top row, and -180 for 180.
|
||||
assert.equal(tileKey(90, 180), '85_-180');
|
||||
});
|
||||
|
||||
test('re-tiles the shipped rows deterministically', async () => {
|
||||
const index = JSON.parse(await readFile(new URL('index.json', PACK), 'utf8'));
|
||||
const rows = [];
|
||||
for (const key of Object.keys(index.tiles)) {
|
||||
const tile = JSON.parse(
|
||||
await readFile(new URL(`${key}.json`, PACK), 'utf8'),
|
||||
);
|
||||
rows.push(...tile.rows);
|
||||
}
|
||||
assert.equal(rows.length, 137662);
|
||||
// As scripts/build-wikidata-cities.mjs orders and filters its rows.
|
||||
rows.sort((a, b) => b[3] - a[3] || a[5].localeCompare(b[5]));
|
||||
const cityIds = JSON.parse(await readFile(CITIES, 'utf8')).rows.map(
|
||||
(row) => row[5],
|
||||
);
|
||||
const kept = withoutNearTwins(withoutCities(rows, cityIds));
|
||||
assert.equal(
|
||||
kept.length,
|
||||
rows.length,
|
||||
'the shipped rows are already filtered',
|
||||
);
|
||||
const readme = await readFile(new URL('README.md', PACK), 'utf8');
|
||||
const digest = createHash('sha256')
|
||||
.update(JSON.stringify(kept))
|
||||
.digest('hex');
|
||||
assert.ok(readme.includes(`\`${digest}\``), 'README rows hash');
|
||||
const files = placeTileFiles(kept, index.meta);
|
||||
const shipped = (await readdir(PACK)).filter((name) => name !== 'README.md');
|
||||
assert.deepEqual([...files.keys()].sort(), shipped.sort());
|
||||
for (const [name, text] of files)
|
||||
assert.equal(text, await readFile(new URL(name, PACK), 'utf8'), name);
|
||||
});
|
||||
|
||||
test('a place the cities pack holds is left out of the tiles', () => {
|
||||
const rows = [
|
||||
['Palmares', 8.1, -82.1, 11000, 'CR', 'Q1542741'],
|
||||
['Smallville', 8.2, -82.2, 2000, 'CR', 'Q1'],
|
||||
];
|
||||
assert.deepEqual(
|
||||
withoutCities(rows, ['Q1542741']).map((row) => row[5]),
|
||||
['Q1'],
|
||||
);
|
||||
});
|
||||
|
||||
test('one place listed twice nearby keeps its larger listing', () => {
|
||||
const rows = [
|
||||
['Orange', 41.278, -73.026, 14280, 'US', 'Q1'],
|
||||
['Orange', 41.28, -73.02, 13950, 'US', 'Q2'],
|
||||
// Across a border: a namesake, kept.
|
||||
['Orange', 41.281, -73.03, 3000, 'CA', 'Q3'],
|
||||
// Far away: another place.
|
||||
['Orange', 33.79, -117.85, 2000, 'US', 'Q4'],
|
||||
['Zábřeh', 49.88, 16.87, 13320, 'CZ', 'Q5'],
|
||||
['Zabreh', 49.881, 16.871, 1200, 'CZ', 'Q6'],
|
||||
];
|
||||
assert.deepEqual(
|
||||
withoutNearTwins(rows).map((row) => row[5]),
|
||||
['Q1', 'Q3', 'Q4', 'Q5'],
|
||||
);
|
||||
});
|
||||
|
||||
test('twins are found where longitude cells narrow, and across 180°', () => {
|
||||
const rows = [
|
||||
// 1.98 km apart at 70° N, two longitude cells apart.
|
||||
['Nord', 70, 10.049, 9000, 'NO', 'Q1'],
|
||||
['Nord', 70, 10.101, 2000, 'NO', 'Q2'],
|
||||
// Either side of the antimeridian, under 2 km apart.
|
||||
['Kai', -16.5, 179.995, 5000, 'FJ', 'Q3'],
|
||||
['Kai', -16.5, -179.995, 1000, 'FJ', 'Q4'],
|
||||
];
|
||||
assert.deepEqual(
|
||||
withoutNearTwins(rows).map((row) => row[5]),
|
||||
['Q1', 'Q3'],
|
||||
);
|
||||
});
|
||||
|
||||
test('a rebuild of unchanged rows keeps the earlier build stamp', () => {
|
||||
const earlier = `${JSON.stringify({ meta: { built: '2026-01-01T00:00:00.000Z', count: 2 }, rows: [1, 2] })}\n`;
|
||||
const same = `${JSON.stringify({ meta: { built: '2026-11-18T00:00:00.000Z', count: 2 }, rows: [1, 2] })}\n`;
|
||||
const changed = `${JSON.stringify({ meta: { built: '2026-11-18T00:00:00.000Z', count: 3 }, rows: [1, 2, 3] })}\n`;
|
||||
assert.equal(keepBuiltIfUnchanged(same, earlier), earlier);
|
||||
assert.equal(keepBuiltIfUnchanged(changed, earlier), changed);
|
||||
assert.equal(keepBuiltIfUnchanged(same, undefined), same);
|
||||
assert.equal(keepBuiltIfUnchanged(same, 'not json'), same);
|
||||
});
|
||||
|
||||
test('the places index keeps its build stamp only when no tile changed', () => {
|
||||
const rows = [
|
||||
['Aa', 31, -98, 2000, 'US', 'Q1'],
|
||||
['Bb', 41, -88, 1500, 'US', 'Q2'],
|
||||
];
|
||||
const meta = (built) => ({
|
||||
source: 's',
|
||||
license: 'l',
|
||||
built,
|
||||
minPopulation: 1000,
|
||||
maxPopulation: 15000,
|
||||
columns: [],
|
||||
});
|
||||
const earlier = placeTileFiles(rows, meta('2026-01-01T00:00:00.000Z'));
|
||||
const built = (files) => JSON.parse(files.get('index.json')).meta.built;
|
||||
const rebuild = (newRows) =>
|
||||
keepPackBuiltIfUnchanged(
|
||||
placeTileFiles(newRows, meta('2026-11-18T00:00:00.000Z')),
|
||||
earlier,
|
||||
);
|
||||
assert.equal(built(rebuild(rows)), '2026-01-01T00:00:00.000Z');
|
||||
// Same row count in every tile, new population: a new stamp.
|
||||
const changed = [['Aa', 31, -98, 2100, 'US', 'Q1'], rows[1]];
|
||||
assert.equal(built(rebuild(changed)), '2026-11-18T00:00:00.000Z');
|
||||
// A tile of the earlier build left out: a new stamp.
|
||||
assert.equal(built(rebuild([rows[0]])), '2026-11-18T00:00:00.000Z');
|
||||
});
|
||||
Reference in new issue
Block a user