repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
neovim/tests/bugabinga/image/kitty_spec.lua
Rawdescribe('bugabinga.image.kitty', function()
local kitty
local sent
local original_send
local fixture_dir
local fixture_path
local function write_fixture(bytes)
local file = assert(io.open(fixture_path, 'wb'))
assert(file:write(bytes))
assert(file:close())
end
before_each(function()
package.loaded['bugabinga.image.kitty'] = nil
kitty = require 'bugabinga.image.kitty'
sent = {}
original_send = vim.api.nvim_ui_send
vim.api.nvim_ui_send = function(command)
sent[#sent + 1] = command
end
fixture_dir = vim.fn.tempname()
vim.fn.mkdir(fixture_dir, 'p')
fixture_path = vim.fs.joinpath(fixture_dir, 'upload.png')
end)
after_each(function()
vim.api.nvim_ui_send = original_send
vim.fn.delete(fixture_dir, 'rf')
end)
it('generates unique positive 32-bit image IDs', function()
local first = kitty.new_id()
local second = kitty.new_id()
assert.is_true(first >= 1 and first <= 0xFFFFFFFF)
assert.is_true(second >= 1 and second <= 0xFFFFFFFF)
assert.are_not_equal(first, second)
end)
it('streams base64 in bounded chunks with initial metadata and final marker', function()
local bytes = string.rep('a', 3072 * 2)
write_fixture(bytes)
assert.is_true(kitty.upload(17, fixture_path))
assert.equal(2, #sent)
assert.is_true(sent[1]:find('\27_Ga=t,f=100,t=d,i=17,m=1,q=1;', 1, true) == 1)
assert.is_true(sent[2]:find('\27_Gm=0,q=1;', 1, true) == 1)
local encoded_bytes = 0
for _, command in ipairs(sent) do
local payload = command:match(';(.*)\27\\$')
assert.is_true(#payload <= 4096)
encoded_bytes = encoded_bytes + #vim.base64.decode(payload)
end
assert.equal(#bytes, encoded_bytes)
end)
it('sends one final chunk when file fits in one block', function()
write_fixture('png-data')
assert.is_true(kitty.upload(18, fixture_path))
assert.equal(1, #sent)
assert.is_true(sent[1]:find('\27_Ga=t,f=100,t=d,i=18,m=0,q=1;', 1, true) == 1)
end)
it('places each placement independently while restoring the cursor', function()
local cursor = vim.api.nvim_win_get_cursor(0)
assert.is_true(kitty.place(21, 3, { row = 4, col = 8, cols = 12, rows = 5 }))
assert.is_true(kitty.place(21, 4, { row = 9, col = 2, cols = 6, rows = 7 }))
assert.equal(2, #sent)
assert.equal('\27[s\27[4;8H\27_Ga=p,i=21,p=3,c=12,C=1,q=1\27\\\27[u', sent[1])
assert.equal('\27[s\27[9;2H\27_Ga=p,i=21,p=4,c=6,C=1,q=1\27\\\27[u', sent[2])
assert.same(cursor, vim.api.nvim_win_get_cursor(0))
end)
it('constrains only height when height limits fitting, preserving pixel aspect ratio', function()
assert.is_true(kitty.place(21, 5, { row = 1, col = 1, cols = 12, rows = 5, axis = 'rows' }))
assert.matches('r=5,C=1', sent[1], 1, true)
assert.is_nil(sent[1]:find('c=', 1, true))
end)
it('can explicitly specify both dimensions for non-compliant terminals', function()
assert.is_true(kitty.place(21, 6, { row = 1, col = 1, cols = 12, rows = 5, axis = 'both' }))
assert.is_truthy(sent[1]:find('c=12,r=5,C=1', 1, true))
end)
it('deletes only requested placement or image data, never all images', function()
assert.is_true(kitty.delete(31, 7))
assert.is_true(kitty.delete(31))
assert.equal(2, #sent)
assert.equal('\27_Ga=d,d=i,i=31,p=7,q=1\27\\', sent[1])
assert.equal('\27_Ga=d,d=I,i=31,q=1\27\\', sent[2])
end)
it('returns errors for missing and empty files', function()
local ok, err = kitty.upload(42, fixture_path)
assert.is_nil(ok)
assert.is_truthy(err)
write_fixture('')
ok, err = kitty.upload(42, fixture_path)
assert.is_nil(ok)
assert.equal('image file is empty', err)
end)
end)