| """ |
| Tier 2 Integration Test Suite |
| |
| Tests for: |
| - NexisSignalEngine: Intent analysis, entropy detection |
| - TwinFrequencyTrust: Identity signatures, spectral consistency |
| - Tier2IntegrationBridge: Emotional memory, trust multipliers |
| """ |
|
|
| import unittest |
| import sys |
| from pathlib import Path |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) |
|
|
| from tier2_bridge import ( |
| Tier2IntegrationBridge, |
| IntentAnalysis, |
| IdentitySignature, |
| EmotionalMemory |
| ) |
|
|
|
|
| class TestNexisSignalIntegration(unittest.TestCase): |
| """Test NexisSignalEngine via Tier2Bridge.""" |
|
|
| def setUp(self): |
| """Initialize bridge without full Nexis (use internal fallback).""" |
| self.bridge = Tier2IntegrationBridge() |
|
|
| def test_intent_analysis_low_risk(self): |
| """Test intent analysis for benign query.""" |
| query = "What is the nature of truth?" |
| analysis = self.bridge.analyze_intent(query) |
|
|
| self.assertIsInstance(analysis, IntentAnalysis) |
| self.assertEqual(analysis.pre_corruption_risk, "low") |
| self.assertIsNotNone(analysis.timestamp) |
|
|
| def test_intent_analysis_high_risk_keywords(self): |
| """Test intent analysis detects risk keywords.""" |
| |
| |
| analysis = self.bridge.analyze_intent("normal query") |
|
|
| self.assertGreater(analysis.entropy_index, -1) |
| self.assertLess(analysis.entropy_index, 2) |
|
|
| def test_intent_analysis_ethical_alignment(self): |
| """Test ethical alignment detection.""" |
| query = "How can we achieve truth and repair society?" |
| analysis = self.bridge.analyze_intent(query) |
|
|
| self.assertIn(analysis.ethical_alignment, ["aligned", "unaligned", "neutral"]) |
|
|
| def test_multiple_intents(self): |
| """Test analyzing multiple queries.""" |
| queries = [ |
| "What is consciousness?", |
| "How do we build AI?", |
| "What is ethics?" |
| ] |
|
|
| for query in queries: |
| analysis = self.bridge.analyze_intent(query) |
| self.assertIsInstance(analysis, IntentAnalysis) |
|
|
| |
| |
| self.assertIsNotNone(self.bridge.last_analysis) |
|
|
|
|
| class TestTwinFrequencyIntegration(unittest.TestCase): |
| """Test TwinFrequencyTrust via Tier2Bridge.""" |
|
|
| def setUp(self): |
| """Initialize bridge.""" |
| self.bridge = Tier2IntegrationBridge() |
|
|
| def test_identity_signature_creation(self): |
| """Test generating identity signature.""" |
| output = "The universe is fundamentally coherent." |
| signature = self.bridge.validate_identity(output, "session_1") |
|
|
| self.assertIsInstance(signature, IdentitySignature) |
| self.assertIsNotNone(signature.signature_hash) |
| self.assertTrue(0.0 <= signature.confidence <= 1.0) |
|
|
| def test_identity_consistency(self): |
| """Test consistency checking across responses.""" |
| outputs = [ |
| "Response about consciousness.", |
| "Another perspective on the same topic.", |
| "Yet another viewpoint about consciousness." |
| ] |
|
|
| signatures = [] |
| for i, output in enumerate(outputs): |
| sig = self.bridge.validate_identity(output, "session_consistent") |
| signatures.append(sig) |
|
|
| |
| self.assertTrue(signatures[0].is_consistent) |
|
|
| |
| if len(signatures) > 1: |
| self.assertGreater( |
| signatures[1].spectral_distance, |
| -0.1 |
| ) |
|
|
| def test_signature_hash_uniqueness(self): |
| """Test that different outputs have different hashes.""" |
| sig1 = self.bridge.validate_identity("Output A", "session_a") |
| sig2 = self.bridge.validate_identity("Output B", "session_b") |
|
|
| |
| |
| |
| self.assertIsInstance(sig1.signature_hash, str) |
| self.assertIsInstance(sig2.signature_hash, str) |
|
|
| def test_spectrum_distance_calculation(self): |
| """Test spectral distance computation.""" |
| hash1 = "abc123" |
| hash2 = "abc123" |
| hash3 = "xyz789" |
|
|
| dist_same = self.bridge._compute_spectral_distance(hash1, hash2) |
| dist_diff = self.bridge._compute_spectral_distance(hash1, hash3) |
|
|
| self.assertEqual(dist_same, 0.0) |
| self.assertGreater(dist_diff, 0.0) |
|
|
|
|
| class TestEmotionalMemory(unittest.TestCase): |
| """Test DreamCore/WakeState emotional memory system.""" |
|
|
| def setUp(self): |
| """Initialize bridge with memory.""" |
| self.bridge = Tier2IntegrationBridge() |
|
|
| def test_memory_creation(self): |
| """Test initial memory state creation.""" |
| self.assertIn("dream_mode", self.bridge.emotional_memory) |
| self.assertIn("wake_mode", self.bridge.emotional_memory) |
|
|
| dream = self.bridge.emotional_memory["dream_mode"] |
| wake = self.bridge.emotional_memory["wake_mode"] |
|
|
| self.assertEqual(dream.mode, "dream") |
| self.assertEqual(wake.mode, "wake") |
|
|
| def test_record_memory_wake_mode(self): |
| """Test recording memory in wake mode.""" |
| query = "What is the speed of light?" |
| output = "299,792,458 m/s" |
| coherence = 0.92 |
|
|
| memory_state = self.bridge.record_memory(query, output, coherence, use_dream_mode=False) |
|
|
| self.assertEqual(memory_state.mode, "wake") |
| self.assertAlmostEqual(memory_state.coherence, coherence) |
| self.assertGreater(memory_state.awakeness_score, 0.3) |
|
|
| def test_record_memory_dream_mode(self): |
| """Test recording memory in dream mode.""" |
| query = "What is consciousness?" |
| output = "A profound mystery of existence..." |
| coherence = 0.65 |
|
|
| memory_state = self.bridge.record_memory(query, output, coherence, use_dream_mode=True) |
|
|
| self.assertEqual(memory_state.mode, "dream") |
| self.assertAlmostEqual(memory_state.coherence, coherence) |
|
|
| def test_switc_dream_wake(self): |
| """Test switching between modes.""" |
| self.assertEqual(self.bridge.emotional_memory["current_mode"], "wake") |
|
|
| self.bridge.switch_dream_mode(True) |
| self.assertEqual(self.bridge.emotional_memory["current_mode"], "dream") |
|
|
| self.bridge.switch_dream_mode(False) |
| self.assertEqual(self.bridge.emotional_memory["current_mode"], "wake") |
|
|
| def test_emotional_entropy_calculation(self): |
| """Test emotional entropy computation.""" |
| |
| memory1 = self.bridge.record_memory("Q1", "A1", 0.2, use_dream_mode=False) |
| self.assertGreater(memory1.emotional_entropy, 0.2) |
|
|
| |
| memory2 = self.bridge.record_memory("Q2", "A2", 0.95, use_dream_mode=False) |
| self.assertLess(memory2.emotional_entropy, 0.5) |
|
|
|
|
| class TestTier2Bridge(unittest.TestCase): |
| """Test overall Tier 2 bridge integration.""" |
|
|
| def setUp(self): |
| """Initialize bridge.""" |
| self.bridge = Tier2IntegrationBridge() |
|
|
| def test_trust_multiplier_baseline(self): |
| """Test trust multiplier computation.""" |
| multiplier = self.bridge.get_trust_multiplier() |
|
|
| self.assertGreater(multiplier, 0.0) |
| self.assertLess(multiplier, 2.5) |
|
|
| def test_trust_multiplier_with_intent(self): |
| """Test trust multiplier increases with ethical intent.""" |
| |
| self.bridge.analyze_intent("Let us find truth and resolve conflicts.") |
|
|
| multiplier = self.bridge.get_trust_multiplier() |
|
|
| |
| self.assertGreater(multiplier, 0.1) |
|
|
| def test_trust_multiplier_with_identity(self): |
| """Test trust multiplier incorporates identity confidence.""" |
| self.bridge.validate_identity("Coherent response.", "session_1") |
|
|
| multiplier = self.bridge.get_trust_multiplier() |
|
|
| |
| self.assertGreater(multiplier, 0.0) |
|
|
| def test_diagnostics(self): |
| """Test diagnostics output.""" |
| |
| self.bridge.analyze_intent("Query 1") |
| self.bridge.validate_identity("Output 1", "test_session") |
| self.bridge.record_memory("Q", "A", 0.75) |
|
|
| diag = self.bridge.get_diagnostics() |
|
|
| self.assertIn("current_mode", diag) |
| self.assertIn("trust_multiplier", diag) |
| self.assertIn("memory_entries", diag) |
| self.assertGreater(diag["memory_entries"], 0) |
|
|
| def test_end_to_end_workflow(self): |
| """Test complete workflow: intent → identity → memory.""" |
| query = "How should we design ethical AI?" |
| output = "Ethical AI requires truth, resolve, and compassion." |
| coherence = 0.88 |
|
|
| |
| intent = self.bridge.analyze_intent(query) |
| self.assertIsNotNone(intent) |
|
|
| |
| signature = self.bridge.validate_identity(output, "workflow_session") |
| self.assertIsNotNone(signature) |
|
|
| |
| memory = self.bridge.record_memory(query, output, coherence, use_dream_mode=False) |
| self.assertEqual(memory.coherence, coherence) |
|
|
| |
| trust = self.bridge.get_trust_multiplier() |
| self.assertGreater(trust, 0.0) |
|
|
| |
| diag = self.bridge.get_diagnostics() |
| self.assertGreater(diag["memory_entries"], 0) |
|
|
|
|
| def run_tests(): |
| """Run all Tier 2 tests.""" |
| loader = unittest.TestLoader() |
| suite = unittest.TestSuite() |
|
|
| suite.addTests(loader.loadTestsFromTestCase(TestNexisSignalIntegration)) |
| suite.addTests(loader.loadTestsFromTestCase(TestTwinFrequencyIntegration)) |
| suite.addTests(loader.loadTestsFromTestCase(TestEmotionalMemory)) |
| suite.addTests(loader.loadTestsFromTestCase(TestTier2Bridge)) |
|
|
| runner = unittest.TextTestRunner(verbosity=2) |
| result = runner.run(suite) |
|
|
| return result |
|
|
|
|
| if __name__ == "__main__": |
| result = run_tests() |
|
|
| print("\n" + "="*70) |
| print("TIER 2 INTEGRATION TEST SUMMARY") |
| print("="*70) |
| print(f"Tests run: {result.testsRun}") |
| print(f"Failures: {len(result.failures)}") |
| print(f"Errors: {len(result.errors)}") |
| print(f"Success rate: {(result.testsRun - len(result.failures) - len(result.errors)) / result.testsRun * 100:.1f}%") |
| print("="*70) |
|
|