test: consolidate repetitive cases across the suite

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-25 14:28:21 +08:00
1 parent c406024b59
commit 6eccc2859a
31 files changed
+745 -1315

No files matched your search

+10 -29
View File
@@ -465,39 +465,20 @@ async def test_loop_stream_filter_handles_think_only_prefix_without_crashing(tmp
@pytest.mark.asyncio
async def test_loop_stream_filter_hides_partial_trailing_think_prefix(tmp_path):
@pytest.mark.parametrize(
"first_delta, second_delta",
[
pytest.param("Hello <thin", "k>hidden</think>World", id="partial_trailing_think_prefix"),
pytest.param("Hello <think>", "hidden</think>World", id="complete_trailing_think_tag"),
],
)
async def test_loop_stream_filter_hides_split_think_tags(tmp_path, first_delta, second_delta):
loop = _make_loop(tmp_path)
deltas: list[str] = []
async def chat_stream_with_retry(*, on_content_delta, **kwargs):
await on_content_delta("Hello <thin")
await on_content_delta("k>hidden</think>World")
return LLMResponse(content="Hello <think>hidden</think>World", tool_calls=[], usage=None)
loop.provider.chat_stream_with_retry = chat_stream_with_retry
async def on_stream(delta: str) -> None:
deltas.append(delta)
result = await loop._run_agent_loop(
TranscriptInput(history=[], current_message=None),
runtime=loop.llm_runtime(),
events=output_events(on_stream=on_stream),
streaming=True,
)
assert result.final_content == "Hello World"
assert deltas == ["Hello", " World"]
@pytest.mark.asyncio
async def test_loop_stream_filter_hides_complete_trailing_think_tag(tmp_path):
loop = _make_loop(tmp_path)
deltas: list[str] = []
async def chat_stream_with_retry(*, on_content_delta, **kwargs):
await on_content_delta("Hello <think>")
await on_content_delta("hidden</think>World")
await on_content_delta(first_delta)
await on_content_delta(second_delta)
return LLMResponse(content="Hello <think>hidden</think>World", tool_calls=[], usage=None)
loop.provider.chat_stream_with_retry = chat_stream_with_retry
+15 -24
View File
@@ -71,33 +71,24 @@ class TestHistoryWithCursor:
cursor = store.append_history("event 3")
assert cursor == 3
def test_append_history_strips_thinking_content(self, store):
"""`strip_think` must run before persistence — well-formed thinking
blocks shouldn't land in history."""
cursor = store.append_history("<think>reasoning</think>final answer")
@pytest.mark.parametrize(
"input_text, expected_content",
[
pytest.param(
"<think>reasoning</think>final answer",
"final answer",
id="strips_thinking_content",
),
pytest.param("<think>nothing user-facing</think>", "", id="drops_pure_leak_content"),
pytest.param("<channel|>", "", id="drops_malformed_leak_prefix"),
],
)
def test_append_history_sanitizes_thinking(self, store, input_text, expected_content):
cursor = store.append_history(input_text)
content = store.read_file(store.history_file)
data = json.loads(content)
assert data["cursor"] == cursor
assert data["content"] == "final answer"
def test_append_history_drops_pure_leak_content(self, store):
"""Regression: entries that strip down to empty (pure template-token
leak) must NOT fall back to the raw leak. Persisting the raw text
would re-pollute context via consolidation / replay, undoing the
protection `strip_think` provides."""
cursor = store.append_history("<think>nothing user-facing</think>")
content = store.read_file(store.history_file)
data = json.loads(content)
assert data["cursor"] == cursor
assert data["content"] == ""
def test_append_history_drops_malformed_leak_prefix(self, store):
"""Channel-marker / malformed opening leaks should not survive."""
cursor = store.append_history("<channel|>")
content = store.read_file(store.history_file)
data = json.loads(content)
assert data["cursor"] == cursor
assert data["content"] == ""
assert data["content"] == expected_content
def test_read_unprocessed_history(self, store):
store.append_history("event 1")
+32 -51
View File
@@ -8,6 +8,7 @@ from pathlib import Path
from types import SimpleNamespace
from typing import Any, cast
import pytest
from pydantic import BaseModel, Field
from nanobot.cli import onboard as onboard_wizard
@@ -240,32 +241,20 @@ class TestGetFieldDisplayName:
name = _get_field_display_name("user_name", field_info)
assert name == "User Name"
def test_adds_url_suffix(self):
@pytest.mark.parametrize(
"field_name, suffix, prefix",
[
pytest.param("api_url", "Url", "Api", id="url_suffix"),
pytest.param("file_path", "Path", "File", id="path_suffix"),
pytest.param("user_id", "Id", "User", id="id_suffix"),
pytest.param("api_key", "Key", "Api", id="key_suffix"),
pytest.param("auth_token", "Token", "Auth", id="token_suffix"),
],
)
def test_title_cases_field_name_parts(self, field_name, suffix, prefix):
field_info = SimpleNamespace(description=None)
name = _get_field_display_name("api_url", field_info)
# Title case: "Api Url"
assert "Url" in name and "Api" in name
def test_adds_path_suffix(self):
field_info = SimpleNamespace(description=None)
name = _get_field_display_name("file_path", field_info)
assert "Path" in name and "File" in name
def test_adds_id_suffix(self):
field_info = SimpleNamespace(description=None)
name = _get_field_display_name("user_id", field_info)
# Title case: "User Id"
assert "Id" in name and "User" in name
def test_adds_key_suffix(self):
field_info = SimpleNamespace(description=None)
name = _get_field_display_name("api_key", field_info)
assert "Key" in name and "Api" in name
def test_adds_token_suffix(self):
field_info = SimpleNamespace(description=None)
name = _get_field_display_name("auth_token", field_info)
assert "Token" in name and "Auth" in name
name = _get_field_display_name(field_name, field_info)
assert suffix in name and prefix in name
def test_adds_seconds_suffix(self):
field_info = SimpleNamespace(description=None)
@@ -462,9 +451,16 @@ class TestConfigurePydanticModelDrafts:
onboard_wizard, "_input_with_existing", lambda *_args, **_kwargs: text_value
)
def test_back_commits_section_draft(self, monkeypatch):
@pytest.mark.parametrize(
"exit_action",
[
pytest.param("back", id="back_commits_section_draft"),
pytest.param("done", id="completing_section_returns_updated_draft"),
],
)
def test_leaving_section_commits_draft(self, monkeypatch, exit_action):
model = _SimpleDraftModel()
self._patch_prompt_helpers(monkeypatch, ["first", "back"])
self._patch_prompt_helpers(monkeypatch, ["first", exit_action])
result = _configure_pydantic_model(model, "Simple")
@@ -482,31 +478,16 @@ class TestConfigurePydanticModelDrafts:
assert result is None
assert model.api_key == ""
def test_completing_section_returns_updated_draft(self, monkeypatch):
model = _SimpleDraftModel()
self._patch_prompt_helpers(monkeypatch, ["first", "done"])
result = _configure_pydantic_model(model, "Simple")
assert result is not None
updated = cast(_SimpleDraftModel, result)
assert updated.api_key == "secret"
assert model.api_key == ""
def test_nested_section_back_commits_nested_edits(self, monkeypatch):
@pytest.mark.parametrize(
"exit_action",
[
pytest.param("back", id="back_commits_nested_edits"),
pytest.param("done", id="done_commits_nested_edits"),
],
)
def test_leaving_nested_section_commits_edits(self, monkeypatch, exit_action):
model = _OuterDraftModel()
self._patch_prompt_helpers(monkeypatch, ["first", "first", "back", "done"])
result = _configure_pydantic_model(model, "Outer")
assert result is not None
updated = cast(_OuterDraftModel, result)
assert updated.nested.api_key == "secret"
assert model.nested.api_key == ""
def test_nested_section_done_commits_nested_edits(self, monkeypatch):
model = _OuterDraftModel()
self._patch_prompt_helpers(monkeypatch, ["first", "first", "done", "done"])
self._patch_prompt_helpers(monkeypatch, ["first", "first", exit_action, "done"])
result = _configure_pydantic_model(model, "Outer")
+10 -17
View File
@@ -481,24 +481,17 @@ class TestModifyOpen:
assert tool._runtime_control.snapshot().workspace == "/new/path"
@pytest.mark.asyncio
async def test_modify_pending_queues_blocked(self):
"""_pending_queues controls message routing — must be blocked."""
@pytest.mark.parametrize(
"key",
[
pytest.param("_pending_queues", id="pending_queues_blocked"),
pytest.param("_session_locks", id="session_locks_blocked"),
pytest.param("_active_tasks", id="active_tasks_blocked"),
],
)
async def test_modify_runtime_coordination_state_blocked(self, key):
tool = _make_tool()
result = await tool.execute(action="set", key="_pending_queues", value={})
assert "protected" in result
@pytest.mark.asyncio
async def test_modify_session_locks_blocked(self):
"""_session_locks controls session isolation — must be blocked."""
tool = _make_tool()
result = await tool.execute(action="set", key="_session_locks", value={})
assert "protected" in result
@pytest.mark.asyncio
async def test_modify_active_tasks_blocked(self):
"""_active_tasks tracks running tasks — must be blocked."""
tool = _make_tool()
result = await tool.execute(action="set", key="_active_tasks", value={})
result = await tool.execute(action="set", key=key, value={})
assert "protected" in result
@pytest.mark.asyncio