/**
 * Unit tests for audit module — types + validation.
 */

import { describe, it, expect } from 'vitest';
import { CreateAuditSchema, QueryAuditSchema } from './types.js';

describe('CreateAuditSchema', () => {
  it('accepts valid input with defaults', () => {
    const result = CreateAuditSchema.safeParse({
      userId: 'user_123',
      action: 'login',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.category).toBe('general');
      expect(result.data.details).toEqual({});
    }
  });

  it('accepts full input', () => {
    const result = CreateAuditSchema.safeParse({
      userId: 'user_123',
      action: 'license_activated',
      category: 'security',
      details: { deviceId: 'dev_001', ip: '192.168.1.1' },
      ipAddress: '192.168.1.1',
      userAgent: 'Mozilla/5.0',
    });
    expect(result.success).toBe(true);
  });

  it('rejects missing action', () => {
    const result = CreateAuditSchema.safeParse({ userId: 'user_123' });
    expect(result.success).toBe(false);
  });

  it('rejects missing userId', () => {
    const result = CreateAuditSchema.safeParse({ action: 'login' });
    expect(result.success).toBe(false);
  });
});

describe('QueryAuditSchema', () => {
  it('accepts empty query with defaults', () => {
    const result = QueryAuditSchema.safeParse({});
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.days).toBe(30);
      expect(result.data.limit).toBe(100);
      expect(result.data.offset).toBe(0);
    }
  });

  it('accepts filters', () => {
    const result = QueryAuditSchema.safeParse({
      userId: 'user_123',
      action: 'login',
      category: 'security',
      days: 7,
      limit: 50,
    });
    expect(result.success).toBe(true);
  });

  it('rejects days > 365', () => {
    const result = QueryAuditSchema.safeParse({ days: 400 });
    expect(result.success).toBe(false);
  });
});
