-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_with_tmux.lua
More file actions
194 lines (157 loc) · 5.83 KB
/
test_with_tmux.lua
File metadata and controls
194 lines (157 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env nvim
-- Real tmux integration test for send-to-agent.nvim
-- Usage: nvim --headless -l test_with_tmux.lua
-- Set up the test environment
local plugin_path = vim.fn.getcwd()
vim.opt.runtimepath:prepend(plugin_path)
-- Simple test setup
vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.writebackup = false
local function run_tmux_test()
print("=== Real Tmux Integration Test ===\n")
local send_to_agent = require("send-to-agent")
-- Setup the plugin (this registers commands)
send_to_agent.setup()
local tests_passed = 0
local tests_failed = 0
local function test(name, fn)
io.write("Testing: " .. name .. " ... ")
local success, err = pcall(fn)
if success then
print("✅ PASS")
tests_passed = tests_passed + 1
else
print("❌ FAIL")
print(" Error: " .. tostring(err))
tests_failed = tests_failed + 1
end
end
-- Test 1: Check if tmux is available
test("Tmux availability", function()
-- Test tmux by trying to detect agents (this uses is_tmux_available internally)
local agents = send_to_agent.detect_agent_panes()
if agents == nil then
error("tmux is not available - please install tmux to run this test")
end
assert(agents ~= nil, "tmux should be available")
end)
-- Test 2: Create test tmux session with mock agent
test("Create test tmux session", function()
-- Clean up any existing test session
os.execute("tmux kill-session -t send-to-agent-test 2>/dev/null")
-- Create new test session
local result = os.execute("tmux new-session -d -s send-to-agent-test")
assert(result == 0, "Should create tmux session")
-- Create a window that simulates claude agent
result = os.execute("tmux new-window -t send-to-agent-test -n claude-test")
assert(result == 0, "Should create test window")
-- Start a simple script that acts like claude (just echoes input)
result = os.execute("tmux send-keys -t send-to-agent-test:claude-test 'bash -c \"while read line; do echo \\\"RECEIVED: \\$line\\\"; done\"' Enter")
assert(result == 0, "Should start mock claude process")
-- Wait a moment for setup
vim.uv.sleep(500)
end)
-- Test 3: Detect AI agents
test("Agent detection in real tmux", function()
-- Use public API to detect agents
local agents = send_to_agent.detect_agent_panes()
assert(agents ~= nil, "Should get response from agent detection")
if #agents == 0 then
print(" Note: No AI agents currently detected (expected for test environment)")
else
print(" Found " .. #agents .. " AI agent(s)")
end
end)
-- Test 4: Test sending functionality
test("Send functionality", function()
-- Test the send_text function (this tests the full pipeline)
local success = send_to_agent.send_text("@test/file.lua")
if not success then
print(" Note: Send failed (expected if no AI agents available)")
print(" This tests the complete send pipeline")
else
print(" Send succeeded - found and sent to agent")
end
-- Test passes if we get here without crashing
assert(true, "Send functionality completed without errors")
end)
-- Test 5: Test full workflow with a real file
test("Full send_buffer workflow", function()
-- Create a test file
local test_file = vim.fn.tempname() .. ".lua"
vim.fn.writefile({
"-- Test file for send-to-agent.nvim",
"local function hello()",
" print('Hello from send-to-agent!')",
"end",
"",
"return hello"
}, test_file)
-- Open the file
vim.cmd("edit " .. test_file)
-- Configure to not auto-switch panes (for testing)
send_to_agent.setup({
tmux = {
auto_switch_pane = false
}
})
-- Try to send buffer
-- Note: This might fail if no actual AI agents are running, but the tmux part should work
local success = send_to_agent.send_buffer()
-- Clean up
vim.fn.delete(test_file)
if not success then
print(" Note: send_buffer failed (expected if no AI agents running)")
print(" This tests the detection logic, not actual agent communication")
end
-- The test passes if we got here without crashing
assert(true, "Workflow completed without errors")
end)
-- Test 6: Test selection workflow
test("Selection workflow", function()
local test_file = vim.fn.tempname() .. ".lua"
vim.fn.writefile({
"line 1",
"line 2",
"line 3",
"line 4",
"line 5"
}, test_file)
vim.cmd("edit " .. test_file)
-- Mock visual selection (lines 2-4)
vim.fn.setpos("'<", { 0, 2, 1, 0 })
vim.fn.setpos("'>", { 0, 4, 1, 0 })
-- Test the selection sending (which creates the reference internally)
local success = send_to_agent.send_selection()
if not success then
print(" Note: Selection send failed (expected if no AI agents)")
else
print(" Selection send succeeded")
end
-- Clean up
vim.fn.delete(test_file)
assert(true, "Selection workflow completed")
end)
-- Cleanup: Remove test tmux session
test("Cleanup test session", function()
local result = os.execute("tmux kill-session -t send-to-agent-test 2>/dev/null")
-- Don't assert on this - session might already be gone
assert(true, "Cleanup attempted")
end)
-- Summary
print(string.format("\n=== Test Results ==="))
print(string.format("✅ Passed: %d", tests_passed))
print(string.format("❌ Failed: %d", tests_failed))
print(string.format("📊 Total: %d", tests_passed + tests_failed))
if tests_failed == 0 then
print("\n🎉 All tests passed!")
print("✨ send-to-agent.nvim is working correctly with tmux!")
vim.cmd("qall!")
else
print("\n💥 Some tests failed!")
os.exit(1)
end
end
-- Run the tests
run_tmux_test()