yumi-golang/internal/service/highwin/broadcast_test.go
2026-05-22 14:22:27 +08:00

72 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package highwin
import (
"context"
"testing"
"chatapp3-golang/internal/service/regionimgroup"
)
func TestSendRegionBroadcastBuildsRequiredPayload(t *testing.T) {
broadcaster := &fakeRegionBroadcaster{}
err := SendRegionBroadcast(context.Background(), broadcaster, BroadcastEvent{
SysOrigin: "LIKEI",
Type: MessageTypeBaishunWin,
UserID: 1001,
RoomID: "9001",
Multiple: 10.5,
Amount: 2100,
Data: map[string]any{
"gameId": 1146,
},
})
if err != nil {
t.Fatalf("SendRegionBroadcast() error = %v", err)
}
if len(broadcaster.requests) != 1 {
t.Fatalf("requests = %d, want 1", len(broadcaster.requests))
}
req := broadcaster.requests[0]
if req.Type != MessageTypeBaishunWin {
t.Fatalf("type = %q, want %q", req.Type, MessageTypeBaishunWin)
}
if req.Data["userId"] != "1001" || req.Data["roomId"] != "9001" ||
req.Data["winAmount"] != int64(2100) || req.Data["multiple"] != 10.5 ||
req.Data["msg"] != "1001 play game get x 10.5 win 2100 coins" {
t.Fatalf("payload = %+v", req.Data)
}
}
func TestSendRegionBroadcastSkipsBelowThreshold(t *testing.T) {
broadcaster := &fakeRegionBroadcaster{}
err := SendRegionBroadcast(context.Background(), broadcaster, BroadcastEvent{
Type: MessageTypeLuckyGift,
UserID: 1001,
RoomID: "9001",
Multiple: 9,
Amount: 900,
})
if err != nil {
t.Fatalf("SendRegionBroadcast() error = %v", err)
}
if len(broadcaster.requests) != 0 {
t.Fatalf("requests = %d, want 0", len(broadcaster.requests))
}
}
type fakeRegionBroadcaster struct {
requests []regionimgroup.RegionBroadcastRequest
}
func (f *fakeRegionBroadcaster) SendRegionBroadcast(_ context.Context, req regionimgroup.RegionBroadcastRequest) (*regionimgroup.RegionBroadcastResponse, error) {
f.requests = append(f.requests, req)
return &regionimgroup.RegionBroadcastResponse{
SysOrigin: req.SysOrigin,
RegionCode: "AR",
GroupID: "@region-ar",
Type: req.Type,
}, nil
}