Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/dacli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ def sections_at_level(ctx: CliContext, level: int):
@pass_context
def search(ctx: CliContext, query: str, scope: str | None, max_results: int):
"""Search for content in the documentation."""
# Validate query is not empty
if not query or not query.strip():
click.echo("Error: Search query cannot be empty", err=True)
sys.exit(EXIT_INVALID_ARGS)

results = ctx.index.search(
query=query,
scope=scope,
Expand Down
7 changes: 7 additions & 0 deletions src/dacli/mcp_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,14 @@ def search(
Returns:
Search results with 'query', 'results' (list of matches with
path, line, context, score), and 'total_results'.

Raises:
ValueError: If query is empty or whitespace-only.
"""
# Validate query is not empty
if not query or not query.strip():
raise ValueError("Search query cannot be empty")

results = index.search(
query=query,
scope=scope,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,32 @@ def test_search_max_results_still_works(self, sample_docs):
data = json.loads(result.output)
assert len(data["results"]) <= 1

def test_search_empty_query_returns_error(self, sample_docs):
"""search command should reject empty query with exit code 2."""
from dacli.cli import cli

runner = CliRunner()
result = runner.invoke(
cli,
["--docs-root", str(sample_docs), "search", ""],
)

assert result.exit_code == 2 # EXIT_INVALID_ARGS
assert "Error: Search query cannot be empty" in result.output

def test_search_whitespace_only_query_returns_error(self, sample_docs):
"""search command should reject whitespace-only query with exit code 2."""
from dacli.cli import cli

runner = CliRunner()
result = runner.invoke(
cli,
["--docs-root", str(sample_docs), "search", " "],
)

assert result.exit_code == 2 # EXIT_INVALID_ARGS
assert "Error: Search query cannot be empty" in result.output


class TestCliMetadataCommand:
"""Test the 'metadata' command."""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_mcp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest
import pytest_asyncio
from fastmcp.client import Client
from fastmcp.exceptions import ToolError

from dacli.mcp_app import create_mcp_server

Expand Down Expand Up @@ -214,6 +215,16 @@ async def test_search_with_max_results(self, mcp_client: Client):

assert len(result.data["results"]) <= 1

async def test_search_empty_query_raises_error(self, mcp_client: Client):
"""search should raise ToolError for empty query."""
with pytest.raises(ToolError, match="Search query cannot be empty"):
await mcp_client.call_tool("search", arguments={"query": ""})

async def test_search_whitespace_query_raises_error(self, mcp_client: Client):
"""search should raise ToolError for whitespace-only query."""
with pytest.raises(ToolError, match="Search query cannot be empty"):
await mcp_client.call_tool("search", arguments={"query": " "})


class TestGetElements:
"""Tests for get_elements tool."""
Expand Down
Loading