summaryrefslogtreecommitdiff
path: root/internal/tmux/tmux_test.go
blob: a8d15740b380e2aaff17c8b12c83da4a7668197d (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package tmux

import (
	"errors"
	"os"
	"os/exec"
	"testing"
)

func TestInSession(t *testing.T) {
	t.Setenv("TMUX", "/tmp/tmux-123,123,0")
	if !InSession() {
		t.Fatal("expected InSession true when TMUX is set")
	}
	t.Setenv("TMUX", "")
	if InSession() {
		t.Fatal("expected InSession false when TMUX is empty")
	}
}

func TestHasBinary_UsesLookPath(t *testing.T) {
	old := lookPath
	t.Cleanup(func() { lookPath = old })
	lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
	if !HasBinary() {
		t.Fatal("expected HasBinary true when lookPath succeeds")
	}
	lookPath = func(file string) (string, error) { return "", errors.New("nope") }
	if HasBinary() {
		t.Fatal("expected HasBinary false when lookPath fails")
	}
}

// --- Phase 3: Shell Utility Tests ---

func TestShellJoin(t *testing.T) {
	tests := []struct {
		name     string
		argv     []string
		expected string
	}{
		{
			name:     "simple alphanumeric",
			argv:     []string{"ls", "-la"},
			expected: "ls -la",
		},
		{
			name:     "with spaces",
			argv:     []string{"echo", "hello world"},
			expected: "echo 'hello world'",
		},
		{
			name:     "with single quotes",
			argv:     []string{"echo", "it's fine"},
			expected: "echo 'it'\\''s fine'",
		},
		{
			name:     "with double quotes",
			argv:     []string{"echo", `say "hello"`},
			expected: `echo 'say "hello"'`,
		},
		{
			name:     "with dollar signs",
			argv:     []string{"echo", "$PATH"},
			expected: "echo '$PATH'",
		},
		{
			name:     "with backticks",
			argv:     []string{"echo", "`whoami`"},
			expected: "echo '`whoami`'",
		},
		{
			name:     "with backslashes",
			argv:     []string{"echo", `path\to\file`},
			expected: `echo 'path\to\file'`,
		},
		{
			name:     "empty string",
			argv:     []string{"echo", ""},
			expected: "echo ''",
		},
		{
			name:     "only empty strings",
			argv:     []string{"", "", ""},
			expected: "'' '' ''",
		},
		{
			name:     "with newlines",
			argv:     []string{"echo", "line1\nline2"},
			expected: "echo 'line1\nline2'",
		},
		{
			name:     "with tabs",
			argv:     []string{"echo", "col1\tcol2"},
			expected: "echo 'col1\tcol2'",
		},
		{
			name:     "with semicolons",
			argv:     []string{"echo", "a;b"},
			expected: "echo 'a;b'",
		},
		{
			name:     "with pipes",
			argv:     []string{"echo", "a|b"},
			expected: "echo 'a|b'",
		},
		{
			name:     "with ampersands",
			argv:     []string{"echo", "a&b"},
			expected: "echo 'a&b'",
		},
		{
			name:     "safe bare characters",
			argv:     []string{"cat", "/path/to/file-name_123.txt"},
			expected: "cat /path/to/file-name_123.txt",
		},
		{
			name:     "with colons and @",
			argv:     []string{"ssh", "user@host:22"},
			expected: "ssh 'user@host:22'", // @ is not safe, so gets quoted
		},
		{
			name:     "unicode characters",
			argv:     []string{"echo", "hello 世界"},
			expected: "echo 'hello 世界'",
		},
		{
			name:     "mixed safe and unsafe",
			argv:     []string{"git", "commit", "-m", "fix: resolve issue #123"},
			expected: "git commit -m 'fix: resolve issue #123'",
		},
		{
			name:     "multiple single quotes",
			argv:     []string{"echo", "can't won't shouldn't"},
			expected: "echo 'can'\\''t won'\\''t shouldn'\\''t'",
		},
		{
			name:     "only spaces",
			argv:     []string{"echo", "   "},
			expected: "echo '   '",
		},
		{
			name:     "parentheses",
			argv:     []string{"echo", "(hello)"},
			expected: "echo '(hello)'",
		},
		{
			name:     "brackets",
			argv:     []string{"echo", "[hello]"},
			expected: "echo '[hello]'",
		},
		{
			name:     "braces",
			argv:     []string{"echo", "{hello}"},
			expected: "echo '{hello}'",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := shellJoin(tt.argv)
			if got != tt.expected {
				t.Errorf("shellJoin(%q) = %q, want %q", tt.argv, got, tt.expected)
			}
		})
	}
}

