loctree 0.8.16

Structural code intelligence for AI agents. Scan once, query everything.
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
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
//! Tauri command coverage analysis.
//!
//! Matches frontend `invoke()` calls with backend `#[tauri::command]` handlers.
//! Identifies missing handlers (FE calls without BE impl) and unused handlers
//! (BE impl without FE calls).

use std::collections::{HashMap, HashSet};

use globset::GlobSet;
use heck::ToSnakeCase;
use regex::Regex;
use std::sync::OnceLock;

use super::report::{CommandGap, Confidence, StringLiteralMatch};
use crate::types::FileAnalysis;

pub type CommandUsage = HashMap<String, Vec<(String, usize, String)>>;

/// Normalize a command name for comparison (snake_case, lowercase, alphanumeric only)
/// Used to match FE calls (camelCase) with BE handlers (snake_case)
pub fn normalize_cmd_name(name: &str) -> String {
    let mut buffered = String::new();
    for ch in name.chars() {
        if ch.is_alphanumeric() {
            buffered.push(ch);
        } else {
            buffered.push('_');
        }
    }
    buffered
        .to_snake_case()
        .chars()
        .filter(|c| c.is_alphanumeric())
        .collect::<String>()
        .to_lowercase()
}

fn strip_excluded_paths(
    paths: &[(String, usize, String)],
    focus: &Option<GlobSet>,
    exclude: &Option<GlobSet>,
) -> Vec<(String, usize)> {
    paths
        .iter()
        .filter_map(|(p, line, _)| {
            let pb = std::path::Path::new(p);
            if let Some(ex) = exclude
                && ex.is_match(pb)
            {
                return None;
            }
            if let Some(focus_globs) = focus
                && !focus_globs.is_match(pb)
            {
                return None;
            }
            Some((p.clone(), *line))
        })
        .collect()
}

/// Regex for finding string literals in frontend code.
/// Reserved for future content-based scanning.
fn regex_string_literal() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r#"['"]([a-z][a-z0-9_]*)['"]"#).expect("valid regex"))
}

/// Scan frontend files for string literals matching a handler name.
/// Returns matches that might indicate dynamic invoke usage.
pub fn find_string_literal_matches(
    handler_name: &str,
    analyses: &[FileAnalysis],
) -> Vec<StringLiteralMatch> {
    let mut matches = Vec::new();
    let normalized = normalize_cmd_name(handler_name);

    // Generate search variations (snake_case, camelCase, with/without _command)
    let variations: HashSet<String> = {
        let mut v = HashSet::new();
        v.insert(handler_name.to_string());
        v.insert(normalized.clone());

        // snake_case variant
        let snake = handler_name.chars().fold(String::new(), |mut acc, c| {
            if c.is_ascii_uppercase() && !acc.is_empty() {
                acc.push('_');
            }
            acc.push(c.to_ascii_lowercase());
            acc
        });
        v.insert(snake.clone());
        v.insert(normalize_cmd_name(&snake));

        // Without _command suffix
        if let Some(stripped) = handler_name.strip_suffix("_command") {
            v.insert(stripped.to_string());
            v.insert(normalize_cmd_name(stripped));
        }
        v
    };

    for analysis in analyses {
        // Skip Rust files - we want frontend string literals
        if analysis.path.ends_with(".rs") {
            continue;
        }

        // Check exports that might be allowlists or command constants
        for export in &analysis.exports {
            let export_lower = export.name.to_lowercase();
            if variations.iter().any(|v| export_lower.contains(v)) {
                matches.push(StringLiteralMatch {
                    file: analysis.path.clone(),
                    line: export.line.unwrap_or(0),
                    context: "export/const".to_string(),
                });
            }
        }

        // Check event constants that might reference handler names
        for (const_name, const_val) in &analysis.event_consts {
            let val_normalized = normalize_cmd_name(const_val);
            if variations.contains(&val_normalized)
                || variations.iter().any(|v| const_val.contains(v))
            {
                matches.push(StringLiteralMatch {
                    file: analysis.path.clone(),
                    line: 0, // Line not available from event_consts
                    context: format!("const {} = '{}'", const_name, const_val),
                });
            }
        }

        // Check string literals (registry-style arrays/objects)
        for lit in &analysis.string_literals {
            let val_normalized = normalize_cmd_name(&lit.value);
            if variations.contains(&val_normalized)
                || variations
                    .iter()
                    .any(|v| lit.value.contains(v) || val_normalized.contains(v))
            {
                matches.push(StringLiteralMatch {
                    file: analysis.path.clone(),
                    line: lit.line,
                    context: format!("string \"{}\"", lit.value),
                });
            }
        }
    }

    matches
}

