mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
- moejs 迁到 github.com/MoeForge/moejs v0.1.0,Dockerfile、Electron 构建与发布流程的 PGO 路径同步更新;发布附带 THIRD-PARTY-LICENSES.md,并补充 moejs 附带的第三方声明。 - moego 升级到 v0.1.0-alpha.2:宿主 JSON 编解码经 common/kitutil 封装改用 moego/json,根模块和 relaykit 生产代码的 regexp 改用 moego/regexp。 - 内存中的请求体由校验器零拷贝解析;磁盘缓存的请求体经独立文件读取器解码,且只接受一个 JSON 值,尾随内容会被拒绝。 - 模型/分组提取与重复 model 键检测、渠道亲和 JSON 路径、任务协议 stream/background 标志、multipart 解析在磁盘模式下不再把整份请求体读进内存。图片透传直接转发已存储的请求体,并校验最终发往上游的 n。 - 出站请求体各自持有读取位置。此前透传重试共用存储游标,上一次尝试仍在发送时,下一次尝试可能发出残缺的请求体。收到响应头后不再提前关闭请求体,避免中断仍在发送的请求体。 - 表达式计费只在表达式使用 param() 时冻结请求体,其余情况不再读取并保留整份请求体。
297 lines
9.2 KiB
Go
297 lines
9.2 KiB
Go
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/QuantumNous/new-api/relaykit/dto"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestJsonRawMessageToString(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
data json.RawMessage
|
|
want string
|
|
}{
|
|
{
|
|
name: "object",
|
|
data: json.RawMessage(`{"city":"Paris","days":0,"strict":false}`),
|
|
want: `{"city":"Paris","days":0,"strict":false}`,
|
|
},
|
|
{
|
|
name: "string",
|
|
data: json.RawMessage(`"{\"city\":\"Paris\",\"days\":0,\"strict\":false}"`),
|
|
want: `{"city":"Paris","days":0,"strict":false}`,
|
|
},
|
|
{
|
|
name: "null",
|
|
data: json.RawMessage(`null`),
|
|
want: "",
|
|
},
|
|
{
|
|
name: "empty",
|
|
data: nil,
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.Equal(t, tt.want, JsonRawMessageToString(tt.data))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecodeJsonWithValidation(t *testing.T) {
|
|
type request struct {
|
|
Code string `json:"code" binding:"required"`
|
|
}
|
|
for _, test := range []struct {
|
|
name, body string
|
|
validationError bool
|
|
decodeError bool
|
|
}{
|
|
{name: "valid", body: `{"code":"123456"}`},
|
|
{name: "missing required field", body: `{}`, validationError: true},
|
|
{name: "empty required field", body: `{"code":""}`, validationError: true},
|
|
{name: "malformed JSON", body: `{"code":`, decodeError: true},
|
|
{name: "wrong field type", body: `{"code":123456}`, decodeError: true},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
var value request
|
|
err := DecodeJsonWithValidation(strings.NewReader(test.body), &value)
|
|
if test.validationError {
|
|
var validationErrors validator.ValidationErrors
|
|
require.ErrorAs(t, err, &validationErrors)
|
|
require.Len(t, validationErrors, 1)
|
|
assert.Equal(t, "Code", validationErrors[0].Field())
|
|
assert.Equal(t, "required", validationErrors[0].Tag())
|
|
return
|
|
}
|
|
if test.decodeError {
|
|
require.Error(t, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "123456", value.Code)
|
|
})
|
|
}
|
|
// Existing decoding callers opt into validation explicitly.
|
|
var unvalidated request
|
|
require.NoError(t, DecodeJson(strings.NewReader(`{}`), &unvalidated))
|
|
}
|
|
|
|
// TestHostJSONCodecConformance runs through the codec injected by common's
|
|
// init() and locks the encoding semantics the relay DTOs depend on. A future
|
|
// engine swap in hostJSONCodec must keep every case here green.
|
|
func TestHostJSONCodecConformance(t *testing.T) {
|
|
type embedded struct {
|
|
Content any `json:"content"`
|
|
}
|
|
type shadowed struct {
|
|
embedded
|
|
Content any `json:"content,omitempty"`
|
|
}
|
|
type anyFields struct {
|
|
Nil any `json:"nil,omitempty"`
|
|
Str any `json:"str,omitempty"`
|
|
Int any `json:"int,omitempty"`
|
|
Bool any `json:"bool,omitempty"`
|
|
}
|
|
type rawFields struct {
|
|
Obj json.RawMessage `json:"obj"`
|
|
Arr json.RawMessage `json:"arr"`
|
|
Nested json.RawMessage `json:"nested"`
|
|
Str json.RawMessage `json:"str"`
|
|
}
|
|
type numberField struct {
|
|
N json.Number `json:"n"`
|
|
}
|
|
type pointerZeros struct {
|
|
Count *int `json:"count,omitempty"`
|
|
Enabled *bool `json:"enabled,omitempty"`
|
|
}
|
|
zero := 0
|
|
off := false
|
|
|
|
t.Run("marshal", func(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
in any
|
|
want string
|
|
}{
|
|
{
|
|
name: "shallowest field shadows embedded content and nil is omitted",
|
|
in: shadowed{embedded: embedded{Content: "inner"}},
|
|
want: `{}`,
|
|
},
|
|
{
|
|
name: "shallowest field keeps empty string content",
|
|
in: shadowed{embedded: embedded{Content: "inner"}, Content: ""},
|
|
want: `{"content":""}`,
|
|
},
|
|
{
|
|
name: "omitempty on any drops nil but keeps zero values",
|
|
in: anyFields{Str: "", Int: 0, Bool: false},
|
|
want: `{"str":"","int":0,"bool":false}`,
|
|
},
|
|
{
|
|
name: "map keys are sorted",
|
|
in: map[string]any{"z": 1, "a": 2, "m": 3},
|
|
want: `{"a":2,"m":3,"z":1}`,
|
|
},
|
|
{
|
|
name: "html characters are escaped",
|
|
in: map[string]string{"s": `<a href="x">&</a>`},
|
|
want: `{"s":"\u003ca href=\"x\"\u003e\u0026\u003c/a\u003e"}`,
|
|
},
|
|
{
|
|
name: "explicit pointer zeros are kept",
|
|
in: pointerZeros{Count: &zero, Enabled: &off},
|
|
want: `{"count":0,"enabled":false}`,
|
|
},
|
|
{
|
|
name: "nil pointers are omitted",
|
|
in: pointerZeros{},
|
|
want: `{}`,
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
encoded, err := Marshal(tt.in)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.want, string(encoded))
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("raw message passthrough", func(t *testing.T) {
|
|
input := `{"obj":{},"arr":[],"nested":{"k":[1,2]},"str":"x"}`
|
|
var value rawFields
|
|
require.NoError(t, UnmarshalJsonStr(input, &value))
|
|
assert.Equal(t, `{}`, string(value.Obj))
|
|
assert.Equal(t, `[]`, string(value.Arr))
|
|
assert.Equal(t, `{"k":[1,2]}`, string(value.Nested))
|
|
assert.Equal(t, `"x"`, string(value.Str))
|
|
encoded, err := Marshal(value)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, input, string(encoded))
|
|
})
|
|
|
|
t.Run("json.Number keeps large integers exact", func(t *testing.T) {
|
|
input := `{"n":18446744073686646784}`
|
|
var value numberField
|
|
require.NoError(t, Unmarshal([]byte(input), &value))
|
|
assert.Equal(t, json.Number("18446744073686646784"), value.N)
|
|
encoded, err := Marshal(value)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, input, string(encoded))
|
|
})
|
|
|
|
t.Run("explicit zeros survive unmarshal into pointers", func(t *testing.T) {
|
|
var value pointerZeros
|
|
require.NoError(t, Unmarshal([]byte(`{"count":0,"enabled":false}`), &value))
|
|
require.NotNil(t, value.Count)
|
|
require.NotNil(t, value.Enabled)
|
|
assert.Equal(t, 0, *value.Count)
|
|
assert.False(t, *value.Enabled)
|
|
|
|
var absent pointerZeros
|
|
require.NoError(t, Unmarshal([]byte(`{}`), &absent))
|
|
assert.Nil(t, absent.Count)
|
|
assert.Nil(t, absent.Enabled)
|
|
})
|
|
|
|
t.Run("decoded values outlive the request buffer", func(t *testing.T) {
|
|
input := []byte(`{"labels":{"role":"user"},"raw":{"enabled":false},"n":9007199254740993}`)
|
|
var value struct {
|
|
Labels map[string]string `json:"labels"`
|
|
Raw json.RawMessage `json:"raw"`
|
|
N json.Number `json:"n"`
|
|
}
|
|
require.NoError(t, Unmarshal(input, &value))
|
|
for i := range input {
|
|
input[i] = 'x'
|
|
}
|
|
assert.Equal(t, map[string]string{"role": "user"}, value.Labels)
|
|
assert.Equal(t, `{"enabled":false}`, string(value.Raw))
|
|
assert.Equal(t, json.Number("9007199254740993"), value.N)
|
|
})
|
|
|
|
t.Run("standard error types remain available to callers", func(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name, input string
|
|
target any
|
|
wantError any
|
|
}{
|
|
{"syntax", `{"n":`, new(any), new(*json.SyntaxError)},
|
|
{"trailing document", `{} {}`, new(any), new(*json.SyntaxError)},
|
|
{"wrong type", `"text"`, new(int), new(*json.UnmarshalTypeError)},
|
|
{"unsigned negative", `-1`, new(uint64), new(*json.UnmarshalTypeError)},
|
|
{"unsigned overflow", `18446744073709551616`, new(uint64), new(*json.UnmarshalTypeError)},
|
|
{"invalid target", `{}`, nil, new(*json.InvalidUnmarshalError)},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.ErrorAs(t, Unmarshal([]byte(tt.input), tt.target), tt.wantError)
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("large request bodies decode from memory and temporary files", func(t *testing.T) {
|
|
type request struct {
|
|
Content string `json:"content"`
|
|
Count *int `json:"count,omitempty"`
|
|
}
|
|
want := request{Content: strings.Repeat("图片<&>", 32<<10), Count: &zero}
|
|
body, err := Marshal(want)
|
|
require.NoError(t, err)
|
|
file, err := os.CreateTemp(t.TempDir(), "request-*.json")
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { require.NoError(t, file.Close()) })
|
|
_, err = file.Write(append([]byte("prefix"), body...))
|
|
require.NoError(t, err)
|
|
_, err = file.Seek(int64(len("prefix")), io.SeekStart)
|
|
require.NoError(t, err)
|
|
for _, reader := range []io.Reader{bytes.NewBuffer(body), file} {
|
|
var got request
|
|
require.NoError(t, DecodeJson(reader, &got))
|
|
assert.Equal(t, want, got)
|
|
}
|
|
})
|
|
|
|
t.Run("relaykit DTO round trip", func(t *testing.T) {
|
|
raw := []byte(`{
|
|
"model":"kimi-k3",
|
|
"messages":[
|
|
{"role":"system","tools":[{"type":"function","function":{"name":"get_current_time","description":"Get the current time of a city","parameters":{"type":"object","properties":{"city":{"type":"string"}},"required":["city"]}}}]},
|
|
{"role":"assistant","content":null,"tool_calls":[{"id":"call_1","type":"function","function":{"name":"get_current_time","arguments":"{\"city\":\"Beijing\"}"}}]}
|
|
]
|
|
}`)
|
|
var req dto.GeneralOpenAIRequest
|
|
require.NoError(t, Unmarshal(raw, &req))
|
|
encoded, err := Marshal(req)
|
|
require.NoError(t, err)
|
|
|
|
messages := ojson.GetBytes(encoded, "messages").Array()
|
|
require.Len(t, messages, 2)
|
|
|
|
// Kimi K3 dynamic tool loading: tools survive and no content key is emitted.
|
|
assert.Equal(t, "system", messages[0].Get("role").String())
|
|
assert.JSONEq(t, ojson.GetBytes(raw, "messages.0.tools").Raw, messages[0].Get("tools").Raw)
|
|
assert.False(t, messages[0].Get("content").Exists())
|
|
|
|
// Assistant tool-call replay still carries an explicit "content": null.
|
|
assistantContent := messages[1].Get("content")
|
|
assert.True(t, assistantContent.Exists())
|
|
assert.Equal(t, ojson.Null, assistantContent.Type)
|
|
assert.JSONEq(t, ojson.GetBytes(raw, "messages.1.tool_calls").Raw, messages[1].Get("tool_calls").Raw)
|
|
})
|
|
}
|