describe('native image viewer', function() local loaded, old_global, root, file, calls, pending, cancelled local modules = { 'bugabinga.image', 'bugabinga.image.kitty', 'bugabinga.image.raster', 'bugabinga.image.terminal' } local function wait_for(predicate) assert.is_true(vim.wait(1000, predicate, 10)) end local function count(kind) return #vim.tbl_filter(function(call) return call.kind == kind end, calls) end before_each(function() root = vim.fn.tempname() vim.fn.mkdir(root, 'p') file = vim.fs.joinpath(root, 'image.png') vim.fn.writefile({ 'original image bytes' }, file) calls, pending, cancelled, loaded = {}, {}, {}, {} old_global = _G.BugabingaImage for _, name in ipairs(modules) do loaded[name] = package.loaded[name]; package.loaded[name] = nil end local id = 0 package.loaded['bugabinga.image.kitty'] = { new_id = function() id = id + 1; return id end, upload = function(image, path) calls[#calls + 1] = { kind = 'upload', image = image, path = path }; return true end, place = function(image, placement, rect) calls[#calls + 1] = { kind = 'place', image = image, placement = placement, rect = rect }; return true end, delete = function(image, placement) calls[#calls + 1] = { kind = 'delete', image = image, placement = placement }; return true end, } package.loaded['bugabinga.image.raster'] = { prepare = function(path, callback) local token = {} pending[#pending + 1] = { path = path, callback = callback, token = token } vim.schedule(function() if not cancelled[token] then callback(nil, { path = path, key = path, width = 600, height = 400 }) end end) return function() cancelled[token] = true end end, } package.loaded['bugabinga.image.terminal'] = { available = function() return true end, get = function() return { cell_width = 10, cell_height = 23 } end, refresh = function(callback) vim.schedule(function() callback({ cell_width = 10, cell_height = 23 }) end) end, } require 'bugabinga.image' vim.cmd.edit(vim.fn.fnameescape(file)) wait_for(function() return count('place') > 0 end) end) after_each(function() vim.api.nvim_exec_autocmds('VimLeavePre', { group = 'bugabinga_image' }) vim.api.nvim_del_augroup_by_name('bugabinga_image') vim.api.nvim_set_decoration_provider(vim.api.nvim_create_namespace('bugabinga_image'), {}) vim.cmd('silent! only!') for _, buf in ipairs(vim.api.nvim_list_bufs()) do if vim.api.nvim_buf_get_name(buf):find(root, 1, true) then pcall(vim.api.nvim_buf_delete, buf, { force = true }) end end vim.fn.delete(root, 'rf') for _, name in ipairs(modules) do package.loaded[name] = loaded[name] end _G.BugabingaImage = old_global end) it('uses a distinct placement for each window sharing one image upload', function() vim.cmd.vsplit() wait_for(function() return count('place') >= 3 end) local ids = {} for _, call in ipairs(calls) do if call.kind == 'place' then ids[call.placement] = true end end assert.equal(2, vim.tbl_count(ids)) assert.equal(1, count('upload')) local before = count('delete') vim.cmd.close() wait_for(function() return count('delete') > before end) local last = vim.tbl_filter(function(call) return call.kind == 'delete' end, calls) assert.is_number(last[#last].placement) end) it('releases hidden terminal data and restores it on return from the prepared raster', function() local image_buf = vim.api.nvim_get_current_buf() local before = count('place') vim.cmd.enew() wait_for(function() return count('delete') > 0 end) assert.is_true(#vim.tbl_filter(function(call) return call.kind == 'delete' and call.placement ~= nil end, calls) > 0) vim.cmd.buffer(image_buf) wait_for(function() return count('place') > before end) assert.equal(2, count('upload')) end) it('recomputes origin and available area when gutters or winbar change', function() local initial = calls[#calls].rect vim.wo.number = true vim.wo.signcolumn = 'yes:2' vim.wo.winbar = 'Header' vim.api.nvim_exec_autocmds('WinResized', {}) wait_for(function() local last = calls[#calls] return last.kind == 'place' and last.rect.row > initial.row and last.rect.col > initial.col end) local info = vim.fn.getwininfo(vim.api.nvim_get_current_win())[1] assert.equal(info.winrow + info.winbar, calls[#calls].rect.row) assert.equal(info.wincol + info.textoff, calls[#calls].rect.col) end) it('hides beneath overlapping native floats and restores after closure', function() local before = count('place') local buf = vim.api.nvim_create_buf(false, true) local win = vim.api.nvim_open_win(buf, false, { relative = 'editor', row = 2, col = 8, width = 20, height = 4, style = 'minimal' }) wait_for(function() return count('delete') > 0 end) vim.api.nvim_win_close(win, true) wait_for(function() return count('place') > before end) vim.api.nvim_buf_delete(buf, { force = true }) end) it('keeps working when quitting is aborted by an unsaved buffer', function() local image_buf = vim.api.nvim_get_current_buf() local before = count('place') vim.cmd.enew() wait_for(function() return count('delete') > 0 end) vim.bo.readonly = false vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'unsaved text' }) assert.is_false(pcall(vim.cmd, 'qall')) vim.bo.modified = false vim.cmd.buffer(image_buf) wait_for(function() return count('place') > before end) end) it('never renders hidden floating image windows', function() local image_buf = vim.api.nvim_get_current_buf() local before = count('place') local win = vim.api.nvim_open_win(image_buf, false, { relative = 'editor', row = 2, col = 8, width = 20, height = 4, style = 'minimal', hide = true }) local settled = false vim.schedule(function() settled = true end) wait_for(function() return settled end) assert.equal(before, count('place')) vim.api.nvim_win_close(win, true) end) it('recovers evicted images and reports owned terminal decoding errors', function() local image for _, call in ipairs(calls) do if call.kind == 'upload' then image = call.image end end vim.api.nvim_exec_autocmds('TermResponse', { data = { sequence = '\27_Gi=' .. image .. ';ENOENT: evicted' } }) wait_for(function() return count('upload') == 2 end) vim.api.nvim_exec_autocmds('TermResponse', { data = { sequence = '\27_Gi=' .. image .. ';EINVAL: bad PNG\nDecoder details' }, }) assert.same({ 'Image: EINVAL: bad PNG', 'Decoder details' }, vim.api.nvim_buf_get_lines(0, 0, -1, false)) assert.is_true(vim.bo.readonly) end) it('rejects writes without changing the source image', function() local ok = pcall(vim.cmd, 'write!') assert.is_false(ok) assert.same({ 'original image bytes' }, vim.fn.readfile(file)) end) it('cancels unloaded buffers and ignores late conversion callbacks', function() local second = vim.fs.joinpath(root, 'second.png') vim.fn.writefile({ 'second' }, second) vim.cmd.edit(vim.fn.fnameescape(second)) local job = pending[#pending] local buf = vim.api.nvim_get_current_buf() vim.api.nvim_buf_delete(buf, { force = true }) assert.is_true(cancelled[job.token]) local before = count('upload') job.callback(nil, { path = second, key = second, width = 1, height = 1 }) vim.wait(20) assert.equal(before, count('upload')) end) end)