mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
feat(tours): bike tours in the contract and on the server (BIK-01 to BIK-06, RTE-01)
A tour can be a bike tour with a routing profile (touring, road, gravel, mtb), stored per tour by a new migration. The server grades the ways of a mountain bike route from OpenStreetMap (mtb:scale) through one shared Overpass client, offers CyclOSM behind its own admin switch, and exports a bike tour's GPX with the activity of its profile. A hike's route choices are now exactly the three the card names: steep climbs, steps and ferries.
This commit is contained in:
@@ -63,6 +63,7 @@ const UNNUMBERED = [
|
||||
'Migration20200101044900_a_tour_waypoint_can_be_a_place_of_the_trip',
|
||||
'Migration20200101045000_a_hike_keeps_its_routing_preferences',
|
||||
'Migration20200101045100_a_tour_remembers_the_trip_it_was_taken_from',
|
||||
'Migration20200101045200_a_tour_can_be_ridden',
|
||||
];
|
||||
// The last step the positional runner ever had; planLegacyBaseline refuses a
|
||||
// schema_version past it.
|
||||
|
||||
@@ -101,6 +101,7 @@ describe('ToursRepository reads', () => {
|
||||
has_recorded_track: 0,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
bike_profile: null,
|
||||
},
|
||||
{
|
||||
place_id: older.id,
|
||||
@@ -131,6 +132,7 @@ describe('ToursRepository reads', () => {
|
||||
has_recorded_track: 0,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
bike_profile: null,
|
||||
},
|
||||
]);
|
||||
expect((await repo.listForTrip(trip.id)).map((r) => r.place_id)).not.toContain(plain.id);
|
||||
@@ -217,6 +219,7 @@ describe('ToursRepository writes', () => {
|
||||
has_recorded_track: 0,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
bike_profile: null,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -297,6 +300,7 @@ describe('ToursRepository writes', () => {
|
||||
track_marks: null,
|
||||
revision: 1,
|
||||
hiking_preferences: null,
|
||||
bike_profile: null,
|
||||
taken_over_from_trip_title: null,
|
||||
taken_over_from_tour_name: null,
|
||||
},
|
||||
|
||||
@@ -60,6 +60,12 @@ describe('global CSP: the other shipped raster presets (#2180)', () => {
|
||||
expect(await connectSrcSources()).toContain('https://tile.openmaps.fr');
|
||||
});
|
||||
|
||||
it('allows the CyclOSM shards the Tours planner offers as a cycling base map (BIK-06), and nothing wider', async () => {
|
||||
const sources = await connectSrcSources();
|
||||
for (const shard of ['a', 'b', 'c']) expect(sources).toContain(`https://${shard}.tile-cyclosm.openstreetmap.fr`);
|
||||
expect(sources).not.toContain('https://*.openstreetmap.fr');
|
||||
});
|
||||
|
||||
it('keeps the routing host, which is a different host and covers nothing here', async () => {
|
||||
// routing.openstreetmap.de was on the list all along and looks close enough
|
||||
// to hide the gap: a CSP source matches a host, not a suffix of one.
|
||||
|
||||
@@ -439,6 +439,22 @@ describe('AddonsService addon/feature flags', () => {
|
||||
expect((await svc.list()).toursHikingMap).toBe(false);
|
||||
});
|
||||
|
||||
it('CyclOSM under Tours is on until an admin switches it off, and offered only with Tours (BIK-06)', async () => {
|
||||
expect(await svc.getToursCycleMap()).toEqual({ enabled: true }); // absent row → ON
|
||||
expect((await svc.list()).toursCycleMap).toBe(false); // Tours is off
|
||||
await insertAddon({ id: 'tours', name: 'Tours', enabled: 1 });
|
||||
expect((await svc.list()).toursCycleMap).toBe(true);
|
||||
|
||||
expect(await svc.updateToursCycleMap(false)).toEqual({ enabled: false });
|
||||
expect(await storedAppSetting('tours_cycle_map_enabled')).toEqual({
|
||||
key: 'tours_cycle_map_enabled',
|
||||
value: 'false',
|
||||
});
|
||||
expect((await svc.list()).toursCycleMap).toBe(false);
|
||||
// The two maps are switched apart.
|
||||
expect((await svc.list()).toursHikingMap).toBe(true);
|
||||
});
|
||||
|
||||
it('getCollabFeatures is opt-out: absent rows default ON, only false disables', async () => {
|
||||
await setAppSetting('collab_chat_enabled', 'false');
|
||||
await setAppSetting('collab_polls_enabled', 'true');
|
||||
|
||||
@@ -48,6 +48,8 @@ const addonsStub = () =>
|
||||
updateToursWanderer: vi.fn((enabled: boolean) => ({ enabled })),
|
||||
getToursHikingMap: vi.fn(() => ({ enabled: true })),
|
||||
updateToursHikingMap: vi.fn((enabled: boolean) => ({ enabled })),
|
||||
getToursCycleMap: vi.fn(() => ({ enabled: true })),
|
||||
updateToursCycleMap: vi.fn((enabled: boolean) => ({ enabled })),
|
||||
getPlacesPhotos: vi.fn(() => ({ enabled: false })),
|
||||
updatePlacesPhotos: vi.fn((enabled: boolean) => ({ enabled })),
|
||||
getPlacesAutocomplete: vi.fn(() => ({ enabled: false })),
|
||||
@@ -495,6 +497,7 @@ describe('AdminController feature toggles', () => {
|
||||
[() => c.updateBagTracking(user, { enabled: true }, req), 'admin.bag_tracking', 'updateBagTracking'],
|
||||
[() => c.updateToursWanderer(user, { enabled: true }, req), 'admin.tours_wanderer', 'updateToursWanderer'],
|
||||
[() => c.updateToursHikingMap(user, { enabled: true }, req), 'admin.tours_hiking_map', 'updateToursHikingMap'],
|
||||
[() => c.updateToursCycleMap(user, { enabled: true }, req), 'admin.tours_cycle_map', 'updateToursCycleMap'],
|
||||
[() => c.updatePlacesPhotos(user, { enabled: true }, req), 'admin.places_photos', 'updatePlacesPhotos'],
|
||||
[
|
||||
() => c.updatePlacesAutocomplete(user, { enabled: true }, req),
|
||||
@@ -517,6 +520,7 @@ describe('AdminController feature toggles', () => {
|
||||
expect(await c.getBagTracking()).toEqual({ enabled: false });
|
||||
expect(await c.getToursWanderer()).toEqual({ enabled: false });
|
||||
expect(await c.getToursHikingMap()).toEqual({ enabled: true });
|
||||
expect(await c.getToursCycleMap()).toEqual({ enabled: true });
|
||||
expect(await c.getPlacesPhotos()).toEqual({ enabled: false });
|
||||
expect(await c.getPlacesAutocomplete()).toEqual({ enabled: false });
|
||||
expect(await c.getPlacesDetails()).toEqual({ enabled: false });
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
ferrataTileBlocks,
|
||||
ferrataTileKeysFor,
|
||||
ferrataTilesBbox,
|
||||
overpassFerrataAnswerSchema,
|
||||
parseFerrataGrade,
|
||||
snapFerrataBbox,
|
||||
splitFerratasByTile,
|
||||
@@ -211,18 +210,6 @@ describe('buildFerrataQuery', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('overpassFerrataAnswerSchema', () => {
|
||||
it('accepts an element list with or without a remark', () => {
|
||||
expect(overpassFerrataAnswerSchema.safeParse({ elements: [] }).success).toBe(true);
|
||||
expect(overpassFerrataAnswerSchema.safeParse({ elements: [], remark: 'runtime error' }).success).toBe(true);
|
||||
});
|
||||
|
||||
it('refuses a body without elements', () => {
|
||||
expect(overpassFerrataAnswerSchema.safeParse({ error: 'x' }).success).toBe(false);
|
||||
expect(overpassFerrataAnswerSchema.safeParse(undefined).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toFerratas', () => {
|
||||
it('maps a way to the wire row', () => {
|
||||
const { ferratas, truncated } = toFerratas([way(42, { name: ' Pisciadù ', via_ferrata_scale: '3+' })]);
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
import { FerrataTiles } from '../../../src/db/entities/FerrataTiles.entity';
|
||||
import { FerrataTilesRepository } from '../../../src/db/repositories/FerrataTiles.repository';
|
||||
import { DomainError } from '../../../src/nest/common/domain-error';
|
||||
import { FerratasOverpassClient } from '../../../src/nest/ferratas/ferratas-overpass.client';
|
||||
import { FerratasModule } from '../../../src/nest/ferratas/ferratas.module';
|
||||
import {
|
||||
FERRATA_TILE_STALE_MS,
|
||||
FERRATAS_UNAVAILABLE_MESSAGE,
|
||||
FerratasService,
|
||||
} from '../../../src/nest/ferratas/ferratas.service';
|
||||
import { OverpassElementsClient } from '../../../src/nest/overpass/overpass-elements.client';
|
||||
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
||||
import { countRows, deleteRows, findRow, findRows, insertRows } from '../../helpers/factories/rows';
|
||||
import { resetTestDb } from '../../helpers/test-db';
|
||||
@@ -73,7 +73,7 @@ async function boot(): Promise<void> {
|
||||
t = await createTestModule({
|
||||
db,
|
||||
imports: [FerratasModule],
|
||||
overrides: [{ provide: FerratasOverpassClient, useValue: { query } }],
|
||||
overrides: [{ provide: OverpassElementsClient, useValue: { query } }],
|
||||
});
|
||||
service = t.get(FerratasService);
|
||||
}
|
||||
|
||||
+22
-10
@@ -1,20 +1,20 @@
|
||||
import { RuntimeEnvService } from '../../../src/nest/app-config/runtime-env.service';
|
||||
import { FerratasOverpassClient } from '../../../src/nest/ferratas/ferratas-overpass.client';
|
||||
import { parseOverpassEndpoints } from '../../../src/nest/maps/overpass.constants';
|
||||
import { OverpassElementsClient, overpassAnswerSchema } from '../../../src/nest/overpass/overpass-elements.client';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const integrations = { overpassUrl: undefined as string | undefined, overpassTimeoutMs: 5000 };
|
||||
let client: FerratasOverpassClient;
|
||||
let client: OverpassElementsClient;
|
||||
|
||||
beforeEach(async () => {
|
||||
integrations.overpassUrl = 'https://a.example/api/interpreter,https://b.example/api/interpreter';
|
||||
integrations.overpassTimeoutMs = 5000;
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
providers: [FerratasOverpassClient, { provide: RuntimeEnvService, useValue: { env: () => ({ integrations }) } }],
|
||||
providers: [OverpassElementsClient, { provide: RuntimeEnvService, useValue: { env: () => ({ integrations }) } }],
|
||||
}).compile();
|
||||
client = moduleRef.get(FerratasOverpassClient);
|
||||
client = moduleRef.get(OverpassElementsClient);
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
});
|
||||
|
||||
@@ -46,12 +46,12 @@ describe('parseOverpassEndpoints', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('FerratasOverpassClient.query', () => {
|
||||
describe('OverpassElementsClient.query', () => {
|
||||
it('POSTs the query with a user agent and a deadline, and answers the elements', async () => {
|
||||
const fetcher = vi.fn(async (_url: string, _init: RequestInit) => json({ elements: [{ type: 'way', id: 1 }] }));
|
||||
vi.stubGlobal('fetch', fetcher);
|
||||
integrations.overpassUrl = 'https://a.example/api/interpreter';
|
||||
const elements = await client.query('[out:json];way(1);out;');
|
||||
const elements = await client.query('[out:json];way(1);out;', 'Ferratas');
|
||||
expect(elements).toEqual([{ type: 'way', id: 1 }]);
|
||||
const [url, init] = fetcher.mock.calls[0] ?? [];
|
||||
expect(url).toBe('https://a.example/api/interpreter');
|
||||
@@ -71,7 +71,7 @@ describe('FerratasOverpassClient.query', () => {
|
||||
return json({ elements: [{ type: 'way', id: 2 }] });
|
||||
}),
|
||||
);
|
||||
expect(await client.query('q')).toEqual([{ type: 'way', id: 2 }]);
|
||||
expect(await client.query('q', 'Ferratas')).toEqual([{ type: 'way', id: 2 }]);
|
||||
expect(signals.every((s) => s.aborted)).toBe(true);
|
||||
});
|
||||
|
||||
@@ -80,7 +80,7 @@ describe('FerratasOverpassClient.query', () => {
|
||||
'fetch',
|
||||
vi.fn(async (url: string) => (url.startsWith('https://a.') ? json({}, 504) : json({ elements: 'nope' }))),
|
||||
);
|
||||
await expect(client.query('q')).rejects.toThrow('Could not reach any Overpass endpoint');
|
||||
await expect(client.query('q', 'Ferratas')).rejects.toThrow('Could not reach any Overpass endpoint');
|
||||
});
|
||||
|
||||
it('throws when a mirror cannot be reached at all', async () => {
|
||||
@@ -90,7 +90,7 @@ describe('FerratasOverpassClient.query', () => {
|
||||
throw new TypeError('fetch failed');
|
||||
}),
|
||||
);
|
||||
await expect(client.query('q')).rejects.toThrow('Could not reach any Overpass endpoint');
|
||||
await expect(client.query('q', 'Ferratas')).rejects.toThrow('Could not reach any Overpass endpoint');
|
||||
});
|
||||
|
||||
it('refuses an answer larger than the cap', async () => {
|
||||
@@ -101,6 +101,18 @@ describe('FerratasOverpassClient.query', () => {
|
||||
new Response('{"elements":[]}', { status: 200, headers: { 'content-length': String(9 * 1024 * 1024) } }),
|
||||
),
|
||||
);
|
||||
await expect(client.query('q')).rejects.toThrow('Could not reach any Overpass endpoint');
|
||||
await expect(client.query('q', 'Ferratas')).rejects.toThrow('Could not reach any Overpass endpoint');
|
||||
});
|
||||
});
|
||||
|
||||
describe('overpassAnswerSchema', () => {
|
||||
it('accepts an element list with or without a remark', () => {
|
||||
expect(overpassAnswerSchema.safeParse({ elements: [] }).success).toBe(true);
|
||||
expect(overpassAnswerSchema.safeParse({ elements: [], remark: 'runtime error' }).success).toBe(true);
|
||||
});
|
||||
|
||||
it('refuses a body without elements', () => {
|
||||
expect(overpassAnswerSchema.safeParse({ error: 'x' }).success).toBe(false);
|
||||
expect(overpassAnswerSchema.safeParse(undefined).success).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
import { OverpassElementsClient } from '../../../src/nest/overpass/overpass-elements.client';
|
||||
import { TourTrailGradeService } from '../../../src/nest/tours/tour-trail-grade.service';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
// TOUR-GRADE-001 to TOUR-GRADE-005
|
||||
|
||||
const query = vi.fn<(q: string, caller: string) => Promise<unknown[]>>();
|
||||
let service: TourTrailGradeService;
|
||||
|
||||
beforeEach(async () => {
|
||||
query.mockReset();
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
providers: [TourTrailGradeService, { provide: OverpassElementsClient, useValue: { query } }],
|
||||
}).compile();
|
||||
service = moduleRef.get(TourTrailGradeService);
|
||||
});
|
||||
|
||||
const way = (id: number, tags: Record<string, string>) => ({ type: 'way', id, tags });
|
||||
|
||||
describe('TourTrailGradeService (BIK-05)', () => {
|
||||
it('TOUR-GRADE-001: reads each scale to its steps and leaves a way without one out', async () => {
|
||||
query.mockResolvedValue([
|
||||
way(1, { 'mtb:scale': '2+', 'mtb:scale:uphill': '3' }),
|
||||
way(2, { 'mtb:scale': 'S1', 'mtb:scale:imba': '2' }),
|
||||
way(3, { highway: 'track' }),
|
||||
way(4, { 'mtb:scale': '9', 'mtb:scale:imba': 'hard' }),
|
||||
]);
|
||||
const answer = await service.grades({ way_ids: [1, 2, 3, 4, 5] });
|
||||
expect(answer).toEqual({
|
||||
ways: [
|
||||
{ id: 1, mtb_scale: '2+', mtb_scale_uphill: '3', mtb_scale_imba: null },
|
||||
{ id: 2, mtb_scale: '1', mtb_scale_uphill: null, mtb_scale_imba: '2' },
|
||||
],
|
||||
unanswered: [],
|
||||
});
|
||||
expect(query).toHaveBeenCalledWith('[out:json][timeout:25];way(id:1,2,3,4,5);out tags;', 'Tours');
|
||||
});
|
||||
|
||||
it('TOUR-GRADE-002: names the ways it could not ask about, never guesses them', async () => {
|
||||
query.mockRejectedValue(new Error('all mirrors down'));
|
||||
expect(await service.grades({ way_ids: [7, 8] })).toEqual({ ways: [], unanswered: [7, 8] });
|
||||
});
|
||||
|
||||
it('TOUR-GRADE-003: asks a long tour in parts, one after another, and keeps what one part answered', async () => {
|
||||
const ids = Array.from({ length: 900 }, (_, index) => index + 1);
|
||||
query
|
||||
.mockResolvedValueOnce([way(1, { 'mtb:scale': '0' })])
|
||||
.mockRejectedValueOnce(new Error('timeout'))
|
||||
.mockResolvedValueOnce([way(900, { 'mtb:scale': '5' })]);
|
||||
const answer = await service.grades({ way_ids: ids });
|
||||
expect(query).toHaveBeenCalledTimes(3);
|
||||
expect(answer.ways.map((grade) => grade.id)).toEqual([1, 900]);
|
||||
expect(answer.unanswered).toHaveLength(400);
|
||||
expect(answer.unanswered[0]).toBe(401);
|
||||
});
|
||||
|
||||
it('TOUR-GRADE-004: asks a way it already read only again a day later', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
query.mockResolvedValue([way(1, { 'mtb:scale': '3' })]);
|
||||
await service.grades({ way_ids: [1, 1] });
|
||||
await service.grades({ way_ids: [1] });
|
||||
expect(query).toHaveBeenCalledTimes(1);
|
||||
vi.advanceTimersByTime(24 * 60 * 60 * 1000 + 1);
|
||||
await service.grades({ way_ids: [1] });
|
||||
expect(query).toHaveBeenCalledTimes(2);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('TOUR-GRADE-005: skips what is no way in the answer', async () => {
|
||||
query.mockResolvedValue([{ type: 'node', id: 1, tags: { 'mtb:scale': '1' } }, { nonsense: true }]);
|
||||
expect(await service.grades({ way_ids: [1] })).toEqual({ ways: [], unanswered: [] });
|
||||
});
|
||||
});
|
||||
@@ -188,6 +188,7 @@ describe('ToursService planner contracts (mock-only; SQLite rollback semantics a
|
||||
portage_speed_kmh: null,
|
||||
paddle_lake_style: null,
|
||||
hiking_preferences: null,
|
||||
bike_profile: null,
|
||||
});
|
||||
expect((setup.toursRepo.insertTour.mock.calls[0][0] as { distance: number }).distance).toBeGreaterThan(0);
|
||||
expect(setup.waypointsRepo.insertForPlace).toHaveBeenCalledExactlyOnceWith(42, request.waypoints);
|
||||
@@ -305,6 +306,7 @@ describe('ToursService planner contracts (mock-only; SQLite rollback semantics a
|
||||
portage_speed_kmh: null,
|
||||
paddle_lake_style: null,
|
||||
hiking_preferences: null,
|
||||
bike_profile: null,
|
||||
});
|
||||
expect(setup.waypointsRepo.deleteForPlace).toHaveBeenCalledExactlyOnceWith(42);
|
||||
expect(setup.waypointsRepo.insertForPlace).toHaveBeenCalledExactlyOnceWith(42, update.waypoints);
|
||||
|
||||
@@ -250,7 +250,7 @@ describe('ToursService planner creation', () => {
|
||||
it('TOURS-SVC-010: a type the catalogue holds but has not enabled is a 400 without a write', async () => {
|
||||
// The contract only lets `hike` through today; this pins what happens the
|
||||
// day it lets more keys in before the planner offers them.
|
||||
const bike = { ...request, tour_type: 'bike' } as unknown as typeof request;
|
||||
const bike = { ...request, tour_type: 'run' } as unknown as typeof request;
|
||||
|
||||
await expect(service.createTour(tripId, bike)).rejects.toBeInstanceOf(BadRequestException);
|
||||
|
||||
@@ -262,7 +262,7 @@ describe('ToursService planner creation', () => {
|
||||
it('TOURS-SVC-011: an edit to a disabled type is a 400 and leaves the saved tour alone', async () => {
|
||||
const created = await service.createTour(tripId, request);
|
||||
broadcast.mockReset();
|
||||
const bike = { ...request, name: 'Renamed', tour_type: 'bike' } as unknown as typeof request;
|
||||
const bike = { ...request, name: 'Renamed', tour_type: 'run' } as unknown as typeof request;
|
||||
|
||||
await expect(service.updateTour(tripId, String(created.tour.place_id), bike)).rejects.toBeInstanceOf(
|
||||
BadRequestException,
|
||||
|
||||
Reference in New Issue
Block a user