| 1 | package tmux |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestBuildArgsSplitWindow(t *testing.T) { |
| 15 | args := BuildArgs("split-window", "-t", "%0", "-v", "-d", "-P", "-F", "#{pane_id}", "-c", "/tmp/test", "echo hello") |
| 16 | want := []string{"split-window", "-t", "%0", "-v", "-d", "-P", "-F", "#{pane_id}", "-c", "/tmp/test", "echo hello"} |
| 17 | |
| 18 | if len(args) != len(want) { |
| 19 | t.Fatalf("len(args) = %d, want %d", len(args), len(want)) |
| 20 | } |
| 21 | for i := range want { |
| 22 | if args[i] != want[i] { |
| 23 | t.Errorf("args[%d] = %q, want %q", i, args[i], want[i]) |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestBuildArgsKillPane(t *testing.T) { |
| 29 | args := BuildArgs("kill-pane", "-t", "%5") |
| 30 | want := []string{"kill-pane", "-t", "%5"} |
| 31 | |
| 32 | if len(args) != len(want) { |
| 33 | t.Fatalf("len(args) = %d, want %d", len(args), len(want)) |
| 34 | } |
| 35 | for i := range want { |
| 36 | if args[i] != want[i] { |
| 37 | t.Errorf("args[%d] = %q, want %q", i, args[i], want[i]) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestBuildArgsCapturePane(t *testing.T) { |
| 43 | args := BuildArgs("capture-pane", "-t", "%5", "-p", "-S", "-500") |
| 44 | want := []string{"capture-pane", "-t", "%5", "-p", "-S", "-500"} |
| 45 | |
| 46 | if len(args) != len(want) { |
| 47 | t.Fatalf("len(args) = %d, want %d", len(args), len(want)) |
| 48 | } |
| 49 | for i := range want { |
| 50 | if args[i] != want[i] { |
| 51 | t.Errorf("args[%d] = %q, want %q", i, args[i], want[i]) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestRebalanceLayoutEmptyPane(t *testing.T) { |
| 57 | err := RebalanceLayout(context.Background(), "") |
| 58 | if err == nil { |
| 59 | t.Fatal("expected error for empty targetPane, got nil") |
| 60 | } |
| 61 | want := "targetPane cannot be empty" |
| 62 | if err.Error() != want { |
| 63 | t.Errorf("error = %q, want %q", err.Error(), want) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestBuildArgsSetWindowOption(t *testing.T) { |
| 68 | args := BuildArgs("set-option", "-w", "-t", "%0", "automatic-rename", "off") |
| 69 | want := []string{"set-option", "-w", "-t", "%0", "automatic-rename", "off"} |
| 70 | |
| 71 | if len(args) != len(want) { |
| 72 | t.Fatalf("len(args) = %d, want %d", len(args), len(want)) |
| 73 | } |
| 74 | for i := range want { |
| 75 | if args[i] != want[i] { |
| 76 | t.Errorf("args[%d] = %q, want %q", i, args[i], want[i]) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestBuildArgsRenameWindow(t *testing.T) { |
| 82 | args := BuildArgs("rename-window", "-t", "%0", "my-repo") |
| 83 | want := []string{"rename-window", "-t", "%0", "my-repo"} |
| 84 | |
| 85 | if len(args) != len(want) { |
| 86 | t.Fatalf("len(args) = %d, want %d", len(args), len(want)) |
| 87 | } |
| 88 | for i := range want { |
| 89 | if args[i] != want[i] { |
| 90 | t.Errorf("args[%d] = %q, want %q", i, args[i], want[i]) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestBuildArgsSplitWindowSized(t *testing.T) { |
| 96 | args := BuildArgs("split-window", "-t", "%0", "-v", "-d", "-l", "30%", "-P", "-F", "#{pane_id}", "-c", "/tmp/test", "klaus dashboard") |
| 97 | want := []string{"split-window", "-t", "%0", "-v", "-d", "-l", "30%", "-P", "-F", "#{pane_id}", "-c", "/tmp/test", "klaus dashboard"} |
| 98 | |
| 99 | if len(args) != len(want) { |
| 100 | t.Fatalf("len(args) = %d, want %d", len(args), len(want)) |
| 101 | } |
| 102 | for i := range want { |
| 103 | if args[i] != want[i] { |
| 104 | t.Errorf("args[%d] = %q, want %q", i, args[i], want[i]) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | func TestBuildArgsSplitWindowSizedHorizontal(t *testing.T) { |
| 110 | args := BuildArgs("split-window", "-t", "%0", "-h", "-d", "-l", "50%", "-P", "-F", "#{pane_id}", "-c", "/tmp/test", "echo hello") |
| 111 | want := []string{"split-window", "-t", "%0", "-h", "-d", "-l", "50%", "-P", "-F", "#{pane_id}", "-c", "/tmp/test", "echo hello"} |
| 112 | |
| 113 | if len(args) != len(want) { |
| 114 | t.Fatalf("len(args) = %d, want %d", len(args), len(want)) |
| 115 | } |
| 116 | for i := range want { |
| 117 | if args[i] != want[i] { |
| 118 | t.Errorf("args[%d] = %q, want %q", i, args[i], want[i]) |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestBuildArgsSetPaneTitle(t *testing.T) { |
| 124 | args := BuildArgs("select-pane", "-t", "%3", "-T", "#42 fix login page crash") |
| 125 | want := []string{"select-pane", "-t", "%3", "-T", "#42 fix login page crash"} |
| 126 | |
| 127 | if !reflect.DeepEqual(args, want) { |
| 128 | t.Errorf("BuildArgs() got = %q, want %q", args, want) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestBuildArgsLockPaneTitle(t *testing.T) { |
| 133 | args := BuildArgs("set-option", "-p", "-t", "%3", "allow-rename", "off") |
| 134 | want := []string{"set-option", "-p", "-t", "%3", "allow-rename", "off"} |
| 135 | |
| 136 | if !reflect.DeepEqual(args, want) { |
| 137 | t.Errorf("BuildArgs() got = %q, want %q", args, want) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestBuildArgsSwapPane(t *testing.T) { |
| 142 | args := BuildArgs("swap-pane", "-s", "%3", "-t", "%5") |
| 143 | want := []string{"swap-pane", "-s", "%3", "-t", "%5"} |
| 144 | |
| 145 | if !reflect.DeepEqual(args, want) { |
| 146 | t.Errorf("BuildArgs() got = %q, want %q", args, want) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestBuildArgsListPanes(t *testing.T) { |
| 151 | args := BuildArgs("list-panes", "-t", "%0", "-F", "#{pane_id}") |
| 152 | want := []string{"list-panes", "-t", "%0", "-F", "#{pane_id}"} |
| 153 | |
| 154 | if !reflect.DeepEqual(args, want) { |
| 155 | t.Errorf("BuildArgs() got = %q, want %q", args, want) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestInSessionOutsideTmux(t *testing.T) { |
| 160 | // When running tests outside tmux, TMUX env var is typically not set |
| 161 | // This test documents the behavior — it may pass or fail depending on env |
| 162 | // The important thing is it doesn't panic |
| 163 | _ = InSession() |
| 164 | } |
| 165 | |
| 166 | // privateServer starts a throwaway tmux server holding session, on a socket |
| 167 | // in the test's temp dir, and returns the socket and a $TMUX naming it. |
| 168 | func privateServer(t *testing.T, session string) (sock, tmuxEnv string) { |
| 169 | t.Helper() |
| 170 | if _, err := exec.LookPath("tmux"); err != nil { |
| 171 | if os.Getenv("KLAUS_TEST_TOOLS") == "required" { |
| 172 | t.Fatal("tmux not on PATH") |
| 173 | } |
| 174 | t.Skip("tmux not on PATH") |
| 175 | } |
| 176 | sock = filepath.Join(t.TempDir(), "sock") |
| 177 | ctl := func(args ...string) string { |
| 178 | t.Helper() |
| 179 | out, err := exec.Command("tmux", append([]string{"-S", sock, "-f", "/dev/null"}, args...)...).CombinedOutput() |
| 180 | if err != nil { |
| 181 | t.Fatalf("tmux %v: %v: %s", args, err, out) |
| 182 | } |
| 183 | return strings.TrimSpace(string(out)) |
| 184 | } |
| 185 | ctl("new-session", "-d", "-s", session, "sleep 300") |
| 186 | t.Cleanup(func() { _ = exec.Command("tmux", "-S", sock, "kill-server").Run() }) |
| 187 | return sock, sock + "," + ctl("display-message", "-p", "#{pid}") + ",0" |
| 188 | } |
| 189 | |
| 190 | // A worker runs with no $TMUX and the shared server's socket in SocketEnv, and |
| 191 | // klaus's own calls there reach that server. In a pane $TMUX wins, so a |
| 192 | // process in a test's private server never trades it for a socket it |
| 193 | // inherited. |
| 194 | func TestCallsReachTheServerTheyAreGiven(t *testing.T) { |
| 195 | ctx := context.Background() |
| 196 | shared, _ := privateServer(t, "shared") |
| 197 | private, privateEnv := privateServer(t, "private") |
| 198 | |
| 199 | t.Setenv("TMUX", "") |
| 200 | t.Setenv(SocketEnv, shared) |
| 201 | if got := SocketPath(); got != shared { |
| 202 | t.Errorf("SocketPath() = %q without $TMUX, want %s's %q", got, SocketEnv, shared) |
| 203 | } |
| 204 | if !SessionExists(ctx, "shared") { |
| 205 | t.Errorf("without $TMUX, a call did not reach the server in %s", SocketEnv) |
| 206 | } |
| 207 | |
| 208 | t.Setenv("TMUX", privateEnv) |
| 209 | if got := SocketPath(); got != private { |
| 210 | t.Errorf("SocketPath() = %q in a pane, want $TMUX's %q", got, private) |
| 211 | } |
| 212 | if !SessionExists(ctx, "private") || SessionExists(ctx, "shared") { |
| 213 | t.Errorf("in a pane, a call left $TMUX's server for the one in %s", SocketEnv) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Issue #308: a pane command over tmux's imsg limit fails with "command too |
| 218 | // long". The error must lead with that, not with kilobytes of echoed command. |
| 219 | // Runs the real tmux against a throwaway server; $TMUX aims runTmux at it. |
| 220 | func TestRunTmuxErrorLeadsWithStderr(t *testing.T) { |
| 221 | _, tmuxEnv := privateServer(t, "main") |
| 222 | t.Setenv("TMUX", tmuxEnv) |
| 223 | |
| 224 | _, err := NewDetachedWindow(context.Background(), "klaus-agents-test", "run", t.TempDir(), "true "+strings.Repeat("x", 20000)) |
| 225 | if err == nil { |
| 226 | t.Fatal("tmux accepted a 20KB command; the pane size guard may be stale") |
| 227 | } |
| 228 | msg := err.Error() |
| 229 | if !strings.HasPrefix(msg, "new-session: tmux new-session: command too long (args: tmux new-session -d -s klaus-agents-test") { |
| 230 | t.Fatalf("tmux's message is not first: %.300s", msg) |
| 231 | } |
| 232 | if !regexp.MustCompile(`… \[truncated \d+ bytes\]\)$`).MatchString(msg) || len(msg) > 400 { |
| 233 | t.Fatalf("args not truncated (%d bytes): %.500s", len(msg), msg) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | func TestTruncateBytesKeepsRunesWhole(t *testing.T) { |
| 238 | if got := truncateBytes("ab", 5); got != "ab" { |
| 239 | t.Fatalf("short input changed: %q", got) |
| 240 | } |
| 241 | got := truncateBytes("aé"+strings.Repeat("z", 10), 2) |
| 242 | if got != "a… [truncated 12 bytes]" { |
| 243 | t.Fatalf("got %q", got) |
| 244 | } |
| 245 | } |