-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
348 lines (281 loc) · 8.61 KB
/
app_test.go
File metadata and controls
348 lines (281 loc) · 8.61 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"sync"
"testing"
)
func TestNewApp(t *testing.T) {
entry := FileEntry{
Name: "test.html",
Path: "/tmp/test.html",
Content: "<html>test</html>",
}
windowID := "test-window-id"
app := NewApp(entry, windowID)
if app == nil {
t.Fatal("NewApp() returned nil")
}
if len(app.files) != 1 {
t.Errorf("Expected 1 file, got %d", len(app.files))
}
if app.files[0].Name != entry.Name {
t.Errorf("File name mismatch: got %q, want %q", app.files[0].Name, entry.Name)
}
if app.currentIndex != 0 {
t.Errorf("Expected currentIndex 0, got %d", app.currentIndex)
}
if app.windowID != windowID {
t.Errorf("Window ID mismatch: got %q, want %q", app.windowID, windowID)
}
}
func TestGetHTMLContent(t *testing.T) {
content := "<html><body>Hello</body></html>"
app := NewApp(FileEntry{Name: "test", Content: content}, "")
got := app.GetHTMLContent()
if got != content {
t.Errorf("GetHTMLContent() = %q, want %q", got, content)
}
}
func TestGetHTMLContentEmpty(t *testing.T) {
app := &App{
files: []FileEntry{},
currentIndex: 0,
}
got := app.GetHTMLContent()
if got != "" {
t.Errorf("GetHTMLContent() with empty files should return empty string, got %q", got)
}
}
func TestGetHTMLContentInvalidIndex(t *testing.T) {
app := NewApp(FileEntry{Name: "test", Content: "<html></html>"}, "")
app.currentIndex = 5 // Invalid index
got := app.GetHTMLContent()
if got != "" {
t.Errorf("GetHTMLContent() with invalid index should return empty string, got %q", got)
}
}
func TestGetCurrentBasePath(t *testing.T) {
app := NewApp(FileEntry{
Name: "test.html",
Path: "/Users/test/documents/test.html",
Content: "<html></html>",
}, "")
got := app.GetCurrentBasePath()
want := "/Users/test/documents"
if got != want {
t.Errorf("GetCurrentBasePath() = %q, want %q", got, want)
}
}
func TestGetCurrentBasePathEmpty(t *testing.T) {
// Test with empty files
app := &App{
files: []FileEntry{},
currentIndex: 0,
}
got := app.GetCurrentBasePath()
if got != "" {
t.Errorf("GetCurrentBasePath() with empty files should return empty string, got %q", got)
}
}
func TestGetCurrentBasePathStdin(t *testing.T) {
// Test stdin content (no path)
app := NewApp(FileEntry{
Name: "stdin",
Path: "", // stdin has no path
Content: "<html></html>",
}, "")
got := app.GetCurrentBasePath()
if got != "" {
t.Errorf("GetCurrentBasePath() with stdin (empty path) should return empty string, got %q", got)
}
}
func TestGetCurrentBasePathInvalidIndex(t *testing.T) {
app := NewApp(FileEntry{Name: "test", Path: "/tmp/test.html", Content: "<html></html>"}, "")
app.currentIndex = 5 // Invalid index
got := app.GetCurrentBasePath()
if got != "" {
t.Errorf("GetCurrentBasePath() with invalid index should return empty string, got %q", got)
}
}
func TestGetFiles(t *testing.T) {
app := NewApp(FileEntry{Name: "test1", Content: "<html>1</html>"}, "")
app.files = append(app.files, FileEntry{Name: "test2", Content: "<html>2</html>"})
files := app.GetFiles()
if len(files) != 2 {
t.Errorf("GetFiles() returned %d files, want 2", len(files))
}
// Verify it's a copy by modifying the returned slice
files[0].Name = "modified"
if app.files[0].Name == "modified" {
t.Error("GetFiles() should return a copy, not the original slice")
}
}
func TestGetCurrentIndex(t *testing.T) {
app := NewApp(FileEntry{Name: "test", Content: "<html></html>"}, "")
app.currentIndex = 3
if got := app.GetCurrentIndex(); got != 3 {
t.Errorf("GetCurrentIndex() = %d, want 3", got)
}
}
func TestSelectFile(t *testing.T) {
app := NewApp(FileEntry{Name: "file1", Content: "<html>1</html>"}, "")
app.files = append(app.files, FileEntry{Name: "file2", Content: "<html>2</html>"})
app.files = append(app.files, FileEntry{Name: "file3", Content: "<html>3</html>"})
content := app.SelectFile(1)
if content != "<html>2</html>" {
t.Errorf("SelectFile(1) returned %q, want %q", content, "<html>2</html>")
}
if app.currentIndex != 1 {
t.Errorf("currentIndex should be 1, got %d", app.currentIndex)
}
}
func TestSelectFileInvalidIndex(t *testing.T) {
app := NewApp(FileEntry{Name: "test", Content: "<html></html>"}, "")
// Test negative index
content := app.SelectFile(-1)
if content != "" {
t.Errorf("SelectFile(-1) should return empty string, got %q", content)
}
// Test out of bounds index
content = app.SelectFile(10)
if content != "" {
t.Errorf("SelectFile(10) should return empty string, got %q", content)
}
}
func TestAddFile(t *testing.T) {
app := NewApp(FileEntry{Name: "beta", Content: "<html>beta</html>"}, "")
// Add file that should sort before "beta"
app.AddFile(FileEntry{Name: "alpha", Path: "/tmp/alpha.html", Content: "<html>alpha</html>"})
files := app.GetFiles()
if len(files) != 2 {
t.Errorf("Expected 2 files, got %d", len(files))
}
// Files should be sorted alphabetically
if files[0].Name != "alpha" {
t.Errorf("First file should be 'alpha', got %q", files[0].Name)
}
if files[1].Name != "beta" {
t.Errorf("Second file should be 'beta', got %q", files[1].Name)
}
}
func TestReplaceFileContentExisting(t *testing.T) {
app := NewApp(FileEntry{Name: "test", Path: "/tmp/test.html", Content: "<html>original</html>"}, "")
app.ReplaceFileContent("/tmp/test.html", "<html>replaced</html>", "newname")
files := app.GetFiles()
if len(files) != 1 {
t.Errorf("Expected 1 file, got %d", len(files))
}
if files[0].Content != "<html>replaced</html>" {
t.Errorf("Content not replaced: got %q", files[0].Content)
}
if files[0].Name != "newname" {
t.Errorf("Name not updated: got %q", files[0].Name)
}
}
func TestReplaceFileContentNew(t *testing.T) {
app := NewApp(FileEntry{Name: "existing", Path: "/tmp/existing.html", Content: "<html>existing</html>"}, "")
app.ReplaceFileContent("/tmp/new.html", "<html>new</html>", "newfile")
files := app.GetFiles()
if len(files) != 2 {
t.Errorf("Expected 2 files, got %d", len(files))
}
// Find the new file
var found bool
for _, f := range files {
if f.Path == "/tmp/new.html" {
found = true
if f.Content != "<html>new</html>" {
t.Errorf("New file content wrong: got %q", f.Content)
}
if f.Name != "newfile" {
t.Errorf("New file name wrong: got %q", f.Name)
}
}
}
if !found {
t.Error("New file not found in files list")
}
}
func TestReplaceFileContentPreservesNameIfEmpty(t *testing.T) {
app := NewApp(FileEntry{Name: "original-name", Path: "/tmp/test.html", Content: "<html>original</html>"}, "")
// Replace with empty name - should preserve original name
app.ReplaceFileContent("/tmp/test.html", "<html>replaced</html>", "")
files := app.GetFiles()
if files[0].Name != "original-name" {
t.Errorf("Name should be preserved when replacement name is empty, got %q", files[0].Name)
}
}
func TestGetWindowID(t *testing.T) {
windowID := "test-uuid-12345"
app := NewApp(FileEntry{Name: "test", Content: "<html></html>"}, windowID)
if got := app.GetWindowID(); got != windowID {
t.Errorf("GetWindowID() = %q, want %q", got, windowID)
}
}
func TestGetWindowIDEmpty(t *testing.T) {
app := NewApp(FileEntry{Name: "test", Content: "<html></html>"}, "")
if got := app.GetWindowID(); got != "" {
t.Errorf("GetWindowID() should return empty string, got %q", got)
}
}
func TestConcurrentAccess(t *testing.T) {
app := NewApp(FileEntry{Name: "initial", Content: "<html></html>"}, "")
var wg sync.WaitGroup
iterations := 100
// Concurrent reads
for i := 0; i < iterations; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_ = app.GetHTMLContent()
_ = app.GetFiles()
_ = app.GetCurrentIndex()
}()
}
// Concurrent writes
for i := 0; i < iterations; i++ {
wg.Add(1)
go func() {
defer wg.Done()
app.AddFile(FileEntry{
Name: "concurrent",
Path: "/tmp/concurrent.html",
Content: "<html></html>",
})
}()
}
// Concurrent selects
for i := 0; i < iterations; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_ = app.SelectFile(0)
}()
}
wg.Wait()
// If we get here without a race condition panic, the test passes
}
func TestSortFilesByName(t *testing.T) {
files := []FileEntry{
{Name: "zebra"},
{Name: "alpha"},
{Name: "middle"},
}
sortFilesByName(files)
expected := []string{"alpha", "middle", "zebra"}
for i, name := range expected {
if files[i].Name != name {
t.Errorf("Position %d: got %q, want %q", i, files[i].Name, name)
}
}
}
func TestSortFilesByNameEmpty(t *testing.T) {
files := []FileEntry{}
sortFilesByName(files) // Should not panic
}
func TestSortFilesByNameSingle(t *testing.T) {
files := []FileEntry{{Name: "single"}}
sortFilesByName(files)
if files[0].Name != "single" {
t.Errorf("Single file sort changed name: got %q", files[0].Name)
}
}