func TestIsSafeBare(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected bool
	}{
		// Safe cases - only alphanumeric, dash, underscore, dot, slash, colon
		{"simple word", "hello", true},
		{"with numbers", "file123", true},
		{"with dash", "my-file", true},
		{"with underscore", "my_file", true},
		{"with dot", "file.txt", true},
		{"with slash", "/path/to/file", true},
		{"with colon only", "path:to:file", true},
		{"uppercase", "README", true},
		{"mixed alphanumeric", "AaBb123", true},
		{"complex safe", "path/to/file-name_v1.2.txt", true},
		{"just numbers", "12345", true},
		{"just dashes", "---", true},
		{"just underscores", "___", true},
		{"just dots", "...", true},
		{"just slashes", "///", true},
		{"just colons", ":::", true},

		// Unsafe cases - contain special characters
		{"with space", "hello world", false},
		{"with single quote", "it's", false},
		{"with double quote", `say "hi"`, false},
		{"with dollar", "$VAR", false},
		{"with backtick", "`cmd`", false},
		{"with backslash", `path\file`, false},
		{"with newline", "line1\nline2", false},
		{"with tab", "col1\tcol2", false},
		{"with semicolon", "cmd;cmd", false},
		{"with pipe", "a|b", false},
		{"with ampersand", "a&b", false},
		{"with asterisk", "*.txt", false},
		{"with question mark", "file?.txt", false},
		{"with exclamation", "hello!", false},
		{"with at sign", "user@host", false},
		{"with hash", "tag#123", false},
		{"with percent", "50%", false},
		{"with caret", "a^b", false},
		{"with tilde", "~/path", false},
		{"with equals", "key=value", false},
		{"with plus", "a+b", false},
		{"with parenthesis", "(test)", false},
		{"with bracket", "[test]", false},
		{"with brace", "{test}", false},
		{"with less than", "a<b", false},
		{"with greater than", "a>b", false},
		{"with comma", "a,b", false},
		{"empty string", "", true}, // edge case: no unsafe chars
		{"unicode", "hello世界", false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := isSafeBare(tt.input)
			if got != tt.expected {
				t.Errorf("isSafeBare(%q) = %v, want %v", tt.input, got, tt.expected)
			}
		})
	}
}

func TestSplitRun_AssemblesArgs(t *testing.T) {
	captured := struct {
		name string
		args []string
	}{}
	oldCmd := command
	t.Cleanup(func() { command = oldCmd })
	command = func(name string, args ...string) *exec.Cmd {
		captured.name = name
		captured.args = append([]string(nil), args...)
		// Use a benign command that exits 0
		return exec.Command("true")
	}
	opts := SplitOpts{Target: ":.", Vertical: true, Percent: 40}
	argv := []string{"/path/to/bin", "-flag", "value with spaces", "and'quote"}
	if err := SplitRun(opts, argv); err != nil {
		t.Fatalf("SplitRun error: %v", err)
	}
	if captured.name != "tmux" {
		t.Fatalf("expected tmux, got %q", captured.name)
	}
	wantFlags := map[string]bool{"split-window": true, "-v": true, "-p": true, "40": true, "-t": true, ":.": true}
	for _, a := range captured.args[:len(captured.args)-1] {
		if wantFlags[a] {
			delete(wantFlags, a)
		}
	}
	if len(wantFlags) != 0 {
		t.Fatalf("missing expected flags: %v", wantFlags)
	}
	last := captured.args[len(captured.args)-1]
	if last == "" || last == argv[0] {
		t.Fatalf("expected last arg to be joined command string, got %q", last)
	}
	_ = os.Unsetenv("TMUX")
}

func TestAvailable(t *testing.T) {
	oldLook := lookPath
	t.Cleanup(func() { lookPath = oldLook })
	// Present binary + TMUX set -> available
	lookPath = func(file string) (string, error) { return "/bin/tmux", nil }
	t.Setenv("TMUX", "/tmp/tmux-1,1,1")
	if !Available() {
		t.Fatal("expected Available true with TMUX + binary")
	}
	// No binary -> not available
	lookPath = func(file string) (string, error) { return "", errors.New("nope") }
	if Available() {
		t.Fatal("expected Available false without binary")
	}
}