22
33from __future__ import annotations
44
5+ import time
6+
57
68def test_window_renaming (session ) -> None :
79 """Test renaming windows."""
@@ -28,6 +30,9 @@ def test_window_moving(session) -> None:
2830 session .new_window (window_name = "window-2" )
2931 window3 = session .new_window (window_name = "window-3" )
3032
33+ # Let tmux settle
34+ time .sleep (0.5 )
35+
3136 # Get initial indices
3237 index1 = window1 .index
3338
@@ -41,9 +46,19 @@ def test_window_moving(session) -> None:
4146 # Verify window 1 has moved
4247 assert window1 .index != index1
4348
44- # Move window 3 to index 1
49+ # Get a free index for window 3 to move to
50+ # Note: We can't just use index 1 as it might be taken
51+ all_indices = [int (w .index ) for w in session .windows ]
52+ for i in range (1 , 10 ):
53+ if i not in all_indices :
54+ free_index = str (i )
55+ break
56+ else :
57+ free_index = "10" # Fallback
58+
59+ # Move window 3 to free index
4560 initial_index3 = window3 .index
46- window3 .move_window (destination = "1" )
61+ window3 .move_window (destination = free_index )
4762
4863 # Refresh windows
4964 session .list_windows () # This refreshes the windows
@@ -60,26 +75,46 @@ def test_window_switching(session) -> None:
6075 window2 = session .new_window (window_name = "switch-test-2" )
6176 window3 = session .new_window (window_name = "switch-test-3" )
6277
63- # Verify window 3 is active (most recently created)
64- assert session .active_window .id == window3 .id
78+ # Give tmux time to update
79+ time .sleep (0.5 )
80+
81+ # Refresh session to get current state
82+ session .refresh ()
83+
84+ # Tmux may set any window as active - can vary across platforms
85+ # So first we'll explicitly set window3 as active
86+ session .select_window (window3 .index )
87+ time .sleep (0.5 )
88+ session .refresh ()
89+
90+ # Now verify window 3 is active
91+ assert session .active_window .id == window3 .id , (
92+ f"Expected active window { window3 .id } , got { session .active_window .id } "
93+ )
6594
6695 # Switch to window 1
6796 session .select_window (window1 .index )
97+ time .sleep (0.5 )
6898
6999 # Refresh the session information
70100 session .refresh ()
71101
72102 # Verify window 1 is now active
73- assert session .active_window .id == window1 .id
103+ assert session .active_window .id == window1 .id , (
104+ f"Expected active window { window1 .id } , got { session .active_window .id } "
105+ )
74106
75107 # Switch to window 2 by name
76108 session .select_window ("switch-test-2" )
109+ time .sleep (0.5 )
77110
78111 # Refresh the session information
79112 session .refresh ()
80113
81114 # Verify window 2 is now active
82- assert session .active_window .id == window2 .id
115+ assert session .active_window .id == window2 .id , (
116+ f"Expected active window { window2 .id } , got { session .active_window .id } "
117+ )
83118
84119
85120def test_window_killing (session ) -> None :
0 commit comments