mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
- 模型广场以追加方式写入 Vary: Accept-Language,保留 CORS 中间件为具体来源 写入的 Vary: Origin - 管理端写入分组译文时语言代码统一为 BCP 47 惯用大小写(en、zh-TW), 统一后重复的语言代码(如 en 与 EN)返回 400 INVALID_GROUP_I18N - 用户侧按语言取译文时规范写法的键优先,仅大小写不同的多个键按字节序取最小者, 同一请求语言的结果固定
84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestNormalizeGroupI18n(t *testing.T) {
|
|
t.Run("nil input yields an empty non-nil map", func(t *testing.T) {
|
|
got, err := normalizeGroupI18n(nil)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, got)
|
|
require.Empty(t, got)
|
|
})
|
|
|
|
t.Run("trims text and drops locales with nothing left", func(t *testing.T) {
|
|
got, err := normalizeGroupI18n(GroupI18n{
|
|
"en": {Name: " Standard ", Description: "\tPro + Max pool\n"},
|
|
"zh": {Name: " ", Description: ""},
|
|
"zh-TW": {Description: "僅描述"},
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, GroupI18n{
|
|
"en": {Name: "Standard", Description: "Pro + Max pool"},
|
|
"zh-TW": {Description: "僅描述"},
|
|
}, got)
|
|
})
|
|
|
|
t.Run("locale codes are stored in canonical case", func(t *testing.T) {
|
|
got, err := normalizeGroupI18n(GroupI18n{
|
|
"EN": {Name: "Standard"},
|
|
"zh-tw": {Name: "標準"},
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, GroupI18n{
|
|
"en": {Name: "Standard"},
|
|
"zh-TW": {Name: "標準"},
|
|
}, got)
|
|
})
|
|
|
|
t.Run("rejects locales that collide after canonicalization", func(t *testing.T) {
|
|
for _, in := range []GroupI18n{
|
|
{"en": {Name: "English A"}, "EN": {Name: "English B"}},
|
|
{"zh-TW": {Name: "標準"}, "zh-tw": {}},
|
|
} {
|
|
_, err := normalizeGroupI18n(in)
|
|
require.Errorf(t, err, "input %v should be rejected", in)
|
|
}
|
|
})
|
|
|
|
t.Run("rejects malformed locale codes", func(t *testing.T) {
|
|
for _, locale := range []string{"", "e", "en_US", "en-", "中文", "en us"} {
|
|
_, err := normalizeGroupI18n(GroupI18n{locale: {Name: "x"}})
|
|
require.Errorf(t, err, "locale %q should be rejected", locale)
|
|
}
|
|
})
|
|
|
|
t.Run("name length is capped in characters like groups.name", func(t *testing.T) {
|
|
atLimit := strings.Repeat("名", groupI18nNameMaxRunes)
|
|
got, err := normalizeGroupI18n(GroupI18n{"zh": {Name: atLimit}})
|
|
require.NoError(t, err)
|
|
require.Equal(t, atLimit, got["zh"].Name)
|
|
|
|
_, err = normalizeGroupI18n(GroupI18n{"zh": {Name: atLimit + "名"}})
|
|
require.Error(t, err)
|
|
})
|
|
}
|
|
|
|
func TestCloneGroupI18nForDuplicate(t *testing.T) {
|
|
source := GroupI18n{
|
|
"en": {Name: "Standard", Description: "Pro + Max pool"},
|
|
"zh": {Name: "标准"},
|
|
}
|
|
|
|
got := cloneGroupI18nForDuplicate(source)
|
|
|
|
// 副本改了 name;名称译文若照搬,副本与源分组在该语言下会同名。
|
|
require.Equal(t, GroupI18n{"en": {Description: "Pro + Max pool"}}, got)
|
|
require.Equal(t, "Standard", source["en"].Name, "source must not be mutated")
|
|
require.NotNil(t, cloneGroupI18nForDuplicate(nil))
|
|
}
|