cartog-db 0.29.3

SQLite persistence layer for cartog code graph
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
use super::test_symbol;
use crate::*;

// ── RAG: Symbol Content Tests ──

#[test]
fn test_upsert_and_get_symbol_content() {
    let db = Database::open_memory().unwrap();
    let sym = test_symbol("my_func", SymbolKind::Function, "a.py", 1);
    db.insert_symbol(&sym).unwrap();

    db.upsert_symbol_content(
        &sym.id,
        "my_func",
        "def my_func(): pass",
        "// File: a.py\n// Type: function\n// Name: my_func",
    )
    .unwrap();

    let result = db.get_symbol_content(&sym.id).unwrap();
    assert!(result.is_some());
    let (content, header) = result.unwrap();
    assert_eq!(content, "def my_func(): pass");
    assert!(header.contains("my_func"));
}

#[test]
fn test_insert_symbol_contents_batch() {
    let db = Database::open_memory().unwrap();
    let sym1 = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let sym2 = test_symbol("bar", SymbolKind::Function, "a.py", 10);
    db.insert_symbols(&[sym1.clone(), sym2.clone()]).unwrap();

    let items = vec![
        (
            sym1.id.clone(),
            "foo".to_string(),
            "def foo(): pass".to_string(),
            "header1".to_string(),
        ),
        (
            sym2.id.clone(),
            "bar".to_string(),
            "def bar(): pass".to_string(),
            "header2".to_string(),
        ),
    ];
    db.insert_symbol_contents(&items).unwrap();

    assert_eq!(db.symbol_content_count().unwrap(), 2);
    assert!(db.get_symbol_content(&sym1.id).unwrap().is_some());
    assert!(db.get_symbol_content(&sym2.id).unwrap().is_some());
}

#[test]
fn test_clear_symbol_content_for_file() {
    let db = Database::open_memory().unwrap();
    let sym1 = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let sym2 = test_symbol("bar", SymbolKind::Function, "b.py", 1);
    db.insert_symbols(&[sym1.clone(), sym2.clone()]).unwrap();

    db.upsert_symbol_content(&sym1.id, "foo", "content1", "header1")
        .unwrap();
    db.upsert_symbol_content(&sym2.id, "bar", "content2", "header2")
        .unwrap();
    assert_eq!(db.symbol_content_count().unwrap(), 2);

    db.clear_symbol_content_for_file("a.py").unwrap();
    assert_eq!(db.symbol_content_count().unwrap(), 1);
    assert!(db.get_symbol_content(&sym1.id).unwrap().is_none());
    assert!(db.get_symbol_content(&sym2.id).unwrap().is_some());
}

// ── RAG: FTS5 Tests ──

#[test]
fn test_fts5_search_by_content() {
    let db = Database::open_memory().unwrap();
    let sym = test_symbol("validate_token", SymbolKind::Function, "auth.py", 1);
    db.insert_symbol(&sym).unwrap();

    db.upsert_symbol_content(
        &sym.id,
        "validate_token",
        "def validate_token(token: str) -> bool:\n    return token.is_valid()",
        "// File: auth.py",
    )
    .unwrap();

    // Search by content keyword
    let results = db.fts5_search("\"validate\"", 10).unwrap();
    assert!(!results.is_empty());
    assert_eq!(results[0], sym.id);
}

#[test]
fn test_fts5_search_no_match() {
    let db = Database::open_memory().unwrap();
    let sym = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    db.insert_symbol(&sym).unwrap();
    db.upsert_symbol_content(&sym.id, "foo", "def foo(): pass", "header")
        .unwrap();

    let results = db.fts5_search("\"nonexistent_term_xyz\"", 10).unwrap();
    assert!(results.is_empty());
}

