mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
add roadtrip warning areas and tidy driving settings
This commit is contained in:
@@ -17,6 +17,7 @@ import { DatabaseModule } from '../../src/nest/database/database.module';
|
||||
import { PermissionsService } from '../../src/nest/permissions/permissions.service';
|
||||
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
||||
import { RoadtripModule } from '../../src/nest/roadtrip/roadtrip.module';
|
||||
import { RoadtripHazardsService } from '../../src/nest/roadtrip/roadtrip-hazards.service';
|
||||
import { seedUser, sessionCookie } from './harness';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
@@ -130,6 +131,17 @@ describe('Roadtrip e2e (real guard chain + temp SQLite)', () => {
|
||||
});
|
||||
|
||||
const cookie = () => sessionCookie(1);
|
||||
it('gates live hazards by addon, authentication and trip access', async () => {
|
||||
const read = vi.spyOn(app.get(RoadtripHazardsService), 'read').mockResolvedValue({ fetchedAt: new Date().toISOString(), hazards: [], sources: [] });
|
||||
try {
|
||||
await request(server).get('/api/trips/5/roadtrip/hazards').expect(401);
|
||||
await request(server).get('/api/trips/6/roadtrip/hazards').set('Cookie', cookie()).expect(404);
|
||||
await request(server).get('/api/trips/5/roadtrip/hazards').set('Cookie', cookie()).expect(200);
|
||||
setAddon(false);
|
||||
await request(server).get('/api/trips/5/roadtrip/hazards').set('Cookie', cookie()).expect(404);
|
||||
expect(read).toHaveBeenCalledTimes(1);
|
||||
} finally { read.mockRestore(); }
|
||||
});
|
||||
|
||||
describe('shared trip driving preferences', () => {
|
||||
it('shares values with members and isolates other trips', async () => {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { RoadtripHazardsService } from '../../../src/nest/roadtrip/roadtrip-hazards.service';
|
||||
|
||||
const polygon = { type: 'Polygon', coordinates: [[[10, 50], [11, 50], [11, 51], [10, 50]]] };
|
||||
const feed = (features: unknown[]) => new Response(JSON.stringify({ type: 'FeatureCollection', features }));
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
|
||||
describe('roadtrip hazard sources', () => {
|
||||
it('isolates failed providers and caches concurrent reads', async () => {
|
||||
const fetcher = vi.fn(async (url: string) => {
|
||||
if (url.includes('dwd.de')) throw new Error('down');
|
||||
return feed([]);
|
||||
});
|
||||
vi.stubGlobal('fetch', fetcher);
|
||||
const service = new RoadtripHazardsService();
|
||||
const [first, second] = await Promise.all([service.read(), service.read()]);
|
||||
expect(first).toEqual(second);
|
||||
expect(first.sources).toEqual([{ source: 'DWD', status: 'unavailable' }, { source: 'GDACS', status: 'ok' }]);
|
||||
await service.read();
|
||||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
it('drops expired warnings and marks malformed records as incomplete', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async (url: string) => feed(url.includes('dwd.de') ? [
|
||||
{ geometry: polygon, properties: { HEADLINE: 'Rain', SENT: '2026-01-01T00:00:00Z', EXPIRES: '2099-01-01T00:00:00Z' } },
|
||||
{ geometry: polygon, properties: { HEADLINE: 'Old', SENT: '2020-01-01T00:00:00Z', EXPIRES: '2020-01-02T00:00:00Z' } },
|
||||
{ geometry: null, properties: {} },
|
||||
] : [])));
|
||||
const saved = await new RoadtripHazardsService().read();
|
||||
expect(saved.hazards).toHaveLength(1);
|
||||
expect(saved.hazards[0].title).toBe('Rain');
|
||||
expect(saved.sources[0].status).toBe('partial');
|
||||
});
|
||||
it('uses affected polygons rather than the global event footprint', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn(async (url: string) => {
|
||||
if (url.includes('dwd.de')) return feed([]);
|
||||
if (url.includes('getgeometry')) return feed([
|
||||
{ geometry: polygon, properties: { Class: 'Poly_Affected' } },
|
||||
{ geometry: polygon, properties: { Class: 'Poly_Global' } },
|
||||
]);
|
||||
return feed([{ geometry: { type: 'Point', coordinates: [10, 50] }, properties: {
|
||||
eventtype: 'FL', eventid: 1, episodeid: 2, name: 'Flood', description: 'Flood report',
|
||||
datemodified: '2026-09-12T08:00:00', iscurrent: 'true',
|
||||
} }]);
|
||||
}));
|
||||
const saved = await new RoadtripHazardsService().read();
|
||||
expect(saved.hazards[0].geometry).toEqual({ type: 'MultiPolygon', coordinates: [polygon.coordinates] });
|
||||
expect(saved.hazards[0].updatedAt).toBe('2026-09-12T08:00:00.000Z');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user