1package main
2
3import (
4 "path/filepath"
5 "strings"
6 "testing"
7 "unicode/utf8"
8
9 "github.com/floatpane/matcha/fetcher"
10)
11
12func TestSanitizeFilenameTruncatesCJKOnUTF8Boundary(t *testing.T) {
13 name := strings.Repeat("文", 100) + ".txt"
14
15 got := sanitizeFilename(name)
16
17 if !utf8.ValidString(got) {
18 t.Fatalf("sanitizeFilename returned invalid UTF-8: %q", got)
19 }
20 if len(got) > 255 {
21 t.Fatalf("sanitizeFilename returned %d bytes, want at most 255", len(got))
22 }
23 if filepath.Ext(got) != ".txt" {
24 t.Fatalf("sanitizeFilename lost extension: got %q", got)
25 }
26}
27
28func TestSanitizeFilenameTruncatesEmojiOnUTF8Boundary(t *testing.T) {
29 name := strings.Repeat("🚀", 80) + ".log"
30
31 got := sanitizeFilename(name)
32
33 if !utf8.ValidString(got) {
34 t.Fatalf("sanitizeFilename returned invalid UTF-8: %q", got)
35 }
36 if len(got) > 255 {
37 t.Fatalf("sanitizeFilename returned %d bytes, want at most 255", len(got))
38 }
39 if filepath.Ext(got) != ".log" {
40 t.Fatalf("sanitizeFilename lost extension: got %q", got)
41 }
42}
43
44func TestParseGlobalFlagsEnablesLogPanel(t *testing.T) {
45 args, _, show := parseGlobalFlags([]string{"matcha", "--debug", "--logs", "--version"})
46 if !show {
47 t.Fatal("expected log panel flag to be enabled")
48 }
49 if got := strings.Join(args, " "); got != "matcha --version" {
50 t.Fatalf("args = %q, want %q", got, "matcha --version")
51 }
52}
53
54func TestParseGlobalFlagsDoesNotConsumeSubcommandFlags(t *testing.T) {
55 args, _, show := parseGlobalFlags([]string{"matcha", "send", "--logs"})
56 if show {
57 t.Fatal("did not expect log panel flag after subcommand to be consumed")
58 }
59 if got := strings.Join(args, " "); got != "matcha send --logs" {
60 t.Fatalf("args = %q, want %q", got, "matcha send --logs")
61 }
62}
63
64func TestUnreadBadgeCountDeduplicatesOverlappingStores(t *testing.T) {
65 email := fetcher.Email{UID: 42, AccountID: "acct-a"}
66 got := unreadBadgeCount(
67 map[string][]fetcher.Email{
68 "acct-a": {email},
69 },
70 map[string][]fetcher.Email{
71 folderInbox: {email},
72 },
73 )
74
75 if got != 1 {
76 t.Fatalf("unreadBadgeCount() = %d, want 1", got)
77 }
78}