nodedb-types 0.1.0

Portable type definitions shared between NodeDB Origin and NodeDB-Lite
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
481
482
483
484
485
486
487
488
// SPDX-License-Identifier: BUSL-1.1

//! Wire-enum stability snapshot tests.
//!
//! Each test asserts the exact JSON form for every known variant.
//! Because all enums here are `#[non_exhaustive]`, a `_ => panic!` wildcard
//! arm ensures a new variant causes a test failure at runtime until a
//! rename attribute and snapshot are added here.
//!
//! These tests cover Category B enums: types that cross the SPSC bridge as
//! MessagePack but also have serde JSON impls used for introspection,
//! catalog export, or SQL DDL round-trips.

use serde_json::json;

// ─── CollectionType ───────────────────────────────────────────────────────────

#[test]
fn collection_type_wire_forms() {
    use nodedb_types::collection::CollectionType;
    use nodedb_types::columnar::{ColumnarProfile, DocumentMode};

    let doc = CollectionType::Document(DocumentMode::Schemaless);
    let col = CollectionType::Columnar(ColumnarProfile::Plain);

    for v in [&doc, &col] {
        match v {
            CollectionType::Document(_) => {}
            CollectionType::Columnar(_) => {}
            CollectionType::KeyValue(_) => {}
        }
        serde_json::to_value(v).expect("CollectionType must serialize");
    }

    let v = serde_json::to_value(&doc).expect("serialize Document");
    assert_eq!(
        v["storage"], "Document",
        "CollectionType::Document wire tag"
    );

    let v = serde_json::to_value(&col).expect("serialize Columnar");
    assert_eq!(
        v["storage"], "Columnar",
        "CollectionType::Columnar wire tag"
    );
}

// ─── ColumnarProfile ──────────────────────────────────────────────────────────

#[test]
fn columnar_profile_wire_forms() {
    use nodedb_types::columnar::ColumnarProfile;

    let plain = ColumnarProfile::Plain;
    match &plain {
        ColumnarProfile::Plain => {}
        ColumnarProfile::Timeseries { .. } => {}
        ColumnarProfile::Spatial { .. } => {}
    }
    let v = serde_json::to_value(&plain).expect("serialize");
    assert_eq!(v["profile"], "Plain", "ColumnarProfile::Plain wire tag");

    let ts = ColumnarProfile::Timeseries {
        time_key: "ts".into(),
        interval: "1h".into(),
    };
    let v = serde_json::to_value(&ts).expect("serialize");
    assert_eq!(
        v["profile"], "Timeseries",
        "ColumnarProfile::Timeseries wire tag"
    );

    let sp = ColumnarProfile::Spatial {
        geometry_column: "geom".into(),
        auto_rtree: true,
        auto_geohash: false,
    };
    let v = serde_json::to_value(&sp).expect("serialize");
    assert_eq!(v["profile"], "Spatial", "ColumnarProfile::Spatial wire tag");
}

// ─── DocumentMode ─────────────────────────────────────────────────────────────

#[test]
fn document_mode_wire_forms() {
    use nodedb_types::columnar::{ColumnDef, ColumnType, DocumentMode, StrictSchema};

    let s = DocumentMode::Schemaless;
    match &s {
        DocumentMode::Schemaless => {}
        DocumentMode::Strict(_) => {}
    }
    let v = serde_json::to_value(&s).expect("serialize");
    assert_eq!(v["mode"], "Schemaless", "DocumentMode::Schemaless wire tag");

    let schema =
        StrictSchema::new(vec![ColumnDef::required("id", ColumnType::Int64)]).expect("schema");
    let strict = DocumentMode::Strict(schema);
    let v = serde_json::to_value(&strict).expect("serialize");
    assert_eq!(v["mode"], "Strict", "DocumentMode::Strict wire tag");
}

// ─── ColumnType ───────────────────────────────────────────────────────────────

