Files
TREK/server/tests/unit/nest/atlas-flights.service.test.ts
Maurice 9018ee8980 test(atlas): cover the new Atlas from engine to hub views
SonarCloud measured 16.9% coverage on this PR's new code. Tests now cover the WebGL globe engine (boot, caching, input, camera, regions, prefetch) and renderer (against a fake WebGL2 context that reads the real shaders), the paint, labels, sky and pins, the central useAtlasGlobe hook and the hub hooks, the snapshot, account queue and Atlas repos, every desktop and phone hub view, the server's people, flights and mesh routes, the shared badge rules, and the new view transition, warm bags and kept planner maps.
2026-11-29 10:21:28 +01:00

142 lines
5.4 KiB
TypeScript

/**
* AtlasFlightsService: the Atlas flight map read through the user's own flight
* endpoints, and the folding rules of aggregateFlights the older
* atlas-flights.test.ts leaves open (airports without a code, endpoints without
* coordinates, dates on either end of a leg, the order of routes and airports).
*/
import { ReservationEndpoints } from '../../../src/db/entities/ReservationEndpoints.entity';
import { AtlasFlightsService, aggregateFlights, greatCircleKm } from '../../../src/nest/atlas/atlas-flights.service';
import { createSnapshotTestDb } from '../../helpers/db-mock';
import { createTestModule, type TestModule } from '../../helpers/test-module';
import { getRepositoryToken } from '@mikro-orm/nestjs';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
type Row = Parameters<typeof aggregateFlights>[0][number];
const FRA = { code: 'FRA', name: 'Frankfurt', lat: 50.033, lng: 8.57 };
const JFK = { code: 'JFK', name: 'New York JFK', lat: 40.641, lng: -73.778 };
const LHR = { code: 'LHR', name: 'London Heathrow', lat: 51.47, lng: -0.4543 };
const row = (
reservation_id: number,
at: Omit<Row, 'reservation_id' | 'local_date'>,
local_date: string | null = null,
) => ({
reservation_id,
...at,
local_date,
});
const db = createSnapshotTestDb();
let t: TestModule;
let service: AtlasFlightsService;
const endpoints = {
listOwnedFlightRoutesForUser: vi.fn(async () => [
{ ...row(1, FRA, '2024-05-01'), sequence: 0 },
{ ...row(1, JFK), sequence: 1 },
]),
};
beforeAll(async () => {
t = await createTestModule({
db,
imports: [],
providers: [AtlasFlightsService, { provide: getRepositoryToken(ReservationEndpoints), useValue: endpoints }],
});
service = t.get(AtlasFlightsService);
});
afterAll(async () => {
await t.close();
db.close();
});
describe('AtlasFlightsService', () => {
it("ATLAS2-REST-020: reads the user's own flight endpoints and folds them into the map", async () => {
const map = await service.flights(9);
expect(endpoints.listOwnedFlightRoutesForUser).toHaveBeenCalledWith(9);
expect(map.routes).toHaveLength(1);
expect(map.routes[0]).toMatchObject({
a: { code: 'FRA' },
b: { code: 'JFK' },
count: 1,
firstDate: '2024-05-01',
lastDate: '2024-05-01',
km: Math.round(greatCircleKm(FRA, JFK)),
});
expect(map.totals).toEqual({
flights: 1,
km: Math.round(greatCircleKm(FRA, JFK)),
airports: 2,
longestKm: Math.round(greatCircleKm(FRA, JFK)),
});
});
});
describe('aggregateFlights', () => {
it('ATLAS2-REST-021: nothing to fold is an empty map', () => {
expect(aggregateFlights([])).toEqual({
routes: [],
airports: [],
totals: { flights: 0, km: 0, airports: 0, longestKm: 0 },
});
});
it('ATLAS2-REST-022: an airport without a code is known by its rounded position, the same spot twice is one airport', () => {
const field = { code: null, name: 'Airstrip', lat: 47.123, lng: 11.456 };
const sameField = { code: null, name: 'Airstrip (again)', lat: 47.1249, lng: 11.4551 };
const r = aggregateFlights([row(1, FRA), row(1, field), row(2, sameField), row(2, FRA)]);
expect(r.routes).toHaveLength(1);
expect(r.routes[0].count).toBe(2);
expect(r.airports).toHaveLength(2);
expect(r.airports.find((a) => a.code === null)).toMatchObject({ name: 'Airstrip', count: 2 });
// A lowercase code is the same airport as its uppercase spelling.
const lower = aggregateFlights([row(3, { ...FRA, code: 'fra' }), row(3, JFK), row(4, JFK), row(4, FRA)]);
expect(lower.routes).toHaveLength(1);
expect(lower.airports).toHaveLength(2);
});
it('ATLAS2-REST-023: skips a leg with an endpoint that has no coordinates', () => {
const nowhere = { code: 'XXX', name: 'Unknown', lat: Number.NaN, lng: Number.NaN };
expect(aggregateFlights([row(1, FRA), row(1, nowhere)]).totals.flights).toBe(0);
expect(aggregateFlights([row(1, nowhere), row(1, FRA)]).totals.flights).toBe(0);
});
it('ATLAS2-REST-024: a leg dated only on its arrival takes that date, an undated leg leaves the dates as they are', () => {
const r = aggregateFlights([
row(1, FRA, null),
row(1, LHR, '2021-07-04'),
row(2, LHR, null),
row(2, FRA, null),
row(3, FRA, '2020-01-01'),
row(3, LHR, null),
]);
expect(r.routes).toHaveLength(1);
expect(r.routes[0]).toMatchObject({ count: 3, firstDate: '2020-01-01', lastDate: '2021-07-04' });
});
it('ATLAS2-REST-025: an undated first leg takes the first date a later leg brings', () => {
const r = aggregateFlights([row(1, FRA), row(1, LHR), row(2, LHR, '2022-02-02'), row(2, FRA)]);
expect(r.routes[0]).toMatchObject({ firstDate: '2022-02-02', lastDate: '2022-02-02', count: 2 });
});
it('ATLAS2-REST-026: routes and airports come busiest first, the totals sum every leg', () => {
const r = aggregateFlights([
row(1, FRA),
row(1, LHR),
row(2, FRA),
row(2, JFK),
row(3, JFK),
row(3, FRA),
row(4, FRA),
row(4, JFK),
]);
expect(r.routes.map((x) => `${x.a.code}-${x.b.code}:${x.count}`)).toEqual(['FRA-JFK:3', 'FRA-LHR:1']);
expect(r.airports.map((a) => `${a.code}:${a.count}`)).toEqual(['FRA:4', 'JFK:3', 'LHR:1']);
expect(r.totals.flights).toBe(4);
expect(r.totals.airports).toBe(3);
expect(r.totals.km).toBe(Math.round(3 * greatCircleKm(FRA, JFK) + greatCircleKm(FRA, LHR)));
});
});