|
| 1 | +package mail |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestIsZipFile(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + filename string |
| 13 | + mimeType string |
| 14 | + expected bool |
| 15 | + }{ |
| 16 | + {"zip extension lowercase", "archive.zip", "", true}, |
| 17 | + {"zip extension uppercase", "ARCHIVE.ZIP", "", true}, |
| 18 | + {"zip extension mixed case", "Archive.Zip", "", true}, |
| 19 | + {"application/zip mime type", "archive", "application/zip", true}, |
| 20 | + {"application/x-zip-compressed mime type", "archive", "application/x-zip-compressed", true}, |
| 21 | + {"pdf file", "document.pdf", "application/pdf", false}, |
| 22 | + {"txt file", "readme.txt", "text/plain", false}, |
| 23 | + {"no extension with wrong mime", "archive", "application/octet-stream", false}, |
| 24 | + {"zip extension with different mime", "archive.zip", "application/octet-stream", true}, |
| 25 | + {"empty filename with zip mime", "", "application/zip", true}, |
| 26 | + } |
| 27 | + |
| 28 | + for _, tt := range tests { |
| 29 | + t.Run(tt.name, func(t *testing.T) { |
| 30 | + result := isZipFile(tt.filename, tt.mimeType) |
| 31 | + assert.Equal(t, tt.expected, result) |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestFormatSize(t *testing.T) { |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + bytes int64 |
| 40 | + expected string |
| 41 | + }{ |
| 42 | + {"zero bytes", 0, "0 B"}, |
| 43 | + {"small bytes", 500, "500 B"}, |
| 44 | + {"exactly 1KB", 1024, "1.0 KB"}, |
| 45 | + {"1.5 KB", 1536, "1.5 KB"}, |
| 46 | + {"exactly 1MB", 1024 * 1024, "1.0 MB"}, |
| 47 | + {"2.5 MB", 2621440, "2.5 MB"}, |
| 48 | + {"exactly 1GB", 1024 * 1024 * 1024, "1.0 GB"}, |
| 49 | + {"large file", 5368709120, "5.0 GB"}, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tt := range tests { |
| 53 | + t.Run(tt.name, func(t *testing.T) { |
| 54 | + result := formatSize(tt.bytes) |
| 55 | + assert.Equal(t, tt.expected, result) |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestAttachmentsCommand(t *testing.T) { |
| 61 | + cmd := newAttachmentsCommand() |
| 62 | + |
| 63 | + t.Run("has correct use", func(t *testing.T) { |
| 64 | + assert.Equal(t, "attachments", cmd.Use) |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("has subcommands", func(t *testing.T) { |
| 68 | + subcommands := cmd.Commands() |
| 69 | + assert.GreaterOrEqual(t, len(subcommands), 2) |
| 70 | + |
| 71 | + var names []string |
| 72 | + for _, cmd := range subcommands { |
| 73 | + names = append(names, cmd.Name()) |
| 74 | + } |
| 75 | + assert.Contains(t, names, "list") |
| 76 | + assert.Contains(t, names, "download") |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func TestListAttachmentsCommand(t *testing.T) { |
| 81 | + cmd := newListAttachmentsCommand() |
| 82 | + |
| 83 | + t.Run("has correct use", func(t *testing.T) { |
| 84 | + assert.Equal(t, "list <message-id>", cmd.Use) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("requires exactly one argument", func(t *testing.T) { |
| 88 | + err := cmd.Args(cmd, []string{}) |
| 89 | + assert.Error(t, err) |
| 90 | + |
| 91 | + err = cmd.Args(cmd, []string{"msg123"}) |
| 92 | + assert.NoError(t, err) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("has json flag", func(t *testing.T) { |
| 96 | + flag := cmd.Flags().Lookup("json") |
| 97 | + assert.NotNil(t, flag) |
| 98 | + assert.Equal(t, "j", flag.Shorthand) |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +func TestDownloadAttachmentsCommand(t *testing.T) { |
| 103 | + cmd := newDownloadAttachmentsCommand() |
| 104 | + |
| 105 | + t.Run("has correct use", func(t *testing.T) { |
| 106 | + assert.Equal(t, "download <message-id>", cmd.Use) |
| 107 | + }) |
| 108 | + |
| 109 | + t.Run("requires exactly one argument", func(t *testing.T) { |
| 110 | + err := cmd.Args(cmd, []string{}) |
| 111 | + assert.Error(t, err) |
| 112 | + |
| 113 | + err = cmd.Args(cmd, []string{"msg123"}) |
| 114 | + assert.NoError(t, err) |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("has required flags", func(t *testing.T) { |
| 118 | + flags := []struct { |
| 119 | + name string |
| 120 | + shorthand string |
| 121 | + }{ |
| 122 | + {"filename", "f"}, |
| 123 | + {"output", "o"}, |
| 124 | + {"extract", "e"}, |
| 125 | + {"all", "a"}, |
| 126 | + } |
| 127 | + |
| 128 | + for _, f := range flags { |
| 129 | + flag := cmd.Flags().Lookup(f.name) |
| 130 | + assert.NotNil(t, flag, "flag %s should exist", f.name) |
| 131 | + assert.Equal(t, f.shorthand, flag.Shorthand, "flag %s should have shorthand %s", f.name, f.shorthand) |
| 132 | + } |
| 133 | + }) |
| 134 | +} |
0 commit comments