|
| 1 | +""" |
| 2 | +Tests for vector index backends and MemoryLayer ANN retrieval. |
| 3 | +""" |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +ROOT = Path(__file__).resolve().parents[1] |
| 8 | +sys.path.insert(0, str(ROOT)) |
| 9 | + |
| 10 | +from voltmem import MemoryLayer |
| 11 | +from voltmem.embeddings import _cosine |
| 12 | +from voltmem.vector_index import BruteForceVectorIndex, SqliteVectorIndex, cosine_similarity |
| 13 | + |
| 14 | + |
| 15 | +# ── deterministic toy embedder for parity tests ─────────────────────────────── |
| 16 | + |
| 17 | +_KEYWORDS = ("berlin", "paris", "concise", "stressed", "billing", "auth") |
| 18 | + |
| 19 | + |
| 20 | +def _toy_embed(text: str) -> list[float]: |
| 21 | + t = text.lower() |
| 22 | + vec = [1.0 if kw in t else 0.0 for kw in _KEYWORDS] |
| 23 | + return vec if any(vec) else [0.1] * len(_KEYWORDS) |
| 24 | + |
| 25 | + |
| 26 | +def _toy_similarity(a: str, b: str) -> float: |
| 27 | + return max(0.0, _cosine(_toy_embed(a), _toy_embed(b))) |
| 28 | + |
| 29 | + |
| 30 | +def _layer_with_index(mode: str = "brute"): |
| 31 | + return MemoryLayer( |
| 32 | + ":memory:", |
| 33 | + similarity_fn=_toy_similarity, |
| 34 | + embed_fn=_toy_embed, |
| 35 | + vector_index=mode, |
| 36 | + namespace="test", |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +# ── index unit tests ────────────────────────────────────────────────────────── |
| 41 | + |
| 42 | +def test_brute_force_search_orders_by_similarity(): |
| 43 | + idx = BruteForceVectorIndex() |
| 44 | + q = [1.0, 0.0, 0.0] |
| 45 | + idx.upsert("a", "u1", "location", [1.0, 0.0, 0.0]) |
| 46 | + idx.upsert("b", "u1", "location", [0.8, 0.2, 0.0]) |
| 47 | + idx.upsert("c", "u1", "location", [0.0, 1.0, 0.0]) |
| 48 | + hits = idx.search(q, "u1", top_k=2) |
| 49 | + assert [h[0] for h in hits] == ["a", "b"] |
| 50 | + |
| 51 | + |
| 52 | +def test_sqlite_index_namespace_isolation(): |
| 53 | + idx = SqliteVectorIndex(":memory:") |
| 54 | + idx.upsert("a", "alice", "location", [1.0, 0.0]) |
| 55 | + idx.upsert("b", "bob", "location", [0.0, 1.0]) |
| 56 | + alice = idx.search([1.0, 0.0], "alice", top_k=5) |
| 57 | + bob = idx.search([1.0, 0.0], "bob", top_k=5) |
| 58 | + assert alice[0][0] == "a" |
| 59 | + assert bob[0][0] == "b" |
| 60 | + idx.close() |
| 61 | + |
| 62 | + |
| 63 | +def test_cosine_similarity_clamps_negative(): |
| 64 | + assert cosine_similarity([1.0, 0.0], [-1.0, 0.0]) == 0.0 |
| 65 | + |
| 66 | + |
| 67 | +# ── MemoryLayer integration ─────────────────────────────────────────────────── |
| 68 | + |
| 69 | +def test_retrieve_with_brute_index_matches_full_scan(): |
| 70 | + with _layer_with_index("brute") as mem: |
| 71 | + mem.write("User lives in Berlin", domain="location") |
| 72 | + mem.write("User prefers concise answers", domain="core_preference") |
| 73 | + mem.write("User is stressed this week", domain="emotional_context") |
| 74 | + |
| 75 | + indexed = mem.retrieve("concise communication", top_k=2) |
| 76 | + mem._vector_index = None |
| 77 | + full = mem.retrieve("concise communication", top_k=2) |
| 78 | + |
| 79 | + assert [i.content for i in indexed.items] == [i.content for i in full.items] |
| 80 | + |
| 81 | + |
| 82 | +def test_retrieve_with_sqlite_index_matches_full_scan(): |
| 83 | + with _layer_with_index("sqlite") as mem: |
| 84 | + mem.write("User lives in Berlin", domain="location") |
| 85 | + mem.write("User lives in Paris now", domain="location") |
| 86 | + mem.write("building the billing service", domain="current_project") |
| 87 | + |
| 88 | + indexed = mem.retrieve("where does user live", top_k=1) |
| 89 | + mem._vector_index = None |
| 90 | + full = mem.retrieve("where does user live", top_k=1) |
| 91 | + |
| 92 | + assert indexed.items[0].content == full.items[0].content |
| 93 | + |
| 94 | + |
| 95 | +def test_supersede_removes_old_vector(): |
| 96 | + with _layer_with_index("brute") as mem: |
| 97 | + mem.write("User lives in Berlin", domain="location") |
| 98 | + mem.observe( |
| 99 | + "User lives in Paris now", |
| 100 | + domain="location", |
| 101 | + mismatch_magnitude=0.9, |
| 102 | + source="explicit_statement", |
| 103 | + ) |
| 104 | + active = mem._active(domain="location") |
| 105 | + assert len(active) == 1 |
| 106 | + assert "Paris" in active[0].content |
| 107 | + hits = mem._vector_index.search(_toy_embed("Berlin"), "test", top_k=5) |
| 108 | + for item_id, _ in hits: |
| 109 | + item = mem._store.get(item_id) |
| 110 | + assert item is None or "Berlin" not in item.content |
| 111 | + |
| 112 | + |
| 113 | +def test_remove_and_clear_drop_vectors(): |
| 114 | + with _layer_with_index("sqlite") as mem: |
| 115 | + r = mem.write("User lives in Berlin", domain="location") |
| 116 | + mem.remove(r.item.id) |
| 117 | + hits = mem._vector_index.search(_toy_embed("Berlin"), "test", top_k=5) |
| 118 | + assert hits == [] |
| 119 | + |
| 120 | + mem.write("User lives in Paris", domain="location") |
| 121 | + mem.clear() |
| 122 | + hits = mem._vector_index.search(_toy_embed("Paris"), "test", top_k=5) |
| 123 | + assert hits == [] |
| 124 | + |
| 125 | + |
| 126 | +def test_vector_index_off_preserves_behavior(): |
| 127 | + with MemoryLayer(":memory:", vector_index="off") as mem: |
| 128 | + mem.remember("I live in Berlin") |
| 129 | + res = mem.remember("I live in Paris") |
| 130 | + assert res.action in ("audited", "logged_mismatch") |
| 131 | + assert len(mem._active(domain="location")) == 1 |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + tests = [ |
| 136 | + test_brute_force_search_orders_by_similarity, |
| 137 | + test_sqlite_index_namespace_isolation, |
| 138 | + test_cosine_similarity_clamps_negative, |
| 139 | + test_retrieve_with_brute_index_matches_full_scan, |
| 140 | + test_retrieve_with_sqlite_index_matches_full_scan, |
| 141 | + test_supersede_removes_old_vector, |
| 142 | + test_remove_and_clear_drop_vectors, |
| 143 | + test_vector_index_off_preserves_behavior, |
| 144 | + ] |
| 145 | + passed = failed = 0 |
| 146 | + for t in tests: |
| 147 | + try: |
| 148 | + t() |
| 149 | + print(f" PASS {t.__name__}") |
| 150 | + passed += 1 |
| 151 | + except Exception as e: |
| 152 | + print(f" FAIL {t.__name__}: {e}") |
| 153 | + failed += 1 |
| 154 | + print(f"\n{passed}/{passed + failed} tests passed") |
| 155 | + if failed: |
| 156 | + sys.exit(1) |
0 commit comments