mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
fix(translate): answer a request whose texts are all empty with 200 as well
Every text is answered now, blank or not, so the all-empty case needs no rule of its own: "" and [""] are texts with nothing to translate in them, and they are answered with themselves like any other blank text. A missing or null `text` decodes to the one empty string and is answered the same way. The 404 is left for a request that names no text at all: `"text": []`, the one shape there is nothing to answer with. Nothing else errors on emptiness. Refs #244.
This commit is contained in:
+40
-38
@@ -15,34 +15,33 @@ func TestTextsToTranslate(t *testing.T) {
|
||||
texts []string
|
||||
wantAt []int
|
||||
wantTotal int
|
||||
wantText bool
|
||||
}{
|
||||
{"no texts", nil, nil, 0, false},
|
||||
{"empty array", []string{}, nil, 0, false},
|
||||
{"empty string", []string{""}, nil, 0, false},
|
||||
{"every text empty", []string{"", ""}, nil, 0, false},
|
||||
{"whitespace only", []string{" \t "}, nil, 0, true},
|
||||
{"empty and blank", []string{"", " "}, nil, 0, true},
|
||||
{"single text", []string{"Hello"}, []int{0}, 5, true},
|
||||
{"empty element in batch", []string{"Hello", ""}, []int{0}, 5, true},
|
||||
{"leading empty element in batch", []string{"", "Hello"}, []int{1}, 5, true},
|
||||
{"blank elements around a batch", []string{"", "Hello", " ", "Good morning", "\n"}, []int{1, 3}, 17, true},
|
||||
{"batch total", []string{"Hello", "Good morning"}, []int{0, 1}, 17, true},
|
||||
{"counts UTF-16 units, not bytes", []string{"你好"}, []int{0}, 2, true},
|
||||
{"astral character costs two units", []string{"😀"}, []int{0}, 2, true},
|
||||
{"combining mark is two units, not one grapheme", []string{"e\u0301"}, []int{0}, 2, true},
|
||||
{"invalid UTF-8 counts one unit per byte", []string{"\xf0\x90\x80"}, []int{0}, 3, true},
|
||||
{"at the limit", []string{strings.Repeat("a", maxFreeTextLength)}, []int{0}, maxFreeTextLength, true},
|
||||
{"astral at the limit", []string{strings.Repeat("😀", maxFreeTextLength/2)}, []int{0}, maxFreeTextLength, true},
|
||||
{"astral past the limit", []string{strings.Repeat("😀", maxFreeTextLength/2), "😀"}, []int{0, 1}, maxFreeTextLength + 2, true},
|
||||
{"astral batch sums units, not runes", []string{strings.Repeat("😀", 400), strings.Repeat("😀", 400)}, []int{0, 1}, 1600, true},
|
||||
{"blank texts are not charged", []string{" ", "Hello"}, []int{1}, 5, true},
|
||||
{"no texts", nil, nil, 0},
|
||||
{"empty array", []string{}, nil, 0},
|
||||
{"empty string", []string{""}, nil, 0},
|
||||
{"every text empty", []string{"", ""}, nil, 0},
|
||||
{"whitespace only", []string{" \t "}, nil, 0},
|
||||
{"empty and blank", []string{"", " "}, nil, 0},
|
||||
{"single text", []string{"Hello"}, []int{0}, 5},
|
||||
{"empty element in batch", []string{"Hello", ""}, []int{0}, 5},
|
||||
{"leading empty element in batch", []string{"", "Hello"}, []int{1}, 5},
|
||||
{"blank elements around a batch", []string{"", "Hello", " ", "Good morning", "\n"}, []int{1, 3}, 17},
|
||||
{"batch total", []string{"Hello", "Good morning"}, []int{0, 1}, 17},
|
||||
{"counts UTF-16 units, not bytes", []string{"你好"}, []int{0}, 2},
|
||||
{"astral character costs two units", []string{"😀"}, []int{0}, 2},
|
||||
{"combining mark is two units, not one grapheme", []string{"e\u0301"}, []int{0}, 2},
|
||||
{"invalid UTF-8 counts one unit per byte", []string{"\xf0\x90\x80"}, []int{0}, 3},
|
||||
{"at the limit", []string{strings.Repeat("a", maxFreeTextLength)}, []int{0}, maxFreeTextLength},
|
||||
{"astral at the limit", []string{strings.Repeat("😀", maxFreeTextLength/2)}, []int{0}, maxFreeTextLength},
|
||||
{"astral past the limit", []string{strings.Repeat("😀", maxFreeTextLength/2), "😀"}, []int{0, 1}, maxFreeTextLength + 2},
|
||||
{"astral batch sums units, not runes", []string{strings.Repeat("😀", 400), strings.Repeat("😀", 400)}, []int{0, 1}, 1600},
|
||||
{"blank texts are not charged", []string{" ", "Hello"}, []int{1}, 5},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
at, total, hasText := textsToTranslate(tt.texts)
|
||||
if hasText != tt.wantText || total != tt.wantTotal || !reflect.DeepEqual(at, tt.wantAt) {
|
||||
t.Fatalf("textsToTranslate(%q) = (%v, %d, %v), want (%v, %d, %v)", tt.texts, at, total, hasText, tt.wantAt, tt.wantTotal, tt.wantText)
|
||||
at, total := textsToTranslate(tt.texts)
|
||||
if total != tt.wantTotal || !reflect.DeepEqual(at, tt.wantAt) {
|
||||
t.Fatalf("textsToTranslate(%q) = (%v, %d), want (%v, %d)", tt.texts, at, total, tt.wantAt, tt.wantTotal)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -121,8 +120,6 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
||||
}{
|
||||
{"no texts", nil, http.StatusNotFound},
|
||||
{"empty array", []string{}, http.StatusNotFound},
|
||||
{"empty string", []string{""}, http.StatusNotFound},
|
||||
{"every text empty", []string{"", ""}, 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
|
||||
@@ -143,26 +140,31 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A request that has text but nothing to translate is answered with its own
|
||||
// texts rather than rejected, and — like the validation cases above — it never
|
||||
// reaches DeepL.
|
||||
// Every text is answered — translated when it carries something, returned as it
|
||||
// is when it is blank — so only a request that names no text at all is an
|
||||
// error, and none of the cases below reaches DeepL.
|
||||
func TestTranslateByDLXAnswersBlankTextsWithThemselves(t *testing.T) {
|
||||
tests := [][]string{
|
||||
{" \t "},
|
||||
{"", " "},
|
||||
{"\n", "\t\n", " "},
|
||||
tests := []struct {
|
||||
name string
|
||||
texts []string
|
||||
}{
|
||||
{"single empty", []string{""}},
|
||||
{"two empty", []string{"", ""}},
|
||||
{"whitespace only", []string{" \t "}},
|
||||
{"empty and whitespace", []string{"", " "}},
|
||||
{"newlines and spaces", []string{"\n", "\t\n", " "}},
|
||||
}
|
||||
for _, texts := range tests {
|
||||
t.Run(strings.Join(texts, "|"), func(t *testing.T) {
|
||||
result, err := TranslateByDLX("", "ZH", texts, "", "", "")
|
||||
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 != http.StatusOK {
|
||||
t.Fatalf("TranslateByDLX() code = %d, want %d", result.Code, http.StatusOK)
|
||||
}
|
||||
if !reflect.DeepEqual(result.Data, texts) {
|
||||
t.Fatalf("TranslateByDLX() data = %q, want %q", result.Data, texts)
|
||||
if !reflect.DeepEqual(result.Data, tt.texts) {
|
||||
t.Fatalf("TranslateByDLX() data = %q, want %q", result.Data, tt.texts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user