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
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
//! Watch mode for live snapshot refresh during iterative development.
//!
//! This module provides file system watching capabilities that:
//! - Monitor file changes in real-time
//! - Debounce changes to avoid thrashing (500ms default)
//! - **Incrementally patch** the snapshot for changed files only (no full rescan)
//! - Respect existing ignore patterns (.gitignore, .loctreeignore)
//! - Periodically do a full rescan to keep bridges, barrels, etc. fresh
//! - Allow graceful shutdown via Ctrl+C

use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::mpsc::channel;
use std::time::{Duration, Instant, UNIX_EPOCH};

use notify::RecursiveMode;
use notify_debouncer_full::{DebounceEventResult, new_debouncer};

use crate::analyzer::ast_js::CommandDetectionConfig;
use crate::analyzer::resolvers::{
    TsPathResolver, resolve_js_relative, resolve_python_absolute, resolve_python_relative,
};
use crate::analyzer::runner::default_analyzer_exts;
use crate::analyzer::scan::{analyze_file, python_stdlib};
use crate::args::ParsedArgs;
use crate::config::LoctreeConfig;
use crate::fs_utils::GitIgnoreChecker;
use crate::snapshot::{self, GraphEdge, Snapshot};
use crate::types::ImportKind;

/// Watch configuration
pub struct WatchConfig {
    /// Paths to watch
    pub roots: Vec<PathBuf>,
    /// Debounce duration (default: 500ms)
    pub debounce_duration: Duration,
    /// File extensions to watch (empty = all)
    pub extensions: Option<Vec<String>>,
    /// Gitignore checker for filtering
    pub gitignore: Option<GitIgnoreChecker>,
}

impl Default for WatchConfig {
    fn default() -> Self {
        Self {
            roots: vec![PathBuf::from(".")],
            debounce_duration: Duration::from_millis(500),
            extensions: None,
            gitignore: None,
        }
    }
}

/// Cached scan infrastructure — created once at watch start, reused for every patch.
///
/// Caches git context to avoid spawning 3 git subprocesses on every patch.
/// Git context is refreshed on periodic full rescans (every [`FULL_RESCAN_INTERVAL`] changes).
struct ScanInfra {
    /// Primary root (first root, used for analysis)
    root_canon: PathBuf,
    /// All roots canonicalized (for multi-root strip_prefix)
    all_roots_canon: Vec<PathBuf>,
    snapshot_root: PathBuf,
    extensions: Option<HashSet<String>>,
    ts_resolver: Option<TsPathResolver>,
    py_roots: Vec<PathBuf>,
    py_stdlib: HashSet<String>,
    custom_command_macros: Vec<String>,
    command_detection: CommandDetectionConfig,
    /// Cached git context (commit, branch, scan_id). Refreshed on full rescan.
    git_context: snapshot::GitContext,
}

/// Stats from a single patch operation.
struct PatchStats {
    updated: usize,
    added: usize,
    deleted: usize,
}

/// How many patches before we do a full rescan (to refresh bridges, barrels, etc.)
const FULL_RESCAN_INTERVAL: usize = 50;

