"""An answer that is no longer wanted stops at the engine's next model or tool call (turn_state.cancel). The voice worker sets it when the app takes the request back (the asker went on talking and the request is joined to what they said next, a newer request took its place, or a stop). The engine then returns no answer and runs no tool on the request's behalf. """ from __future__ import annotations from unittest.mock import patch import pytest def _native_call(name: str, args: dict) -> dict: return {"message": {"role": "assistant", "content": "", "tool_calls": [ {"id": "call_1", "type": "function", "function": {"name": name, "arguments": args}}]}} def _run(mock_config, db, dialogue_memory, cancel_after_calls: int): from jarvis.memory.history import HistoryContext from jarvis.reply import engine as engine_mod from jarvis.reply import turn_state from jarvis.tools.types import ToolExecutionResult mock_config.ollama_chat_model = "gpt-oss:20b" mock_config.evaluator_enabled = False mock_config.stream_browser = False calls = [] invoked = [] def fake_chat(*args, **kwargs): calls.append(1) return _native_call("getWeather", {"location": "Seoul"}) def fake_tool(db, cfg, tool_name, tool_args, **kwargs): invoked.append(tool_name) return ToolExecutionResult(success=True, reply_text="Seoul: 13C", error_message=None) turn_state.reset() turn_state.set_cancel(lambda: len(calls) >= cancel_after_calls) try: with patch.object(engine_mod, "run_tool_with_retries", side_effect=fake_tool), \ patch.object(engine_mod, "chat_with_messages", side_effect=fake_chat), \ patch.object(engine_mod, "select_tools", return_value=["getWeather", "stop"]), \ patch.object(engine_mod, "plan_query", return_value=["Reply to the user."]): reply = engine_mod.run_reply_engine( db=db, cfg=mock_config, tts=None, text="서울 날씨 어때?", dialogue_memory=dialogue_memory, history=HistoryContext(), legacy_memory=False, intent_judge=True) finally: turn_state.reset() return reply, len(calls), invoked @pytest.mark.unit def test_an_answer_cancelled_before_it_starts_makes_no_model_call(mock_config, db, dialogue_memory): reply, calls, invoked = _run(mock_config, db, dialogue_memory, cancel_after_calls=0) assert reply is None and calls == 0 and invoked == [] @pytest.mark.unit def test_an_answer_cancelled_during_a_model_call_runs_no_tool_for_it(mock_config, db, dialogue_memory): # The model asks for a tool, but the answer was taken back meanwhile: the tool is not run. reply, calls, invoked = _run(mock_config, db, dialogue_memory, cancel_after_calls=1) assert reply is None and calls == 1 and invoked == []