import { describe, expect, it } from 'vitest' import type { Environment } from '../env.js' import type { AuthConfig } from './index.js' import { auth, createAuth } from './index.js' describe('auth Module', () => { describe('createAuth', () => { it('should create auth config with provided environment variables', () => { const mockEnv: Environment = { BETTER_AUTH_SECRET: 'test-secret', BETTER_AUTH_URL: 'https://example.com', DB: {} as D1Database, NODE_ENV: 'test', LOG_LEVEL: 'info', } const config = createAuth(mockEnv) expect(config).toEqual({ secret: 'test-secret', baseURL: 'https://example.com', database: mockEnv.DB, session: { expiresIn: 60 * 60 * 24 * 7, // 7 days updateAge: 60 * 60 * 24, // 1 day }, }) }) it('should use default values when environment variables are not provided', () => { const mockEnv: Environment = { NODE_ENV: 'development', LOG_LEVEL: 'debug', } as Environment const config = createAuth(mockEnv) expect(config.secret).toBe('default-secret-for-development') expect(config.baseURL).toBe('http://localhost:8787') expect(config.database).toBeUndefined() expect(config.session).toEqual({ expiresIn: 60 * 60 * 24 * 7, updateAge: 60 * 60 * 24, }) }) it('should handle partial environment configuration', () => { const mockEnv: Environment = { BETTER_AUTH_SECRET: 'partial-secret', NODE_ENV: 'test', LOG_LEVEL: 'info', } as Environment const config = createAuth(mockEnv) expect(config.secret).toBe('partial-secret') expect(config.baseURL).toBe('http://localhost:8787') }) }) describe('auth utilities', () => { describe('verifySession', () => { it('should return null for session verification (placeholder)', async () => { const result = await auth.verifySession('test-token') expect(result).toBeNull() }) it('should handle empty token', async () => { const result = await auth.verifySession('') expect(result).toBeNull() }) }) describe('createSession', () => { it('should return null for session creation (placeholder)', async () => { const result = await auth.createSession('user-123') expect(result).toBeNull() }) it('should handle empty userId', async () => { const result = await auth.createSession('') expect(result).toBeNull() }) }) describe('destroySession', () => { it('should return true for session destruction (placeholder)', async () => { const result = await auth.destroySession('test-token') expect(result).toBe(true) }) it('should handle empty token', async () => { const result = await auth.destroySession('') expect(result).toBe(true) }) }) }) describe('authConfig interface', () => { it('should accept valid auth config', () => { const config: AuthConfig = { secret: 'test-secret', baseURL: 'https://api.example.com', database: {} as D1Database, session: { expiresIn: 3600, updateAge: 1800, }, } expect(config.secret).toBe('test-secret') expect(config.baseURL).toBe('https://api.example.com') expect(config.session?.expiresIn).toBe(3600) expect(config.session?.updateAge).toBe(1800) }) it('should work with minimal config', () => { const config: AuthConfig = { secret: 'minimal-secret', baseURL: 'http://localhost:3000', } expect(config.secret).toBe('minimal-secret') expect(config.baseURL).toBe('http://localhost:3000') expect(config.database).toBeUndefined() expect(config.session).toBeUndefined() }) }) })