codenexus 0.3.3

A queryable code knowledge graph tool built on LadybugDB and tree-sitter
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
// Copyright (c) 2026 Kirky.X. All rights reserved.
// SPDX-License-Identifier: MIT

//! BM25 full-text search (PRD §4.4.3, H11).
//!
//! [`FullTextSearcher`] attempts to use the LadybugDB FTS extension
//! (`CALL fts_search(...)`) for BM25-ranked results. When the FTS extension is
//! unavailable (the common case in tests), it falls back to a `CONTAINS`-based
//! scan over the symbol tables, ranking results with a simple relevance score.
//!
//! # codenexus_tokenizer (H11)
//!
//! Both the FTS query and the CONTAINS fallback use [`codenexus_tokenize`] to
//! split camelCase / snake_case identifiers before matching. This enables
//! searching for `parse` to match `parseFile`, `parse_file`, and
//! `my_parse_helper` — a plain `CONTAINS` would only match the exact substring.

use super::error::{QueryError, Result};
use super::tokenizer::codenexus_tokenize;
use super::SearchResult;
use crate::model::NodeLabel;
use crate::storage::schema::{escape_cypher_string, escape_identifier, node_table_columns};
use crate::storage::StorageConnection;
use tracing::warn;

/// Symbol-bearing node labels searched by [`FullTextSearcher`] when falling
/// back to `CONTAINS`. Mirrors [`super::structured::SYMBOL_LABELS`].
const SYMBOL_LABELS: &[NodeLabel] = &[
    NodeLabel::Module,
    NodeLabel::Class,
    NodeLabel::Struct,
    NodeLabel::Enum,
    NodeLabel::Trait,
    NodeLabel::Impl,
    NodeLabel::Function,
    NodeLabel::Method,
    NodeLabel::Variable,
    NodeLabel::GlobalVar,
    NodeLabel::Const,
    NodeLabel::Static,
    NodeLabel::Macro,
    NodeLabel::TypeAlias,
    NodeLabel::Typedef,
    NodeLabel::Namespace,
];

/// FTS indexes on symbol `name` columns, created by
/// [`crate::storage::schema::index_ddl`]. Each entry pairs the FTS index name
/// with the [`NodeLabel`] used to tag search results.
///
/// Extended coverage from 3 tables (Function/Class/Method) to
/// all 15 symbol-bearing tables so that BM25 search reaches Struct/Enum/Trait/
/// Macro/Typedef/Namespace/Module/Variable/GlobalVar/Const/Static/TypeAlias.
const FTS_NAME_INDEXES: &[(&str, NodeLabel)] = &[
    ("fts_function_name", NodeLabel::Function),
    ("fts_class_name", NodeLabel::Class),
    ("fts_method_name", NodeLabel::Method),
    ("fts_struct_name", NodeLabel::Struct),
    ("fts_enum_name", NodeLabel::Enum),
    ("fts_trait_name", NodeLabel::Trait),
    ("fts_macro_name", NodeLabel::Macro),
    ("fts_typedef_name", NodeLabel::Typedef),
    ("fts_namespace_name", NodeLabel::Namespace),
    ("fts_module_name", NodeLabel::Module),
    ("fts_variable_name", NodeLabel::Variable),
    ("fts_globalvar_name", NodeLabel::GlobalVar),
    ("fts_const_name", NodeLabel::Const),
    ("fts_static_name", NodeLabel::Static),
    ("fts_typealias_name", NodeLabel::TypeAlias),
];

/// Executes BM25 full-text searches against a [`StorageConnection`].
///
/// Tries the LadybugDB FTS extension first; falls back to a `CONTAINS`-based
/// scan when FTS is unavailable.
pub struct FullTextSearcher<'a> {
    conn: &'a StorageConnection,
}

impl<'a> FullTextSearcher<'a> {
    /// Creates a new [`FullTextSearcher`] borrowing `conn`.
    #[must_use]
    pub fn new(conn: &'a StorageConnection) -> Self {
        Self { conn }
    }

    /// Searches for `text` using BM25 (FTS) when available, falling back to a
    /// `CONTAINS` scan when the FTS extension is unavailable. Results are sorted
    /// by descending relevance score.
    ///
    /// Only FTS errors that indicate the extension/index is unsupported (parser
    /// exceptions, "not supported", "does not exist", "already exists") trigger
    /// the fallback. Any other error (e.g. a genuine runtime failure) is
    /// propagated to the caller.
    pub fn search(
        &self,
        text: &str,
        project: Option<&str>,
        limit: usize,
    ) -> Result<Vec<SearchResult>> {
        if text.trim().is_empty() {
            return Err(QueryError::InvalidQuery(
                "fulltext query must not be empty".to_string(),
            ));
        }
        // Try the FTS extension first. If it is unavailable (or the FTS index
        // does not exist), fall back to a CONTAINS-based scan.
        match self.try_fts_search(text, project, limit) {
            Ok(results) => Ok(results),
            Err(e) if is_fts_unsupported_error(&e) => {
                warn!(error = %e, "FTS not supported, falling back to CONTAINS scan");
                self.fallback_contains_search(text, project, limit)
            }
            Err(e) => Err(e),
        }
    }

