aboutsummaryrefslogtreecommitdiff
path: root/test/functional/ui/img_spec.lua
blob: cbfe4d415f60b27b1cf563690cf77e679bf815e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local describe, it, before_each = t.describe, t.it, t.before_each
local eq = t.eq

local clear = n.clear
local exec_lua = n.exec_lua

---4x4 PNG image bytes.
---@type string
-- stylua: ignore
local PNG_IMG_BYTES = string.char(unpack({
  137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 4, 0,
  0, 0, 4, 8, 6, 0, 0, 0, 169, 241, 158, 126, 0, 0, 0, 1, 115, 82, 71, 66, 0,
  174, 206, 28, 233, 0, 0, 0, 39, 73, 68, 65, 84, 8, 153, 99, 252, 207, 192,
  240, 159, 129, 129, 129, 193, 226, 63, 3, 3, 3, 3, 3, 3, 19, 3, 26, 96, 97,
  156, 1, 145, 250, 207, 184, 12, 187, 10, 0, 36, 189, 6, 125, 75, 9, 40, 46,
  0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
}))

---@param s string
---@return string
local function escape_ansi(s)
  return (
    string.gsub(s, '.', function(c)
      local byte = string.byte(c)
      if byte < 32 or byte == 127 then
        return string.format('\\%03d', byte)
      else
        return c
      end
    end)
  )
end

---@param s string
---@return string
local function base64_encode(s)
  return exec_lua(function()
    return vim.base64.encode(s)
  end)
end

---Mock nvim_ui_send to capture escape sequence output.
local function setup_img_api()
  exec_lua(function()
    _G.data = {}
    local original_ui_send = vim.api.nvim_ui_send
    vim.api.nvim_ui_send = function(d)
      table.insert(_G.data, d)
    end
    _G._original_ui_send = original_ui_send
  end)
end

---@param esc string
---@param opts? {strict?:boolean}
---@return {i:integer, j:integer, control:table<string, string>, data:string|nil}
local function parse_kitty_seq(esc, opts)
  opts = opts or {}
  local i, j, c, d = string.find(esc, '\027_G([^;\027]+)([^\027]*)\027\\')
  assert(c, 'invalid kitty escape sequence: ' .. escape_ansi(esc))

  if opts.strict then
    assert(i == 1, 'not starting with kitty graphics sequence: ' .. escape_ansi(esc))
  end

  ---@type table<string, string>
  local control = {}
  local idx = 0
  while true do
    local k, v, _
    idx, _, k, v = string.find(c, '(%a+)=([^,]+),?', idx + 1)
    if idx == nil then
      break
    end
    if k and v then
      control[k] = v
    end
  end

  ---@type string|nil
  local payload
  if d and d ~= '' then
    payload = string.sub(d, 2)
  end

  return { i = i, j = j, control = control, data = payload }
end