#[test]
fn fts5_drops_old_content_when_symbol_content_is_replaced() {
    // Re-indexing a symbol (INSERT OR REPLACE on symbol_content) must not
    // leave the previous content searchable. Without an explicit delete the
    // FTS5 external-content delete trigger does not fire on REPLACE-conflict
    // (recursive_triggers is off), so a stale secret stays searchable.
    let db = Database::open_memory().unwrap();
    let sym = test_symbol("load", SymbolKind::Function, "a.py", 1);
    db.insert_symbol(&sym).unwrap();

    db.upsert_symbol_content(&sym.id, "load", "key = ghp_oldsecrettoken_value", "h")
        .unwrap();
    assert!(!db
        .fts5_search("\"ghp_oldsecrettoken_value\"", 10)
        .unwrap()
        .is_empty());

    db.upsert_symbol_content(&sym.id, "load", "key = [REDACTED_SECRET]", "h")
        .unwrap();

    // Assert against the raw FTS index, not the JOIN-filtered fts5_search:
    // an orphaned FTS row survives the JOIN filter but still leaks the
    // plaintext token at the index level.
    let stale: i64 = db
        .conn
        .query_row(
            "SELECT count(*) FROM symbol_fts WHERE symbol_fts MATCH 'ghp_oldsecrettoken_value'",
            [],
            |r| r.get(0),
        )
        .unwrap();
    assert_eq!(stale, 0, "old plaintext must not remain in the FTS index");
    assert_eq!(db.symbol_content_count().unwrap(), 1);
}

// ── RAG: Embedding Map Tests ──

#[test]
fn test_get_or_create_embedding_id() {
    let db = Database::open_memory().unwrap();

    let id1 = db.get_or_create_embedding_id("a.py:foo:1").unwrap();
    let id2 = db.get_or_create_embedding_id("a.py:foo:1").unwrap();
    let id3 = db.get_or_create_embedding_id("b.py:bar:5").unwrap();

    assert_eq!(id1, id2, "same symbol should return same ID");
    assert_ne!(id1, id3, "different symbols should get different IDs");
}

#[test]
fn test_symbol_id_for_embedding() {
    let db = Database::open_memory().unwrap();
    let eid = db.get_or_create_embedding_id("test:sym:1").unwrap();

    let sym_id = db.symbol_id_for_embedding(eid).unwrap();
    assert_eq!(sym_id, Some("test:sym:1".to_string()));

    let none = db.symbol_id_for_embedding(99999).unwrap();
    assert!(none.is_none());
}

#[test]
fn test_symbol_ids_for_embeddings_batch() {
    let db = Database::open_memory().unwrap();
    let eid1 = db.get_or_create_embedding_id("a:foo:1").unwrap();
    let eid2 = db.get_or_create_embedding_id("b:bar:2").unwrap();

    let results = db.symbol_ids_for_embeddings(&[eid1, eid2]).unwrap();
    assert_eq!(results.len(), 2);
}

// ── RAG: Vector Storage Tests ──

#[test]
fn test_upsert_and_search_embedding() {
    let db = Database::open_memory().unwrap();
    let eid = db.get_or_create_embedding_id("a:foo:1").unwrap();

    // Create a simple 384-dim vector
    let mut embedding = vec![0.0f32; 384];
    embedding[0] = 1.0;
    let bytes: Vec<u8> = embedding.iter().flat_map(|f| f.to_le_bytes()).collect();

    db.upsert_embedding(eid, &bytes).unwrap();

    // Search with a similar vector
    let query = bytes.clone();
    let results = db.vector_search(&query, 5).unwrap();

    assert_eq!(results.len(), 1);
    assert_eq!(results[0].0, eid);
    assert!(
        results[0].1 < 0.01,
        "self-match should have near-zero distance"
    );
}

#[test]
fn test_insert_embeddings_batch() {
    let db = Database::open_memory().unwrap();
    let eid1 = db.get_or_create_embedding_id("a:foo:1").unwrap();
    let eid2 = db.get_or_create_embedding_id("b:bar:2").unwrap();

    let make_vec = |val: f32| -> Vec<u8> {
        let v = vec![val; 384];
        v.iter().flat_map(|f| f.to_le_bytes()).collect()
    };

    let items = vec![(eid1, make_vec(0.1)), (eid2, make_vec(0.9))];
    db.insert_embeddings(&items).unwrap();

    assert_eq!(db.embedding_count().unwrap(), 2);
}

#[test]
fn test_has_embedding() {
    let db = Database::open_memory().unwrap();
    assert!(!db.has_embedding("nonexistent").unwrap());

    let eid = db.get_or_create_embedding_id("a:foo:1").unwrap();
    // Map exists but no vector yet
    assert!(!db.has_embedding("a:foo:1").unwrap());

    // Insert vector
    let bytes: Vec<u8> = vec![0.0f32; 384]
        .iter()
        .flat_map(|f| f.to_le_bytes())
        .collect();
    db.upsert_embedding(eid, &bytes).unwrap();
    assert!(db.has_embedding("a:foo:1").unwrap());
}

