diff options
Diffstat (limited to 'internal/game/exit_migrate_test.go')
| -rw-r--r-- | internal/game/exit_migrate_test.go | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/internal/game/exit_migrate_test.go b/internal/game/exit_migrate_test.go new file mode 100644 index 0000000..05edea5 --- /dev/null +++ b/internal/game/exit_migrate_test.go @@ -0,0 +1,107 @@ +package game + +import ( + "reflect" + "testing" + + "thehouseoficarus/internal/player" +) + +func TestRewritePlayerRoomIDsRename(t *testing.T) { + p := &player.Player{ + RoomID: 10, + EnterSeqRoom: 20, + 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 = map[int]bool{10: true, 40: true} + + 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.EnterSeqRoom != 200 { + t.Errorf("EnterSeqRoom = %d, want 200", p.EnterSeqRoom) + } + + 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[100] { + t.Error("RoomsVisited missing key 100") + } + if !p.Stats.RoomsVisited[40] { + t.Error("RoomsVisited missing unchanged key 40") + } + if p.Stats.RoomsVisited[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) + } +} |