#[test]
fn column_type_wire_forms() {
    use nodedb_types::columnar::ColumnType;

    let unit_cases: &[(ColumnType, &str)] = &[
        (ColumnType::Int64, "Int64"),
        (ColumnType::Float64, "Float64"),
        (ColumnType::String, "String"),
        (ColumnType::Bool, "Bool"),
        (ColumnType::Bytes, "Bytes"),
        (ColumnType::Timestamp, "Timestamp"),
        (ColumnType::Timestamptz, "Timestamptz"),
        (ColumnType::SystemTimestamp, "SystemTimestamp"),
        (ColumnType::Geometry, "Geometry"),
        (ColumnType::Uuid, "Uuid"),
        (ColumnType::Json, "Json"),
        (ColumnType::Ulid, "Ulid"),
        (ColumnType::Duration, "Duration"),
        (ColumnType::Array, "Array"),
        (ColumnType::Set, "Set"),
        (ColumnType::Regex, "Regex"),
        (ColumnType::Range, "Range"),
        (ColumnType::Record, "Record"),
    ];

    for (ct, expected_tag) in unit_cases {
        // Wildcard arm required because ColumnType is #[non_exhaustive].
        match ct {
            ColumnType::Int64 => {}
            ColumnType::Float64 => {}
            ColumnType::String => {}
            ColumnType::Bool => {}
            ColumnType::Bytes => {}
            ColumnType::Timestamp => {}
            ColumnType::Timestamptz => {}
            ColumnType::SystemTimestamp => {}
            ColumnType::Decimal { .. } => {}
            ColumnType::Geometry => {}
            ColumnType::Vector(_) => {}
            ColumnType::Uuid => {}
            ColumnType::Json => {}
            ColumnType::Ulid => {}
            ColumnType::Duration => {}
            ColumnType::Array => {}
            ColumnType::Set => {}
            ColumnType::Regex => {}
            ColumnType::Range => {}
            ColumnType::Record => {}
            _ => panic!("unrecognized ColumnType — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(ct).expect("serialize");
        assert_eq!(
            v["type"], *expected_tag,
            "ColumnType::{expected_tag} has wrong wire tag"
        );
    }

    // Parametric variants.
    let dec = ColumnType::Decimal {
        precision: 10,
        scale: 2,
    };
    let v = serde_json::to_value(dec).expect("serialize");
    assert_eq!(v["type"], "Decimal", "ColumnType::Decimal wire tag");

    let vec = ColumnType::Vector(128);
    let v = serde_json::to_value(vec).expect("serialize");
    assert_eq!(v["type"], "Vector", "ColumnType::Vector wire tag");
}

// ─── KvTtlPolicy ──────────────────────────────────────────────────────────────

#[test]
fn kv_ttl_policy_wire_forms() {
    use nodedb_types::kv::KvTtlPolicy;

    let fd = KvTtlPolicy::FixedDuration {
        duration_ms: 60_000,
    };
    match &fd {
        KvTtlPolicy::FixedDuration { .. } => {}
        KvTtlPolicy::FieldBased { .. } => {}
        _ => panic!("unrecognized KvTtlPolicy — update wire_enum_lock.rs"),
    }
    let v = serde_json::to_value(&fd).expect("serialize");
    assert_eq!(
        v["kind"], "FixedDuration",
        "KvTtlPolicy::FixedDuration wire tag"
    );

    let fb = KvTtlPolicy::FieldBased {
        field: "expires_at".into(),
        offset_ms: 0,
    };
    let v = serde_json::to_value(&fb).expect("serialize");
    assert_eq!(v["kind"], "FieldBased", "KvTtlPolicy::FieldBased wire tag");
}

// ─── PartitionState ───────────────────────────────────────────────────────────

#[test]
fn partition_state_wire_forms() {
    use nodedb_types::timeseries::partition::PartitionState;

    let cases: &[(PartitionState, &str)] = &[
        (PartitionState::Active, "Active"),
        (PartitionState::Sealed, "Sealed"),
        (PartitionState::Merging, "Merging"),
        (PartitionState::Merged, "Merged"),
        (PartitionState::Deleted, "Deleted"),
        (PartitionState::Archived, "Archived"),
    ];

    for (ps, expected) in cases {
        match ps {
            PartitionState::Active => {}
            PartitionState::Sealed => {}
            PartitionState::Merging => {}
            PartitionState::Merged => {}
            PartitionState::Deleted => {}
            PartitionState::Archived => {}
            _ => panic!("unrecognized PartitionState — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(ps).expect("serialize");
        assert_eq!(v, json!(expected), "PartitionState::{expected} wire form");
    }
}

// ─── PartitionInterval ────────────────────────────────────────────────────────

#[test]
fn partition_interval_wire_forms() {
    use nodedb_types::timeseries::partition::PartitionInterval;

    let cases: Vec<(PartitionInterval, serde_json::Value)> = vec![
        (PartitionInterval::Month, json!("Month")),
        (PartitionInterval::Year, json!("Year")),
        (PartitionInterval::Unbounded, json!("Unbounded")),
        (PartitionInterval::Auto, json!("Auto")),
    ];

    for (pi, expected) in &cases {
        match pi {
            PartitionInterval::Duration(_) => {}
            PartitionInterval::Month => {}
            PartitionInterval::Year => {}
            PartitionInterval::Unbounded => {}
            PartitionInterval::Auto => {}
            _ => panic!("unrecognized PartitionInterval — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(pi).expect("serialize");
        assert_eq!(v, *expected, "PartitionInterval wire form");
    }

    let dur = PartitionInterval::Duration(3_600_000);
    serde_json::to_value(&dur).expect("PartitionInterval::Duration must serialize");
}

// ─── DistanceMetric ───────────────────────────────────────────────────────────

#[test]
fn distance_metric_wire_forms() {
    use nodedb_types::vector_distance::DistanceMetric;

    let cases: &[(DistanceMetric, &str)] = &[
        (DistanceMetric::L2, "L2"),
        (DistanceMetric::Cosine, "Cosine"),
        (DistanceMetric::InnerProduct, "InnerProduct"),
        (DistanceMetric::Manhattan, "Manhattan"),
        (DistanceMetric::Chebyshev, "Chebyshev"),
        (DistanceMetric::Hamming, "Hamming"),
        (DistanceMetric::Jaccard, "Jaccard"),
        (DistanceMetric::Pearson, "Pearson"),
    ];

    for (dm, expected) in cases {
        match dm {
            DistanceMetric::L2 => {}
            DistanceMetric::Cosine => {}
            DistanceMetric::InnerProduct => {}
            DistanceMetric::Manhattan => {}
            DistanceMetric::Chebyshev => {}
            DistanceMetric::Hamming => {}
            DistanceMetric::Jaccard => {}
            DistanceMetric::Pearson => {}
            _ => panic!("unrecognized DistanceMetric — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(dm).expect("serialize");
        assert_eq!(v, json!(expected), "DistanceMetric::{expected} wire form");
    }
}

// ─── VectorQuantization ───────────────────────────────────────────────────────

#[test]
fn vector_quantization_wire_forms() {
    use nodedb_types::vector_ann::VectorQuantization;

    let cases: &[(VectorQuantization, &str)] = &[
        (VectorQuantization::None, "None"),
        (VectorQuantization::Sq8, "Sq8"),
        (VectorQuantization::Pq, "Pq"),
        (VectorQuantization::RaBitQ, "RaBitQ"),
        (VectorQuantization::Bbq, "Bbq"),
        (VectorQuantization::Binary, "Binary"),
        (VectorQuantization::Ternary, "Ternary"),
        (VectorQuantization::Opq, "Opq"),
    ];

    for (vq, expected) in cases {
        match vq {
            VectorQuantization::None => {}
            VectorQuantization::Sq8 => {}
            VectorQuantization::Pq => {}
            VectorQuantization::RaBitQ => {}
            VectorQuantization::Bbq => {}
            VectorQuantization::Binary => {}
            VectorQuantization::Ternary => {}
            VectorQuantization::Opq => {}
            _ => panic!("unrecognized VectorQuantization — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(vq).expect("serialize");
        assert_eq!(
            v,
            json!(expected),
            "VectorQuantization::{expected} wire form"
        );
    }
}

// ─── BatteryState ─────────────────────────────────────────────────────────────

#[test]
fn battery_state_wire_forms() {
    use nodedb_types::timeseries::series::BatteryState;

    let cases: &[(BatteryState, &str)] = &[
        (BatteryState::Normal, "Normal"),
        (BatteryState::Low, "Low"),
        (BatteryState::Charging, "Charging"),
        (BatteryState::Unknown, "Unknown"),
    ];

    for (bs, expected) in cases {
        match bs {
            BatteryState::Normal => {}
            BatteryState::Low => {}
            BatteryState::Charging => {}
            BatteryState::Unknown => {}
            _ => panic!("unrecognized BatteryState — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(bs).expect("serialize");
        assert_eq!(v, json!(expected), "BatteryState::{expected} wire form");
    }
}

// ─── MultiVectorScoreMode ─────────────────────────────────────────────────────

#[test]
fn multi_vector_score_mode_wire_forms() {
    use nodedb_types::multi_vector::MultiVectorScoreMode;

    let cases: &[(MultiVectorScoreMode, &str)] = &[
        (MultiVectorScoreMode::MaxSim, "MaxSim"),
        (MultiVectorScoreMode::AvgSim, "AvgSim"),
        (MultiVectorScoreMode::SumSim, "SumSim"),
    ];

    for (m, expected) in cases {
        match m {
            MultiVectorScoreMode::MaxSim => {}
            MultiVectorScoreMode::AvgSim => {}
            MultiVectorScoreMode::SumSim => {}
            _ => panic!("unrecognized MultiVectorScoreMode — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(m).expect("serialize");
        assert_eq!(
            v,
            json!(expected),
            "MultiVectorScoreMode::{expected} wire form"
        );
    }
}

// ─── PrimaryEngine ────────────────────────────────────────────────────────────

#[test]
fn primary_engine_wire_forms() {
    use nodedb_types::collection_config::PrimaryEngine;

    let cases: &[(PrimaryEngine, &str)] = &[
        (PrimaryEngine::Document, "Document"),
        (PrimaryEngine::Strict, "Strict"),
        (PrimaryEngine::KeyValue, "KeyValue"),
        (PrimaryEngine::Columnar, "Columnar"),
        (PrimaryEngine::Spatial, "Spatial"),
        (PrimaryEngine::Vector, "Vector"),
    ];

    for (pe, expected) in cases {
        match pe {
            PrimaryEngine::Document => {}
            PrimaryEngine::Strict => {}
            PrimaryEngine::KeyValue => {}
            PrimaryEngine::Columnar => {}
            PrimaryEngine::Spatial => {}
            PrimaryEngine::Vector => {}
            _ => panic!("unrecognized PrimaryEngine — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(pe).expect("serialize");
        assert_eq!(v, json!(expected), "PrimaryEngine::{expected} wire form");
    }
}

// ─── PayloadIndexKind ─────────────────────────────────────────────────────────

#[test]
fn payload_index_kind_wire_forms() {
    use nodedb_types::collection_config::PayloadIndexKind;

    let cases: &[(PayloadIndexKind, &str)] = &[
        (PayloadIndexKind::Equality, "Equality"),
        (PayloadIndexKind::Range, "Range"),
        (PayloadIndexKind::Boolean, "Boolean"),
    ];

    for (pik, expected) in cases {
        match pik {
            PayloadIndexKind::Equality => {}
            PayloadIndexKind::Range => {}
            PayloadIndexKind::Boolean => {}
            _ => panic!("unrecognized PayloadIndexKind — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(pik).expect("serialize");
        assert_eq!(v, json!(expected), "PayloadIndexKind::{expected} wire form");
    }
}

// ─── QueryMode (text_search) ──────────────────────────────────────────────────

#[test]
fn query_mode_wire_forms() {
    use nodedb_types::text_search::QueryMode;

    let cases: &[(QueryMode, &str)] = &[(QueryMode::Or, "Or"), (QueryMode::And, "And")];

    for (qm, expected) in cases {
        match qm {
            QueryMode::Or => {}
            QueryMode::And => {}
            _ => panic!("unrecognized QueryMode — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(qm).expect("serialize");
        assert_eq!(v, json!(expected), "QueryMode::{expected} wire form");
    }
}

// ─── RefreshPolicy ────────────────────────────────────────────────────────────

#[test]
fn refresh_policy_wire_forms() {
    use nodedb_types::timeseries::continuous_agg::RefreshPolicy;

    let cases: Vec<(RefreshPolicy, serde_json::Value)> = vec![
        (RefreshPolicy::OnFlush, json!("on_flush")),
        (RefreshPolicy::OnSeal, json!("on_seal")),
        (RefreshPolicy::Manual, json!("manual")),
    ];

    for (rp, expected) in &cases {
        match rp {
            RefreshPolicy::OnFlush => {}
            RefreshPolicy::OnSeal => {}
            RefreshPolicy::Periodic(_) => {}
            RefreshPolicy::Manual => {}
            _ => panic!("unrecognized RefreshPolicy — update wire_enum_lock.rs"),
        }
        let v = serde_json::to_value(rp).expect("serialize");
        assert_eq!(v, *expected, "RefreshPolicy wire form");
    }

    let periodic = RefreshPolicy::Periodic(60_000);
    serde_json::to_value(&periodic).expect("RefreshPolicy::Periodic must serialize");
}