sqlitegraph 3.2.5

Embedded graph database with full ACID transactions, HNSW vector search, dual backend support, and comprehensive graph algorithms library
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! Comprehensive HNSW Persistence Tests
//!
//! Test suite for validating HNSW index persistence across sessions.

use rusqlite::Connection;
use sqlitegraph::{
    SqliteGraph,
    hnsw::{DistanceMetric, HnswConfig, HnswIndex},
    schema::ensure_schema,
};
use tempfile::TempDir;

/// Test metadata persistence across database reconnection
#[test]
fn test_hnsw_metadata_persistence() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    // Session 1: Create index and save metadata
    {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(128, 16, 200, DistanceMetric::Cosine);
        let hnsw = HnswIndex::new("test_index", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();
    }

    // Session 2: Reopen and verify metadata loaded
    {
        let conn = Connection::open(&db_path).unwrap();
        let loaded = HnswIndex::load_metadata(&conn, "test_index").unwrap();

        assert_eq!(loaded.config().dimension, 128);
        assert_eq!(loaded.config().m, 16);
        assert_eq!(loaded.config().ef_construction, 200);
        assert_eq!(loaded.config().distance_metric, DistanceMetric::Cosine);
        assert_eq!(loaded.name(), "test_index");
    }
}

/// Test vector persistence across database reconnection
///
/// NOTE: This test manually persists vectors to the database to work around
/// the current limitation where HnswIndex uses InMemoryVectorStorage by default.
/// Full automatic vector persistence will be added in a future update.
#[test]
fn test_hnsw_vector_persistence() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let vectors = vec![
        vec![1.0_f32, 0.0, 0.0],
        vec![0.0, 1.0, 0.0],
        vec![0.0, 0.0, 1.0],
    ];

    // Session 1: Create index and manually persist vectors
    let index_id = {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        let hnsw = HnswIndex::new("test_index", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();

        // Get index ID
        conn.query_row(
            "SELECT id FROM hnsw_indexes WHERE name = ?",
            ["test_index"],
            |row| row.get::<_, i64>(0),
        )
        .unwrap()
    };

    // Manually insert vectors into database (simulating SQLiteVectorStorage)
    {
        let conn = Connection::open(&db_path).unwrap();
        for vector in &vectors {
            let vector_bytes = bytemuck::cast_slice::<f32, u8>(vector).to_vec();
            conn.execute(
                "INSERT INTO hnsw_vectors (index_id, vector_data, metadata, created_at, updated_at)
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params![index_id, vector_bytes, None::<String>, 1000, 1000],
            )
            .unwrap();
        }
    }

    // Session 2: Reopen and verify vectors loaded
    {
        let conn = Connection::open(&db_path).unwrap();
        let hnsw = HnswIndex::load_with_vectors(&conn, "test_index").unwrap();

        assert_eq!(hnsw.vector_count(), 3);
    }
}

/// Test full lifecycle: create -> insert -> close -> reopen -> search
///
/// NOTE: This test manually persists vectors to work around current limitations.
#[test]
fn test_hnsw_create_insert_close_reopen_search() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    // Create and insert vectors
    let index_id = {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        let hnsw = HnswIndex::new("lifecycle_test", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();

        conn.query_row(
            "SELECT id FROM hnsw_indexes WHERE name = ?",
            ["lifecycle_test"],
            |row| row.get::<_, i64>(0),
        )
        .unwrap()
    };

    // Manually insert vectors
    {
        let conn = Connection::open(&db_path).unwrap();
        for i in 0..10 {
            let vector = vec![i as f32, (i * 2) as f32, (i * 3) as f32];
            let vector_bytes = bytemuck::cast_slice::<f32, u8>(&vector).to_vec();
            conn.execute(
                "INSERT INTO hnsw_vectors (index_id, vector_data, metadata, created_at, updated_at)
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params![index_id, vector_bytes, None::<String>, 1000, 1000],
            )
            .unwrap();
        }
    }

    // Reopen and search
    {
        let conn = Connection::open(&db_path).unwrap();
        let hnsw = HnswIndex::load_with_vectors(&conn, "lifecycle_test").unwrap();

        assert_eq!(hnsw.vector_count(), 10);

        // Search for a similar vector
        let query = vec![5.0, 10.0, 15.0];
        let results = hnsw.search(&query, 3).unwrap();

        assert!(!results.is_empty(), "Search should return results");
        let (_best_id, distance) = &results[0];
        assert!(
            *distance < 5.0,
            "Distance should be small for similar vector"
        );
    }
}

