import { describe, it, expect } from 'vitest'
import { constantTimeEqual, constantTimeEqualBytes } from '../src/security/compare.js'

describe('constantTimeEqual', () => {
  it('returns true for identical strings', () => {
    expect(constantTimeEqual('correct-horse', 'correct-horse')).toBe(true)
  })

  it('returns false for different strings', () => {
    expect(constantTimeEqual('correct-horse', 'battery-staple')).toBe(false)
  })

  it('returns false for different-length strings (no length oracle)', () => {
    expect(constantTimeEqual('abc', 'abcdef')).toBe(false)
  })

  it('returns false when either side is empty or undefined', () => {
    expect(constantTimeEqual('', 'x')).toBe(false)
    expect(constantTimeEqual('x', '')).toBe(false)
    expect(constantTimeEqual(undefined, 'x')).toBe(false)
    expect(constantTimeEqual('x', undefined)).toBe(false)
  })
})

describe('constantTimeEqualBytes', () => {
  it('true for equal buffers, false for differing or mismatched length', () => {
    expect(constantTimeEqualBytes(Buffer.from('aa'), Buffer.from('aa'))).toBe(true)
    expect(constantTimeEqualBytes(Buffer.from('aa'), Buffer.from('ab'))).toBe(false)
    expect(constantTimeEqualBytes(Buffer.from('aa'), Buffer.from('aaa'))).toBe(false)
  })
})
