/**
 * @module tests/unit/universal/uuid
 * @description Pins the in-house UUID utilities (v4/v5/v7 + isUUID). v5 is checked
 * against a known RFC/python `uuid5(NAMESPACE_DNS, 'python.org')` vector — if the
 * bundled SHA-1 or the v5 byte-twiddling were wrong, this fails.
 */
import { describe, it, expect } from 'vitest'
import { v4, v5, v7, isUUID, BRAINY_ID_NAMESPACE } from '../../../src/universal/uuid.js'

describe('universal/uuid', () => {
  it('v5 matches the known DNS-namespace vector (SHA-1 correctness)', () => {
    // python: uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
    expect(v5('python.org', '6ba7b810-9dad-11d1-80b4-00c04fd430c8')).toBe(
      '886313e1-3b8a-5372-9b90-0c9aee199e5d'
    )
  })

  it('v5 is deterministic, valid, version-5, and input-sensitive', () => {
    const a = v5('user-123')
    expect(v5('user-123')).toBe(a) // deterministic
    expect(isUUID(a)).toBe(true)
    expect(a[14]).toBe('5') // version nibble
    expect(v5('user-124')).not.toBe(a) // different name → different id
    expect(BRAINY_ID_NAMESPACE).toBe('62726169-6e79-5000-8000-000000000000')
  })

  it('v7 is valid, version-7, unique, and time-sortable', () => {
    const a = v7()
    expect(isUUID(a)).toBe(true)
    expect(a[14]).toBe('7')
    const ids = Array.from({ length: 8 }, () => v7())
    expect(new Set(ids).size).toBe(8) // unique
    // the timestamp prefix makes lexical order track creation order
    const sorted = [...ids].sort()
    expect(sorted[sorted.length - 1] >= sorted[0]).toBe(true)
  })

  it('v4 is valid', () => {
    expect(isUUID(v4())).toBe(true)
  })

  it('isUUID rejects non-UUID strings', () => {
    expect(isUUID('user-123')).toBe(false)
    expect(isUUID('hello')).toBe(false)
    expect(isUUID('')).toBe(false)
  })
})
