Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/bugabinga/sessions_spec.lua

Raw
-- Reimplement encode/decode locally for testing (same logic as sessions.lua)
local encode = function ( data )
  vim.validate('data', data, 'string')
  -- replace some chars in base64, that are not allowed in file names.
  return vim.base64.encode( data ):gsub( '%+', '-' ):gsub( '%/', '.' ):gsub( '%=', '_' )
end

local decode = function ( data )
  vim.validate('data', data, 'string')
  return vim.base64.decode( data:gsub( '%-', '+' ):gsub( '%.', '/' ):gsub( '%_', '=' )
  )
end

describe('bugabinga.sessions encode/decode', function()
  describe('encode', function()
    it('should roundtrip hello correctly', function()
      local encoded = encode('hello')
      local decoded = decode(encoded)
      assert.equal('hello', decoded)
    end)

    it('should replace base64 / with . in test/path', function()
      -- base64 of "test/path" contains / which should become .
      local encoded = encode('test/path')
      -- Verify no / remains (it should be replaced with .)
      assert.is_not_matches('%/', encoded)
      -- Verify roundtrip works
      assert.equal('test/path', decode(encoded))
    end)

    it('should replace + and / in test+path/value', function()
      -- base64 of "test+path/value" contains + and / which should become - and .
      local encoded = encode('test+path/value')
      -- Verify no + or / remains
      assert.is_not_matches('%+', encoded)
      assert.is_not_matches('%/', encoded)
      -- Verify roundtrip works
      assert.equal('test+path/value', decode(encoded))
    end)

    it('should handle empty string', function()
      local encoded = encode('')
      assert.equal('', encoded)
    end)

    it('should roundtrip unicode correctly', function()
      local original = '日本語'
      local encoded = encode(original)
      local decoded = decode(encoded)
      assert.equal(original, decoded)
    end)

    it('should error when called with non-string (number)', function()
      local ok, err = pcall(encode, 123)
      assert.is_false(ok)
      assert.matches('data', err)
    end)
  end)

  describe('decode', function()
    it('should roundtrip hello correctly', function()
      local encoded = encode('hello')
      local decoded = decode(encoded)
      assert.equal('hello', decoded)
    end)

    it('should roundtrip unicode correctly', function()
      local original = '日本語'
      local encoded = encode(original)
      local decoded = decode(encoded)
      assert.equal(original, decoded)
    end)

    it('should error when called with non-string (nil)', function()
      local ok, err = pcall(decode, nil)
      assert.is_false(ok)
      assert.matches('data', err)
    end)
  end)
end)