#[test]
fn test_clear_all_embeddings() {
    let db = Database::open_memory().unwrap();
    let eid1 = db.get_or_create_embedding_id("a:foo:1").unwrap();
    let eid2 = db.get_or_create_embedding_id("b:bar:2").unwrap();

    let bytes: Vec<u8> = vec![0.0f32; 384]
        .iter()
        .flat_map(|f| f.to_le_bytes())
        .collect();
    db.upsert_embedding(eid1, &bytes).unwrap();
    db.upsert_embedding(eid2, &bytes).unwrap();
    assert_eq!(db.embedding_count().unwrap(), 2);

    db.clear_all_embeddings().unwrap();
    assert_eq!(db.embedding_count().unwrap(), 0);
}

#[test]
fn embedding_count_excludes_orphan_map_rows() {
    let db = Database::open_memory().unwrap();
    // Orphan map row (no vector yet) must not count as a usable embedding.
    let _eid = db.get_or_create_embedding_id("a:foo:1").unwrap();
    assert_eq!(db.embedding_count().unwrap(), 0);

    // Once a vector is written it counts.
    let eid = db.get_or_create_embedding_id("a:foo:1").unwrap();
    let bytes: Vec<u8> = vec![0.0f32; 384]
        .iter()
        .flat_map(|f| f.to_le_bytes())
        .collect();
    db.upsert_embedding(eid, &bytes).unwrap();
    assert_eq!(db.embedding_count().unwrap(), 1);
}

#[test]
fn test_symbols_needing_embeddings() {
    let db = Database::open_memory().unwrap();
    let sym1 = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let sym2 = test_symbol("bar", SymbolKind::Function, "a.py", 10);
    db.insert_symbols(&[sym1.clone(), sym2.clone()]).unwrap();

    // Add content for both
    db.upsert_symbol_content(&sym1.id, "foo", "def foo(): pass", "header")
        .unwrap();
    db.upsert_symbol_content(&sym2.id, "bar", "def bar(): pass", "header")
        .unwrap();

    // Both need embeddings initially
    let needing = db.symbols_needing_embeddings().unwrap();
    assert_eq!(needing.len(), 2);

    // Embed one
    let eid = db.get_or_create_embedding_id(&sym1.id).unwrap();
    let bytes: Vec<u8> = vec![0.0f32; 384]
        .iter()
        .flat_map(|f| f.to_le_bytes())
        .collect();
    db.upsert_embedding(eid, &bytes).unwrap();

    // Only one needs embedding now
    let needing = db.symbols_needing_embeddings().unwrap();
    assert_eq!(needing.len(), 1);
    assert_eq!(needing[0], sym2.id);
}

#[test]
fn test_clear_rag_data_for_file() {
    let db = Database::open_memory().unwrap();
    let sym1 = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let sym2 = test_symbol("bar", SymbolKind::Function, "b.py", 1);
    db.insert_symbols(&[sym1.clone(), sym2.clone()]).unwrap();

    db.upsert_symbol_content(&sym1.id, "foo", "content1", "header1")
        .unwrap();
    db.upsert_symbol_content(&sym2.id, "bar", "content2", "header2")
        .unwrap();

    let eid1 = db.get_or_create_embedding_id(&sym1.id).unwrap();
    let eid2 = db.get_or_create_embedding_id(&sym2.id).unwrap();
    let bytes: Vec<u8> = vec![0.0f32; 384]
        .iter()
        .flat_map(|f| f.to_le_bytes())
        .collect();
    db.upsert_embedding(eid1, &bytes).unwrap();
    db.upsert_embedding(eid2, &bytes).unwrap();

    // Clear RAG data for a.py only
    db.clear_rag_data_for_file("a.py").unwrap();

    // a.py data gone
    assert!(db.get_symbol_content(&sym1.id).unwrap().is_none());
    assert!(!db.has_embedding(&sym1.id).unwrap());

    // b.py data intact
    assert!(db.get_symbol_content(&sym2.id).unwrap().is_some());
    assert!(db.has_embedding(&sym2.id).unwrap());
}

