From 2c7a5fcda68368f99559646198b9dd7c720da4c0 Mon Sep 17 00:00:00 2001 From: jubnl Date: Mon, 21 Sep 2026 11:03:16 +0200 Subject: [PATCH] test(server): websocket suites boot on the migrated schema snapshot --- server/tests/websocket/connection.test.ts | 33 +++----------------- server/tests/websocket/wire-envelope.test.ts | 27 +++------------- 2 files changed, 8 insertions(+), 52 deletions(-) diff --git a/server/tests/websocket/connection.test.ts b/server/tests/websocket/connection.test.ts index a58e6ff2b..a9f815e45 100644 --- a/server/tests/websocket/connection.test.ts +++ b/server/tests/websocket/connection.test.ts @@ -12,31 +12,10 @@ import request from 'supertest'; import WebSocket from 'ws'; import { broadcastToUser, getOnlineUserIds } from '../../src/websocket'; -const { testDb, dbMock } = vi.hoisted(() => { - const Database = require('better-sqlite3'); - const db = new Database(':memory:'); - db.exec('PRAGMA journal_mode = WAL'); - db.exec('PRAGMA foreign_keys = ON'); - db.exec('PRAGMA busy_timeout = 5000'); - const mock = { - db, - closeDb: () => {}, - reinitialize: () => {}, - getPlaceWithTags: (placeId: number) => { - const place: any = db.prepare(`SELECT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon FROM places p LEFT JOIN categories c ON p.category_id = c.id WHERE p.id = ?`).get(placeId); - if (!place) return null; - const tags = db.prepare(`SELECT t.* FROM tags t JOIN place_tags pt ON t.id = pt.tag_id WHERE pt.place_id = ?`).all(placeId); - return { ...place, category: place.category_id ? { id: place.category_id, name: place.category_name, color: place.category_color, icon: place.category_icon } : null, tags }; - }, - canAccessTrip: (tripId: any, userId: number) => - db.prepare(`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`).get(userId, tripId, userId), - isOwner: (tripId: any, userId: number) => - !!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId), - }; - return { testDb: db, dbMock: mock }; +vi.mock('../../src/db/database', async () => { + const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); + return buildDbMock(createSnapshotTestDb()); }); - -vi.mock('../../src/db/database', () => dbMock); vi.mock('../../src/config', () => ({ JWT_SECRET: 'test-jwt-secret-for-trek-testing-only', ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2', @@ -49,8 +28,7 @@ vi.mock('../../src/config', () => ({ import type { INestApplication } from '@nestjs/common'; import { buildApp, getHttpServer } from '../../src/bootstrap'; -import { createTables } from '../../src/db/schema'; -import { runMigrations } from '../../src/db/migrations'; +import { db as testDb } from '../../src/db/database'; import { resetTestDb, resetRateLimits } from '../helpers/test-db'; import { createUser, createTrip } from '../helpers/factories'; import { authCookie } from '../helpers/auth'; @@ -71,9 +49,6 @@ let wsUrl: string; let nestApp: INestApplication; beforeAll(async () => { - createTables(testDb); - runMigrations(testDb); - // Real WebSocket against the unified NestJS app (Express is gone). buildApp owns // the same composition production uses; we attach the real ws server to it. nestApp = await buildApp(); diff --git a/server/tests/websocket/wire-envelope.test.ts b/server/tests/websocket/wire-envelope.test.ts index 3b129538a..c827a6131 100644 --- a/server/tests/websocket/wire-envelope.test.ts +++ b/server/tests/websocket/wire-envelope.test.ts @@ -19,26 +19,10 @@ import { describe, it, expect, vi, beforeAll, afterAll, beforeEach } from 'vites import http from 'http'; import WebSocket from 'ws'; -const { testDb, dbMock } = vi.hoisted(() => { - const Database = require('better-sqlite3'); - const db = new Database(':memory:'); - db.exec('PRAGMA journal_mode = WAL'); - db.exec('PRAGMA foreign_keys = ON'); - db.exec('PRAGMA busy_timeout = 5000'); - const mock = { - db, - closeDb: () => {}, - reinitialize: () => {}, - getPlaceWithTags: () => null, - canAccessTrip: (tripId: any, userId: number) => - db.prepare(`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`).get(userId, tripId, userId), - isOwner: (tripId: any, userId: number) => - !!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId), - }; - return { testDb: db, dbMock: mock }; +vi.mock('../../src/db/database', async () => { + const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock'); + return buildDbMock(createSnapshotTestDb()); }); - -vi.mock('../../src/db/database', () => dbMock); vi.mock('../../src/config', () => ({ JWT_SECRET: 'test-jwt-secret-for-trek-testing-only', ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2', @@ -51,8 +35,7 @@ vi.mock('../../src/config', () => ({ import type { INestApplication } from '@nestjs/common'; import { buildApp, getHttpServer } from '../../src/bootstrap'; -import { createTables } from '../../src/db/schema'; -import { runMigrations } from '../../src/db/migrations'; +import { db as testDb } from '../../src/db/database'; import { resetTestDb } from '../helpers/test-db'; import { createUser, createTrip } from '../helpers/factories'; import { broadcast, broadcastToUser } from '../../src/websocket'; @@ -63,8 +46,6 @@ let wsUrl: string; let nestApp: INestApplication; beforeAll(async () => { - createTables(testDb); - runMigrations(testDb); nestApp = await buildApp(); // buildApp binds /ws to the server it creates, before app.init(). server = getHttpServer();