import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { clearSession, getConfigValue, getCurrentUserEmail, isLoggedIn, setCurrentUserEmail, setSessionValues } from '../../src/main/auth/session' describe('session', () => { const originalEnv = process.env.OPENROUTER_API_KEY beforeEach(() => { clearSession() }) afterEach(() => { clearSession() if (originalEnv === undefined) delete process.env.OPENROUTER_API_KEY else process.env.OPENROUTER_API_KEY = originalEnv }) it('should report not logged in and fall back to process.env before any session is set', () => { process.env.OPENROUTER_API_KEY = 'env-value' expect(isLoggedIn()).toBe(false) expect(getCurrentUserEmail()).toBeNull() expect(getConfigValue('OPENROUTER_API_KEY')).toBe('env-value') }) it('should return the session value instead of process.env once set', () => { process.env.OPENROUTER_API_KEY = 'env-value' setSessionValues(new Map([['OPENROUTER_API_KEY', 'session-value']])) expect(getConfigValue('OPENROUTER_API_KEY')).toBe('session-value') }) it('should report logged in with the given email once set', () => { setCurrentUserEmail('william@example.com') expect(isLoggedIn()).toBe(true) expect(getCurrentUserEmail()).toBe('william@example.com') }) it('should clear both the session values and the logged-in state', () => { setSessionValues(new Map([['OPENROUTER_API_KEY', 'session-value']])) setCurrentUserEmail('william@example.com') clearSession() expect(isLoggedIn()).toBe(false) expect(getCurrentUserEmail()).toBeNull() expect(getConfigValue('OPENROUTER_API_KEY')).toBeUndefined() }) it('should return undefined when neither session nor process.env have the variable', () => { delete process.env.SOME_VAR_THAT_DOES_NOT_EXIST expect(getConfigValue('SOME_VAR_THAT_DOES_NOT_EXIST')).toBeUndefined() }) })