| |
| """ |
| Test Suite for Deterministic Guardrail Adapter |
| """ |
|
|
| import unittest |
| from llm_adapter import DeterministicGuardrail, MockEmbedder |
|
|
| class TestDeterministicGuardrail(unittest.TestCase): |
| |
| def test_mock_embedder_determinism(self): |
| """Test that MockEmbedder is deterministic""" |
| embedder = MockEmbedder() |
| text = "Hello World" |
| |
| vec1 = embedder.embed(text) |
| vec2 = embedder.embed(text) |
| |
| self.assertEqual(vec1, vec2) |
| self.assertIsInstance(vec1[0], float) |
| self.assertIsInstance(vec1[1], float) |
| |
| def test_basic_filtering(self): |
| """Test basic filtering capability""" |
| |
| substrate = ["The fast fox jumps"] |
| |
| guard = DeterministicGuardrail(substrate_texts=substrate) |
| |
| candidates = [ |
| "The fast fox jumps", |
| "The slow turtle crawls" |
| ] |
| |
| |
| |
| |
| |
| |
| |
| result = guard.filter(candidates) |
| self.assertEqual(result, "The fast fox jumps") |
| |
| def test_abstention(self): |
| """Test that the system abstains when no candidate is good enough""" |
| |
| substrate = ["Apple Banana Cherry"] |
| |
| guard = DeterministicGuardrail(substrate_texts=substrate) |
| |
| |
| candidates = [ |
| "Xylophone Zebra", |
| "Quantum Physics" |
| ] |
| |
| |
| |
| |
| result = guard.filter(candidates) |
| |
| |
| if result is not None: |
| print(f"WARNING: Unlucky hash collision allowed '{result}' to survive against unrelated substrate.") |
| else: |
| self.assertIsNone(result) |
|
|
| def test_multi_candidate_selection(self): |
| """Test that the best candidate is selected from multiple options""" |
| substrate = ["The quick brown fox"] |
| guard = DeterministicGuardrail(substrate_texts=substrate) |
| |
| candidates = [ |
| "The quick brown fox", |
| "The quick brown", |
| "brown fox" |
| ] |
| |
| result = guard.filter(candidates) |
| self.assertEqual(result, "The quick brown fox") |
|
|
| if __name__ == "__main__": |
| unittest.main() |
|
|