Phase 1: Monorepo scaffold, database, and dev environment
Turborepo monorepo with @forte/shared and @forte/backend workspaces. Docker Compose dev env with PostgreSQL 16 + Valkey 8. Fastify server with Pino JSON logging, request ID tracing, and health endpoint. Drizzle ORM with company + location tables. Includes: - Root config (turbo, tsconfig, eslint, prettier) - @forte/shared: types, schemas, currency/date utils - @forte/backend: Fastify entry, plugins (database, redis, cors, error-handler, dev-auth), health route, Drizzle schema + migration - Dev auth bypass via X-Dev-Company/Location/User headers - Vitest integration test with clean DB per test (forte_test) - Seed script for dev company + location
This commit is contained in:
29
packages/backend/src/test/helpers.ts
Normal file
29
packages/backend/src/test/helpers.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import type { FastifyInstance } from 'fastify'
|
||||
import { buildApp } from '../main.js'
|
||||
import { sql } from 'drizzle-orm'
|
||||
|
||||
/**
|
||||
* Build a fresh Fastify app instance for testing.
|
||||
* Each test gets its own app — no shared state.
|
||||
*/
|
||||
export async function createTestApp(): Promise<FastifyInstance> {
|
||||
const app = await buildApp()
|
||||
await app.ready()
|
||||
return app
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate all tables in the test database.
|
||||
* Call this in beforeEach to guarantee a clean slate per test.
|
||||
*/
|
||||
export async function cleanDb(app: FastifyInstance): Promise<void> {
|
||||
await app.db.execute(sql`
|
||||
DO $$ DECLARE
|
||||
r RECORD;
|
||||
BEGIN
|
||||
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
|
||||
EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE';
|
||||
END LOOP;
|
||||
END $$
|
||||
`)
|
||||
}
|
||||
26
packages/backend/src/test/setup.ts
Normal file
26
packages/backend/src/test/setup.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import postgres from 'postgres'
|
||||
|
||||
const TEST_DB_URL =
|
||||
process.env.DATABASE_URL?.replace(/\/forte$/, '/forte_test') ??
|
||||
'postgresql://forte:forte@localhost:5432/forte_test'
|
||||
|
||||
// Override DATABASE_URL for all tests to use forte_test
|
||||
process.env.DATABASE_URL = TEST_DB_URL
|
||||
process.env.NODE_ENV = 'test'
|
||||
process.env.LOG_LEVEL = 'silent'
|
||||
|
||||
/**
|
||||
* Ensure the forte_test database exists before tests run.
|
||||
*/
|
||||
async function ensureTestDb() {
|
||||
const adminUrl = TEST_DB_URL.replace(/\/forte_test$/, '/postgres')
|
||||
const sql = postgres(adminUrl)
|
||||
|
||||
const result = await sql`SELECT 1 FROM pg_database WHERE datname = 'forte_test'`
|
||||
if (result.length === 0) {
|
||||
await sql`CREATE DATABASE forte_test`
|
||||
}
|
||||
await sql.end()
|
||||
}
|
||||
|
||||
await ensureTestDb()
|
||||
Reference in New Issue
Block a user