#[test]
fn clear_embeddings_for_symbols_drops_only_named_ids() {
    let db = Database::open_memory().unwrap();
    let sym1 = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let sym2 = test_symbol("bar", SymbolKind::Function, "a.py", 10);
    db.insert_symbols(&[sym1.clone(), sym2.clone()]).unwrap();
    db.upsert_symbol_content(&sym1.id, "foo", "def foo(): pass", "header")
        .unwrap();
    db.upsert_symbol_content(&sym2.id, "bar", "def bar(): pass", "header")
        .unwrap();

    let bytes: Vec<u8> = vec![0.0f32; 384]
        .iter()
        .flat_map(|f| f.to_le_bytes())
        .collect();
    for sym in [&sym1, &sym2] {
        let eid = db.get_or_create_embedding_id(&sym.id).unwrap();
        db.upsert_embedding(eid, &bytes).unwrap();
    }
    assert_eq!(db.embedding_count().unwrap(), 2);

    let tx = db.begin_indexing_tx().unwrap();
    db.clear_embeddings_for_symbols_in_tx(std::slice::from_ref(&sym1.id))
        .unwrap();
    tx.commit().unwrap();

    // sym1's embedding gone, sym2 intact, content untouched for both.
    assert!(!db.has_embedding(&sym1.id).unwrap());
    assert!(db.has_embedding(&sym2.id).unwrap());
    assert!(db.get_symbol_content(&sym1.id).unwrap().is_some());
    // The cleared symbol is now back in the needs-embedding set.
    let needing = db.symbols_needing_embeddings().unwrap();
    assert_eq!(needing, vec![sym1.id.clone()]);
}

#[test]
fn clear_embeddings_for_symbols_is_noop_for_unembedded_id() {
    let db = Database::open_memory().unwrap();
    let sym = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    db.insert_symbols(std::slice::from_ref(&sym)).unwrap();
    db.upsert_symbol_content(&sym.id, "foo", "def foo(): pass", "header")
        .unwrap();

    let tx = db.begin_indexing_tx().unwrap();
    db.clear_embeddings_for_symbols_in_tx(std::slice::from_ref(&sym.id))
        .unwrap();
    tx.commit().unwrap();

    assert_eq!(db.embedding_count().unwrap(), 0);
}

#[test]
fn test_all_content_symbol_ids() {
    let db = Database::open_memory().unwrap();
    let sym1 = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let sym2 = test_symbol("bar", SymbolKind::Function, "b.py", 1);
    db.insert_symbols(&[sym1.clone(), sym2.clone()]).unwrap();

    db.upsert_symbol_content(&sym1.id, "foo", "content1", "header1")
        .unwrap();
    db.upsert_symbol_content(&sym2.id, "bar", "content2", "header2")
        .unwrap();

    let all = db.all_content_symbol_ids().unwrap();
    assert_eq!(all.len(), 2);
}

#[test]
fn test_symbols_needing_embeddings_excludes_variables() {
    let db = Database::open_memory().unwrap();
    let func = test_symbol("process", SymbolKind::Function, "a.py", 1);
    let var = test_symbol("MAX_RETRIES", SymbolKind::Variable, "a.py", 10);
    let cls = test_symbol("Service", SymbolKind::Class, "a.py", 20);
    db.insert_symbols(&[func.clone(), var.clone(), cls.clone()])
        .unwrap();

    // Add content for all three
    db.upsert_symbol_content(&func.id, "process", "def process(): pass", "header")
        .unwrap();
    db.upsert_symbol_content(&var.id, "MAX_RETRIES", "MAX_RETRIES = 3", "header")
        .unwrap();
    db.upsert_symbol_content(&cls.id, "Service", "class Service: pass", "header")
        .unwrap();

    // Only function and class should need embeddings (variable excluded)
    let needing = db.symbols_needing_embeddings().unwrap();
    assert_eq!(needing.len(), 2);
    assert!(!needing.contains(&var.id), "variables should be excluded");
    assert!(needing.contains(&func.id));
    assert!(needing.contains(&cls.id));
}

#[test]
fn test_all_content_symbol_ids_excludes_variables() {
    let db = Database::open_memory().unwrap();
    let func = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let var = test_symbol("MY_VAR", SymbolKind::Variable, "a.py", 10);
    let method = test_symbol("bar", SymbolKind::Method, "a.py", 20);
    db.insert_symbols(&[func.clone(), var.clone(), method.clone()])
        .unwrap();

    db.upsert_symbol_content(&func.id, "foo", "def foo(): pass", "header")
        .unwrap();
    db.upsert_symbol_content(&var.id, "MY_VAR", "MY_VAR = 42", "header")
        .unwrap();
    db.upsert_symbol_content(&method.id, "bar", "def bar(self): pass", "header")
        .unwrap();

    let all = db.all_content_symbol_ids().unwrap();
    assert_eq!(all.len(), 2, "variables should be excluded");
    assert!(!all.contains(&var.id));
}

