mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
refactor(server): fold airports and distance, drop the boot-time backfill cycle (#1834)
Two thirds of be-weather-airports-fold. weatherService (513 LOC, a process cache and a setInterval at import) is left for its own PR. airportService split along the line that was already there. The dataset lookup touches no database — it reads one JSON file and keeps it in memory — and both MCP registrars call it from outside the container, so findByIata/searchAirports are free functions in nest/airports/airports.data.ts. backfillFlightEndpoints needs the connection and became a method on AirportsService. That backfill was the interesting part. db/database.ts ran it at module eval through a bare require() back into services/, so the module graph closed a cycle that only held because `db` happened to be initialised by the time the require resolved. It runs in onApplicationBootstrap now: it repairs rows, nothing boots on it, and the container is fully built by then. The cycle had a visible cost. tests/setup.console-noise.ts carried a suppression for `Cannot find module '../services/airportService'`, because vitest's transform pipeline could not resolve that require at all — it fired in roughly sixty test files. That suppression is now narrowed to the only case left: an e2e TestingModule whose fixture DB has no reservations table. distanceService split the same way: haversineKm is arithmetic and moved to common/geo.ts; getFlightDistanceKm reads the database and became a private method on AuthService, its only caller. AirportsModule imports DatabaseModule explicitly. @Global only reaches modules that are in the graph, and the airports e2e builds its container from AirportsModule alone — exactly the trap the nest README now documents. The search ranking gets its first direct tests. It sat in services/, outside the coverage gate, and only ever ran incidentally through the route; moving it into src/nest made the gap measurable (16% branches). The ten cases pin the scoring ladder — exact IATA short-circuit, ICAO over prefix, IATA prefix over city prefix, city prefix over substring — and the missing-dataset fallback, which no test had reached. 90% branches now.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* The bundled airport dataset — AIRPORT-DATA-001..010.
|
||||
*
|
||||
* These have no predecessor: the file lived in services/, outside the coverage
|
||||
* gate, so its scoring ladder was only ever exercised incidentally through the
|
||||
* e2e route. Moving it into src/nest made the gap visible.
|
||||
*
|
||||
* fs is stubbed with a four-airport fixture rather than reading the real
|
||||
* assets/airports.json, so the ranking assertions stay deterministic and the
|
||||
* missing-file branch is reachable.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { readFileSync, existsSync } = vi.hoisted(() => ({
|
||||
readFileSync: vi.fn(),
|
||||
existsSync: vi.fn(() => true),
|
||||
}));
|
||||
vi.mock('node:fs', () => ({ default: { readFileSync, existsSync }, readFileSync, existsSync }));
|
||||
|
||||
const FIXTURE = [
|
||||
{ iata: 'FRA', icao: 'EDDF', name: 'Frankfurt Airport', city: 'Frankfurt', country: 'DE', lat: 50, lng: 8.5, tz: 'Europe/Berlin' },
|
||||
{ iata: 'FRO', icao: 'ENFL', name: 'Floro Airport', city: 'Floro', country: 'NO', lat: 61, lng: 5, tz: 'Europe/Oslo' },
|
||||
{ iata: 'JFK', icao: 'KJFK', name: 'John F Kennedy International', city: 'New York', country: 'US', lat: 40, lng: -73, tz: 'America/New_York' },
|
||||
{ iata: 'BER', icao: 'EDDB', name: 'Berlin Brandenburg', city: 'Berlin', country: 'DE', lat: 52, lng: 13, tz: 'Europe/Berlin' },
|
||||
];
|
||||
|
||||
/** A fresh module registry per case: the dataset is cached after the first load. */
|
||||
async function load(opts: { missing?: boolean; body?: string } = {}) {
|
||||
vi.resetModules();
|
||||
existsSync.mockReturnValue(!opts.missing);
|
||||
readFileSync.mockReturnValue(opts.body ?? JSON.stringify(FIXTURE));
|
||||
return import('../../../../src/nest/airports/airports.data');
|
||||
}
|
||||
|
||||
beforeEach(() => vi.clearAllMocks());
|
||||
|
||||
describe('findByIata', () => {
|
||||
it('AIRPORT-DATA-001: finds an airport by its IATA code, case-insensitively', async () => {
|
||||
const { findByIata } = await load();
|
||||
expect(findByIata('fra')?.name).toBe('Frankfurt Airport');
|
||||
expect(findByIata('FRA')?.icao).toBe('EDDF');
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-002: returns null for an unknown code', async () => {
|
||||
const { findByIata } = await load();
|
||||
expect(findByIata('ZZZ')).toBeNull();
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-003: returns null for every code when the dataset file is missing', async () => {
|
||||
const { findByIata } = await load({ missing: true });
|
||||
expect(findByIata('FRA')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('searchAirports ranking', () => {
|
||||
it('AIRPORT-DATA-004: an exact three-letter IATA match short-circuits to that one airport', async () => {
|
||||
const { searchAirports } = await load();
|
||||
expect(searchAirports('JFK').map(a => a.iata)).toEqual(['JFK']);
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-005: an exact ICAO match outranks a prefix match', async () => {
|
||||
const { searchAirports } = await load();
|
||||
// 'EDDF' is FRA's ICAO; nothing else starts with it.
|
||||
expect(searchAirports('EDDF')[0].iata).toBe('FRA');
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-006: an IATA prefix outranks a city prefix', async () => {
|
||||
const { searchAirports } = await load();
|
||||
// 'FR' prefixes the IATA of FRA and FRO, and the city of Frankfurt.
|
||||
const iatas = searchAirports('FR').map(a => a.iata);
|
||||
expect(iatas.slice(0, 2).sort()).toEqual(['FRA', 'FRO']);
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-007: a city prefix outranks a substring match', async () => {
|
||||
const { searchAirports } = await load();
|
||||
// 'Berlin' is BER's city (prefix) and appears inside its name too.
|
||||
expect(searchAirports('Berlin')[0].iata).toBe('BER');
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-008: matches a name substring when nothing better applies', async () => {
|
||||
const { searchAirports } = await load();
|
||||
expect(searchAirports('Kennedy').map(a => a.iata)).toEqual(['JFK']);
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-009: an empty or whitespace query returns nothing', async () => {
|
||||
const { searchAirports } = await load();
|
||||
expect(searchAirports('')).toEqual([]);
|
||||
expect(searchAirports(' ')).toEqual([]);
|
||||
});
|
||||
|
||||
it('AIRPORT-DATA-010: honours the result limit', async () => {
|
||||
const { searchAirports } = await load();
|
||||
// Every fixture airport matches on country or name somewhere; cap it at one.
|
||||
expect(searchAirports('a', 1)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user