@@ -637,6 +637,51 @@ async def _test():
637637
638638 anyio .run (_test )
639639
640+ def test_stop_task (self ):
641+ """Test stop_task sends correct control request with task_id."""
642+
643+ async def _test ():
644+ with patch (
645+ "claude_agent_sdk._internal.transport.subprocess_cli.SubprocessCLITransport"
646+ ) as mock_transport_class :
647+ mock_transport = _create_mock_transport_with_control_responses ()
648+ mock_transport_class .return_value = mock_transport
649+
650+ async with ClaudeSDKClient () as client :
651+ await client .stop_task ("task-abc123" )
652+ # Check that a control request was sent via write
653+ write_calls = mock_transport .write .call_args_list
654+ request_found = False
655+ for call in write_calls :
656+ data = call [0 ][0 ]
657+ try :
658+ msg = json .loads (data .strip ())
659+ req = msg .get ("request" , {})
660+ if (
661+ msg .get ("type" ) == "control_request"
662+ and req .get ("subtype" ) == "stop_task"
663+ ):
664+ assert req .get ("task_id" ) == "task-abc123"
665+ request_found = True
666+ break
667+ except (json .JSONDecodeError , KeyError , AttributeError ):
668+ pass
669+ assert request_found , (
670+ "stop_task control request with task_id not found"
671+ )
672+
673+ anyio .run (_test )
674+
675+ def test_stop_task_not_connected (self ):
676+ """Test stop_task when not connected raises error."""
677+
678+ async def _test ():
679+ client = ClaudeSDKClient ()
680+ with pytest .raises (CLIConnectionError , match = "Not connected" ):
681+ await client .stop_task ("task-abc123" )
682+
683+ anyio .run (_test )
684+
640685 def test_client_with_options (self ):
641686 """Test client initialization with options."""
642687
0 commit comments