2026-06-18 18:58:21 +08:00

81 lines
2.5 KiB
Go

package service
import (
"math/rand"
"testing"
"time"
"hyapp/pkg/xerr"
)
func TestHumanRoomRobotGiftSenderLimit(t *testing.T) {
activity := newRobotRoomActivity([]int64{101, 102, 103, 104})
for _, userID := range []int64{101, 102, 103, 104} {
activity.markActive(userID)
}
markInitialHumanRoomGiftSenders(activity, []int64{101, 102, 103, 104}, 2)
if got := activity.giftSenderCount(); got != 2 {
t.Fatalf("gift sender count mismatch: got %d want 2", got)
}
if activity.markGiftSenderWithinLimit(104, 2) {
t.Fatalf("gift sender limit allowed one more sender")
}
}
func TestHumanRoomRobotGiftPresenceDriftRemovesStaleUsers(t *testing.T) {
activity := newRobotRoomActivity([]int64{101, 102, 103})
for _, userID := range []int64{101, 102, 103} {
activity.markActive(userID)
}
activity.markGiftSender(101)
reconcileHumanRoomRobotGiftPresence(activity, 101, 102, xerr.New(xerr.NotFound, "sender not in room"))
if activity.isActive(101) || activity.isGiftSender(101) {
t.Fatalf("stale sender must be removed from active and gift sender sets")
}
if !activity.isActive(102) {
t.Fatalf("target must stay active when sender is stale")
}
activity.markActive(101)
activity.markGiftSender(101)
reconcileHumanRoomRobotGiftPresence(activity, 101, 102, xerr.New(xerr.NotFound, "target not in room"))
if !activity.isActive(101) || !activity.isGiftSender(101) {
t.Fatalf("sender must stay active when target is stale")
}
if activity.isActive(102) {
t.Fatalf("stale target must be removed from active set")
}
}
func TestHumanRoomRobotNormalGiftDelayRange(t *testing.T) {
rng := rand.New(rand.NewSource(1))
config := HumanRoomRobotConfig{NormalGiftIntervalMinMS: 1000, NormalGiftIntervalMaxMS: 20000}
for i := 0; i < 100; i++ {
delay := humanRoomRobotNormalGiftDelay(config, rng)
if delay < time.Second || delay > 20*time.Second {
t.Fatalf("gift delay out of range: %s", delay)
}
}
}
func TestRandomHumanRoomTargetOnlineRange(t *testing.T) {
config := HumanRoomRobotConfig{RoomTargetMinOnline: 8, RoomTargetMaxOnline: 10}
for i := 0; i < 100; i++ {
target := randomHumanRoomTargetOnline(config)
if target < 8 || target > 10 {
t.Fatalf("target online out of range: %d", target)
}
}
}
func TestRandomHumanRoomTargetOnlineLegacyFixedValue(t *testing.T) {
config := HumanRoomRobotConfig{RoomFullStopOnline: 10}
for i := 0; i < 20; i++ {
if target := randomHumanRoomTargetOnline(config); target != 10 {
t.Fatalf("legacy target online mismatch: got %d want 10", target)
}
}
}