summaryrefslogtreecommitdiff
path: root/internal/tmuxedit/history_test.go
blob: f6d6d7dd846c7fdb59d08beb20e558b1ae3997e6 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package tmuxedit

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"
	"time"

	"codeberg.org/snonux/hexai/internal/textutil"
)

func TestAppendHistory(t *testing.T) {
	// Create temp directory for test
	tmpDir := t.TempDir()
	t.Setenv("XDG_STATE_HOME", tmpDir)

	text := "test prompt text"
	agent := "claude"
	cwd := "/tmp/test"

	// Append first entry
	if err := AppendHistory(text, agent, cwd); err != nil {
		t.Fatalf("AppendHistory failed: %v", err)
	}

	// Verify file was created
	historyPath := filepath.Join(tmpDir, "state", "tmux-edit-history.jsonl")
	if _, err := os.Stat(historyPath); err != nil {
		t.Fatalf("history file not created: %v", err)
	}

	// Read and verify content
	data, err := os.ReadFile(historyPath)
	if err != nil {
		t.Fatalf("cannot read history file: %v", err)
	}

	content := string(data)
	if content == "" {
		t.Fatal("history file is empty")
	}

	// Verify it contains expected fields
	if !containsString(content, "test prompt text") {
		t.Error("history doesn't contain text")
	}
	if !containsString(content, "claude") {
		t.Error("history doesn't contain agent")
	}
	if !containsString(content, "/tmp/test") {
		t.Error("history doesn't contain cwd")
	}
}

func TestGetHistory(t *testing.T) {
	tmpDir := t.TempDir()
	t.Setenv("XDG_STATE_HOME", tmpDir)

	// Append multiple entries
	entries := []struct {
		text  string
		agent string
		cwd   string
	}{
		{"first prompt", "claude", "/home/user"},
		{"second prompt", "aider", "/tmp/project"},
		{"third prompt", "claude", "/var/tmp"},
	}

	for _, e := range entries {
		if err := AppendHistory(e.text, e.agent, e.cwd); err != nil {
			t.Fatalf("AppendHistory failed: %v", err)
		}
		time.Sleep(10 * time.Millisecond) // Ensure different timestamps
	}

	// Get all history
	history, err := GetHistory(0)
	if err != nil {
		t.Fatalf("GetHistory failed: %v", err)
	}

	if len(history) != 3 {
		t.Fatalf("expected 3 entries, got %d", len(history))
	}

	// Verify first entry
	if history[0].Text != "first prompt" {
		t.Errorf("first entry text: got %q, want %q", history[0].Text, "first prompt")
	}
	if history[0].Agent != "claude" {
		t.Errorf("first entry agent: got %q, want %q", history[0].Agent, "claude")
	}

	// Test limit
	limited, err := GetHistory(2)
	if err != nil {
		t.Fatalf("GetHistory with limit failed: %v", err)
	}
	if len(limited) != 2 {
		t.Fatalf("expected 2 entries with limit, got %d", len(limited))
	}

	// Should get the most recent 2
	if limited[0].Text != "second prompt" {
		t.Errorf("limited[0] should be second entry")
	}
	if limited[1].Text != "third prompt" {
		t.Errorf("limited[1] should be third entry")
	}
}

func TestGetHistory_EmptyFile(t *testing.T) {
	tmpDir := t.TempDir()
	t.Setenv("XDG_STATE_HOME", tmpDir)

	// Get history when file doesn't exist
	history, err := GetHistory(0)
	if err != nil {
		t.Fatalf("GetHistory should not error on missing file: %v", err)
	}

	if len(history) != 0 {
		t.Errorf("expected empty history, got %d entries", len(history))
	}
}

func TestSplitLines(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  []string
	}{
		{
			name:  "unix newlines",
			input: "line1\nline2\nline3",
			want:  []string{"line1", "line2", "line3"},
		},
		{
			name:  "windows newlines",
			input: "line1\r\nline2\r\nline3",
			want:  []string{"line1", "line2", "line3"},
		},
		{
			name:  "mixed newlines",
			input: "line1\nline2\r\nline3",
			want:  []string{"line1", "line2", "line3"},
		},
		{
			name:  "trailing newline",
			input: "line1\nline2\n",
			want:  []string{"line1", "line2"},
		},
		{
			name:  "empty string",
			input: "",
			want:  []string{},
		},
		{
			name:  "single line no newline",
			input: "single",
			want:  []string{"single"},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := textutil.SplitLinesBytes([]byte(tt.input))
			gotStr := make([]string, len(got))
			for i, b := range got {
				gotStr[i] = string(b)
			}

			if len(gotStr) != len(tt.want) {
				t.Fatalf("got %d lines, want %d", len(gotStr), len(tt.want))
			}

			for i := range gotStr {
				if gotStr[i] != tt.want[i] {
					t.Errorf("line %d: got %q, want %q", i, gotStr[i], tt.want[i])
				}
			}
		})
	}
}

