|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +import pytest |
| 6 | +from republic import ToolContext |
| 7 | + |
| 8 | +import bub.builtin.tools as builtin_tools |
| 9 | +from bub.builtin.tools import tape_search |
| 10 | + |
| 11 | + |
| 12 | +@dataclass(frozen=True) |
| 13 | +class _FakeEntry: |
| 14 | + date: str |
| 15 | + payload: object |
| 16 | + |
| 17 | + |
| 18 | +class _FakeTapes: |
| 19 | + def __init__(self, entries: list[_FakeEntry]) -> None: |
| 20 | + self._entries = entries |
| 21 | + self._store = object() |
| 22 | + |
| 23 | + async def search(self, _query: object) -> list[_FakeEntry]: |
| 24 | + return list(self._entries) |
| 25 | + |
| 26 | + |
| 27 | +class _FakeAgent: |
| 28 | + def __init__(self, entries: list[_FakeEntry]) -> None: |
| 29 | + self.tapes = _FakeTapes(entries) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.asyncio |
| 33 | +async def test_tape_search_reports_shown_matches_and_filtered_count(monkeypatch) -> None: |
| 34 | + entries = [ |
| 35 | + _FakeEntry(date="2026-01-01T00:00:00Z", payload={"content": "ok"}), |
| 36 | + _FakeEntry(date="2026-01-01T00:00:01Z", payload={"content": "[tape.search]: 1 matches"}), |
| 37 | + ] |
| 38 | + monkeypatch.setattr(builtin_tools, "_get_agent", lambda _context: _FakeAgent(entries)) |
| 39 | + |
| 40 | + output = await tape_search.run(query="x", context=ToolContext(tape="tape", run_id="run", state={})) |
| 41 | + |
| 42 | + assert output.splitlines()[0] == "[tape.search]: 1 matches (1 filtered)" |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_tape_search_reports_zero_filtered_explicitly(monkeypatch) -> None: |
| 47 | + entries = [ |
| 48 | + _FakeEntry(date="2026-01-01T00:00:00Z", payload={"content": "a"}), |
| 49 | + _FakeEntry(date="2026-01-01T00:00:01Z", payload={"content": "b"}), |
| 50 | + ] |
| 51 | + monkeypatch.setattr(builtin_tools, "_get_agent", lambda _context: _FakeAgent(entries)) |
| 52 | + |
| 53 | + output = await tape_search.run(query="x", context=ToolContext(tape="tape", run_id="run", state={})) |
| 54 | + |
| 55 | + assert output.splitlines()[0] == "[tape.search]: 2 matches (0 filtered)" |
0 commit comments