nodedb-lite 0.1.0

Embedded edge client for NodeDB — offline vector search, graph traversal, and CRDT sync
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// SPDX-License-Identifier: Apache-2.0

//! Transport routing, end to end over a real WebSocket.
//!
//! Each test stands up a mock Origin on loopback, runs the public
//! `run_sync_loop` against it, and asserts that a frame put on the wire
//! reaches the right `SyncDelegate` callback — or, for the outbound
//! direction, that the client puts the right frames on the wire in the right
//! order. Nothing here reaches into the transport's private dispatch
//! functions, so the handshake, receive loop, and push loop are all exercised
//! as a client actually uses them.

#![cfg(feature = "sync")]

use std::sync::Arc;
use std::time::Duration;

use nodedb_lite::engine::crdt::engine::PendingDelta;
use nodedb_lite::nodedb::CollectionMeta;
use nodedb_lite::sync::{SyncClient, SyncConfig, SyncDelegate, run_sync_loop};
use nodedb_types::sync::wire::SyncMessageType;

pub mod common;

use common::mock_delegate::MockDelegate;
use common::ws_origin::{MockOrigin, await_until, collect_frames_for, send_frame};

/// Window for observing outbound traffic. The push loop ticks every 100ms, so
/// this covers several ticks — enough for a missing per-session dedup to show
/// up as a repeated announce.
const PUSH_OBSERVATION_WINDOW: Duration = Duration::from_millis(600);

/// Aborts the sync loop when the test ends, so a client that is mid-reconnect
/// does not outlive its mock Origin.
struct LoopGuard(tokio::task::JoinHandle<()>);

impl Drop for LoopGuard {
    fn drop(&mut self) {
        self.0.abort();
    }
}

fn make_client(origin: &MockOrigin) -> Arc<SyncClient> {
    Arc::new(SyncClient::new(SyncConfig::new(
        origin.url(),
        "test.jwt.token",
    )))
}

fn spawn_loop(client: &Arc<SyncClient>, mock: &Arc<MockDelegate>) -> LoopGuard {
    let delegate: Arc<dyn SyncDelegate> = Arc::clone(mock) as _;
    LoopGuard(tokio::spawn(run_sync_loop(Arc::clone(client), delegate)))
}

fn delta_ack(
    mutation_id: u64,
    status: nodedb_types::sync::wire::AckStatus,
) -> nodedb_types::sync::wire::DeltaAckMsg {
    nodedb_types::sync::wire::DeltaAckMsg {
        mutation_id,
        lsn: 100,
        clock_skew_warning_ms: None,
        applied_seq: 0,
        status,
    }
}

fn delta_reject(mutation_id: u64) -> nodedb_types::sync::wire::DeltaRejectMsg {
    nodedb_types::sync::wire::DeltaRejectMsg {
        mutation_id,
        reason: "unique violation".into(),
        compensation: None,
    }
}

#[tokio::test]
async fn dispatch_delta_ack() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    send_frame(
        &mut ws,
        SyncMessageType::DeltaAck,
        &delta_ack(42, nodedb_types::sync::wire::AckStatus::Applied),
    )
    .await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { recorded.acked_up_to() == 42 },
        "an Applied DeltaAck to retire mutation 42",
    )
    .await;
}

#[tokio::test]
async fn dispatch_delta_reject() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    send_frame(&mut ws, SyncMessageType::DeltaReject, &delta_reject(7)).await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { recorded.rejected() == vec![7] },
        "a DeltaReject to reach the delegate",
    )
    .await;
}

/// Origin refuses a delta whose Loro peer id belongs to another replica. The
/// client is the only layer that can recover: every later write carries the
/// same refused id, so a client that merely reports the refusal never syncs
/// again. The refusal must reach the rotation path, not the rollback path.
#[tokio::test]
async fn peer_id_collision_reject_drives_a_rotation() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let before = mock.peer_id();
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    send_frame(
        &mut ws,
        SyncMessageType::DeltaReject,
        &nodedb_types::sync::wire::DeltaRejectMsg {
            mutation_id: 7,
            reason: "PEER_ID_COLLISION: peer id 7 on collection 'users' is already owned by \
                     another replica"
                .into(),
            compensation: Some(nodedb_types::sync::compensation::CompensationHint::Custom {
                constraint: nodedb_types::sync::compensation::CompensationHint::PEER_ID_COLLISION
                    .into(),
                detail: "peer id owned by another replica".into(),
            }),
        },
    )
    .await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { recorded.peer_id_rotations() == 1 },
        "a peer-id collision refusal to drive a rotation",
    )
    .await;

    assert_ne!(mock.peer_id(), before, "the rotation must change the id");
    assert!(
        mock.rejected().is_empty(),
        "a collision must not be rolled back as a constraint violation — that deletes the \
         row and drops the write"
    );
}