#[test]
fn test_get_symbol_contents_batch() {
    let db = Database::open_memory().unwrap();
    let sym1 = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    let sym2 = test_symbol("bar", SymbolKind::Function, "a.py", 10);
    let sym3 = test_symbol("baz", SymbolKind::Function, "a.py", 20);
    db.insert_symbols(&[sym1.clone(), sym2.clone(), sym3.clone()])
        .unwrap();

    db.upsert_symbol_content(&sym1.id, "foo", "def foo(): pass", "h1")
        .unwrap();
    db.upsert_symbol_content(&sym2.id, "bar", "def bar(): pass", "h2")
        .unwrap();
    // sym3 has no content

    let ids = vec![sym1.id.clone(), sym2.id.clone(), sym3.id.clone()];
    let map = db.get_symbol_contents_batch(&ids).unwrap();
    assert_eq!(map.len(), 2);
    assert!(map.contains_key(&sym1.id));
    assert!(map.contains_key(&sym2.id));
    assert!(!map.contains_key(&sym3.id));
    assert_eq!(map[&sym1.id].0, "def foo(): pass");
}

#[test]
fn test_get_symbol_contents_batch_empty() {
    let db = Database::open_memory().unwrap();
    let map = db.get_symbol_contents_batch(&[]).unwrap();
    assert!(map.is_empty());
}

#[test]
fn test_get_symbol_by_id() {
    let db = Database::open_memory().unwrap();
    let sym = test_symbol("foo", SymbolKind::Function, "a.py", 1);
    db.insert_symbol(&sym).unwrap();

    let found = db.get_symbol(&sym.id).unwrap();
    assert!(found.is_some());
    assert_eq!(found.unwrap().name, "foo");

    let not_found = db.get_symbol("nonexistent").unwrap();
    assert!(not_found.is_none());
}

#[test]
fn test_symbols_for_files_basic() {
    let db = Database::open_memory().unwrap();
    let s1 = test_symbol("func_a", SymbolKind::Function, "src/a.py", 1);
    let s2 = test_symbol("func_b", SymbolKind::Function, "src/a.py", 10);
    let s3 = test_symbol("ClassC", SymbolKind::Class, "src/b.py", 1);
    let s4 = test_symbol("func_d", SymbolKind::Function, "src/c.py", 1);
    db.insert_symbols(&[s1, s2, s3, s4]).unwrap();

    // Query for two files
    let files = vec!["src/a.py".to_string(), "src/b.py".to_string()];
    let results = db.symbols_for_files(&files, None).unwrap();
    assert_eq!(results.len(), 3);
    assert_eq!(results[0].file_path, "src/a.py");
    assert_eq!(results[2].file_path, "src/b.py");
}

#[test]
fn test_symbols_for_files_kind_filter() {
    let db = Database::open_memory().unwrap();
    let s1 = test_symbol("func_a", SymbolKind::Function, "src/a.py", 1);
    let s2 = test_symbol("ClassB", SymbolKind::Class, "src/a.py", 10);
    db.insert_symbols(&[s1, s2]).unwrap();

    let files = vec!["src/a.py".to_string()];
    let results = db
        .symbols_for_files(&files, Some(SymbolKind::Function))
        .unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].name, "func_a");
}

#[test]
fn test_symbols_for_files_empty_input() {
    let db = Database::open_memory().unwrap();
    let results = db.symbols_for_files(&[], None).unwrap();
    assert!(results.is_empty());
}

#[test]
fn test_symbols_for_files_no_matching_files() {
    let db = Database::open_memory().unwrap();
    let s1 = test_symbol("func_a", SymbolKind::Function, "src/a.py", 1);
    db.insert_symbol(&s1).unwrap();

    let files = vec!["src/nonexistent.py".to_string()];
    let results = db.symbols_for_files(&files, None).unwrap();
    assert!(results.is_empty());
}

// ── In-degree centrality tests ──

