Skip to content

Commit 688a003

Browse files
committed
✨ Unit test add: The terminal tool supports password login #1052
1 parent b332204 commit 688a003

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

test/sdk/core/tools/test_terminal_tool.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,76 @@ def test_execute_command_exception(self, terminal_tool, mock_ssh_session):
448448

449449
assert "Error executing command" in result
450450
assert "Send failed" in result
451+
452+
def test_execute_command_prompt_detection_with_no_more_data(self, terminal_tool, mock_ssh_session):
453+
"""Test command execution with prompt detection and no more data after prompt"""
454+
mock_channel = mock_ssh_session["channel"]
455+
456+
# Simulate dynamic recv_ready behavior:
457+
# First call: data available, second call: no more data after prompt detection
458+
mock_channel.recv_ready.side_effect = [True, False]
459+
mock_channel.recv.return_value = b"file1.txt\nfile2.txt\n$ "
460+
461+
with patch.object(terminal_tool, '_clean_output') as mock_clean:
462+
mock_clean.return_value = "cleaned output"
463+
464+
result = terminal_tool._execute_command(mock_channel, "ls", 30)
465+
466+
assert result == "cleaned output"
467+
mock_channel.send.assert_called_with("ls\n")
468+
mock_clean.assert_called_once()
469+
# Verify recv_ready was called multiple times
470+
assert mock_channel.recv_ready.call_count >= 2
471+
472+
def test_execute_command_multiple_prompt_types(self, terminal_tool, mock_ssh_session):
473+
"""Test command execution with different prompt types (# and >)"""
474+
mock_channel = mock_ssh_session["channel"]
475+
476+
# Test with # prompt (root shell)
477+
mock_channel.recv_ready.side_effect = [True, False]
478+
mock_channel.recv.return_value = b"root@server:~# "
479+
480+
with patch.object(terminal_tool, '_clean_output') as mock_clean:
481+
mock_clean.return_value = "cleaned output"
482+
483+
result = terminal_tool._execute_command(mock_channel, "whoami", 30)
484+
485+
assert result == "cleaned output"
486+
mock_channel.send.assert_called_with("whoami\n")
487+
mock_clean.assert_called_once()
488+
489+
def test_execute_command_windows_prompt(self, terminal_tool, mock_ssh_session):
490+
"""Test command execution with Windows prompt (>)"""
491+
mock_channel = mock_ssh_session["channel"]
492+
493+
# Test with > prompt (Windows)
494+
mock_channel.recv_ready.side_effect = [True, False]
495+
mock_channel.recv.return_value = b"C:\\Users\\test> "
496+
497+
with patch.object(terminal_tool, '_clean_output') as mock_clean:
498+
mock_clean.return_value = "cleaned output"
499+
500+
result = terminal_tool._execute_command(mock_channel, "dir", 30)
501+
502+
assert result == "cleaned output"
503+
mock_channel.send.assert_called_with("dir\n")
504+
mock_clean.assert_called_once()
505+
506+
def test_execute_command_no_output_timeout(self, terminal_tool, mock_ssh_session):
507+
"""Test command execution with no output for extended period"""
508+
mock_channel = mock_ssh_session["channel"]
509+
510+
# No data available, should timeout after 2 seconds of no output
511+
mock_channel.recv_ready.return_value = False
512+
513+
with patch('time.time') as mock_time:
514+
# Simulate time progression: start at 0, then 1 second, then 3 seconds (timeout)
515+
mock_time.side_effect = [0, 1, 3]
516+
517+
result = terminal_tool._execute_command(mock_channel, "sleep 10", 30)
518+
519+
# Should return empty or minimal output due to timeout
520+
assert isinstance(result, str)
451521

452522

453523
class TestForwardMethod:

0 commit comments

Comments
 (0)