/// Start watching for file changes and trigger re-scans
pub fn watch_and_rescan(config: WatchConfig, parsed_args: &ParsedArgs) -> anyhow::Result<()> {
    let (tx, rx) = channel();

    // Create debouncer with specified duration
    let mut debouncer = new_debouncer(
        config.debounce_duration,
        None, // No separate tick rate
        move |result: DebounceEventResult| {
            if let Err(e) = tx.send(result) {
                eprintln!("[watch] Error sending event: {e}");
            }
        },
    )?;

    // Add paths to watch
    for root in &config.roots {
        debouncer
            .watch(root, RecursiveMode::Recursive)
            .map_err(|e| anyhow::anyhow!("Failed to watch {}: {}", root.display(), e))?;
    }

    // Perform initial full scan
    eprintln!("[watch] Initial scan...");
    let start = Instant::now();
    if let Err(e) = snapshot::run_init(&config.roots, parsed_args) {
        eprintln!("[watch] Initial scan failed: {e}");
        return Err(anyhow::anyhow!("Initial scan failed: {e}"));
    }

    // Load snapshot into memory and set up scan infrastructure
    let infra = setup_scan_infra(&config.roots, parsed_args)?;
    let mut snap = Snapshot::load(&infra.snapshot_root)
        .map_err(|e| anyhow::anyhow!("Failed to load snapshot after initial scan: {e}"))?;

    let initial_count = snap.files.len();
    eprintln!(
        "[watch] [OK] Scanned {} files in {:.2}s",
        initial_count,
        start.elapsed().as_secs_f64()
    );

    // Print watching status
    let timestamp = chrono::Local::now().format("%H:%M:%S");
    eprintln!("[{}] Watching {} files...", timestamp, initial_count);
    eprintln!("[watch] Press Ctrl+C to exit");

    let mut patch_count: usize = 0;

    // Watch loop
    loop {
        match rx.recv() {
            Ok(Ok(events)) => {
                // Filter events to only those we care about
                let changed_paths =
                    collect_changed_paths(&events, &config.extensions, &config.gitignore);

                if changed_paths.is_empty() {
                    continue;
                }

                // Print what changed
                let timestamp = chrono::Local::now().format("%H:%M:%S");
                if changed_paths.len() == 1 {
                    eprintln!(
                        "[{}] Changed: {}",
                        timestamp,
                        changed_paths.iter().next().unwrap().display()
                    );
                } else {
                    eprintln!("[{}] Changed {} files", timestamp, changed_paths.len());
                }

                let start = Instant::now();

                // Decide: incremental patch or full rescan
                patch_count += 1;
                if patch_count >= FULL_RESCAN_INTERVAL {
                    // Periodic full rescan to refresh bridges, barrels, etc.
                    eprintln!(
                        "[watch] Periodic full rescan (every {} changes)...",
                        FULL_RESCAN_INTERVAL
                    );
                    if let Err(e) = snapshot::run_init(&config.roots, parsed_args) {
                        eprintln!("[watch] Full re-scan failed: {e}");
                        continue;
                    }
                    match Snapshot::load(&infra.snapshot_root) {
                        Ok(fresh) => {
                            let file_count = fresh.files.len();
                            snap = fresh;
                            patch_count = 0;
                            eprintln!(
                                "[{}] [OK] Full rescan: {} files in {:.2}s",
                                chrono::Local::now().format("%H:%M:%S"),
                                file_count,
                                start.elapsed().as_secs_f64()
                            );
                        }
                        Err(e) => {
                            eprintln!("[watch] Failed to reload snapshot: {e}");
                        }
                    }
                    continue;
                }

                // Incremental patch
                match patch_snapshot(&mut snap, &infra, &changed_paths, parsed_args) {
                    Ok(stats) => {
                        // Save patched snapshot
                        if let Err(e) = snap.save(&infra.snapshot_root) {
                            eprintln!("[watch] Failed to save snapshot: {e}");
                            continue;
                        }

                        let elapsed = start.elapsed();
                        let action = if stats.deleted > 0 {
                            format!("{} updated, {} deleted", stats.updated, stats.deleted)
                        } else if stats.added > 0 {
                            format!("{} updated, {} added", stats.updated, stats.added)
                        } else {
                            format!("{} updated", stats.updated)
                        };
                        eprintln!(
                            "[{}] [OK] Patched {} ({} files total) in {:.2}s",
                            chrono::Local::now().format("%H:%M:%S"),
                            action,
                            snap.files.len(),
                            elapsed.as_secs_f64()
                        );
                    }
                    Err(e) => {
                        // Patch failed — fall back to full rescan
                        eprintln!(
                            "[watch] Patch failed ({}), falling back to full rescan...",
                            e
                        );
                        if let Err(e2) = snapshot::run_init(&config.roots, parsed_args) {
                            eprintln!("[watch] Full re-scan also failed: {e2}");
                            continue;
                        }
                        match Snapshot::load(&infra.snapshot_root) {
                            Ok(fresh) => {
                                snap = fresh;
                                patch_count = 0;
                                eprintln!(
                                    "[{}] [OK] Full rescan: {} files in {:.2}s",
                                    chrono::Local::now().format("%H:%M:%S"),
                                    snap.files.len(),
                                    start.elapsed().as_secs_f64()
                                );
                            }
                            Err(e3) => {
                                eprintln!("[watch] Failed to reload snapshot: {e3}");
                            }
                        }
                    }
                }
            }
            Ok(Err(errors)) => {
                for error in errors {
                    eprintln!("[watch] Error: {error}");
                }
            }
            Err(e) => {
                eprintln!("[watch] Watch error: {e}");
                break;
            }
        }
    }

    Ok(())
}