func TestGetHistory_MalformedEntries(t *testing.T) {
	tmpDir := t.TempDir()
	t.Setenv("XDG_STATE_HOME", tmpDir)

	// Create state directory and write a file with some valid and invalid lines
	stateDir := filepath.Join(tmpDir, "state")
	if err := os.MkdirAll(stateDir, 0o755); err != nil {
		t.Fatalf("cannot create state dir: %v", err)
	}
	historyPath := filepath.Join(stateDir, "tmux-edit-history.jsonl")
	content := `{"timestamp":"2025-01-01T00:00:00Z","agent":"claude","cwd":"/tmp","text":"valid"}
not json at all
{"timestamp":"2025-01-02T00:00:00Z","agent":"aider","cwd":"/home","text":"also valid"}
`
	if err := os.WriteFile(historyPath, []byte(content), 0o644); err != nil {
		t.Fatalf("cannot write history file: %v", err)
	}

	entries, err := GetHistory(0)
	if err != nil {
		t.Fatalf("GetHistory failed: %v", err)
	}
	// Should skip the malformed line and return the 2 valid entries
	if len(entries) != 2 {
		t.Fatalf("expected 2 entries, got %d", len(entries))
	}
	if entries[0].Text != "valid" {
		t.Errorf("entries[0].Text = %q, want 'valid'", entries[0].Text)
	}
	if entries[1].Text != "also valid" {
		t.Errorf("entries[1].Text = %q, want 'also valid'", entries[1].Text)
	}
}

func TestGetHistory_EmptyLines(t *testing.T) {
	tmpDir := t.TempDir()
	t.Setenv("XDG_STATE_HOME", tmpDir)

	stateDir := filepath.Join(tmpDir, "state")
	if err := os.MkdirAll(stateDir, 0o755); err != nil {
		t.Fatalf("cannot create state dir: %v", err)
	}
	historyPath := filepath.Join(stateDir, "tmux-edit-history.jsonl")
	// File with empty lines interspersed
	content := "\n" +
		`{"timestamp":"2025-01-01T00:00:00Z","agent":"claude","cwd":"/tmp","text":"entry1"}` + "\n" +
		"\n\n" +
		`{"timestamp":"2025-01-02T00:00:00Z","agent":"aider","cwd":"/home","text":"entry2"}` + "\n"
	if err := os.WriteFile(historyPath, []byte(content), 0o644); err != nil {
		t.Fatalf("cannot write history file: %v", err)
	}

	entries, err := GetHistory(0)
	if err != nil {
		t.Fatalf("GetHistory failed: %v", err)
	}
	if len(entries) != 2 {
		t.Fatalf("expected 2 entries (skipping empty lines), got %d", len(entries))
	}
}

func TestGetHistory_LimitZeroReturnsAll(t *testing.T) {
	tmpDir := t.TempDir()
	t.Setenv("XDG_STATE_HOME", tmpDir)

	for i := 0; i < 5; i++ {
		if err := AppendHistory(fmt.Sprintf("entry%d", i), "claude", "/tmp"); err != nil {
			t.Fatalf("AppendHistory failed: %v", err)
		}
	}

	entries, err := GetHistory(0)
	if err != nil {
		t.Fatalf("GetHistory failed: %v", err)
	}
	if len(entries) != 5 {
		t.Errorf("expected 5 entries with limit=0, got %d", len(entries))
	}
}

func TestGetHistory_LimitLargerThanEntries(t *testing.T) {
	tmpDir := t.TempDir()
	t.Setenv("XDG_STATE_HOME", tmpDir)

	if err := AppendHistory("only one", "claude", "/tmp"); err != nil {
		t.Fatalf("AppendHistory failed: %v", err)
	}

	entries, err := GetHistory(100)
	if err != nil {
		t.Fatalf("GetHistory failed: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("expected 1 entry with large limit, got %d", len(entries))
	}
}

func TestAppendHistory_InvalidStateDir(t *testing.T) {
	// Point XDG_STATE_HOME to a path that can't be created (file, not dir)
	tmpDir := t.TempDir()
	blockingFile := filepath.Join(tmpDir, "blocker")
	if err := os.WriteFile(blockingFile, []byte("x"), 0o644); err != nil {
		t.Fatalf("cannot create blocking file: %v", err)
	}
	// Set state home to a path under the file (impossible to mkdir)
	t.Setenv("XDG_STATE_HOME", filepath.Join(blockingFile, "sub"))

	err := AppendHistory("text", "agent", "/cwd")
	if err == nil {
		t.Fatal("expected error when state directory cannot be created")
	}
}

func TestGetHistory_InvalidStateDir(t *testing.T) {
	tmpDir := t.TempDir()
	blockingFile := filepath.Join(tmpDir, "blocker")
	if err := os.WriteFile(blockingFile, []byte("x"), 0o644); err != nil {
		t.Fatalf("cannot create blocking file: %v", err)
	}
	t.Setenv("XDG_STATE_HOME", filepath.Join(blockingFile, "sub"))

	_, err := GetHistory(0)
	if err == nil {
		t.Fatal("expected error when state directory cannot be created")
	}
}

func containsString(s, substr string) bool {
	return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && findSubstring(s, substr))
}

func findSubstring(s, substr string) bool {
	for i := 0; i <= len(s)-len(substr); i++ {
		if s[i:i+len(substr)] == substr {
			return true
		}
	}
	return false
}