/**
 * Ceph Adapter Tests
 */

import { describe, it, expect, beforeEach, vi } from 'vitest'
import { CephAdapter } from '../storage/ceph-adapter'

// Mock fetch globally
global.fetch = vi.fn()

describe('CephAdapter', () => {
  let adapter: CephAdapter

  beforeEach(() => {
    adapter = new CephAdapter({
      apiUrl: 'http://localhost:7480',
      accessKey: 'test-key',
      secretKey: 'test-secret',
    })
    vi.clearAllMocks()
  })

  describe('discoverResources', () => {
    it('should discover Ceph buckets and pools', async () => {
      const mockBuckets = {
        buckets: [
          {
            name: 'test-bucket',
            creation_date: new Date().toISOString(),
          },
        ],
      }

      ;(global.fetch as any).mockResolvedValue({
        ok: true,
        json: async () => mockBuckets,
      })

      const resources = await adapter.discoverResources()

      expect(resources.length).toBeGreaterThan(0)
      expect(resources.some((r) => r.type === 'bucket')).toBe(true)
    })
  })

  describe('healthCheck', () => {
    it('should return healthy when Ceph is accessible', async () => {
      ;(global.fetch as any).mockResolvedValue({
        ok: true,
        json: async () => ({ health: { status: 'HEALTH_OK' } }),
      })

      const health = await adapter.healthCheck()

      expect(health.status).toBe('healthy')
    })

    it('should return unhealthy when Ceph is inaccessible', async () => {
      ;(global.fetch as any).mockResolvedValue({
        ok: false,
        status: 500,
      })

      const health = await adapter.healthCheck()

      expect(health.status).toBe('unhealthy')
    })
  })
})

