/**
 * WAF Service Tests
 */

import { describe, it, expect, beforeEach, vi } from 'vitest'
import * as wafService from '../waf'
import type { Context } from '../../types/context'

describe('WAF Service', () => {
  let mockContext: Context
  let mockDb: any

  beforeEach(() => {
    mockDb = {
      query: vi.fn(),
    }

    mockContext = {
      db: mockDb as any,
      user: {
        id: 'user-1',
        email: 'test@example.com',
        name: 'Test User',
        role: 'ADMIN',
      },
    } as Context
  })

  describe('getPillars', () => {
    it('should return all pillars', async () => {
      const mockRows = [
        {
          id: 'pillar-1',
          code: 'SECURITY',
          name: 'Security',
          description: 'Security pillar',
          created_at: new Date(),
        },
      ]

      mockDb.query.mockResolvedValue({ rows: mockRows })

      const pillars = await wafService.getPillars(mockContext)

      expect(pillars).toHaveLength(1)
      expect(pillars[0].code).toBe('SECURITY')
    })
  })

  describe('getFindings', () => {
    it('should return findings with filters', async () => {
      const mockRows = [
        {
          id: 'finding-1',
          control_id: 'control-1',
          resource_id: 'resource-1',
          status: 'FAIL',
          severity: 'HIGH',
          title: 'Security finding',
          description: 'Description',
          recommendation: 'Fix it',
          created_at: new Date(),
          updated_at: new Date(),
        },
      ]

      mockDb.query.mockResolvedValue({ rows: mockRows })

      const findings = await wafService.getFindings(mockContext, {
        severity: 'HIGH',
      })

      expect(findings).toHaveLength(1)
      expect(findings[0].severity).toBe('HIGH')
    })
  })
})

