nodedb 0.4.0

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
// SPDX-License-Identifier: BUSL-1.1

use loro::LoroValue;

use nodedb_crdt::constraint::ConstraintSet;
use nodedb_crdt::policy::CollectionPolicy;
use nodedb_crdt::pre_validate::PreValidationResult;
use nodedb_crdt::validator::ProposedChange;

use crate::types::TenantId;

use super::core::TenantCrdtEngine;

fn test_constraints() -> ConstraintSet {
    let mut cs = ConstraintSet::new();
    cs.add_unique("users_email_unique", "users", "email");
    cs.add_not_null("users_name_nn", "users", "name");
    cs
}

#[test]
fn valid_write_applies() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, test_constraints()).unwrap();

    let change = ProposedChange {
        collection: "users".into(),
        row_id: "u1".into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields: vec![
            ("name".into(), LoroValue::String("Alice".into())),
            (
                "email".into(),
                LoroValue::String("alice@example.com".into()),
            ),
        ],
    };

    engine
        .validate_and_apply(
            1,
            nodedb_crdt::CrdtAuthContext::default(),
            &change,
            b"delta".to_vec(),
        )
        .unwrap();

    assert!(engine.row_exists("users", "u1"));
    assert_eq!(engine.dlq_len(), 0);
}

#[test]
fn constraint_violation_routes_to_dlq() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, test_constraints()).unwrap();
    // Use strict policy so violations escalate to DLQ instead of auto-resolving.
    engine
        .validator
        .policies_mut()
        .set("users", CollectionPolicy::strict());

    // Missing "name" field violates NOT NULL.
    let change = ProposedChange {
        collection: "users".into(),
        row_id: "u1".into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields: vec![("email".into(), LoroValue::String("a@b.com".into()))],
    };

    let err = engine
        .validate_and_apply(
            42,
            nodedb_crdt::CrdtAuthContext::default(),
            &change,
            b"delta".to_vec(),
        )
        .unwrap_err();

    assert!(matches!(err, crate::Error::Crdt(_)));
    assert_eq!(engine.dlq_len(), 1);
}

#[test]
fn pre_validate_fast_rejects() {
    let engine = TenantCrdtEngine::new(TenantId::new(1), 0, test_constraints()).unwrap();

    let change = ProposedChange {
        collection: "users".into(),
        row_id: "u1".into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields: vec![("email".into(), LoroValue::String("a@b.com".into()))],
    };

    match engine.pre_validate(&change) {
        PreValidationResult::FastReject { constraint, .. } => {
            assert_eq!(constraint, "users_name_nn");
        }
        _ => panic!("expected fast reject"),
    }
}

#[test]
fn unique_violation_after_first_write() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, test_constraints()).unwrap();
    // Strict mode: UNIQUE violations escalate to DLQ.
    engine
        .validator
        .policies_mut()
        .set("users", CollectionPolicy::strict());

    let first = ProposedChange {
        collection: "users".into(),
        row_id: "u1".into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields: vec![
            ("name".into(), LoroValue::String("Alice".into())),
            (
                "email".into(),
                LoroValue::String("alice@example.com".into()),
            ),
        ],
    };
    engine
        .validate_and_apply(
            1,
            nodedb_crdt::CrdtAuthContext::default(),
            &first,
            b"d1".to_vec(),
        )
        .unwrap();

    // Second write with same email should fail.
    let second = ProposedChange {
        collection: "users".into(),
        row_id: "u2".into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields: vec![
            ("name".into(), LoroValue::String("Bob".into())),
            (
                "email".into(),
                LoroValue::String("alice@example.com".into()),
            ),
        ],
    };
    assert!(
        engine
            .validate_and_apply(
                2,
                nodedb_crdt::CrdtAuthContext::default(),
                &second,
                b"d2".to_vec()
            )
            .is_err()
    );
    assert_eq!(engine.dlq_len(), 1);
}

// ── per-collection isolation ──────────────────────────────────────────────────

#[test]
fn separate_collections_have_isolated_docs() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, ConstraintSet::new()).unwrap();

    let change = ProposedChange {
        collection: "users".into(),
        row_id: "u1".into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields: vec![("name".into(), LoroValue::String("Alice".into()))],
    };
    engine
        .validate_and_apply(
            1,
            nodedb_crdt::CrdtAuthContext::default(),
            &change,
            b"d".to_vec(),
        )
        .unwrap();

    assert!(engine.row_exists("users", "u1"));
    assert!(!engine.row_exists("orders", "u1"));
    assert!(engine.read_row("users", "u1").is_some());
    assert!(engine.read_row("orders", "u1").is_none());
}

// ── cross-collection FK via tenant-wide validator ─────────────────────────────

fn fk_constraints() -> ConstraintSet {
    let mut cs = ConstraintSet::new();
    cs.add_foreign_key("posts_author_fk", "posts", "author_id", "users", "id");
    cs
}

