feat(perms): canRead/canEdit with ancestor walk + tests

This commit is contained in:
2026-05-21 00:07:05 +02:00
parent 262ac8b162
commit 66363b8094
3 changed files with 158 additions and 2 deletions

View File

@@ -1,8 +1,8 @@
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
import { resolve } from 'node:path';
import { createDb, type Db } from '../db/client.js';
import { users } from '../db/schema.js';
import type { UserRow } from '../db/schema.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:');
@@ -24,3 +24,24 @@ export async function createUserDirect(
}).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();
}