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