/// Create scan infrastructure once — reused for every patch in the watch loop.
fn setup_scan_infra(roots: &[PathBuf], parsed_args: &ParsedArgs) -> anyhow::Result<ScanInfra> {
    let root = roots.first().cloned().unwrap_or_else(|| PathBuf::from("."));
    let root_canon = root.canonicalize().unwrap_or_else(|_| root.clone());

    // Canonicalize all roots for multi-root strip_prefix
    let all_roots_canon: Vec<PathBuf> = roots
        .iter()
        .map(|r| r.canonicalize().unwrap_or_else(|_| r.clone()))
        .collect();

    let snapshot_root = snapshot::resolve_snapshot_root(roots);

    // Extensions: use parsed_args or defaults
    let extensions = parsed_args
        .extensions
        .clone()
        .or_else(|| Some(default_analyzer_exts()));

    // TypeScript path resolver
    let ts_resolver = TsPathResolver::from_tsconfig(&root_canon);

    // Python roots
    let py_roots = crate::analyzer::root_scan::build_py_roots(&root_canon, &parsed_args.py_roots);
    let py_stdlib = python_stdlib();

    // Tauri/command detection config
    let loctree_config = LoctreeConfig::load(&root);
    let custom_command_macros = loctree_config.tauri.command_macros;
    let command_detection = CommandDetectionConfig::new(
        &loctree_config.tauri.dom_exclusions,
        &loctree_config.tauri.non_invoke_exclusions,
        &loctree_config.tauri.invalid_command_names,
    );

    // Cache git context once (avoids spawning 3 git subprocesses per patch)
    let git_context = Snapshot::git_context_for(&snapshot_root);

    Ok(ScanInfra {
        root_canon,
        all_roots_canon,
        snapshot_root,
        extensions,
        ts_resolver,
        py_roots,
        py_stdlib,
        custom_command_macros,
        command_detection,
        git_context,
    })
}

