import { describe, expect, it } from 'vitest'; import type { Grant } from './types'; import { grantValidationError } from './validate'; const base: Grant = { subjectType: 'user', subjectId: 'u1', role: 'editor', scopeType: 'pond', scopeId: null, effect: 'allow', }; describe('grantValidationError (issue #51)', () => { it('accepts a valid editor grant', () => { expect(grantValidationError(base, { pondType: 'shared' })).toBeNull(); }); it('accepts a valid pond_admin grant on a shared pond', () => { expect( grantValidationError({ ...base, role: 'pond_admin' }, { pondType: 'shared' }), ).toBeNull(); }); it('rejects pond_admin at label scope', () => { const g: Grant = { ...base, role: 'pond_admin', scopeType: 'label', scopeId: 'l1' }; expect(grantValidationError(g, { pondType: 'shared' })).toBe('grant_pond_admin_scope'); }); it('rejects pond_admin with a non-user subject', () => { const g: Grant = { ...base, role: 'pond_admin', subjectType: 'authenticated', subjectId: null }; expect(grantValidationError(g, { pondType: 'shared' })).toBe('grant_pond_admin_scope'); }); it('rejects a second admin on a personal pond', () => { const g: Grant = { ...base, role: 'pond_admin' }; expect(grantValidationError(g, { pondType: 'personal' })).toBe( 'grant_pond_admin_personal_pond', ); }); it('rejects a user subject without an id, and a non-user subject with one', () => { expect(grantValidationError({ ...base, subjectId: null }, { pondType: 'shared' })).toBe( 'grant_subject_id_mismatch', ); expect( grantValidationError( { ...base, subjectType: 'public', subjectId: 'x' }, { pondType: 'shared' }, ), ).toBe('grant_subject_id_mismatch'); }); it('rejects a scoped grant without an id, and a pond grant with one', () => { expect( grantValidationError({ ...base, scopeType: 'label', scopeId: null }, { pondType: 'shared' }), ).toBe('grant_scope_id_mismatch'); expect(grantValidationError({ ...base, scopeId: 'p1' }, { pondType: 'shared' })).toBe( 'grant_scope_id_mismatch', ); }); });