package game import ( "reflect" "testing" "thehouseoficarus/internal/player" ) func TestRewritePlayerRoomIDsRename(t *testing.T) { p := &player.Player{ RoomID: 10, PendingSequence: &player.PendingSequence{RoomID: 20, Key: "k"}, Flags: map[string]any{ "hidden_exit_10_north": 1, "hidden_exit_20_down": 1, "unrelated_flag": 1, }, MapSymbols: map[int]player.MapSymbolData{ 10: {Char: "X"}, 30: {Char: "Y"}, }, } p.Stats.RoomsVisited = player.NewRoomSetFrom(10, 40) idMap := map[int]int{10: 100, 20: 200} if !rewritePlayerRoomIDs(p, idMap) { t.Fatal("expected changed=true") } if p.RoomID != 100 { t.Errorf("RoomID = %d, want 100", p.RoomID) } if p.PendingSequence == nil || p.PendingSequence.RoomID != 200 { t.Errorf("PendingSequence.RoomID = %v, want 200", p.PendingSequence) } wantFlags := map[string]any{ "hidden_exit_100_north": 1, "hidden_exit_200_down": 1, "unrelated_flag": 1, } if !reflect.DeepEqual(p.Flags, wantFlags) { t.Errorf("Flags = %v, want %v", p.Flags, wantFlags) } if _, ok := p.MapSymbols[100]; !ok { t.Error("MapSymbols missing key 100") } if _, ok := p.MapSymbols[30]; !ok { t.Error("MapSymbols missing unchanged key 30") } if _, ok := p.MapSymbols[10]; ok { t.Error("MapSymbols should not still have key 10") } if !p.Stats.RoomsVisited.Has(100) { t.Error("RoomsVisited missing key 100") } if !p.Stats.RoomsVisited.Has(40) { t.Error("RoomsVisited missing unchanged key 40") } if p.Stats.RoomsVisited.Has(10) { t.Error("RoomsVisited should not still have key 10") } } func TestRewritePlayerRoomIDsNoChange(t *testing.T) { p := &player.Player{ RoomID: 5, Flags: map[string]any{"hidden_exit_5_north": 1}, } if rewritePlayerRoomIDs(p, map[int]int{99: 100}) { t.Error("expected changed=false when no embedded ID matches") } if p.RoomID != 5 { t.Errorf("RoomID = %d, want 5 (untouched)", p.RoomID) } if _, ok := p.Flags["hidden_exit_5_north"]; !ok { t.Error("hidden_exit_5_north should be unchanged") } } // A swap idMap (A<->B) must not clobber a flag whose old key maps to another // flag's old key and vice versa. func TestRewritePlayerRoomIDsSwap(t *testing.T) { p := &player.Player{ RoomID: 10, Flags: map[string]any{ "hidden_exit_10_north": 1, "hidden_exit_100_east": 1, }, } swap := map[int]int{10: 100, 100: 10} if !rewritePlayerRoomIDs(p, swap) { t.Fatal("expected changed=true") } if p.RoomID != 100 { t.Errorf("RoomID = %d, want 100", p.RoomID) } wantFlags := map[string]any{ "hidden_exit_100_north": 1, "hidden_exit_10_east": 1, } if !reflect.DeepEqual(p.Flags, wantFlags) { t.Errorf("Flags = %v, want %v", p.Flags, wantFlags) } }