test: consolidate redundant compaction and tool tests
This commit is contained in:
1 parent
48443ece20
commit
c406024b59
6 files changed
+39
-172
No files matched your search
@@ -259,26 +259,6 @@ class TestAgentLoopTTLParam:
|
|||||||
class TestAutoCompact:
|
class TestAutoCompact:
|
||||||
"""Test the _archive method."""
|
"""Test the _archive method."""
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_is_expired_boundary(self, tmp_path):
|
|
||||||
"""Exactly at TTL boundary should be expired (>= not >)."""
|
|
||||||
loop = _make_loop(tmp_path, session_ttl_minutes=15)
|
|
||||||
ts = datetime.now() - timedelta(minutes=15)
|
|
||||||
assert loop.auto_compact._is_expired(ts) is True
|
|
||||||
ts2 = datetime.now() - timedelta(minutes=14, seconds=59)
|
|
||||||
assert loop.auto_compact._is_expired(ts2) is False
|
|
||||||
await loop.aclose()
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_is_expired_string_timestamp(self, tmp_path):
|
|
||||||
"""_is_expired should parse ISO string timestamps."""
|
|
||||||
loop = _make_loop(tmp_path, session_ttl_minutes=15)
|
|
||||||
ts = (datetime.now() - timedelta(minutes=20)).isoformat()
|
|
||||||
assert loop.auto_compact._is_expired(ts) is True
|
|
||||||
assert loop.auto_compact._is_expired(None) is False
|
|
||||||
assert loop.auto_compact._is_expired("") is False
|
|
||||||
await loop.aclose()
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_check_expired_only_archives_expired_sessions(self, tmp_path):
|
async def test_check_expired_only_archives_expired_sessions(self, tmp_path):
|
||||||
"""With multiple sessions, only the expired one should be archived."""
|
"""With multiple sessions, only the expired one should be archived."""
|
||||||
|
|||||||
@@ -58,45 +58,20 @@ def _add_turns(session: Session, turns: int, *, prefix: str = "msg") -> None:
|
|||||||
session.add_message("assistant", f"{prefix} assistant {i}")
|
session.add_message("assistant", f"{prefix} assistant {i}")
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
def test_default_ttl_disables_idle_compaction():
|
||||||
# __init__
|
sessions = MagicMock(spec=SessionManager)
|
||||||
# ---------------------------------------------------------------------------
|
sessions.list_sessions.return_value = [
|
||||||
|
{"key": "cli:idle", "updated_at": datetime.now() - timedelta(days=365)},
|
||||||
|
]
|
||||||
|
sessions.get_or_create.return_value = _make_session(
|
||||||
|
key="cli:idle", messages=[{"role": "user", "content": "pending"}],
|
||||||
|
)
|
||||||
|
ac = AutoCompact(sessions=sessions, consolidator=MagicMock())
|
||||||
|
schedule = MagicMock(side_effect=lambda pending: pending.close())
|
||||||
|
|
||||||
|
ac.check_expired(schedule, _runtime)
|
||||||
|
|
||||||
class TestInit:
|
assert schedule.call_count == 0
|
||||||
"""Test AutoCompact.__init__ stores constructor arguments correctly."""
|
|
||||||
|
|
||||||
def test_stores_ttl(self):
|
|
||||||
"""_ttl should match session_ttl_minutes argument."""
|
|
||||||
ac = _make_autocompact(ttl=30)
|
|
||||||
assert ac._ttl == 30
|
|
||||||
|
|
||||||
def test_default_ttl_is_zero(self):
|
|
||||||
"""Default TTL should be 0."""
|
|
||||||
ac = _make_autocompact(ttl=0)
|
|
||||||
assert ac._ttl == 0
|
|
||||||
|
|
||||||
def test_archiving_set_is_empty(self):
|
|
||||||
"""_archiving should start as an empty set."""
|
|
||||||
ac = _make_autocompact()
|
|
||||||
assert ac._archiving == set()
|
|
||||||
|
|
||||||
def test_summaries_dict_is_empty(self):
|
|
||||||
"""_summaries should start as an empty dict."""
|
|
||||||
ac = _make_autocompact()
|
|
||||||
assert ac._summaries == {}
|
|
||||||
|
|
||||||
def test_stores_sessions_reference(self):
|
|
||||||
"""sessions attribute should reference the passed SessionManager."""
|
|
||||||
mock_sm = MagicMock(spec=SessionManager)
|
|
||||||
ac = _make_autocompact(sessions=mock_sm)
|
|
||||||
assert ac.sessions is mock_sm
|
|
||||||
|
|
||||||
def test_stores_consolidator_reference(self):
|
|
||||||
"""consolidator attribute should reference the passed Consolidator."""
|
|
||||||
mock_c = MagicMock()
|
|
||||||
ac = _make_autocompact(consolidator=mock_c)
|
|
||||||
assert ac.consolidator is mock_c
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -902,13 +902,6 @@ class TestScratchpadInspection:
|
|||||||
assert "step" in result
|
assert "step" in result
|
||||||
assert "2" in result
|
assert "2" in result
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_inspect_nonexistent_still_returns_not_found(self):
|
|
||||||
tool = _make_tool()
|
|
||||||
result = await tool.execute(action="check", key="never_set_key_xyz")
|
|
||||||
assert "not found" in result
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# sensitive sub-field blocking (Fix #3: API key leak prevention)
|
# sensitive sub-field blocking (Fix #3: API key leak prevention)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -162,64 +162,26 @@ def test_list_cron_job_shows_expression_and_timezone(tmp_path) -> None:
|
|||||||
assert "cron: 0 9 * * 1-5 (America/Denver)" in result
|
assert "cron: 0 9 * * 1-5 (America/Denver)" in result
|
||||||
|
|
||||||
|
|
||||||
def test_list_every_job_shows_human_interval(tmp_path) -> None:
|
@pytest.mark.parametrize(
|
||||||
|
("every_ms", "expected"),
|
||||||
|
[
|
||||||
|
pytest.param(1_800_000, "every 30m", id="minutes"),
|
||||||
|
pytest.param(7_200_000, "every 2h", id="hours"),
|
||||||
|
pytest.param(30_000, "every 30s", id="seconds"),
|
||||||
|
pytest.param(90_000, "every 90s", id="non-minute-seconds"),
|
||||||
|
pytest.param(200, "every 200ms", id="milliseconds"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_list_every_job_shows_human_interval(tmp_path, every_ms, expected) -> None:
|
||||||
tool = _make_tool(tmp_path)
|
tool = _make_tool(tmp_path)
|
||||||
tool._cron.add_job(
|
tool._cron.add_job(
|
||||||
name="Frequent check",
|
name="Frequent check",
|
||||||
schedule=CronSchedule(kind="every", every_ms=1_800_000),
|
schedule=CronSchedule(kind="every", every_ms=every_ms),
|
||||||
message="check",
|
message="check",
|
||||||
**_bound_chat(),
|
**_bound_chat(),
|
||||||
)
|
)
|
||||||
result = tool._list_jobs()
|
result = tool._list_jobs()
|
||||||
assert "every 30m" in result
|
assert expected in result
|
||||||
|
|
||||||
|
|
||||||
def test_list_every_job_hours(tmp_path) -> None:
|
|
||||||
tool = _make_tool(tmp_path)
|
|
||||||
tool._cron.add_job(
|
|
||||||
name="Hourly check",
|
|
||||||
schedule=CronSchedule(kind="every", every_ms=7_200_000),
|
|
||||||
message="check",
|
|
||||||
**_bound_chat(),
|
|
||||||
)
|
|
||||||
result = tool._list_jobs()
|
|
||||||
assert "every 2h" in result
|
|
||||||
|
|
||||||
|
|
||||||
def test_list_every_job_seconds(tmp_path) -> None:
|
|
||||||
tool = _make_tool(tmp_path)
|
|
||||||
tool._cron.add_job(
|
|
||||||
name="Fast check",
|
|
||||||
schedule=CronSchedule(kind="every", every_ms=30_000),
|
|
||||||
message="check",
|
|
||||||
**_bound_chat(),
|
|
||||||
)
|
|
||||||
result = tool._list_jobs()
|
|
||||||
assert "every 30s" in result
|
|
||||||
|
|
||||||
|
|
||||||
def test_list_every_job_non_minute_seconds(tmp_path) -> None:
|
|
||||||
tool = _make_tool(tmp_path)
|
|
||||||
tool._cron.add_job(
|
|
||||||
name="Ninety-second check",
|
|
||||||
schedule=CronSchedule(kind="every", every_ms=90_000),
|
|
||||||
message="check",
|
|
||||||
**_bound_chat(),
|
|
||||||
)
|
|
||||||
result = tool._list_jobs()
|
|
||||||
assert "every 90s" in result
|
|
||||||
|
|
||||||
|
|
||||||
def test_list_every_job_milliseconds(tmp_path) -> None:
|
|
||||||
tool = _make_tool(tmp_path)
|
|
||||||
tool._cron.add_job(
|
|
||||||
name="Sub-second check",
|
|
||||||
schedule=CronSchedule(kind="every", every_ms=200),
|
|
||||||
message="check",
|
|
||||||
**_bound_chat(),
|
|
||||||
)
|
|
||||||
result = tool._list_jobs()
|
|
||||||
assert "every 200ms" in result
|
|
||||||
|
|
||||||
|
|
||||||
def test_list_at_job_shows_iso_timestamp(tmp_path) -> None:
|
def test_list_at_job_shows_iso_timestamp(tmp_path) -> None:
|
||||||
|
|||||||
@@ -159,12 +159,3 @@ async def test_exec_path_append_command_substitution_does_not_execute(tmp_path):
|
|||||||
|
|
||||||
assert "OK" in result
|
assert "OK" in result
|
||||||
assert "SHOULD_NOT_APPEAR" not in result
|
assert "SHOULD_NOT_APPEAR" not in result
|
||||||
|
|
||||||
|
|
||||||
@_UNIX_ONLY
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_exec_path_append_legitimate_path_still_works():
|
|
||||||
"""A normal, safe path_append value must still be appended to PATH."""
|
|
||||||
tool = ExecTool(path_append="/opt/custom/bin")
|
|
||||||
result = await tool.execute(command="echo $PATH")
|
|
||||||
assert "/opt/custom/bin" in result
|
|
||||||
@@ -81,57 +81,23 @@ def test_backtick_url_produces_empty_scheme_in_urlparse():
|
|||||||
# --- WebFetchTool.execute integration tests ---
|
# --- WebFetchTool.execute integration tests ---
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_execute_strips_backticks_and_succeeds():
|
@pytest.mark.parametrize(
|
||||||
|
"url",
|
||||||
|
[
|
||||||
|
pytest.param("`https://example.com/page`", id="backticks"),
|
||||||
|
pytest.param('"https://example.com/page"', id="double-quotes"),
|
||||||
|
pytest.param("'https://example.com/page'", id="single-quotes"),
|
||||||
|
pytest.param(" `https://example.com/page` ", id="space-and-backticks"),
|
||||||
|
pytest.param('"`https://example.com/page`"', id="mixed-markdown-and-quotes"),
|
||||||
|
pytest.param("HTTPS://example.com/page", id="uppercase-scheme"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_execute_accepts_cleanable_http_urls(url):
|
||||||
tool = WebFetchTool()
|
tool = WebFetchTool()
|
||||||
with _patched_web_fetch():
|
with _patched_web_fetch():
|
||||||
result = await tool.execute(url="`https://example.com/page`")
|
result = await tool.execute(url=url)
|
||||||
data = json.loads(result)
|
data = json.loads(result)
|
||||||
assert "error" not in data, f"unexpected error: {data}"
|
assert data["status"] == 200
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_execute_strips_double_quotes_and_succeeds():
|
|
||||||
tool = WebFetchTool()
|
|
||||||
with _patched_web_fetch():
|
|
||||||
result = await tool.execute(url='"https://example.com/page"')
|
|
||||||
data = json.loads(result)
|
|
||||||
assert "error" not in data, f"unexpected error: {data}"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_execute_strips_single_quotes_and_succeeds():
|
|
||||||
tool = WebFetchTool()
|
|
||||||
with _patched_web_fetch():
|
|
||||||
result = await tool.execute(url="'https://example.com/page'")
|
|
||||||
data = json.loads(result)
|
|
||||||
assert "error" not in data, f"unexpected error: {data}"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_execute_strips_space_and_backticks():
|
|
||||||
tool = WebFetchTool()
|
|
||||||
with _patched_web_fetch():
|
|
||||||
result = await tool.execute(url=" `https://example.com/page` ")
|
|
||||||
data = json.loads(result)
|
|
||||||
assert "error" not in data, f"unexpected error: {data}"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_execute_strips_mixed_markdown_and_quotes():
|
|
||||||
tool = WebFetchTool()
|
|
||||||
with _patched_web_fetch():
|
|
||||||
result = await tool.execute(url='"`https://example.com/page`"')
|
|
||||||
data = json.loads(result)
|
|
||||||
assert "error" not in data, f"unexpected error: {data}"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
|
||||||
async def test_execute_keeps_case_insensitive_http_scheme():
|
|
||||||
tool = WebFetchTool()
|
|
||||||
with _patched_web_fetch():
|
|
||||||
result = await tool.execute(url="HTTPS://example.com/page")
|
|
||||||
data = json.loads(result)
|
|
||||||
assert "error" not in data, f"unexpected error: {data}"
|
|
||||||
|
|
||||||
|
|
||||||
# --- startswith guard tests ---
|
# --- startswith guard tests ---
|
||||||
|
|||||||
Reference in new issue
Block a user