Spaces:
Configuration error
Configuration error
File size: 5,373 Bytes
d242052 a5d33a6 d242052 a5d33a6 d242052 a5d33a6 d242052 a5d33a6 d242052 a5d33a6 d242052 a5d33a6 d242052 a5d33a6 d242052 a5d33a6 d242052 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | """Unit tests for C8 Knowledge Layer domain models, intent analyzer, prompt builder, provider registry, validator, conversation manager, formatter, and orchestrator."""
import pytest
from ria.domain.context import (
Citation,
ContextMetadata,
ContextPackage,
ContextSection,
ContextSnippet,
RankingScore,
)
from ria.domain.index.value_objects import FilePath
from ria.domain.knowledge import (
ConversationId,
ConversationTurn,
IntentType,
InvalidKnowledgeRequestError,
KnowledgeRequest,
)
from ria.domain.resolution import SymbolMoniker
from ria.knowledge import (
ConversationManager,
IntentAnalyzer,
KnowledgeEngine,
KnowledgeOrchestrator,
MockLLMProvider,
PromptBuilder,
ProviderRegistry,
ResponseFormatter,
ResponseValidator,
)
def test_knowledge_domain_value_objects() -> None:
cid = ConversationId(value="conv_123")
assert cid.value == "conv_123"
with pytest.raises(InvalidKnowledgeRequestError):
ConversationId(value="")
req = KnowledgeRequest(conversation_id=cid, question="What is UserService?")
assert req.question == "What is UserService?"
with pytest.raises(InvalidKnowledgeRequestError):
KnowledgeRequest(conversation_id=cid, question="")
def test_intent_analyzer_all_intents() -> None:
analyzer = IntentAnalyzer()
pkg = ContextPackage(
package_id="p1",
question="q",
sections=(),
references=(),
metadata=ContextMetadata(0, 0, 0, 4000),
)
assert analyzer.analyze_intent("What is UserService?", pkg) == IntentType.DEFINITION
assert (
analyzer.analyze_intent("Explain the architecture", pkg)
== IntentType.ARCHITECTURE
)
assert (
analyzer.analyze_intent("How does login flow work?", pkg)
== IntentType.CODE_FLOW
)
assert (
analyzer.analyze_intent("Who calls hash_password?", pkg)
== IntentType.CALL_GRAPH
)
assert (
analyzer.analyze_intent("What depends on auth.py?", pkg)
== IntentType.DEPENDENCY_ANALYSIS
)
assert (
analyzer.analyze_intent("Investigate bug in login", pkg)
== IntentType.BUG_INVESTIGATION
)
assert (
analyzer.analyze_intent("How to refactor user module?", pkg)
== IntentType.REFACTORING
)
assert (
analyzer.analyze_intent("Generate documentation for auth", pkg)
== IntentType.DOCUMENTATION
)
assert (
analyzer.analyze_intent("Compare python versus typescript resolvers", pkg)
== IntentType.COMPARISON
)
assert (
analyzer.analyze_intent("What is the impact of changing user_id?", pkg)
== IntentType.IMPACT_ANALYSIS
)
def test_prompt_builder_and_provider_registry() -> None:
builder = PromptBuilder()
fp = FilePath(relative_path="auth.py")
moniker = SymbolMoniker(value="repo:auth.py:global:login")
cit = Citation(
repo_name="repo",
commit_sha="a" * 40,
file_path=fp,
module_name="auth",
symbol_moniker=moniker,
start_line=1,
end_line=5,
)
score = RankingScore(priority=1, score_value=1.0, category="Definition")
snip = ContextSnippet(
snippet_id="s1",
content="def login(): pass",
citation=cit,
score=score,
estimated_tokens=10,
)
sec = ContextSection(title="Definition", snippets=(snip,))
pkg = ContextPackage(
package_id="p1",
question="login",
sections=(sec,),
references=(),
metadata=ContextMetadata(1, 1, 10, 4000),
)
prompt_pkg = builder.build_prompt("login", pkg, IntentType.DEFINITION)
assert "RIA" in prompt_pkg.system_prompt
assert "login" in prompt_pkg.user_prompt
registry = ProviderRegistry()
mock_prov = MockLLMProvider()
registry.register_provider("mock", mock_prov)
assert registry.get_provider("mock") == mock_prov
def test_conversation_manager_and_formatter() -> None:
conv_mgr = ConversationManager()
cid = ConversationId(value="c1")
assert conv_mgr.get_conversation(cid) is None
turn = ConversationTurn(user_message="hi", assistant_response="hello")
ctx = conv_mgr.add_turn(cid, turn)
assert len(ctx.turns) == 1
conv_mgr.clear_conversation(cid)
assert conv_mgr.get_conversation(cid) is None
def test_knowledge_orchestrator_pipeline() -> None:
intent_analyzer = IntentAnalyzer()
prompt_builder = PromptBuilder()
registry = ProviderRegistry()
registry.register_provider("mock", MockLLMProvider())
validator = ResponseValidator()
formatter = ResponseFormatter()
conv_mgr = ConversationManager()
orchestrator = KnowledgeOrchestrator(
intent_analyzer, prompt_builder, registry, validator, formatter, conv_mgr
)
engine = KnowledgeEngine(orchestrator)
pkg = ContextPackage(
package_id="p1",
question="auth",
sections=(),
references=(),
metadata=ContextMetadata(0, 0, 0, 4000),
)
req = KnowledgeRequest(
conversation_id=ConversationId(value="c1"), question="What is auth?"
)
resp = engine.answer_question(req, pkg)
assert resp.is_success
assert resp.answer.validation.grounding_score.is_grounded
assert "Grounded Answer" in resp.formatted_content
|