@@ -133,6 +133,48 @@ def test_add_graph_documents_idempotent(graph, unique_tag):
133133 assert result [0 ]["cnt" ] == 1
134134
135135
136+ # ── similarity_search ─────────────────────────────────────────────────────────
137+
138+
139+ def test_similarity_search_returns_results (graph , unique_tag ):
140+ """similarity_search() returns node dicts with id, node, and distance keys.
141+
142+ Seeds a :LCSim node with a known embedding, then searches for the closest
143+ vector. The seeded node must appear in the top-k results.
144+ """
145+ # Derive a unique embedding from the test tag (same technique as llama-index
146+ # test) to avoid collisions with other :LCSim nodes in the shared DB.
147+ seed = list (bytes .fromhex (unique_tag ))
148+ vec = [float (seed [i % len (seed )]) / 255.0 for i in range (16 )]
149+
150+ try :
151+ seed_rows = graph .query (
152+ "CREATE (n:LCSim {id: $id, embedding: $vec}) RETURN n AS nid" ,
153+ params = {"id" : f"lcsim-{ unique_tag } " , "vec" : vec },
154+ )
155+ # graph.query() wraps CoordinodeClient.cypher() which returns raw dict values.
156+ # CoordiNode: CREATE ... RETURN n yields the internal integer node ID directly
157+ # (NOT a node object). similarity_search() also returns {"id": r.node.id, ...}
158+ # where r.node.id is the same integer. Direct equality comparison is correct.
159+ seeded_internal_id = seed_rows [0 ]["nid" ]
160+
161+ results = graph .similarity_search (vec , k = 5 , label = "LCSim" , property = "embedding" )
162+
163+ assert isinstance (results , list )
164+ assert len (results ) >= 1
165+ assert all ("id" in r and "node" in r and "distance" in r for r in results )
166+ assert any (r ["id" ] == seeded_internal_id for r in results )
167+ assert all (results [i ]["distance" ] <= results [i + 1 ]["distance" ] for i in range (len (results ) - 1 ))
168+ finally :
169+ graph .query ("MATCH (n:LCSim {id: $id}) DELETE n" , params = {"id" : f"lcsim-{ unique_tag } " })
170+
171+
172+ def test_similarity_search_empty_vector_returns_empty (graph ):
173+ """similarity_search() with an empty vector list returns an empty list without error."""
174+ results = graph .similarity_search ([], k = 5 )
175+ assert results == []
176+
177+
136178def test_schema_refreshes_after_add (graph , unique_tag ):
137179 """structured_schema is invalidated and re-fetched after add_graph_documents."""
138180 graph ._schema = None # force refresh
0 commit comments