|
| 1 | +-- Tests for the :ClaudeCodeSendText command (#197). |
| 2 | +require("tests.busted_setup") |
| 3 | +require("tests.mocks.vim") |
| 4 | + |
| 5 | +describe("ClaudeCodeSendText command", function() |
| 6 | + local claudecode |
| 7 | + local mock_logger |
| 8 | + local mock_terminal |
| 9 | + local saved_require = _G.require |
| 10 | + |
| 11 | + local function setup_mocks() |
| 12 | + mock_logger = { |
| 13 | + setup = function() end, |
| 14 | + debug = spy.new(function() end), |
| 15 | + error = spy.new(function() end), |
| 16 | + warn = spy.new(function() end), |
| 17 | + info = spy.new(function() end), |
| 18 | + } |
| 19 | + |
| 20 | + mock_terminal = { |
| 21 | + setup = function() end, |
| 22 | + open = spy.new(function() end), |
| 23 | + close = spy.new(function() end), |
| 24 | + simple_toggle = spy.new(function() end), |
| 25 | + focus_toggle = spy.new(function() end), |
| 26 | + ensure_visible = spy.new(function() end), |
| 27 | + get_active_terminal_bufnr = function() |
| 28 | + return 1 |
| 29 | + end, |
| 30 | + send_to_terminal = spy.new(function() |
| 31 | + return true |
| 32 | + end), |
| 33 | + } |
| 34 | + |
| 35 | + vim.fn.getcwd = function() |
| 36 | + return "/current/dir" |
| 37 | + end |
| 38 | + vim.api.nvim_create_user_command = spy.new(function() end) |
| 39 | + vim.notify = spy.new(function() end) |
| 40 | + |
| 41 | + _G.require = function(mod) |
| 42 | + if mod == "claudecode.logger" then |
| 43 | + return mock_logger |
| 44 | + elseif mod == "claudecode.config" then |
| 45 | + return { |
| 46 | + apply = function(opts) |
| 47 | + return opts or {} |
| 48 | + end, |
| 49 | + } |
| 50 | + elseif mod == "claudecode.diff" then |
| 51 | + return { setup = function() end } |
| 52 | + elseif mod == "claudecode.terminal" then |
| 53 | + return mock_terminal |
| 54 | + elseif mod == "claudecode.visual_commands" then |
| 55 | + return { |
| 56 | + create_visual_command_wrapper = function(normal_handler) |
| 57 | + return normal_handler |
| 58 | + end, |
| 59 | + } |
| 60 | + else |
| 61 | + return saved_require(mod) |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + before_each(function() |
| 67 | + setup_mocks() |
| 68 | + |
| 69 | + package.loaded["claudecode"] = nil |
| 70 | + package.loaded["claudecode.config"] = nil |
| 71 | + package.loaded["claudecode.logger"] = nil |
| 72 | + package.loaded["claudecode.diff"] = nil |
| 73 | + package.loaded["claudecode.visual_commands"] = nil |
| 74 | + package.loaded["claudecode.terminal"] = nil |
| 75 | + |
| 76 | + claudecode = require("claudecode") |
| 77 | + claudecode.state.server = { |
| 78 | + broadcast = spy.new(function() |
| 79 | + return true |
| 80 | + end), |
| 81 | + } |
| 82 | + claudecode.state.port = 12345 |
| 83 | + end) |
| 84 | + |
| 85 | + after_each(function() |
| 86 | + _G.require = saved_require |
| 87 | + package.loaded["claudecode"] = nil |
| 88 | + end) |
| 89 | + |
| 90 | + local function find_command(name) |
| 91 | + for _, call in ipairs(vim.api.nvim_create_user_command.calls) do |
| 92 | + if call.vals[1] == name then |
| 93 | + return call.vals[2], call.vals[3] |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + it("registers ClaudeCodeSendText with nargs=+ and bang support", function() |
| 99 | + claudecode.setup({ auto_start = false }) |
| 100 | + |
| 101 | + local handler, config = find_command("ClaudeCodeSendText") |
| 102 | + assert.is_function(handler) |
| 103 | + assert.is_equal("+", config.nargs) |
| 104 | + assert.is_true(config.bang) |
| 105 | + assert.is_string(config.desc) |
| 106 | + end) |
| 107 | + |
| 108 | + it("sends text and submits by default", function() |
| 109 | + claudecode.setup({ auto_start = false }) |
| 110 | + local handler = find_command("ClaudeCodeSendText") |
| 111 | + |
| 112 | + handler({ args = "run the tests", bang = false }) |
| 113 | + |
| 114 | + assert.spy(mock_terminal.send_to_terminal).was_called() |
| 115 | + local call = mock_terminal.send_to_terminal.calls[1] |
| 116 | + assert.is_equal("run the tests", call.vals[1]) |
| 117 | + assert.is_true(call.vals[2].submit) |
| 118 | + end) |
| 119 | + |
| 120 | + it("inserts without submitting when bang is used", function() |
| 121 | + claudecode.setup({ auto_start = false }) |
| 122 | + local handler = find_command("ClaudeCodeSendText") |
| 123 | + |
| 124 | + handler({ args = "draft text", bang = true }) |
| 125 | + |
| 126 | + local call = mock_terminal.send_to_terminal.calls[1] |
| 127 | + assert.is_equal("draft text", call.vals[1]) |
| 128 | + assert.is_false(call.vals[2].submit) |
| 129 | + end) |
| 130 | + |
| 131 | + it("warns and does not send when no text is provided", function() |
| 132 | + claudecode.setup({ auto_start = false }) |
| 133 | + local handler = find_command("ClaudeCodeSendText") |
| 134 | + |
| 135 | + handler({ args = "", bang = false }) |
| 136 | + |
| 137 | + assert.spy(mock_logger.warn).was_called() |
| 138 | + assert.spy(mock_terminal.send_to_terminal).was_not_called() |
| 139 | + end) |
| 140 | +end) |
0 commit comments