import { describe, it, expect } from 'vitest'; import { isAuthenticated, parseRollen, parseStoredPrincipal, Principal } from './principal'; const principal: Principal = { kind: 'medewerker', medewerkerId: 'medewerker-1', naam: 'Test', rollen: ['behandelaar'], }; describe('isAuthenticated', () => { it('narrows a present principal to Principal', () => { expect(isAuthenticated(principal)).toBe(true); }); it('reports no principal as not authenticated', () => { expect(isAuthenticated(null)).toBe(false); }); }); describe('parseStoredPrincipal', () => { it('returns null when nothing is stored', () => { expect(parseStoredPrincipal(null)).toBeNull(); }); it('returns null for a non-JSON string', () => { expect(parseStoredPrincipal('not json')).toBeNull(); }); it('returns null when the stored shape is wrong (no naam)', () => { expect( parseStoredPrincipal(JSON.stringify({ kind: 'medewerker', medewerkerId: 'medewerker-1' })), ).toBeNull(); }); it('returns null when kind is not medewerker', () => { expect( parseStoredPrincipal( JSON.stringify({ kind: 'zorgverlener', medewerkerId: 'medewerker-1', naam: 'Test', rollen: [], }), ), ).toBeNull(); }); it('returns null when rollen holds an unrecognized token', () => { expect( parseStoredPrincipal( JSON.stringify({ kind: 'medewerker', medewerkerId: 'medewerker-1', naam: 'Test', rollen: ['geen'], }), ), ).toBeNull(); }); it('restores a well-shaped stored principal as-is (no BSN to strip)', () => { const restored = parseStoredPrincipal(JSON.stringify(principal)); expect(restored).toEqual(principal); }); }); describe('parseRollen', () => { it('parses a single recognized rol', () => { expect(parseRollen('behandelaar')).toEqual(['behandelaar']); }); it('is case-insensitive and trims whitespace', () => { expect(parseRollen(' Behandelaar , behandelaar ')).toEqual(['behandelaar', 'behandelaar']); }); it('drops unrecognized tokens (the deny-path toggle, e.g. ?rollen=geen)', () => { expect(parseRollen('geen')).toEqual([]); }); it('returns an empty list for an empty string', () => { expect(parseRollen('')).toEqual([]); }); });