/// Test empty index persistence
#[test]
fn test_hnsw_empty_index_persistence() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    // Create index without inserting vectors
    {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(5, 16, 200, DistanceMetric::Cosine);
        let hnsw = HnswIndex::new("empty_index", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();
        assert_eq!(hnsw.vector_count(), 0);
    }

    // Reopen and verify empty index loads
    {
        let conn = Connection::open(&db_path).unwrap();
        let hnsw = HnswIndex::load_with_vectors(&conn, "empty_index").unwrap();

        assert_eq!(hnsw.config().dimension, 5);
        assert_eq!(hnsw.vector_count(), 0);
    }
}

/// Test index deletion cascades to vectors
#[test]
fn test_hnsw_delete_index() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    // Create index with vectors
    {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        let mut hnsw = HnswIndex::new("delete_test", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();

        hnsw.insert_vector(&[1.0, 0.0, 0.0], None).unwrap();
        hnsw.insert_vector(&[0.0, 1.0, 0.0], None).unwrap();
        hnsw.insert_vector(&[0.0, 0.0, 1.0], None).unwrap();
    }

    // Delete index
    {
        let conn = Connection::open(&db_path).unwrap();
        HnswIndex::delete_index(&conn, "delete_test").unwrap();

        // Verify index gone
        let result = HnswIndex::load_metadata(&conn, "delete_test");
        assert!(result.is_err(), "Index should not exist after deletion");
    }
}

/// Test configuration preservation
#[test]
fn test_hnsw_config_preservation() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    // Create index with specific config
    {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(256, 32, 400, DistanceMetric::Euclidean);
        let hnsw = HnswIndex::new("config_test", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();
    }

    // Reopen and verify config matches
    {
        let conn = Connection::open(&db_path).unwrap();
        let loaded = HnswIndex::load_metadata(&conn, "config_test").unwrap();

        assert_eq!(loaded.config().dimension, 256);
        assert_eq!(loaded.config().m, 32);
        assert_eq!(loaded.config().ef_construction, 400);
        assert_eq!(loaded.config().distance_metric, DistanceMetric::Euclidean);
    }
}

/// Test all distance metrics persist correctly
#[test]
fn test_hnsw_distance_metric_preservation() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let metrics = [
        DistanceMetric::Euclidean,
        DistanceMetric::Cosine,
        DistanceMetric::DotProduct,
        DistanceMetric::Manhattan,
    ];

    for (i, metric) in metrics.iter().enumerate() {
        let index_name = format!("metric_test_{}", i);

        // Create index
        {
            let conn = Connection::open(&db_path).unwrap();
            ensure_schema(&conn).unwrap();

            let config = HnswConfig::new(10, 16, 200, *metric);
            let hnsw = HnswIndex::new(&index_name, config).unwrap();
            hnsw.save_metadata(&conn).unwrap();
        }

        // Verify metric preserved
        {
            let conn = Connection::open(&db_path).unwrap();
            let loaded = HnswIndex::load_metadata(&conn, &index_name).unwrap();
            assert_eq!(loaded.config().distance_metric, *metric);
        }
    }
}

/// Regression: delete_index must remove vectors from hnsw_vectors (Bug 4)
///
/// Before fix: delete_index only deleted from hnsw_indexes. FK CASCADE
/// didn't fire because pool connections lack PRAGMA foreign_keys=ON.
#[test]
fn test_delete_index_removes_vectors() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let index_id = {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        let hnsw = HnswIndex::new("cascade_test", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();

        conn.query_row(
            "SELECT id FROM hnsw_indexes WHERE name = ?",
            ["cascade_test"],
            |row| row.get::<_, i64>(0),
        )
        .unwrap()
    };

    {
        let conn = Connection::open(&db_path).unwrap();
        for v in &[vec![1.0_f32, 0.0, 0.0], vec![0.0, 1.0, 0.0]] {
            let bytes = bytemuck::cast_slice::<f32, u8>(v).to_vec();
            conn.execute(
                "INSERT INTO hnsw_vectors (index_id, vector_data, metadata, created_at, updated_at) VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params![index_id, bytes, None::<String>, 1000, 1000],
            ).unwrap();
        }
    }

    {
        let conn = Connection::open(&db_path).unwrap();
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM hnsw_vectors WHERE index_id = ?1",
                [index_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 2, "vectors should exist before delete");
    }

    {
        let conn = Connection::open(&db_path).unwrap();
        HnswIndex::delete_index(&conn, "cascade_test").unwrap();
    }

    {
        let conn = Connection::open(&db_path).unwrap();
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM hnsw_vectors WHERE index_id = ?1",
                [index_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 0, "delete_index must remove all vectors (Bug 4)");
    }
}

