import Fastify from 'fastify'
import privyPlugin, {getAuthorizationSignature} from '../../src/plugins/custom/privy.js'
import assert from 'node:assert'
import test from 'node:test'

test('getAuthorizationSignature generates valid signatures', async () => {
  const url = 'https://api.privy.io/v1/wallets'
  const body = { chain_type: 'ethereum' }
  
  const signature = getAuthorizationSignature({ url, body })
  
  assert.ok(signature && typeof signature === 'string', 'Signature should be a string')
  assert.ok(signature.length > 0, 'Signature should not be empty')
  
  // Check if signature matches base64 pattern
  const base64Regex = /^[A-Za-z0-9+/]+={0,2}$/
  assert.ok(base64Regex.test(signature), 'Signature should be a valid base64 string')
})

test('privy plugin decorates request with signPrivyMessage', async (t) => {
  const app = Fastify()
  
  t.after(() => {
    app.close()
  })
  
  await app.register(privyPlugin)
  
  app.get('/', (request) => {
    return { 
      decorated: {
        signPrivyMessage: !!request.signPrivyMessage,
      }
    }
  })
  
  const result = await app.inject({
    method: 'GET',
    url: '/'
  }).then(r => r.json())
  
  assert.equal(result.decorated.signPrivyMessage, true, 'Request should be decorated with signPrivyMessage method')
})
