File size: 2,987 Bytes
c7fa397 | 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 | """Test recursive link graph: context storage, linking, traversal."""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from singularity_llm.Singularity.recursive_link import RecursiveLinkGraph
from singularity_llm.Singularity.universal_link import UniversalLinkManager
from singularity_llm.Singularity.Singularity_tokens import SingularityTokenizer, SingularityTokenConfig
def test_recursive_link_basic():
"""Test basic context storage and retrieval."""
graph = RecursiveLinkGraph()
ctx_id = graph.add_context("Hello world", "Hi there!")
assert ctx_id in graph._contexts
print(f" Context stored: {ctx_id[:8]}")
def test_recursive_link_related():
"""Test finding related contexts."""
graph = RecursiveLinkGraph()
graph.add_context("Hello world programming", "Programming is fun!")
graph.add_context("Hello world greeting", "Hi there!")
graph.add_context("Python code example", "Here is some code.")
related = graph.find_related("Hello world")
assert len(related) > 0, "No related contexts found"
print(f" Found {len(related)} related contexts")
def test_recursive_link_injection():
"""Test context injection for inference."""
graph = RecursiveLinkGraph()
graph.add_context("What is Python?", "Python is a programming language.")
graph.add_context("How to code in Python?", "You can write Python code in any editor.")
injection = graph.get_injection_context("Tell me about Python")
assert "Python" in injection, f"Injection missing context: {injection}"
print(f" Injection: {injection[:80]}")
def test_universal_link():
"""Test universal link manager."""
mgr = UniversalLinkManager(instance_id="test-instance")
learning_id = mgr.share_learning("conversation", {"message": "test"})
assert learning_id is not None
stats = mgr.get_stats()
assert stats["learnings_shared"] == 1
print(f" Shared learning: {learning_id[:8]}")
def test_Singularity_tokens():
"""Test Singularity token encoding/decoding."""
tok = SingularityTokenizer(SingularityTokenConfig(format="q4_k_m"))
token_ids = [1, 2, 3, 4, 5, 10, 20, 30, 40, 50]
encoded = tok.encode(token_ids)
decoded = tok.decode(encoded, len(token_ids))
assert len(encoded) < len(token_ids) * 4, "No compression achieved"
print(f" Encoded {len(token_ids)} tokens to {len(encoded)} bytes")
print(f" Compression: {len(token_ids) * 4 / len(encoded):.1f}x")
if __name__ == "__main__":
print("Running Singularity tests...")
test_recursive_link_basic()
print(" ✓ test_recursive_link_basic")
test_recursive_link_related()
print(" ✓ test_recursive_link_related")
test_recursive_link_injection()
print(" ✓ test_recursive_link_injection")
test_universal_link()
print(" ✓ test_universal_link")
test_Singularity_tokens()
print(" ✓ test_Singularity_tokens")
print("\nAll Singularity tests passed!")
|