Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /ai_assistant /tests /test_nlu.py
| """ | |
| ACGS-2 AI Assistant - NLU Engine Tests | |
| Constitutional Hash: 608508a9bd224290 | |
| """ | |
| import os | |
| import sys | |
| import pytest | |
| sys.path.insert( | |
| 0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) | |
| ) | |
| # Import centralized constitutional hash with fallback | |
| from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH | |
| from enhanced_agent_bus.ai_assistant.nlu import ( | |
| BasicSentimentAnalyzer, | |
| Entity, | |
| EntityExtractor, | |
| Intent, | |
| IntentClassifier, | |
| NLUEngine, | |
| NLUResult, | |
| PatternEntityExtractor, | |
| RuleBasedIntentClassifier, | |
| Sentiment, | |
| SentimentAnalyzer, | |
| ) | |
| class TestNLUResult: | |
| """Tests for NLUResult dataclass.""" | |
| def test_create_default_result(self): | |
| """Test creating a default NLU result.""" | |
| result = NLUResult() | |
| assert result.primary_intent is None | |
| assert result.secondary_intents == [] | |
| assert result.sentiment == Sentiment.NEUTRAL | |
| assert result.confidence == 0.0 | |
| assert result.constitutional_hash == CONSTITUTIONAL_HASH | |
| def test_primary_intent(self): | |
| """Test creating result with primary intent.""" | |
| result = NLUResult( | |
| original_text="Test", | |
| processed_text="Test", | |
| primary_intent=Intent(name="order_status", confidence=0.9), | |
| secondary_intents=[Intent(name="greeting", confidence=0.3)], | |
| ) | |
| assert result.primary_intent is not None | |
| assert result.primary_intent.name == "order_status" | |
| assert result.primary_intent.confidence == 0.9 | |
| def test_primary_intent_empty(self): | |
| """Test primary intent with no intents.""" | |
| result = NLUResult() | |
| assert result.primary_intent is None | |
| def test_result_to_dict(self): | |
| """Test result serialization.""" | |
| result = NLUResult( | |
| original_text="Hello", | |
| processed_text="Hello", | |
| primary_intent=Intent(name="greeting", confidence=0.9), | |
| entities={"name": "John"}, | |
| sentiment=Sentiment.POSITIVE, | |
| confidence=0.9, | |
| ) | |
| data = result.to_dict() | |
| assert "primary_intent" in data | |
| assert "entities" in data | |
| assert "sentiment" in data | |
| assert data["constitutional_hash"] == CONSTITUTIONAL_HASH | |
| def test_result_with_intents_convenience(self): | |
| """Test using intents convenience parameter.""" | |
| result = NLUResult( | |
| intents=[ | |
| {"intent": "order_status", "confidence": 0.9}, | |
| {"intent": "greeting", "confidence": 0.3}, | |
| ] | |
| ) | |
| # Primary intent should be set from first intent | |
| assert result.primary_intent is not None | |
| assert result.primary_intent.name == "order_status" | |
| assert len(result.secondary_intents) == 1 | |
| class TestRuleBasedIntentClassifier: | |
| """Tests for RuleBasedIntentClassifier.""" | |
| def test_create_classifier(self): | |
| """Test creating a classifier.""" | |
| classifier = RuleBasedIntentClassifier() | |
| assert classifier is not None | |
| async def test_classify_greeting(self): | |
| """Test classifying greeting intent.""" | |
| classifier = RuleBasedIntentClassifier() | |
| intents = await classifier.classify("Hello, how are you?") | |
| assert len(intents) > 0 | |
| assert isinstance(intents[0], Intent) | |
| # Should detect greeting | |
| intent_names = [i.name for i in intents] | |
| assert "greeting" in intent_names or any("greet" in n.lower() for n in intent_names) | |
| async def test_classify_order_status(self): | |
| """Test classifying order status intent.""" | |
| classifier = RuleBasedIntentClassifier() | |
| # Use text that matches the order_status pattern | |
| intents = await classifier.classify("Where is my order?") | |
| assert len(intents) > 0 | |
| assert isinstance(intents[0], Intent) | |
| intent_names = [i.name for i in intents] | |
| assert "order_status" in intent_names or any("order" in n.lower() for n in intent_names) | |
| async def test_classify_help(self): | |
| """Test classifying help intent.""" | |
| classifier = RuleBasedIntentClassifier() | |
| intents = await classifier.classify("I need help with something") | |
| assert len(intents) > 0 | |
| assert isinstance(intents[0], Intent) | |
| async def test_classify_unknown(self): | |
| """Test classifying unknown text.""" | |
| classifier = RuleBasedIntentClassifier() | |
| intents = await classifier.classify("xyzzy foobar random") | |
| # Should return something, possibly with low confidence | |
| assert isinstance(intents, list) | |
| async def test_classify_with_context(self): | |
| """Test classification with context.""" | |
| classifier = RuleBasedIntentClassifier() | |
| intents = await classifier.classify( | |
| "Yes, that one", | |
| context={"previous_intent": "order_selection"}, | |
| ) | |
| assert isinstance(intents, list) | |
| class TestPatternEntityExtractor: | |
| """Tests for PatternEntityExtractor.""" | |
| def test_create_extractor(self): | |
| """Test creating an extractor.""" | |
| extractor = PatternEntityExtractor() | |
| assert extractor is not None | |
| async def test_extract_email(self): | |
| """Test extracting email entities.""" | |
| extractor = PatternEntityExtractor() | |
| entities = await extractor.extract("My email is john@example.com") | |
| assert isinstance(entities, list) | |
| assert len(entities) > 0 | |
| # Find email entity | |
| email_entities = [e for e in entities if e.type == "email"] | |
| assert len(email_entities) > 0 | |
| assert email_entities[0].value == "john@example.com" | |
| async def test_extract_phone(self): | |
| """Test extracting phone number entities.""" | |
| extractor = PatternEntityExtractor() | |
| entities = await extractor.extract("Call me at 555-123-4567") | |
| assert isinstance(entities, list) | |
| # Should extract phone or number | |
| phone_entities = [e for e in entities if "phone" in e.type.lower()] | |
| assert len(phone_entities) > 0 | |
| async def test_extract_order_id(self): | |
| """Test extracting order ID entities.""" | |
| extractor = PatternEntityExtractor() | |
| entities = await extractor.extract("My order number is ORD-12345") | |
| assert isinstance(entities, list) | |
| # Should extract order_id or number | |
| order_entities = [ | |
| e for e in entities if "order" in e.type.lower() or "number" in e.type.lower() | |
| ] | |
| assert len(order_entities) > 0 | |
| async def test_extract_date(self): | |
| """Test extracting date entities.""" | |
| extractor = PatternEntityExtractor() | |
| entities = await extractor.extract("I want to schedule for 2024-12-25") | |
| assert isinstance(entities, list) | |
| # Should extract date or number | |
| date_entities = [e for e in entities if "date" in e.type.lower() or e.type == "number"] | |
| assert len(date_entities) >= 0 # May or may not extract depending on patterns | |
| async def test_extract_multiple_entities(self): | |
| """Test extracting multiple entities.""" | |
| extractor = PatternEntityExtractor() | |
| entities = await extractor.extract("Email john@example.com about order ORD-12345") | |
| assert isinstance(entities, list) | |
| assert len(entities) >= 1 | |
| async def test_extract_no_entities(self): | |
| """Test extracting from text with no entities.""" | |
| extractor = PatternEntityExtractor() | |
| entities = await extractor.extract("Just some random text here") | |
| assert isinstance(entities, list) | |
| class TestBasicSentimentAnalyzer: | |
| """Tests for BasicSentimentAnalyzer.""" | |
| def test_create_analyzer(self): | |
| """Test creating a sentiment analyzer.""" | |
| analyzer = BasicSentimentAnalyzer() | |
| assert analyzer is not None | |
| async def test_analyze_positive(self): | |
| """Test analyzing positive sentiment.""" | |
| analyzer = BasicSentimentAnalyzer() | |
| # Use text with clear positive words (no punctuation attached) | |
| sentiment = await analyzer.analyze("This is great and I love it") | |
| assert sentiment == "positive" | |
| async def test_analyze_negative(self): | |
| """Test analyzing negative sentiment.""" | |
| analyzer = BasicSentimentAnalyzer() | |
| # Use text with clear negative words (no punctuation attached) | |
| sentiment = await analyzer.analyze("This is terrible and I hate it") | |
| assert sentiment == "negative" | |
| async def test_analyze_neutral(self): | |
| """Test analyzing neutral sentiment.""" | |
| analyzer = BasicSentimentAnalyzer() | |
| sentiment = await analyzer.analyze("The package arrived today.") | |
| assert sentiment == "neutral" | |
| class TestNLUEngine: | |
| """Tests for NLUEngine.""" | |
| def test_create_engine(self): | |
| """Test creating an NLU engine.""" | |
| engine = NLUEngine() | |
| assert engine is not None | |
| async def test_process_greeting(self): | |
| """Test processing a greeting message.""" | |
| engine = NLUEngine() | |
| result = await engine.process("Hello, how are you?") | |
| assert isinstance(result, NLUResult) | |
| assert result.constitutional_hash == CONSTITUTIONAL_HASH | |
| async def test_process_with_entities(self): | |
| """Test processing message with entities.""" | |
| engine = NLUEngine() | |
| result = await engine.process("My email is test@example.com") | |
| assert isinstance(result, NLUResult) | |
| # entities can be list or dict | |
| assert result.entities is not None | |
| async def test_process_with_context(self): | |
| """Test processing with context.""" | |
| engine = NLUEngine() | |
| result = await engine.process( | |
| "Yes, that one", | |
| context={"entities": {"product": "laptop"}}, | |
| ) | |
| assert isinstance(result, NLUResult) | |
| async def test_process_order_inquiry(self): | |
| """Test processing an order inquiry.""" | |
| engine = NLUEngine() | |
| result = await engine.process("What is the status of order ORD-12345?") | |
| assert isinstance(result, NLUResult) | |
| # Should have a primary intent | |
| assert result.primary_intent is not None | |
| async def test_confidence_score(self): | |
| """Test that confidence score is set.""" | |
| engine = NLUEngine() | |
| result = await engine.process("Hello there!") | |
| assert 0.0 <= result.confidence <= 1.0 | |
| async def test_sentiment_included(self): | |
| """Test that sentiment is included.""" | |
| engine = NLUEngine() | |
| result = await engine.process("I'm really happy with the service!") | |
| assert result.sentiment is not None | |
| # Sentiment is an enum | |
| assert isinstance(result.sentiment, Sentiment) | |
| async def test_empty_text(self): | |
| """Test processing empty text.""" | |
| engine = NLUEngine() | |
| result = await engine.process("") | |
| assert isinstance(result, NLUResult) | |
| # Should handle gracefully | |
| assert result.confidence <= 0.5 | |
| async def test_whitespace_text(self): | |
| """Test processing whitespace-only text.""" | |
| engine = NLUEngine() | |
| result = await engine.process(" ") | |
| assert isinstance(result, NLUResult) | |
| class TestNLUEngineCustomComponents: | |
| """Tests for NLUEngine with custom components.""" | |
| async def test_custom_intent_classifier(self): | |
| """Test using a custom intent classifier.""" | |
| class MockClassifier(IntentClassifier): | |
| async def classify(self, text: str, context=None) -> list[Intent]: | |
| return [Intent(name="custom_intent", confidence=1.0)] | |
| engine = NLUEngine(intent_classifier=MockClassifier()) | |
| result = await engine.process("Test") | |
| assert result.primary_intent is not None | |
| assert result.primary_intent.name == "custom_intent" | |
| async def test_custom_entity_extractor(self): | |
| """Test using a custom entity extractor.""" | |
| class MockExtractor(EntityExtractor): | |
| async def extract(self, text: str, context=None) -> list[Entity]: | |
| return [Entity(text="value", type="custom_entity", value="value", start=0, end=5)] | |
| engine = NLUEngine(entity_extractor=MockExtractor()) | |
| result = await engine.process("Test") | |
| # entities may be list or dict | |
| if isinstance(result.entities, list): | |
| entity_types = [e.type for e in result.entities] | |
| assert "custom_entity" in entity_types | |
| else: | |
| assert "custom_entity" in result.entities | |
| async def test_custom_sentiment_analyzer(self): | |
| """Test using a custom sentiment analyzer.""" | |
| class MockAnalyzer(SentimentAnalyzer): | |
| async def analyze(self, text: str, context=None) -> str: | |
| return "positive" | |
| engine = NLUEngine(sentiment_analyzer=MockAnalyzer()) | |
| result = await engine.process("Test") | |
| # Result sentiment will be mapped to enum | |
| assert result.sentiment == Sentiment.POSITIVE | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v"]) | |
Xet Storage Details
- Size:
- 13.2 kB
- Xet hash:
- 8024cc64298bab2ad55bdd4cbcc153b464c60fc85641d00364727c807c062bad
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.