import { describe, it, expect } from 'vitest'
import { buildCredentialValue, extractFormValues } from '../utils'

describe('Credential Utilities', () => {
  describe('buildCredentialValue', () => {
    it('should build single-field credential value', () => {
      const result = buildCredentialValue('api-key', {
        apiKey: 'test-key-123'
      })
      expect(result).toEqual({ apiKey: 'test-key-123' })
    })

    it('should trim whitespace from values', () => {
      const result = buildCredentialValue('api-key', {
        apiKey: '  test-key  '
      })
      expect(result).toEqual({ apiKey: 'test-key' })
    })

    it('should throw error for OAuth types', () => {
      expect(() => buildCredentialValue('dropbox', { apiKey: 'test' })).toThrow(
        'buildCredentialValue should not be used for OAuth type: dropbox'
      )

      expect(() => buildCredentialValue('google-sheets', { apiKey: 'test' })).toThrow(
        'buildCredentialValue should not be used for OAuth type: google-sheets'
      )
    })

    it('should ignore undefined and null values', () => {
      const result = buildCredentialValue('api-key', {
        apiKey: 'valid-key',
        extra: undefined as unknown,
        another: null as unknown
      })
      // Only includes defined fields from schema
      expect(result).toEqual({
        apiKey: 'valid-key'
      })
    })

    it('should only include fields defined in schema', () => {
      const result = buildCredentialValue('api-key', {
        apiKey: 'test-key',
        extraField: 'should-not-be-included'
      })
      // Only apiKey is in api-key schema
      expect(result).toEqual({ apiKey: 'test-key' })
    })

    it('should build webhook-secret credential with signingSecret field', () => {
      const result = buildCredentialValue('webhook-secret', {
        signingSecret: 'whsec_abc123'
      })
      // webhook-secret schema uses 'signingSecret' key (matches gateway.ts lookup)
      expect(result).toEqual({ signingSecret: 'whsec_abc123' })
    })

    it('should trim whitespace from webhook-secret signingSecret', () => {
      const result = buildCredentialValue('webhook-secret', {
        signingSecret: '  whsec_abc123  '
      })
      expect(result).toEqual({ signingSecret: 'whsec_abc123' })
    })
  })

  describe('extractFormValues', () => {
    it('should extract string values from credential object', () => {
      const credentialValue = {
        apiKey: 'test-key',
        token: 'test-token'
      }
      const result = extractFormValues(credentialValue)
      expect(result).toEqual({
        apiKey: 'test-key',
        token: 'test-token'
      })
    })

    it('should convert non-string values to strings', () => {
      const credentialValue = {
        apiKey: 'test-key',
        count: 123,
        enabled: true
      }
      const result = extractFormValues(credentialValue)
      expect(result).toEqual({
        apiKey: 'test-key',
        count: '123',
        enabled: 'true'
      })
    })

    it('should skip null and undefined values', () => {
      const credentialValue = {
        apiKey: 'test-key',
        nullField: null,
        undefinedField: undefined
      }
      const result = extractFormValues(credentialValue)
      expect(result).toEqual({
        apiKey: 'test-key'
      })
    })

    it('should handle empty objects', () => {
      const result = extractFormValues({})
      expect(result).toEqual({})
    })

    it('should handle nested objects by converting to string', () => {
      const credentialValue = {
        apiKey: 'test-key',
        metadata: { nested: 'value' }
      }
      const result = extractFormValues(credentialValue)
      expect(result.apiKey).toBe('test-key')
      expect(result.metadata).toBe('[object Object]')
    })
  })

  describe('Integration: Round-trip conversion', () => {
    it('should preserve single-field credentials through round-trip', () => {
      const original = { apiKey: 'test-key-123' }
      const formValues = extractFormValues(original)
      const rebuilt = buildCredentialValue('api-key', formValues)
      expect(rebuilt).toEqual(original)
    })

    it('should handle whitespace during round-trip', () => {
      const original = { apiKey: 'test-key' }
      const formValuesWithSpace = { apiKey: '  test-key  ' }
      const rebuilt = buildCredentialValue('api-key', formValuesWithSpace)
      expect(rebuilt).toEqual(original)
    })

    it('should preserve webhook-secret credentials through round-trip', () => {
      const original = { signingSecret: 'whsec_abc123' }
      const formValues = extractFormValues(original)
      const rebuilt = buildCredentialValue('webhook-secret', formValues)
      expect(rebuilt).toEqual(original)
    })
  })
})
