Files
GearBox/tests/helpers/db.ts
Jean-Luc Makiola 050478c543 feat(16-01): update test helper to seed user and return { db, userId }
- createTestDb uses PGlite with drizzle-pg migrations
- Seeds test user with logtoSub and per-user Uncategorized category
- Returns { db, userId } instead of just db
- Add createSecondTestUser helper for cross-user isolation tests
2026-04-05 10:34:38 +02:00

41 lines
1.0 KiB
TypeScript

import { PGlite } from "@electric-sql/pglite";
import { drizzle } from "drizzle-orm/pglite";
import { migrate } from "drizzle-orm/pglite/migrator";
import * as schema from "../../src/db/schema.ts";
type Db = ReturnType<typeof drizzle<typeof schema>>;
export async function createTestDb() {
const client = new PGlite();
const db = drizzle(client, { schema });
// Apply all migrations to create tables
await migrate(db, { migrationsFolder: "./drizzle-pg" });
// Seed a test user
const [user] = await db
.insert(schema.users)
.values({ logtoSub: "test-user-sub" })
.returning();
// Seed per-user Uncategorized category
await db
.insert(schema.categories)
.values({ name: "Uncategorized", icon: "package", userId: user.id });
return { db, userId: user.id };
}
export async function createSecondTestUser(db: Db) {
const [user] = await db
.insert(schema.users)
.values({ logtoSub: "test-user-2-sub" })
.returning();
await db
.insert(schema.categories)
.values({ name: "Uncategorized", icon: "package", userId: user.id });
return user.id;
}