/// Scan raw file content for string literal occurrences of handler name.
/// This is a more thorough scan that finds any string literal matching.
/// Reserved for future content-based scanning beyond analysis data.
pub fn scan_content_for_handler_literals(
    handler_name: &str,
    content: &str,
    file_path: &str,
) -> Vec<StringLiteralMatch> {
    let mut matches = Vec::new();
    let normalized = normalize_cmd_name(handler_name);

    for caps in regex_string_literal().captures_iter(content) {
        if let Some(lit) = caps.get(1) {
            let lit_str = lit.as_str();
            let lit_normalized = normalize_cmd_name(lit_str);

            if lit_normalized == normalized {
                // Calculate line number
                let line = content[..lit.start()].matches('\n').count() + 1;
                matches.push(StringLiteralMatch {
                    file: file_path.to_string(),
                    line,
                    context: format!("string_literal '{}'", lit_str),
                });
            }
        }
    }

    matches
}

pub fn compute_command_gaps(
    fe_commands: &CommandUsage,
    be_commands: &CommandUsage,
    focus_set: &Option<GlobSet>,
    exclude_set: &Option<GlobSet>,
) -> (Vec<CommandGap>, Vec<CommandGap>) {
    compute_command_gaps_with_confidence(fe_commands, be_commands, focus_set, exclude_set, &[])
}

/// Compute command gaps with confidence scoring based on string literal analysis.
pub fn compute_command_gaps_with_confidence(
    fe_commands: &CommandUsage,
    be_commands: &CommandUsage,
    focus_set: &Option<GlobSet>,
    exclude_set: &Option<GlobSet>,
    analyses: &[FileAnalysis],
) -> (Vec<CommandGap>, Vec<CommandGap>) {
    let fe_norms: HashMap<String, String> = fe_commands
        .keys()
        .map(|k| (k.clone(), normalize_cmd_name(k)))
        .collect();
    let be_norms: HashMap<String, String> = be_commands
        .keys()
        .map(|k| (k.clone(), normalize_cmd_name(k)))
        .collect();
    let be_norm_set: HashSet<String> = be_norms.values().cloned().collect();
    let fe_norm_set: HashSet<String> = fe_norms.values().cloned().collect();

    let missing_handlers: Vec<CommandGap> = fe_commands
        .iter()
        .filter_map(|(name, locs)| {
            let norm = fe_norms
                .get(name)
                .cloned()
                .unwrap_or_else(|| normalize_cmd_name(name));
            if be_norm_set.contains(&norm) {
                return None;
            }
            let kept = strip_excluded_paths(locs, focus_set, exclude_set);
            if kept.is_empty() {
                None
            } else {
                let impl_name = locs
                    .iter()
                    .find(|(p, l, _)| p == &kept[0].0 && *l == kept[0].1)
                    .map(|(_, _, n)| n.clone());
                Some(CommandGap {
                    name: name.clone(),
                    implementation_name: impl_name,
                    locations: kept,
                    confidence: None, // Missing handlers don't have confidence
                    string_literal_matches: Vec::new(),
                })
            }
        })
        .collect();

    let unused_handlers: Vec<CommandGap> = be_commands
        .iter()
        .filter_map(|(name, locs)| {
            let norm = be_norms
                .get(name)
                .cloned()
                .unwrap_or_else(|| normalize_cmd_name(name));
            if fe_norm_set.contains(&norm) {
                return None;
            }
            let kept = strip_excluded_paths(locs, focus_set, exclude_set);
            if kept.is_empty() {
                None
            } else {
                let impl_name = locs
                    .iter()
                    .find(|(p, l, _)| p == &kept[0].0 && *l == kept[0].1)
                    .map(|(_, _, n)| n.clone());

                // Find string literal matches for confidence scoring
                let string_literal_matches = find_string_literal_matches(name, analyses);
                let confidence = if string_literal_matches.is_empty() {
                    Confidence::High
                } else {
                    Confidence::Smell // String literals suggest possible dynamic usage
                };

                Some(CommandGap {
                    name: name.clone(),
                    implementation_name: impl_name,
                    locations: kept,
                    confidence: Some(confidence),
                    string_literal_matches,
                })
            }
        })
        .collect();

    (missing_handlers, unused_handlers)
}

