2026-06-18 22:41:52 +08:00

84 lines
2.9 KiB
Go

package roomadmin
import (
"encoding/json"
"errors"
"reflect"
"testing"
)
func TestNormalizeRoomConfigSortsAndKeepsFixedCandidates(t *testing.T) {
config, err := normalizeRoomConfig(RoomConfig{
AllowedSeatCounts: []int32{30, 10, 20},
DefaultSeatCount: 20,
UpdatedAtMS: 123,
})
if err != nil {
t.Fatalf("normalizeRoomConfig failed: %v", err)
}
if !reflect.DeepEqual(config.CandidateSeatCounts, []int32{10, 15, 20, 25, 30}) {
t.Fatalf("candidate seat counts mismatch: %+v", config.CandidateSeatCounts)
}
if !reflect.DeepEqual(config.AllowedSeatCounts, []int32{10, 20, 30}) {
t.Fatalf("allowed seat counts should be sorted: %+v", config.AllowedSeatCounts)
}
if config.DefaultSeatCount != 20 || config.UpdatedAtMS != 123 {
t.Fatalf("config fields changed unexpectedly: %+v", config)
}
}
func TestNormalizeRoomConfigRejectsInvalidValues(t *testing.T) {
tests := []struct {
name string
config RoomConfig
}{
{name: "empty", config: RoomConfig{AllowedSeatCounts: nil, DefaultSeatCount: 15}},
{name: "duplicate", config: RoomConfig{AllowedSeatCounts: []int32{10, 10}, DefaultSeatCount: 10}},
{name: "outside_candidate", config: RoomConfig{AllowedSeatCounts: []int32{12}, DefaultSeatCount: 12}},
{name: "default_not_allowed", config: RoomConfig{AllowedSeatCounts: []int32{10, 20}, DefaultSeatCount: 15}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := normalizeRoomConfig(test.config)
if !errors.Is(err, ErrInvalidArgument) {
t.Fatalf("normalizeRoomConfig should reject %s with ErrInvalidArgument, got %v", test.name, err)
}
})
}
}
func TestHumanRoomRobotAllowedOwnerIDsAcceptsCommaStringAndArray(t *testing.T) {
for _, test := range []struct {
name string
body string
want []string
}{
{name: "comma_string", body: `{"allowedOwnerIds":"1001, 1002,,1001"}`, want: []string{"1001", "1002"}},
{name: "array", body: `{"allowedOwnerIds":["1003"," 1004 ","1003"]}`, want: []string{"1003", "1004"}},
} {
t.Run(test.name, func(t *testing.T) {
var req updateHumanRoomRobotConfigRequest
if err := json.Unmarshal([]byte(test.body), &req); err != nil {
t.Fatalf("unmarshal human room robot config failed: %v", err)
}
if got := normalizeStringList([]string(req.AllowedOwnerIDs)); !reflect.DeepEqual(got, test.want) {
t.Fatalf("allowed owner ids mismatch: got %+v want %+v", got, test.want)
}
})
}
}
func TestHumanRoomRobotCountryRulesDecode(t *testing.T) {
var req updateHumanRoomRobotConfigRequest
if err := json.Unmarshal([]byte(`{"countryLimitEnabled":true,"countryRules":[{"countryCode":"sa","maxRoomCount":2}]}`), &req); err != nil {
t.Fatalf("unmarshal human room robot country rules failed: %v", err)
}
if !req.CountryLimitEnabled || len(req.CountryRules) != 1 {
t.Fatalf("country rules not decoded: %+v", req)
}
if req.CountryRules[0].CountryCode != "sa" || req.CountryRules[0].MaxRoomCount != 2 {
t.Fatalf("country rule mismatch: %+v", req.CountryRules[0])
}
}