mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
feat(transit): fall back between configured public transit providers
This commit is contained in:
@@ -11,6 +11,7 @@ import { db } from '../../src/db/database';
|
||||
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
|
||||
import { Users } from '../../src/db/entities/Users.entity';
|
||||
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
||||
import { GoogleQuotaService } from '../../src/nest/google-quota/google-quota.service';
|
||||
import { RealtimeModule } from '../../src/nest/realtime/realtime.module';
|
||||
import { clearGoogleTransitCache } from '../../src/nest/transit/google-transit.provider';
|
||||
import { TransitModule } from '../../src/nest/transit/transit.module';
|
||||
@@ -75,6 +76,7 @@ describe('Transit backend switch e2e (#1699)', () => {
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.restoreAllMocks();
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
fetchMock.mockReset();
|
||||
// MOTIS and Routes shapes both parse as "no itineraries", which is all this
|
||||
@@ -137,4 +139,101 @@ describe('Transit backend switch e2e (#1699)', () => {
|
||||
expect(await planUpstream()).toBe('https://routes.googleapis.com/directions/v2:computeRoutes');
|
||||
expect(fetchMock.mock.calls[0][1].headers['X-Goog-Api-Key']).toBe('personal-key');
|
||||
});
|
||||
|
||||
const motisRoute = {
|
||||
itineraries: [
|
||||
{
|
||||
startTime: '2026-09-10T09:00:00Z',
|
||||
endTime: '2026-09-10T09:08:00Z',
|
||||
legs: [
|
||||
{
|
||||
mode: 'SUBWAY',
|
||||
duration: 480,
|
||||
from: { name: 'A', lat: 34.69, lon: 135.49 },
|
||||
to: { name: 'B', lat: 34.68, lon: 135.51 },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
const googleRoute = {
|
||||
routes: [
|
||||
{
|
||||
legs: [
|
||||
{
|
||||
steps: [
|
||||
{
|
||||
travelMode: 'TRANSIT',
|
||||
staticDuration: '480s',
|
||||
transitDetails: {
|
||||
stopDetails: {
|
||||
departureStop: { name: 'A', location: { latLng: { latitude: 34.69, longitude: 135.49 } } },
|
||||
arrivalStop: { name: 'B', location: { latLng: { latitude: 34.68, longitude: 135.51 } } },
|
||||
departureTime: '2026-09-10T09:00:00Z',
|
||||
arrivalTime: '2026-09-10T09:08:00Z',
|
||||
},
|
||||
transitLine: { vehicle: { type: 'SUBWAY' } },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
const okJson = (data: unknown) => ({ ok: true, status: 200, headers: { get: () => null }, json: async () => data });
|
||||
|
||||
describe.each(['google', 'transitous'] as const)('%s primary fallback', (primary) => {
|
||||
it.each(['success', 'primary-success', 'empty', 'secondary-error', 'primary-error'])('%s', async (outcome) => {
|
||||
await setAppSetting(orm, 'transit_provider', primary);
|
||||
await setAppSetting(orm, 'maps_api_key', 'instance-key');
|
||||
const secondary = primary === 'google' ? 'transitous' : 'google';
|
||||
fetchMock.mockImplementation(async (url: string) => {
|
||||
const provider = String(url).includes('googleapis') ? 'google' : 'transitous';
|
||||
if (
|
||||
(outcome === 'primary-error' && provider === primary) ||
|
||||
(outcome === 'secondary-error' && provider === secondary)
|
||||
) {
|
||||
return { ok: false, status: 500, headers: { get: () => null }, json: async () => ({}) };
|
||||
}
|
||||
const hasRoutes =
|
||||
(outcome === 'success' && provider === secondary) || (outcome === 'primary-success' && provider === primary);
|
||||
return okJson(hasRoutes ? (provider === 'google' ? googleRoute : motisRoute) : {});
|
||||
});
|
||||
const res = await request(server).get(nextPlanUrl()).set('Cookie', sessionCookie(ADMIN));
|
||||
expect(res.status).toBe(outcome === 'primary-error' ? 502 : 200);
|
||||
if (outcome !== 'primary-error') {
|
||||
expect(res.body.provider).toBe(outcome === 'success' ? secondary : primary);
|
||||
expect(res.body.itineraries).toHaveLength(['success', 'primary-success'].includes(outcome) ? 1 : 0);
|
||||
}
|
||||
expect(fetchMock).toHaveBeenCalledTimes(outcome.startsWith('primary-') ? 1 : 2);
|
||||
expect(String(fetchMock.mock.calls[0][0]).includes('googleapis')).toBe(primary === 'google');
|
||||
if (secondary === 'google' && outcome === 'success') {
|
||||
expect(fetchMock.mock.calls[1][1].headers['X-Goog-Api-Key']).toBe('instance-key');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it.each(['absent', 'personal-only', 'quota-exhausted'])('does not fall back to Google when %s', async (state) => {
|
||||
if (state === 'personal-only') await updateRows(orm, Users, { id: ADMIN }, { maps_api_key: 'personal-key' });
|
||||
if (state === 'quota-exhausted') {
|
||||
await setAppSetting(orm, 'maps_api_key', 'instance-key');
|
||||
vi.spyOn(app.get(GoogleQuotaService), 'exhausted').mockResolvedValue(true);
|
||||
}
|
||||
await planUpstream();
|
||||
expect(lastBody.provider).toBe('transitous');
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('uses the operator key for Google fallback', async () => {
|
||||
vi.stubEnv('PLACES_API_KEY', 'operator-key');
|
||||
try {
|
||||
fetchMock.mockResolvedValueOnce(okJson({})).mockResolvedValueOnce(okJson(googleRoute));
|
||||
await planUpstream();
|
||||
expect(lastBody.provider).toBe('google');
|
||||
expect(fetchMock.mock.calls[1][1].headers['X-Goog-Api-Key']).toBe('operator-key');
|
||||
} finally {
|
||||
vi.unstubAllEnvs();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user