wip test stabalization is a mess still

This commit is contained in:
2026-03-03 23:53:53 -05:00
parent c0a8777204
commit 3203891b79
17 changed files with 263 additions and 422 deletions

View File

@@ -3,6 +3,7 @@ from unittest.mock import patch, MagicMock
import json
import sys
import os
import subprocess
# Ensure the project root is in sys.path to resolve imports correctly
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
@@ -46,10 +47,8 @@ class TestGeminiCliAdapterParity(unittest.TestCase):
as this functionality is no longer supported via CLI flags.
"""
process_mock = MagicMock()
mock_stdout_content = [json.dumps({"type": "result", "usage": {}}) + "\n", ""]
process_mock.stdout.readline.side_effect = mock_stdout_content
process_mock.stderr.read.return_value = ""
process_mock.poll.return_value = 0
jsonl_output = json.dumps({"type": "result", "usage": {}}) + "\n"
process_mock.communicate.return_value = (jsonl_output, "")
mock_popen.return_value = process_mock
message_content = "User's prompt here."
safety_settings = [
@@ -58,13 +57,13 @@ class TestGeminiCliAdapterParity(unittest.TestCase):
]
self.adapter.send(message=message_content, safety_settings=safety_settings)
args, kwargs = mock_popen.call_args
command = args[0]
cmd_list = args[0]
# Verify that no --safety flags were added to the command
self.assertNotIn("--safety", command)
# Verify that the message was passed correctly via stdin
# We might need to wait a tiny bit for the thread, or just check if it was called
# In most cases it will be called by the time send() returns because of wait()
process_mock.stdin.write.assert_called_with(message_content)
for part in cmd_list:
self.assertNotIn("--safety", part)
# Verify that the message was passed correctly via communicate
process_mock.communicate.assert_called_with(input=message_content)
@patch('subprocess.Popen')
def test_send_without_safety_settings_no_flags(self, mock_popen: MagicMock) -> None:
@@ -72,22 +71,20 @@ class TestGeminiCliAdapterParity(unittest.TestCase):
Test that when safety_settings is None or an empty list, no --safety flags are added.
"""
process_mock = MagicMock()
mock_stdout_content = [json.dumps({"type": "result", "usage": {}}) + "\n", ""]
process_mock.stdout.readline.side_effect = mock_stdout_content
process_mock.stderr.read.return_value = ""
process_mock.poll.return_value = 0
jsonl_output = json.dumps({"type": "result", "usage": {}}) + "\n"
process_mock.communicate.return_value = (jsonl_output, "")
mock_popen.return_value = process_mock
message_content = "Another prompt."
self.adapter.send(message=message_content, safety_settings=None)
args_none, _ = mock_popen.call_args
self.assertNotIn("--safety", args_none[0])
mock_popen.reset_mock()
for part in args_none[0]:
self.assertNotIn("--safety", part)
# Reset side effects for the second call
process_mock.stdout.readline.side_effect = [json.dumps({"type": "result", "usage": {}}) + "\n", ""]
mock_popen.reset_mock()
self.adapter.send(message=message_content, safety_settings=[])
args_empty, _ = mock_popen.call_args
self.assertNotIn("--safety", args_empty[0])
for part in args_empty[0]:
self.assertNotIn("--safety", part)
@patch('subprocess.Popen')
def test_send_with_system_instruction_prepended_to_stdin(self, mock_popen: MagicMock) -> None:
@@ -96,21 +93,20 @@ class TestGeminiCliAdapterParity(unittest.TestCase):
sent via stdin, and does NOT add a --system flag to the command.
"""
process_mock = MagicMock()
mock_stdout_content = [json.dumps({"type": "result", "usage": {}}) + "\n", ""]
process_mock.stdout.readline.side_effect = mock_stdout_content
process_mock.stderr.read.return_value = ""
process_mock.poll.return_value = 0
jsonl_output = json.dumps({"type": "result", "usage": {}}) + "\n"
process_mock.communicate.return_value = (jsonl_output, "")
mock_popen.return_value = process_mock
message_content = "User's prompt here."
system_instruction_text = "Some instruction"
expected_input = f"{system_instruction_text}\n\n{message_content}"
self.adapter.send(message=message_content, system_instruction=system_instruction_text)
args, kwargs = mock_popen.call_args
command = args[0]
# Verify that the system instruction was prepended to the input sent to write
process_mock.stdin.write.assert_called_with(expected_input)
cmd_list = args[0]
# Verify that the system instruction was prepended to the input sent to communicate
process_mock.communicate.assert_called_with(input=expected_input)
# Verify that no --system flag was added to the command
self.assertNotIn("--system", command)
for part in cmd_list:
self.assertNotIn("--system", part)
@patch('subprocess.Popen')
def test_send_with_model_parameter(self, mock_popen: MagicMock) -> None:
@@ -118,21 +114,19 @@ class TestGeminiCliAdapterParity(unittest.TestCase):
Test that the send method correctly adds the -m <model> flag when a model is specified.
"""
process_mock = MagicMock()
mock_stdout_content = [json.dumps({"type": "result", "usage": {}}) + "\n", ""]
process_mock.stdout.readline.side_effect = mock_stdout_content
process_mock.stderr.read.return_value = ""
process_mock.poll.return_value = 0
jsonl_output = json.dumps({"type": "result", "usage": {}}) + "\n"
process_mock.communicate.return_value = (jsonl_output, "")
mock_popen.return_value = process_mock
message_content = "User's prompt here."
model_name = "gemini-1.5-flash"
expected_command_part = f'-m "{model_name}"'
self.adapter.send(message=message_content, model=model_name)
args, kwargs = mock_popen.call_args
command = args[0]
cmd_list = args[0]
# Verify that the -m <model> flag was added to the command
self.assertIn(expected_command_part, command)
# Verify that the message was passed correctly via stdin
process_mock.stdin.write.assert_called_with(message_content)
self.assertIn("-m", cmd_list)
self.assertIn(model_name, cmd_list)
# Verify that the message was passed correctly via communicate
process_mock.communicate.assert_called_with(input=message_content)
@patch('subprocess.Popen')
def test_send_parses_tool_calls_from_streaming_json(self, mock_popen: MagicMock) -> None:
@@ -140,16 +134,13 @@ class TestGeminiCliAdapterParity(unittest.TestCase):
Test that tool_use messages in the streaming JSON are correctly parsed.
"""
process_mock = MagicMock()
mock_stdout_content = [
json.dumps({"type": "init", "session_id": "session-123"}) + "\n",
json.dumps({"type": "chunk", "text": "I will call a tool. "}) + "\n",
json.dumps({"type": "tool_use", "name": "get_weather", "args": {"location": "London"}, "id": "call-456"}) + "\n",
json.dumps({"type": "result", "usage": {"total_tokens": 100}}) + "\n",
""
]
process_mock.stdout.readline.side_effect = mock_stdout_content
process_mock.stderr.read.return_value = ""
process_mock.poll.return_value = 0
mock_stdout_content = (
json.dumps({"type": "init", "session_id": "session-123"}) + "\n" +
json.dumps({"type": "chunk", "text": "I will call a tool. "}) + "\n" +
json.dumps({"type": "tool_use", "name": "get_weather", "args": {"location": "London"}, "id": "call-456"}) + "\n" +
json.dumps({"type": "result", "usage": {"total_tokens": 100}}) + "\n"
)
process_mock.communicate.return_value = (mock_stdout_content, "")
mock_popen.return_value = process_mock
result = self.adapter.send(message="What is the weather?")