summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/pane_test.go
blob: 5b6f1b6cf73ac162ded6a883f5d8c79c465eab0f (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
package tmuxedit

import (
	"fmt"
	"testing"
)

func TestResolveTargetPane_FlagWins(t *testing.T) {
	old := runCommand
	defer func() { runCommand = old }()
	runCommand = func(string, ...string) ([]byte, error) {
		return []byte("%99"), nil
	}
	t.Setenv("HEXAI_TMUX_PANE", "%10")
	got, err := resolveTargetPane("%5")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if got != "%5" {
		t.Errorf("got %q, want %%5 (flag should win)", got)
	}
}

func TestResolveTargetPane_EnvFallback(t *testing.T) {
	old := runCommand
	defer func() { runCommand = old }()
	runCommand = func(string, ...string) ([]byte, error) {
		return []byte("%99"), nil
	}
	t.Setenv("HEXAI_TMUX_PANE", "%10")
	got, err := resolveTargetPane("")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if got != "%10" {
		t.Errorf("got %q, want %%10 (env fallback)", got)
	}
}

func TestResolveTargetPane_TmuxQuery(t *testing.T) {
	old := runCommand
	defer func() { runCommand = old }()
	runCommand = func(name string, args ...string) ([]byte, error) {
		if name == "tmux" && len(args) > 0 && args[0] == "display-message" {
			return []byte("%42\n"), nil
		}
		return nil, fmt.Errorf("unexpected command: %s", name)
	}
	t.Setenv("HEXAI_TMUX_PANE", "")
	got, err := resolveTargetPane("")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if got != "%42" {
		t.Errorf("got %q, want %%42 (tmux query)", got)
	}
}

func TestResolveTargetPane_TmuxError(t *testing.T) {
	old := runCommand
	defer func() { runCommand = old }()
	runCommand = func(string, ...string) ([]byte, error) {
		return nil, fmt.Errorf("tmux not available")
	}
	t.Setenv("HEXAI_TMUX_PANE", "")
	_, err := resolveTargetPane("")
	if err == nil {
		t.Fatal("expected error when tmux fails")
	}
}

func TestResolveTargetPane_TmuxEmptyOutput(t *testing.T) {
	old := runCommand
	defer func() { runCommand = old }()
	runCommand = func(string, ...string) ([]byte, error) {
		return []byte("  \n"), nil
	}
	t.Setenv("HEXAI_TMUX_PANE", "")
	_, err := resolveTargetPane("")
	if err == nil {
		t.Fatal("expected error for empty tmux output")
	}
}