import { describe, it, expect } from 'vitest'
import { hashIp, evaluateFingerprint } from './fingerprint.js'

describe('hashIp', () => {
  it('same input+secret yields stable 16-char hex hash', async () => {
    const a = await hashIp('203.0.113.7', '2026-06-17', 'test-pii-key')
    const b = await hashIp('203.0.113.7', '2026-06-17', 'test-pii-key')
    expect(a).toBe(b)
    expect(a).toMatch(/^[0-9a-f]{16}$/)
  })

  it('different secret yields different hash', async () => {
    const a = await hashIp('203.0.113.7', '2026-06-17', 'secret-a')
    const b = await hashIp('203.0.113.7', '2026-06-17', 'secret-b')
    expect(a).not.toBe(b)
  })
})

describe('evaluateFingerprint', () => {
  it('malformed hash: pass through without blocking', () => {
    expect(evaluateFingerprint('not-a-valid-hash')).toEqual({
      isSuspicious: false,
      codes: [],
      setCookie: true,
    })
  })

  it('well-formed 16-char hex: pass', () => {
    expect(evaluateFingerprint('deadbeefcafebabe')).toEqual({
      isSuspicious: false,
      codes: [],
      setCookie: true,
    })
  })
})
