package service import ( "testing" "hyapp/services/room-service/internal/room/command" "hyapp/services/room-service/internal/room/state" ) func TestReplayMicHeartbeatSkipsStaleMissingSeat(t *testing.T) { current := state.NewRoomState("room-replay-stale-heartbeat", 7101, 8, "voice") current.OnlineUsers[7101] = &state.RoomUserState{UserID: 7101, Role: "owner", JoinedAtMS: 1000, LastSeenAtMS: 1000} if err := replay(current, &command.MicHeartbeat{ Base: command.Base{ActorID: 7101, Room: current.RoomID, SentAtMS: 2000}, TargetUserID: 7101, MicSessionID: "mic-old", }, 2000); err != nil { t.Fatalf("stale heartbeat without seat must be skipped: %v", err) } if current.Version != 0 || current.OnlineUsers[7101].LastSeenAtMS != 1000 { t.Fatalf("stale heartbeat must not mutate state, version=%d user=%+v", current.Version, current.OnlineUsers[7101]) } } func TestReplayMicHeartbeatSkipsStaleSession(t *testing.T) { current := state.NewRoomState("room-replay-stale-session", 7201, 8, "voice") current.OnlineUsers[7201] = &state.RoomUserState{UserID: 7201, Role: "owner", JoinedAtMS: 1000, LastSeenAtMS: 1000} current.MicSeats[0].UserID = 7201 current.MicSeats[0].PublishState = state.MicPublishPending current.MicSeats[0].MicSessionID = "mic-new" if err := replay(current, &command.MicHeartbeat{ Base: command.Base{ActorID: 7201, Room: current.RoomID, SentAtMS: 2000}, TargetUserID: 7201, MicSessionID: "mic-old", }, 2000); err != nil { t.Fatalf("stale heartbeat for replaced session must be skipped: %v", err) } if current.Version != 0 || current.MicSeats[0].MicHeartbeatAtMS != 0 || current.OnlineUsers[7201].LastSeenAtMS != 1000 { t.Fatalf("stale heartbeat must not refresh pending/replaced session, version=%d seat=%+v user=%+v", current.Version, current.MicSeats[0], current.OnlineUsers[7201]) } } func TestReplayConfirmMicPublishingSkipsStaleMissingSeat(t *testing.T) { current := state.NewRoomState("room-replay-stale-confirm", 7301, 8, "voice") current.OnlineUsers[7301] = &state.RoomUserState{UserID: 7301, Role: "owner", JoinedAtMS: 1000, LastSeenAtMS: 1000} if err := replay(current, &command.ConfirmMicPublishing{ Base: command.Base{ActorID: 7301, Room: current.RoomID, SentAtMS: 2000}, TargetUserID: 7301, MicSessionID: "mic-old", RoomVersion: 1, EventTimeMS: 1900, }, 2000); err != nil { t.Fatalf("stale confirm without seat must be skipped: %v", err) } if current.Version != 0 { t.Fatalf("stale confirm must not mutate version, got %d", current.Version) } } func TestReplayConfirmMicPublishingSkipsStaleSession(t *testing.T) { current := state.NewRoomState("room-replay-stale-confirm-session", 7401, 8, "voice") current.OnlineUsers[7401] = &state.RoomUserState{UserID: 7401, Role: "owner", JoinedAtMS: 1000, LastSeenAtMS: 1000} current.MicSeats[0].UserID = 7401 current.MicSeats[0].PublishState = state.MicPublishPending current.MicSeats[0].MicSessionID = "mic-new" if err := replay(current, &command.ConfirmMicPublishing{ Base: command.Base{ActorID: 7401, Room: current.RoomID, SentAtMS: 2000}, TargetUserID: 7401, MicSessionID: "mic-old", RoomVersion: 1, EventTimeMS: 1900, }, 2000); err != nil { t.Fatalf("stale confirm for replaced session must be skipped: %v", err) } if current.Version != 0 || current.MicSeats[0].PublishState != state.MicPublishPending { t.Fatalf("stale confirm must not publish replaced session, version=%d seat=%+v", current.Version, current.MicSeats[0]) } } func TestReplayApplyRTCEventSkipsStaleMissingSeat(t *testing.T) { current := state.NewRoomState("room-replay-stale-rtc", 7501, 8, "voice") current.OnlineUsers[7501] = &state.RoomUserState{UserID: 7501, Role: "owner", JoinedAtMS: 1000, LastSeenAtMS: 1000} if err := replay(current, &command.ApplyRTCEvent{ Base: command.Base{ActorID: 7501, Room: current.RoomID, SentAtMS: 2000}, TargetUserID: 7501, EventType: rtcEventAudioStarted, EventTimeMS: 1900, }, 2000); err != nil { t.Fatalf("stale rtc start without seat must be skipped: %v", err) } if err := replay(current, &command.ApplyRTCEvent{ Base: command.Base{ActorID: 7501, Room: current.RoomID, SentAtMS: 2100}, TargetUserID: 7501, EventType: rtcEventAudioStopped, EventTimeMS: 2050, }, 2100); err != nil { t.Fatalf("stale rtc stop without seat must be skipped: %v", err) } if current.Version != 0 { t.Fatalf("stale rtc events must not mutate version, got %d", current.Version) } }