package support import ( "errors" "strings" "testing" "unicode/utf8" ) func TestNormalizeCSATScore(t *testing.T) { t.Parallel() for _, tc := range []struct { score int want error }{ {0, ErrInvalidCSATScore}, {1, nil}, {5, nil}, {6, ErrInvalidCSATScore}, {-1, ErrInvalidCSATScore}, } { got, err := normalizeCSATScore(tc.score) if !errors.Is(err, tc.want) { t.Fatalf("score=%d err=%v want %v", tc.score, err, tc.want) } if tc.want == nil && got != tc.score { t.Fatalf("score=%d got %d", tc.score, got) } if tc.want != nil { if msg, ok := ClientError(err); !ok || msg == "" { t.Fatalf("ClientError(%v) = %q ok=%v", err, msg, ok) } } } } func TestNormalizeCSATComment(t *testing.T) { t.Parallel() got, err := normalizeCSATComment(" hello\x00 ") if err != nil { t.Fatal(err) } if got != "hello" { t.Fatalf("got %q", got) } long := strings.Repeat("รค", maxCSATCommentLen+50) got, err = normalizeCSATComment(long) if err != nil { t.Fatal(err) } if utf8.RuneCountInString(got) != maxCSATCommentLen { t.Fatalf("len=%d want %d", utf8.RuneCountInString(got), maxCSATCommentLen) } } func TestClientErrorCSATSentinels(t *testing.T) { t.Parallel() for _, err := range []error{ErrAlreadyRated, ErrCSATNotEligible, ErrInvalidCSATScore} { msg, ok := ClientError(err) if !ok || msg == "" { t.Fatalf("ClientError(%v) = %q ok=%v", err, msg, ok) } } }