/// Incrementally patch a snapshot for changed files.
///
/// Only re-analyzes files in `changed_paths`. Deleted files are removed.
/// Edges from changed files are recalculated. Export index is rebuilt.
///
/// **Note:** `command_bridges`, `event_bridges`, and `barrels` are NOT updated
/// during incremental patches — they require cross-file correlation that only
/// the full `run_init()` pipeline performs. These are refreshed on periodic
/// full rescans (every [`FULL_RESCAN_INTERVAL`] changes).
///
/// This is ~30-40x faster than a full rescan for single-file changes.
fn patch_snapshot(
    snapshot: &mut Snapshot,
    infra: &ScanInfra,
    changed_paths: &HashSet<PathBuf>,
    parsed_args: &ParsedArgs,
) -> anyhow::Result<PatchStats> {
    let mut stats = PatchStats {
        updated: 0,
        added: 0,
        deleted: 0,
    };

    for path in changed_paths {
        // Compute relative path — try all roots for strip_prefix (multi-root support).
        // For deleted files, canonicalize the parent dir (file no longer exists).
        let canon = path.canonicalize().unwrap_or_else(|_| {
            path.parent()
                .and_then(|p| p.canonicalize().ok())
                .map(|p| p.join(path.file_name().unwrap_or_default()))
                .unwrap_or_else(|| path.clone())
        });
        let rel_path = infra
            .all_roots_canon
            .iter()
            .find_map(|root| {
                canon
                    .strip_prefix(root)
                    .ok()
                    .map(|p| p.to_string_lossy().to_string())
            })
            .unwrap_or_else(|| path.to_string_lossy().to_string())
            .replace('\\', "/");

        // Skip directories (debouncer may report dir events)
        if path.is_dir() {
            continue;
        }

        // Check if file was deleted
        if !path.exists() {
            let before = snapshot.files.len();
            snapshot.files.retain(|f| f.path != rel_path);
            snapshot
                .edges
                .retain(|e| e.from != rel_path && e.to != rel_path);
            if snapshot.files.len() < before {
                stats.deleted += 1;
            }
            continue;
        }

        // Re-analyze the file
        let analysis = match analyze_file(
            path,
            &infra.root_canon,
            infra.extensions.as_ref(),
            infra.ts_resolver.as_ref(),
            &infra.py_roots,
            &infra.py_stdlib,
            parsed_args.symbol.as_deref(),
            &infra.custom_command_macros,
            &infra.command_detection,
        ) {
            Ok(mut a) => {
                // Set mtime + size for future cache hits
                if let Ok(metadata) = std::fs::metadata(path) {
                    a.mtime = metadata
                        .modified()
                        .ok()
                        .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
                        .map(|d| d.as_secs())
                        .unwrap_or(0);
                    a.size = metadata.len();
                }
                a
            }
            Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {
                // Binary file — skip
                continue;
            }
            Err(e) => {
                return Err(anyhow::anyhow!(
                    "Failed to analyze {}: {}",
                    path.display(),
                    e
                ));
            }
        };

        // Remove old edges FROM this file
        snapshot.edges.retain(|e| e.from != rel_path);

        // Build new edges from the re-analyzed imports
        let file_ext = path
            .extension()
            .and_then(|e| e.to_str())
            .map(|s| s.to_lowercase())
            .unwrap_or_default();

        for imp in &analysis.imports {
            if imp.is_type_checking {
                continue;
            }

            let resolved = imp
                .resolved_path
                .clone()
                .or_else(|| match file_ext.as_str() {
                    "py" => {
                        if imp.source.starts_with('.') {
                            resolve_python_relative(
                                &imp.source,
                                path,
                                &infra.root_canon,
                                infra.extensions.as_ref(),
                            )
                        } else {
                            resolve_python_absolute(
                                &imp.source,
                                &infra.py_roots,
                                &infra.root_canon,
                                infra.extensions.as_ref(),
                            )
                        }
                    }
                    "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" | "css" | "svelte" | "vue" => {
                        if imp.source.starts_with('.') {
                            resolve_js_relative(
                                path,
                                &infra.root_canon,
                                &imp.source,
                                infra.extensions.as_ref(),
                            )
                        } else {
                            infra
                                .ts_resolver
                                .as_ref()
                                .and_then(|r| r.resolve(&imp.source, infra.extensions.as_ref()))
                        }
                    }
                    _ => None,
                });

            if let Some(target) = resolved {
                let label = if imp.is_mod_declaration {
                    "mod"
                } else if imp.is_lazy {
                    "lazy_import"
                } else {
                    match imp.kind {
                        ImportKind::Static | ImportKind::Type | ImportKind::SideEffect => "import",
                        ImportKind::Dynamic => "dynamic_import",
                    }
                };

                snapshot.edges.push(GraphEdge {
                    from: rel_path.clone(),
                    to: target,
                    label: label.to_string(),
                });
            }
        }

        // Also handle reexport edges (match root_scan.rs resolution logic)
        for re in &analysis.reexports {
            let resolved = re.resolved.clone().or_else(|| {
                let spec = &re.source;
                if spec.starts_with('.') {
                    resolve_js_relative(path, &infra.root_canon, spec, infra.extensions.as_ref())
                } else if matches!(
                    file_ext.as_str(),
                    "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" | "svelte" | "vue"
                ) {
                    infra
                        .ts_resolver
                        .as_ref()
                        .and_then(|r| r.resolve(spec, infra.extensions.as_ref()))
                } else {
                    None
                }
            });
            if let Some(target) = resolved {
                snapshot.edges.push(GraphEdge {
                    from: rel_path.clone(),
                    to: target,
                    label: "reexport".to_string(),
                });
            }
        }

        // Replace or insert file analysis
        if let Some(existing) = snapshot.files.iter_mut().find(|f| f.path == rel_path) {
            *existing = analysis;
            stats.updated += 1;
        } else {
            snapshot.files.push(analysis);
            stats.added += 1;
        }
    }

    // Rebuild export_index from all files (fast iteration, no I/O).
    // Must match root_scan.rs filtering: exclude test fixtures, .d.ts declarations, reexports.
    snapshot.export_index.clear();
    for file in &snapshot.files {
        // Exclude test fixtures from export index (same filter as root_scan.rs)
        if crate::analyzer::classify::should_exclude_from_reports(&file.path) {
            continue;
        }
        let is_decl = [".d.ts", ".d.tsx", ".d.mts", ".d.cts"]
            .iter()
            .any(|ext| file.path.ends_with(ext));
        for exp in &file.exports {
            if exp.kind == "reexport" {
                continue;
            }
            if exp.export_type == "default" {
                continue;
            }
            // Skip ambient "default" from .d.ts files
            if is_decl && exp.name.to_lowercase() == "default" {
                continue;
            }
            snapshot
                .export_index
                .entry(exp.name.clone())
                .or_default()
                .push(file.path.clone());
        }
    }

    // Update metadata using cached git context (no subprocess spawning)
    snapshot.metadata.git_commit = infra.git_context.commit.clone();
    snapshot.metadata.git_branch = infra.git_context.branch.clone();
    snapshot.metadata.git_scan_id = infra.git_context.scan_id.clone();
    snapshot.metadata.generated_at = chrono::Utc::now().to_rfc3339();
    snapshot.metadata.file_count = snapshot.files.len();
    snapshot.metadata.total_loc = snapshot.files.iter().map(|f| f.loc).sum();

    Ok(stats)
}

