package app import ( "strings" "testing" ) func TestValidateKolDisplayName(t *testing.T) { cases := []struct { name string input string wantOK bool wantVal string }{ {"trim spaces", " alice ", true, "alice"}, {"keep unicode", "张小明", true, "张小明"}, {"empty rejected", " ", false, ""}, {"40 chars ok", strings.Repeat("a", 40), true, strings.Repeat("a", 40)}, {"41 chars rejected", strings.Repeat("a", 41), false, ""}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { got, err := validateKolDisplayName(tc.input) if tc.wantOK && err != nil { t.Fatalf("unexpected error: %v", err) } if !tc.wantOK && err == nil { t.Fatalf("expected error, got nil (value=%q)", got) } if tc.wantOK && got != tc.wantVal { t.Fatalf("got %q want %q", got, tc.wantVal) } }) } } func TestValidateKolBio(t *testing.T) { cases := []struct { name string input *string wantOK bool }{ {"nil ok", nil, true}, {"empty trimmed to nil", strPtr(" "), true}, {"500 chars ok", strPtr(strings.Repeat("b", 500)), true}, {"501 chars rejected", strPtr(strings.Repeat("b", 501)), false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { _, err := validateKolBio(tc.input) if tc.wantOK && err != nil { t.Fatalf("unexpected error: %v", err) } if !tc.wantOK && err == nil { t.Fatalf("expected error, got nil") } }) } } func TestValidateKolAvatarURL(t *testing.T) { cases := []struct { name string input *string wantOK bool }{ {"nil ok", nil, true}, {"empty trimmed to nil", strPtr(" "), true}, {"asset path ok", strPtr("/api/public/assets/abc.def"), true}, {"external url rejected", strPtr("https://evil.example.com/a.jpg"), false}, {"javascript scheme rejected", strPtr("javascript:alert(1)"), false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { _, err := validateKolAvatarURL(tc.input) if tc.wantOK && err != nil { t.Fatalf("unexpected error: %v", err) } if !tc.wantOK && err == nil { t.Fatalf("expected error, got nil") } }) } } func strPtr(s string) *string { return &s }