summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/send_test.go
blob: 3722d1af541dc16b117442e4015b172c16011a0b (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
package tmuxedit

import (
	"fmt"
	"strings"
	"testing"
)

// noSleep disables the post-clear sleep in tests and restores it on cleanup.
func noSleep(t *testing.T) {
	t.Helper()
	old := sleepAfterClear
	sleepAfterClear = func() {}
	t.Cleanup(func() { sleepAfterClear = old })
}

func TestDeduplicateText(t *testing.T) {
	tests := []struct {
		name     string
		original string
		edited   string
		want     string
	}{
		{"empty both", "", "", ""},
		{"empty original", "", "new text", "new text"},
		{"empty edited", "original", "", ""},
		{"unchanged", "hello world", "hello world", ""},
		{"appended", "hello", "hello world", "hello world"},
		{"rewritten", "hello world", "goodbye world", "goodbye world"},
		{"whitespace handling", "  hello  ", "  hello  world  ", "hello  world"},
		{"appended with newlines", "line1\nline2", "line1\nline2\nline3", "line1\nline2\nline3"},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := deduplicateText(tt.original, tt.edited)
			if got != tt.want {
				t.Errorf("deduplicateText(%q, %q) = %q, want %q",
					tt.original, tt.edited, got, tt.want)
			}
		})
	}
}

func TestSendLines_SingleLine(t *testing.T) {
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, fmt.Sprintf("send:%s:%s", paneID, strings.Join(keys, ",")))
		return nil
	}

	err := sendLines("%5", "hello", "S-Enter")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(calls) != 1 {
		t.Fatalf("got %d calls, want 1: %v", len(calls), calls)
	}
	if calls[0] != "send:%5:hello" {
		t.Errorf("call[0] = %q, want text", calls[0])
	}
}

func TestSendLines_MultiLine(t *testing.T) {
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, strings.Join(keys, ","))
		return nil
	}

	err := sendLines("%1", "line1\nline2\nline3", "S-Enter")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want := []string{"line1", "S-Enter", "line2", "S-Enter", "line3"}
	if len(calls) != len(want) {
		t.Fatalf("got %d calls, want %d: %v", len(calls), len(want), calls)
	}
	for i, w := range want {
		if calls[i] != w {
			t.Errorf("call[%d] = %q, want %q", i, calls[i], w)
		}
	}
}

func TestSendLines_FallbackNewline(t *testing.T) {
	var calls []string
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(paneID string, keys ...string) error {
		calls = append(calls, strings.Join(keys, ","))
		return nil
	}

	// Empty newlineKeys should fallback to "Enter"
	err := sendLines("%1", "a\nb", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(calls) != 3 {
		t.Fatalf("got %d calls, want 3: %v", len(calls), calls)
	}
	if calls[1] != "Enter" {
		t.Errorf("newline key = %q, want Enter (fallback)", calls[1])
	}
}

func TestSendLines_Error(t *testing.T) {
	oldSend := sendKeys
	defer func() { sendKeys = oldSend }()
	sendKeys = func(string, ...string) error {
		return fmt.Errorf("send failed")
	}

	err := sendLines("%1", "hello", "Enter")
	if err == nil {
		t.Fatal("expected error on send failure")
	}
}