-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
574 lines (529 loc) · 14.1 KB
/
main_test.go
File metadata and controls
574 lines (529 loc) · 14.1 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package main
import (
"bytes"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestConvertMarkdownToHTML(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "simple paragraph",
input: "Hello world",
expected: "<p>Hello world</p>",
},
{
name: "heading h2",
input: "## Heading",
expected: "<h2>Heading</h2>",
},
{
name: "heading h3",
input: "### Subheading",
expected: "<h3>Subheading</h3>",
},
{
name: "bold text",
input: "This is **bold** text",
expected: "<p>This is <strong>bold</strong> text</p>",
},
{
name: "italic text",
input: "This is *italic* text",
expected: "<p>This is <em>italic</em> text</p>",
},
{
name: "inline code",
input: "Use `code` here",
expected: "<p>Use <code>code</code> here</p>",
},
{
name: "unordered list",
input: "- Item 1\n- Item 2\n- Item 3",
expected: "<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n<li>Item 3</li>\n</ul>",
},
{
name: "ordered list",
input: "1. First\n2. Second\n3. Third",
expected: "<ol>\n<li>First</li>\n<li>Second</li>\n<li>Third</li>\n</ol>",
},
{
name: "link",
input: "Check [this link](https://example.com)",
expected: `<p>Check <a href="https://example.com">this link</a></p>`,
},
{
name: "code block",
input: "```\ncode here\n```",
expected: "<pre><code>code here\n</code></pre>",
},
{
name: "code block with language",
input: "```go\nfunc main() {}\n```",
expected: "<pre><code class=\"language-go\">func main() {}\n</code></pre>",
},
{
name: "blockquote",
input: "> This is a quote",
expected: "<blockquote>\n<p>This is a quote</p>\n</blockquote>",
},
{
name: "complex document",
input: "## Overview\n\nThis is **important**.\n\n- Item 1\n- Item 2",
expected: "<h2>Overview</h2>\n<p>This is <strong>important</strong>.</p>\n<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "emoji preservation",
input: "Status: ✅ Complete 🚀",
expected: "<p>Status: ✅ Complete 🚀</p>",
},
{
name: "simple table",
input: "| Name | Value |\n|------|-------|\n| Foo | Bar |\n| Baz | Qux |",
expected: `<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Foo</td>
<td>Bar</td>
</tr>
<tr>
<td>Baz</td>
<td>Qux</td>
</tr>
</tbody>
</table>`,
},
{
name: "table with alignment",
input: "| Left | Center | Right |\n|:-----|:------:|------:|\n| A | B | C |",
expected: `<table>
<thead>
<tr>
<th style="text-align:left">Left</th>
<th style="text-align:center">Center</th>
<th style="text-align:right">Right</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">A</td>
<td style="text-align:center">B</td>
<td style="text-align:right">C</td>
</tr>
</tbody>
</table>`,
},
{
name: "table with inline formatting",
input: "| **Bold** | *Italic* | `Code` |\n|----------|----------|--------|\n| foo | bar | baz |",
expected: `<table>
<thead>
<tr>
<th><strong>Bold</strong></th>
<th><em>Italic</em></th>
<th><code>Code</code></th>
</tr>
</thead>
<tbody>
<tr>
<td>foo</td>
<td>bar</td>
<td>baz</td>
</tr>
</tbody>
</table>`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := convertMarkdownToHTML(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("\nInput: %q\nExpected: %q\nGot: %q", tt.input, tt.expected, result)
}
})
}
}
func TestReadAndConvertFile(t *testing.T) {
// Create temp directory for test files
tmpDir := t.TempDir()
tests := []struct {
name string
filename string
content string
expected string
expectError bool
}{
{
name: "markdown file (.md)",
filename: "test.md",
content: "## Hello\n\nWorld",
expected: "<h2>Hello</h2>\n<p>World</p>",
},
{
name: "html file (.html) - passthrough",
filename: "test.html",
content: "<h2>Already HTML</h2>",
expected: "<h2>Already HTML</h2>",
},
{
name: "htm file (.htm) - passthrough",
filename: "test.htm",
content: "<p>Also HTML</p>",
expected: "<p>Also HTML</p>",
},
{
name: "no extension - assume markdown",
filename: "notes",
content: "**Bold** text",
expected: "<p><strong>Bold</strong> text</p>",
},
{
name: "txt extension - convert as markdown",
filename: "notes.txt",
content: "## Heading",
expected: "<h2>Heading</h2>",
},
{
name: "non-existent file",
filename: "does-not-exist.md",
content: "",
expected: "",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var filePath string
if !tt.expectError || tt.filename != "does-not-exist.md" {
// Create test file
filePath = filepath.Join(tmpDir, tt.filename)
if err := os.WriteFile(filePath, []byte(tt.content), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
} else {
filePath = filepath.Join(tmpDir, tt.filename)
}
result, err := readAndConvertFile(filePath)
if tt.expectError {
if err == nil {
t.Errorf("expected error but got none")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("\nFilename: %s\nExpected: %q\nGot: %q", tt.filename, tt.expected, result)
}
})
}
}
func TestProcessArgs(t *testing.T) {
tests := []struct {
name string
input []string
checkFn func([]string) error
expectError bool
}{
{
name: "passthrough - no conversion needed",
input: []string{"card", "list", "--board", "123"},
checkFn: func(result []string) error {
expected := []string{"card", "list", "--board", "123"}
if len(result) != len(expected) {
return nil // length mismatch will be caught
}
for i, v := range expected {
if result[i] != v {
return nil
}
}
return nil
},
},
{
name: "convert --description flag",
input: []string{"card", "create", "--title", "Test", "--description", "## Hello"},
checkFn: func(result []string) error {
// Check that --description value was converted
for i, v := range result {
if v == "--description" && i+1 < len(result) {
if !strings.Contains(result[i+1], "<h2>") {
t.Errorf("expected HTML conversion, got: %s", result[i+1])
}
return nil
}
}
t.Error("--description flag not found in result")
return nil
},
},
{
name: "convert --body flag",
input: []string{"comment", "create", "--card", "42", "--body", "**Bold** text"},
checkFn: func(result []string) error {
// Check that --body value was converted
for i, v := range result {
if v == "--body" && i+1 < len(result) {
if !strings.Contains(result[i+1], "<strong>") {
t.Errorf("expected HTML conversion, got: %s", result[i+1])
}
return nil
}
}
t.Error("--body flag not found in result")
return nil
},
},
{
name: "missing value for --description",
input: []string{"card", "create", "--description"},
expectError: true,
},
{
name: "missing value for --body",
input: []string{"comment", "create", "--body"},
expectError: true,
},
{
name: "multiple flags in one command",
input: []string{"card", "create", "--title", "Test", "--description", "## Hello", "--board", "123"},
checkFn: func(result []string) error {
foundTitle := false
foundBoard := false
foundDescription := false
for i, v := range result {
if v == "--title" && i+1 < len(result) && result[i+1] == "Test" {
foundTitle = true
}
if v == "--board" && i+1 < len(result) && result[i+1] == "123" {
foundBoard = true
}
if v == "--description" && i+1 < len(result) {
if strings.Contains(result[i+1], "<h2>") {
foundDescription = true
}
}
}
if !foundTitle {
t.Error("--title flag not preserved correctly")
}
if !foundBoard {
t.Error("--board flag not preserved correctly")
}
if !foundDescription {
t.Error("--description not converted correctly")
}
return nil
},
},
{
name: "empty args",
input: []string{},
checkFn: func(result []string) error {
if len(result) != 0 {
t.Errorf("expected empty result, got %v", result)
}
return nil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := processArgs(tt.input)
if tt.expectError {
if err == nil {
t.Errorf("expected error but got none")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.checkFn != nil {
tt.checkFn(result)
}
})
}
}
func TestProcessArgsWithFiles(t *testing.T) {
tmpDir := t.TempDir()
// Create test markdown file
mdFile := filepath.Join(tmpDir, "test.md")
if err := os.WriteFile(mdFile, []byte("## From File\n\n- Item 1\n- Item 2"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
// Create test HTML file (should passthrough)
htmlFile := filepath.Join(tmpDir, "test.html")
if err := os.WriteFile(htmlFile, []byte("<h2>Already HTML</h2>"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
t.Run("convert --description_file with .md", func(t *testing.T) {
result, err := processArgs([]string{"card", "create", "--title", "Test", "--description_file", mdFile})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Find the converted file path
for i, v := range result {
if v == "--description_file" && i+1 < len(result) {
tmpPath := result[i+1]
content, err := os.ReadFile(tmpPath)
if err != nil {
t.Fatalf("failed to read temp file: %v", err)
}
if !strings.Contains(string(content), "<h2>From File</h2>") {
t.Errorf("expected converted HTML, got: %s", content)
}
if !strings.Contains(string(content), "<li>Item 1</li>") {
t.Errorf("expected list conversion, got: %s", content)
}
return
}
}
t.Error("--description_file flag not found in result")
})
t.Run("passthrough --description_file with .html", func(t *testing.T) {
result, err := processArgs([]string{"card", "create", "--title", "Test", "--description_file", htmlFile})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Find the file path - should still create temp file but with passthrough content
for i, v := range result {
if v == "--description_file" && i+1 < len(result) {
tmpPath := result[i+1]
content, err := os.ReadFile(tmpPath)
if err != nil {
t.Fatalf("failed to read temp file: %v", err)
}
if string(content) != "<h2>Already HTML</h2>" {
t.Errorf("expected passthrough, got: %s", content)
}
return
}
}
t.Error("--description_file flag not found in result")
})
t.Run("error on non-existent file", func(t *testing.T) {
_, err := processArgs([]string{"card", "create", "--description_file", "/nonexistent/file.md"})
if err == nil {
t.Error("expected error for non-existent file")
}
})
}
func TestFindFizzySkipsSelf(t *testing.T) {
tmpDir := t.TempDir()
fakeFizzy := filepath.Join(tmpDir, "fizzy")
if runtime.GOOS == "windows" {
ext := ".exe"
if pathext := os.Getenv("PATHEXT"); pathext != "" {
parts := strings.Split(pathext, ";")
if len(parts) > 0 && strings.TrimSpace(parts[0]) != "" {
ext = strings.TrimSpace(parts[0])
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
}
}
fakeFizzy += ext
}
if err := os.WriteFile(fakeFizzy, []byte("#!/bin/sh\necho fake\n"), 0755); err != nil {
t.Fatalf("failed to create fake fizzy: %v", err)
}
t.Setenv("PATH", tmpDir)
result := findFizzy()
if result == "" {
t.Fatal("expected fizzy binary to be found in PATH")
}
self, _ := os.Executable()
if self != "" {
selfReal, _ := filepath.EvalSymlinks(self)
resultReal, _ := filepath.EvalSymlinks(result)
if selfReal == resultReal {
t.Errorf("findFizzy returned self: %s", result)
}
}
if filepath.Clean(result) != filepath.Clean(fakeFizzy) {
t.Errorf("expected %s, got %s", fakeFizzy, result)
}
}
func TestFindFizzyRespectsEnvVar(t *testing.T) {
// Create a fake fizzy binary
tmpDir := t.TempDir()
fakeFizzy := filepath.Join(tmpDir, "fizzy")
if err := os.WriteFile(fakeFizzy, []byte("#!/bin/sh\necho fake"), 0755); err != nil {
t.Fatalf("failed to create fake fizzy: %v", err)
}
t.Setenv("FIZZY_PATH", fakeFizzy)
result := findFizzy()
if result != fakeFizzy {
t.Errorf("expected %s, got %s", fakeFizzy, result)
}
}
func TestIsShellWrapperForFizzyMd(t *testing.T) {
tmpDir := t.TempDir()
// A wrapper script that references fizzy-md
wrapper := filepath.Join(tmpDir, "wrapper")
os.WriteFile(wrapper, []byte("#!/bin/sh\nexec /opt/homebrew/bin/fizzy-md \"$@\"\n"), 0755)
if !isShellWrapperForFizzyMd(wrapper) {
t.Error("expected wrapper to be detected")
}
// A real binary (large file, not a wrapper)
notWrapper := filepath.Join(tmpDir, "real")
largePayload := bytes.Repeat([]byte("a"), 5000)
os.WriteFile(notWrapper, largePayload, 0755)
if isShellWrapperForFizzyMd(notWrapper) {
t.Error("expected non-wrapper to not be detected")
}
}
// Benchmark for performance requirement (<100ms)
func BenchmarkConvertMarkdownToHTML(b *testing.B) {
input := `## Overview
This is a **complex** document with multiple elements.
### Features
- Feature 1 with ` + "`code`" + `
- Feature 2 with **bold**
- Feature 3 with *italic*
### Code Example
` + "```go\nfunc main() {\n fmt.Println(\"Hello\")\n}\n```" + `
### Conclusion
> This is a blockquote for emphasis.
Visit [our site](https://example.com) for more info.
`
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = convertMarkdownToHTML(input)
}
}
func BenchmarkProcessArgs(b *testing.B) {
args := []string{
"card", "create",
"--title", "Test Card",
"--description", "## Hello\n\nThis is **bold** and *italic*.\n\n- Item 1\n- Item 2",
"--board", "123",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = processArgs(args)
}
}