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
@@ -22,6 +22,15 @@ from nanobot.webui import session_list_index, transcript
|
||||
from nanobot.webui.recovery import RecoveryActionError, RecoveryCoordinator
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _isolate_webui_transcripts(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Recovery scans must discover only test-owned transcripts, not active user chats."""
|
||||
webui_dir = tmp_path / "isolated-webui"
|
||||
webui_dir.mkdir()
|
||||
monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)
|
||||
monkeypatch.setattr(transcript, "get_webui_dir", lambda: webui_dir)
|
||||
|
||||
|
||||
def _persist(manager: SessionManager, session: Session) -> None:
|
||||
session.metadata["webui"] = True
|
||||
manager.save(session)
|
||||
@@ -732,26 +741,59 @@ async def test_scan_failure_is_visible_instead_of_aborting_other_sessions(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
coordinator, bus, sessions = _coordinator(tmp_path)
|
||||
session = sessions.get_or_create("websocket:chat")
|
||||
session.metadata[PENDING_USER_TURN_KEY] = True
|
||||
_persist(sessions, session)
|
||||
keys = {"websocket:first", "websocket:second"}
|
||||
for key in sorted(keys):
|
||||
session = sessions.get_or_create(key)
|
||||
session.add_message("user", f"finish {key}")
|
||||
session.metadata[PENDING_USER_TURN_KEY] = True
|
||||
_persist(sessions, session)
|
||||
recover_session = RecoveryCoordinator._recover_session
|
||||
attempted = []
|
||||
|
||||
async def fail(*_args: object, **_kwargs: object) -> None:
|
||||
raise RuntimeError("boom")
|
||||
async def fail_first(self, session: Session, chat_id: str) -> None:
|
||||
attempted.append((session.key, chat_id))
|
||||
if len(attempted) == 1:
|
||||
raise RuntimeError("boom")
|
||||
await recover_session(self, session, chat_id)
|
||||
|
||||
monkeypatch.setattr(RecoveryCoordinator, "_recover_session", fail)
|
||||
await coordinator.scan()
|
||||
monkeypatch.setattr(RecoveryCoordinator, "_recover_session", fail_first)
|
||||
try:
|
||||
await coordinator.scan()
|
||||
|
||||
state = sessions.get_or_create("websocket:chat").metadata[RECOVERY_METADATA_KEY]
|
||||
assert state["status"] == "failed"
|
||||
assert state["can_continue"] is False
|
||||
assert isinstance(bus.outbound.get_nowait().event, RecoveryStateEvent)
|
||||
assert len(attempted) == 2
|
||||
assert {key for key, _ in attempted} == keys
|
||||
failed_key, failed_chat = attempted[0]
|
||||
healthy_key, healthy_chat = attempted[1]
|
||||
state = sessions.read_session_snapshot(failed_key).metadata[RECOVERY_METADATA_KEY]
|
||||
assert state["status"] == "failed"
|
||||
assert state["reason"] == "recovery_failed"
|
||||
assert state["can_continue"] is False
|
||||
healthy = sessions.read_session_snapshot(healthy_key)
|
||||
healthy_state = healthy.metadata[RECOVERY_METADATA_KEY]
|
||||
assert healthy_state["status"] == "awaiting_user"
|
||||
assert healthy_state["reason"] == "restart_requires_confirmation"
|
||||
assert healthy.metadata[PENDING_USER_TURN_KEY] is True
|
||||
assert [message["content"] for message in healthy.messages] == [f"finish {healthy_key}"]
|
||||
assert bus.inbound.empty()
|
||||
messages = []
|
||||
while not bus.outbound.empty():
|
||||
messages.append(bus.outbound.get_nowait())
|
||||
recovery_messages = [message for message in messages if isinstance(message.event, RecoveryStateEvent)]
|
||||
assert [(message.channel, message.chat_id, message.event.status, message.event.recovery_id)
|
||||
for message in recovery_messages] == [
|
||||
("websocket", failed_chat, "failed", state["recovery_id"]),
|
||||
("websocket", healthy_chat, "awaiting_user", healthy_state["recovery_id"]),
|
||||
]
|
||||
assert recovery_messages[0].event.can_continue is False
|
||||
assert recovery_messages[1].event.reason == "restart_requires_confirmation"
|
||||
|
||||
with pytest.raises(RecoveryActionError, match="context is unavailable"):
|
||||
await coordinator.handle_action(
|
||||
"continue",
|
||||
{"chat_id": "chat", "recovery_id": state["recovery_id"]},
|
||||
)
|
||||
with pytest.raises(RecoveryActionError, match="context is unavailable"):
|
||||
await coordinator.handle_action(
|
||||
"continue",
|
||||
{"chat_id": failed_chat, "recovery_id": state["recovery_id"]},
|
||||
)
|
||||
finally:
|
||||
await sessions.state.aclose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -293,15 +293,27 @@ async def test_detached_reads_do_not_publish_mutations(tmp_path):
|
||||
async def test_checkpoint_does_not_rewrite_messages(tmp_path):
|
||||
manager = SessionManager(tmp_path)
|
||||
state = manager.state
|
||||
draft = await state.get("chat:a")
|
||||
draft.add_message("user", "hello")
|
||||
await state.prepare_input(draft)
|
||||
path = manager.sessions_dir / "sessions.sqlite3"
|
||||
with sqlite3.connect(path) as db:
|
||||
db.execute("CREATE TRIGGER prohibit_rewrite BEFORE UPDATE ON messages BEGIN SELECT RAISE(ABORT, 'rewritten'); END")
|
||||
await state.checkpoint_view(draft, {"step": 1})
|
||||
assert (await state.get(draft.key)).metadata["runtime_checkpoint"] == {"step": 1}
|
||||
await state.aclose()
|
||||
try:
|
||||
draft = await state.import_messages("chat:a", [
|
||||
{"role": "user", "content": "earlier question"},
|
||||
{"role": "assistant", "content": "earlier answer"},
|
||||
])
|
||||
draft.add_message("user", "hello")
|
||||
await state.prepare_input(draft)
|
||||
accepted = manager.read_session_snapshot(draft.key).messages
|
||||
assert accepted == draft.messages
|
||||
path = manager.sessions_dir / "sessions.sqlite3"
|
||||
with sqlite3.connect(path) as db:
|
||||
for operation in ("INSERT", "UPDATE", "DELETE"):
|
||||
db.execute(f"""CREATE TRIGGER prohibit_{operation.lower()} BEFORE {operation} ON messages
|
||||
BEGIN SELECT RAISE(ABORT, 'rewritten'); END""")
|
||||
await state.checkpoint_view(draft, {"step": 1})
|
||||
finally:
|
||||
await state.aclose()
|
||||
|
||||
stored = manager.read_session_snapshot(draft.key)
|
||||
assert stored.messages == accepted
|
||||
assert stored.metadata["runtime_checkpoint"] == {"step": 1}
|
||||
|
||||
|
||||
async def test_database_lock_does_not_block_event_loop(tmp_path):
|
||||
|
||||
@@ -156,15 +156,6 @@ def test_internal_continuation_requires_budget_boundary_and_queue():
|
||||
)
|
||||
|
||||
|
||||
def test_save_skip_matches_prefix_when_current_message_was_persisted():
|
||||
skip = _save_skip_for_turn(
|
||||
message_metadata=None,
|
||||
initial_message_count=3, # [system, history user, current user]
|
||||
input_persisted_early=True,
|
||||
)
|
||||
assert skip == 3
|
||||
|
||||
|
||||
def test_save_skip_unchanged_for_standalone_current_message():
|
||||
# [system, history user, current user] with the current user already saved.
|
||||
assert _save_skip_for_turn(
|
||||
|
||||
Reference in new issue
Block a user