fn apply_change(
    engine: &mut TenantCrdtEngine,
    collection: &str,
    row_id: &str,
    fields: Vec<(String, LoroValue)>,
) -> crate::Result<()> {
    let change = ProposedChange {
        collection: collection.into(),
        row_id: row_id.into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields,
    };
    engine.validate_and_apply(
        1,
        nodedb_crdt::CrdtAuthContext::default(),
        &change,
        b"d".to_vec(),
    )
}

#[test]
fn cross_collection_fk_rejects_missing_referent() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(2), 0, fk_constraints()).unwrap();
    engine.set_collection_policy_typed("posts", CollectionPolicy::strict());

    let result = apply_change(
        &mut engine,
        "posts",
        "p1",
        vec![
            ("title".into(), LoroValue::String("Hello".into())),
            ("author_id".into(), LoroValue::String("u1".into())),
        ],
    );

    assert!(result.is_err());
    assert_eq!(engine.dlq_len(), 1);
    assert!(!engine.row_exists("posts", "p1"));
}

#[test]
fn cross_collection_fk_accepts_after_referent_inserted() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(3), 0, fk_constraints()).unwrap();
    engine.set_collection_policy_typed("posts", CollectionPolicy::strict());

    apply_change(
        &mut engine,
        "users",
        "u1",
        vec![("name".into(), LoroValue::String("Alice".into()))],
    )
    .unwrap();

    apply_change(
        &mut engine,
        "posts",
        "p1",
        vec![
            ("title".into(), LoroValue::String("Hello".into())),
            ("author_id".into(), LoroValue::String("u1".into())),
        ],
    )
    .unwrap();

    assert!(engine.row_exists("users", "u1"));
    assert!(engine.row_exists("posts", "p1"));
    assert_eq!(engine.dlq_len(), 0);
}

// ── array-surrogate FK via tenant registry ────────────────────────────────────

#[test]
fn array_surrogate_satisfies_cross_engine_fk() {
    let mut cs = ConstraintSet::new();
    cs.add_foreign_key("posts_author_fk", "posts", "author_id", "users", "id");
    let mut engine = TenantCrdtEngine::new(TenantId::new(4), 0, cs).unwrap();
    engine.set_collection_policy_typed("posts", CollectionPolicy::strict());

    engine.register_array_surrogate("arr_42");

    apply_change(
        &mut engine,
        "posts",
        "p1",
        vec![
            ("title".into(), LoroValue::String("Hello".into())),
            ("author_id".into(), LoroValue::String("arr_42".into())),
        ],
    )
    .unwrap();

    assert!(engine.row_exists("posts", "p1"));
    assert_eq!(engine.dlq_len(), 0);
}

#[test]
fn installed_constraints_are_enforced_and_droppable() {
    // Engine starts with NO constraints; U2 installs them at runtime.
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, ConstraintSet::new()).unwrap();
    // Strict policy so a UNIQUE violation escalates (returns Err) instead of
    // auto-resolving.
    engine
        .validator
        .policies_mut()
        .set("users", CollectionPolicy::strict());

    let unique_email = nodedb_crdt::Constraint {
        name: "users_email_unique".into(),
        collection: "users".into(),
        field: "email".into(),
        kind: nodedb_crdt::ConstraintKind::Unique,
    };
    assert!(engine.set_collection_constraints("users", 1, vec![unique_email.clone()]));

    let mk = |row: &str, coll: &str, email: &str| ProposedChange {
        collection: coll.into(),
        row_id: row.into(),
        surrogate: nodedb_types::Surrogate::ZERO,
        fields: vec![("email".into(), LoroValue::String(email.into()))],
    };

    // First insert with the email succeeds.
    engine
        .validate_and_apply(
            1,
            nodedb_crdt::CrdtAuthContext::default(),
            &mk("u1", "users", "a@b.com"),
            b"d".to_vec(),
        )
        .unwrap();

    // Duplicate email in the same collection is rejected.
    assert!(
        engine
            .validate_and_apply(
                1,
                nodedb_crdt::CrdtAuthContext::default(),
                &mk("u2", "users", "a@b.com"),
                b"d".to_vec()
            )
            .is_err(),
        "duplicate email must violate the installed UNIQUE constraint"
    );

    // A different collection carries no such constraint — same value is fine.
    engine
        .validate_and_apply(
            1,
            nodedb_crdt::CrdtAuthContext::default(),
            &mk("p1", "posts", "a@b.com"),
            b"d".to_vec(),
        )
        .unwrap();

    // After dropping the constraint, the duplicate is accepted.
    assert!(engine.drop_collection_constraints("users", 2));
    engine
        .validate_and_apply(
            1,
            nodedb_crdt::CrdtAuthContext::default(),
            &mk("u3", "users", "a@b.com"),
            b"d".to_vec(),
        )
        .unwrap();
    assert!(engine.row_exists("users", "u3"));
}