/// Regression: SqliteGraph.delete_hnsw_index must remove vectors via pool (Bug 4 real path)
///
/// Before fix: pool connections lack PRAGMA foreign_keys=ON, so CASCADE never
/// fires and hnsw_vectors rows are orphaned. This test proves it by checking
/// raw row count after delete — the FK constraint is in DDL but won't cascade
/// without the pragma.
#[test]
fn test_graph_delete_hnsw_index_removes_vectors() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");
    let index_id: i64;

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        {
            let _guard = graph
                .hnsw_index_persistent("graph_cascade", config)
                .unwrap();
        }

        graph
            .get_hnsw_index_mut("graph_cascade", |idx| {
                idx.insert_vector(&[1.0, 0.0, 0.0], None)
            })
            .unwrap()
            .unwrap();

        graph
            .get_hnsw_index_mut("graph_cascade", |idx| {
                idx.insert_vector(&[0.0, 1.0, 0.0], None)
            })
            .unwrap()
            .unwrap();

        let conn = Connection::open(&db_path).unwrap();
        index_id = conn
            .query_row(
                "SELECT id FROM hnsw_indexes WHERE name = ?",
                ["graph_cascade"],
                |row| row.get::<_, i64>(0),
            )
            .unwrap();

        let count_before: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM hnsw_vectors WHERE index_id = ?1",
                [index_id],
                |row| row.get(0),
            )
            .unwrap();
        assert!(
            count_before >= 2,
            "vectors must exist before delete, got {}",
            count_before
        );
    }

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        graph.delete_hnsw_index("graph_cascade").unwrap();
    }

    {
        let conn = Connection::open(&db_path).unwrap();
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM hnsw_vectors WHERE index_id = ?1",
                [index_id],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(
            count, 0,
            "delete_hnsw_index must remove all vectors via pool (Bug 4), got {} orphaned rows",
            count
        );
    }
}

/// Regression: delete+recreate persistent index via SqliteGraph (Bug 1 + Bug 4)
///
/// Before fix: hnsw_index_persistent uses auto-increment rowids that become
/// stale after delete, causing InvalidNodeId when HNSW layers expect
/// sequential 0-based IDs.
#[test]
fn test_persistent_index_delete_and_recreate() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        {
            let _guard = graph
                .hnsw_index_persistent("recreate_test", config)
                .unwrap();
        }

        graph
            .get_hnsw_index_mut("recreate_test", |idx| {
                idx.insert_vector(&[1.0, 0.0, 0.0], None)
            })
            .unwrap()
            .unwrap();

        graph
            .get_hnsw_index_mut("recreate_test", |idx| {
                idx.insert_vector(&[0.0, 1.0, 0.0], None)
            })
            .unwrap()
            .unwrap();

        let results = graph
            .get_hnsw_index_ref("recreate_test", |idx| idx.search(&[1.0, 0.0, 0.0], 1))
            .unwrap()
            .unwrap();
        assert!(
            !results.is_empty(),
            "search should find results before delete"
        );
    }

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        graph.delete_hnsw_index("recreate_test").unwrap();
    }

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        {
            let _guard = graph
                .hnsw_index_persistent("recreate_test", config)
                .unwrap();
        }

        for i in 0..5 {
            let v = vec![i as f32, 0.0, 0.0];
            graph
                .get_hnsw_index_mut("recreate_test", |idx| idx.insert_vector(&v, None))
                .unwrap()
                .unwrap();
        }

        let results = graph
            .get_hnsw_index_ref("recreate_test", |idx| idx.search(&[4.0, 0.0, 0.0], 1))
            .unwrap()
            .unwrap();
        assert!(
            !results.is_empty(),
            "search must work after delete+recreate (Bug 1)"
        );
    }
}