    /// Attempts a LadybugDB FTS query against the `name` FTS indexes (H11).
    ///
    /// Queries three FTS indexes — `fts_function_name`, `fts_class_name`,
    /// `fts_method_name` — created by [`crate::storage::schema::index_ddl`].
    /// The query is pre-tokenized via [`codenexus_tokenize`] so that
    /// `parseFile` becomes `parse file`, enabling the FTS engine to match
    /// individual sub-tokens.
    ///
    /// LadybugDB builds may not ship the FTS extension or the named index, so
    /// any error here is treated as "FTS unavailable" and the caller falls
    /// back to [`Self::fallback_contains_search`].
    fn try_fts_search(
        &self,
        text: &str,
        project: Option<&str>,
        limit: usize,
    ) -> Result<Vec<SearchResult>> {
        // H11: tokenize the query so camelCase/snake_case identifiers are split
        // into sub-tokens before passing to the FTS engine.
        let tokenized = codenexus_tokenize(text);
        if tokenized.is_empty() {
            // Single-line for coverage: tarpaulin attribute continuation
            return Err(QueryError::InvalidQuery(
                "fulltext query tokenized to empty".to_string(),
            ));
        }
        let fts_query = tokenized.join(" ");
        let escaped = escape_cypher_string(&fts_query);

        // Query all name-column FTS indexes and merge results.
        let mut all_results = Vec::new();
        for (index_name, label) in FTS_NAME_INDEXES {
            // Namespace and Module tables have no `startLine`
            // column; return NULL instead of erroring.
            let line_expr = if node_table_columns(*label).contains(&"startLine") {
                "node.startLine"
            } else {
                "NULL"
            };
            let cypher = match project {
                Some(p) => format!(
                    "CALL fts_search('{index_name}', '{escaped}') YIELD node \
                     WHERE node.project = '{}' \
                     RETURN node.name AS name, node.qualifiedName AS qn, \
                     node.filePath AS filePath, {line_expr} AS line;",
                    escape_cypher_string(p),
                ),
                None => format!(
                    "CALL fts_search('{index_name}', '{escaped}') YIELD node \
                     RETURN node.name AS name, node.qualifiedName AS qn, \
                     node.filePath AS filePath, {line_expr} AS line;",
                ),
            };
            match self.conn.query(&cypher) {
                Ok(rows) => {
                    all_results.extend(rows_to_search_results(rows, *label, text));
                }
                // Propagate the first error — the caller will check
                // `is_fts_unsupported_error` to decide whether to fall back.
                // Single-line for coverage: tarpaulin attribute continuation
                Err(e) => return Err(QueryError::from(e)),
            }
        }
        // FTS results are already BM25-ranked per-index; merge-sort by score.
        sort_and_truncate(&mut all_results, limit);
        Ok(all_results)
    }

    /// Fallback: scans symbol tables with `CONTAINS` and ranks by a relevance
    /// score (exact > prefix > token-match > substring). Case-insensitive.
    ///
    /// H11: the query is pre-tokenized via [`codenexus_tokenize`] so that a
    /// multi-token query like `parseFile` becomes `["parse", "file"]`. The
    /// WHERE clause ORs one `CONTAINS` per token, enabling `parseFile` to
    /// match `parse_file` (which contains both `parse` and `file` as
    /// substrings) — a plain single-substring `CONTAINS('parseFile')` would
    /// miss it.
    fn fallback_contains_search(
        &self,
        text: &str,
        project: Option<&str>,
        limit: usize,
    ) -> Result<Vec<SearchResult>> {
        let tokens = codenexus_tokenize(text);
        if tokens.is_empty() {
            // Single-line for coverage: tarpaulin attribute continuation
            return Err(QueryError::InvalidQuery(
                "fulltext query tokenized to empty".to_string(),
            ));
        }
        let or_clauses: Vec<String> = tokens
            .iter()
            .map(|t| {
                // Single-line for coverage: tarpaulin attribute continuation
                format!(
                    "toLower(n.name) CONTAINS toLower('{}')",
                    escape_cypher_string(t)
                )
            })
            .collect();
        let where_inner = or_clauses.join(" OR ");
        let mut results = Vec::new();
        for &label in SYMBOL_LABELS {
            let table = escape_identifier(label.table_name());
            // Namespace and Module tables have no `startLine`
            // column; return NULL instead of erroring so the fallback path
            // reaches all symbol tables (previously these were silently
            // skipped via `Err(_) => continue`).
            let line_expr = if node_table_columns(label).contains(&"startLine") {
                "n.startLine"
            } else {
                "NULL"
            };
            let cypher = match project {
                Some(p) => format!(
                    "MATCH (n:{table}) WHERE ({where_inner}) AND n.project = '{}' \
                     RETURN n.name AS name, n.qualifiedName AS qn, \
                     n.filePath AS filePath, {line_expr} AS line;",
                    escape_cypher_string(p),
                ),
                None => format!(
                    "MATCH (n:{table}) WHERE ({where_inner}) \
                     RETURN n.name AS name, n.qualifiedName AS qn, \
                     n.filePath AS filePath, {line_expr} AS line;",
                ),
            };
            match self.conn.query(&cypher) {
                Ok(rows) => results.extend(rows_to_search_results(rows, label, text)),
                Err(_) => continue,
            }
        }
        sort_and_truncate(&mut results, limit);
        Ok(results)
    }
}

