|
| 1 | +local MiniTest = require("mini.test") |
| 2 | +local eq = MiniTest.expect.equality |
| 3 | +local child = MiniTest.new_child_neovim() |
| 4 | + |
| 5 | +local T = MiniTest.new_set({ |
| 6 | + hooks = { |
| 7 | + pre_case = function() |
| 8 | + child.restart({ "-u", "scripts/minimal_init.lua" }) |
| 9 | + child.lua([[ |
| 10 | + -- Setup commands |
| 11 | + require('eca.commands').setup() |
| 12 | + |
| 13 | + -- Instantiate state singleton |
| 14 | + _G.State = require('eca.state').new() |
| 15 | + |
| 16 | + -- Mock vim.ui.select for testing |
| 17 | + _G.selected_choice = nil |
| 18 | + _G.shown_items = nil |
| 19 | + _G.shown_prompt = nil |
| 20 | + _G.original_select = vim.ui.select |
| 21 | + |
| 22 | + _G.mock_select = function(choice) |
| 23 | + _G.selected_choice = choice |
| 24 | + vim.ui.select = function(items, opts, on_choice) |
| 25 | + _G.shown_items = items |
| 26 | + _G.shown_prompt = opts.prompt |
| 27 | + on_choice(choice) |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + _G.restore_select = function() |
| 32 | + vim.ui.select = _G.original_select |
| 33 | + end |
| 34 | + ]]) |
| 35 | + end, |
| 36 | + post_case = function() |
| 37 | + child.lua([[_G.restore_select()]]) |
| 38 | + end, |
| 39 | + post_once = child.stop, |
| 40 | + }, |
| 41 | +}) |
| 42 | + |
| 43 | +-- Test EcaChatSelectModel command |
| 44 | +T["EcaChatSelectModel"] = MiniTest.new_set() |
| 45 | + |
| 46 | +T["EcaChatSelectModel"]["command is registered"] = function() |
| 47 | + local commands = child.lua_get("vim.api.nvim_get_commands({})") |
| 48 | + eq(type(commands.EcaChatSelectModel), "table") |
| 49 | + eq(commands.EcaChatSelectModel.name, "EcaChatSelectModel") |
| 50 | +end |
| 51 | + |
| 52 | +T["EcaChatSelectModel"]["updates state when model selected"] = function() |
| 53 | + -- Setup initial state with models |
| 54 | + child.lua([[ |
| 55 | + _G.State.config.models.list = { "model1", "model2", "model3" } |
| 56 | + _G.State.config.models.selected = "model1" |
| 57 | + |
| 58 | + -- Mock vim.ui.select to auto-select model2 |
| 59 | + _G.mock_select("model2") |
| 60 | + ]]) |
| 61 | + |
| 62 | + -- Execute command |
| 63 | + child.cmd("EcaChatSelectModel") |
| 64 | + |
| 65 | + -- Check that state was updated |
| 66 | + eq(child.lua_get("_G.State.config.models.selected"), "model2") |
| 67 | +end |
| 68 | + |
| 69 | +T["EcaChatSelectModel"]["handles nil selection"] = function() |
| 70 | + -- Setup initial state |
| 71 | + child.lua([[ |
| 72 | + _G.State.config.models.list = { "model1", "model2" } |
| 73 | + _G.State.config.models.selected = "model1" |
| 74 | + |
| 75 | + -- Mock vim.ui.select to return nil (user cancelled) |
| 76 | + _G.mock_select(nil) |
| 77 | + ]]) |
| 78 | + |
| 79 | + -- Execute command |
| 80 | + child.cmd("EcaChatSelectModel") |
| 81 | + |
| 82 | + -- Check that state was NOT updated (still model1) |
| 83 | + eq(child.lua_get("_G.State.config.models.selected"), "model1") |
| 84 | +end |
| 85 | + |
| 86 | +T["EcaChatSelectModel"]["displays all available models"] = function() |
| 87 | + -- Setup models list |
| 88 | + child.lua([[ |
| 89 | + _G.State.config.models.list = { "gpt-4", "gpt-3.5-turbo", "claude-3" } |
| 90 | + |
| 91 | + -- Mock vim.ui.select to capture the items shown |
| 92 | + _G.mock_select(nil) |
| 93 | + ]]) |
| 94 | + |
| 95 | + -- Execute command |
| 96 | + child.cmd("EcaChatSelectModel") |
| 97 | + |
| 98 | + -- Verify all models were shown |
| 99 | + local shown_items = child.lua_get("_G.shown_items") |
| 100 | + eq(shown_items[1], "gpt-4") |
| 101 | + eq(shown_items[2], "gpt-3.5-turbo") |
| 102 | + eq(shown_items[3], "claude-3") |
| 103 | +end |
| 104 | + |
| 105 | +-- Test EcaChatSelectBehavior command |
| 106 | +T["EcaChatSelectBehavior"] = MiniTest.new_set() |
| 107 | + |
| 108 | +T["EcaChatSelectBehavior"]["command is registered"] = function() |
| 109 | + local commands = child.lua_get("vim.api.nvim_get_commands({})") |
| 110 | + eq(type(commands.EcaChatSelectBehavior), "table") |
| 111 | + eq(commands.EcaChatSelectBehavior.name, "EcaChatSelectBehavior") |
| 112 | +end |
| 113 | + |
| 114 | +T["EcaChatSelectBehavior"]["updates state when behavior selected"] = function() |
| 115 | + -- Setup initial state with behaviors |
| 116 | + child.lua([[ |
| 117 | + _G.State.config.behaviors.list = { "helpful", "creative", "concise" } |
| 118 | + _G.State.config.behaviors.selected = "helpful" |
| 119 | + |
| 120 | + -- Mock vim.ui.select to auto-select creative |
| 121 | + _G.mock_select("creative") |
| 122 | + ]]) |
| 123 | + |
| 124 | + -- Execute command |
| 125 | + child.cmd("EcaChatSelectBehavior") |
| 126 | + |
| 127 | + -- Check that state was updated |
| 128 | + eq(child.lua_get("_G.State.config.behaviors.selected"), "creative") |
| 129 | +end |
| 130 | + |
| 131 | +T["EcaChatSelectBehavior"]["handles nil selection"] = function() |
| 132 | + -- Setup initial state |
| 133 | + child.lua([[ |
| 134 | + _G.State.config.behaviors.list = { "helpful", "creative" } |
| 135 | + _G.State.config.behaviors.selected = "helpful" |
| 136 | + |
| 137 | + -- Mock vim.ui.select to return nil (user cancelled) |
| 138 | + _G.mock_select(nil) |
| 139 | + ]]) |
| 140 | + |
| 141 | + -- Execute command |
| 142 | + child.cmd("EcaChatSelectBehavior") |
| 143 | + |
| 144 | + -- Check that state was NOT updated (still helpful) |
| 145 | + eq(child.lua_get("_G.State.config.behaviors.selected"), "helpful") |
| 146 | +end |
| 147 | + |
| 148 | +T["EcaChatSelectBehavior"]["displays all available behaviors"] = function() |
| 149 | + -- Setup behaviors list |
| 150 | + child.lua([[ |
| 151 | + _G.State.config.behaviors.list = { "helpful", "creative", "concise", "technical" } |
| 152 | + |
| 153 | + -- Mock vim.ui.select to capture the items shown |
| 154 | + _G.mock_select(nil) |
| 155 | + ]]) |
| 156 | + |
| 157 | + -- Execute command |
| 158 | + child.cmd("EcaChatSelectBehavior") |
| 159 | + |
| 160 | + -- Verify all behaviors were shown |
| 161 | + local shown_items = child.lua_get("_G.shown_items") |
| 162 | + eq(shown_items[1], "helpful") |
| 163 | + eq(shown_items[2], "creative") |
| 164 | + eq(shown_items[3], "concise") |
| 165 | + eq(shown_items[4], "technical") |
| 166 | +end |
| 167 | + |
| 168 | +-- Test State update methods |
| 169 | +T["State"] = MiniTest.new_set() |
| 170 | + |
| 171 | +T["State"]["update_selected_model updates config"] = function() |
| 172 | + child.lua([[ |
| 173 | + _G.State.config.models.list = { "model1", "model2" } |
| 174 | + _G.State.config.models.selected = "model1" |
| 175 | + |
| 176 | + _G.State:update_selected_model("model2") |
| 177 | + ]]) |
| 178 | + |
| 179 | + eq(child.lua_get("_G.State.config.models.selected"), "model2") |
| 180 | +end |
| 181 | + |
| 182 | +T["State"]["update_selected_model handles nil"] = function() |
| 183 | + child.lua([[ |
| 184 | + _G.State.config.models.selected = "model1" |
| 185 | + |
| 186 | + -- Should not update if nil is passed |
| 187 | + _G.State:update_selected_model(nil) |
| 188 | + ]]) |
| 189 | + |
| 190 | + eq(child.lua_get("_G.State.config.models.selected"), "model1") |
| 191 | +end |
| 192 | + |
| 193 | +T["State"]["update_selected_model handles non-string"] = function() |
| 194 | + child.lua([[ |
| 195 | + _G.State.config.models.selected = "model1" |
| 196 | + |
| 197 | + -- Should not update if non-string is passed |
| 198 | + _G.State:update_selected_model(123) |
| 199 | + ]]) |
| 200 | + |
| 201 | + eq(child.lua_get("_G.State.config.models.selected"), "model1") |
| 202 | +end |
| 203 | + |
| 204 | +T["State"]["update_selected_behavior updates config"] = function() |
| 205 | + child.lua([[ |
| 206 | + _G.State.config.behaviors.list = { "helpful", "creative" } |
| 207 | + _G.State.config.behaviors.selected = "helpful" |
| 208 | + |
| 209 | + _G.State:update_selected_behavior("creative") |
| 210 | + ]]) |
| 211 | + |
| 212 | + eq(child.lua_get("_G.State.config.behaviors.selected"), "creative") |
| 213 | +end |
| 214 | + |
| 215 | +T["State"]["update_selected_behavior handles nil"] = function() |
| 216 | + child.lua([[ |
| 217 | + _G.State.config.behaviors.selected = "helpful" |
| 218 | + |
| 219 | + -- Should not update if nil is passed |
| 220 | + _G.State:update_selected_behavior(nil) |
| 221 | + ]]) |
| 222 | + |
| 223 | + eq(child.lua_get("_G.State.config.behaviors.selected"), "helpful") |
| 224 | +end |
| 225 | + |
| 226 | +T["State"]["update_selected_behavior handles non-string"] = function() |
| 227 | + child.lua([[ |
| 228 | + _G.State.config.behaviors.selected = "helpful" |
| 229 | + |
| 230 | + -- Should not update if non-string is passed |
| 231 | + _G.State:update_selected_behavior(123) |
| 232 | + ]]) |
| 233 | + |
| 234 | + eq(child.lua_get("_G.State.config.behaviors.selected"), "helpful") |
| 235 | +end |
| 236 | + |
| 237 | +return T |
0 commit comments