/// Regression: persistent index survives process restart via SqliteGraph (Bug 3 partial)
#[test]
fn test_persistent_index_survives_reopen() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        {
            let _guard = graph.hnsw_index_persistent("survive_test", config).unwrap();
        }

        for i in 0..3 {
            let v = vec![i as f32, 0.0, 0.0];
            graph
                .get_hnsw_index_mut("survive_test", |idx| idx.insert_vector(&v, None))
                .unwrap()
                .unwrap();
        }
    }

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        let names = graph.list_hnsw_indexes().unwrap();
        assert!(
            names.contains(&"survive_test".to_string()),
            "index must survive reopen (Bug 3)"
        );

        graph
            .get_hnsw_index_ref("survive_test", |idx| {
                assert_eq!(idx.vector_count(), 3, "vectors must survive reopen");
            })
            .unwrap();
    }
}
///
/// NOTE: This test manually persists vectors to work around current limitations.
#[test]
fn test_hnsw_graph_autoload() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    // Create index
    let index_id = {
        let conn = Connection::open(&db_path).unwrap();
        ensure_schema(&conn).unwrap();

        let config = HnswConfig::new(10, 16, 200, DistanceMetric::Cosine);
        let hnsw = HnswIndex::new("autoload_test", config).unwrap();
        hnsw.save_metadata(&conn).unwrap();

        conn.query_row(
            "SELECT id FROM hnsw_indexes WHERE name = ?",
            ["autoload_test"],
            |row| row.get::<_, i64>(0),
        )
        .unwrap()
    };

    // Manually insert vectors
    {
        let conn = Connection::open(&db_path).unwrap();
        for vector in &[vec![1.0; 10], vec![2.0; 10]] {
            let vector_bytes = bytemuck::cast_slice::<f32, u8>(vector).to_vec();
            conn.execute(
                "INSERT INTO hnsw_vectors (index_id, vector_data, metadata, created_at, updated_at)
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                rusqlite::params![index_id, vector_bytes, None::<String>, 1000, 1000],
            )
            .unwrap();
        }
    }

    // Reopen via SqliteGraph and verify auto-load
    {
        let graph = SqliteGraph::open(&db_path).unwrap();

        // Verify index was auto-loaded
        let index_names = graph.list_hnsw_indexes().unwrap();
        assert_eq!(index_names, vec!["autoload_test".to_string()]);

        // Verify vectors loaded
        graph
            .get_hnsw_index_ref("autoload_test", |hnsw| {
                assert_eq!(hnsw.vector_count(), 2);
            })
            .unwrap();
    }
}

/// Regression: HnswIndex::delete_vector removes a single vector by ID
#[test]
fn test_hnsw_delete_single_vector() {
    let temp_dir = TempDir::new().unwrap();
    let _db_path = temp_dir.path().join("test.db");

    let mut hnsw = {
        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        HnswIndex::new("delete_vec_test", config).unwrap()
    };

    let _id_a = hnsw.insert_vector(&[1.0, 0.0, 0.0], None).unwrap();
    let id_b = hnsw.insert_vector(&[0.0, 1.0, 0.0], None).unwrap();
    let _id_c = hnsw.insert_vector(&[0.0, 0.0, 1.0], None).unwrap();

    assert_eq!(hnsw.vector_count(), 3);

    hnsw.delete_vector(id_b).unwrap();
    assert_eq!(
        hnsw.vector_count(),
        2,
        "vector_count must decrease after delete"
    );

    let results = hnsw.search(&[1.0, 0.0, 0.0], 2).unwrap();
    assert!(!results.is_empty(), "search must still return results");
    let returned_ids: Vec<u64> = results.iter().map(|(id, _)| *id).collect();
    assert!(
        !returned_ids.contains(&id_b),
        "deleted vector must not appear in search results"
    );
}

/// Regression: SqliteGraph::delete_hnsw_vector removes vector from persistent index
#[test]
fn test_graph_delete_hnsw_vector() {
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("test.db");

    let mut vector_ids: Vec<u64> = Vec::new();

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        let config = HnswConfig::new(3, 16, 200, DistanceMetric::Euclidean);
        {
            let _guard = graph.hnsw_index_persistent("del_test", config).unwrap();
        }

        for i in 0..5 {
            let v = vec![i as f32, 0.0, 0.0];
            let id = graph
                .get_hnsw_index_mut("del_test", |idx| idx.insert_vector(&v, None))
                .unwrap()
                .unwrap();
            vector_ids.push(id);
        }

        graph
            .get_hnsw_index_ref("del_test", |idx| {
                assert_eq!(idx.vector_count(), 5);
            })
            .unwrap();
    }

    {
        let graph = SqliteGraph::open(&db_path).unwrap();
        graph.delete_hnsw_vector("del_test", vector_ids[2]).unwrap();

        graph
            .get_hnsw_index_ref("del_test", |idx| {
                assert_eq!(
                    idx.vector_count(),
                    4,
                    "count must be 4 after deleting 1 vector"
                );
            })
            .unwrap();

        let results = graph
            .get_hnsw_index_ref("del_test", |idx| idx.search(&[0.0, 0.0, 0.0], 5))
            .unwrap()
            .unwrap();
        let returned_ids: Vec<u64> = results.iter().map(|(id, _)| *id).collect();
        assert!(
            !returned_ids.contains(&vector_ids[2]),
            "deleted vector must not appear in results"
        );
    }
}