-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
353 lines (287 loc) · 9.2 KB
/
Copy pathapp_test.go
File metadata and controls
353 lines (287 loc) · 9.2 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
349
350
351
352
353
package main
import (
"os"
"path/filepath"
"testing"
)
func TestReadDirEntries(t *testing.T) {
app := NewApp()
t.Run("normal directory", func(t *testing.T) {
dir := t.TempDir()
createFile(t, dir, "alpha.md", "# Alpha")
createFile(t, dir, "beta.md", "# Beta")
createFile(t, dir, ".hidden.md", "# Hidden")
createFile(t, dir, "readme.txt", "readme")
createDir(t, dir, "subdir")
createDir(t, dir, ".hiddenDir")
entries := app.ReadDirEntries(dir)
if len(entries) != 3 {
t.Fatalf("expected 3 entries, got %d", len(entries))
}
// directories first, then files sorted by name
assertEntry(t, entries[0], "subdir", filepath.Join(dir, "subdir"), true)
assertEntry(t, entries[1], "alpha.md", filepath.Join(dir, "alpha.md"), false)
assertEntry(t, entries[2], "beta.md", filepath.Join(dir, "beta.md"), false)
})
t.Run("empty directory", func(t *testing.T) {
dir := t.TempDir()
entries := app.ReadDirEntries(dir)
if entries != nil {
t.Fatalf("expected nil for empty directory, got %v", entries)
}
})
t.Run("non-existent directory", func(t *testing.T) {
entries := app.ReadDirEntries("/non/existent/path")
if entries != nil {
t.Fatalf("expected nil for non-existent directory, got %v", entries)
}
})
}
func TestReadFileContent(t *testing.T) {
app := NewApp()
t.Run("normal file", func(t *testing.T) {
dir := t.TempDir()
fp := filepath.Join(dir, "test.md")
content := "# Hello World"
createFile(t, dir, "test.md", content)
result := app.ReadFileContent(fp)
if result != content {
t.Fatalf("expected %q, got %q", content, result)
}
})
t.Run("non-existent file", func(t *testing.T) {
result := app.ReadFileContent("/non/existent/file.md")
if result != "" {
t.Fatalf("expected empty string for non-existent file, got %q", result)
}
})
}
func TestSaveFileContent(t *testing.T) {
app := NewApp()
t.Run("normal save", func(t *testing.T) {
dir := t.TempDir()
fp := filepath.Join(dir, "test.md")
content := "# Saved Content"
result := app.SaveFileContent(fp, content)
if result != "" {
t.Fatalf("expected empty string on success, got %q", result)
}
data, err := os.ReadFile(fp)
if err != nil {
t.Fatalf("failed to read saved file: %v", err)
}
if string(data) != content {
t.Fatalf("expected file content %q, got %q", content, string(data))
}
})
t.Run("save to non-existent directory", func(t *testing.T) {
dir := t.TempDir()
fp := filepath.Join(dir, "nested", "deep", "test.md")
content := "# Nested Save"
result := app.SaveFileContent(fp, content)
if result != "" {
t.Fatalf("expected empty string on success, got %q", result)
}
data, err := os.ReadFile(fp)
if err != nil {
t.Fatalf("failed to read saved file: %v", err)
}
if string(data) != content {
t.Fatalf("expected file content %q, got %q", content, string(data))
}
})
t.Run("save to read-only directory", func(t *testing.T) {
dir := t.TempDir()
readOnlyDir := filepath.Join(dir, "readonly")
if err := os.Mkdir(readOnlyDir, 0555); err != nil {
t.Fatalf("failed to create read-only dir: %v", err)
}
fp := filepath.Join(readOnlyDir, "test.md")
result := app.SaveFileContent(fp, "content")
if result == "" {
t.Fatal("expected error when saving to read-only directory, got success")
}
})
}
func TestFileExists(t *testing.T) {
app := NewApp()
t.Run("existing file", func(t *testing.T) {
dir := t.TempDir()
fp := filepath.Join(dir, "exists.md")
createFile(t, dir, "exists.md", "content")
if !app.FileExists(fp) {
t.Fatal("expected FileExists to return true for existing file")
}
})
t.Run("existing directory", func(t *testing.T) {
dir := t.TempDir()
subDir := filepath.Join(dir, "subdir")
createDir(t, dir, "subdir")
if !app.FileExists(subDir) {
t.Fatal("expected FileExists to return true for existing directory")
}
})
t.Run("non-existent path", func(t *testing.T) {
if app.FileExists("/non/existent/file.md") {
t.Fatal("expected FileExists to return false for non-existent path")
}
})
}
func TestDeleteFile(t *testing.T) {
app := NewApp()
// Override HOME to avoid polluting the real trash.
home := t.TempDir()
t.Setenv("HOME", home)
t.Run("delete existing file", func(t *testing.T) {
dir := t.TempDir()
fp := filepath.Join(dir, "delete-me.md")
createFile(t, dir, "delete-me.md", "bye")
err := app.DeleteFile(fp)
if err != nil {
t.Fatalf("DeleteFile failed: %v", err)
}
if _, err := os.Stat(fp); !os.IsNotExist(err) {
t.Fatal("file should not exist after deletion")
}
})
t.Run("delete existing directory", func(t *testing.T) {
dir := t.TempDir()
subDir := filepath.Join(dir, "delete-dir")
createDir(t, dir, "delete-dir")
createFile(t, subDir, "inner.md", "content")
err := app.DeleteFile(subDir)
if err != nil {
t.Fatalf("DeleteFile failed: %v", err)
}
if _, err := os.Stat(subDir); !os.IsNotExist(err) {
t.Fatal("directory should not exist after deletion")
}
})
t.Run("return error for non-existent path", func(t *testing.T) {
err := app.DeleteFile("/non/existent/path")
if err == nil {
t.Fatal("expected error when deleting non-existent path")
}
})
}
func assertEntry(t *testing.T, entry FileEntry, expectedName, expectedPath string, expectedIsDir bool) {
t.Helper()
if entry.Name != expectedName {
t.Errorf("expected name %q, got %q", expectedName, entry.Name)
}
if entry.Path != expectedPath {
t.Errorf("expected path %q, got %q", expectedPath, entry.Path)
}
if entry.IsDir != expectedIsDir {
t.Errorf("expected IsDir %v, got %v", expectedIsDir, entry.IsDir)
}
}
func createFile(t *testing.T, dir, name, content string) {
t.Helper()
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0644); err != nil {
t.Fatalf("failed to create file %s: %v", name, err)
}
}
func createDir(t *testing.T, parent, name string) {
t.Helper()
if err := os.Mkdir(filepath.Join(parent, name), 0755); err != nil {
t.Fatalf("failed to create directory %s: %v", name, err)
}
}
func TestConfirmDelete(t *testing.T) {
app := NewApp()
// ConfirmDelete calls runtime.MessageDialog which requires a valid Wails
// context. In unit tests the context is nil, so the call panics.
// We verify the method exists and has the expected signature by checking
// that it is a bound method on *App.
_ = app.ConfirmDelete
}
func TestClipboardSetText(t *testing.T) {
app := NewApp()
// ClipboardSetText calls runtime.ClipboardSetText which requires a valid
// Wails context. In unit tests the context is nil, so the call panics.
// We verify the method exists and has the expected signature.
_ = app.ClipboardSetText
}
func TestClipboardGetText(t *testing.T) {
app := NewApp()
// ClipboardGetText calls runtime.ClipboardGetText which requires a valid
// Wails context. In unit tests the context is nil, so the call panics.
// We verify the method exists and has the expected signature.
_ = app.ClipboardGetText
}
func TestIsMarkdownFilePath(t *testing.T) {
cases := []struct {
name string
path string
expected bool
}{
{"lowercase md", "/tmp/notes.md", true},
{"uppercase MD", "/tmp/notes.MD", true},
{"markdown extension", "/tmp/notes.markdown", true},
{"mixed case Markdown", "/tmp/notes.Markdown", true},
{"nested path", "/Users/J/docs/sub/note.md", true},
{"txt file", "/tmp/notes.txt", false},
{"no extension", "/tmp/README", false},
{"empty path", "", false},
{"directory-like", "/tmp/foo.md/", false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := isMarkdownFilePath(c.path)
if got != c.expected {
t.Fatalf("isMarkdownFilePath(%q) = %v, want %v", c.path, got, c.expected)
}
})
}
}
func TestGetPendingOpenFile(t *testing.T) {
t.Run("returns empty when nothing pending and marks frontend ready", func(t *testing.T) {
app := NewApp()
if got := app.GetPendingOpenFile(); got != "" {
t.Fatalf("expected empty pending path, got %q", got)
}
app.mu.RLock()
ready := app.frontendReady
app.mu.RUnlock()
if !ready {
t.Fatalf("expected frontendReady=true after GetPendingOpenFile call")
}
})
t.Run("buffers markdown path arrived before frontend ready", func(t *testing.T) {
dir := t.TempDir()
fp := filepath.Join(dir, "doc.md")
if err := os.WriteFile(fp, []byte("# hi"), 0o644); err != nil {
t.Fatalf("write temp file: %v", err)
}
app := NewApp()
app.HandleOpenFile(fp)
got := app.GetPendingOpenFile()
if got != fp {
t.Fatalf("expected pending path %q, got %q", fp, got)
}
// Subsequent reads should be empty since the buffer is consumed.
if got := app.GetPendingOpenFile(); got != "" {
t.Fatalf("expected empty after consume, got %q", got)
}
})
t.Run("ignores non-markdown extensions", func(t *testing.T) {
dir := t.TempDir()
fp := filepath.Join(dir, "doc.txt")
if err := os.WriteFile(fp, []byte("plain"), 0o644); err != nil {
t.Fatalf("write temp file: %v", err)
}
app := NewApp()
app.HandleOpenFile(fp)
if got := app.GetPendingOpenFile(); got != "" {
t.Fatalf("expected non-markdown path to be ignored, got %q", got)
}
})
t.Run("ignores missing files", func(t *testing.T) {
app := NewApp()
app.HandleOpenFile("/non/existent/file.md")
if got := app.GetPendingOpenFile(); got != "" {
t.Fatalf("expected missing path to be ignored, got %q", got)
}
})
}