|
| 1 | +local MiniTest = require("mini.test") |
| 2 | +local eq = MiniTest.expect.equality |
| 3 | +local child = MiniTest.new_child_neovim() |
| 4 | + |
| 5 | +local function flush(ms) |
| 6 | + vim.uv.sleep(ms or 50) |
| 7 | + child.api.nvim_eval("1") |
| 8 | +end |
| 9 | + |
| 10 | +local function setup_env() |
| 11 | + require('eca.commands').setup() |
| 12 | + |
| 13 | + -- Stub Picker.pick so commands can run without snacks.nvim |
| 14 | + local Picker = require('eca.ui.picker') |
| 15 | + _G.picker_calls = {} |
| 16 | + Picker.pick = function(config) |
| 17 | + table.insert(_G.picker_calls, config) |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +local T = MiniTest.new_set({ |
| 22 | + hooks = { |
| 23 | + pre_case = function() |
| 24 | + child.restart({ "-u", "scripts/minimal_init.lua" }) |
| 25 | + child.lua_func(setup_env) |
| 26 | + end, |
| 27 | + post_case = function() |
| 28 | + child.lua("_G.picker_calls = nil") |
| 29 | + end, |
| 30 | + post_once = child.stop, |
| 31 | + }, |
| 32 | +}) |
| 33 | + |
| 34 | +T["EcaServerMessages"] = MiniTest.new_set() |
| 35 | + |
| 36 | +T["EcaServerMessages"]["uses picker and filters invalid JSON messages"] = function() |
| 37 | + child.lua([[ |
| 38 | + local eca = require('eca') |
| 39 | + eca.server = eca.server or {} |
| 40 | + eca.server.messages = { |
| 41 | + { id = 1, direction = 'send', content = vim.json.encode({ jsonrpc = '2.0', method = 'test/method', id = 1 }) }, |
| 42 | + { id = 2, direction = 'recv', content = 'not-json' }, |
| 43 | + } |
| 44 | +
|
| 45 | + vim.cmd('EcaServerMessages') |
| 46 | + ]]) |
| 47 | + |
| 48 | + flush() |
| 49 | + |
| 50 | + child.lua([[ |
| 51 | + local calls = _G.picker_calls or {} |
| 52 | + local cfg = calls[1] |
| 53 | + _G.picker_info = { |
| 54 | + count = #calls, |
| 55 | + source = cfg and cfg.source or nil, |
| 56 | + } |
| 57 | + ]]) |
| 58 | + |
| 59 | + local picker_info = child.lua_get("_G.picker_info") |
| 60 | + |
| 61 | + eq(picker_info.count, 1) |
| 62 | + eq(picker_info.source, "eca messages") |
| 63 | + |
| 64 | + -- Run finder and inspect produced items |
| 65 | + child.lua([[ |
| 66 | + local cfg = _G.picker_calls[1] |
| 67 | + local items = cfg.finder({}, {}) |
| 68 | + _G.result_messages = { |
| 69 | + count = #items, |
| 70 | + first = items[1], |
| 71 | + } |
| 72 | + ]]) |
| 73 | + |
| 74 | + local result = child.lua_get("_G.result_messages") |
| 75 | + |
| 76 | + -- Only the valid JSON message should be included |
| 77 | + eq(result.count, 1) |
| 78 | + eq(type(result.first), "table") |
| 79 | + eq(result.first.idx, 1) |
| 80 | + eq(result.first.preview.ft, "lua") |
| 81 | + |
| 82 | + -- Preview text should contain the method name |
| 83 | + local has_method = child.lua_get("string.find(..., 'test/method', 1, true) ~= nil", { result.first.preview.text }) |
| 84 | + eq(has_method, true) |
| 85 | + |
| 86 | + -- Confirm callback yanks preview text and closes picker |
| 87 | + child.lua([[ |
| 88 | + local cfg = _G.picker_calls[1] |
| 89 | + local item = cfg.finder({}, {})[1] |
| 90 | + local picker = { closed = false } |
| 91 | + function picker:close() self.closed = true end |
| 92 | +
|
| 93 | + cfg.confirm(picker, item, nil) |
| 94 | +
|
| 95 | + _G.confirm_messages = { |
| 96 | + reg = vim.fn.getreg(''), |
| 97 | + closed = picker.closed, |
| 98 | + } |
| 99 | + ]]) |
| 100 | + |
| 101 | + local confirm_ok = child.lua_get("_G.confirm_messages") |
| 102 | + |
| 103 | + eq(confirm_ok.closed, true) |
| 104 | + eq(type(confirm_ok.reg), "string") |
| 105 | + local has_method_in_reg = child.lua_get("string.find(..., 'test/method', 1, true) ~= nil", { confirm_ok.reg }) |
| 106 | + eq(has_method_in_reg, true) |
| 107 | +end |
| 108 | + |
| 109 | +T["EcaServerTools"] = MiniTest.new_set() |
| 110 | + |
| 111 | +T["EcaServerTools"]["lists tools from state in sorted order"] = function() |
| 112 | + child.lua([[ |
| 113 | + local eca = require('eca') |
| 114 | + eca.state = eca.state or {} |
| 115 | + eca.state.tools = { |
| 116 | + zebra = { kind = 'z' }, |
| 117 | + alpha = { kind = 'a' }, |
| 118 | + middle = { kind = 'm' }, |
| 119 | + } |
| 120 | +
|
| 121 | + vim.cmd('EcaServerTools') |
| 122 | + ]]) |
| 123 | + |
| 124 | + flush() |
| 125 | + |
| 126 | + child.lua([[ |
| 127 | + local calls = _G.picker_calls or {} |
| 128 | + _G.picker_info = { |
| 129 | + count = #calls, |
| 130 | + } |
| 131 | + ]]) |
| 132 | + |
| 133 | + local picker_info = child.lua_get("_G.picker_info") |
| 134 | + |
| 135 | + eq(picker_info.count, 1) |
| 136 | + |
| 137 | + child.lua([[ |
| 138 | + local cfg = _G.picker_calls[1] |
| 139 | + local items = cfg.finder({}, {}) |
| 140 | + _G.result_tools = { |
| 141 | + count = #items, |
| 142 | + names = { items[1].text, items[2].text, items[3].text }, |
| 143 | + first = items[1], |
| 144 | + } |
| 145 | + ]]) |
| 146 | + |
| 147 | + local result = child.lua_get("_G.result_tools") |
| 148 | + |
| 149 | + eq(result.count, 3) |
| 150 | + -- Names must be sorted alphabetically |
| 151 | + eq(result.names[1], "alpha") |
| 152 | + eq(result.names[2], "middle") |
| 153 | + eq(result.names[3], "zebra") |
| 154 | + |
| 155 | + -- Preview contains vim.inspect output of the tool |
| 156 | + local has_kind = child.lua_get("string.find(..., 'kind%p a', 1) ~= nil", { result.first.preview.text }) |
| 157 | + eq(has_kind, true) |
| 158 | + |
| 159 | + -- Confirm yanks preview text and closes picker |
| 160 | + child.lua([[ |
| 161 | + local cfg = _G.picker_calls[1] |
| 162 | + local items = cfg.finder({}, {}) |
| 163 | + local picker = { closed = false } |
| 164 | + function picker:close() self.closed = true end |
| 165 | +
|
| 166 | + cfg.confirm(picker, items[2], nil) |
| 167 | +
|
| 168 | + _G.confirm_tools = { |
| 169 | + reg = vim.fn.getreg(''), |
| 170 | + closed = picker.closed, |
| 171 | + } |
| 172 | + ]]) |
| 173 | + |
| 174 | + local confirm_ok = child.lua_get("_G.confirm_tools") |
| 175 | + |
| 176 | + eq(confirm_ok.closed, true) |
| 177 | + eq(type(confirm_ok.reg), "string") |
| 178 | + local has_middle = child.lua_get("string.find(..., 'middle', 1, true) ~= nil", { confirm_ok.reg }) |
| 179 | + eq(has_middle, true) |
| 180 | +end |
| 181 | + |
| 182 | +return T |
0 commit comments