velesdb-server 3.2.1

REST API server for VelesDB vector database
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#![allow(clippy::doc_markdown)]
//! BDD tests for the maintenance and bulk-ops endpoints introduced in
//! PR #648:
//!
//! - `POST /collections/{name}/points/delete` — bulk delete by id
//! - `POST /collections/{name}/vacuum`         — HNSW index vacuum
//! - `POST /collections/{name}/compact`        — storage compaction
//!
//! Coverage:
//! - Nominal (~60%): happy paths, end-to-end behaviour observable from
//!   the REST surface.
//! - Edge (~20%): boundary conditions (empty payload, max-batch size,
//!   collection with no deletions to compact).
//! - Negative (~20%): unknown collection, oversized batch, malformed
//!   JSON. Each must produce the documented HTTP status code.
//!
//! Tests build a router via `common::create_test_app` and exercise the
//! endpoints with `tower::ServiceExt::oneshot` requests, asserting
//! status codes and JSON response shape — no internal state inspection.

mod common;

use axum::{
    body::Body,
    http::{Request, StatusCode},
};
use common::create_test_app;
use serde_json::{json, Value};
use tempfile::TempDir;
use tower::ServiceExt;

const TEST_COLLECTION: &str = "admin_endpoints_bdd";
const DIM: usize = 4;

/// Bootstrap a vector collection and seed it with `n` deterministic points.
async fn seed_collection(app: axum::Router, n: usize) {
    let response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/collections")
                .header("Content-Type", "application/json")
                .body(Body::from(
                    json!({
                        "name": TEST_COLLECTION,
                        "dimension": DIM,
                        "metric": "cosine"
                    })
                    .to_string(),
                ))
                .expect("test: build create request"),
        )
        .await
        .expect("test: collection create request");
    assert_eq!(
        response.status(),
        StatusCode::CREATED,
        "test setup: failed to create collection"
    );

    if n == 0 {
        return;
    }
    let points: Vec<Value> = (0..n)
        .map(|i| {
            #[allow(clippy::cast_precision_loss)]
            let v = (i as f32) / 100.0;
            json!({
                "id": u64::try_from(i).expect("test: idx fits in u64") + 1,
                "vector": vec![v; DIM],
                "payload": { "idx": i }
            })
        })
        .collect();
    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/points"))
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "points": points }).to_string()))
                .expect("test: build upsert request"),
        )
        .await
        .expect("test: upsert request");
    assert_eq!(
        response.status(),
        StatusCode::OK,
        "test setup: failed to seed points"
    );
}

async fn read_json(response: axum::response::Response) -> Value {
    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
        .await
        .expect("test: read body");
    serde_json::from_slice(&body).expect("test: response is valid JSON")
}

// ---------------------------------------------------------------------
// /points/delete — bulk_delete_points
// ---------------------------------------------------------------------

#[tokio::test]
async fn bulk_delete_nominal_returns_200_with_deleted_count() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 5).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/points/delete"))
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "ids": [1, 2, 3] }).to_string()))
                .expect("test: build delete request"),
        )
        .await
        .expect("test: delete request");
    assert_eq!(response.status(), StatusCode::OK);

    let json = read_json(response).await;
    assert_eq!(json["deleted_count"], 3);
    assert_eq!(json["collection"], TEST_COLLECTION);
}

#[tokio::test]
async fn bulk_delete_empty_payload_returns_200_noop() {
    // Documented behaviour: empty `ids: []` is a no-op (200, count=0).
    // See `bulk_delete_points` rustdoc — idempotent batch semantics.
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 3).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/points/delete"))
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "ids": [] }).to_string()))
                .expect("test: build delete request"),
        )
        .await
        .expect("test: delete request");
    assert_eq!(response.status(), StatusCode::OK);

    let json = read_json(response).await;
    assert_eq!(json["deleted_count"], 0);
}

#[tokio::test]
async fn bulk_delete_unknown_ids_silently_skipped() {
    // Idempotent: deleting non-existent IDs returns 200, count = batch size.
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 2).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/points/delete"))
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "ids": [999, 1000] }).to_string()))
                .expect("test: build delete request"),
        )
        .await
        .expect("test: delete request");
    assert_eq!(response.status(), StatusCode::OK);
    let json = read_json(response).await;
    assert_eq!(json["deleted_count"], 2);
}

#[tokio::test]
async fn bulk_delete_oversized_batch_returns_400() {
    // Negative: > MAX_BULK_DELETE_SIZE (10_000) must be 400.
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 1).await;

    let oversized: Vec<u64> = (1..=10_001).collect();
    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/points/delete"))
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "ids": oversized }).to_string()))
                .expect("test: build delete request"),
        )
        .await
        .expect("test: delete request");
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn bulk_delete_unknown_collection_returns_404() {
    // Negative: ghost collection must be 404.
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/collections/ghost_collection/points/delete")
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "ids": [1, 2] }).to_string()))
                .expect("test: build delete request"),
        )
        .await
        .expect("test: delete request");
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn bulk_delete_malformed_payload_returns_400() {
    // Negative: missing `ids` field must be a deserialisation failure (4xx).
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 1).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/points/delete"))
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "wrong_field": [1, 2] }).to_string()))
                .expect("test: build delete request"),
        )
        .await
        .expect("test: delete request");
    assert!(
        response.status().is_client_error(),
        "expected 4xx for malformed payload, got {}",
        response.status()
    );
}

