|
| 1 | +local MiniTest = require("mini.test") |
| 2 | +local eq = MiniTest.expect.equality |
| 3 | +local child = MiniTest.new_child_neovim() |
| 4 | + |
| 5 | +local function setup_test_environment() |
| 6 | + -- Setup commands |
| 7 | + require('eca.commands').setup() |
| 8 | + |
| 9 | + -- Initialize everything |
| 10 | + _G.Server = require('eca.server').new() |
| 11 | + _G.State = require('eca.state').new() |
| 12 | + _G.Mediator = require('eca.mediator').new(_G.Server, _G.State) |
| 13 | + _G.Sidebar = require('eca.sidebar').new(1, _G.Mediator) |
| 14 | + _G.Eca = require('eca') |
| 15 | + _G.Eca.current = { sidebar = _G.Sidebar } |
| 16 | + |
| 17 | + -- Mock vim.ui.select for testing |
| 18 | + _G.selected_choice = nil |
| 19 | + _G.shown_items = nil |
| 20 | + _G.shown_prompt = nil |
| 21 | + _G.original_select = vim.ui.select |
| 22 | + |
| 23 | + _G.mock_select = function(choice) |
| 24 | + _G.selected_choice = choice |
| 25 | + vim.ui.select = function(items, opts, on_choice) |
| 26 | + _G.shown_items = items |
| 27 | + _G.shown_prompt = opts.prompt |
| 28 | + on_choice(choice) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + _G.restore_select = function() |
| 33 | + vim.ui.select = _G.original_select |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +local T = MiniTest.new_set({ |
| 38 | + hooks = { |
| 39 | + pre_case = function() |
| 40 | + child.restart({ "-u", "scripts/minimal_init.lua" }) |
| 41 | + child.lua_func(setup_test_environment) |
| 42 | + end, |
| 43 | + post_case = function() |
| 44 | + child.lua([[_G.restore_select()]]) |
| 45 | + end, |
| 46 | + post_once = child.stop, |
| 47 | + }, |
| 48 | +}) |
| 49 | + |
| 50 | +-- Test EcaChatSelectModel command |
| 51 | +T["EcaChatSelectModel"] = MiniTest.new_set() |
| 52 | + |
| 53 | +T["EcaChatSelectModel"]["command is registered"] = function() |
| 54 | + local commands = child.lua_get("vim.api.nvim_get_commands({})") |
| 55 | + eq(type(commands.EcaChatSelectModel), "table") |
| 56 | + eq(commands.EcaChatSelectModel.name, "EcaChatSelectModel") |
| 57 | +end |
| 58 | + |
| 59 | +T["EcaChatSelectModel"]["updates state when model selected"] = function() |
| 60 | + -- Setup initial state with models |
| 61 | + child.lua([[ |
| 62 | + _G.State.config.models.list = { "model1", "model2", "model3" } |
| 63 | + _G.State.config.models.selected = "model1" |
| 64 | +
|
| 65 | + -- Mock vim.ui.select to auto-select model2 |
| 66 | + _G.mock_select("model2") |
| 67 | + ]]) |
| 68 | + |
| 69 | + -- Execute command |
| 70 | + child.cmd("EcaChatSelectModel") |
| 71 | + |
| 72 | + -- Check that state was updated |
| 73 | + eq(child.lua_get("_G.State.config.models.selected"), "model2") |
| 74 | +end |
| 75 | + |
| 76 | +T["EcaChatSelectModel"]["handles nil selection"] = function() |
| 77 | + -- Setup initial state |
| 78 | + child.lua([[ |
| 79 | + _G.State.config.models.list = { "model1", "model2" } |
| 80 | + _G.State.config.models.selected = "model1" |
| 81 | +
|
| 82 | + -- Mock vim.ui.select to return nil (user cancelled) |
| 83 | + _G.mock_select(nil) |
| 84 | + ]]) |
| 85 | + |
| 86 | + -- Execute command |
| 87 | + child.cmd("EcaChatSelectModel") |
| 88 | + |
| 89 | + -- Check that state was NOT updated (still model1) |
| 90 | + eq(child.lua_get("_G.State.config.models.selected"), "model1") |
| 91 | +end |
| 92 | + |
| 93 | +T["EcaChatSelectModel"]["displays all available models"] = function() |
| 94 | + -- Setup models list |
| 95 | + child.lua([[ |
| 96 | + _G.State.config.models.list = { "gpt-4", "gpt-3.5-turbo", "claude-3" } |
| 97 | +
|
| 98 | + -- Mock vim.ui.select to capture the items shown |
| 99 | + _G.mock_select(nil) |
| 100 | + ]]) |
| 101 | + |
| 102 | + -- Execute command |
| 103 | + child.cmd("EcaChatSelectModel") |
| 104 | + |
| 105 | + -- Verify all models were shown |
| 106 | + local shown_items = child.lua_get("_G.shown_items") |
| 107 | + eq(shown_items[1], "gpt-4") |
| 108 | + eq(shown_items[2], "gpt-3.5-turbo") |
| 109 | + eq(shown_items[3], "claude-3") |
| 110 | +end |
| 111 | + |
| 112 | +-- Test EcaChatSelectBehavior command |
| 113 | +T["EcaChatSelectBehavior"] = MiniTest.new_set() |
| 114 | + |
| 115 | +T["EcaChatSelectBehavior"]["command is registered"] = function() |
| 116 | + local commands = child.lua_get("vim.api.nvim_get_commands({})") |
| 117 | + eq(type(commands.EcaChatSelectBehavior), "table") |
| 118 | + eq(commands.EcaChatSelectBehavior.name, "EcaChatSelectBehavior") |
| 119 | +end |
| 120 | + |
| 121 | +T["EcaChatSelectBehavior"]["updates state when behavior selected"] = function() |
| 122 | + -- Setup initial state with behaviors |
| 123 | + child.lua([[ |
| 124 | + _G.State.config.behaviors.list = { "helpful", "creative", "concise" } |
| 125 | + _G.State.config.behaviors.selected = "helpful" |
| 126 | +
|
| 127 | + -- Mock vim.ui.select to auto-select creative |
| 128 | + _G.mock_select("creative") |
| 129 | + ]]) |
| 130 | + |
| 131 | + -- Execute command |
| 132 | + child.cmd("EcaChatSelectBehavior") |
| 133 | + |
| 134 | + -- Check that state was updated |
| 135 | + eq(child.lua_get("_G.State.config.behaviors.selected"), "creative") |
| 136 | +end |
| 137 | + |
| 138 | +T["EcaChatSelectBehavior"]["handles nil selection"] = function() |
| 139 | + -- Setup initial state |
| 140 | + child.lua([[ |
| 141 | + _G.State.config.behaviors.list = { "helpful", "creative" } |
| 142 | + _G.State.config.behaviors.selected = "helpful" |
| 143 | +
|
| 144 | + -- Mock vim.ui.select to return nil (user cancelled) |
| 145 | + _G.mock_select(nil) |
| 146 | + ]]) |
| 147 | + |
| 148 | + -- Execute command |
| 149 | + child.cmd("EcaChatSelectBehavior") |
| 150 | + |
| 151 | + -- Check that state was NOT updated (still helpful) |
| 152 | + eq(child.lua_get("_G.State.config.behaviors.selected"), "helpful") |
| 153 | +end |
| 154 | + |
| 155 | +T["EcaChatSelectBehavior"]["displays all available behaviors"] = function() |
| 156 | + -- Setup behaviors list |
| 157 | + child.lua([[ |
| 158 | + _G.State.config.behaviors.list = { "helpful", "creative", "concise", "technical" } |
| 159 | +
|
| 160 | + -- Mock vim.ui.select to capture the items shown |
| 161 | + _G.mock_select(nil) |
| 162 | + ]]) |
| 163 | + |
| 164 | + -- Execute command |
| 165 | + child.cmd("EcaChatSelectBehavior") |
| 166 | + |
| 167 | + -- Verify all behaviors were shown |
| 168 | + local shown_items = child.lua_get("_G.shown_items") |
| 169 | + eq(shown_items[1], "helpful") |
| 170 | + eq(shown_items[2], "creative") |
| 171 | + eq(shown_items[3], "concise") |
| 172 | + eq(shown_items[4], "technical") |
| 173 | +end |
| 174 | + |
| 175 | +return T |
0 commit comments