96 lines
3.7 KiB
Go
96 lines
3.7 KiB
Go
package roomturnoverreward
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"chatapp3-golang/internal/integration"
|
|
)
|
|
|
|
type stubRoomTurnoverRewardJavaGateway struct {
|
|
contributions []integration.RoomContributionActivityCount
|
|
current integration.RoomContributionActivityCount
|
|
changeErr error
|
|
changed []integration.GoldReceiptCommand
|
|
markedIDs []string
|
|
userProfile integration.UserProfile
|
|
userRoom integration.RoomProfile
|
|
profiles map[int64]integration.RoomProfile
|
|
}
|
|
|
|
func (s stubRoomTurnoverRewardJavaGateway) GetUserProfile(_ context.Context, _ int64) (integration.UserProfile, error) {
|
|
return s.userProfile, nil
|
|
}
|
|
|
|
func (s stubRoomTurnoverRewardJavaGateway) GetRoomProfileByUserID(_ context.Context, _ int64) (integration.RoomProfile, error) {
|
|
return s.userRoom, nil
|
|
}
|
|
|
|
func (s stubRoomTurnoverRewardJavaGateway) GetCurrentWeekRoomContribution(_ context.Context, _ int64) (integration.RoomContributionActivityCount, error) {
|
|
return s.current, nil
|
|
}
|
|
|
|
func (s stubRoomTurnoverRewardJavaGateway) ListCurrentWeekRoomContribution(_ context.Context, _ int) ([]integration.RoomContributionActivityCount, error) {
|
|
return s.contributions, nil
|
|
}
|
|
|
|
func (s stubRoomTurnoverRewardJavaGateway) MapRoomProfiles(_ context.Context, _ []int64) (map[int64]integration.RoomProfile, error) {
|
|
return s.profiles, nil
|
|
}
|
|
|
|
func (s *stubRoomTurnoverRewardJavaGateway) ChangeGoldBalance(_ context.Context, cmd integration.GoldReceiptCommand) error {
|
|
s.changed = append(s.changed, cmd)
|
|
return s.changeErr
|
|
}
|
|
|
|
func (s *stubRoomTurnoverRewardJavaGateway) MarkRoomTurnoverRewardCoinsSent(_ context.Context, id string) (bool, error) {
|
|
s.markedIDs = append(s.markedIDs, id)
|
|
return true, nil
|
|
}
|
|
|
|
func TestCurrentRoomRankingSortsAndMatchesHighestLevel(t *testing.T) {
|
|
service, _ := newTestService(t)
|
|
service.repo.Java = &stubRoomTurnoverRewardJavaGateway{
|
|
contributions: []integration.RoomContributionActivityCount{
|
|
{RoomID: integration.Int64Value(101), ContributionValue: integration.AmountValue(120000)},
|
|
{RoomID: integration.Int64Value(102), ContributionValue: integration.AmountValue(800000)},
|
|
{RoomID: integration.Int64Value(103), ContributionValue: integration.AmountValue(900000)},
|
|
},
|
|
profiles: map[int64]integration.RoomProfile{
|
|
101: {ID: integration.Int64Value(101), RoomName: "A", SysOrigin: "LIKEI", UserID: integration.Int64Value(1001)},
|
|
102: {ID: integration.Int64Value(102), RoomName: "B", SysOrigin: "LIKEI", UserID: integration.Int64Value(1002)},
|
|
103: {ID: integration.Int64Value(103), RoomName: "C", SysOrigin: "TARAB", UserID: integration.Int64Value(1003)},
|
|
},
|
|
}
|
|
|
|
_, err := service.SaveConfig(context.Background(), SaveRoomTurnoverRewardConfigRequest{
|
|
SysOrigin: "LIKEI",
|
|
Enabled: true,
|
|
Timezone: "Asia/Riyadh",
|
|
LevelConfigs: []RoomTurnoverRewardLevelInput{
|
|
{Level: 1, TurnoverThreshold: 100000, RewardGold: 2000, Enabled: true},
|
|
{Level: 2, TurnoverThreshold: 500000, RewardGold: 15000, Enabled: true},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("SaveConfig() error = %v", err)
|
|
}
|
|
|
|
resp, err := service.CurrentRoomRanking(context.Background(), "likei", 10)
|
|
if err != nil {
|
|
t.Fatalf("CurrentRoomRanking() error = %v", err)
|
|
}
|
|
if len(resp.Records) != 2 {
|
|
t.Fatalf("record count = %d, want 2", len(resp.Records))
|
|
}
|
|
if got := resp.Records[0]; got.RoomID != 102 || got.Rank != 1 || got.MatchedLevel != 2 || got.ExpectedRewardGold != 15000 {
|
|
t.Fatalf("top record = %+v", got)
|
|
}
|
|
if got := resp.Records[1]; got.RoomID != 101 || got.Rank != 2 || got.MatchedLevel != 1 || got.ExpectedRewardGold != 2000 {
|
|
t.Fatalf("second record = %+v", got)
|
|
}
|
|
if resp.CycleKey == "" || resp.PeriodStartAt == "" || resp.PeriodEndAt == "" {
|
|
t.Fatalf("expected cycle metadata, got %+v", resp)
|
|
}
|
|
}
|