test: replace false-green checks with behavioral coverage (#6282)
* test: replace false-green checks with behavioral coverage * test: complete behavior-focused audit coverage
This commit is contained in:
1 parent
c7ab7aa6da
commit
74942bd190
91 files changed
+1891
-1391
No files matched your search
@@ -147,6 +147,7 @@ class TestInspectPathNavigation:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_inspect_config_subfield(self):
|
||||
"""Positive control for sensitive-field blocking: ordinary config stays inspectable."""
|
||||
loop = _make_mock_loop()
|
||||
tool = _make_tool(loop=loop)
|
||||
result = await tool.execute(action="check", key="web_config.enable")
|
||||
@@ -649,60 +650,6 @@ class TestSubagentHookStatus:
|
||||
assert context.iteration == 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Checkpoint callback updates status
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestCheckpointCallback:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_checkpoint_updates_phase_and_iteration(self):
|
||||
"""The _on_checkpoint callback should update status.phase and iteration."""
|
||||
|
||||
from nanobot.agent.subagent import SubagentStatus
|
||||
|
||||
status = SubagentStatus(
|
||||
task_id="cp",
|
||||
label="test",
|
||||
task_description="test",
|
||||
started_at=time.monotonic(),
|
||||
)
|
||||
|
||||
# Simulate the checkpoint callback as defined in _run_subagent
|
||||
async def _on_checkpoint(payload: dict) -> None:
|
||||
status.phase = payload.get("phase", status.phase)
|
||||
status.iteration = payload.get("iteration", status.iteration)
|
||||
|
||||
await _on_checkpoint({"phase": "awaiting_tools", "iteration": 2})
|
||||
assert status.phase == "awaiting_tools"
|
||||
assert status.iteration == 2
|
||||
|
||||
await _on_checkpoint({"phase": "tools_completed", "iteration": 3})
|
||||
assert status.phase == "tools_completed"
|
||||
assert status.iteration == 3
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_checkpoint_preserves_phase_on_missing_key(self):
|
||||
"""If payload doesn't have 'phase', status.phase should stay unchanged."""
|
||||
from nanobot.agent.subagent import SubagentStatus
|
||||
|
||||
status = SubagentStatus(
|
||||
task_id="cp",
|
||||
label="test",
|
||||
task_description="test",
|
||||
started_at=time.monotonic(),
|
||||
phase="initializing",
|
||||
)
|
||||
|
||||
async def _on_checkpoint(payload: dict) -> None:
|
||||
status.phase = payload.get("phase", status.phase)
|
||||
status.iteration = payload.get("iteration", status.iteration)
|
||||
|
||||
await _on_checkpoint({"iteration": 1})
|
||||
assert status.phase == "initializing"
|
||||
assert status.iteration == 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# read-only mode (tools.my.allow_set=False)
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -819,13 +766,6 @@ class TestSensitiveSubFieldBlocking:
|
||||
result = await tool.execute(action="set", key="some_config.password", value="evil")
|
||||
assert "not accessible" in result
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_inspect_non_sensitive_subfield_allowed(self):
|
||||
"""web_config.enable should still be inspectable."""
|
||||
tool = _make_tool()
|
||||
result = await tool.execute(action="check", key="web_config.enable")
|
||||
assert "True" in result
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_modify_sensitive_top_level_blocked(self):
|
||||
"""Top-level key matching sensitive name must be blocked for set."""
|
||||
|
||||
@@ -110,19 +110,13 @@ async def test_subagent_exec_tool_receives_allowed_env_keys(tmp_path):
|
||||
)
|
||||
mgr._announce_result = AsyncMock()
|
||||
|
||||
async def fake_run(spec):
|
||||
exec_tool = spec.tools.get("exec")
|
||||
assert exec_tool is not None
|
||||
assert exec_tool.allowed_env_keys == ["GOPATH", "JAVA_HOME"]
|
||||
return AgentRunResult(
|
||||
messages=[],
|
||||
stop_reason="done",
|
||||
final_content="done",
|
||||
error=None,
|
||||
tool_events=[],
|
||||
)
|
||||
|
||||
mgr.runner.run = AsyncMock(side_effect=fake_run)
|
||||
mgr.runner.run = AsyncMock(return_value=AgentRunResult(
|
||||
messages=[],
|
||||
stop_reason="done",
|
||||
final_content="done",
|
||||
error=None,
|
||||
tool_events=[],
|
||||
))
|
||||
|
||||
await mgr.spawn(
|
||||
task="do task",
|
||||
@@ -134,6 +128,11 @@ async def test_subagent_exec_tool_receives_allowed_env_keys(tmp_path):
|
||||
await asyncio.gather(*mgr._running_tasks.values(), return_exceptions=True)
|
||||
|
||||
mgr.runner.run.assert_awaited_once()
|
||||
spec = mgr.runner.run.await_args.args[0]
|
||||
exec_tool = spec.tools.get("exec")
|
||||
assert exec_tool is not None
|
||||
assert exec_tool.allowed_env_keys == ["GOPATH", "JAVA_HOME"]
|
||||
assert [s.state for s in mgr.statuses_for_session("test:c1").values()] == ["done"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -154,17 +153,13 @@ async def test_subagent_uses_configured_max_iterations(tmp_path):
|
||||
)
|
||||
mgr._announce_result = AsyncMock()
|
||||
|
||||
async def fake_run(spec):
|
||||
assert spec.max_iterations == 37
|
||||
return AgentRunResult(
|
||||
messages=[],
|
||||
stop_reason="done",
|
||||
final_content="done",
|
||||
error=None,
|
||||
tool_events=[],
|
||||
)
|
||||
|
||||
mgr.runner.run = AsyncMock(side_effect=fake_run)
|
||||
mgr.runner.run = AsyncMock(return_value=AgentRunResult(
|
||||
messages=[],
|
||||
stop_reason="done",
|
||||
final_content="done",
|
||||
error=None,
|
||||
tool_events=[],
|
||||
))
|
||||
|
||||
await mgr.spawn(
|
||||
task="do task",
|
||||
@@ -176,6 +171,55 @@ async def test_subagent_uses_configured_max_iterations(tmp_path):
|
||||
await asyncio.gather(*mgr._running_tasks.values(), return_exceptions=True)
|
||||
|
||||
mgr.runner.run.assert_awaited_once()
|
||||
spec = mgr.runner.run.await_args.args[0]
|
||||
assert spec.max_iterations == 37
|
||||
assert [s.state for s in mgr.statuses_for_session("test:c1").values()] == ["done"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_subagent_checkpoint_updates_status_and_preserves_omitted_fields(tmp_path):
|
||||
"""Runner checkpoints update detached task snapshots without resetting omitted fields."""
|
||||
from nanobot.agent.subagent import SubagentManager
|
||||
from nanobot.bus.queue import MessageBus
|
||||
|
||||
mgr = SubagentManager(
|
||||
consolidator=MagicMock(spec=Consolidator),
|
||||
workspace=tmp_path,
|
||||
bus=MessageBus(),
|
||||
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
|
||||
)
|
||||
mgr._announce_result = AsyncMock()
|
||||
snapshots = []
|
||||
|
||||
async def fake_run(spec):
|
||||
for payload in [
|
||||
{"phase": "awaiting_tools", "iteration": 2},
|
||||
{"phase": "tools_completed", "iteration": 3},
|
||||
{"iteration": 4},
|
||||
{"phase": "final_response"},
|
||||
]:
|
||||
await spec.checkpoint_callback(payload)
|
||||
snapshots.extend(mgr.statuses_for_session("test:c1").values())
|
||||
return AgentRunResult(
|
||||
messages=[],
|
||||
stop_reason="done",
|
||||
final_content="done",
|
||||
error=None,
|
||||
tool_events=[],
|
||||
)
|
||||
|
||||
mgr.runner.run = AsyncMock(side_effect=fake_run)
|
||||
|
||||
await mgr.spawn(task="do task", session_key="test:c1", runtime=_runtime(MagicMock()))
|
||||
await asyncio.gather(*mgr._running_tasks.values(), return_exceptions=True)
|
||||
|
||||
assert [(s.state, s.phase, s.iteration) for s in snapshots] == [
|
||||
("running", "awaiting_tools", 2),
|
||||
("running", "tools_completed", 3),
|
||||
("running", "tools_completed", 4),
|
||||
("running", "final_response", 4),
|
||||
]
|
||||
assert [s.state for s in mgr.statuses_for_session("test:c1").values()] == ["done"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in new issue
Block a user