import { describe, it, expect, vi } from 'vitest'

vi.mock('@/actions/push', () => ({
  subscribeToPushAction: vi.fn(),
  unsubscribeFromPushAction: vi.fn(),
}))

import { urlBase64ToUint8Array } from '@/lib/push-client'

describe('urlBase64ToUint8Array', () => {
  it('converts a base64url-encoded VAPID public key to Uint8Array', () => {
    // 65-byte uncompressed EC public key encoded as base64url (no padding)
    const base64url = 'BNMxB-LJm6XvGGiJSsYLdumcYiM7q9s_1aM9i5lI8lVzZ7GYJw1QkQFmrknwFsI4dI-e1iyvUhYHjNpHJKJD3oc'
    const result = urlBase64ToUint8Array(base64url)
    expect(result).toBeInstanceOf(Uint8Array)
    expect(result.length).toBe(65)
    expect(result[0]).toBe(0x04) // uncompressed EC point prefix
  })

  it('handles base64url with padding', () => {
    // simple known vector: "hello" = aGVsbG8= in base64
    const result = urlBase64ToUint8Array('aGVsbG8')
    expect(result).toBeInstanceOf(Uint8Array)
    expect(Array.from(result)).toEqual([104, 101, 108, 108, 111]) // "hello"
  })

  it('converts - and _ characters correctly', () => {
    // base64url uses - and _ instead of + and /
    const base64standard = 'AB+/AA=='
    const base64url = 'AB-_AA'
    const fromStd = urlBase64ToUint8Array(base64standard)
    const fromUrl = urlBase64ToUint8Array(base64url)
    expect(Array.from(fromStd)).toEqual(Array.from(fromUrl))
  })
})
