From bf459d3bb956677947abb483ba11fbfe3138f4db Mon Sep 17 00:00:00 2001 From: Kkkakania <200867803+Kkkakania@users.noreply.github.com> Date: Wed, 17 Jun 2026 23:46:11 +0800 Subject: [PATCH] test: cover tools output formats --- tests/unit_tests/test_cli.py | 63 +++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/test_cli.py b/tests/unit_tests/test_cli.py index ff5e715..689e7eb 100644 --- a/tests/unit_tests/test_cli.py +++ b/tests/unit_tests/test_cli.py @@ -1,12 +1,19 @@ """Tests for CLI argument parsing and validation.""" import argparse +import json from contextlib import redirect_stderr from io import StringIO import pytest -from contextseek.cli.main import build_parser, _positive_int +from contextseek.cli.main import build_parser, run_cli, _positive_int +from contextseek.domain.tools import default_tool_specs + + +class _ToolSpecClient: + def tools(self): + return default_tool_specs() class TestPositiveInt: @@ -48,3 +55,57 @@ def test_k_positive_accepted(self) -> None: ["retrieve", "--scope", "t", "--query", "q", "--k", "5"] ) assert args.k == 5 + + +class TestToolsOutput: + def test_openai_format_outputs_valid_tool_definitions(self, capsys): + assert run_cli(["tools", "--format", "openai"], client=_ToolSpecClient()) == 0 + + payload = json.loads(capsys.readouterr().out) + tool_names = {tool["function"]["name"] for tool in payload} + + assert tool_names == {"retrieve", "expand"} + retrieve_tool = next( + tool["function"] for tool in payload if tool["function"]["name"] == "retrieve" + ) + expand_tool = next( + tool["function"] for tool in payload if tool["function"]["name"] == "expand" + ) + + assert all(tool["type"] == "function" for tool in payload) + assert retrieve_tool["parameters"]["type"] == "object" + assert retrieve_tool["parameters"]["required"] == ["query", "scope"] + assert set(retrieve_tool["parameters"]["properties"]) == { + "query", + "scope", + "k", + "full", + } + assert expand_tool["parameters"]["required"] == ["ids", "scope"] + assert expand_tool["parameters"]["properties"]["ids"]["type"] == "array" + + def test_anthropic_format_outputs_valid_tool_definitions(self, capsys): + assert ( + run_cli(["tools", "--format", "anthropic"], client=_ToolSpecClient()) == 0 + ) + + payload = json.loads(capsys.readouterr().out) + tool_names = {tool["name"] for tool in payload} + + assert tool_names == {"retrieve", "expand"} + retrieve_tool = next(tool for tool in payload if tool["name"] == "retrieve") + expand_tool = next(tool for tool in payload if tool["name"] == "expand") + + assert all("function" not in tool for tool in payload) + assert retrieve_tool["input_schema"]["type"] == "object" + assert retrieve_tool["input_schema"]["required"] == ["query", "scope"] + assert set(retrieve_tool["input_schema"]["properties"]) == { + "query", + "scope", + "k", + "full", + } + assert expand_tool["input_schema"]["required"] == ["ids", "scope"] + assert expand_tool["input_schema"]["properties"]["ids"]["items"] == { + "type": "string" + }