import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { currentRole, ROLES } from './role'; const setUrl = (search: string) => history.pushState({}, '', search || '/'); // isDevMode() reads the `ngDevMode` global the Angular CLI defines away in a // production build. There is no ambient type for it in app code, so this is // accessed through an untyped bag rather than a `declare const`. const globals = globalThis as Record; const originalNgDevMode = globals['ngDevMode']; const setDevMode = (on: boolean) => { globals['ngDevMode'] = on; }; describe('currentRole (dev mechanism)', () => { beforeEach(() => { sessionStorage.clear(); setUrl('/'); setDevMode(true); }); afterEach(() => { globals['ngDevMode'] = originalNgDevMode; }); it('lists the three roles', () => { expect(ROLES).toEqual(['drafter', 'approver', 'admin']); }); it('reads a valid ?role= from the URL and persists it for the tab', () => { setUrl('?role=admin'); expect(currentRole()).toBe('admin'); setUrl('/'); // navigation drops the query param — value stays sticky expect(currentRole()).toBe('admin'); }); it('falls back to drafter when nothing is set or the value is invalid', () => { expect(currentRole()).toBe('drafter'); setUrl('?role=nonsense'); expect(currentRole()).toBe('drafter'); }); // BIO-012: the three hand-written `fetch` adapters call this function directly, // bypassing `roleInterceptor`'s own isDevMode()-gated registration — so the gate // has to hold here, not just at the interceptor, or `?role=` keeps working in a // production build through that side door. describe('outside isDevMode() (production build)', () => { beforeEach(() => setDevMode(false)); it('ignores a ?role= in the URL and returns the default', () => { setUrl('?role=admin'); expect(currentRole()).toBe('drafter'); }); it('never touches sessionStorage', () => { setUrl('?role=admin'); currentRole(); expect(sessionStorage.getItem('dev-role')).toBeNull(); }); it('ignores a role already sitting in sessionStorage from a prior dev session', () => { sessionStorage.setItem('dev-role', 'admin'); expect(currentRole()).toBe('drafter'); }); }); });