mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
The anonymous oneshot cap is 1500 UTF-16 code units — what String.prototype.length reports in the extension and String.length in the Kotlin iOS client this endpoint is modelled on. utf8.RuneCountInString scores an astral character as one where oneshot scores it two, so 751 emoji (751 runes, 1502 units) passed the guard, travelled upstream and came back as the generic 503 "request failed with status code: 400" instead of the 413 the guard exists to produce. Count the unit oneshot counts. BMP text is unaffected: runes and UTF-16 units agree everywhere except a surrogate pair, which is why the rune count looked right. Invalid UTF-8 is not part of this and needs no validation of its own: encoding/json has already replaced every invalid byte with U+FFFD before the guard runs, one unit each, exactly as a rune count scored them. Measured against the live endpoint through a local build: - 750 astral characters (1500 units) -> 200, 751 (1502 units) -> 400 - 800 CJK characters (800 units, 2400 bytes, 1600 columns) -> 200 - 1000 combining-mark pairs (2000 units, 1000 graphemes) -> 400 - 1500 ASCII -> 200, 1501 ASCII -> 413 from the guard Refs #242
115 lines
4.2 KiB
Go
115 lines
4.2 KiB
Go
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)
|
|
}
|
|
})
|
|
}
|
|
}
|