From e083e2d48086cb48a92cc40dde739efd89dd0952 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 18:03:33 +0100 Subject: [PATCH] test(db): run the scheduler gate, storage health context and uploads suites on the real database module They drop the SQLite snapshot mock and, where they seed, reset through resetDatabase and seed through the ORM factories, so they run in the Postgres lane as well. --- server/scripts/test-mocks-baseline.json | 4 -- .../tests/integration/scheduler-gate.test.ts | 12 +----- ...ge-health-notifier-request-context.test.ts | 7 ---- .../uploads-photo-request-context.test.ts | 38 +++++++++---------- .../tests/integration/uploads-static.test.ts | 9 +---- .../postgres/snapshot-suites-baseline.json | 4 -- 6 files changed, 20 insertions(+), 54 deletions(-) diff --git a/server/scripts/test-mocks-baseline.json b/server/scripts/test-mocks-baseline.json index f2716c2b7..40a4c3678 100644 --- a/server/scripts/test-mocks-baseline.json +++ b/server/scripts/test-mocks-baseline.json @@ -60,18 +60,14 @@ "tests/integration/legacy-upgrade-v244.test.ts": 1, "tests/integration/plugins/boot-registry-order.test.ts": 1, "tests/integration/plugins/plugin-runtime.test.ts": 1, - "tests/integration/scheduler-gate.test.ts": 1, "tests/integration/security.test.ts": 1, "tests/integration/settings.test.ts": 1, "tests/integration/share.test.ts": 1, - "tests/integration/storage-health-notifier-request-context.test.ts": 1, "tests/integration/systemNotices.test.ts": 1, "tests/integration/tags.test.ts": 1, "tests/integration/todo.test.ts": 1, "tests/integration/trip-access-primitives.test.ts": 1, "tests/integration/trips.test.ts": 1, - "tests/integration/uploads-photo-request-context.test.ts": 1, - "tests/integration/uploads-static.test.ts": 1, "tests/integration/vacay.test.ts": 1, "tests/setup.ts": 1, "tests/unit/auto-backup.test.ts": 1, diff --git a/server/tests/integration/scheduler-gate.test.ts b/server/tests/integration/scheduler-gate.test.ts index bc4e17a10..5f1cac11e 100644 --- a/server/tests/integration/scheduler-gate.test.ts +++ b/server/tests/integration/scheduler-gate.test.ts @@ -9,17 +9,11 @@ * timers. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { CronRegistrarService } from '../../src/nest/scheduling/cron-registrar.service'; import type { INestApplication } from '@nestjs/common'; import { SchedulerRegistry } from '@nestjs/schedule'; -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()); -}); +import { describe, it, expect, beforeAll } from 'vitest'; describe('SCHED-GATE — the harness boots without scheduling anything', () => { let app: INestApplication; @@ -28,10 +22,6 @@ describe('SCHED-GATE — the harness boots without scheduling anything', () => { app = await buildApp(); }); - afterAll(() => { - testDb.close(); - }); - it('SCHED-GATE-001 — the registrar reports the test gate closed', () => { expect(app.get(CronRegistrarService).isEnabled()).toBe(false); }); diff --git a/server/tests/integration/storage-health-notifier-request-context.test.ts b/server/tests/integration/storage-health-notifier-request-context.test.ts index 9e587614b..52340bf0c 100644 --- a/server/tests/integration/storage-health-notifier-request-context.test.ts +++ b/server/tests/integration/storage-health-notifier-request-context.test.ts @@ -21,17 +21,11 @@ * seven cron boot sweeps. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { StorageEventsService } from '../../src/nest/storage/storage-events.service'; import type { INestApplication } from '@nestjs/common'; 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('StorageHealthNotifierService listener runs inside its own request context, outside any active one', () => { let app: INestApplication; @@ -41,7 +35,6 @@ describe('StorageHealthNotifierService listener runs inside its own request cont afterAll(async () => { await app.close(); - testDb.close(); }); it('STORAGE-HEALTH-CTX-001: emitting replica_failure from a bare (non-request) context never logs cannotUseGlobalContext', async () => { diff --git a/server/tests/integration/uploads-photo-request-context.test.ts b/server/tests/integration/uploads-photo-request-context.test.ts index 295d40d6f..f520aa814 100644 --- a/server/tests/integration/uploads-photo-request-context.test.ts +++ b/server/tests/integration/uploads-photo-request-context.test.ts @@ -24,16 +24,17 @@ * succeed, not an accident of the test setup. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import { Photos } from '../../src/db/entities/Photos.entity'; import { Users } from '../../src/db/entities/Users.entity'; import { applyPlatformUploads } from '../../src/nest/platform/platform.routes'; import { StorageService } from '../../src/nest/storage/storage.service'; import { generateToken } from '../helpers/auth'; -import { createUser, createTrip } from '../helpers/factories'; +import type { FactoryOrm } from '../helpers/factories/context'; import { insertRow } from '../helpers/factories/rows'; +import { makeTrip } from '../helpers/factories/trips'; import { makeShareToken } from '../helpers/factories/trips'; -import { resetTestDb } from '../helpers/test-db'; +import { makeUser } from '../helpers/factories/users'; +import { resetDatabase } from '../helpers/reset-database'; import { MikroORM } from '@mikro-orm/core'; import type { INestApplication } from '@nestjs/common'; @@ -44,13 +45,10 @@ import path from 'node:path'; import request from 'supertest'; import { describe, it, expect, vi, beforeAll, afterAll, beforeEach } 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; +// The booted app's own ORM, the one the routes read through. +let orm: FactoryOrm; // A real file under the local driver's actual 'photos' root (DEFAULT_UPLOADS_ROOT, // server/uploads — same fixture pattern as tests/e2e/share.e2e.test.ts's @@ -64,6 +62,7 @@ beforeAll(async () => { fs.writeFileSync(photoFile, photoBytes); nestApp = await buildApp(); app = nestApp.getHttpAdapter().getInstance(); + orm = nestApp.get(MikroORM); }); afterAll(async () => { @@ -75,13 +74,13 @@ afterAll(async () => { } }); -beforeEach(() => { - resetTestDb(testDb); +beforeEach(async () => { + await resetDatabase(orm); }); describe('GET /uploads/photos/:filename — request context (Plan 3b Task 0, D6)', () => { it('PHOTOCTX-001: a valid JWT session still serves the photo — the wrap changes nothing observable', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const res = await request(app) .get(`/uploads/photos/${photoName}`) .set('Authorization', `Bearer ${generateToken(user.id)}`); @@ -90,9 +89,8 @@ describe('GET /uploads/photos/:filename — request context (Plan 3b Task 0, D6) }); it('PHOTOCTX-002: a repository read forced inside the handler succeeds under the wrap (load-bearing positive proof)', async () => { - const { user } = createUser(testDb); + const { user } = await makeUser(orm); const storage = nestApp.get(StorageService); - const orm = nestApp.get(MikroORM); let caught: unknown; let found: unknown; // storage.exists is the very first thing servePhoto calls — forcing a @@ -131,10 +129,10 @@ describe('GET /uploads/photos/:filename — request context (Plan 3b Task 0, D6) // These two cases drive the REAL pre-init route, anonymously, through the // real compiled `buildApp()` boot above — not a wrapper mock. it('PHOTOCTX-004 (R1): a valid share token serves the SAME photo bytes as the JWT path, byte-identical', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - await insertRow(nestApp.get(MikroORM), Photos, { trip: trip.id, filename: photoName, original_name: photoName }); - await makeShareToken(nestApp.get(MikroORM), trip.id, user.id, { token: 'ratchet-valid-token' }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await insertRow(orm, Photos, { trip: trip.id, filename: photoName, original_name: photoName }); + await makeShareToken(orm, trip.id, user.id, { token: 'ratchet-valid-token' }); const res = await request(app).get(`/uploads/photos/${photoName}?token=ratchet-valid-token`); @@ -143,9 +141,9 @@ describe('GET /uploads/photos/:filename — request context (Plan 3b Task 0, D6) }); it('PHOTOCTX-005 (R1): an invalid/unknown share token answers the legacy 401 — never `cannotUseGlobalContext`', async () => { - const { user } = createUser(testDb); - const trip = createTrip(testDb, user.id); - await insertRow(nestApp.get(MikroORM), Photos, { trip: trip.id, filename: photoName, original_name: photoName }); + const { user } = await makeUser(orm); + const trip = await makeTrip(orm, user.id); + await insertRow(orm, Photos, { trip: trip.id, filename: photoName, original_name: photoName }); // No matching share_tokens row for this token at all. const res = await request(app).get(`/uploads/photos/${photoName}?token=ratchet-unknown-token`); diff --git a/server/tests/integration/uploads-static.test.ts b/server/tests/integration/uploads-static.test.ts index 10b7abb96..5c082ed36 100644 --- a/server/tests/integration/uploads-static.test.ts +++ b/server/tests/integration/uploads-static.test.ts @@ -14,19 +14,13 @@ * public pages, and email-embedded avatars. No auth setup here is deliberate. */ import { buildApp } from '../../src/bootstrap'; -import { db as testDb } from '../../src/db/database'; import type { INestApplication } from '@nestjs/common'; import type { Application } from 'express'; import fs from 'fs'; import path from 'path'; 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()); -}); +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; let nestApp: INestApplication; let app: Application; @@ -64,7 +58,6 @@ beforeAll(async () => { afterAll(async () => { await nestApp.close(); - testDb.close(); for (const abs of created) fs.rmSync(abs, { force: true }); fs.rmSync(path.join(uploadsDir, 'avatars/.tmp'), { recursive: true, force: true }); fs.rmSync(path.join(uploadsDir, 'journey/parity-subdir'), { recursive: true, force: true }); diff --git a/server/tests/postgres/snapshot-suites-baseline.json b/server/tests/postgres/snapshot-suites-baseline.json index 2e4d1855f..52fe105e9 100644 --- a/server/tests/postgres/snapshot-suites-baseline.json +++ b/server/tests/postgres/snapshot-suites-baseline.json @@ -1,16 +1,12 @@ [ - "tests/integration/scheduler-gate.test.ts", "tests/integration/security.test.ts", "tests/integration/settings.test.ts", "tests/integration/share.test.ts", - "tests/integration/storage-health-notifier-request-context.test.ts", "tests/integration/systemNotices.test.ts", "tests/integration/tags.test.ts", "tests/integration/todo.test.ts", "tests/integration/trip-access-primitives.test.ts", "tests/integration/trips.test.ts", - "tests/integration/uploads-photo-request-context.test.ts", - "tests/integration/uploads-static.test.ts", "tests/integration/vacay.test.ts", "tests/unit/mcp/demo-tool-gate.test.ts", "tests/unit/mcp/tools-addon-gating.test.ts",