import { expect } from 'vitest'
import { base64ToString, stringToBase64 } from './base64'

describe('base64 encoding', () => {
  test('to base64, simple code', async () => {
    const code = `extrusionDistance = 12`
    // Generated by online tool
    const expectedBase64 = `ZXh0cnVzaW9uRGlzdGFuY2UgPSAxMg==`

    const base64 = stringToBase64(code)
    expect(base64).toBe(expectedBase64)
  })

  test(`to base64, code with UTF-8 characters`, async () => {
    // example adapted from MDN docs: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem
    const code = `a = 5; Ā = 12.in(); 𐀀 = Ā; 文 = 12.0; 🦄 = -5;`
    // Generated by online tool
    const expectedBase64 = `YSA9IDU7IMSAID0gMTIuaW4oKTsg8JCAgCA9IMSAOyDmlocgPSAxMi4wOyDwn6aEID0gLTU7`

    const base64 = stringToBase64(code)
    expect(base64).toBe(expectedBase64)
  })

  // The following are simply the reverse of the above tests
  test('from base64, simple code', async () => {
    const base64 = `ZXh0cnVzaW9uRGlzdGFuY2UgPSAxMg==`
    const expectedCode = `extrusionDistance = 12`

    const code = base64ToString(base64)
    expect(code).toBe(expectedCode)
  })

  test(`from base64, code with UTF-8 characters`, async () => {
    const base64 = `YSA9IDU7IMSAID0gMTIuaW4oKTsg8JCAgCA9IMSAOyDmlocgPSAxMi4wOyDwn6aEID0gLTU7`
    const expectedCode = `a = 5; Ā = 12.in(); 𐀀 = Ā; 文 = 12.0; 🦄 = -5;`

    const code = base64ToString(base64)
    expect(code).toBe(expectedCode)
  })
})
