- Add ChatManager class for OpenAI-compatible API communication (silicon.fhgr.ch:7080) - Add SystemPrompter for intelligent prompt generation with file context - Integrate ChatManager into frontend chat component - Add comprehensive pytest tests (40+ tests covering unit and integration scenarios) - Implement error handling for API failures, timeouts, and connection issues - Add environment variable-based configuration for API credentials - Update frontend state initialization to include ChatManager - All tests passing with mock/patch isolation for API calls
89 lines
3.3 KiB
Python
89 lines
3.3 KiB
Python
"""Tests for SystemPrompter."""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
import pytest
|
|
from backend.managers.system_prompter import SystemPrompter, MAX_FILE_CHARS
|
|
|
|
|
|
class TestSystemPrompterBasePrompt:
|
|
"""Tests for generate_prompt() without file context."""
|
|
|
|
def test_returns_non_empty_string(self):
|
|
prompt = SystemPrompter.generate_prompt()
|
|
assert isinstance(prompt, str)
|
|
assert len(prompt) > 0
|
|
|
|
def test_describes_code_assistant(self):
|
|
prompt = SystemPrompter.generate_prompt()
|
|
assert "code assistant" in prompt.lower()
|
|
|
|
def test_contains_no_file_xml_tag(self):
|
|
prompt = SystemPrompter.generate_prompt()
|
|
assert "<file" not in prompt
|
|
assert "<code>" not in prompt
|
|
|
|
def test_none_equals_no_argument(self):
|
|
assert SystemPrompter.generate_prompt(file_context=None) == SystemPrompter.generate_prompt()
|
|
|
|
|
|
class TestSystemPrompterWithFileContext:
|
|
"""Tests for generate_prompt() with file_context provided."""
|
|
|
|
def test_includes_filename(self):
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "main.py", "content": ""})
|
|
assert "main.py" in prompt
|
|
|
|
def test_includes_file_content(self):
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "app.py", "content": "x = 42"})
|
|
assert "x = 42" in prompt
|
|
|
|
def test_uses_xml_file_tag(self):
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "f.py", "content": "pass"})
|
|
assert "<file" in prompt
|
|
|
|
def test_uses_xml_code_tag(self):
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "f.py", "content": "pass"})
|
|
assert "<code>" in prompt
|
|
|
|
def test_with_context_is_longer_than_base(self):
|
|
base = SystemPrompter.generate_prompt()
|
|
with_ctx = SystemPrompter.generate_prompt(file_context={"name": "f.py", "content": "x=1"})
|
|
assert len(with_ctx) > len(base)
|
|
|
|
def test_missing_name_key_uses_unknown(self):
|
|
prompt = SystemPrompter.generate_prompt(file_context={"content": "some code"})
|
|
assert "unknown" in prompt
|
|
|
|
def test_missing_content_key_does_not_raise(self):
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "empty.py"})
|
|
assert "empty.py" in prompt
|
|
|
|
|
|
class TestSystemPrompterTruncation:
|
|
"""Tests for file content truncation."""
|
|
|
|
def test_large_file_is_truncated(self):
|
|
large = "a" * (MAX_FILE_CHARS + 500)
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "big.py", "content": large})
|
|
assert "[truncated]" in prompt
|
|
|
|
def test_small_file_is_not_truncated(self):
|
|
content = "print('hello')"
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "small.py", "content": content})
|
|
assert "[truncated]" not in prompt
|
|
assert content in prompt
|
|
|
|
def test_file_exactly_at_limit_is_not_truncated(self):
|
|
content = "x" * MAX_FILE_CHARS
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "f.py", "content": content})
|
|
assert "[truncated]" not in prompt
|
|
|
|
def test_file_one_over_limit_is_truncated(self):
|
|
content = "x" * (MAX_FILE_CHARS + 1)
|
|
prompt = SystemPrompter.generate_prompt(file_context={"name": "f.py", "content": content})
|
|
assert "[truncated]" in prompt
|