import { describe, expect, it } from 'vitest'; import { isOwner, isOwnerOnlyPath, isOwnerWritePath } from './owner'; const event = (user: Partial> | null) => ({ locals: { user, session: null } }) as unknown as { locals: App.Locals }; describe('owner gate', () => { it('is only the owner account', () => { expect(isOwner(event(null))).toBe(false); expect(isOwner(event({ role: 'admin', email: 'someone@example.com' }))).toBe(false); expect(isOwner(event({ role: 'user', email: 'Stan@StanRunge.dev' }))).toBe(true); }); it('matches owner-only paths', () => { expect(isOwnerOnlyPath('/trackers/health')).toBe(true); expect(isOwnerOnlyPath('/trackers/health/')).toBe(true); expect(isOwnerOnlyPath('/trackers/Health/import')).toBe(true); expect(isOwnerOnlyPath('/trackers/healthy')).toBe(false); expect(isOwnerOnlyPath('/trackers')).toBe(false); expect(isOwnerOnlyPath('/trackers/tasks')).toBe(true); expect(isOwnerOnlyPath('/trackers/Tasks/')).toBe(true); expect(isOwnerWritePath('/trackers/tasks')).toBe(true); }); it('gates the book reader, files and APIs but not the catalogue', () => { expect(isOwnerOnlyPath('/trackers/books/files/12')).toBe(true); expect(isOwnerOnlyPath('/trackers/books/12/read')).toBe(true); expect(isOwnerOnlyPath('/trackers/books/12/progress')).toBe(true); expect(isOwnerOnlyPath('/trackers/books/12/notes/3')).toBe(true); expect(isOwnerOnlyPath('/trackers/books')).toBe(false); expect(isOwnerOnlyPath('/trackers/books/12')).toBe(false); expect(isOwnerOnlyPath('/trackers/books/covers/12')).toBe(false); expect(isOwnerOnlyPath('/trackers/books/12/reader-notes-public')).toBe(false); }); it('only lets the owner write in owner areas', () => { expect(isOwnerWritePath('/trackers/books/12')).toBe(true); expect(isOwnerWritePath('/trackers/books')).toBe(true); expect(isOwnerWritePath('/trackers/health/import')).toBe(true); expect(isOwnerWritePath('/trackers/bookshelf')).toBe(false); expect(isOwnerWritePath('/lists/books')).toBe(false); }); });