#[test]
fn set_collection_constraints_replaces_rather_than_accumulates() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, ConstraintSet::new()).unwrap();
    let c = nodedb_crdt::Constraint {
        name: "users_email_unique".into(),
        collection: "users".into(),
        field: "email".into(),
        kind: nodedb_crdt::ConstraintKind::Unique,
    };
    engine.set_collection_constraints("users", 1, vec![c.clone()]);
    engine.set_collection_constraints("users", 1, vec![c.clone()]);
    // Setting twice (same version, allowed by the `>=` fence) leaves exactly
    // one rule scoped to "users".
    assert_eq!(engine.validator.constraints_for("users").len(), 1);

    // An empty set clears the collection's constraints.
    engine.set_collection_constraints("users", 2, Vec::<nodedb_crdt::Constraint>::new());
    assert_eq!(engine.validator.constraints_for("users").len(), 0);
}

/// Builds a UNIQUE constraint named `name` on `users.email`.
fn unique_named(name: &str) -> nodedb_crdt::Constraint {
    nodedb_crdt::Constraint {
        name: name.into(),
        collection: "users".into(),
        field: "email".into(),
        kind: nodedb_crdt::ConstraintKind::Unique,
    }
}

#[test]
fn set_constraint_version_fence_rejects_stale_and_accepts_newer() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, ConstraintSet::new()).unwrap();

    // Install version 5: the constraint is visible.
    let v5 = unique_named("rule_v5");
    assert!(engine.set_collection_constraints("users", 5, vec![v5.clone()]));
    let installed = engine.constraints_for_collection("users");
    assert_eq!(installed.len(), 1);
    assert_eq!(installed[0].name, "rule_v5");

    // An older version 3 with a different set is rejected as stale; the
    // version-5 constraints remain untouched.
    let v3 = unique_named("rule_v3");
    assert!(!engine.set_collection_constraints("users", 3, vec![v3.clone()]));
    let unchanged = engine.constraints_for_collection("users");
    assert_eq!(unchanged.len(), 1);
    assert_eq!(unchanged[0].name, "rule_v5");

    // A newer version 7 applies and replaces.
    let v7 = unique_named("rule_v7");
    assert!(engine.set_collection_constraints("users", 7, vec![v7.clone()]));
    let replaced = engine.constraints_for_collection("users");
    assert_eq!(replaced.len(), 1);
    assert_eq!(replaced[0].name, "rule_v7");
}

#[test]
fn drop_constraint_version_fence_rejects_stale_and_accepts_newer() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, ConstraintSet::new()).unwrap();
    assert!(engine.set_collection_constraints("users", 5, vec![unique_named("rule_v5")]));

    // A drop at version 4 (older than the installed 5) is rejected; the
    // constraints survive.
    assert!(!engine.drop_collection_constraints("users", 4));
    assert_eq!(engine.constraints_for_collection("users").len(), 1);

    // A drop at version 6 applies and clears.
    assert!(engine.drop_collection_constraints("users", 6));
    assert_eq!(
        engine.constraints_for_collection("users"),
        Vec::<nodedb_crdt::Constraint>::new()
    );
}

#[test]
fn set_constraint_same_version_is_idempotent() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, ConstraintSet::new()).unwrap();
    let rule = unique_named("rule_v5");

    // The same version delivered twice both apply (the `>=` fence) and leave a
    // single rule per name — re-delivery is harmless.
    assert!(engine.set_collection_constraints("users", 5, vec![rule.clone()]));
    assert!(engine.set_collection_constraints("users", 5, vec![rule.clone()]));
    let installed = engine.constraints_for_collection("users");
    assert_eq!(installed.len(), 1);
    assert_eq!(installed[0].name, "rule_v5");
}

#[test]
fn purge_clears_constraints_and_resets_version_fence() {
    let mut engine = TenantCrdtEngine::new(TenantId::new(1), 0, ConstraintSet::new()).unwrap();

    // Install a constraint at version 5, then drop the whole collection.
    assert!(engine.set_collection_constraints("users", 5, vec![unique_named("old_rule")]));
    engine.purge_collection("users").unwrap();

    // Purge clears the constraints outright.
    assert_eq!(
        engine.constraints_for_collection("users"),
        Vec::<nodedb_crdt::Constraint>::new()
    );

    // A re-created collection of the same name restarts its descriptor version
    // at 1. Because purge also reset the fence, that fresh low-version install
    // is accepted rather than rejected as stale against the dropped 5.
    assert!(engine.set_collection_constraints("users", 1, vec![unique_named("new_rule")]));
    let installed = engine.constraints_for_collection("users");
    assert_eq!(installed.len(), 1);
    assert_eq!(installed[0].name, "new_rule");
}