import { test } from '@japa/runner'
import { bufferToBase64, base64ToBuffer } from '../../../src/lib/Buffer.js'

test.group('Buffer/bufferToBase64()', () => {
  test('converts an Uint8Array to a string', ({ assert }) => {
    const encoder = new TextEncoder()
    const view = encoder.encode('Paweł Uchida-Psztyć')
    const result = bufferToBase64(view)
    assert.equal(result, 'UGF3ZcWCIFVjaGlkYS1Qc3p0ecSH')
  })

  test('converts an ArrayBuffer to a string', ({ assert }) => {
    const encoder = new TextEncoder()
    const view = encoder.encode('Paweł Uchida-Psztyć')
    // In node, accessing the `view.buffer` will lead to an error.
    const result = bufferToBase64(Uint8Array.from(view).buffer)
    assert.equal(result, 'UGF3ZcWCIFVjaGlkYS1Qc3p0ecSH')
  })

  test('converts an Buffer to a string', ({ assert }) => {
    const buffer = Buffer.from('Paweł Uchida-Psztyć')
    const result = bufferToBase64(buffer)
    assert.equal(result, 'UGF3ZcWCIFVjaGlkYS1Qc3p0ecSH')
  })
})

test.group('Buffer/base64ToBuffer()', () => {
  test('returns a Buffer', ({ assert }) => {
    const result = base64ToBuffer('UGF3ZcWCIFVjaGlkYS1Qc3p0ecSH') as Buffer
    const txt = result.toString('utf8')
    assert.equal(txt, 'Paweł Uchida-Psztyć')
  })
})
