test(ccr): cover the mixed-turn tool-call helpers directly

drop_tool_calls on unrecognized response shapes, tool_call_signature on
dict, reformatted and unparseable OpenAI arguments, and the multiset
semantics of reissues_tool_calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Garm
2026-10-08 13:17:34 +02:00
co-authored by Claude Opus 5.5
parent 636d1c93dd
commit 95dc497557
+64
View File
@@ -2,10 +2,13 @@ from __future__ import annotations
from headroom.ccr.tool_calls import (
CCRToolCall,
drop_tool_calls,
extract_tool_calls,
has_ccr_tool_calls,
parse_ccr_tool_calls,
reissues_tool_calls,
tool_call_id_for_provider,
tool_call_signature,
)
from headroom.ccr.tool_injection import CCR_TOOL_NAME
@@ -114,3 +117,64 @@ def test_tool_call_id_for_provider_models_matching_result_ids() -> None:
== "call_1"
)
assert tool_call_id_for_provider({"id": "tool_1"}, "anthropic") == "tool_1"
def test_drop_tool_calls_removes_only_the_given_calls() -> None:
keep = {"type": "tool_use", "id": "r", "name": CCR_TOOL_NAME}
drop = {"type": "tool_use", "id": "b", "name": "Read"}
anthropic = {"content": [{"type": "text", "text": "hi"}, keep, drop], "stop_reason": "x"}
out = drop_tool_calls(anthropic, "anthropic", [drop])
assert out["content"] == [{"type": "text", "text": "hi"}, keep]
assert out["stop_reason"] == "x"
assert len(anthropic["content"]) == 3
call = {"id": "c", "function": {"name": "Read", "arguments": "{}"}}
openai = {"choices": [{"message": {"tool_calls": [call]}}, {"index": 1}]}
out = drop_tool_calls(openai, "openai", [call])
assert out["choices"] == [{"message": {"tool_calls": []}}, {"index": 1}]
assert openai["choices"][0]["message"]["tool_calls"] == [call]
def test_drop_tool_calls_returns_unrecognized_shapes_unchanged() -> None:
"""Callers detect "cannot drop here" by identity."""
for response, provider in [
({"content": "text"}, "anthropic"),
({"choices": []}, "openai"),
({"choices": [None]}, "openai"),
({"choices": [{"message": None}]}, "openai"),
({"choices": [{"message": {"content": "no tools"}}]}, "openai"),
({"output": []}, "openai_responses"),
({"candidates": []}, "google"),
]:
assert drop_tool_calls(response, provider, []) is response
def test_tool_call_signature_ignores_ids_and_argument_formatting() -> None:
def openai(call_id: str, arguments: object) -> dict:
return {"id": call_id, "function": {"name": "Read", "arguments": arguments}}
assert tool_call_signature(openai("a", '{"p": 1, "q": 2}')) == tool_call_signature(
openai("b", '{"q":2,"p":1}')
)
assert tool_call_signature(openai("a", {"p": 1})) == tool_call_signature(
openai("b", '{"p": 1}')
)
# Unparseable arguments compare as the raw string.
assert tool_call_signature(openai("a", "{oops")) == tool_call_signature(openai("b", "{oops"))
assert tool_call_signature(openai("a", "{oops")) != tool_call_signature(openai("a", "{}"))
def anthropic(call_id: str, value: int) -> dict:
return {"type": "tool_use", "id": call_id, "name": "Read", "input": {"p": value}}
assert tool_call_signature(anthropic("a", 1)) == tool_call_signature(anthropic("b", 1))
assert tool_call_signature(anthropic("a", 1)) != tool_call_signature(anthropic("a", 2))
def test_reissues_tool_calls_is_a_multiset_check() -> None:
def call(path: str) -> dict:
return {"type": "tool_use", "id": path, "name": "Read", "input": {"path": path}}
wanted = [tool_call_signature(call("a")), tool_call_signature(call("a"))]
assert reissues_tool_calls([call("a"), call("a"), call("b")], wanted)
assert not reissues_tool_calls([call("a"), call("b")], wanted)
assert reissues_tool_calls([], [])