diff options
Diffstat (limited to 'internal/tmuxedit/send_test.go')
| -rw-r--r-- | internal/tmuxedit/send_test.go | 183 |
1 files changed, 30 insertions, 153 deletions
diff --git a/internal/tmuxedit/send_test.go b/internal/tmuxedit/send_test.go index e458282..3722d1a 100644 --- a/internal/tmuxedit/send_test.go +++ b/internal/tmuxedit/send_test.go @@ -6,6 +6,14 @@ import ( "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 @@ -33,16 +41,7 @@ func TestDeduplicateText(t *testing.T) { } } -// 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 TestSendTextToPane_SingleLine(t *testing.T) { - noSleep(t) +func TestSendLines_SingleLine(t *testing.T) { var calls []string oldSend := sendKeys defer func() { sendKeys = oldSend }() @@ -50,24 +49,20 @@ func TestSendTextToPane_SingleLine(t *testing.T) { calls = append(calls, fmt.Sprintf("send:%s:%s", paneID, strings.Join(keys, ","))) return nil } - agent := AgentConfig{ClearFirst: true, ClearKeys: "C-u", NewlineKeys: "S-Enter"} - err := sendTextToPane("%5", "hello", agent) + + err := sendLines("%5", "hello", "S-Enter") if err != nil { t.Fatalf("unexpected error: %v", err) } - // Expect: clear, then single line (no newline after last line) - if len(calls) != 2 { - t.Fatalf("got %d calls, want 2: %v", len(calls), calls) - } - if calls[0] != "send:%5:C-u" { - t.Errorf("call[0] = %q, want clear", calls[0]) + if len(calls) != 1 { + t.Fatalf("got %d calls, want 1: %v", len(calls), calls) } - if calls[1] != "send:%5:hello" { - t.Errorf("call[1] = %q, want text", calls[1]) + if calls[0] != "send:%5:hello" { + t.Errorf("call[0] = %q, want text", calls[0]) } } -func TestSendTextToPane_MultiLine(t *testing.T) { +func TestSendLines_MultiLine(t *testing.T) { var calls []string oldSend := sendKeys defer func() { sendKeys = oldSend }() @@ -75,12 +70,11 @@ func TestSendTextToPane_MultiLine(t *testing.T) { calls = append(calls, strings.Join(keys, ",")) return nil } - agent := AgentConfig{NewlineKeys: "S-Enter"} - err := sendTextToPane("%1", "line1\nline2\nline3", agent) + + err := sendLines("%1", "line1\nline2\nline3", "S-Enter") if err != nil { t.Fatalf("unexpected error: %v", err) } - // Expect: line1, S-Enter, line2, S-Enter, line3 (no trailing newline) 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) @@ -92,7 +86,7 @@ func TestSendTextToPane_MultiLine(t *testing.T) { } } -func TestSendTextToPane_NoClear(t *testing.T) { +func TestSendLines_FallbackNewline(t *testing.T) { var calls []string oldSend := sendKeys defer func() { sendKeys = oldSend }() @@ -100,146 +94,29 @@ func TestSendTextToPane_NoClear(t *testing.T) { calls = append(calls, strings.Join(keys, ",")) return nil } - agent := AgentConfig{ClearFirst: false, ClearKeys: "C-u"} - err := sendTextToPane("%1", "hello", agent) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - // No clear call; just the text - if len(calls) != 1 { - t.Fatalf("got %d calls, want 1: %v", len(calls), calls) - } -} -func TestSendTextToPane_Empty(t *testing.T) { - oldSend := sendKeys - defer func() { sendKeys = oldSend }() - sendKeys = func(string, ...string) error { - t.Fatal("sendKeys should not be called for empty text") - return nil - } - err := sendTextToPane("%1", "", AgentConfig{}) + // Empty newlineKeys should fallback to "Enter" + err := sendLines("%1", "a\nb", "") if err != nil { t.Fatalf("unexpected error: %v", err) } -} - -func TestSendTextToPane_ClearError(t *testing.T) { - noSleep(t) - oldSend := sendKeys - defer func() { sendKeys = oldSend }() - sendKeys = func(paneID string, keys ...string) error { - return fmt.Errorf("tmux error") + if len(calls) != 3 { + t.Fatalf("got %d calls, want 3: %v", len(calls), calls) } - agent := AgentConfig{ClearFirst: true, ClearKeys: "C-u"} - err := sendTextToPane("%1", "hello", agent) - if err == nil { - t.Fatal("expected error on clear failure") + if calls[1] != "Enter" { + t.Errorf("newline key = %q, want Enter (fallback)", calls[1]) } } -func TestSendTextToPane_SendError(t *testing.T) { - noSleep(t) - callCount := 0 +func TestSendLines_Error(t *testing.T) { oldSend := sendKeys defer func() { sendKeys = oldSend }() - sendKeys = func(paneID string, keys ...string) error { - callCount++ - if callCount == 2 { // fail on second call (first line text) - return fmt.Errorf("send failed") - } - return nil + sendKeys = func(string, ...string) error { + return fmt.Errorf("send failed") } - agent := AgentConfig{ClearFirst: true, ClearKeys: "C-u"} - err := sendTextToPane("%1", "hello", agent) + + err := sendLines("%1", "hello", "Enter") if err == nil { t.Fatal("expected error on send failure") } } - -func TestSendTextToPane_BulkClear(t *testing.T) { - noSleep(t) - var calls []string - oldSend := sendKeys - oldRepeat := sendRepeatedKey - defer func() { - sendKeys = oldSend - sendRepeatedKey = oldRepeat - }() - sendKeys = func(paneID string, keys ...string) error { - calls = append(calls, fmt.Sprintf("send:%s:%s", paneID, strings.Join(keys, ","))) - return nil - } - sendRepeatedKey = func(paneID, key string, count int) error { - calls = append(calls, fmt.Sprintf("repeat:%s:%s*%d", paneID, key, count)) - return nil - } - // "End BSpace*200" should send End normally, then BSpace 200 times via -N - agent := AgentConfig{ClearFirst: true, ClearKeys: "End BSpace*200", NewlineKeys: "S-Enter"} - err := sendTextToPane("%5", "new text", agent) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - want := []string{ - "send:%5:End", - "repeat:%5:BSpace*200", - "send:%5:new text", - } - 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 TestParseKeyRepeat(t *testing.T) { - tests := []struct { - token string - wantKey string - wantCount int - }{ - {"BSpace*200", "BSpace", 200}, - {"End", "End", 1}, - {"C-u", "C-u", 1}, - {"BSpace*1", "BSpace", 1}, - {"BSpace*0", "BSpace*0", 1}, // invalid count - {"BSpace*abc", "BSpace*abc", 1}, // non-numeric - {"*200", "*200", 1}, // no key name - {"x*3", "x", 3}, - } - for _, tt := range tests { - t.Run(tt.token, func(t *testing.T) { - key, count := parseKeyRepeat(tt.token) - if key != tt.wantKey || count != tt.wantCount { - t.Errorf("parseKeyRepeat(%q) = (%q, %d), want (%q, %d)", - tt.token, key, count, tt.wantKey, tt.wantCount) - } - }) - } -} - -func TestSendTextToPane_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 - } - // Agent with empty NewlineKeys should fallback to "Enter" - agent := AgentConfig{NewlineKeys: ""} - err := sendTextToPane("%1", "a\nb", agent) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - // Expect: a, Enter, b - 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]) - } -} |
