chore: move transit backend to proper DI

Includes the quirk-repair pass (upstream timeout + size cap, LRU cache, NaN-duration guard) that maps landed as a sibling fix commit.
This commit is contained in:
jubnl
2026-07-29 21:39:51 +02:00
parent a7cb4eb880
commit f4bf7cf3dc
17 changed files with 813 additions and 623 deletions
+27 -14
View File
@@ -1,7 +1,8 @@
/**
* Transit proxy e2e — /api/transit/geocode + /api/transit/plan through the real
* JwtAuthGuard against a temp SQLite db. The transit service is mocked; this
* focuses on auth (401), param pass-through and error propagation (#1065).
* JwtAuthGuard against a temp SQLite db. The DI-native TransitService's provider
* methods are stubbed via instance spies (no outbound HTTP); this focuses on
* auth (401), param pass-through and error propagation (#1065).
*/
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
import request from 'supertest';
@@ -20,19 +21,28 @@ const { db } = vi.hoisted(() => {
return { db: tmp };
});
vi.mock('../../src/db/database', () => ({ db, closeDb: () => {}, reinitialize: () => {} }));
const { transitSvc } = vi.hoisted(() => ({ transitSvc: { geocode: vi.fn(), plan: vi.fn() } }));
vi.mock('../../src/services/transitService', () => transitSvc);
// TransitMcp (in the module graph, never invoked here) reaches _shared →
// src/websocket at import time — silence the real ws module.
vi.mock('../../src/websocket', () => ({ broadcast: vi.fn() }));
import { TransitModule } from '../../src/nest/transit/transit.module';
import { TransitService } from '../../src/nest/transit/transit.service';
import { DatabaseModule } from '../../src/nest/database/database.module';
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
describe('Transit proxy e2e (real auth guard + temp SQLite)', () => {
let server: Server;
let app: Awaited<ReturnType<typeof build>>;
let geocodeSpy: ReturnType<typeof vi.spyOn>;
let planSpy: ReturnType<typeof vi.spyOn>;
async function build() {
const moduleRef = await Test.createTestingModule({ imports: [TransitModule] }).compile();
const moduleRef = await Test.createTestingModule({
// DatabaseModule + RealtimeModule are @Global in the app graph;
// TransitModule's DaysModule/ReservationsModule imports need them here.
imports: [DatabaseModule, RealtimeModule, TransitModule],
}).compile();
const nest = moduleRef.createNestApplication();
nest.use(cookieParser());
nest.useGlobalFilters(new TrekExceptionFilter());
@@ -44,11 +54,14 @@ describe('Transit proxy e2e (real auth guard + temp SQLite)', () => {
seedUser(db as never, { id: 1 });
app = await build();
server = app.getHttpServer();
const transit = app.get(TransitService);
geocodeSpy = vi.spyOn(transit, 'geocode') as ReturnType<typeof vi.spyOn>;
planSpy = vi.spyOn(transit, 'plan') as ReturnType<typeof vi.spyOn>;
});
beforeEach(() => {
transitSvc.geocode.mockReset();
transitSvc.plan.mockReset();
geocodeSpy.mockReset();
planSpy.mockReset();
});
afterAll(async () => { await app.close(); });
@@ -59,20 +72,20 @@ describe('Transit proxy e2e (real auth guard + temp SQLite)', () => {
});
it('geocode passes q/lang/near through and returns the service result', async () => {
transitSvc.geocode.mockResolvedValueOnce({ results: [{ name: 'Alexanderplatz' }] });
geocodeSpy.mockResolvedValueOnce({ results: [{ name: 'Alexanderplatz' }] });
const res = await request(server).get('/api/transit/geocode?q=alex&lang=de&near=52.5,13.4').set('Cookie', sessionCookie(1));
expect(res.status).toBe(200);
expect(res.body.results[0].name).toBe('Alexanderplatz');
expect(transitSvc.geocode).toHaveBeenCalledWith('alex', 'de', '52.5,13.4');
expect(geocodeSpy).toHaveBeenCalledWith('alex', 'de', '52.5,13.4');
});
it('plan passes all params through (arriveBy + maxTransfers coerced)', async () => {
transitSvc.plan.mockResolvedValueOnce({ itineraries: [] });
planSpy.mockResolvedValueOnce({ itineraries: [] });
const res = await request(server)
.get('/api/transit/plan?from=52.5,13.4&to=52.6,13.5&time=2026-07-13T09:00:00Z&arriveBy=true&modes=BUS&maxTransfers=2')
.set('Cookie', sessionCookie(1));
expect(res.status).toBe(200);
expect(transitSvc.plan).toHaveBeenCalledWith({
expect(planSpy).toHaveBeenCalledWith({
from: '52.5,13.4', to: '52.6,13.5', time: '2026-07-13T09:00:00Z', arriveBy: true, modes: 'BUS', maxTransfers: 2,
});
});
@@ -80,7 +93,7 @@ describe('Transit proxy e2e (real auth guard + temp SQLite)', () => {
it('service validation errors propagate with their status', async () => {
const err = new Error('from must be "lat,lng"') as Error & { status: number };
err.status = 400;
transitSvc.plan.mockRejectedValueOnce(err);
planSpy.mockRejectedValueOnce(err);
const res = await request(server).get('/api/transit/plan?from=bad&to=1,2').set('Cookie', sessionCookie(1));
expect(res.status).toBe(400);
expect(res.body.error).toContain('lat,lng');
@@ -89,7 +102,7 @@ describe('Transit proxy e2e (real auth guard + temp SQLite)', () => {
it('upstream failures surface as 502', async () => {
const err = new Error('Transit provider error (HTTP 500)') as Error & { status: number };
err.status = 502;
transitSvc.plan.mockRejectedValueOnce(err);
planSpy.mockRejectedValueOnce(err);
const res = await request(server).get('/api/transit/plan?from=1,2&to=3,4').set('Cookie', sessionCookie(1));
expect(res.status).toBe(502);
});