#[test]
fn test_compute_in_degrees() {
    let db = Database::open_memory().unwrap();
    let s1 = test_symbol("func_a", SymbolKind::Function, "a.py", 1);
    let s2 = test_symbol("func_b", SymbolKind::Function, "b.py", 1);
    let s3 = test_symbol("func_c", SymbolKind::Function, "c.py", 1);
    db.insert_symbols(&[s1.clone(), s2.clone(), s3.clone()])
        .unwrap();

    // func_b calls func_a (2 call sites), func_c calls func_a (1 call site)
    let e1 = Edge::new(&s2.id, "func_a", EdgeKind::Calls, "b.py", 5);
    let e2 = Edge::new(&s2.id, "func_a", EdgeKind::Calls, "b.py", 10);
    let e3 = Edge::new(&s3.id, "func_a", EdgeKind::Calls, "c.py", 3);
    // func_c also calls func_b
    let e4 = Edge::new(&s3.id, "func_b", EdgeKind::Calls, "c.py", 7);
    db.insert_edges(&[e1, e2, e3, e4]).unwrap();
    db.resolve_edges().unwrap();
    db.compute_in_degrees().unwrap();

    let sym_a = db.get_symbol(&s1.id).unwrap().unwrap();
    let sym_b = db.get_symbol(&s2.id).unwrap().unwrap();
    let sym_c = db.get_symbol(&s3.id).unwrap().unwrap();

    assert_eq!(sym_a.in_degree, 3, "func_a should have 3 incoming edges");
    assert_eq!(sym_b.in_degree, 1, "func_b should have 1 incoming edge");
    assert_eq!(sym_c.in_degree, 0, "func_c should have 0 incoming edges");
}

#[test]
fn test_compute_in_degrees_resets() {
    let db = Database::open_memory().unwrap();
    let s1 = test_symbol("func_a", SymbolKind::Function, "a.py", 1);
    db.insert_symbol(&s1).unwrap();

    // Manually set in_degree to 99
    db.conn
        .execute(
            "UPDATE symbols SET in_degree = 99 WHERE id = ?1",
            params![s1.id],
        )
        .unwrap();

    // compute_in_degrees should reset to 0 (no edges)
    db.compute_in_degrees().unwrap();
    let sym = db.get_symbol(&s1.id).unwrap().unwrap();
    assert_eq!(sym.in_degree, 0);
}

#[test]
fn test_top_symbols_ordered_by_centrality() {
    let db = Database::open_memory().unwrap();
    let s1 = test_symbol("hub", SymbolKind::Function, "a.py", 1);
    let s2 = test_symbol("leaf", SymbolKind::Function, "b.py", 1);
    let s3 = test_symbol("mid", SymbolKind::Function, "c.py", 1);
    db.insert_symbols(&[s1.clone(), s2.clone(), s3.clone()])
        .unwrap();

    // Set in-degrees directly for testing
    db.conn
        .execute(
            "UPDATE symbols SET in_degree = 10 WHERE id = ?1",
            params![s1.id],
        )
        .unwrap();
    db.conn
        .execute(
            "UPDATE symbols SET in_degree = 1 WHERE id = ?1",
            params![s2.id],
        )
        .unwrap();
    db.conn
        .execute(
            "UPDATE symbols SET in_degree = 5 WHERE id = ?1",
            params![s3.id],
        )
        .unwrap();

    let top = db.top_symbols(10).unwrap();
    assert_eq!(top.len(), 3);
    assert_eq!(top[0].name, "hub");
    assert_eq!(top[0].in_degree, 10);
    assert_eq!(top[1].name, "mid");
    assert_eq!(top[2].name, "leaf");
}

#[test]
fn test_search_uses_in_degree_tiebreaker() {
    let db = Database::open_memory().unwrap();
    // Two functions with same name prefix, different centrality
    let s1 = test_symbol("parse_request", SymbolKind::Function, "a.py", 1);
    let s2 = test_symbol("parse_response", SymbolKind::Function, "b.py", 1);
    db.insert_symbols(&[s1.clone(), s2.clone()]).unwrap();

    db.conn
        .execute(
            "UPDATE symbols SET in_degree = 20 WHERE id = ?1",
            params![s1.id],
        )
        .unwrap();
    db.conn
        .execute(
            "UPDATE symbols SET in_degree = 5 WHERE id = ?1",
            params![s2.id],
        )
        .unwrap();

    let results = db.search("parse", None, None, 10).unwrap();
    assert_eq!(results.len(), 2);
    // parse_request (in_degree=20) should come before parse_response (in_degree=5)
    assert_eq!(results[0].name, "parse_request");
    assert_eq!(results[1].name, "parse_response");
}

#[test]
fn test_schema_version_stored() {
    let db = Database::open_memory().unwrap();
    let version = db.get_metadata("schema_version").unwrap();
    assert!(version.is_some());
    assert_eq!(version.unwrap(), SCHEMA_VERSION.to_string());
}