48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
|
|
import { resolve } from 'node:path';
|
|
import { createDb, type Db } from '../db/client.js';
|
|
import { users, lessons, lessonSubscriptions } from '../db/schema.js';
|
|
import type { UserRow, LessonRow } from '../db/schema.js';
|
|
|
|
export function makeTestDb(): { db: Db; close: () => void } {
|
|
const { db, sqlite } = createDb(':memory:');
|
|
migrate(db, { migrationsFolder: resolve(import.meta.dirname, '../../drizzle') });
|
|
return { db, close: () => sqlite.close() };
|
|
}
|
|
|
|
export async function createUserDirect(
|
|
db: Db,
|
|
init: { email: string; displayName?: string; role?: 'user' | 'sysadmin'; passwordHash?: string | null; emailVerifiedAt?: number | null; isActive?: boolean }
|
|
): Promise<UserRow> {
|
|
const [row] = db.insert(users).values({
|
|
email: init.email,
|
|
displayName: init.displayName ?? 'Test User',
|
|
role: init.role ?? 'user',
|
|
passwordHash: init.passwordHash ?? null,
|
|
emailVerifiedAt: init.emailVerifiedAt ?? null,
|
|
isActive: init.isActive ?? true,
|
|
}).returning().all();
|
|
return row!;
|
|
}
|
|
|
|
export async function createLessonOwnedBy(
|
|
db: Db,
|
|
ownerId: number,
|
|
init: { name?: string; parentId?: number | null; visibility?: 'private' | 'shared'; isCurated?: boolean; bidirectional?: boolean } = {}
|
|
): Promise<LessonRow> {
|
|
const [row] = db.insert(lessons).values({
|
|
name: init.name ?? 'Test lesson',
|
|
parentId: init.parentId ?? null,
|
|
ownerId,
|
|
visibility: init.visibility ?? 'private',
|
|
isCurated: init.isCurated ?? false,
|
|
bidirectional: init.bidirectional ?? false,
|
|
position: 0,
|
|
}).returning().all();
|
|
return row!;
|
|
}
|
|
|
|
export async function subscribeUserToLesson(db: Db, userId: number, lessonId: number): Promise<void> {
|
|
db.insert(lessonSubscriptions).values({ userId, lessonId }).run();
|
|
}
|