/// Collect paths that changed from debounced events
fn collect_changed_paths(
    events: &[notify_debouncer_full::DebouncedEvent],
    extensions: &Option<Vec<String>>,
    gitignore: &Option<GitIgnoreChecker>,
) -> HashSet<PathBuf> {
    let mut paths = HashSet::new();

    for event in events {
        for path in &event.paths {
            // Skip if gitignored
            if let Some(checker) = gitignore
                && checker.is_ignored(path)
            {
                continue;
            }

            // Skip if wrong extension
            if let Some(exts) = extensions {
                let has_matching_ext = path
                    .extension()
                    .and_then(|e| e.to_str())
                    .is_some_and(|ext_str| exts.iter().any(|e| e == ext_str));
                if !has_matching_ext {
                    continue;
                }
            }

            // Skip directories
            if path.is_dir() {
                continue;
            }

            paths.insert(path.clone());
        }
    }

    paths
}

/// Count files that would be tracked (used only by external callers, not watch loop)
pub fn count_tracked_files(
    roots: &[PathBuf],
    extensions: &Option<Vec<String>>,
    gitignore: &Option<GitIgnoreChecker>,
) -> usize {
    let mut count = 0;

    for root in roots {
        if let Ok(walker) = walkdir::WalkDir::new(root)
            .follow_links(false)
            .into_iter()
            .collect::<Result<Vec<_>, _>>()
        {
            for entry in walker {
                if !entry.file_type().is_file() {
                    continue;
                }

                let path = entry.path();

                // Skip if gitignored
                if let Some(checker) = gitignore
                    && checker.is_ignored(path)
                {
                    continue;
                }

                // Skip if wrong extension
                if let Some(exts) = extensions {
                    let has_matching_ext = path
                        .extension()
                        .and_then(|e| e.to_str())
                        .is_some_and(|ext_str| exts.iter().any(|e| e == ext_str));
                    if !has_matching_ext {
                        continue;
                    }
                }

                count += 1;
            }
        }
    }

    count
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_watch_config_defaults() {
        let config = WatchConfig::default();
        assert_eq!(config.roots, vec![PathBuf::from(".")]);
        assert_eq!(config.debounce_duration, Duration::from_millis(500));
        assert!(config.extensions.is_none());
        assert!(config.gitignore.is_none());
    }

    #[test]
    fn test_watch_config_custom() {
        let config = WatchConfig {
            roots: vec![PathBuf::from("src"), PathBuf::from("tests")],
            debounce_duration: Duration::from_millis(1000),
            extensions: Some(vec!["ts".to_string(), "tsx".to_string()]),
            gitignore: None,
        };

        assert_eq!(config.roots.len(), 2);
        assert_eq!(config.debounce_duration, Duration::from_millis(1000));
        assert!(config.extensions.is_some());
        assert_eq!(config.extensions.unwrap(), vec!["ts", "tsx"]);
    }

    #[test]
    fn test_count_tracked_files() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();

        fs::write(temp.path().join("test1.ts"), "").unwrap();
        fs::write(temp.path().join("test2.ts"), "").unwrap();
        fs::write(temp.path().join("test3.js"), "").unwrap();
        fs::write(temp.path().join("readme.txt"), "").unwrap();

        let count = count_tracked_files(std::slice::from_ref(&root), &None, &None);
        assert_eq!(count, 4);

        let extensions = Some(vec!["ts".to_string()]);
        let count_filtered = count_tracked_files(std::slice::from_ref(&root), &extensions, &None);
        assert_eq!(count_filtered, 2);
    }

    #[test]
    fn test_count_tracked_files_empty_directory() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();
        let count = count_tracked_files(&[root], &None, &None);
        assert_eq!(count, 0);
    }

    #[test]
    fn test_count_tracked_files_nested() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();
        let subdir = temp.path().join("subdir");
        fs::create_dir(&subdir).unwrap();

        fs::write(temp.path().join("root.ts"), "").unwrap();
        fs::write(subdir.join("nested.ts"), "").unwrap();

        let count = count_tracked_files(&[root], &None, &None);
        assert_eq!(count, 2);
    }

    #[test]
    fn test_count_tracked_files_multiple_roots() {
        let temp1 = TempDir::new().unwrap();
        let temp2 = TempDir::new().unwrap();
        let root1 = temp1.path().to_path_buf();
        let root2 = temp2.path().to_path_buf();

        fs::write(temp1.path().join("file1.ts"), "").unwrap();
        fs::write(temp2.path().join("file2.ts"), "").unwrap();

        let count = count_tracked_files(&[root1, root2], &None, &None);
        assert_eq!(count, 2);
    }

    #[test]
    fn test_count_tracked_files_with_extension_filter() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();

        fs::write(temp.path().join("file.ts"), "").unwrap();
        fs::write(temp.path().join("file.js"), "").unwrap();
        fs::write(temp.path().join("file.tsx"), "").unwrap();
        fs::write(temp.path().join("readme.md"), "").unwrap();

        let extensions = Some(vec!["ts".to_string(), "tsx".to_string()]);
        let count = count_tracked_files(&[root], &extensions, &None);
        assert_eq!(count, 2);
    }

    #[test]
    fn test_count_tracked_files_ignores_subdirectories() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();
        let subdir = temp.path().join("subdir");
        fs::create_dir(&subdir).unwrap();

        fs::write(temp.path().join("file.ts"), "").unwrap();

        let count = count_tracked_files(&[root], &None, &None);
        assert_eq!(count, 1);
    }

    #[test]
    fn test_debounce_duration_custom() {
        let config = WatchConfig {
            debounce_duration: Duration::from_millis(250),
            ..Default::default()
        };
        assert_eq!(config.debounce_duration, Duration::from_millis(250));
    }

    #[test]
    fn test_watch_config_with_gitignore() {
        let config = WatchConfig {
            gitignore: None,
            ..Default::default()
        };
        assert!(config.gitignore.is_none());
    }

    #[test]
    fn test_watch_config_with_multiple_extensions() {
        let config = WatchConfig {
            extensions: Some(vec![
                "ts".to_string(),
                "tsx".to_string(),
                "js".to_string(),
                "jsx".to_string(),
            ]),
            ..Default::default()
        };

        assert_eq!(config.extensions.unwrap().len(), 4);
    }

    #[test]
    fn test_patch_snapshot_updates_existing_file() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();
        let root_canon = root.canonicalize().unwrap();

        // Create a simple TS file
        let file = root.join("src").join("app.ts");
        fs::create_dir_all(file.parent().unwrap()).unwrap();
        fs::write(&file, "export function hello() { return 42; }").unwrap();

        // Build minimal snapshot with one existing file
        let mut snap = Snapshot::new(vec![]);
        snap.files.push(crate::types::FileAnalysis {
            path: "src/app.ts".to_string(),
            ..Default::default()
        });

        let infra = ScanInfra {
            root_canon: root_canon.clone(),
            all_roots_canon: vec![root_canon.clone()],
            snapshot_root: root.clone(),
            extensions: Some(
                crate::analyzer::runner::default_analyzer_exts()
                    .into_iter()
                    .collect(),
            ),
            ts_resolver: None,
            py_roots: vec![],
            py_stdlib: HashSet::new(),
            custom_command_macros: vec![],
            command_detection: CommandDetectionConfig::default(),
            git_context: snapshot::GitContext {
                repo: None,
                branch: None,
                commit: None,
                scan_id: None,
            },
        };

        let mut changed = HashSet::new();
        changed.insert(file);

        let parsed = ParsedArgs::default();
        let stats = patch_snapshot(&mut snap, &infra, &changed, &parsed).unwrap();

        assert_eq!(stats.updated, 1, "should count as updated");
        assert_eq!(stats.added, 0, "existing file should not be added");
        assert_eq!(stats.deleted, 0);
        assert_eq!(snap.files.len(), 1);
        // File should have been re-analyzed (exports detected)
        assert!(!snap.files[0].exports.is_empty() || snap.files[0].loc > 0);
    }

    #[test]
    fn test_patch_snapshot_adds_new_file() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();
        let root_canon = root.canonicalize().unwrap();

        // Create a new TS file (not in snapshot)
        let file = root.join("src").join("new.ts");
        fs::create_dir_all(file.parent().unwrap()).unwrap();
        fs::write(&file, "export const x = 1;").unwrap();

        let mut snap = Snapshot::new(vec![]);

        let infra = ScanInfra {
            root_canon: root_canon.clone(),
            all_roots_canon: vec![root_canon.clone()],
            snapshot_root: root.clone(),
            extensions: Some(
                crate::analyzer::runner::default_analyzer_exts()
                    .into_iter()
                    .collect(),
            ),
            ts_resolver: None,
            py_roots: vec![],
            py_stdlib: HashSet::new(),
            custom_command_macros: vec![],
            command_detection: CommandDetectionConfig::default(),
            git_context: snapshot::GitContext {
                repo: None,
                branch: None,
                commit: None,
                scan_id: None,
            },
        };

        let mut changed = HashSet::new();
        changed.insert(file);

        let parsed = ParsedArgs::default();
        let stats = patch_snapshot(&mut snap, &infra, &changed, &parsed).unwrap();

        assert_eq!(stats.added, 1, "new file should be added");
        assert_eq!(stats.updated, 0, "new file should NOT count as updated");
        assert_eq!(snap.files.len(), 1);
        assert_eq!(snap.files[0].path, "src/new.ts");
    }

    #[test]
    fn test_patch_snapshot_deletes_file() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();
        let root_canon = root.canonicalize().unwrap();

        // File doesn't exist on disk but is in snapshot.
        // Create the parent dir so canonicalize() works on the parent.
        let src_dir = root.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        let file = src_dir.join("deleted.ts");

        let mut snap = Snapshot::new(vec![]);
        snap.files.push(crate::types::FileAnalysis {
            path: "src/deleted.ts".to_string(),
            ..Default::default()
        });
        snap.edges.push(snapshot::GraphEdge {
            from: "src/deleted.ts".to_string(),
            to: "src/other.ts".to_string(),
            label: "import".to_string(),
        });

        let infra = ScanInfra {
            root_canon: root_canon.clone(),
            all_roots_canon: vec![root_canon.clone()],
            snapshot_root: root.clone(),
            extensions: Some(
                crate::analyzer::runner::default_analyzer_exts()
                    .into_iter()
                    .collect(),
            ),
            ts_resolver: None,
            py_roots: vec![],
            py_stdlib: HashSet::new(),
            custom_command_macros: vec![],
            command_detection: CommandDetectionConfig::default(),
            git_context: snapshot::GitContext {
                repo: None,
                branch: None,
                commit: None,
                scan_id: None,
            },
        };

        let mut changed = HashSet::new();
        changed.insert(file);

        let parsed = ParsedArgs::default();
        let stats = patch_snapshot(&mut snap, &infra, &changed, &parsed).unwrap();

        assert_eq!(stats.deleted, 1);
        assert_eq!(stats.updated, 0);
        assert!(snap.files.is_empty(), "deleted file should be removed");
        assert!(
            snap.edges.is_empty(),
            "edges from deleted file should be removed"
        );
    }

    #[test]
    fn test_export_index_excludes_test_fixtures() {
        let temp = TempDir::new().unwrap();
        let root = temp.path().to_path_buf();
        let root_canon = root.canonicalize().unwrap();

        // Create a test fixture file and a real source file
        let fixture = root.join("tests").join("fixtures").join("fixture.ts");
        let source = root.join("src").join("utils.ts");
        fs::create_dir_all(fixture.parent().unwrap()).unwrap();
        fs::create_dir_all(source.parent().unwrap()).unwrap();
        fs::write(&fixture, "export function testHelper() {}").unwrap();
        fs::write(&source, "export function realUtil() {}").unwrap();

        let mut snap = Snapshot::new(vec![]);

        let infra = ScanInfra {
            root_canon: root_canon.clone(),
            all_roots_canon: vec![root_canon.clone()],
            snapshot_root: root.clone(),
            extensions: Some(
                crate::analyzer::runner::default_analyzer_exts()
                    .into_iter()
                    .collect(),
            ),
            ts_resolver: None,
            py_roots: vec![],
            py_stdlib: HashSet::new(),
            custom_command_macros: vec![],
            command_detection: CommandDetectionConfig::default(),
            git_context: snapshot::GitContext {
                repo: None,
                branch: None,
                commit: None,
                scan_id: None,
            },
        };

        let mut changed = HashSet::new();
        changed.insert(fixture);
        changed.insert(source);

        let parsed = ParsedArgs::default();
        patch_snapshot(&mut snap, &infra, &changed, &parsed).unwrap();

        // The fixture's export should NOT be in the export_index
        assert!(
            !snap.export_index.contains_key("testHelper"),
            "test fixture exports should be excluded from export_index"
        );
        // The real source's export SHOULD be in the export_index
        assert!(
            snap.export_index.contains_key("realUtil"),
            "real source exports should be in export_index"
        );
    }

    #[test]
    fn test_multi_root_strip_prefix() {
        // Test that path resolution correctly strips prefix from multiple roots
        // (without calling analyze_file, which validates single-root)
        let temp1 = TempDir::new().unwrap();
        let temp2 = TempDir::new().unwrap();
        let root1_canon = temp1.path().canonicalize().unwrap();
        let root2_canon = temp2.path().canonicalize().unwrap();

        let all_roots = [root1_canon.clone(), root2_canon.clone()];

        // File under root1
        let file1 = root1_canon.join("src").join("app.ts");
        let rel1 = all_roots
            .iter()
            .find_map(|root| {
                file1
                    .strip_prefix(root)
                    .ok()
                    .map(|p| p.to_string_lossy().to_string())
            })
            .unwrap();
        assert_eq!(rel1, "src/app.ts");

        // File under root2
        let file2 = root2_canon.join("lib").join("utils.ts");
        let rel2 = all_roots
            .iter()
            .find_map(|root| {
                file2
                    .strip_prefix(root)
                    .ok()
                    .map(|p| p.to_string_lossy().to_string())
            })
            .unwrap();
        assert_eq!(rel2, "lib/utils.ts");
    }
}