mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 14:57:52 +00:00
`text` now accepts either a single string or an array of strings. A string is answered with a string in `data` exactly as before; an array is answered with a position-aligned array, and the whole batch travels in a single oneshot request, so a caller with many segments needs one round trip instead of one per segment. A batch is all-or-nothing: an empty array or an empty element answers with the existing 404 "No text to translate", and if upstream returns a different number of translations or leaves one empty the request fails with 503 "Translation failed" instead of returning placeholders or a misaligned array. The anonymous length cap applies to the total length of the array, which the README now documents. Refs #240
49 lines
1.7 KiB
Go
49 lines
1.7 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The `text` field of POST /translate accepts either shape; decoding uses the
|
|
// same encoding/json decoder gin binds request bodies with.
|
|
func TestPayloadTextUnmarshalJSON(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
payload string
|
|
wantTexts []string
|
|
wantBatch bool
|
|
wantErr bool
|
|
}{
|
|
{name: "string", payload: `{"text":"Hello"}`, wantTexts: []string{"Hello"}},
|
|
{name: "empty string", payload: `{"text":""}`, wantTexts: []string{""}},
|
|
{name: "array", payload: `{"text":["Hello","Good morning"]}`, wantTexts: []string{"Hello", "Good morning"}, wantBatch: true},
|
|
{name: "single-element array", payload: `{"text":["Hello"]}`, wantTexts: []string{"Hello"}, wantBatch: true},
|
|
{name: "empty array", payload: `{"text":[]}`, wantTexts: []string{}, wantBatch: true},
|
|
{name: "null", payload: `{"text":null}`, wantTexts: []string{""}},
|
|
{name: "number", payload: `{"text":1}`, wantErr: true},
|
|
{name: "object", payload: `{"text":{}}`, wantErr: true},
|
|
{name: "mixed array", payload: `{"text":["Hello",1]}`, wantErr: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var req PayloadTranslate
|
|
err := json.NewDecoder(strings.NewReader(tt.payload)).Decode(&req)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("Decode(%s) error = %v, wantErr %v", tt.payload, err, tt.wantErr)
|
|
}
|
|
if tt.wantErr {
|
|
return
|
|
}
|
|
if req.Text.Batch != tt.wantBatch {
|
|
t.Errorf("Batch = %v, want %v", req.Text.Batch, tt.wantBatch)
|
|
}
|
|
if !reflect.DeepEqual(req.Text.Texts, tt.wantTexts) {
|
|
t.Errorf("Texts = %q, want %q", req.Text.Texts, tt.wantTexts)
|
|
}
|
|
})
|
|
}
|
|
}
|