|
| 1 | +"""Tests for wait_for_state_messages in the tutorials' test_utils. |
| 2 | +
|
| 3 | +The tutorials' `test_utils` package shares its name with `tests/test_utils`, so the |
| 4 | +module is loaded from its file path instead of imported by name. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import time |
| 10 | +import importlib.util |
| 11 | +from types import ModuleType, SimpleNamespace |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +_ASYNC_UTILS = Path(__file__).resolve().parents[2] / "examples" / "tutorials" / "test_utils" / "async_utils.py" |
| 17 | + |
| 18 | + |
| 19 | +def _load_async_utils() -> ModuleType: |
| 20 | + spec = importlib.util.spec_from_file_location("tutorial_async_utils", _ASYNC_UTILS) |
| 21 | + assert spec is not None and spec.loader is not None |
| 22 | + module = importlib.util.module_from_spec(spec) |
| 23 | + spec.loader.exec_module(module) |
| 24 | + return module |
| 25 | + |
| 26 | + |
| 27 | +wait_for_state_messages = _load_async_utils().wait_for_state_messages |
| 28 | + |
| 29 | + |
| 30 | +class _FakeStates: |
| 31 | + """Returns one state whose message count grows from 1 to 3 once `write_after` seconds pass.""" |
| 32 | + |
| 33 | + def __init__(self, write_after: float | None): |
| 34 | + self._start = time.monotonic() |
| 35 | + self._write_after = write_after |
| 36 | + self.calls = 0 |
| 37 | + |
| 38 | + async def list(self, agent_id: str, task_id: str) -> list[SimpleNamespace]: |
| 39 | + del agent_id, task_id |
| 40 | + self.calls += 1 |
| 41 | + written = self._write_after is not None and time.monotonic() - self._start >= self._write_after |
| 42 | + return [SimpleNamespace(state={"messages": [{"role": "system"}] * (3 if written else 1)})] |
| 43 | + |
| 44 | + |
| 45 | +def _client(write_after: float | None) -> SimpleNamespace: |
| 46 | + return SimpleNamespace(states=_FakeStates(write_after)) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_waits_for_a_state_write_that_lands_after_the_reply() -> None: |
| 51 | + client = _client(write_after=0.3) |
| 52 | + |
| 53 | + messages = await wait_for_state_messages(client, "agent", "task", expected_count=3, timeout=5, sleep_interval=0.05) |
| 54 | + |
| 55 | + assert len(messages) == 3 |
| 56 | + assert client.states.calls > 1 |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_returns_the_real_state_at_the_deadline_when_it_never_updates() -> None: |
| 61 | + client = _client(write_after=None) |
| 62 | + start = time.monotonic() |
| 63 | + |
| 64 | + messages = await wait_for_state_messages(client, "agent", "task", expected_count=3, timeout=0.3, sleep_interval=0.05) |
| 65 | + |
| 66 | + assert len(messages) == 1 |
| 67 | + assert 0.3 <= time.monotonic() - start < 2 |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_returns_on_the_first_poll_when_the_state_is_already_written() -> None: |
| 72 | + client = _client(write_after=0) |
| 73 | + |
| 74 | + messages = await wait_for_state_messages(client, "agent", "task", expected_count=3) |
| 75 | + |
| 76 | + assert len(messages) == 3 |
| 77 | + assert client.states.calls == 1 |
0 commit comments