// ---------------------------------------------------------------------
// /vacuum — vacuum_collection (alias of /index/rebuild, by design)
// ---------------------------------------------------------------------

#[tokio::test]
async fn vacuum_nominal_returns_200_with_compacted_count() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 4).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/vacuum"))
                .header("Content-Type", "application/json")
                .body(Body::empty())
                .expect("test: build vacuum request"),
        )
        .await
        .expect("test: vacuum request");
    assert_eq!(response.status(), StatusCode::OK);

    let json = read_json(response).await;
    assert_eq!(json["message"], "Index vacuumed");
    assert_eq!(json["collection"], TEST_COLLECTION);
    assert!(
        json["compacted_entries"].is_number(),
        "compacted_entries must be a number, got {:?}",
        json["compacted_entries"]
    );
}

#[tokio::test]
async fn vacuum_empty_collection_returns_200() {
    // Edge: vacuuming a collection with 0 vectors must succeed (no-op).
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 0).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/vacuum"))
                .body(Body::empty())
                .expect("test: build vacuum request"),
        )
        .await
        .expect("test: vacuum request");
    assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn vacuum_unknown_collection_returns_404() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/collections/ghost_collection/vacuum")
                .body(Body::empty())
                .expect("test: build vacuum request"),
        )
        .await
        .expect("test: vacuum request");
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ---------------------------------------------------------------------
// /compact — compact_collection
// ---------------------------------------------------------------------

#[tokio::test]
async fn compact_nominal_returns_200_with_bytes_reclaimed() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 6).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/compact"))
                .body(Body::empty())
                .expect("test: build compact request"),
        )
        .await
        .expect("test: compact request");
    assert_eq!(response.status(), StatusCode::OK);

    let json = read_json(response).await;
    assert_eq!(json["message"], "Storage compacted");
    assert_eq!(json["collection"], TEST_COLLECTION);
    assert!(
        json["bytes_reclaimed"].is_number(),
        "bytes_reclaimed must be a number, got {:?}",
        json["bytes_reclaimed"]
    );
}

#[tokio::test]
async fn compact_empty_collection_returns_200() {
    // Edge: compacting an empty collection is a valid no-op.
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 0).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/compact"))
                .body(Body::empty())
                .expect("test: build compact request"),
        )
        .await
        .expect("test: compact request");
    assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn compact_unknown_collection_returns_404() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/collections/ghost_collection/compact")
                .body(Body::empty())
                .expect("test: build compact request"),
        )
        .await
        .expect("test: compact request");
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ---------------------------------------------------------------------
// /locality/reorder — reorder_for_locality
// ---------------------------------------------------------------------

#[tokio::test]
async fn reorder_nominal_returns_200() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 6).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/locality/reorder"))
                .body(Body::empty())
                .expect("test: build reorder request"),
        )
        .await
        .expect("test: reorder request");
    assert_eq!(response.status(), StatusCode::OK);

    let json = read_json(response).await;
    assert_eq!(json["message"], "Locality reordered");
    assert_eq!(json["collection"], TEST_COLLECTION);
}

#[tokio::test]
async fn reorder_empty_collection_returns_200() {
    // Edge: reordering an empty collection is a valid no-op.
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 0).await;

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/locality/reorder"))
                .body(Body::empty())
                .expect("test: build reorder request"),
        )
        .await
        .expect("test: reorder request");
    assert_eq!(response.status(), StatusCode::OK);
}

#[tokio::test]
async fn reorder_unknown_collection_returns_404() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);

    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/collections/ghost_collection/locality/reorder")
                .body(Body::empty())
                .expect("test: build reorder request"),
        )
        .await
        .expect("test: reorder request");
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

// ---------------------------------------------------------------------
// Cross-endpoint sanity: delete -> vacuum -> compact lifecycle
// ---------------------------------------------------------------------

#[tokio::test]
async fn lifecycle_delete_then_vacuum_then_compact_succeeds() {
    let temp_dir = TempDir::new().expect("test: temp dir");
    let app = create_test_app(&temp_dir);
    seed_collection(app.clone(), 8).await;

    // Delete half the points.
    let response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/points/delete"))
                .header("Content-Type", "application/json")
                .body(Body::from(json!({ "ids": [1, 2, 3, 4] }).to_string()))
                .expect("test: build delete request"),
        )
        .await
        .expect("test: delete request");
    assert_eq!(response.status(), StatusCode::OK);

    // Vacuum the (now half-empty) index.
    let response = app
        .clone()
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/vacuum"))
                .body(Body::empty())
                .expect("test: build vacuum request"),
        )
        .await
        .expect("test: vacuum request");
    assert_eq!(response.status(), StatusCode::OK);

    // Compact the storage.
    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri(format!("/collections/{TEST_COLLECTION}/compact"))
                .body(Body::empty())
                .expect("test: build compact request"),
        )
        .await
        .expect("test: compact request");
    assert_eq!(response.status(), StatusCode::OK);
}