describe('image terminal measurements', function() local terminal, old_pane, old_system, old_uis, old_channel, old_module, old_ffi, old_os before_each(function() old_pane, old_system = vim.env.WEZTERM_PANE, vim.system old_uis, old_channel = vim.api.nvim_list_uis, vim.api.nvim_get_chan_info old_module = package.loaded['bugabinga.image.terminal'] old_ffi, old_os = package.loaded.ffi, jit.os vim.env.WEZTERM_PANE = '0' vim.api.nvim_list_uis = function() return { { chan = 1 } } end vim.api.nvim_get_chan_info = function() return { client = { name = 'nvim-tui' } } end package.loaded['bugabinga.image.terminal'] = nil terminal = require 'bugabinga.image.terminal' end) after_each(function() vim.env.WEZTERM_PANE, vim.system = old_pane, old_system vim.api.nvim_list_uis, vim.api.nvim_get_chan_info = old_uis, old_channel package.loaded['bugabinga.image.terminal'] = old_module package.loaded.ffi, jit.os = old_ffi, old_os end) it('selects the exact pane including ID zero', function() local size = terminal.decode({ { pane_id = 10, size = { cols = 1, rows = 1, pixel_width = 1, pixel_height = 1 } }, { pane_id = 0, size = { cols = 98, rows = 29, pixel_width = 980, pixel_height = 667 } }, }) assert.same({ cell_width = 10, cell_height = 23, explicit_size = true }, size) end) it('rejects zero or missing dimensions instead of guessing', function() assert.is_nil(terminal.decode({ { pane_id = 0, size = { cols = 80, rows = 24, pixel_width = 0 } } })) assert.is_nil(terminal.decode({ { pane_id = 5 } })) assert.is_nil(terminal.decode({ 5, { pane_id = 0, size = 1 } })) end) it('remeasures when another refresh invalidates an in-flight measurement', function() local calls, complete, received = 0, nil, 0 vim.system = function(args, options, callback) calls = calls + 1 assert.same({ 'wezterm', 'cli', 'list', '--format', 'json' }, args) assert.equal(2000, options.timeout) complete = callback return {} end local function callback(size, err) assert.is_nil(err) assert.equal(12, size.cell_width) received = received + 1 end terminal.refresh(callback) terminal.refresh(callback) assert.equal(1, calls) complete({ code = 0, stdout = vim.json.encode({ { pane_id = 0, size = { cols = 100, rows = 30, pixel_width = 1000, pixel_height = 690 }, } }) }) assert.is_true(vim.wait(500, function() return calls == 2 end)) assert.equal(0, received) complete({ code = 0, stdout = vim.json.encode({ { pane_id = 0, size = { cols = 100, rows = 30, pixel_width = 1200, pixel_height = 750 }, } }) }) assert.is_true(vim.wait(500, function() return received == 2 end)) assert.same({ cell_width = 12, cell_height = 25, explicit_size = true }, terminal.get()) end) it('uses native pixel measurements on Linux, macOS and BSD without a WezTerm dependency', function() vim.env.WEZTERM_PANE = nil local requested package.loaded.ffi = { typeof = function() return true end, new = function() return { row = 30, col = 100, xpixel = 1000, ypixel = 690 } end, C = { ioctl = function(_, request) requested = request; return 0 end }, } for name, request in pairs({ Linux = 0x5413, OSX = 0x40087468, BSD = 0x40087468 }) do jit.os = name assert.same({ cell_width = 10, cell_height = 23 }, terminal.get()) assert.equal(request, requested) end package.loaded.ffi.new = function() return { row = 30, col = 100, xpixel = 0, ypixel = 0 } end assert.is_nil(terminal.get()) jit.os = 'Windows' assert.is_nil(terminal.get()) end) it('reports unavailable terminal UIs and failed measurements clearly', function() vim.api.nvim_list_uis = function() return {} end local failure terminal.refresh(function(_, err) failure = err end) assert.is_true(vim.wait(500, function() return failure ~= nil end)) assert.matches('terminal UI', failure) vim.api.nvim_list_uis = function() return { { chan = 1 } } end vim.system = function(_, _, callback) callback({ code = 1, stderr = 'no mux' }) return {} end failure = nil terminal.refresh(function(_, err) failure = err end) assert.is_true(vim.wait(500, function() return failure ~= nil end)) assert.matches('no mux', failure) assert.is_nil(terminal.get()) end) end)