From 3572058917f2811f67b8db76494142156aa2ac89 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 16:06:58 +0100 Subject: [PATCH] test(db): run the feeds, atlas geocode, collections and Immich suites on the real database module They reset through resetDatabase and seed through the ORM factories instead of the SQLite snapshot mock, so they run in the Postgres lane as well. --- server/scripts/test-mocks-baseline.json | 4 --- .../atlas-geocode-request-context.test.ts | 30 +++++++--------- server/tests/integration/collections.test.ts | 26 ++++++-------- .../integration/feeds-request-context.test.ts | 28 +++++++-------- server/tests/integration/immich.test.ts | 35 ++++++++----------- .../postgres/snapshot-suites-baseline.json | 4 --- 6 files changed, 50 insertions(+), 77 deletions(-) diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index efa4be5a2..1a706dd7a 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -55,17 +55,13 @@ "tests/integration/admin.test.ts": 1, "tests/integration/assignments.test.ts": 1, "tests/integration/async-closure-truthiness-guards.test.ts": 1, - "tests/integration/atlas-geocode-request-context.test.ts": 1, "tests/integration/atlas.test.ts": 1, "tests/integration/auth.test.ts": 1, "tests/integration/backup.test.ts": 1, "tests/integration/collab.test.ts": 1, - "tests/integration/collections.test.ts": 1, "tests/integration/dayNotes.test.ts": 1, "tests/integration/dayReorder.test.ts": 1, - "tests/integration/feeds-request-context.test.ts": 1, "tests/integration/files.test.ts": 1, - "tests/integration/immich.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, diff --git a/server/tests/integration/atlas-geocode-request-context.test.ts b/server/tests/integration/atlas-geocode-request-context.test.ts index a0a764299..dfb771a99 100644 --- a/server/tests/integration/atlas-geocode-request-context.test.ts +++ b/server/tests/integration/atlas-geocode-request-context.test.ts @@ -20,14 +20,15 @@ * the same timing, and nothing reaches the error log. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { PlaceRegions } from '../../src/db/entities/PlaceRegions.entity'; import { PlaceRegionsRepository } from '../../src/db/repositories/PlaceRegions.repository'; import { authCookie } from '../helpers/auth'; -import { createUser, createTrip, createPlace } from '../helpers/factories'; import type { FactoryOrm } from '../helpers/factories/context'; +import { makePlace } from '../helpers/factories/places'; import { findRow, findRows } from '../helpers/factories/rows'; -import { resetTestDb } from '../helpers/test-db'; +import { makeTrip } from '../helpers/factories/trips'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -35,10 +36,6 @@ import type { Application } from 'express'; import request from 'supertest'; import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'; -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); vi.mock('../../src/nest/atlas/atlas-geo', async (orig) => { const actual = await orig(); return { @@ -71,15 +68,14 @@ beforeAll(async () => { }, 60_000); afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('AT4/AT29 background geocode request context', () => { it('IIFE-CTX-AT4 — /stats IIFE writes place_regions after the response, no cannotUseGlobalContext', async () => { - resetTestDb(testDb); - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const p = createPlace(testDb, trip.id, { lat: 48.85, lng: 2.35 }); + await resetDatabase(orm()); + const { user } = await makeUser(orm()); + const trip = await makeTrip(orm(), user.id); + const p = await makePlace(orm(), trip.id, { lat: 48.85, lng: 2.35 }); const res = await request(app).get('/api/addons/atlas/stats').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); expect(await findRow(orm(), PlaceRegions, { place: p.id })).toBeNull(); @@ -92,11 +88,11 @@ describe('AT4/AT29 background geocode request context', () => { }, 15_000); it('IIFE-CTX-AT29 — /regions IIFE writes two places sequentially (2nd after ~2.4s)', async () => { - resetTestDb(testDb); - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - const p1 = createPlace(testDb, trip.id, { lat: 48.85, lng: 2.35 }); - const p2 = createPlace(testDb, trip.id, { lat: 43.3, lng: 5.4 }); + await resetDatabase(orm()); + const { user } = await makeUser(orm()); + const trip = await makeTrip(orm(), user.id); + const p1 = await makePlace(orm(), trip.id, { lat: 48.85, lng: 2.35 }); + const p2 = await makePlace(orm(), trip.id, { lat: 43.3, lng: 5.4 }); const res = await request(app).get('/api/addons/atlas/regions').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); await new Promise((r) => setTimeout(r, 3500)); diff --git a/server/tests/integration/collections.test.ts b/server/tests/integration/collections.test.ts index 146a6a7ca..aa46d3b28 100644 --- a/server/tests/integration/collections.test.ts +++ b/server/tests/integration/collections.test.ts @@ -7,15 +7,15 @@ * statusCode-400 contract. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { Addons } from '../../src/db/entities/Addons.entity'; import { CollectionPlaces } from '../../src/db/entities/CollectionPlaces.entity'; import { Collections } from '../../src/db/entities/Collections.entity'; import { authCookie } from '../helpers/auth'; -import { createUser } from '../helpers/factories'; import type { FactoryOrm } from '../helpers/factories/context'; import { insertRow, upsertRow } from '../helpers/factories/rows'; -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'; @@ -23,12 +23,7 @@ import type { Application } from 'express'; import fs from 'fs'; import path from 'path'; 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; @@ -52,7 +47,7 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); // Enable the collections addon (the controller sits behind AddonGuard). await upsertRow(orm, Addons, { @@ -68,14 +63,13 @@ beforeEach(async () => { afterAll(async () => { await nestApp.close(); - testDb.close(); fs.rmSync(coversDir, { recursive: true, force: true }); fs.rmSync(placesDir, { recursive: true, force: true }); }); describe('Collection cover upload', () => { it('COLL-P01 — cover upload stores /uploads/covers/ and writes the file', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const collectionId = await createCollection(user.id); const res = await request(app) @@ -89,7 +83,7 @@ describe('Collection cover upload', () => { }); it('COLL-P02 — no file → 400 "No image uploaded"', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const collectionId = await createCollection(user.id); const res = await request(app) @@ -100,7 +94,7 @@ describe('Collection cover upload', () => { }); it('COLL-P03 — non-image cover is 500 (plain-Error filter quirk — pinned, do not "fix")', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const collectionId = await createCollection(user.id); const res = await request(app) @@ -113,7 +107,7 @@ describe('Collection cover upload', () => { describe('Collection place image upload', () => { it('COLL-P04 — place image upload stores /uploads/places/ and writes the file', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const collectionId = await createCollection(user.id); const placeId = await createCollectionPlace(collectionId, user.id); @@ -128,7 +122,7 @@ describe('Collection place image upload', () => { }); it('COLL-P05 — non-image place upload is 400 with the bespoke message', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const collectionId = await createCollection(user.id); const placeId = await createCollectionPlace(collectionId, user.id); diff --git a/server/tests/integration/feeds-request-context.test.ts b/server/tests/integration/feeds-request-context.test.ts index 0a714ae8b..9ed1799ab 100644 --- a/server/tests/integration/feeds-request-context.test.ts +++ b/server/tests/integration/feeds-request-context.test.ts @@ -42,12 +42,13 @@ * conversion regresses this. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { Trips } from '../../src/db/entities/Trips.entity'; import { Users } from '../../src/db/entities/Users.entity'; import { CalendarService } from '../../src/nest/calendar/calendar.service'; -import { createTrip, createUser } from '../helpers/factories'; import { updateRows } from '../helpers/factories/rows'; +import { makeTrip } from '../helpers/factories/trips'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -56,27 +57,23 @@ import { randomUUID } from 'node:crypto'; import request from 'supertest'; import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; -vi.mock('../../src/db/database', async () => { - const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); - return buildDbMock(createSnapshotTestDb()); -}); - describe('Anonymous ICS feed routes run inside a request context', () => { let app: INestApplication; + let orm: MikroORM; beforeAll(async () => { app = await buildApp(); + orm = app.get(MikroORM); + await resetDatabase(orm); }); afterAll(async () => { await app.close(); - testDb.close(); }); it('SEAM-FEED-001: GET /api/feed/trip/:token.ics for a VALID token performs a real repository read, inside the request, against the SAME MikroORM app.get(MikroORM) does', async () => { - const orm = app.get(MikroORM); - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Context-Proof Trip' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Context-Proof Trip' }); const token = randomUUID(); await updateRows(app.get(MikroORM), Trips, { id: trip.id }, { feed_token: token }); @@ -125,8 +122,8 @@ describe('Anonymous ICS feed routes run inside a request context', () => { * regression of this shape. */ it('SEAM-FEED-003 (mutation proof): a forced cannotUseGlobalContext throw inside the calendar builder degrades a VALID token to the SAME 404 body as a wrong one — the 404 alone cannot tell them apart', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id, { title: 'Mutation-Proof Trip' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id, { title: 'Mutation-Proof Trip' }); const token = randomUUID(); await updateRows(app.get(MikroORM), Trips, { id: trip.id }, { feed_token: token }); @@ -172,9 +169,8 @@ describe('Anonymous ICS feed routes run inside a request context', () => { * silently swallowed". */ it('SEAM-FEED-005: GET /api/feed/user/:token.ics for a VALID token performs a real repository read and the resulting event reaches the body, inside the request, against the SAME MikroORM app.get(MikroORM) does', async () => { - const orm = app.get(MikroORM); - const { user } = createUser(testDb); - createTrip(testDb, user.id, { title: 'User-Feed Context-Proof Trip' }); + const { user } = await makeUser(orm); + await makeTrip(orm, user.id, { title: 'User-Feed Context-Proof Trip' }); const token = randomUUID(); await updateRows(app.get(MikroORM), Users, { id: user.id }, { feed_token: token }); diff --git a/server/tests/integration/immich.test.ts b/server/tests/integration/immich.test.ts index 63815c261..602756963 100644 --- a/server/tests/integration/immich.test.ts +++ b/server/tests/integration/immich.test.ts @@ -6,18 +6,19 @@ * and input validation. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { PhotoProviders } from '../../src/db/entities/PhotoProviders.entity'; import { TrekPhotos } from '../../src/db/entities/TrekPhotos.entity'; import { TripAlbumLinks } from '../../src/db/entities/TripAlbumLinks.entity'; import { TripPhotos } from '../../src/db/entities/TripPhotos.entity'; import { authCookie } from '../helpers/auth'; -import { createUser } from '../helpers/factories'; import type { FactoryOrm } from '../helpers/factories/context'; import { addAlbumLink, addTripPhoto } from '../helpers/factories/photos'; import { findRow, findRows, updateRows } from '../helpers/factories/rows'; +import { setAddonEnabled } from '../helpers/factories/settings'; import { makeTrip } from '../helpers/factories/trips'; -import { resetTestDb, resetRateLimits, setAddonEnabled } 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'; @@ -25,11 +26,6 @@ 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()); -}); - // Mock SSRF guard: block loopback and private IPs, allow external hostnames without DNS. vi.mock('../../src/utils/ssrfGuard', async () => { const actual = await vi.importActual('../../src/utils/ssrfGuard'); @@ -65,10 +61,10 @@ beforeAll(async () => { }); beforeEach(async () => { - resetTestDb(testDb); + await resetDatabase(orm); await resetRateLimits(nestApp); // Providers only count as enabled under an enabled journey addon (migration 84 seeds it off). - setAddonEnabled(testDb, 'journey', true); + await setAddonEnabled(orm, 'journey', true); // The migrated snapshot seeds photo_providers.immich.enabled = 0 (an admin must // configure it before it's usable in production); the legacy test helper always // seeded it enabled, which is what these tests assume. Same convention @@ -78,12 +74,11 @@ beforeEach(async () => { afterAll(async () => { await nestApp.close(); - testDb.close(); }); describe('Immich settings', () => { it('IMMICH-001 — GET /api/integrations/memories/immich/settings returns current settings', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app).get('/api/integrations/memories/immich/settings').set('Cookie', authCookie(user.id)); expect(res.status).toBe(200); @@ -92,7 +87,7 @@ describe('Immich settings', () => { }); it('IMMICH-001 — PUT /api/integrations/memories/immich/settings saves Immich URL and API key', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .put('/api/integrations/memories/immich/settings') @@ -103,7 +98,7 @@ describe('Immich settings', () => { }); it('IMMICH-002 — PUT /api/integrations/memories/immich/settings with private IP is blocked by SSRF guard', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .put('/api/integrations/memories/immich/settings') @@ -113,7 +108,7 @@ describe('Immich settings', () => { }); it('IMMICH-002 — PUT /api/integrations/memories/immich/settings with loopback is blocked', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .put('/api/integrations/memories/immich/settings') @@ -139,7 +134,7 @@ describe('Immich authentication', () => { describe('Immich album links', () => { it('IMMICH-020 — POST album-links creates a link', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const trip = await makeTrip(orm, user.id, { title: 'Test Trip' }); const res = await request(app) @@ -157,7 +152,7 @@ describe('Immich album links', () => { }); it('IMMICH-021 — GET album-links returns linked albums', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const trip = await makeTrip(orm, user.id, { title: 'Test Trip' }); await addAlbumLink(orm, trip.id, user.id, 'immich', 'album-abc', 'My Album'); @@ -172,7 +167,7 @@ describe('Immich album links', () => { }); it('IMMICH-022 — DELETE album-links removes associated photos but not individually-added ones', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const trip = await makeTrip(orm, user.id, { title: 'Test Trip' }); // Create album link @@ -208,8 +203,8 @@ describe('Immich album links', () => { }); it('IMMICH-023 — DELETE album-link by non-member returns 404', async () => { - const { user: owner } = createUser(testDb); - const { user: other } = createUser(testDb); + const { user: owner } = await makeUser(orm); + const { user: other } = await makeUser(orm); const trip = await makeTrip(orm, owner.id, { title: 'Test Trip' }); const linkResult = await addAlbumLink(orm, trip.id, owner.id, 'immich', 'album-secret', 'Secret Album'); diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index 1a5a3e398..d27bdb587 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -2,16 +2,12 @@ "tests/integration/admin.test.ts", "tests/integration/assignments.test.ts", "tests/integration/async-closure-truthiness-guards.test.ts", - "tests/integration/atlas-geocode-request-context.test.ts", "tests/integration/atlas.test.ts", "tests/integration/auth.test.ts", "tests/integration/backup.test.ts", "tests/integration/collab.test.ts", - "tests/integration/collections.test.ts", "tests/integration/dayNotes.test.ts", - "tests/integration/feeds-request-context.test.ts", "tests/integration/files.test.ts", - "tests/integration/immich.test.ts", "tests/integration/journey-place-order.test.ts", "tests/integration/journey.test.ts", "tests/integration/leg-mode-incoming.test.ts",