/// Converts query rows into [`SearchResult`]s with a relevance score.
fn rows_to_search_results(
    rows: Vec<Vec<serde_json::Value>>,
    label: NodeLabel,
    query: &str,
) -> Vec<SearchResult> {
    let label_str = label.to_string();
    rows.into_iter()
        .filter_map(|row| {
            let name = row.first().and_then(|v| v.as_str())?.to_string();
            let qualified_name = row.get(1).and_then(|v| v.as_str()).map(String::from);
            let file_path = row.get(2).and_then(|v| v.as_str()).map(String::from);
            let start_line = row
                .get(3)
                .and_then(|v| v.as_i64())
                .and_then(|i| u32::try_from(i).ok());
            let (score, reason) = relevance_score_with_reason(&name, query);
            Some(SearchResult {
                name,
                label: label_str.clone(),
                file_path,
                start_line,
                qualified_name,
                score,
                match_reason: reason.to_string(),
                degree: 0,
            })
        })
        .collect()
}

/// Computes a relevance score in `[0.0, 1.0]` for `name` against `query`.
///
/// Scoring tiers (H11 token-aware):
/// - `1.0` — exact match (case-insensitive)
/// - `0.8` — name starts with query (prefix)
/// - `0.7` — every query token appears as a substring of some name token
///   (e.g. `my_parse_helper` vs `parse` → `["my","parse","helper"]` contains
///   `parse`)
/// - `0.5` — name contains query as a plain substring
/// - `0.3` — no match (defensive; callers pre-filter via `CONTAINS`)
#[allow(dead_code)]
fn relevance_score(name: &str, query: &str) -> f64 {
    relevance_score_with_reason(name, query).0
}

/// Computes both the score and a human-readable match reason.
fn relevance_score_with_reason(name: &str, query: &str) -> (f64, &'static str) {
    let name_lower = name.to_ascii_lowercase();
    let query_lower = query.to_ascii_lowercase();
    if name_lower == query_lower {
        return (1.0, "exact name match");
    }
    if name_lower.starts_with(&query_lower) {
        return (0.8, "prefix match");
    }
    let query_tokens = codenexus_tokenize(&query_lower);
    let name_tokens = codenexus_tokenize(&name_lower);
    if !query_tokens.is_empty() && !name_tokens.is_empty() {
        let all_match = query_tokens
            .iter()
            .all(|qt| name_tokens.iter().any(|nt| nt == qt));
        if all_match {
            return (0.7, "token-aligned match");
        }
    }
    if name_lower.contains(&query_lower) {
        return (0.5, "substring match");
    }
    (0.3, "no match")
}

/// Sorts results by descending score then ascending name, and truncates.
fn sort_and_truncate(results: &mut Vec<SearchResult>, limit: usize) {
    results.sort_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| a.name.cmp(&b.name))
    });
    if limit < results.len() {
        results.truncate(limit);
    }
}