/// A `Gap` ack means Origin did NOT apply the delta — it expected a different
/// sequence number and skipped this frame entirely. Acknowledging it discards
/// the pending delta, so the write is lost with only a warning logged.
///
/// A non-applied ack must not retire the delta from the pending queue.
#[tokio::test]
async fn gap_ack_does_not_retire_the_unapplied_delta() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    send_frame(
        &mut ws,
        SyncMessageType::DeltaAck,
        &delta_ack(
            42,
            nodedb_types::sync::wire::AckStatus::Gap { expected: 41 },
        ),
    )
    .await;
    // Frames are dispatched in arrival order, so once this later frame's
    // effect is visible the Gap ack has certainly been handled — the absence
    // of an acknowledge is then a real absence, not a race.
    send_frame(&mut ws, SyncMessageType::DeltaReject, &delta_reject(99)).await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { recorded.rejected() == vec![99] },
        "the frame following the Gap ack to be dispatched",
    )
    .await;

    assert_eq!(
        mock.acked_up_to(),
        0,
        "a Gap ack retired a delta Origin never applied; the write is lost"
    );
}

/// A `Fenced` ack means this producer's epoch was rejected — the delta was not
/// applied and the client must re-establish its producer identity, not throw
/// the write away.
#[tokio::test]
async fn fenced_ack_does_not_retire_the_unapplied_delta() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    send_frame(
        &mut ws,
        SyncMessageType::DeltaAck,
        &delta_ack(7, nodedb_types::sync::wire::AckStatus::Fenced),
    )
    .await;

    // Fencing flips the client's own flag, which is the observable proof the
    // ack was dispatched before the acked-up-to assertion below.
    let fenced = client.as_ref();
    await_until(
        move || async move { fenced.is_fenced() },
        "the Fenced ack to fence the producer",
    )
    .await;

    assert_eq!(
        mock.acked_up_to(),
        0,
        "a Fenced ack retired a delta Origin never applied; the write is lost"
    );
}

#[tokio::test]
async fn dispatch_shape_delta_imports() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());

    // Subscribed before the loop starts so the subscription is already in the
    // handshake the mock Origin answers.
    {
        let mut shapes = client.shapes().lock().await;
        shapes.subscribe(nodedb_types::sync::shape::ShapeDefinition {
            shape_id: "s1".into(),
            tenant_id: 1,
            shape_type: nodedb_types::sync::shape::ShapeType::Document {
                collection: "orders".into(),
                predicate: Vec::new(),
            },
            description: "test".into(),
            field_filter: vec![],
        });
    }

    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    let delta = nodedb_types::sync::wire::ShapeDeltaMsg {
        shape_id: "s1".into(),
        collection: "orders".into(),
        document_id: "o1".into(),
        operation: "INSERT".into(),
        delta: vec![1, 2, 3],
        lsn: 50,
    };
    send_frame(&mut ws, SyncMessageType::ShapeDelta, &delta).await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { recorded.imported().len() == 1 },
        "a ShapeDelta to be imported",
    )
    .await;

    assert_eq!(mock.imported()[0], ("orders".to_string(), vec![1, 2, 3]));

    let shapes = client.shapes().lock().await;
    assert_eq!(
        shapes
            .get("s1")
            .expect("subscription still present")
            .last_lsn,
        50
    );
}

#[tokio::test]
async fn dispatch_clock_sync() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    let clock_msg = nodedb_types::sync::wire::VectorClockSyncMsg {
        clocks: {
            let mut m = std::collections::HashMap::new();
            m.insert("0000000000000001".to_string(), 99u64);
            m
        },
        sender_id: 0,
    };
    send_frame(&mut ws, SyncMessageType::VectorClockSync, &clock_msg).await;

    let synced = client.as_ref();
    await_until(
        move || async move { synced.clock().lock().await.get(1) == 99 },
        "a VectorClockSync to advance the local clock",
    )
    .await;
}

