@@ -25,17 +25,21 @@ def test_initialize_success(self, mock_tmux, mock_wait_status, mock_wait_shell,
2525 """Test successful initialization."""
2626 mock_wait_shell .return_value = True
2727 mock_wait_status .return_value = True
28- # _handle_startup_prompts needs get_history to return a string
29- mock_tmux .get_history .return_value = "Welcome to Claude Code v2.0"
28+ # First call is the pre-launch snapshot, subsequent calls return Claude output
29+ mock_tmux .get_history .side_effect = [
30+ "" ,
31+ "Welcome to Claude Code v2.0" ,
32+ "Welcome to Claude Code v2.0" ,
33+ ]
3034
3135 provider = ClaudeCodeProvider ("test123" , "test-session" , "window-0" )
32- result = provider .initialize ()
36+ with patch .object (provider , "get_status" , return_value = TerminalStatus .IDLE ):
37+ result = provider .initialize ()
3338
3439 assert result is True
3540 assert provider ._initialized is True
3641 mock_wait_shell .assert_called_once ()
3742 mock_tmux .send_keys .assert_called_once ()
38- mock_wait_status .assert_called_once ()
3943
4044 @patch ("cli_agent_orchestrator.providers.claude_code.wait_for_shell" )
4145 @patch ("cli_agent_orchestrator.providers.claude_code.tmux_client" )
@@ -53,15 +57,21 @@ def test_initialize_shell_timeout(self, mock_tmux, mock_wait_shell):
5357 @patch ("cli_agent_orchestrator.providers.claude_code.wait_until_status" )
5458 @patch ("cli_agent_orchestrator.providers.claude_code.tmux_client" )
5559 def test_initialize_timeout (self , mock_tmux , mock_wait_status , mock_wait_shell , _ ):
56- """Test initialization timeout."""
60+ """Test initialization timeout when no Claude markers appear ."""
5761 mock_wait_shell .return_value = True
5862 mock_wait_status .return_value = False
59- mock_tmux .get_history .return_value = "Welcome to Claude Code v2.0"
63+ # Snapshot and loop return the same content → no new Claude markers
64+ mock_tmux .get_history .return_value = "some shell output"
6065
6166 provider = ClaudeCodeProvider ("test123" , "test-session" , "window-0" )
6267
63- with pytest .raises (TimeoutError , match = "Claude Code initialization timed out" ):
64- provider .initialize ()
68+ with (
69+ patch .object (provider , "_handle_startup_prompts" ),
70+ patch ("cli_agent_orchestrator.providers.claude_code.time.time" , side_effect = [0 , 31 ]),
71+ patch ("cli_agent_orchestrator.providers.claude_code.time.sleep" ),
72+ ):
73+ with pytest .raises (TimeoutError , match = "Claude Code initialization timed out" ):
74+ provider .initialize ()
6575
6676 @_PATCH_SETTINGS
6777 @patch ("cli_agent_orchestrator.providers.claude_code.load_agent_profile" )
@@ -74,15 +84,20 @@ def test_initialize_with_agent_profile(
7484 """Test initialization with agent profile."""
7585 mock_wait_shell .return_value = True
7686 mock_wait_status .return_value = True
77- mock_tmux .get_history .return_value = "Welcome to Claude Code v2.0"
87+ mock_tmux .get_history .side_effect = [
88+ "" ,
89+ "Welcome to Claude Code v2.0" ,
90+ "Welcome to Claude Code v2.0" ,
91+ ]
7892 mock_profile = MagicMock ()
7993 mock_profile .model = None
8094 mock_profile .system_prompt = "Test system prompt"
8195 mock_profile .mcpServers = None
8296 mock_load .return_value = mock_profile
8397
8498 provider = ClaudeCodeProvider ("test123" , "test-session" , "window-0" , "test-agent" )
85- result = provider .initialize ()
99+ with patch .object (provider , "get_status" , return_value = TerminalStatus .IDLE ):
100+ result = provider .initialize ()
86101
87102 assert result is True
88103 mock_load .assert_called_once_with ("test-agent" )
@@ -112,15 +127,20 @@ def test_initialize_with_mcp_servers(
112127 """Test initialization with MCP servers in profile."""
113128 mock_wait_shell .return_value = True
114129 mock_wait_status .return_value = True
115- mock_tmux .get_history .return_value = "Welcome to Claude Code v2.0"
130+ mock_tmux .get_history .side_effect = [
131+ "" ,
132+ "Welcome to Claude Code v2.0" ,
133+ "Welcome to Claude Code v2.0" ,
134+ ]
116135 mock_profile = MagicMock ()
117136 mock_profile .model = None
118137 mock_profile .system_prompt = None
119138 mock_profile .mcpServers = {"server1" : {"command" : "test" , "args" : ["--flag" ]}}
120139 mock_load .return_value = mock_profile
121140
122141 provider = ClaudeCodeProvider ("test123" , "test-session" , "window-0" , "test-agent" )
123- result = provider .initialize ()
142+ with patch .object (provider , "get_status" , return_value = TerminalStatus .IDLE ):
143+ result = provider .initialize ()
124144
125145 assert result is True
126146
@@ -132,10 +152,15 @@ def test_initialize_sends_claude_command(self, mock_tmux, mock_wait_status, mock
132152 """Test that initialize sends the 'claude' command to tmux."""
133153 mock_wait_shell .return_value = True
134154 mock_wait_status .return_value = True
135- mock_tmux .get_history .return_value = "Welcome to Claude Code v2.0"
155+ mock_tmux .get_history .side_effect = [
156+ "" ,
157+ "Welcome to Claude Code v2.0" ,
158+ "Welcome to Claude Code v2.0" ,
159+ ]
136160
137161 provider = ClaudeCodeProvider ("test123" , "test-session" , "window-0" )
138- provider .initialize ()
162+ with patch .object (provider , "get_status" , return_value = TerminalStatus .IDLE ):
163+ provider .initialize ()
139164
140165 call_args = mock_tmux .send_keys .call_args
141166 assert call_args [0 ][0 ] == "test-session"
@@ -781,7 +806,8 @@ def test_initialize_calls_handle_startup_prompts(
781806 """Test that initialize calls _handle_startup_prompts."""
782807 mock_wait_shell .return_value = True
783808 mock_wait_status .return_value = True
784- mock_tmux .get_history .return_value = "❯ 1. Yes, I trust this folder\n 2. No"
809+ trust_output = "❯ 1. Yes, I trust this folder\n 2. No"
810+ mock_tmux .get_history .side_effect = ["" , trust_output , trust_output ]
785811 mock_session = MagicMock ()
786812 mock_window = MagicMock ()
787813 mock_pane = MagicMock ()
@@ -790,7 +816,8 @@ def test_initialize_calls_handle_startup_prompts(
790816 mock_window .active_pane = mock_pane
791817
792818 provider = ClaudeCodeProvider ("test123" , "test-session" , "window-0" )
793- result = provider .initialize ()
819+ with patch .object (provider , "get_status" , return_value = TerminalStatus .IDLE ):
820+ result = provider .initialize ()
794821
795822 assert result is True
796823 mock_pane .send_keys .assert_called_with ("" , enter = True )
0 commit comments