package game import ( "os" "path/filepath" "testing" ) // TestFindNextRoomIDSkipsIDsInOtherDirectories: the in-game allocator must // check every room ID in the world, not just the current room's directory. // A directory-scoped scan hands out an ID that already exists elsewhere, and // World.AddRoomPath then silently shadows that room. func TestFindNextRoomIDSkipsIDsInOtherDirectories(t *testing.T) { root := t.TempDir() intro := filepath.Join(root, "intro") arena := filepath.Join(root, "arena") for _, dir := range []string{intro, arena} { if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } } // intro owns 1001 and 1002; arena owns 1003. for _, id := range []string{"1001", "1002"} { if err := os.WriteFile(filepath.Join(intro, id+".yaml"), []byte("name: intro\n"), 0o644); err != nil { t.Fatal(err) } } if err := os.WriteFile(filepath.Join(arena, "1003.yaml"), []byte("name: arena\n"), 0o644); err != nil { t.Fatal(err) } globallyUsed := map[int]bool{1001: true, 1002: true, 1003: true} got, err := findNextRoomID(intro, globallyUsed) if err != nil { t.Fatalf("findNextRoomID: %v", err) } if got != 1004 { t.Fatalf("findNextRoomID = %d, want 1004 (1003 is used by arena)", got) } } // TestFindNextRoomIDEmptyDirectoryStartsAtOne: an empty directory still // allocates the globally lowest free ID rather than blindly returning 1. func TestFindNextRoomIDEmptyDirectoryStartsAtOne(t *testing.T) { root := t.TempDir() empty := filepath.Join(root, "empty") if err := os.MkdirAll(empty, 0o755); err != nil { t.Fatal(err) } got, err := findNextRoomID(empty, map[int]bool{1: true, 2: true}) if err != nil { t.Fatalf("findNextRoomID: %v", err) } if got != 3 { t.Fatalf("findNextRoomID = %d, want 3", got) } }