#[tokio::test]
async fn dispatch_collection_schema() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    let msg = nodedb_types::sync::wire::CollectionSchemaSyncMsg {
        descriptor: nodedb_types::sync::wire::CollectionDescriptor {
            tenant_id: 1,
            database_id: nodedb_types::id::DatabaseId::new(1),
            name: "users".into(),
            collection_type: nodedb_types::collection::CollectionType::document(),
            bitemporal: false,
            crdt: false,
            fields: Vec::new(),
            primary: nodedb_types::PrimaryEngine::Document,
            vector_primary: None,
            partition_strategy: nodedb_types::PartitionStrategy::default(),
            declared_primary_key: None,
            descriptor_version: 1,
        },
        creation_hlc: nodedb_types::hlc::Hlc::new(1, 0),
    };
    send_frame(&mut ws, SyncMessageType::CollectionSchema, &msg).await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { recorded.imported_schemas() == vec!["users".to_string()] },
        "a CollectionSchema to be imported",
    )
    .await;
}

/// Origin sends server-originated writes as `RowPush`, carrying a row
/// post-image rather than Loro update bytes. Before this path existed the
/// frame hit the dispatcher's catch-all and was discarded as "unexpected
/// frame type from Origin", so nothing written on the server ever reached the
/// device.
#[tokio::test]
async fn dispatch_row_push_applies_the_row() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    let msg = nodedb_types::sync::wire::RowPushMsg {
        collection: "orders".into(),
        document_id: "o-1".into(),
        payload: vec![0x80], // empty msgpack map
        op: nodedb_types::sync::wire::RowOp::Upsert,
        lsn: 7,
        peer_id: 1,
        sequence: 3,
    };
    send_frame(&mut ws, SyncMessageType::RowPush, &msg).await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { !recorded.applied_rows().is_empty() },
        "a RowPush to be applied",
    )
    .await;

    assert_eq!(
        mock.applied_rows(),
        vec![(
            "orders".to_string(),
            "o-1".to_string(),
            nodedb_types::sync::wire::RowOp::Upsert
        )],
        "a RowPush frame must be applied, not discarded as an unknown type"
    );
}

/// A delete carries an empty payload and must be applied as a removal, not
/// inferred from the payload being empty.
#[tokio::test]
async fn dispatch_row_push_carries_delete_explicitly() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());
    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    let msg = nodedb_types::sync::wire::RowPushMsg {
        collection: "orders".into(),
        document_id: "o-2".into(),
        payload: Vec::new(),
        op: nodedb_types::sync::wire::RowOp::Delete,
        lsn: 8,
        peer_id: 1,
        sequence: 4,
    };
    send_frame(&mut ws, SyncMessageType::RowPush, &msg).await;

    let recorded = mock.as_ref();
    await_until(
        move || async move { !recorded.applied_rows().is_empty() },
        "a RowPush delete to be applied",
    )
    .await;

    let applied = mock.applied_rows();
    assert_eq!(applied.len(), 1);
    assert_eq!(applied[0].2, nodedb_types::sync::wire::RowOp::Delete);
}

/// A `CollectionSchema` (0x13) frame for a collection must be sent before the
/// first `DeltaPush` frame for that collection, and later push ticks must NOT
/// re-announce it (per-session dedup via `announced_collections`).
#[tokio::test]
async fn collection_schema_announced_before_first_delta_and_deduped() {
    let origin = MockOrigin::bind().await;
    let client = make_client(&origin);
    let mock = Arc::new(MockDelegate::new());

    mock.set_collection_meta(
        "widgets",
        CollectionMeta {
            name: "widgets".to_string(),
            collection_type: "document".to_string(),
            created_at_ms: 0,
            fields: Vec::new(),
            config_json: None,
            descriptor_json: None,
            bitemporal: false,
            crdt: false,
        },
    );
    // Never acknowledged, so the collection still has pending work on every
    // subsequent tick — the announce would repeat if it were not deduped.
    mock.set_pending(vec![PendingDelta {
        mutation_id: 1,
        collection: "widgets".to_string(),
        document_id: "d1".to_string(),
        delta_bytes: vec![9, 9, 9],
        seq: 0,
    }]);

    let _guard = spawn_loop(&client, &mock);

    let mut ws = origin.accept_handshaked().await;
    let frames = collect_frames_for(&mut ws, PUSH_OBSERVATION_WINDOW).await;

    assert!(
        frames.len() >= 2,
        "expected at least a schema frame followed by a delta frame, got {}",
        frames.len()
    );
    assert_eq!(frames[0].msg_type, SyncMessageType::CollectionSchema);
    assert_eq!(frames[1].msg_type, SyncMessageType::DeltaPush);

    let schema_count = frames
        .iter()
        .filter(|f| f.msg_type == SyncMessageType::CollectionSchema)
        .count();
    assert_eq!(
        schema_count, 1,
        "collection must be announced only once per session"
    );
}