Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

test: assert on serialized contents through a narrowing helper

Mohil-Ahuja9d410fec3d01
diff --git a/tests/ci/models/test_google_serializer.py b/tests/ci/models/test_google_serializer.py
index 9418ddac0..2816bd6ae 100644
--- a/tests/ci/models/test_google_serializer.py
+++ b/tests/ci/models/test_google_serializer.py
@@ -1,9 +1,26 @@
"""Regression tests for GoogleMessageSerializer's handling of system messages."""
+from google.genai.types import Content, ContentListUnion, Part
+
from browser_use.llm.google.serializer import GoogleMessageSerializer
from browser_use.llm.messages import AssistantMessage, SystemMessage, UserMessage
+def _role_and_text(contents: ContentListUnion) -> list[tuple[str | None, str | None]]:
+ """Flatten serialized contents into (role, first part text) pairs for assertions."""
+ assert isinstance(contents, list)
+
+ pairs: list[tuple[str | None, str | None]] = []
+ for content in contents:
+ assert isinstance(content, Content)
+ parts = content.parts
+ assert parts is not None
+ assert isinstance(parts[0], Part)
+ pairs.append((content.role, parts[0].text))
+
+ return pairs
+
+
def test_single_system_message_becomes_the_system_instruction():
"""The common case must keep returning the system text verbatim."""
contents, system_instruction = GoogleMessageSerializer.serialize_messages(
@@ -11,7 +28,7 @@ def test_single_system_message_becomes_the_system_instruction():
)
assert system_instruction == 'Follow the base system rule.'
- assert len(contents) == 1
+ assert _role_and_text(contents) == [('user', 'Continue the task.')]
def test_all_system_messages_reach_the_system_instruction():
@@ -25,8 +42,7 @@ def test_all_system_messages_reach_the_system_instruction():
)
assert system_instruction == 'Follow the base system rule.\n\nAlso follow the additional system rule.'
- assert len(contents) == 1
- assert contents[0].parts[0].text == 'Continue the task.' # type: ignore[union-attr]
+ assert _role_and_text(contents) == [('user', 'Continue the task.')]
def test_system_text_is_prepended_even_when_an_assistant_message_comes_first():
@@ -41,8 +57,10 @@ def test_system_text_is_prepended_even_when_an_assistant_message_comes_first():
)
assert system_instruction is None
- assert [content.role for content in contents] == ['model', 'user'] # type: ignore[union-attr]
- assert contents[1].parts[0].text == 'Follow the system rule.\n\nContinue the task.' # type: ignore[union-attr]
+ assert _role_and_text(contents) == [
+ ('model', 'Earlier assistant turn.'),
+ ('user', 'Follow the system rule.\n\nContinue the task.'),
+ ]
def test_system_text_falls_back_to_the_instruction_when_there_is_no_user_message():