feat: improve analyze_code terminal output with non-verbose mode
- Add verbose_output parameter (default: False) to CodeAnalysisRequest - Modify prepare_code_context to return both full context and summary - Show only file paths and sizes in terminal by default, not full content - Full file content is still sent to Gemini for analysis - Add comprehensive tests for verbose output functionality This prevents terminal hangs when analyzing large files while still providing Gemini with complete file contents for analysis. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -61,6 +61,7 @@ class TestModels:
|
||||
assert request.max_tokens == 8192
|
||||
assert request.temperature == 0.2
|
||||
assert request.model == DEFAULT_MODEL
|
||||
assert request.verbose_output == False
|
||||
|
||||
|
||||
class TestFileOperations:
|
||||
@@ -97,18 +98,21 @@ class TestFileOperations:
|
||||
file2 = tmp_path / "file2.py"
|
||||
file2.write_text("print('file2')", encoding='utf-8')
|
||||
|
||||
context = prepare_code_context([str(file1), str(file2)], None)
|
||||
context, summary = prepare_code_context([str(file1), str(file2)], None)
|
||||
assert "file1.py" in context
|
||||
assert "file2.py" in context
|
||||
assert "print('file1')" in context
|
||||
assert "print('file2')" in context
|
||||
assert "Analyzing 2 file(s)" in summary
|
||||
assert "bytes)" in summary
|
||||
|
||||
def test_prepare_code_context_with_code(self):
|
||||
"""Test preparing context from direct code"""
|
||||
code = "def test():\n pass"
|
||||
context = prepare_code_context(None, code)
|
||||
context, summary = prepare_code_context(None, code)
|
||||
assert "=== Direct Code ===" in context
|
||||
assert code in context
|
||||
assert "Direct code provided" in summary
|
||||
|
||||
def test_prepare_code_context_mixed(self, tmp_path):
|
||||
"""Test preparing context from both files and code"""
|
||||
@@ -116,9 +120,11 @@ class TestFileOperations:
|
||||
test_file.write_text("# From file", encoding='utf-8')
|
||||
code = "# Direct code"
|
||||
|
||||
context = prepare_code_context([str(test_file)], code)
|
||||
context, summary = prepare_code_context([str(test_file)], code)
|
||||
assert "# From file" in context
|
||||
assert "# Direct code" in context
|
||||
assert "Analyzing 1 file(s)" in summary
|
||||
assert "Direct code provided" in summary
|
||||
|
||||
|
||||
class TestToolHandlers:
|
||||
@@ -219,7 +225,11 @@ class TestToolHandlers:
|
||||
})
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0].text == "Analysis result"
|
||||
# Check that the response contains both summary and Gemini's response
|
||||
response_text = result[0].text
|
||||
assert "Analyzing 1 file(s)" in response_text
|
||||
assert "Gemini's response:" in response_text
|
||||
assert "Analysis result" in response_text
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@patch('google.generativeai.list_models')
|
||||
|
||||
74
tests/test_verbose_output.py
Normal file
74
tests/test_verbose_output.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
Test verbose output functionality
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
# Add parent directory to path for imports
|
||||
parent_dir = Path(__file__).resolve().parent.parent
|
||||
if str(parent_dir) not in sys.path:
|
||||
sys.path.insert(0, str(parent_dir))
|
||||
|
||||
from gemini_server import prepare_code_context
|
||||
|
||||
|
||||
class TestVerboseOutput:
|
||||
"""Test verbose output functionality"""
|
||||
|
||||
def test_verbose_true_shows_full_content(self, tmp_path):
|
||||
"""Test that verbose=True shows full file content"""
|
||||
test_file = tmp_path / "test.py"
|
||||
content = "def hello():\n return 'world'"
|
||||
test_file.write_text(content, encoding='utf-8')
|
||||
|
||||
context, summary = prepare_code_context([str(test_file)], None, verbose=True)
|
||||
|
||||
# With verbose=True, summary should equal context
|
||||
assert summary == context
|
||||
assert content in summary
|
||||
|
||||
def test_verbose_false_shows_summary(self, tmp_path):
|
||||
"""Test that verbose=False shows only summary"""
|
||||
test_file = tmp_path / "large_file.py"
|
||||
content = "x = 1\n" * 1000 # Large content
|
||||
test_file.write_text(content, encoding='utf-8')
|
||||
|
||||
context, summary = prepare_code_context([str(test_file)], None, verbose=False)
|
||||
|
||||
# Summary should be much smaller than context
|
||||
assert len(summary) < len(context)
|
||||
assert "Analyzing 1 file(s)" in summary
|
||||
assert str(test_file) in summary
|
||||
assert "bytes)" in summary
|
||||
# Content should not be in summary
|
||||
assert content not in summary
|
||||
|
||||
def test_multiple_files_summary(self, tmp_path):
|
||||
"""Test summary with multiple files"""
|
||||
files = []
|
||||
for i in range(3):
|
||||
file = tmp_path / f"file{i}.py"
|
||||
file.write_text(f"# File {i}\nprint({i})", encoding='utf-8')
|
||||
files.append(str(file))
|
||||
|
||||
context, summary = prepare_code_context(files, None, verbose=False)
|
||||
|
||||
assert "Analyzing 3 file(s)" in summary
|
||||
for file in files:
|
||||
assert file in summary
|
||||
assert "bytes)" in summary
|
||||
|
||||
def test_code_and_files_summary(self, tmp_path):
|
||||
"""Test summary with both files and direct code"""
|
||||
test_file = tmp_path / "test.py"
|
||||
test_file.write_text("# Test file", encoding='utf-8')
|
||||
direct_code = "# Direct code\nprint('hello')"
|
||||
|
||||
context, summary = prepare_code_context([str(test_file)], direct_code, verbose=False)
|
||||
|
||||
assert "Analyzing 1 file(s)" in summary
|
||||
assert str(test_file) in summary
|
||||
assert "Direct code provided" in summary
|
||||
assert f"({len(direct_code):,} characters)" in summary
|
||||
Reference in New Issue
Block a user