diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index c442fcffb..011778ca4 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -53,7 +53,6 @@ "tests/e2e/vacay.e2e.test.ts": 1, "tests/e2e/weather.e2e.test.ts": 1, "tests/integration/dayReorder.test.ts": 1, - "tests/integration/journey-place-order.test.ts": 1, "tests/integration/journey.test.ts": 1, "tests/integration/leg-mode-incoming.test.ts": 1, "tests/integration/legacy-upgrade-v066.test.ts": 1, @@ -61,16 +60,13 @@ "tests/integration/legacy-upgrade-v205.test.ts": 1, "tests/integration/legacy-upgrade-v242.test.ts": 1, "tests/integration/legacy-upgrade-v244.test.ts": 1, - "tests/integration/maps.test.ts": 1, "tests/integration/mcp.test.ts": 1, "tests/integration/memories-immich.test.ts": 1, "tests/integration/memories-synology.test.ts": 1, "tests/integration/memories-unified.test.ts": 1, - "tests/integration/misc.test.ts": 1, "tests/integration/notifications.test.ts": 1, "tests/integration/oauth-authorize.test.ts": 1, "tests/integration/oauth.test.ts": 1, - "tests/integration/oidc-local-provider.test.ts": 1, "tests/integration/oidc.test.ts": 1, "tests/integration/orm-request-context-seams.test.ts": 1, "tests/integration/packing.test.ts": 1, diff --git a/server/tests/integration/journey-place-order.test.ts b/server/tests/integration/journey-place-order.test.ts index 7ea155ea7..33eb1fa71 100644 --- a/server/tests/integration/journey-place-order.test.ts +++ b/server/tests/integration/journey-place-order.test.ts @@ -17,26 +17,24 @@ * that stubs a macrotask hop and does catch a detached hook today. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { Addons } from '../../src/db/entities/Addons.entity'; import { JourneyEntries } from '../../src/db/entities/JourneyEntries.entity'; import { Places } from '../../src/db/entities/Places.entity'; import { invalidatePermissionsCache } from '../../src/nest/permissions/permissions-cache'; import { authCookie } from '../helpers/auth'; -import { createUser, createTrip, createPlace, createJourney, linkTripToJourney } from '../helpers/factories'; +import { linkTripToJourney, makeJourney } from '../helpers/factories/journeys'; +import { makePlace } from '../helpers/factories/places'; import { findRow, insertRow, upsertRow } from '../helpers/factories/rows'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { makeTrip } from '../helpers/factories/trips'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import type { Application } from 'express'; import request from 'supertest'; -import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; - -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; let nestApp: INestApplication; let app: Application; @@ -48,7 +46,7 @@ beforeAll(async () => { orm = nestApp.get(MikroORM); }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); await invalidatePermissionsCache(); // Enable the journey addon. @@ -64,16 +62,15 @@ beforeEach(async () => { }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('deleting a place detaches its journey entry ahead of the FK cascade', () => { it('annotates a filled entry with the removal note instead of leaving it silently orphaned', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id, { name: 'Café de Flore' }); - const journey = createJourney(testDb, user.id); - linkTripToJourney(testDb, journey.id, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id, { name: 'Café de Flore' }); + const journey = await makeJourney(orm, user.id); + await linkTripToJourney(orm, journey.id, trip.id); // A filled entry (not a bare skeleton) sourced from this place, the way // onPlaceDeleted documents: it survives the delete but gets detached and @@ -115,11 +112,11 @@ describe('deleting a place detaches its journey entry ahead of the FK cascade', }); it('deletes a content-less skeleton outright rather than leaving an orphaned row behind', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const place = createPlace(testDb, trip.id, { name: 'Musée Rodin' }); - const journey = createJourney(testDb, user.id); - linkTripToJourney(testDb, journey.id, trip.id); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + const place = await makePlace(orm, trip.id, { name: 'Musée Rodin' }); + const journey = await makeJourney(orm, user.id); + await linkTripToJourney(orm, journey.id, trip.id); const now = Date.now(); const entryId = await insertRow(orm, JourneyEntries, { diff --git a/server/tests/integration/maps.test.ts b/server/tests/integration/maps.test.ts index e19795bb9..748c1043f 100644 --- a/server/tests/integration/maps.test.ts +++ b/server/tests/integration/maps.test.ts @@ -6,24 +6,21 @@ * input validation level. Full integration tests would require live external APIs. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { MapsService } from '../../src/nest/maps/maps.service'; import { authCookie } from '../helpers/auth'; -import { createUser } from '../helpers/factories'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; +import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import type { Application } from 'express'; import request from 'supertest'; import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - let nestApp: INestApplication; let app: Application; +let orm: MikroORM; // Instance spies on the container's MapsService (permissions-migration // precedent) — the DI fold made the provider methods the outbound seam. // Defaults mirror the old module mock: bare resolves, except @@ -41,6 +38,7 @@ let mapsService: { beforeAll(async () => { nestApp = await buildApp(); app = nestApp.getHttpAdapter().getInstance(); + orm = nestApp.get(MikroORM); const maps = nestApp.get(MapsService); mapsService = { searchPlaces: vi.spyOn(maps, 'searchPlaces').mockResolvedValue(undefined as never), @@ -55,13 +53,12 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('Maps authentication', () => { @@ -78,21 +75,21 @@ describe('Maps authentication', () => { describe('Maps validation', () => { it('MAPS-001 — POST /maps/search without query returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).post('/api/maps/search').set('Cookie', authCookie(user.id)).send({}); expect(res.status).toBe(400); }); it('MAPS-006 — GET /maps/reverse without lat/lng returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).get('/api/maps/reverse').set('Cookie', authCookie(user.id)); expect(res.status).toBe(400); }); it('MAPS-007 — POST /maps/resolve-url without url returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).post('/api/maps/resolve-url').set('Cookie', authCookie(user.id)).send({}); expect(res.status).toBe(400); @@ -101,7 +98,7 @@ describe('Maps validation', () => { describe('Maps SSRF protection', () => { it('MAPS-007 — POST /maps/resolve-url with internal IP is blocked', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); // SSRF: should be blocked by ssrfGuard const res = await request(app) @@ -112,7 +109,7 @@ describe('Maps SSRF protection', () => { }); it('MAPS-007 — POST /maps/resolve-url with loopback IP is blocked', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/maps/resolve-url') @@ -124,7 +121,7 @@ describe('Maps SSRF protection', () => { describe('Maps happy paths (mocked service)', () => { it('MAPS-002 — POST /maps/search returns results from service', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.searchPlaces.mockResolvedValueOnce({ results: [{ address: 'Paris, France', source: 'nominatim' }], } as any); @@ -137,7 +134,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-003 — GET /maps/details/:placeId returns place details', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.getPlaceDetails.mockResolvedValueOnce({ name: 'Eiffel Tower', address: 'Champ de Mars, Paris', @@ -152,7 +149,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-004 — GET /maps/place-photo/:placeId returns photo url', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.getPlacePhoto.mockResolvedValueOnce({ url: 'https://example.com/photo.jpg', source: 'wikimedia', @@ -167,7 +164,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-005 — GET /maps/reverse returns geocoded location', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.reverseGeocode.mockResolvedValueOnce({ name: 'Eiffel Tower', address: 'Champ de Mars, Paris', @@ -180,7 +177,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-008 — POST /maps/resolve-url returns extracted coordinates', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.resolveGoogleMapsUrl.mockResolvedValueOnce({ lat: 48.8584, lng: 2.2945, @@ -197,7 +194,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-002 — search service error propagates correct status', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const err = Object.assign(new Error('No API key'), { status: 503 }); mapsService.searchPlaces.mockRejectedValueOnce(err); @@ -210,7 +207,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-003 — getPlaceDetails error returns 500', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.getPlaceDetails.mockRejectedValueOnce(new Error('External API failure')); const res = await request(app).get('/api/maps/details/some-place-id').set('Cookie', authCookie(user.id)); @@ -220,7 +217,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-004 — getPlacePhoto error with status returns that status', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.getPlacePhoto.mockRejectedValueOnce( Object.assign(new Error('Photo provider unavailable'), { status: 503 }), ); @@ -236,7 +233,7 @@ describe('Maps happy paths (mocked service)', () => { // A photo-less place must answer 200 { photoUrl: null }: one 404 per place turns // a normal itinerary render into a 404 storm that fail2ban/CrowdSec bans (#1727). it('MAPS-004b — a place with no photo answers 200 with photoUrl null', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.getPlacePhoto.mockResolvedValueOnce({ photoUrl: null, attribution: null }); const res = await request(app) @@ -251,7 +248,7 @@ describe('Maps happy paths (mocked service)', () => { // request per place on the next trip render — the second half of the same ban // vector. Nothing to serve is an empty 204, not a 404. it('MAPS-004c — the bytes proxy answers 204 for a photo that is not cached', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).get('/api/maps/place-photo/ChIJnot-cached/bytes').set('Cookie', authCookie(user.id)); @@ -260,7 +257,7 @@ describe('Maps happy paths (mocked service)', () => { }); it('MAPS-005 — reverseGeocode error returns null values', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.reverseGeocode.mockRejectedValueOnce(new Error('Geocode failed')); const res = await request(app).get('/api/maps/reverse?lat=48.8584&lng=2.2945').set('Cookie', authCookie(user.id)); @@ -278,14 +275,14 @@ describe('Maps autocomplete', () => { }); it('MAPS-010 — POST /maps/autocomplete without input returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).post('/api/maps/autocomplete').set('Cookie', authCookie(user.id)).send({}); expect(res.status).toBe(400); }); it('MAPS-011 — POST /maps/autocomplete with non-string input returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/maps/autocomplete') @@ -295,7 +292,7 @@ describe('Maps autocomplete', () => { }); it('MAPS-012 — POST /maps/autocomplete with invalid locationBias returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/maps/autocomplete') @@ -305,7 +302,7 @@ describe('Maps autocomplete', () => { }); it('MAPS-013 — POST /maps/autocomplete returns suggestions from service', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.autocompletePlaces.mockResolvedValueOnce({ suggestions: [{ placeId: 'ChIJ1234', mainText: 'Paris', secondaryText: 'France' }], source: 'google', @@ -323,7 +320,7 @@ describe('Maps autocomplete', () => { }); it('MAPS-014 — POST /maps/autocomplete passes lang and locationBias to service', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.autocompletePlaces.mockResolvedValueOnce({ suggestions: [], source: 'google', @@ -348,7 +345,7 @@ describe('Maps autocomplete', () => { }); it('MAPS-015 — autocomplete service error propagates correct status', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const err = Object.assign(new Error('Rate limited'), { status: 429 }); mapsService.autocompletePlaces.mockRejectedValueOnce(err); @@ -362,7 +359,7 @@ describe('Maps autocomplete', () => { }); it('MAPS-016 — autocomplete service error without status returns 500', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); mapsService.autocompletePlaces.mockRejectedValueOnce(new Error('Unknown')); const res = await request(app) @@ -374,7 +371,7 @@ describe('Maps autocomplete', () => { }); it('MAPS-017 — POST /maps/autocomplete with input > 200 chars returns 400', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .post('/api/maps/autocomplete') diff --git a/server/tests/integration/misc.test.ts b/server/tests/integration/misc.test.ts index 04b1d5eba..4d1777721 100644 --- a/server/tests/integration/misc.test.ts +++ b/server/tests/integration/misc.test.ts @@ -3,38 +3,35 @@ * Covers MISC-001, 002, 004, 007, 008, 009 to 014. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { authCookie } from '../helpers/auth'; -import { createUser } from '../helpers/factories'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; +import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import { TOUR_WRITE_MAX_BYTES } from '@trek/shared'; import type { Application } from 'express'; import request from 'supertest'; -import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest'; - -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); +import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'; let nestApp: INestApplication; let app: Application; +let orm: MikroORM; beforeAll(async () => { nestApp = await buildApp(); app = nestApp.getHttpAdapter().getInstance(); + orm = nestApp.get(MikroORM); }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); }); afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('Health check', () => { @@ -47,7 +44,7 @@ describe('Health check', () => { describe('Addons list', () => { it('MISC-002 — GET /api/addons returns enabled addons', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).get('/api/addons').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); diff --git a/server/tests/integration/oidc-local-provider.test.ts b/server/tests/integration/oidc-local-provider.test.ts index 454f75cfb..fd285e468 100644 --- a/server/tests/integration/oidc-local-provider.test.ts +++ b/server/tests/integration/oidc-local-provider.test.ts @@ -14,9 +14,10 @@ * reach; the admin lane OIDC rides on treats the two alike. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; -import { createUser } from '../helpers/factories'; -import { resetTestDb, resetRateLimits } from '../helpers/test-db'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; +import { resetRateLimits } from '../helpers/test-db'; +import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; import type { Application } from 'express'; @@ -28,10 +29,6 @@ import { describe, it, expect, vi, beforeAll, beforeEach, afterAll, afterEach } const { answers } = vi.hoisted(() => ({ answers: new Map() })); -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); // The provider's names are answered here; every other name goes to the real resolver. vi.mock('dns/promises', async (importOriginal) => { const real = await importOriginal(); @@ -137,6 +134,7 @@ function handle(req: http.IncomingMessage, res: http.ServerResponse) { let nestApp: INestApplication; let app: Application; +let orm: MikroORM; beforeAll(async () => { provider = http.createServer(handle); @@ -144,10 +142,11 @@ beforeAll(async () => { port = (provider.address() as AddressInfo).port; nestApp = await buildApp(); app = nestApp.getHttpAdapter().getInstance(); + orm = nestApp.get(MikroORM); }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); seen.length = 0; answers.clear(); @@ -167,7 +166,6 @@ afterEach(() => { afterAll(async () => { await nestApp.close(); await new Promise((resolve) => provider.close(resolve)); - testDb.close(); }); /** Point TREK at the provider under `host`, answering the name with `answer`. */ @@ -180,7 +178,7 @@ function useIssuer(host: string, answer: { address: string; family: number }[]): describe('OIDC login against a provider whose name has an fe80:: record (#2506)', () => { it('OIDC-LAN-001: signs in end to end, and every provider request lands on the vetted address', async () => { - const { user } = createUser(testDb, { email: EMAIL }); + const { user } = await makeUser(orm, { email: EMAIL }); // Link-local first, the order Windows and some LAN resolvers hand it out in. const issuer = useIssuer('idp.home.test', [ { address: 'fe80::1', family: 6 }, diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index db79fa24b..f7f656fd9 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -1,5 +1,4 @@ [ - "tests/integration/journey-place-order.test.ts", "tests/integration/journey.test.ts", "tests/integration/leg-mode-incoming.test.ts", "tests/integration/legacy-upgrade-v066.test.ts", @@ -7,16 +6,13 @@ "tests/integration/legacy-upgrade-v205.test.ts", "tests/integration/legacy-upgrade-v242.test.ts", "tests/integration/legacy-upgrade-v244.test.ts", - "tests/integration/maps.test.ts", "tests/integration/mcp.test.ts", "tests/integration/memories-immich.test.ts", "tests/integration/memories-synology.test.ts", "tests/integration/memories-unified.test.ts", - "tests/integration/misc.test.ts", "tests/integration/notifications.test.ts", "tests/integration/oauth-authorize.test.ts", "tests/integration/oauth.test.ts", - "tests/integration/oidc-local-provider.test.ts", "tests/integration/oidc.test.ts", "tests/integration/orm-request-context-seams.test.ts", "tests/integration/packing.test.ts",