import { describe, it, expect } from 'vitest'
import { OAUTH_PROVIDERS, getProviderConfig, listProviders } from '../provider-registry'

describe('OAuth Provider Registry', () => {
  describe('OAUTH_PROVIDERS', () => {
    it('should have Google Sheets configured', () => {
      expect(OAUTH_PROVIDERS['google-sheets']).toBeDefined()
      expect(OAUTH_PROVIDERS['google-sheets'].id).toBe('google-sheets')
      expect(OAUTH_PROVIDERS['google-sheets'].name).toBe('Google Sheets')
      expect(OAUTH_PROVIDERS['google-sheets'].authUrl).toBe('https://accounts.google.com/o/oauth2/v2/auth')
      expect(OAUTH_PROVIDERS['google-sheets'].tokenUrl).toBe('https://oauth2.googleapis.com/token')
      expect(OAUTH_PROVIDERS['google-sheets'].tokenExchange).toBe('form-encoded')
      expect(OAUTH_PROVIDERS['google-sheets'].authParams).toEqual({
        access_type: 'offline',
        prompt: 'consent'
      })
      expect(OAUTH_PROVIDERS['google-sheets'].scopes).toContain('https://www.googleapis.com/auth/spreadsheets')
    })

    it('should use correct token exchange methods', () => {
      // Verify Google Sheets uses form-encoded (standard OAuth 2.0)
      expect(OAUTH_PROVIDERS['google-sheets'].tokenExchange).toBe('form-encoded')

      // Verify Dropbox uses form-encoded
      expect(OAUTH_PROVIDERS.dropbox.tokenExchange).toBe('form-encoded')
    })
  })

  describe('getProviderConfig', () => {
    it('should return config for valid provider', () => {
      const config = getProviderConfig('google-sheets')
      expect(config).toBe(OAUTH_PROVIDERS['google-sheets'])
    })

    it('should throw for unknown provider', () => {
      expect(() => getProviderConfig('unknown')).toThrow('Unknown OAuth provider: unknown')
    })
  })

  describe('listProviders', () => {
    it('should return all providers as array', () => {
      const providers = listProviders()
      expect(providers).toHaveLength(Object.keys(OAUTH_PROVIDERS).length)
      expect(providers).toContainEqual(OAUTH_PROVIDERS['google-sheets'])
      expect(providers).toContainEqual(OAUTH_PROVIDERS['google-calendar'])
      expect(providers).toContainEqual(OAUTH_PROVIDERS.dropbox)
    })

    it('should return array with correct provider structure', () => {
      const providers = listProviders()
      providers.forEach((provider) => {
        expect(provider).toHaveProperty('id')
        expect(provider).toHaveProperty('name')
        expect(provider).toHaveProperty('authUrl')
        expect(provider).toHaveProperty('tokenUrl')
        expect(provider).toHaveProperty('tokenExchange')
      })
    })
  })
})
