mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(roadtrip): shape the drive — vias, alternatives and stops along the way
The road-trip addon's second half: the parts that let a planned drive be argued with rather than only read. Reshaping. A via records which stop it follows by position, and that position is resolved fresh every time the day is routed — so inserting, reordering or removing a stop moved the ground under every via after it, and the drive silently snapped back to the road it had been steered away from while the handle still sat on the map where it was left. The correction is computed against the day as it stands and written with the same change, in one transaction: a half-applied re-anchoring mixes two numbering schemes in one day. The one judgement that needs geometry — which half of a split leg a via belongs to when a stop lands in the middle of it — is measured on the road actually driven, so a fuel stop added to a bent leg keeps the bend on the side it belongs to. Alternatives. OSRM answers most long legs with exactly one route, which read as "there is no other way" when a perfectly good slower road existed. When it offers nothing, the same drive is asked for again without the motorway, and failing that without the tolls — one question at a time, stopping as soon as one lands. The offer says what it is rather than how much slower. Stops along the way. Everything the corridor search can find is a pause, not a destination: fuel, charging, rest areas, campsites, food and sights all sit inside the leg they were found on, carry their own colour and icon, and are left out of every stop count. That colour is defined once and read by the rail, the corridor list, the map pin and the picker — and it replaced the chains' logos, which turned a corridor full of petrol stations into a row of advertisements and left the brands with no logo on file looking like something else entirely. How long a stop takes was readable in the rail and writable nowhere; new places were given an invented hour that nobody had entered. It is now set from the rail, cleared as a zero because the update statement folds a null into "leave it alone", and new places start with nothing. Both columns are rebuilt on one type scale, anchored to the day plan's own sizes so switching tabs does not change the size of the text beside it. The rail reads as trip totals, day cards and a chain of stops with the drives between them; the corridor column as what to look for, how the search is getting on, and what it found. Six category pills over three lines became one dropdown, which is the space the results wanted. Also: the offered routes are framed on the map when the picker opens (`fitKey` carries no coordinates and each renderer reads it as "the selected day"), road trip mode does not cluster at all so the whole drive stays readable zoomed out, and the drive-time labels finally render in the app's own typeface instead of Leaflet's Helvetica. All of it in both renderers.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, afterEach } from 'vitest';
|
||||
import express from 'express';
|
||||
import request from 'supertest';
|
||||
import { applyGlobalMiddleware } from '../../../src/middleware/globalMiddleware';
|
||||
import { applyGlobalMiddleware, routingCspOrigins } from '../../../src/middleware/globalMiddleware';
|
||||
|
||||
async function directiveSources(name: string): Promise<string[]> {
|
||||
const app = express();
|
||||
@@ -122,3 +122,45 @@ describe('forced-HTTPS redirect', () => {
|
||||
expect(probe.status).toBe(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe('routingCspOrigins', () => {
|
||||
it('CSP-ROUTING-001: keeps only the origin, because a path source matches that path alone', () => {
|
||||
// A router is asked at several paths (/route/v1/…, /table/v1/…); pinning one would
|
||||
// block the rest without an error the app could report.
|
||||
expect(routingCspOrigins(['https://osrm.example.org/route/v1/driving']))
|
||||
.toEqual(['https://osrm.example.org']);
|
||||
});
|
||||
|
||||
it('CSP-ROUTING-002: a port belongs to the origin and is kept', () => {
|
||||
expect(routingCspOrigins(['http://192.168.178.72:5000'])).toEqual(['http://192.168.178.72:5000']);
|
||||
});
|
||||
|
||||
it('CSP-ROUTING-003: anything that is not an http(s) URL widens nothing', () => {
|
||||
// A bad settings row must not be able to loosen the policy.
|
||||
expect(routingCspOrigins(['', null, undefined, 'not a url', 'javascript:alert(1)', 'ftp://x/y']))
|
||||
.toEqual([]);
|
||||
});
|
||||
|
||||
it('CSP-ROUTING-004: the same host twice is one source', () => {
|
||||
expect(routingCspOrigins([
|
||||
'https://osrm.example.org/route/v1/driving',
|
||||
'https://osrm.example.org/table/v1/driving',
|
||||
])).toEqual(['https://osrm.example.org']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('connect-src with a self-hosted router', () => {
|
||||
it('CSP-ROUTING-005: a configured origin is named, or the browser blocks it silently', async () => {
|
||||
const app = express();
|
||||
applyGlobalMiddleware(app, { extraConnectSrc: ['https://osrm.example.org'] });
|
||||
app.get('/probe', (_req, res) => res.json({ ok: true }));
|
||||
|
||||
const res = await request(app).get('/probe');
|
||||
const csp = String(res.headers['content-security-policy'] || '');
|
||||
const connect = csp.split(';').map(d => d.trim()).find(d => d.startsWith('connect-src'))!;
|
||||
|
||||
expect(connect).toContain('https://osrm.example.org');
|
||||
// The public hosts stay, so an instance can be switched back without another deploy.
|
||||
expect(connect).toContain('https://routing.openstreetmap.de/');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import Database from 'better-sqlite3';
|
||||
import { RoadtripService } from '../../../src/nest/roadtrip/roadtrip.service';
|
||||
|
||||
/**
|
||||
* Via points, against a real SQLite. The table is tiny and the interesting parts are the
|
||||
* ordering and the day scoping, both of which are SQL — mocking the DB would test the mock.
|
||||
*/
|
||||
function makeService() {
|
||||
const raw = new Database(':memory:');
|
||||
raw.exec(`
|
||||
CREATE TABLE days (id INTEGER PRIMARY KEY, trip_id INTEGER NOT NULL);
|
||||
CREATE TABLE roadtrip_vias (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
day_id INTEGER NOT NULL REFERENCES days(id) ON DELETE CASCADE,
|
||||
after_order_index INTEGER NOT NULL,
|
||||
sequence INTEGER NOT NULL DEFAULT 0,
|
||||
lat REAL NOT NULL,
|
||||
lng REAL NOT NULL,
|
||||
created_at TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
INSERT INTO days (id, trip_id) VALUES (1, 7), (2, 7), (3, 99);
|
||||
`);
|
||||
const db = {
|
||||
get: <T>(sql: string, ...params: unknown[]) => raw.prepare(sql).get(...params as never[]) as T | undefined,
|
||||
all: <T>(sql: string, ...params: unknown[]) => raw.prepare(sql).all(...params as never[]) as T[],
|
||||
run: (sql: string, ...params: unknown[]) => raw.prepare(sql).run(...params as never[]),
|
||||
transaction: <T>(fn: (conn: unknown) => T) => raw.transaction(() => fn(raw))(),
|
||||
};
|
||||
return new RoadtripService(db as never);
|
||||
}
|
||||
|
||||
describe('RoadtripService', () => {
|
||||
let service: RoadtripService;
|
||||
beforeEach(() => { service = makeService(); });
|
||||
|
||||
it('ROADTRIP-SVC-001: a day of another trip is not this trip’s day', () => {
|
||||
// The trip guard proves access to the trip; this is what stops a valid day id from
|
||||
// someone else’s trip being edited through one the caller can reach.
|
||||
expect(service.dayExists(1, 7)).toBe(true);
|
||||
expect(service.dayExists(3, 7)).toBe(false);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-002: vias appended after the same stop keep the order they were added in', () => {
|
||||
const first = service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
const second = service.create(1, { after_order_index: 0, lat: 53.5, lng: 10.5 });
|
||||
|
||||
expect(first.sequence).toBe(0);
|
||||
expect(second.sequence).toBe(1);
|
||||
expect(service.listForDay(1).map(v => v.id)).toEqual([first.id, second.id]);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-003: the list is ordered by stop first, then by sequence', () => {
|
||||
const late = service.create(1, { after_order_index: 2, lat: 51, lng: 12 });
|
||||
const early = service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
|
||||
expect(service.listForDay(1).map(v => v.id)).toEqual([early.id, late.id]);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-004: the trip listing spans its days and stops at the trip boundary', () => {
|
||||
service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
service.create(2, { after_order_index: 0, lat: 52, lng: 11 });
|
||||
service.create(3, { after_order_index: 0, lat: 50, lng: 14 });
|
||||
|
||||
// Day 3 belongs to trip 99 and must not appear in trip 7.
|
||||
expect(service.listForTrip(7).map(v => v.day_id)).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-005: moving a via changes where it is, not where it sits in the chain', () => {
|
||||
const via = service.create(1, { after_order_index: 1, lat: 53, lng: 10 });
|
||||
|
||||
const moved = service.move(via.id, 1, 52.5, 11.5);
|
||||
|
||||
expect(moved).toMatchObject({ lat: 52.5, lng: 11.5, after_order_index: 1, sequence: via.sequence });
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-006: a via cannot be moved or removed through the wrong day', () => {
|
||||
const via = service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
|
||||
expect(service.move(via.id, 2, 0, 0)).toBeNull();
|
||||
expect(service.remove(via.id, 2)).toBe(false);
|
||||
// Still where it was.
|
||||
expect(service.listForDay(1)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-008: re-anchoring moves the vias it names and leaves the rest alone', () => {
|
||||
const a = service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
const b = service.create(1, { after_order_index: 1, lat: 54, lng: 11 });
|
||||
const c = service.create(1, { after_order_index: 2, lat: 55, lng: 12 });
|
||||
|
||||
const after = service.reanchor(1, { vias: [{ id: b.id, after_order_index: 2 }, { id: c.id, after_order_index: 3 }] });
|
||||
|
||||
const byId = new Map(after.map(v => [v.id, v.after_order_index]));
|
||||
expect(byId.get(a.id)).toBe(0);
|
||||
expect(byId.get(b.id)).toBe(2);
|
||||
expect(byId.get(c.id)).toBe(3);
|
||||
// Where a via sits is untouched — only which leg it belongs to changed.
|
||||
expect(after.find(v => v.id === b.id)?.lat).toBe(54);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-009: re-anchoring deletes the vias whose leg stopped existing', () => {
|
||||
const gone = service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
const kept = service.create(1, { after_order_index: 2, lat: 54, lng: 11 });
|
||||
|
||||
const after = service.reanchor(1, {
|
||||
vias: [{ id: kept.id, after_order_index: 1 }],
|
||||
remove: [gone.id],
|
||||
});
|
||||
|
||||
expect(after.map(v => v.id)).toEqual([kept.id]);
|
||||
expect(after[0].after_order_index).toBe(1);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-010: a via of another day cannot be renumbered through this one', () => {
|
||||
// The batch is trusted to name ids, so `AND day_id = ?` is the only thing standing
|
||||
// between a day the caller can reach and every via of the trip.
|
||||
const other = service.create(2, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
|
||||
service.reanchor(1, { vias: [{ id: other.id, after_order_index: 9 }], remove: [other.id] });
|
||||
|
||||
const untouched = service.listForDay(2);
|
||||
expect(untouched.map(v => v.id)).toEqual([other.id]);
|
||||
expect(untouched[0].after_order_index).toBe(0);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-011: an id that is gone does not fail the rest of the batch', () => {
|
||||
// The caller computed the batch from a snapshot; a via somebody else deleted in the
|
||||
// meantime must not leave the remaining ones mis-pinned.
|
||||
const kept = service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
|
||||
const after = service.reanchor(1, {
|
||||
vias: [{ id: 9999, after_order_index: 4 }, { id: kept.id, after_order_index: 1 }],
|
||||
});
|
||||
|
||||
expect(after.map(v => v.after_order_index)).toEqual([1]);
|
||||
});
|
||||
|
||||
it('ROADTRIP-SVC-007: removing one reports whether there was anything to remove', () => {
|
||||
const via = service.create(1, { after_order_index: 0, lat: 53, lng: 10 });
|
||||
|
||||
expect(service.remove(via.id, 1)).toBe(true);
|
||||
expect(service.remove(via.id, 1)).toBe(false);
|
||||
expect(service.listForDay(1)).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user