/// Returns `true` when `err` indicates the FTS extension or FTS index is
/// unavailable in the linked LadybugDB build.
///
/// Mirrors the "unsupported DDL" classification in
/// [`crate::storage::connection::StorageConnection::run_init_ddl`]: parser
/// exceptions, "not supported", "does not exist", and "already exists" all
/// signal that the feature is absent rather than genuinely broken. Any other
/// error (e.g. "connection refused") is a real failure that should propagate.
fn is_fts_unsupported_error(err: &QueryError) -> bool {
    let msg = err.to_string().to_ascii_lowercase();
    msg.contains("not supported")
        || msg.contains("parser exception")
        || msg.contains("does not exist")
        || msg.contains("already exists")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Language, Node, NodeLabel};
    use crate::storage::Repository;

    fn fresh_repo() -> Repository {
        Repository::in_memory().expect("in_memory repository")
    }

    fn sample_function(
        id: &str,
        project: &str,
        name: &str,
        qn: &str,
        file: &str,
        line: u32,
    ) -> Node {
        Node::builder(NodeLabel::Function, name, qn)
            .id(id)
            .project(project)
            .file_path(file)
            .start_line(line)
            .end_line(line + 10)
            .language(Language::Rust)
            .signature("fn x()")
            .build()
    }

    #[test]
    fn search_falls_back_to_contains_when_fts_unavailable() {
        // No FTS index is created in the test DB, so this exercises the
        // fallback path.
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "parse_file", "demo.parse_file", "/a.rs", 1),
                sample_function("f2", "demo", "parse_line", "demo.parse_line", "/b.rs", 1),
                sample_function("f3", "demo", "read_input", "demo.read_input", "/a.rs", 10),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parse", None, 100).expect("search");
        let names: Vec<&str> = results.iter().map(|r| r.name.as_str()).collect();
        assert!(names.contains(&"parse_file"));
        assert!(names.contains(&"parse_line"));
        assert!(!names.contains(&"read_input"));
    }

    #[test]
    fn search_returns_results_sorted_by_relevance() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "parse", "demo.parse", "/a.rs", 1),
                sample_function("f2", "demo", "parse_file", "demo.parse_file", "/a.rs", 5),
                sample_function(
                    "f3",
                    "demo",
                    "my_parse_helper",
                    "demo.my_parse_helper",
                    "/b.rs",
                    1,
                ),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parse", None, 100).expect("search");
        assert!(!results.is_empty());
        // Exact match should rank first.
        assert_eq!(results[0].name, "parse");
        assert!(results[0].score >= results[1].score);
    }

    #[test]
    fn search_filters_by_project() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "alpha",
                "parse",
                "alpha.parse",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes alpha");
        repo.save_nodes(
            &[sample_function(
                "f2",
                "beta",
                "parse",
                "beta.parse",
                "/b.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes beta");

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher
            .search("parse", Some("alpha"), 100)
            .expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].qualified_name.as_deref(), Some("alpha.parse"));
    }

    #[test]
    fn search_respects_limit() {
        let repo = fresh_repo();
        let mut nodes = Vec::new();
        for i in 0..10 {
            nodes.push(sample_function(
                &format!("f{i}"),
                "demo",
                &format!("parse_{i}"),
                &format!("demo.parse_{i}"),
                "/a.rs",
                i + 1,
            ));
        }
        repo.save_nodes(&nodes, NodeLabel::Function)
            .expect("save_nodes");

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parse", None, 3).expect("search");
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn search_rejects_empty_query() {
        let repo = fresh_repo();
        let searcher = FullTextSearcher::new(repo.connection());
        let err = searcher
            .search("", None, 10)
            .expect_err("empty query should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_rejects_query_that_tokenizes_to_empty() {
        // Punctuation-only query tokenizes to empty → InvalidQuery error
        // (exercises the tokenized.is_empty() branch in try_fts_search).
        let repo = fresh_repo();
        let searcher = FullTextSearcher::new(repo.connection());
        let err = searcher
            .search("...", None, 10)
            .expect_err("punctuation-only query should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_returns_empty_when_no_match() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "main",
                "demo.main",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("nonexistent", None, 10).expect("search");
        assert!(results.is_empty());
    }

    #[test]
    fn search_populates_search_result_fields() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse",
                "demo.parse",
                "/src/main.rs",
                42,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parse", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        let r = &results[0];
        assert_eq!(r.name, "parse");
        assert_eq!(r.label, "Function");
        assert_eq!(r.file_path.as_deref(), Some("/src/main.rs"));
        assert_eq!(r.start_line, Some(42));
        assert_eq!(r.qualified_name.as_deref(), Some("demo.parse"));
        assert!(r.score > 0.0);
    }

    #[test]
    fn relevance_score_exact_match() {
        assert_eq!(relevance_score("parse", "parse"), 1.0);
        assert_eq!(relevance_score("PARSE", "parse"), 1.0);
    }

    #[test]
    fn relevance_score_prefix_match() {
        assert_eq!(relevance_score("parse_file", "parse"), 0.8);
    }

    #[test]
    fn relevance_score_token_match() {
        // H11: token-aligned match ranks above plain substring. `my_parse`
        // tokenizes to `["my", "parse"]` which contains the query token
        // `parse` exactly.
        assert_eq!(relevance_score("my_parse", "parse"), 0.7);
        assert_eq!(relevance_score("my_parse_helper", "parse"), 0.7);
    }

    #[test]
    fn relevance_score_substring_match() {
        // H11: a non-token-aligned substring (e.g. `xparse` → single token
        // `xparse`) scores below a token-aligned match.
        assert_eq!(relevance_score("xparse", "parse"), 0.5);
    }

    #[test]
    fn relevance_score_no_match() {
        assert_eq!(relevance_score("read_input", "parse"), 0.3);
    }

    #[test]
    fn is_fts_unsupported_error_detects_unsupported_patterns() {
        // Mirrors the unsupported-DDL patterns from storage::connection::run_init_ddl.
        assert!(is_fts_unsupported_error(&QueryError::Query(
            "Parser exception: syntax error near CALL".to_string()
        )));
        assert!(is_fts_unsupported_error(&QueryError::Query(
            "feature not supported in this build".to_string()
        )));
        assert!(is_fts_unsupported_error(&QueryError::Query(
            "Catalog exception: function fts_search does not exist".to_string()
        )));
        assert!(is_fts_unsupported_error(&QueryError::Query(
            "table already exists".to_string()
        )));
        // Case-insensitive matching.
        assert!(is_fts_unsupported_error(&QueryError::Query(
            "NOT SUPPORTED".to_string()
        )));
        assert!(is_fts_unsupported_error(&QueryError::Query(
            "PARSER EXCEPTION".to_string()
        )));
    }

    #[test]
    fn is_fts_unsupported_error_rejects_genuine_errors() {
        // Errors that are NOT "unsupported" signals must propagate, not fall back.
        assert!(!is_fts_unsupported_error(&QueryError::Query(
            "connection refused".to_string()
        )));
        assert!(!is_fts_unsupported_error(&QueryError::Query(
            "permission denied".to_string()
        )));
        assert!(!is_fts_unsupported_error(&QueryError::Query(
            "out of memory".to_string()
        )));
        assert!(!is_fts_unsupported_error(&QueryError::InvalidQuery(
            "empty query".to_string()
        )));
        assert!(!is_fts_unsupported_error(&QueryError::FullText(
            "index corrupted".to_string()
        )));
    }

    #[test]
    fn search_uses_fts_when_index_exists() {
        // Attempt to create an FTS index on the Function table. If LadybugDB
        // does not support FTS in this build, the creation fails silently and
        // the test falls back to verifying the CONTAINS path (which is already
        // covered above). When FTS is available, this exercises the
        // `try_fts_search` success path.
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse_file",
                "demo.parse_file",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        // Try the common LadybugDB FTS index creation syntaxes. At least one
        // may succeed depending on the linked build.
        let fts_created = repo
            .connection()
            .execute("CALL create_fts_index('fts_func_name', 'Function', ['name']);")
            .is_ok()
            || repo
                .connection()
                .execute("CREATE FTS INDEX fts_func_name ON Function(name);")
                .is_ok();

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parse", None, 100).expect("search");
        // Whether FTS or fallback was used, we should find the function.
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "parse_file");
        // If FTS was created, the success path (try_fts_search Ok branch) was
        // exercised; otherwise the fallback path was exercised.
        let _ = fts_created;
    }

    #[test]
    fn search_with_project_filter_when_fts_unavailable() {
        // Exercises the fallback path with a project filter (the FTS path
        // would also apply the filter, but FTS is typically unavailable in
        // tests).
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "alpha", "parse", "alpha.parse", "/a.rs", 1),
                sample_function("f2", "beta", "parse", "beta.parse", "/b.rs", 1),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher
            .search("parse", Some("alpha"), 100)
            .expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].qualified_name.as_deref(), Some("alpha.parse"));
    }

    #[test]
    fn try_fts_search_returns_error_when_fts_unavailable() {
        // Directly exercise try_fts_search to document that it returns an
        // error when the FTS extension/index is not available. This covers
        // the error-return path (the `?` on the query line).
        let repo = fresh_repo();
        let searcher = FullTextSearcher::new(repo.connection());
        let result = searcher.try_fts_search("parse", None, 100);
        assert!(result.is_err(), "try_fts_search should error without FTS");
    }

    #[test]
    fn try_fts_search_with_project_returns_error_when_fts_unavailable() {
        let repo = fresh_repo();
        let searcher = FullTextSearcher::new(repo.connection());
        let result = searcher.try_fts_search("parse", Some("demo"), 100);
        assert!(result.is_err());
    }

    #[test]
    fn fallback_contains_search_returns_empty_when_no_data() {
        // Exercises the fallback path with an empty database. All table
        // queries succeed (returning zero rows), so the `Err(_) => continue`
        // path is not taken, but the merge/sort/truncate logic is exercised.
        let repo = fresh_repo();
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher
            .fallback_contains_search("anything", None, 10)
            .expect("fallback");
        assert!(results.is_empty());
    }

    #[test]
    fn fallback_contains_search_with_project_filter() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "alpha", "parse", "alpha.parse", "/a.rs", 1),
                sample_function("f2", "beta", "parse", "beta.parse", "/b.rs", 1),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher
            .fallback_contains_search("parse", Some("beta"), 100)
            .expect("fallback");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].qualified_name.as_deref(), Some("beta.parse"));
    }

    #[test]
    fn fallback_contains_search_matches_camel_query_to_snake_name() {
        // H11: searching `parseFile` (camelCase) should match `parse_file`
        // (snake_case) via tokenization. Without tokenization, a single
        // `CONTAINS('parseFile')` would miss `parse_file`.
        let repo = fresh_repo();
        repo.save_nodes(
            &[
                sample_function("f1", "demo", "parse_file", "demo.parse_file", "/a.rs", 1),
                sample_function("f2", "demo", "read_input", "demo.read_input", "/b.rs", 1),
            ],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher
            .fallback_contains_search("parseFile", None, 100)
            .expect("fallback");
        let names: Vec<&str> = results.iter().map(|r| r.name.as_str()).collect();
        assert!(
            names.contains(&"parse_file"),
            "parse_file should match parseFile"
        );
        assert!(
            !names.contains(&"read_input"),
            "read_input should not match"
        );
    }

    #[test]
    fn fallback_contains_search_rejects_query_that_tokenizes_to_empty() {
        // H11: a query consisting only of separators tokenizes to empty and
        // must error (Rule 12: fail loud) rather than silently returning all
        // rows.
        let repo = fresh_repo();
        let searcher = FullTextSearcher::new(repo.connection());
        let err = searcher
            .fallback_contains_search("___", None, 10)
            .expect_err("separator-only query should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn search_camel_query_matches_snake_name() {
        // End-to-end: `search("parseFile")` should find `parse_file` through
        // whichever path is available (FTS or fallback).
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse_file",
                "demo.parse_file",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parseFile", None, 100).expect("search");
        let names: Vec<&str> = results.iter().map(|r| r.name.as_str()).collect();
        assert!(names.contains(&"parse_file"));
    }

    // --- BM25 FTS index coverage extension ---

    /// Helper: builds a minimal node of any symbol-bearing label.
    fn sample_symbol(
        label: NodeLabel,
        id: &str,
        project: &str,
        name: &str,
        qn: &str,
        file: &str,
        line: u32,
    ) -> Node {
        Node::builder(label, name, qn)
            .id(id)
            .project(project)
            .file_path(file)
            .start_line(line)
            .language(Language::Rust)
            .build()
    }

    #[test]
    fn fts_name_indexes_covers_all_symbol_tables() {
        // R-search-001: FTS_NAME_INDEXES must contain 15 entries covering all
        // symbol-bearing node labels (excluding Impl which has no meaningful
        // name for search).
        assert_eq!(
            FTS_NAME_INDEXES.len(),
            15,
            "expected 15 FTS name indexes, got {}: {FTS_NAME_INDEXES:?}",
            FTS_NAME_INDEXES.len()
        );
        let labels: Vec<NodeLabel> = FTS_NAME_INDEXES.iter().map(|(_, l)| *l).collect();
        for expected in [
            NodeLabel::Function,
            NodeLabel::Class,
            NodeLabel::Method,
            NodeLabel::Struct,
            NodeLabel::Enum,
            NodeLabel::Trait,
            NodeLabel::Macro,
            NodeLabel::Typedef,
            NodeLabel::Namespace,
            NodeLabel::Module,
            NodeLabel::Variable,
            NodeLabel::GlobalVar,
            NodeLabel::Const,
            NodeLabel::Static,
            NodeLabel::TypeAlias,
        ] {
            assert!(
                labels.contains(&expected),
                "FTS_NAME_INDEXES missing {expected:?}: {FTS_NAME_INDEXES:?}"
            );
        }
    }

    #[test]
    fn search_finds_struct_by_name() {
        // R-search-001: saving a Struct named "Point" and searching "Point"
        // must return a result with label == "Struct".
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Struct,
                "s1",
                "demo",
                "Point",
                "demo.Point",
                "/a.rs",
                1,
            )],
            NodeLabel::Struct,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("Point", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "Point");
        assert_eq!(results[0].label, "Struct");
    }

    #[test]
    fn search_finds_enum_by_name() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Enum,
                "e1",
                "demo",
                "Color",
                "demo.Color",
                "/a.rs",
                1,
            )],
            NodeLabel::Enum,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("Color", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "Color");
        assert_eq!(results[0].label, "Enum");
    }

    #[test]
    fn search_finds_macro_by_name() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Macro,
                "m1",
                "demo",
                "FOO",
                "demo.FOO",
                "/a.rs",
                1,
            )],
            NodeLabel::Macro,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("FOO", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "FOO");
        assert_eq!(results[0].label, "Macro");
    }

    #[test]
    fn search_finds_namespace_by_name() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Namespace,
                "n1",
                "demo",
                "graphics",
                "demo.graphics",
                "/a.rs",
                1,
            )],
            NodeLabel::Namespace,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("graphics", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "graphics");
        assert_eq!(results[0].label, "Namespace");
    }

    #[test]
    fn search_finds_typedef_by_name() {
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Typedef,
                "t1",
                "demo",
                "Handle",
                "demo.Handle",
                "/a.rs",
                1,
            )],
            NodeLabel::Typedef,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("Handle", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "Handle");
        assert_eq!(results[0].label, "Typedef");
    }

    #[test]
    fn search_across_multiple_label_types() {
        // R-search-001: saving Function + Struct + Enum with a shared name
        // token and searching that token must return all three label types.
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse",
                "demo.parse",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes function");
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Struct,
                "s1",
                "demo",
                "Parser",
                "demo.Parser",
                "/b.rs",
                1,
            )],
            NodeLabel::Struct,
        )
        .expect("save_nodes struct");
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Enum,
                "e1",
                "demo",
                "ParseMode",
                "demo.ParseMode",
                "/c.rs",
                1,
            )],
            NodeLabel::Enum,
        )
        .expect("save_nodes enum");

        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parse", None, 100).expect("search");
        let labels: Vec<&str> = results.iter().map(|r| r.label.as_str()).collect();
        // All three symbol tables should return at least one hit (Function
        // exact match, Struct token match via "Parser" → ["parser"], Enum
        // token match via "ParseMode" → ["parse", "mode"]).
        assert!(
            labels.contains(&"Function"),
            "expected Function in results: {labels:?}"
        );
        assert!(
            labels.contains(&"Struct"),
            "expected Struct in results: {labels:?}"
        );
        assert!(
            labels.contains(&"Enum"),
            "expected Enum in results: {labels:?}"
        );
    }

    #[test]
    fn fallback_contains_search_continues_on_query_error() {
        // Cover the `Err(_) => continue` arm (line 239) of
        // fallback_contains_search: when a per-table CONTAINS query fails
        // (table dropped), the loop skips it and continues.
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_function(
                "f1",
                "demo",
                "parse",
                "demo.parse",
                "/a.rs",
                1,
            )],
            NodeLabel::Function,
        )
        .expect("save_nodes");
        repo.connection()
            .execute("DROP TABLE Class;")
            .expect("drop table");
        let searcher = FullTextSearcher::new(repo.connection());
        // search() falls back to CONTAINS; the dropped Class table is skipped.
        let results = searcher.search("parse", None, 100).expect("search");
        assert!(results.iter().any(|r| r.name == "parse"));
    }

    // --- Coverage gap tests: relevance_score_with_reason all branches ---

    #[test]
    fn relevance_score_with_reason_exact_match_returns_one() {
        let (score, reason) = relevance_score_with_reason("parse", "parse");
        assert_eq!(score, 1.0);
        assert_eq!(reason, "exact name match");
    }

    #[test]
    fn relevance_score_with_reason_prefix_match_returns_zero_eight() {
        let (score, reason) = relevance_score_with_reason("parse_file", "parse");
        assert_eq!(score, 0.8);
        assert_eq!(reason, "prefix match");
    }

    #[test]
    fn relevance_score_with_reason_token_aligned_match_returns_zero_seven() {
        // Cover token-aligned match: "my_parse_helper" tokenized as
        // ["my","parse","helper"], query "parse" → all query tokens
        // present in name tokens → 0.7
        let (score, reason) = relevance_score_with_reason("my_parse_helper", "parse");
        assert_eq!(score, 0.7);
        assert_eq!(reason, "token-aligned match");
    }

    #[test]
    fn relevance_score_with_reason_substring_match_returns_zero_five() {
        // Cover substring match: name contains query but not as prefix,
        // and token alignment doesn't fully match.
        let (score, reason) = relevance_score_with_reason("myparsehelper", "parse");
        assert_eq!(score, 0.5);
        assert_eq!(reason, "substring match");
    }

    #[test]
    fn relevance_score_with_reason_no_match_returns_zero_three() {
        // Cover `(0.3, "no match")`: name doesn't contain query at all.
        let (score, reason) = relevance_score_with_reason("read_input", "parse");
        assert_eq!(score, 0.3);
        assert_eq!(reason, "no match");
    }

    #[test]
    fn relevance_score_with_reason_camel_case_token_alignment() {
        // camelCase "parseFile" tokenized as ["parse","file"],
        // query "fileparse" tokenized as ["fileparse"] → not all tokens
        // match → falls through to substring → 0.3
        let (score, _) = relevance_score_with_reason("parseFile", "fileparse");
        assert_eq!(score, 0.3);
    }

    // --- Coverage gap tests: relevance_score wrapper ---

    #[test]
    fn relevance_score_returns_score_only() {
        // Cover the #[allow(dead_code)] wrapper that delegates to
        // relevance_score_with_reason and returns only the score.
        assert_eq!(relevance_score("parse", "parse"), 1.0);
        assert_eq!(relevance_score("parse_file", "parse"), 0.8);
        assert_eq!(relevance_score("read_input", "parse"), 0.3);
    }

    // --- Coverage gap tests: is_fts_unsupported_error all branches ---

    #[test]
    fn is_fts_unsupported_error_already_exists() {
        // Cover `msg.contains("already exists")` branch
        let err = QueryError::InvalidQuery("index already exists".to_string());
        assert!(is_fts_unsupported_error(&err));
    }

    #[test]
    fn is_fts_unsupported_error_not_supported() {
        let err = QueryError::InvalidQuery("FTS not supported".to_string());
        assert!(is_fts_unsupported_error(&err));
    }

    #[test]
    fn is_fts_unsupported_error_parser_exception() {
        let err = QueryError::InvalidQuery("parser exception at line 1".to_string());
        assert!(is_fts_unsupported_error(&err));
    }

    #[test]
    fn is_fts_unsupported_error_does_not_exist() {
        let err = QueryError::InvalidQuery("table does not exist".to_string());
        assert!(is_fts_unsupported_error(&err));
    }

    #[test]
    fn is_fts_unsupported_error_other_error_returns_false() {
        // Cover the non-matching error path → false
        let err = QueryError::InvalidQuery("connection refused".to_string());
        assert!(!is_fts_unsupported_error(&err));
    }

    // --- Coverage gap tests: sort_and_truncate ---

    #[test]
    fn sort_and_truncate_preserves_order_when_within_limit() {
        let mut results = vec![
            SearchResult {
                name: "b".to_string(),
                label: "Function".to_string(),
                file_path: None,
                start_line: None,
                qualified_name: None,
                score: 1.0,
                match_reason: "exact".to_string(),
                degree: 0,
            },
            SearchResult {
                name: "a".to_string(),
                label: "Function".to_string(),
                file_path: None,
                start_line: None,
                qualified_name: None,
                score: 1.0,
                match_reason: "exact".to_string(),
                degree: 0,
            },
        ];
        sort_and_truncate(&mut results, 10);
        assert_eq!(results.len(), 2);
        // Same score → sorted by name ascending
        assert_eq!(results[0].name, "a");
        assert_eq!(results[1].name, "b");
    }

    #[test]
    fn sort_and_truncate_truncates_when_exceeding_limit() {
        let mut results: Vec<SearchResult> = (0..5)
            .map(|i| SearchResult {
                name: format!("func{i}"),
                label: "Function".to_string(),
                file_path: None,
                start_line: None,
                qualified_name: None,
                score: 1.0 - i as f64 * 0.1,
                match_reason: "match".to_string(),
                degree: 0,
            })
            .collect();
        sort_and_truncate(&mut results, 3);
        assert_eq!(results.len(), 3);
        // Highest scores first
        assert_eq!(results[0].name, "func0");
        assert_eq!(results[1].name, "func1");
    }

    #[test]
    fn sort_and_truncate_zero_limit_empties_results() {
        let mut results = vec![SearchResult {
            name: "x".to_string(),
            label: "Function".to_string(),
            file_path: None,
            start_line: None,
            qualified_name: None,
            score: 1.0,
            match_reason: "exact".to_string(),
            degree: 0,
        }];
        sort_and_truncate(&mut results, 0);
        assert!(results.is_empty());
    }

    // --- Coverage gap: whitespace-only query, empty-token name ---

    #[test]
    fn search_rejects_whitespace_only_query() {
        // Cover `text.trim().is_empty()` for non-empty whitespace input
        // (line 101). The existing test only uses "".
        let repo = fresh_repo();
        let searcher = FullTextSearcher::new(repo.connection());
        let err = searcher
            .search("   \t\n", None, 10)
            .expect_err("whitespace-only query should error");
        assert!(err.is_invalid_query());
    }

    #[test]
    fn relevance_score_with_reason_name_with_empty_tokens() {
        // Cover the false branch of `!query_tokens.is_empty() && !name_tokens.is_empty()`
        // (line 312). When the name is all digits, codenexus_tokenize returns
        // empty → the token-alignment check is skipped → falls through to
        // substring → 0.3 (since "12345" doesn't contain "parse").
        let (score, reason) = relevance_score_with_reason("12345", "parse");
        assert_eq!(score, 0.3);
        assert_eq!(reason, "no match");
    }

    #[test]
    fn relevance_score_with_reason_query_with_empty_tokens() {
        // Cover the false branch when query_tokens is empty (query is all
        // digits). The name "parse" does contain... wait, "parse" doesn't
        // contain "12345". So it falls to substring → 0.3.
        let (score, reason) = relevance_score_with_reason("parse", "12345");
        assert_eq!(score, 0.3);
        assert_eq!(reason, "no match");
    }

    #[test]
    fn search_finds_module_by_name() {
        // Cover searching for a Module-labeled node (line_expr = "NULL"
        // branch in fallback_contains_search for Module which has no startLine).
        let repo = fresh_repo();
        repo.save_nodes(
            &[sample_symbol(
                NodeLabel::Module,
                "mod1",
                "demo",
                "parser",
                "demo.parser",
                "/a.rs",
                1,
            )],
            NodeLabel::Module,
        )
        .expect("save_nodes");
        let searcher = FullTextSearcher::new(repo.connection());
        let results = searcher.search("parser", None, 100).expect("search");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "parser");
        assert_eq!(results[0].label, "Module");
        // Module has no startLine column → start_line should be None.
        assert!(results[0].start_line.is_none());
    }
}