Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion tests/unit_tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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"
}