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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// SPDX-License-Identifier: Apache-2.0
//! Gate test: vector insert/delete sync — Lite → Origin round-trip.
//!
//! Proves that vectors inserted into a vector collection on Lite replicate
//! to Origin via the `VectorInsert` (0xA2) wire frame and are returned by a
//! nearest-neighbour query on Origin. Also proves that `VectorDelete` (0xA4)
//! removes the vector so it no longer appears in subsequent searches.
//!
//! ## Important caveat
//!
//! The sync path writes vectors to Origin's HNSW index via `VectorOp::Insert`.
//! The vector search response uses surrogate identifiers (u32 hex), not the
//! original string ids from Lite. Presence is therefore verified by result
//! count rather than id string matching.
//!
//! ## How to run
//!
//! Build the Origin binary first:
//! ```text
//! cd <project-root>/nodedb && cargo build -p nodedb
//! ```
//! Then run from the nodedb-lite workspace root:
//! ```text
//! cargo nextest run -p nodedb-lite --test sync_interop_vector
//! ```
//!
//! The test is placed in the `heavy` nextest group (serialized) by the
//! `binary(/sync_interop/)` filter in `.config/nextest.toml`.
#![cfg(feature = "sync")]
pub mod common;
use std::sync::Arc;
use std::time::Duration;
use nodedb_client::NodeDb;
use nodedb_lite::sync::{SyncClient, SyncConfig, run_sync_loop};
use nodedb_lite::{NodeDbLite, PagedbStorageMem};
use common::origin::OriginServer;
use common::sql::OriginPgwire;
// ── Collection DDL ──────────────────────────────────────────────────────────
/// Dim-3 FP32 vector collection on Origin (pgwire dialect).
///
/// The collection must be pre-created on Origin because the sync path writes
/// directly to the HNSW index via `VectorOp::Insert`, which calls
/// `get_or_create_vector_index`. The DDL establishes the schema so that
/// vector_distance queries work.
const CREATE_ORIGIN: &str = "CREATE COLLECTION vec_sync_test \
FIELDS (id TEXT, embedding VECTOR(3)) \
WITH (engine='vector', m=8, ef_construction=50)";
// NOTE: The Lite vector engine auto-creates collections on first `vector_insert`.
// No explicit DDL is required on the Lite side.
// ── Helper: open a Lite DB backed by in-memory storage ─────────────────────────
async fn open_lite() -> Arc<NodeDbLite<PagedbStorageMem>> {
let storage = PagedbStorageMem::open_in_memory()
.await
.expect("open_in_memory");
NodeDbLite::open(storage).await.expect("NodeDbLite::open")
}
/// Wire up the sync transport and wait until the connection is established.
async fn start_sync(lite: Arc<NodeDbLite<PagedbStorageMem>>) -> Arc<SyncClient> {
let sync_config = SyncConfig::new(common::origin::ORIGIN_WS, "");
let sync_client = Arc::new(SyncClient::new(sync_config));
let delegate = Arc::clone(&lite) as Arc<dyn nodedb_lite::sync::SyncDelegate>;
let client_clone = Arc::clone(&sync_client);
tokio::spawn(async move {
run_sync_loop(client_clone, delegate).await;
});
// Wait up to 10 s for the connection to become established.
let deadline = tokio::time::sleep(Duration::from_secs(10));
tokio::pin!(deadline);
loop {
tokio::select! {
_ = &mut deadline => panic!("sync connection did not establish within 10 seconds"),
_ = tokio::time::sleep(Duration::from_millis(50)) => {
if sync_client.state().await == nodedb_lite::sync::SyncState::Connected {
break;
}
}
}
}
sync_client
}
// ── Tests ─────────────────────────────────────────────────────────────────────
/// Inserts 5 vectors on Lite; waits for replication to Origin; asserts that
/// a nearest-neighbour query on Origin returns 5 results.
///
/// The search result "id" column contains the surrogate hex (not the original
/// string id from Lite), so presence is verified by result count.
#[tokio::test]
async fn vector_inserts_replicate_to_origin() {
let Some(_origin) = OriginServer::try_spawn_with_pgwire() else {
eprintln!("SKIP: Origin binary unavailable (set NODEDB_BIN or run via `cargo nextest`)");
return;
};
let pg = OriginPgwire::connect().await;
// Create the collection on Origin.
pg.execute(CREATE_ORIGIN).await;
let lite = open_lite().await;
let _sync = start_sync(Arc::clone(&lite)).await;
// Insert 5 well-separated vectors.
for i in 0u32..5 {
let embedding: Vec<f32> = vec![i as f32, 0.0, 0.0];
lite.vector_insert("vec_sync_test", &format!("v{i}"), &embedding, None)
.await
.unwrap_or_else(|e| panic!("Lite vector_insert v{i}: {e}"));
}
// Wait up to 5 s for Origin's HNSW index to return 5 results for top-5 search.
let mut origin_count: usize = 0;
let deadline = tokio::time::sleep(Duration::from_secs(5));
tokio::pin!(deadline);
loop {
tokio::select! {
_ = &mut deadline => break,
_ = tokio::time::sleep(Duration::from_millis(200)) => {
let rows = pg
.poll_query(
"SELECT id FROM vec_sync_test \
ORDER BY vector_distance(embedding, ARRAY[0.0, 0.0, 0.0]) \
LIMIT 5",
)
.await;
if rows.len() >= 5 {
origin_count = rows.len();
break;
}
}
}
}
assert_eq!(
origin_count, 5,
"Origin HNSW must return 5 results after sync; got {origin_count}"
);
// Cleanup.
pg.execute("DROP COLLECTION vec_sync_test").await;
}
/// Inserts 6 vectors on Lite (5 background + 1 target), waits for replication,
/// deletes the target on Lite, waits for delete to replicate, then asserts the
/// nearest-neighbour result count drops from 6 to 5.
#[tokio::test]
async fn vector_delete_replicates_to_origin() {
let Some(_origin) = OriginServer::try_spawn_with_pgwire() else {
eprintln!("SKIP: Origin binary unavailable (set NODEDB_BIN or run via `cargo nextest`)");
return;
};
let pg = OriginPgwire::connect().await;
pg.execute(CREATE_ORIGIN).await;
let lite = open_lite().await;
let _sync = start_sync(Arc::clone(&lite)).await;
// Insert 5 background vectors and 1 target (6 total).
for i in 0u32..5 {
let embedding: Vec<f32> = vec![i as f32 * 10.0, 0.0, 0.0];
lite.vector_insert("vec_sync_test", &format!("bg{i}"), &embedding, None)
.await
.unwrap_or_else(|e| panic!("Lite vector_insert bg{i}: {e}"));
}
let target: Vec<f32> = vec![1.0, 0.0, 0.0];
lite.vector_insert("vec_sync_test", "target", &target, None)
.await
.expect("Lite vector_insert target");
// Wait for Origin to have all 6 vectors.
let deadline = tokio::time::sleep(Duration::from_secs(5));
tokio::pin!(deadline);
let mut all_appeared = false;
loop {
tokio::select! {
_ = &mut deadline => break,
_ = tokio::time::sleep(Duration::from_millis(200)) => {
let rows = pg
.poll_query(
"SELECT id FROM vec_sync_test \
ORDER BY vector_distance(embedding, ARRAY[0.0, 0.0, 0.0]) \
LIMIT 6",
)
.await;
if rows.len() >= 6 {
all_appeared = true;
break;
}
}
}
}
assert!(
all_appeared,
"all 6 vectors must appear on Origin before testing delete"
);
// Delete the target on Lite.
lite.vector_delete("vec_sync_test", "target")
.await
.expect("Lite vector_delete target");
// Wait for Origin's count to drop to 5.
let deadline = tokio::time::sleep(Duration::from_secs(5));
tokio::pin!(deadline);
let mut count_after: usize = 6;
loop {
tokio::select! {
_ = &mut deadline => break,
_ = tokio::time::sleep(Duration::from_millis(200)) => {
let rows = pg
.poll_query(
"SELECT id FROM vec_sync_test \
ORDER BY vector_distance(embedding, ARRAY[0.0, 0.0, 0.0]) \
LIMIT 6",
)
.await;
if rows.len() <= 5 {
count_after = rows.len();
break;
}
}
}
}
assert_eq!(
count_after, 5,
"after delete replicates, Origin must return 5 results; got {count_after}"
);
// Cleanup.
pg.execute("DROP COLLECTION vec_sync_test").await;
}
/// Vectors inserted before the sync connection is established are flushed
/// once the connection comes up (same guarantee as columnar).
#[tokio::test]
async fn vector_pre_connection_inserts_sync_after_connect() {
let Some(_origin) = OriginServer::try_spawn_with_pgwire() else {
eprintln!("SKIP: Origin binary unavailable (set NODEDB_BIN or run via `cargo nextest`)");
return;
};
let pg = OriginPgwire::connect().await;
pg.execute(CREATE_ORIGIN).await;
let lite = open_lite().await;
// Insert 3 vectors BEFORE starting sync.
for i in 0u32..3 {
let embedding: Vec<f32> = vec![i as f32, 0.0, 0.0];
lite.vector_insert("vec_sync_test", &format!("pre{i}"), &embedding, None)
.await
.unwrap_or_else(|e| panic!("Lite pre-sync vector_insert pre{i}: {e}"));
}
// Now start sync transport.
let _sync = start_sync(Arc::clone(&lite)).await;
// Wait up to 8 s for Origin to have 3 vectors.
let mut origin_count: usize = 0;
let deadline = tokio::time::sleep(Duration::from_secs(8));
tokio::pin!(deadline);
loop {
tokio::select! {
_ = &mut deadline => break,
_ = tokio::time::sleep(Duration::from_millis(200)) => {
let rows = pg
.poll_query(
"SELECT id FROM vec_sync_test \
ORDER BY vector_distance(embedding, ARRAY[0.0, 0.0, 0.0]) \
LIMIT 3",
)
.await;
if rows.len() >= 3 {
origin_count = rows.len();
break;
}
}
}
}
assert_eq!(
origin_count, 3,
"pre-connection vectors must replicate once sync connects; got {origin_count}"
);
pg.execute("DROP COLLECTION vec_sync_test").await;
}