fix(playground): resolve auto group model listing (#6163)
* fix(playground): resolve auto group model listing - merge and deduplicate available models in configured auto group order. - reuse special usable group rules and add model filtering regression coverage. * refactor: extract GetGroupsEnabledModels to dedupe group model expansion
This commit is contained in:
@@ -12,8 +12,10 @@ import (
|
||||
"github.com/QuantumNous/new-api/constant"
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/QuantumNous/new-api/setting"
|
||||
"github.com/QuantumNous/new-api/setting/config"
|
||||
"github.com/QuantumNous/new-api/setting/operation_setting"
|
||||
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -213,6 +215,56 @@ func TestGetUserModelsFiltersByRequestedGroup(t *testing.T) {
|
||||
require.Empty(t, decodeUserModelsResponse(t, vipRecorder))
|
||||
}
|
||||
|
||||
func TestGetUserModelsExpandsAutoGroupsInConfiguredOrder(t *testing.T) {
|
||||
originalAutoGroups := setting.AutoGroups2JsonString()
|
||||
originalUsableGroups := setting.UserUsableGroups2JSONString()
|
||||
originalSpecialGroups := ratio_setting.GetGroupRatioSetting().GroupSpecialUsableGroup.ReadAll()
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, setting.UpdateAutoGroupsByJsonString(originalAutoGroups))
|
||||
require.NoError(t, setting.UpdateUserUsableGroupsByJSONString(originalUsableGroups))
|
||||
specialGroups := ratio_setting.GetGroupRatioSetting().GroupSpecialUsableGroup
|
||||
specialGroups.Clear()
|
||||
specialGroups.AddAll(originalSpecialGroups)
|
||||
})
|
||||
|
||||
require.NoError(t, setting.UpdateAutoGroupsByJsonString(`["vip","default","unavailable"]`))
|
||||
require.NoError(t, setting.UpdateUserUsableGroupsByJSONString(`{"auto":"自动分组","default":"默认分组","unavailable":"不可用分组"}`))
|
||||
specialGroups := ratio_setting.GetGroupRatioSetting().GroupSpecialUsableGroup
|
||||
specialGroups.Clear()
|
||||
specialGroups.Set("default", map[string]string{
|
||||
"+:vip": "VIP 分组",
|
||||
"-:unavailable": "",
|
||||
})
|
||||
|
||||
db := setupModelListControllerTestDB(t)
|
||||
require.NoError(t, db.Create(&model.User{
|
||||
Id: 1003,
|
||||
Username: "playground-auto-model-user",
|
||||
Password: "password",
|
||||
Group: "default",
|
||||
Status: common.UserStatusEnabled,
|
||||
}).Error)
|
||||
require.NoError(t, db.Create(&[]model.Ability{
|
||||
{Group: "vip", Model: "zz-vip-model", ChannelId: 1, Enabled: true},
|
||||
{Group: "vip", Model: "zz-shared-model", ChannelId: 1, Enabled: true},
|
||||
{Group: "default", Model: "zz-default-model", ChannelId: 1, Enabled: true},
|
||||
{Group: "default", Model: "zz-shared-model", ChannelId: 2, Enabled: true},
|
||||
{Group: "unavailable", Model: "zz-unavailable-model", ChannelId: 1, Enabled: true},
|
||||
}).Error)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
context, _ := gin.CreateTestContext(recorder)
|
||||
context.Request = httptest.NewRequest(http.MethodGet, "/api/user/models?group=auto", nil)
|
||||
context.Set("id", 1003)
|
||||
|
||||
GetUserModels(context)
|
||||
|
||||
models := decodeUserModelsResponse(t, recorder)
|
||||
require.Len(t, models, 3)
|
||||
assert.ElementsMatch(t, []string{"zz-vip-model", "zz-shared-model"}, models[:2])
|
||||
assert.Equal(t, "zz-default-model", models[2])
|
||||
}
|
||||
|
||||
func TestListModelsIncludesTieredBillingModel(t *testing.T) {
|
||||
withSelfUseModeDisabled(t)
|
||||
withTieredBillingConfig(t, map[string]string{
|
||||
|
||||
Reference in New Issue
Block a user