import { describe, it, expect } from 'vitest'
import { generateHmacToken, verifyHmacToken, generateBrochureUrl } from './server/hmac'

describe('HMAC Token Utilities', () => {
  const TEST_SECRET = 'test-secret-key-12345'
  const TEST_CONTACT_ID = '550e8400-e29b-41d4-a716-446655440000'

  describe('generateHmacToken', () => {
    it('returns 16-character hex token', () => {
      const token = generateHmacToken('test-data', TEST_SECRET)
      expect(token).toHaveLength(16)
      expect(token).toMatch(/^[0-9a-f]{16}$/)
    })

    it('produces consistent tokens for same input', () => {
      const token1 = generateHmacToken('test@example.com', TEST_SECRET)
      const token2 = generateHmacToken('test@example.com', TEST_SECRET)
      expect(token1).toBe(token2)
    })

    it('normalizes input (lowercase and trim)', () => {
      const token1 = generateHmacToken('TEST@EXAMPLE.COM', TEST_SECRET)
      const token2 = generateHmacToken('  test@example.com  ', TEST_SECRET)
      const token3 = generateHmacToken('test@example.com', TEST_SECRET)
      expect(token1).toBe(token2)
      expect(token2).toBe(token3)
    })

    it('produces different tokens for different secrets', () => {
      const token1 = generateHmacToken('data', 'secret1')
      const token2 = generateHmacToken('data', 'secret2')
      expect(token1).not.toBe(token2)
    })

    it('produces different tokens for different data', () => {
      const token1 = generateHmacToken('data1', TEST_SECRET)
      const token2 = generateHmacToken('data2', TEST_SECRET)
      expect(token1).not.toBe(token2)
    })
  })

  describe('verifyHmacToken', () => {
    it('returns true for valid token', () => {
      const token = generateHmacToken(TEST_CONTACT_ID, TEST_SECRET)
      const isValid = verifyHmacToken(TEST_CONTACT_ID, token, TEST_SECRET)
      expect(isValid).toBe(true)
    })

    it('returns false for invalid token', () => {
      const isValid = verifyHmacToken(TEST_CONTACT_ID, 'invalid-token-00', TEST_SECRET)
      expect(isValid).toBe(false)
    })

    it('returns false for wrong length token', () => {
      const isValid = verifyHmacToken(TEST_CONTACT_ID, 'short', TEST_SECRET)
      expect(isValid).toBe(false)
    })

    it('returns false for wrong secret', () => {
      const token = generateHmacToken(TEST_CONTACT_ID, TEST_SECRET)
      const isValid = verifyHmacToken(TEST_CONTACT_ID, token, 'wrong-secret')
      expect(isValid).toBe(false)
    })

    it('verifies with normalized input', () => {
      const token = generateHmacToken('test@example.com', TEST_SECRET)
      const isValid = verifyHmacToken('  TEST@EXAMPLE.COM  ', token, TEST_SECRET)
      expect(isValid).toBe(true)
    })
  })

  describe('generateBrochureUrl', () => {
    it('generates URL with default base', () => {
      const url = generateBrochureUrl(TEST_CONTACT_ID, TEST_SECRET)
      expect(url).toMatch(/^https:\/\/brochure\.elevasis\.io\//)
      expect(url).toContain(TEST_CONTACT_ID)
      expect(url).toMatch(/\?t=[0-9a-f]{16}$/)
    })

    it('generates URL with custom base', () => {
      const customBase = 'https://custom.example.com/brochure'
      const url = generateBrochureUrl(TEST_CONTACT_ID, TEST_SECRET, customBase)
      expect(url).toMatch(/^https:\/\/custom\.example\.com\/brochure\//)
      expect(url).toContain(TEST_CONTACT_ID)
    })

    it('generates verifiable token in URL', () => {
      const url = generateBrochureUrl(TEST_CONTACT_ID, TEST_SECRET)
      const tokenMatch = url.match(/\?t=([0-9a-f]{16})$/)
      expect(tokenMatch).not.toBeNull()

      const extractedToken = tokenMatch![1]
      const isValid = verifyHmacToken(TEST_CONTACT_ID, extractedToken, TEST_SECRET)
      expect(isValid).toBe(true)
    })
  })
})