describe('vim.ui.img', function()
  before_each(function()
    clear()
    setup_img_api()
  end)

  it('can set an image', function()
    local esc_codes = exec_lua(function()
      _G.data = {}
      vim.ui.img.set(PNG_IMG_BYTES, {
        col = 1,
        row = 2,
        width = 3,
        height = 4,
        zindex = 123,
      })
      return table.concat(_G.data)
    end)

    -- Transmit image bytes
    local seq = parse_kitty_seq(esc_codes, { strict = true })
    local image_id = seq.control.i
    eq({
      f = '100',
      a = 't',
      t = 'd',
      i = image_id,
      q = '2',
      m = '0',
    }, seq.control, 'transmit image control data')
    eq(base64_encode(PNG_IMG_BYTES), seq.data)
    esc_codes = string.sub(esc_codes, seq.j + 1)

    -- Cursor save
    eq(escape_ansi('\0277'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor save')
    esc_codes = string.sub(esc_codes, 3)

    -- Cursor hide
    eq(escape_ansi('\027[?25l'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor hide')
    esc_codes = string.sub(esc_codes, 7)

    -- Cursor move
    eq(escape_ansi('\027[2;1H'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor movement')
    esc_codes = string.sub(esc_codes, 7)

    -- Place image
    seq = parse_kitty_seq(esc_codes, { strict = true })
    eq({
      a = 'p',
      i = image_id,
      p = seq.control.p,
      C = '1',
      q = '2',
      c = '3',
      r = '4',
      z = '123',
    }, seq.control, 'display image control data')
    esc_codes = string.sub(esc_codes, seq.j + 1)

    -- Cursor restore
    eq(escape_ansi('\0278'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor restore')
    esc_codes = string.sub(esc_codes, 3)

    -- Cursor show
    eq(escape_ansi('\027[?25h'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor show')
  end)

  it('can get image info', function()
    local result = exec_lua(function()
      local id = vim.ui.img.set(PNG_IMG_BYTES, {
        row = 5,
        col = 10,
        width = 20,
        height = 15,
        zindex = 42,
      })

      return {
        info = vim.ui.img.get(id),
        missing = vim.ui.img.get(999999),
      }
    end)

    eq({ row = 5, col = 10, width = 20, height = 15, zindex = 42 }, result.info)
    eq(nil, result.missing)
  end)

  it('can update an image', function()
    local result = exec_lua(function()
      local id = vim.ui.img.set(PNG_IMG_BYTES, {
        row = 1,
        col = 1,
        width = 10,
        height = 20,
        zindex = 99,
      })

      _G.data = {}
      vim.ui.img.set(id, {
        col = 5,
        row = 6,
        width = 7,
        height = 8,
        zindex = 9,
      })
      local esc_codes = table.concat(_G.data)

      -- Partial update: only change row, other fields preserved
      vim.ui.img.set(id, { row = 50 })
      local info = vim.ui.img.get(id)

      return { esc_codes = esc_codes, info = info }
    end)

    -- Verify partial update merged opts
    eq({ row = 50, col = 5, width = 7, height = 8, zindex = 9 }, result.info)

    local esc_codes = result.esc_codes

    -- Cursor save
    eq(escape_ansi('\0277'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor save')
    esc_codes = string.sub(esc_codes, 3)

    -- Cursor hide
    eq(escape_ansi('\027[?25l'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor hide')
    esc_codes = string.sub(esc_codes, 7)

    -- Cursor move to new position
    eq(escape_ansi('\027[6;5H'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor movement')
    esc_codes = string.sub(esc_codes, 7)

    -- Place command reuses same placement ID (flicker-free update)
    local seq = parse_kitty_seq(esc_codes, { strict = true })
    eq({
      a = 'p',
      i = seq.control.i,
      p = seq.control.p,
      C = '1',
      q = '2',
      c = '7',
      r = '8',
      z = '9',
    }, seq.control, 'update image control data')
    esc_codes = string.sub(esc_codes, seq.j + 1)

    -- Cursor restore
    eq(escape_ansi('\0278'), escape_ansi(string.sub(esc_codes, 1, 2)), 'cursor restore')
    esc_codes = string.sub(esc_codes, 3)

    -- Cursor show
    eq(escape_ansi('\027[?25h'), escape_ansi(string.sub(esc_codes, 1, 6)), 'cursor show')
  end)

  it('can delete all images', function()
    local result = exec_lua(function()
      local id1 = vim.ui.img.set(PNG_IMG_BYTES, { row = 1, col = 1 })
      local id2 = vim.ui.img.set(PNG_IMG_BYTES, { row = 2, col = 2 })

      _G.data = {}
      local deleted = vim.ui.img.del(math.huge)
      return {
        esc_codes = table.concat(_G.data),
        deleted = deleted,
        after_id1 = vim.ui.img.get(id1),
        after_id2 = vim.ui.img.get(id2),
        not_deleted = vim.ui.img.del(math.huge), -- nothing to delete
      }
    end)

    local seq = parse_kitty_seq(result.esc_codes, { strict = true })
    eq({ a = 'd', d = 'A', q = '2' }, seq.control, 'delete all control data')

    eq(true, result.deleted)
    eq(nil, result.after_id1)
    eq(nil, result.after_id2)
    eq(false, result.not_deleted)
  end)

  it('can delete an image', function()
    local result = exec_lua(function()
      local id = vim.ui.img.set(PNG_IMG_BYTES, { row = 1, col = 1 })

      _G.data = {}
      local found = vim.ui.img.del(id)
      local after = vim.ui.img.get(id)
      local not_found = vim.ui.img.del(id)

      return {
        esc_codes = table.concat(_G.data),
        found = found,
        after = after,
        not_found = not_found,
      }
    end)

    local seq = parse_kitty_seq(result.esc_codes, { strict = true })
    eq({
      a = 'd',
      d = 'i',
      i = seq.control.i,
      q = '2',
    }, seq.control, 'delete image')

    eq(true, result.found)
    eq(nil, result.after)
    eq(false, result.not_found)
  end)
end)