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

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

func TestClaudeAgent_ExtractPrompt(t *testing.T) {
	agent := newClaudeAgent()
	tests := []struct {
		name    string
		content string
		want    string
	}{
		{
			name:    "single line",
			content: "──────\n❯ hello world\n──────",
			want:    "hello world",
		},
		{
			name: "multi-line between rules",
			content: "previous output\n" +
				"──────────────\n" +
				"❯ first line\n" +
				"\n" +
				"❯ second line\n" +
				"\n" +
				"❯ third line\n" +
				"──────────────\n" +
				"  -- INSERT --",
			want: "first line\nsecond line\nthird line",
		},
		{
			name: "wrapped long line",
			content: "──────────────\n" +
				"❯ This is a really long prompt that wraps\n" +
				"  to a second line in the terminal\n" +
				"──────────────\n" +
				"  -- INSERT --",
			want: "This is a really long prompt that wraps\nto a second line in the terminal",
		},
		{
			name: "ignores previous messages",
			content: "──────────────\n" +
				"❯ old user message\n" +
				"──────────────\n" +
				"assistant response here\n" +
				"──────────────\n" +
				"❯ current prompt\n" +
				"──────────────\n" +
				"  -- INSERT --",
			want: "current prompt",
		},
		{
			name:    "no match",
			content: "no prompt here",
			want:    "",
		},
		{
			name:    "no section delimiters",
			content: "❯ hello world",
			want:    "hello world",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := agent.ExtractPrompt(tt.content)
			if got != tt.want {
				t.Errorf("ExtractPrompt() = %q, want %q", got, tt.want)
			}
		})
	}
}

func TestClaudeAgent_ClearInput(t *testing.T) {
	noSleep(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
	}

	agent := newClaudeAgent()
	err := agent.ClearInput("%3")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// "Escape gg C-v G d i" should send each as separate send-keys call
	want := []string{
		"send:%3:Escape",
		"send:%3:gg",
		"send:%3:C-v",
		"send:%3:G",
		"send:%3:d",
		"send:%3:i",
	}
	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 TestClaudeAgent_Detect(t *testing.T) {
	agent := newClaudeAgent()
	tests := []struct {
		name    string
		content string
		want    bool
	}{
		{"prompt symbol", "❯ hello", true},
		{"claude code banner", "claude code v1.0", true},
		{"anthropic mention", "Powered by Anthropic", true},
		{"no match", "some text", false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := agent.Detect(tt.content); got != tt.want {
				t.Errorf("Detect() = %v, want %v", got, tt.want)
			}
		})
	}
}