From a2a3e828533656ccff0acc7c8f9d9e180d36d4dc Mon Sep 17 00:00:00 2001 From: jxyyz <132339113+jxyyz@users.noreply.github.com> Date: Thu, 26 Feb 2026 04:53:24 +0100 Subject: [PATCH] refactor: make send_keys a generic key sender, drop hardcoded Enter - Accept string|string[] instead of single string - Callers now explicitly pass "Enter" where needed - Replace mock with real implementation in tests --- lua/wiremux/backend/tmux/action.lua | 10 +++++-- lua/wiremux/backend/tmux/operation.lua | 4 +-- tests/helpers_operation.lua | 4 +-- tests/operation_spec.lua | 38 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lua/wiremux/backend/tmux/action.lua b/lua/wiremux/backend/tmux/action.lua index 4829a81..92e0276 100644 --- a/lua/wiremux/backend/tmux/action.lua +++ b/lua/wiremux/backend/tmux/action.lua @@ -50,10 +50,16 @@ function M.select_window(window_id) end ---@param target_id string ----@param keys string +---@param keys string|string[] e.g. "Enter", { cmd, "Enter" }, { "Escape", "i" }, { "C-c" } ---@return string[] function M.send_keys(target_id, keys) - return { "send-keys", "-t", target_id, keys, "Enter" } + local cmd = { "send-keys", "-t", target_id } + if type(keys) == "table" then + vim.list_extend(cmd, keys) + else + table.insert(cmd, keys) + end + return cmd end ---@param buffer_name string diff --git a/lua/wiremux/backend/tmux/operation.lua b/lua/wiremux/backend/tmux/operation.lua index 13bce37..7b8a6e8 100644 --- a/lua/wiremux/backend/tmux/operation.lua +++ b/lua/wiremux/backend/tmux/operation.lua @@ -18,7 +18,7 @@ local function submit(targets) vim.defer_fn(function() local batch = {} for _, t in ipairs(targets) do - table.insert(batch, action.send_keys(t.id, "")) + table.insert(batch, action.send_keys(t.id, "Enter")) end client.execute(batch) end, 300) @@ -167,7 +167,7 @@ function M.create(target_name, def, st) state.set_instance_metadata(id, target_name, st.origin_pane_id or "", vim.fn.getcwd(), kind) if use_shell and cmd then - client.execute({ action.send_keys(id, cmd) }) + client.execute({ action.send_keys(id, { cmd, "Enter" }) }) end notify.debug("create: %s %s target=%s", kind, id, target_name) diff --git a/tests/helpers_operation.lua b/tests/helpers_operation.lua index 85ae652..282aec1 100644 --- a/tests/helpers_operation.lua +++ b/tests/helpers_operation.lua @@ -34,9 +34,7 @@ function M.setup() set_pane_option = function(pane_id, key, value) return { "set-option", "-p", "-t", pane_id, key, value } end, - send_keys = function(target, keys) - return { "send-keys", "-t", target, keys, "Enter" } - end, + send_keys = require("wiremux.backend.tmux.action").send_keys, new_window = function(name, command) local cmd = { "new-window" } if name then diff --git a/tests/operation_spec.lua b/tests/operation_spec.lua index fa8fb05..3045da1 100644 --- a/tests/operation_spec.lua +++ b/tests/operation_spec.lua @@ -2,6 +2,22 @@ local helpers = require("tests.helpers_operation") +describe("action.send_keys", function() + local action = require("wiremux.backend.tmux.action") + + it("accepts single string key", function() + assert.are.same({ "send-keys", "-t", "%1", "Enter" }, action.send_keys("%1", "Enter")) + end) + + it("accepts array of keys", function() + assert.are.same({ "send-keys", "-t", "%1", "i", "Enter" }, action.send_keys("%1", { "i", "Enter" })) + end) + + it("accepts modifier keys", function() + assert.are.same({ "send-keys", "-t", "%1", "C-c" }, action.send_keys("%1", "C-c")) + end) +end) + describe("tmux operations", function() local mocks @@ -224,6 +240,28 @@ describe("tmux operations", function() assert.are.equal("myapp", new_window_cmd[3]) end) + it("sends cmd with Enter for shell targets", function() + local shell_cmds + local call_count = 0 + + mocks.client.execute = function(cmds) + call_count = call_count + 1 + if call_count == 1 then + return "%5" + else + shell_cmds = cmds + return "ok" + end + end + + local st = { instances = {}, origin_pane_id = "%0" } + local def = { kind = "pane", shell = true, cmd = "npm start" } + + mocks.operation.create("myapp", def, st) + + assert.are.same({ { "send-keys", "-t", "%5", "npm start", "Enter" } }, shell_cmds) + end) + it("ignores function label for window name", function() local captured_cmds mocks.client.execute = function(cmds)