/**
 * MinIO Adapter Tests
 */

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

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

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

  beforeEach(() => {
    adapter = new MinIOAdapter({
      endpoint: 'http://localhost:9000',
      accessKey: 'minioadmin',
      secretKey: 'minioadmin',
    })
    vi.clearAllMocks()
  })

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

      // MinIO uses S3-compatible API
      ;(global.fetch as any).mockResolvedValue({
        ok: true,
        text: async () => {
          // Simulate XML response from S3 API
          return `<?xml version="1.0"?>
            <ListAllMyBucketsResult>
              ${mockBuckets.map((b) => `<Bucket><Name>${b.name}</Name></Bucket>`).join('')}
            </ListAllMyBucketsResult>`
        },
      })

      const resources = await adapter.discoverResources()

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

  describe('createResource', () => {
    it('should create a bucket', async () => {
      ;(global.fetch as any).mockResolvedValue({
        ok: true,
        status: 200,
      })

      const resource = await adapter.createResource({
        name: 'new-bucket',
        type: 'bucket',
        config: {},
      })

      expect(resource).toBeDefined()
      expect(resource.name).toBe('new-bucket')
    })
  })
})

