package translate import ( "net/http" "reflect" "strings" "testing" "github.com/tidwall/gjson" ) func TestTotalTextLength(t *testing.T) { tests := []struct { name string texts []string wantTotal int wantOK bool }{ {"no texts", nil, 0, false}, {"empty array", []string{}, 0, false}, {"empty string", []string{""}, 0, false}, {"empty element in batch", []string{"Hello", ""}, 0, false}, {"single text", []string{"Hello"}, 5, true}, {"batch total", []string{"Hello", "Good morning"}, 17, true}, {"counts UTF-16 units, not bytes", []string{"你好"}, 2, true}, {"astral character costs two units", []string{"😀"}, 2, true}, {"combining mark is two units, not one grapheme", []string{"e\u0301"}, 2, true}, {"invalid UTF-8 counts one unit per byte", []string{"\xf0\x90\x80"}, 3, true}, {"at the limit", []string{strings.Repeat("a", maxFreeTextLength)}, maxFreeTextLength, true}, {"astral at the limit", []string{strings.Repeat("😀", maxFreeTextLength/2)}, maxFreeTextLength, true}, {"astral past the limit", []string{strings.Repeat("😀", maxFreeTextLength/2), "😀"}, maxFreeTextLength + 2, true}, {"astral batch sums units, not runes", []string{strings.Repeat("😀", 400), strings.Repeat("😀", 400)}, 1600, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { total, ok := totalTextLength(tt.texts) if ok != tt.wantOK || total != tt.wantTotal { t.Fatalf("totalTextLength(%q) = (%d, %v), want (%d, %v)", tt.texts, total, ok, tt.wantTotal, tt.wantOK) } }) } } func TestTranslationsFromResult(t *testing.T) { tests := []struct { name string body string count int want []string wantErr bool }{ { name: "single", body: `{"translations":[{"text":"你好,世界!","detected_source_language":"en"}]}`, count: 1, want: []string{"你好,世界!"}, }, { name: "batch keeps order", body: `{"translations":[{"text":"你好,世界!"},{"text":"早上好"}]}`, count: 2, want: []string{"你好,世界!", "早上好"}, }, {name: "no translations at all", body: `{}`, count: 1, wantErr: true}, {name: "fewer than requested", body: `{"translations":[{"text":"你好"}]}`, count: 2, wantErr: true}, {name: "more than requested", body: `{"translations":[{"text":"你好"},{"text":"早上好"}]}`, count: 1, wantErr: true}, {name: "empty translation", body: `{"translations":[{"text":"你好"},{"text":""}]}`, count: 2, wantErr: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := translationsFromResult(gjson.Parse(tt.body), tt.count) if (err != nil) != tt.wantErr { t.Fatalf("translationsFromResult() error = %v, wantErr %v", err, tt.wantErr) } if tt.wantErr { return } if !reflect.DeepEqual(got, tt.want) { t.Fatalf("translationsFromResult() = %q, want %q", got, tt.want) } }) } } // Rejected requests must fail before anything is sent upstream: every case // below returns on validation alone, so the test does not touch DeepL. func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) { tests := []struct { name string texts []string code int }{ {"no texts", nil, http.StatusNotFound}, {"empty array", []string{}, http.StatusNotFound}, {"empty element in batch", []string{"Hello", ""}, http.StatusNotFound}, {"over the limit", []string{strings.Repeat("a", maxFreeTextLength+1)}, http.StatusRequestEntityTooLarge}, {"batch over the limit", []string{strings.Repeat("a", maxFreeTextLength), "b"}, http.StatusRequestEntityTooLarge}, // 751 runes in the batch, but 1502 UTF-16 units: the case a rune count // let through to be rejected by oneshot instead. {"astral batch over the unit limit", []string{strings.Repeat("😀", maxFreeTextLength/2+1)}, http.StatusRequestEntityTooLarge}, {"astral batch split over the unit limit", []string{strings.Repeat("😀", 400), strings.Repeat("😀", 400)}, http.StatusRequestEntityTooLarge}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result, err := TranslateByDLX("", "ZH", tt.texts, "", "", "") if err != nil { t.Fatalf("TranslateByDLX() error = %v", err) } if result.Code != tt.code { t.Fatalf("TranslateByDLX() code = %d, want %d", result.Code, tt.code) } }) } }