-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclippy_test.go
More file actions
313 lines (282 loc) · 8.8 KB
/
clippy_test.go
File metadata and controls
313 lines (282 loc) · 8.8 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
package clippy
import (
"os"
"strings"
"testing"
"github.com/gabriel-vasile/mimetype"
)
func TestIsTextualMimeType(t *testing.T) {
tests := []struct {
name string
mimeType string
want bool
}{
// Text types
{"plain text", "text/plain", true},
{"HTML", "text/html", true},
{"CSS", "text/css", true},
{"JavaScript text", "text/javascript", true},
{"XML text", "text/xml", true},
{"CSV text", "text/csv", true},
// Application types that are actually text
{"JSON", "application/json", true},
{"XML app", "application/xml", true},
{"JavaScript app", "application/javascript", true},
{"YAML", "application/x-yaml", true},
{"SQL", "application/sql", true},
{"GraphQL", "application/graphql", true},
{"XHTML", "application/xhtml+xml", true},
{"JSON-LD", "application/ld+json", true},
{"Atom feed", "application/atom+xml", true},
{"RSS", "application/rss+xml", true},
// Binary types (should return false)
{"PDF", "application/pdf", false},
{"ZIP", "application/zip", false},
{"JPEG", "image/jpeg", false},
{"PNG", "image/png", false},
{"MP3", "audio/mpeg", false},
{"MP4", "video/mp4", false},
{"Binary", "application/octet-stream", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isTextualMimeType(tt.mimeType); got != tt.want {
t.Errorf("isTextualMimeType(%q) = %v, want %v", tt.mimeType, got, tt.want)
}
})
}
}
func TestMimeToUTI(t *testing.T) {
tests := []struct {
name string
mime string
want string
}{
{"HTML", "text/html", "public.html"},
{"JSON", "application/json", "public.json"},
{"XML text", "text/xml", "public.xml"},
{"XML app", "application/xml", "public.xml"},
{"Plain text", "text/plain", "public.plain-text"},
{"RTF text", "text/rtf", "public.rtf"},
{"RTF app", "application/rtf", "public.rtf"},
{"Markdown", "text/markdown", "net.daringfireball.markdown"},
{"Unknown type", "application/unknown", "application/unknown"}, // Returns as-is
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mimeToUTI(tt.mime); got != tt.want {
t.Errorf("mimeToUTI(%q) = %v, want %v", tt.mime, got, tt.want)
}
})
}
}
func TestCopyTextWithAutoDetection(t *testing.T) {
// Note: These tests check the detection logic but can't test actual clipboard operations
// without mocking the clipboard package
tests := []struct {
name string
content string
wantUTI string // Expected UTI that would be set
description string
}{
{
name: "JSON content",
content: `{"key": "value", "number": 123}`,
wantUTI: "public.json",
description: "Should detect JSON and set public.json",
},
{
name: "HTML content",
content: `<!DOCTYPE html><html><body><h1>Test</h1></body></html>`,
wantUTI: "public.html",
description: "Should detect HTML and set public.html",
},
{
name: "XML content",
content: `<?xml version="1.0"?><root><item>test</item></root>`,
wantUTI: "public.xml",
description: "Should detect XML and set public.xml",
},
{
name: "Plain text",
content: "This is just plain text without any special format.",
wantUTI: "", // Would use default CopyText
description: "Should fall back to plain text",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Detect MIME type as the function would
mtype := mimetype.Detect([]byte(tt.content))
mimeStr := mtype.String()
// Check what UTI would be selected
var expectedUTI string
switch {
case strings.HasPrefix(mimeStr, "text/html"):
expectedUTI = "public.html"
case mimeStr == "application/json":
expectedUTI = "public.json"
case strings.HasPrefix(mimeStr, "text/xml") || mimeStr == "application/xml":
expectedUTI = "public.xml"
default:
expectedUTI = ""
}
if expectedUTI != tt.wantUTI {
t.Errorf("Content detection failed for %s\nDetected MIME: %s\nExpected UTI: %s\nGot UTI: %s",
tt.name, mimeStr, tt.wantUTI, expectedUTI)
}
})
}
}
func TestCopyTextWithType(t *testing.T) {
// Test MIME to UTI conversion in CopyTextWithType
tests := []struct {
name string
typeIdentifier string
wantUTI string
}{
{"MIME type HTML", "text/html", "public.html"},
{"MIME type JSON", "application/json", "public.json"},
{"Direct UTI", "public.xml", "public.xml"},
{"Unknown MIME", "application/custom", "application/custom"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Check conversion logic
result := tt.typeIdentifier
if strings.Contains(result, "/") {
result = mimeToUTI(result)
}
if result != tt.wantUTI {
t.Errorf("Type conversion failed\nInput: %s\nExpected: %s\nGot: %s",
tt.typeIdentifier, tt.wantUTI, result)
}
})
}
}
func TestConvertImageFormat(t *testing.T) {
// Verify the function handles errors gracefully
_, err := convertImageFormat([]byte("not an image"), ".png")
if err == nil {
t.Error("Expected error for invalid image data")
}
// Test unsupported format
_, err = convertImageFormat([]byte("fake image"), ".bmp")
if err == nil {
t.Error("Expected error for unsupported format")
}
}
func TestFindAvailableFilename(t *testing.T) {
tmpDir := t.TempDir()
tests := []struct {
name string
existingFiles []string
inputPath string
want string
}{
{
name: "no conflict",
existingFiles: []string{},
inputPath: tmpDir + "/photo.png",
want: tmpDir + "/photo.png",
},
{
name: "one conflict with extension",
existingFiles: []string{"photo.png"},
inputPath: tmpDir + "/photo.png",
want: tmpDir + "/photo 2.png",
},
{
name: "two conflicts with extension",
existingFiles: []string{"photo.png", "photo 2.png"},
inputPath: tmpDir + "/photo.png",
want: tmpDir + "/photo 3.png",
},
{
name: "no conflict without extension",
existingFiles: []string{},
inputPath: tmpDir + "/README",
want: tmpDir + "/README",
},
{
name: "one conflict without extension",
existingFiles: []string{"README"},
inputPath: tmpDir + "/README",
want: tmpDir + "/README 2",
},
{
name: "multiple conflicts without extension",
existingFiles: []string{"README", "README 2", "README 3"},
inputPath: tmpDir + "/README",
want: tmpDir + "/README 4",
},
{
name: "multi-part extension",
existingFiles: []string{"archive.tar.gz"},
inputPath: tmpDir + "/archive.tar.gz",
want: tmpDir + "/archive 2.tar.gz",
},
{
name: "multi-part extension multiple conflicts",
existingFiles: []string{"backup.tar.bz2", "backup 2.tar.bz2"},
inputPath: tmpDir + "/backup.tar.bz2",
want: tmpDir + "/backup 3.tar.bz2",
},
{
name: "gaps in numbering",
existingFiles: []string{"file.txt", "file 3.txt"},
inputPath: tmpDir + "/file.txt",
want: tmpDir + "/file 2.txt",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, f := range tt.existingFiles {
if err := os.WriteFile(tmpDir+"/"+f, []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
}
got := findAvailableFilename(tt.inputPath, false)
if got != tt.want {
t.Errorf("findAvailableFilename(%q, false)\n got: %q\n want: %q", tt.inputPath, got, tt.want)
}
for _, f := range tt.existingFiles {
_ = os.Remove(tmpDir + "/" + f)
}
})
}
}
func TestFindAvailableFilenameWithForce(t *testing.T) {
tmpDir := t.TempDir()
if err := os.WriteFile(tmpDir+"/existing.txt", []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
path := tmpDir + "/existing.txt"
got := findAvailableFilename(path, true)
want := path
if got != want {
t.Errorf("findAvailableFilename(%q, true) should return original path when force=true\n got: %q\n want: %q", path, got, want)
}
}
func TestCopyFilesToDestination_Directory(t *testing.T) {
srcRoot := t.TempDir()
srcDir := srcRoot + "/src-folder"
if err := os.MkdirAll(srcDir+"/nested", 0755); err != nil {
t.Fatalf("Failed to create source dir: %v", err)
}
if err := os.WriteFile(srcDir+"/nested/file.txt", []byte("hello"), 0644); err != nil {
t.Fatalf("Failed to create source file: %v", err)
}
destRoot := t.TempDir()
// Destination is an existing directory: should copy folder into it.
if _, err := copyFilesToDestination([]string{srcDir}, destRoot, false); err != nil {
t.Fatalf("copyFilesToDestination returned error: %v", err)
}
got, err := os.ReadFile(destRoot + "/src-folder/nested/file.txt")
if err != nil {
t.Fatalf("Expected copied file, got error: %v", err)
}
if string(got) != "hello" {
t.Fatalf("Copied file content mismatch: got %q want %q", string(got), "hello")
}
}