"""Unit tests for core.chat conversation primitives.""" from core.chat import ( build_api_kwargs, build_messages_for_api, finalize_response, flush_tool_results, init_state, record_tool_result, ) def test_init_state_has_all_keys(): state = init_state() assert state["messages"] == [] assert state["context_start_index"] == 0 assert state["pending_tool_calls"] == [] assert state["pending_assistant_msg"] is None assert state["submitted_tool_results"] == [] def test_build_messages_for_api_prepends_system_prompt(): state = init_state() state["messages"] = [{"role": "user", "content": "hi"}] out = build_messages_for_api(state, "be helpful") assert out[0] == {"role": "system", "content": "be helpful"} assert out[-1]["content"] == "hi" def test_build_messages_for_api_skips_blank_system(): state = init_state() state["messages"] = [{"role": "user", "content": "hi"}] assert build_messages_for_api(state, " ") == state["messages"] def test_build_messages_for_api_respects_context_start(): state = init_state() state["messages"] = [ {"role": "user", "content": "old"}, {"role": "assistant", "content": "older reply"}, {"role": "user", "content": "new"}, ] state["context_start_index"] = 2 out = build_messages_for_api(state, "") assert out == [{"role": "user", "content": "new"}] def test_build_api_kwargs_defaults(): state = init_state() state["messages"] = [{"role": "user", "content": "hi"}] kwargs = build_api_kwargs( state, system_prompt="", functions_json_str=None, think_level="low", temperature=0.9, max_tokens=512, top_p=0.95, ) assert kwargs["stream"] is True assert kwargs["max_tokens"] == 512 assert kwargs["reasoning_effort"] == "low" assert kwargs["temperature"] == 0.7 assert kwargs["top_p"] == 0.95 assert "extra_body" not in kwargs assert "tools" not in kwargs def test_build_api_kwargs_omits_unset_sampling_knobs(): state = init_state() kwargs = build_api_kwargs( state, system_prompt="", functions_json_str=None, think_level="high", temperature=None, max_tokens=0, top_p=0, ) assert "max_tokens" not in kwargs assert "temperature" not in kwargs assert "top_p" not in kwargs assert "extra_body" not in kwargs def test_build_api_kwargs_sends_explicit_zero_temperature(): """``temperature=0`` is greedy decoding and must reach the wire — only ``temperature=None`` should be treated as "use the server default".""" state = init_state() kwargs = build_api_kwargs( state, system_prompt="", functions_json_str=None, think_level="high", temperature=0, max_tokens=0, top_p=0, ) assert kwargs["temperature"] == 0.0 def test_finalize_response_without_tool_calls(): state = init_state() has_tools, pending = finalize_response( state=state, assistant_content="hello", reasoning_content="", tool_calls_acc=[], ) assert has_tools is False assert pending == [] assert state["messages"] == [{"role": "assistant", "content": "hello"}] def test_finalize_response_with_tool_calls_records_pending(): state = init_state() tc = { "id": "call_1", "type": "function", "function": {"name": "ping", "arguments": "{}"}, } has_tools, pending = finalize_response( state=state, assistant_content="", reasoning_content="", tool_calls_acc=[tc], ) assert has_tools is True assert pending == [tc] assert state["pending_tool_calls"] == [tc] assert state["pending_assistant_msg"]["tool_calls"] == [tc] def test_record_tool_result_then_flush_appends_to_messages(): state = init_state() tc = {"id": "call_1", "type": "function", "function": {"name": "ping", "arguments": "{}"}} record_tool_result(state, tc, "pong") assert state["submitted_tool_results"] == [{ "role": "tool", "tool_call_id": "call_1", "content": "pong", }] flush_tool_results(state) assert state["submitted_tool_results"] == [] assert state["messages"][-1] == { "role": "tool", "tool_call_id": "call_1", "content": "pong", } assert state["pending_assistant_msg"] is None