mirror of
https://github.com/alibaba/higress.git
synced 2026-02-26 13:40:49 +08:00
109 lines
2.4 KiB
Go
109 lines
2.4 KiB
Go
package util
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDecodeUnicodeEscapes(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "Chinese characters",
|
|
input: `\u4e2d\u6587\u6d4b\u8bd5`,
|
|
expected: `中文测试`,
|
|
},
|
|
{
|
|
name: "Mixed content",
|
|
input: `Hello \u4e16\u754c World`,
|
|
expected: `Hello 世界 World`,
|
|
},
|
|
{
|
|
name: "No escape sequences",
|
|
input: `Hello World`,
|
|
expected: `Hello World`,
|
|
},
|
|
{
|
|
name: "JSON with Unicode escapes",
|
|
input: `{"content":"\u76c8\u5229\u80fd\u529b"}`,
|
|
expected: `{"content":"盈利能力"}`,
|
|
},
|
|
{
|
|
name: "Full width parentheses",
|
|
input: `\uff08\u76c8\u5229\uff09`,
|
|
expected: `(盈利)`,
|
|
},
|
|
{
|
|
name: "Empty string",
|
|
input: ``,
|
|
expected: ``,
|
|
},
|
|
{
|
|
name: "Invalid escape sequence (not modified)",
|
|
input: `\u00GG`,
|
|
expected: `\u00GG`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := DecodeUnicodeEscapes([]byte(tt.input))
|
|
assert.Equal(t, tt.expected, string(result))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecodeUnicodeEscapesInSSE(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "SSE data with Unicode escapes",
|
|
input: `data: {"choices":[{"delta":{"content":"\u4e2d\u6587"}}]}
|
|
|
|
`,
|
|
expected: `data: {"choices":[{"delta":{"content":"中文"}}]}
|
|
|
|
`,
|
|
},
|
|
{
|
|
name: "Multiple SSE data lines",
|
|
input: `data: {"content":"\u4e2d\u6587"}
|
|
data: {"content":"\u82f1\u6587"}
|
|
data: [DONE]
|
|
`,
|
|
expected: `data: {"content":"中文"}
|
|
data: {"content":"英文"}
|
|
data: [DONE]
|
|
`,
|
|
},
|
|
{
|
|
name: "Non-data lines unchanged",
|
|
input: ": comment\nevent: message\ndata: test\n",
|
|
expected: ": comment\nevent: message\ndata: test\n",
|
|
},
|
|
{
|
|
name: "Real Vertex AI response format",
|
|
input: `data: {"choices":[{"delta":{"content":"\uff08\u76c8\u5229\u80fd\u529b\uff09","role":"assistant"},"index":0}],"created":1768307454,"id":"test","model":"gemini","object":"chat.completion.chunk"}
|
|
|
|
`,
|
|
expected: `data: {"choices":[{"delta":{"content":"(盈利能力)","role":"assistant"},"index":0}],"created":1768307454,"id":"test","model":"gemini","object":"chat.completion.chunk"}
|
|
|
|
`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := DecodeUnicodeEscapesInSSE([]byte(tt.input))
|
|
assert.Equal(t, tt.expected, string(result))
|
|
})
|
|
}
|
|
}
|