mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
93 lines
3.1 KiB
TypeScript
93 lines
3.1 KiB
TypeScript
import { RoadtripHazardsService } from '../../../src/nest/roadtrip/roadtrip-hazards.service';
|
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
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');
|
|
});
|
|
});
|