/// Compute gaps for backend handlers that are defined but never registered with Tauri.
///
/// `be_commands` is the full backend command usage map (including both registered and
/// unregistered handlers). `registered_impls` is the set of Rust function names that
/// are actually registered via `tauri::generate_handler![...]` across the project.
///
/// We treat a command name as "unregistered" if **none** of its implementation symbols
/// appear in `registered_impls`. Paths are filtered through `focus_set` / `exclude_set`
/// in the same way as in `compute_command_gaps`.
pub fn compute_unregistered_handlers(
    be_commands: &CommandUsage,
    registered_impls: &std::collections::HashSet<String>,
    focus_set: &Option<GlobSet>,
    exclude_set: &Option<GlobSet>,
) -> Vec<CommandGap> {
    be_commands
        .iter()
        .filter_map(|(name, locs)| {
            // If any impl symbol for this command is registered, skip it.
            let has_registered_impl = locs
                .iter()
                .any(|(_, _, impl_name)| registered_impls.contains(impl_name));
            if has_registered_impl {
                return None;
            }

            let kept = strip_excluded_paths(locs, focus_set, exclude_set);
            if kept.is_empty() {
                return None;
            }

            let impl_name = locs
                .iter()
                .find(|(p, l, _)| p == &kept[0].0 && *l == kept[0].1)
                .map(|(_, _, n)| n.clone());

            Some(CommandGap {
                name: name.clone(),
                implementation_name: impl_name,
                locations: kept,
                confidence: None, // Unregistered handlers don't have confidence scoring
                string_literal_matches: Vec::new(),
            })
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use globset::{Glob, GlobSetBuilder};

    #[test]
    fn matches_commands_across_casing() {
        let mut fe: CommandUsage = HashMap::new();
        fe.insert(
            "fetchUserData".into(),
            vec![("src/fe.ts".into(), 10usize, "fetchUserData".into())],
        );
        let mut be: CommandUsage = HashMap::new();
        be.insert(
            "fetch_user_data".into(),
            vec![("src/be.rs".into(), 20usize, "fetch_user_data".into())],
        );
        let (missing, unused) = compute_command_gaps(&fe, &be, &None, &None);
        assert!(missing.is_empty(), "should detect matching handler");
        assert!(unused.is_empty(), "should detect frontend usage");
    }

    #[test]
    fn ignores_excluded_paths_before_gap_report() {
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("**/ignored/**").expect("valid glob"));
        let exclude_set = Some(builder.build().expect("build globset"));
        let mut fe: CommandUsage = HashMap::new();
        fe.insert(
            "audio-play".into(),
            vec![("ignored/fe.ts".into(), 5usize, "audio-play".into())],
        );
        let mut be: CommandUsage = HashMap::new();
        be.insert(
            "audio_play".into(),
            vec![("src/handler.rs".into(), 8usize, "audio_play".into())],
        );
        let (missing, unused) = compute_command_gaps(&fe, &be, &None, &exclude_set);
        assert!(missing.is_empty());
        assert!(unused.is_empty());
    }

    /// Tests that commands with rename attribute match correctly.
    /// Simulates: BE has `alpha_status_command` with `rename = "alpha_status"`,
    /// FE invokes `alpha_status`. They should match.
    #[test]
    fn matches_renamed_commands() {
        // When root_scan processes `#[tauri::command(rename = "alpha_status")]`,
        // it uses exposed_name ("alpha_status") as the key, not the function name.
        // So be_commands will have key "alpha_status", not "alpha_status_command".
        let mut fe: CommandUsage = HashMap::new();
        fe.insert(
            "alpha_status".into(),
            vec![("src/service.ts".into(), 42usize, "alpha_status".into())],
        );
        let mut be: CommandUsage = HashMap::new();
        // Key is the exposed name, impl_name is the function name
        be.insert(
            "alpha_status".into(),
            vec![(
                "src-tauri/src/commands/alpha_gate.rs".into(),
                15usize,
                "alpha_status_command".into(),
            )],
        );
        let (missing, unused) = compute_command_gaps(&fe, &be, &None, &None);
        assert!(
            missing.is_empty(),
            "FE invoke('alpha_status') should match BE handler with rename='alpha_status'"
        );
        assert!(
            unused.is_empty(),
            "BE alpha_status handler should be detected as used"
        );
    }

    /// Tests that suffix stripping doesn't break renamed commands.
    /// If someone uses `rename = "some_thing_command"` (unusual but valid),
    /// the _command suffix should still be stripped for matching.
    #[test]
    fn suffix_stripping_on_exposed_name() {
        let mut fe: CommandUsage = HashMap::new();
        fe.insert(
            "some_thing".into(),
            vec![("src/app.ts".into(), 10usize, "some_thing".into())],
        );
        let mut be: CommandUsage = HashMap::new();
        // Edge case: exposed name has _command suffix (will be stripped)
        be.insert(
            "some_thing".into(), // After suffix stripping in root_scan
            vec![("src-tauri/handler.rs".into(), 5usize, "impl_fn".into())],
        );
        let (missing, unused) = compute_command_gaps(&fe, &be, &None, &None);
        assert!(missing.is_empty());
        assert!(unused.is_empty());
    }

    /// Tests confidence scoring for unused handlers.
    /// HIGH confidence = no string literal matches found.
    /// SMELL confidence = string literal matches found (may be dynamic usage).
    #[test]
    fn confidence_scoring_for_unused_handlers() {
        use super::Confidence;
        use crate::types::{ExportSymbol, FileAnalysis};

        let fe: CommandUsage = HashMap::new();
        // No FE usage - both BE handlers are unused
        let mut be: CommandUsage = HashMap::new();
        be.insert(
            "truly_unused".into(),
            vec![("src-tauri/cmd.rs".into(), 10usize, "truly_unused".into())],
        );
        be.insert(
            "get_pin_status".into(),
            vec![("src-tauri/cmd.rs".into(), 20usize, "get_pin_status".into())],
        );

        // Create analyses with exports that reference one handler name
        let mut analysis = FileAnalysis::new("src/commands.ts".into());
        analysis.exports.push(ExportSymbol::new(
            "GET_PIN_STATUS_CMD".into(), // Contains handler name
            "const",
            "named",
            Some(5),
        ));

        let (missing, unused) =
            compute_command_gaps_with_confidence(&fe, &be, &None, &None, &[analysis]);

        assert!(missing.is_empty());
        assert_eq!(unused.len(), 2);

        // Find handlers by name
        let truly_unused = unused.iter().find(|g| g.name == "truly_unused").unwrap();
        let pin_status = unused.iter().find(|g| g.name == "get_pin_status").unwrap();

        // truly_unused should have HIGH confidence (no string literal matches)
        assert_eq!(truly_unused.confidence, Some(Confidence::High));
        assert!(truly_unused.string_literal_matches.is_empty());

        // get_pin_status should have SMELL confidence (string literal match found)
        assert_eq!(pin_status.confidence, Some(Confidence::Smell));
        assert!(!pin_status.string_literal_matches.is_empty());
    }

    #[test]
    fn test_normalize_cmd_name() {
        // Basic snake_case normalization
        assert_eq!(normalize_cmd_name("get_user"), "getuser");
        assert_eq!(normalize_cmd_name("getUser"), "getuser");
        assert_eq!(normalize_cmd_name("GetUser"), "getuser");

        // With special characters
        assert_eq!(normalize_cmd_name("get-user"), "getuser");
        assert_eq!(normalize_cmd_name("get.user"), "getuser");
        assert_eq!(normalize_cmd_name("get::user"), "getuser");

        // Numbers preserved
        assert_eq!(normalize_cmd_name("get_user_v2"), "getuserv2");
        assert_eq!(normalize_cmd_name("http2_request"), "http2request");
    }

    #[test]
    fn test_strip_excluded_paths_with_focus() {
        let paths = vec![
            ("src/api.ts".to_string(), 10, "api".to_string()),
            ("lib/utils.ts".to_string(), 20, "utils".to_string()),
            ("test/mock.ts".to_string(), 30, "mock".to_string()),
        ];

        // Focus on src only
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("src/**").expect("valid glob"));
        let focus = Some(builder.build().expect("build globset"));

        let result = strip_excluded_paths(&paths, &focus, &None);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "src/api.ts");
    }

    #[test]
    fn test_strip_excluded_paths_with_exclude() {
        let paths = vec![
            ("src/api.ts".to_string(), 10, "api".to_string()),
            (
                "node_modules/pkg/index.ts".to_string(),
                20,
                "pkg".to_string(),
            ),
            ("src/main.ts".to_string(), 30, "main".to_string()),
        ];

        // Exclude node_modules
        let mut builder = GlobSetBuilder::new();
        builder.add(Glob::new("node_modules/**").expect("valid glob"));
        let exclude = Some(builder.build().expect("build globset"));

        let result = strip_excluded_paths(&paths, &None, &exclude);
        assert_eq!(result.len(), 2);
        assert!(result.iter().all(|(p, _)| !p.contains("node_modules")));
    }

    #[test]
    fn test_strip_excluded_paths_both() {
        let paths = vec![
            ("src/api.ts".to_string(), 10, "api".to_string()),
            ("src/test/mock.ts".to_string(), 20, "mock".to_string()),
            ("lib/utils.ts".to_string(), 30, "utils".to_string()),
        ];

        // Focus on src, exclude test
        let mut focus_builder = GlobSetBuilder::new();
        focus_builder.add(Glob::new("src/**").expect("valid glob"));
        let focus = Some(focus_builder.build().expect("build globset"));

        let mut exclude_builder = GlobSetBuilder::new();
        exclude_builder.add(Glob::new("**/test/**").expect("valid glob"));
        let exclude = Some(exclude_builder.build().expect("build globset"));

        let result = strip_excluded_paths(&paths, &focus, &exclude);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "src/api.ts");
    }

    #[test]
    fn test_find_string_literal_matches_event_consts() {
        use crate::types::FileAnalysis;

        let mut analysis = FileAnalysis::new("src/events.ts".into());
        analysis
            .event_consts
            .insert("FETCH_USER_EVENT".to_string(), "fetch_user".to_string());

        let matches = find_string_literal_matches("fetch_user", &[analysis]);
        assert_eq!(matches.len(), 1);
        assert!(matches[0].context.contains("FETCH_USER_EVENT"));
    }

    #[test]
    fn test_find_string_literal_matches_skips_rust() {
        use crate::types::{ExportSymbol, FileAnalysis};

        let mut rust_analysis = FileAnalysis::new("src-tauri/src/handlers.rs".into());
        rust_analysis.exports.push(ExportSymbol::new(
            "get_user".into(),
            "fn",
            "named",
            Some(10),
        ));

        let matches = find_string_literal_matches("get_user", &[rust_analysis]);
        assert!(matches.is_empty()); // Should skip .rs files
    }

    #[test]
    fn test_find_string_literal_matches_with_command_suffix() {
        use crate::types::{ExportSymbol, FileAnalysis};

        let mut analysis = FileAnalysis::new("src/commands.ts".into());
        analysis.exports.push(ExportSymbol::new(
            "save_config_command".into(),
            "const",
            "named",
            Some(5),
        ));

        // Search with _command suffix - should find match
        let matches = find_string_literal_matches("save_config_command", &[analysis.clone()]);
        assert!(!matches.is_empty());

        // Search without _command suffix - should also find (due to variation generation)
        let matches2 = find_string_literal_matches("save_config", &[analysis]);
        // The export "save_config_command" contains "save_config"
        assert!(!matches2.is_empty());
    }

    #[test]
    fn test_compute_unregistered_handlers_basic() {
        let mut be: CommandUsage = HashMap::new();
        be.insert(
            "registered_handler".into(),
            vec![("src-tauri/cmd.rs".into(), 10, "registered_handler".into())],
        );
        be.insert(
            "unregistered_handler".into(),
            vec![("src-tauri/cmd.rs".into(), 20, "unregistered_handler".into())],
        );

        let registered: HashSet<String> = ["registered_handler".to_string()].into_iter().collect();

        let unregistered = compute_unregistered_handlers(&be, &registered, &None, &None);
        assert_eq!(unregistered.len(), 1);
        assert_eq!(unregistered[0].name, "unregistered_handler");
    }

    #[test]
    fn test_compute_unregistered_handlers_all_registered() {
        let mut be: CommandUsage = HashMap::new();
        be.insert(
            "handler_a".into(),
            vec![("src-tauri/cmd.rs".into(), 10, "handler_a".into())],
        );
        be.insert(
            "handler_b".into(),
            vec![("src-tauri/cmd.rs".into(), 20, "handler_b".into())],
        );

        let registered: HashSet<String> = ["handler_a".to_string(), "handler_b".to_string()]
            .into_iter()
            .collect();

        let unregistered = compute_unregistered_handlers(&be, &registered, &None, &None);
        assert!(unregistered.is_empty());
    }

    #[test]
    fn test_compute_unregistered_handlers_with_exclude() {
        let mut be: CommandUsage = HashMap::new();
        be.insert(
            "test_handler".into(),
            vec![("test/mock.rs".into(), 10, "test_handler".into())],
        );

        let registered: HashSet<String> = HashSet::new();

        let mut exclude_builder = GlobSetBuilder::new();
        exclude_builder.add(Glob::new("test/**").expect("valid glob"));
        let exclude = Some(exclude_builder.build().expect("build globset"));

        let unregistered = compute_unregistered_handlers(&be, &registered, &None, &exclude);
        assert!(unregistered.is_empty()); // Excluded by path
    }

    #[test]
    fn test_compute_command_gaps_missing_handler() {
        let mut fe: CommandUsage = HashMap::new();
        fe.insert(
            "missing_handler".into(),
            vec![("src/app.ts".into(), 10, "missing_handler".into())],
        );

        let be: CommandUsage = HashMap::new(); // No backend handlers

        let (missing, unused) = compute_command_gaps(&fe, &be, &None, &None);
        assert_eq!(missing.len(), 1);
        assert_eq!(missing[0].name, "missing_handler");
        assert!(unused.is_empty());
    }

    #[test]
    fn test_compute_command_gaps_unused_handler() {
        let fe: CommandUsage = HashMap::new(); // No frontend usage

        let mut be: CommandUsage = HashMap::new();
        be.insert(
            "unused_handler".into(),
            vec![("src-tauri/cmd.rs".into(), 10, "unused_handler".into())],
        );

        let (missing, unused) = compute_command_gaps(&fe, &be, &None, &None);
        assert!(missing.is_empty());
        assert_eq!(unused.len(), 1);
        assert_eq!(unused[0].name, "unused_handler");
    }

    #[test]
    fn test_scan_content_for_handler_literals() {
        let content = r#"
            const handler = 'get_user';
            invoke('get_user');
            const other = "different";
        "#;

        let matches = scan_content_for_handler_literals("get_user", content, "src/test.ts");
        assert_eq!(matches.len(), 2); // Two occurrences of 'get_user'
        assert!(matches.iter().all(|m| m.file == "src/test.ts"));
    }

    #[test]
    fn test_scan_content_no_matches() {
        let content = r#"
            const handler = 'other_handler';
            invoke('different_command');
        "#;

        let matches = scan_content_for_handler_literals("get_user", content, "src/test.ts");
        assert!(matches.is_empty());
    }
}