// Jest test setup file

// Mock Redis
jest.mock('redis', () => ({
  createClient: jest.fn(() => ({
    connect: jest.fn().mockResolvedValue(undefined),
    disconnect: jest.fn().mockResolvedValue(undefined),
    quit: jest.fn().mockResolvedValue(undefined),
    on: jest.fn(),
    get: jest.fn().mockResolvedValue(null),
    set: jest.fn().mockResolvedValue('OK'),
    del: jest.fn().mockResolvedValue(1),
    exists: jest.fn().mockResolvedValue(0),
    keys: jest.fn().mockResolvedValue([]),
    scan: jest.fn().mockResolvedValue(['0', []]),
    expire: jest.fn().mockResolvedValue(1),
    ttl: jest.fn().mockResolvedValue(-1)
  }))
}));

// Mock environment variables for testing
(process.env as any).NODE_ENV = 'test';
(process.env as any).JWT_SECRET = 'test-jwt-secret';
(process.env as any).JWT_REFRESH_SECRET = 'test-refresh-secret';
(process.env as any).DATABASE_URL = 'postgresql://test:test@localhost:5432/test_db';
(process.env as any).DB_HOST = 'localhost';
(process.env as any).DB_PORT = '5432';
(process.env as any).DB_NAME = 'test_db';
(process.env as any).DB_USER = 'test';
(process.env as any).DB_PASSWORD = 'test';
(process.env as any).REDIS_URL = 'redis://localhost:6379';
(process.env as any).LLM_PROVIDER = 'anthropic';
(process.env as any).ANTHROPIC_API_KEY = 'dummy_key';

// Global test timeout
jest.setTimeout(10000);

// Suppress console logs during tests unless there's an error
const originalConsoleLog = console.log;
const originalConsoleInfo = console.info;
const originalConsoleWarn = console.warn;

beforeAll(() => {
  console.log = jest.fn();
  console.info = jest.fn();
  console.warn = jest.fn();
});

afterAll(() => {
  console.log = originalConsoleLog;
  console.info = originalConsoleInfo;
  console.warn = originalConsoleWarn;
});

// Global test utilities
(global as any).testUtils = {
  // Helper to create mock database results
  createMockDbResult: (data: any) => ({
    rows: Array.isArray(data) ? data : [data],
    rowCount: Array.isArray(data) ? data.length : 1
  }),
  
  // Helper to create mock user data
  createMockUser: (overrides = {}) => ({
    id: '123e4567-e89b-12d3-a456-426614174000',
    email: 'test@example.com',
    name: 'Test User',
    password_hash: 'hashed_password',
    role: 'user',
    created_at: new Date(),
    updated_at: new Date(),
    is_active: true,
    ...overrides
  }),
  
  // Helper to create mock document data
  createMockDocument: (overrides = {}) => ({
    id: '123e4567-e89b-12d3-a456-426614174001',
    user_id: '123e4567-e89b-12d3-a456-426614174000',
    original_file_name: 'test.pdf',
    file_path: '/uploads/test.pdf',
    file_size: 1024000,
    uploaded_at: new Date(),
    status: 'uploaded',
    created_at: new Date(),
    updated_at: new Date(),
    ...overrides
  })
}; 