robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
7.9 KiB299 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local eq = t.eq
6
7local clear = n.clear
8local exec_lua = n.exec_lua
9
10---4x4 PNG image bytes.
11---@type string
12-- stylua: ignore
13local PNG_IMG_BYTES = string.char(unpack({
14  137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 4, 0,
15  0, 0, 4, 8, 6, 0, 0, 0, 169, 241, 158, 126, 0, 0, 0, 1, 115, 82, 71, 66, 0,
16  174, 206, 28, 233, 0, 0, 0, 39, 73, 68, 65, 84, 8, 153, 99, 252, 207, 192,
17  240, 159, 129, 129, 129, 193, 226, 63, 3, 3, 3, 3, 3, 3, 19, 3, 26, 96, 97,
18  156, 1, 145, 250, 207, 184, 12, 187, 10, 0, 36, 189, 6, 125, 75, 9, 40, 46,
19  0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
20}))
21
22---@param s string
23---@return string
24local function escape_ansi(s)
25  return (
26    string.gsub(s, '.', function(c)
27      local byte = string.byte(c)
28      if byte < 32 or byte == 127 then
29        return string.format('\\%03d', byte)
30      else
31        return c
32      end
33    end)
34  )
35end
36
37---@param s string
38---@return string
39local function base64_encode(s)
40  return exec_lua(function()
41    return vim.base64.encode(s)
42  end)
43end
44
45---Mock nvim_ui_send to capture escape sequence output.
46local function setup_img_api()
47  exec_lua(function()
48    _G.data = {}
49    local original_ui_send = vim.api.nvim_ui_send
50    vim.api.nvim_ui_send = function(d)
51      table.insert(_G.data, d)
52    end
53    _G._original_ui_send = original_ui_send
54  end)
55end
56
57---@param esc string
58---@param opts? {strict?:boolean}
59---@return {i:integer, j:integer, control:table<string, string>, data:string|nil}
60local function parse_kitty_seq(esc, opts)
61  opts = opts or {}
62  local i, j, c, d = string.find(esc, '\027_G([^;\027]+)([^\027]*)\027\\')
63  assert(c, 'invalid kitty escape sequence: ' .. escape_ansi(esc))
64
65  if opts.strict then
66    assert(i == 1, 'not starting with kitty graphics sequence: ' .. escape_ansi(esc))
67  end
68
69  ---@type table<string, string>
70  local control = {}
71  local idx = 0
72  while true do
73    local k, v, _
74    idx, _, k, v = string.find(c, '(%a+)=([^,]+),?', idx + 1)
75    if idx == nil then
76      break
77    end
78    if k and v then
79      control[k] = v
80    end
81  end
82
83  ---@type string|nil
84  local payload
85  if d and d ~= '' then
86    payload = string.sub(d, 2)
87  end
88
89  return { i = i, j = j, control = control, data = payload }
90end
91
92describe('vim.ui.img', function()
93  before_each(function()
94    clear()
95    setup_img_api()
96  end)
97
98  it('can set an image', function()
99    local esc_codes = exec_lua(function()
100      _G.data = {}
101      vim.ui.img.set(PNG_IMG_BYTES, {
102        col = 1,
103        row = 2,
104        width = 3,
105        height = 4,
106        zindex = 123,
107      })
108      return table.concat(_G.data)
109    end)
110
111    -- Transmit image bytes
112    local seq = parse_kitty_seq(esc_codes, { strict = true })
113    local image_id = seq.control.i
114    eq({
115      f = '100',
116      a = 't',
117      t = 'd',
118      i = image_id,
119      q = '2',
120      m = '0',
121    }, seq.control, 'transmit image control data')
122    eq(base64_encode(PNG_IMG_BYTES), seq.data)
123    esc_codes = string.sub(esc_codes, seq.j + 1)
124
125    -- Cursor save
126    eq(escape_ansi('\0277'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor save')
127    esc_codes = string.sub(esc_codes, 3)
128
129    -- Cursor hide
130    eq(escape_ansi('\027[?25l'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor hide')
131    esc_codes = string.sub(esc_codes, 7)
132
133    -- Cursor move
134    eq(escape_ansi('\027[2;1H'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor movement')
135    esc_codes = string.sub(esc_codes, 7)
136
137    -- Place image
138    seq = parse_kitty_seq(esc_codes, { strict = true })
139    eq({
140      a = 'p',
141      i = image_id,
142      p = seq.control.p,
143      C = '1',
144      q = '2',
145      c = '3',
146      r = '4',
147      z = '123',
148    }, seq.control, 'display image control data')
149    esc_codes = string.sub(esc_codes, seq.j + 1)
150
151    -- Cursor restore
152    eq(escape_ansi('\0278'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor restore')
153    esc_codes = string.sub(esc_codes, 3)
154
155    -- Cursor show
156    eq(escape_ansi('\027[?25h'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor show')
157  end)
158
159  it('can get image info', function()
160    local result = exec_lua(function()
161      local id = vim.ui.img.set(PNG_IMG_BYTES, {
162        row = 5,
163        col = 10,
164        width = 20,
165        height = 15,
166        zindex = 42,
167      })
168
169      return {
170        info = vim.ui.img.get(id),
171        missing = vim.ui.img.get(999999),
172      }
173    end)
174
175    eq({ row = 5, col = 10, width = 20, height = 15, zindex = 42 }, result.info)
176    eq(nil, result.missing)
177  end)
178
179  it('can update an image', function()
180    local result = exec_lua(function()
181      local id = vim.ui.img.set(PNG_IMG_BYTES, {
182        row = 1,
183        col = 1,
184        width = 10,
185        height = 20,
186        zindex = 99,
187      })
188
189      _G.data = {}
190      vim.ui.img.set(id, {
191        col = 5,
192        row = 6,
193        width = 7,
194        height = 8,
195        zindex = 9,
196      })
197      local esc_codes = table.concat(_G.data)
198
199      -- Partial update: only change row, other fields preserved
200      vim.ui.img.set(id, { row = 50 })
201      local info = vim.ui.img.get(id)
202
203      return { esc_codes = esc_codes, info = info }
204    end)
205
206    -- Verify partial update merged opts
207    eq({ row = 50, col = 5, width = 7, height = 8, zindex = 9 }, result.info)
208
209    local esc_codes = result.esc_codes
210
211    -- Cursor save
212    eq(escape_ansi('\0277'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor save')
213    esc_codes = string.sub(esc_codes, 3)
214
215    -- Cursor hide
216    eq(escape_ansi('\027[?25l'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor hide')
217    esc_codes = string.sub(esc_codes, 7)
218
219    -- Cursor move to new position
220    eq(escape_ansi('\027[6;5H'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor movement')
221    esc_codes = string.sub(esc_codes, 7)
222
223    -- Place command reuses same placement ID (flicker-free update)
224    local seq = parse_kitty_seq(esc_codes, { strict = true })
225    eq({
226      a = 'p',
227      i = seq.control.i,
228      p = seq.control.p,
229      C = '1',
230      q = '2',
231      c = '7',
232      r = '8',
233      z = '9',
234    }, seq.control, 'update image control data')
235    esc_codes = string.sub(esc_codes, seq.j + 1)
236
237    -- Cursor restore
238    eq(escape_ansi('\0278'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor restore')
239    esc_codes = string.sub(esc_codes, 3)
240
241    -- Cursor show
242    eq(escape_ansi('\027[?25h'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor show')
243  end)
244
245  it('can delete all images', function()
246    local result = exec_lua(function()
247      local id1 = vim.ui.img.set(PNG_IMG_BYTES, { row = 1, col = 1 })
248      local id2 = vim.ui.img.set(PNG_IMG_BYTES, { row = 2, col = 2 })
249
250      _G.data = {}
251      local deleted = vim.ui.img.del(math.huge)
252      return {
253        esc_codes = table.concat(_G.data),
254        deleted = deleted,
255        after_id1 = vim.ui.img.get(id1),
256        after_id2 = vim.ui.img.get(id2),
257        not_deleted = vim.ui.img.del(math.huge), -- nothing to delete
258      }
259    end)
260
261    local seq = parse_kitty_seq(result.esc_codes, { strict = true })
262    eq({ a = 'd', d = 'A', q = '2' }, seq.control, 'delete all control data')
263
264    eq(true, result.deleted)
265    eq(nil, result.after_id1)
266    eq(nil, result.after_id2)
267    eq(false, result.not_deleted)
268  end)
269
270  it('can delete an image', function()
271    local result = exec_lua(function()
272      local id = vim.ui.img.set(PNG_IMG_BYTES, { row = 1, col = 1 })
273
274      _G.data = {}
275      local found = vim.ui.img.del(id)
276      local after = vim.ui.img.get(id)
277      local not_found = vim.ui.img.del(id)
278
279      return {
280        esc_codes = table.concat(_G.data),
281        found = found,
282        after = after,
283        not_found = not_found,
284      }
285    end)
286
287    local seq = parse_kitty_seq(result.esc_codes, { strict = true })
288    eq({
289      a = 'd',
290      d = 'i',
291      i = seq.control.i,
292      q = '2',
293    }, seq.control, 'delete image')
294
295    eq(true, result.found)
296    eq(nil, result.after)
297    eq(false, result.not_found)
298  end)
299end)