symforge 8.17.0

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
// Server-only integration test: depends on a `#[cfg(feature = "server")]`
// module (protocol/daemon/cli/sidecar/watcher/analytics). Gating the whole
// file keeps `--no-default-features --features embed --all-targets` compiling.
#![cfg(feature = "server")]

use parking_lot::Mutex;
/// Integration tests for the file watcher — proves FRSH-01 through FRSH-06 and RELY-03.
///
/// Each test uses a real tempdir, spawns the watcher via tokio::spawn, performs a
/// filesystem operation, waits for the debounce window to pass, then queries the
/// live LiveIndex to confirm the expected mutation.
///
/// Timing: debounce window is 200ms; tests wait 500ms (200ms debounce + 300ms margin).
///
/// Test map:
///   test_watcher_detects_modify_and_updates_index  → FRSH-01, FRSH-03, FRSH-06
///   test_watcher_indexes_new_file                  → FRSH-04
///   test_watcher_removes_deleted_file              → FRSH-05
///   test_watcher_hash_skip_on_noop_write           → content_hash optimization
///   test_watcher_enoent_handled_gracefully         → RELY-03
///   test_single_file_reparse_under_50ms            → FRSH-02
///   test_watcher_state_reports_active              → health extension
///   test_watcher_ignores_non_source_files          → filter correctness
use std::fs;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use symforge::live_index::LiveIndex;
use symforge::watcher::{WatcherInfo, WatcherState, run_watcher};
use tempfile::TempDir;

// ---------------------------------------------------------------------------
// Test helpers
// ---------------------------------------------------------------------------

/// Write a file, creating parent directories as needed.
fn write_file(dir: &Path, name: &str, content: &str) {
    let path = dir.join(name);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).unwrap();
    }
    fs::write(path, content).unwrap();
}

/// Spawn the watcher as a background tokio task and wait for it to initialize.
///
/// Returns the Arc<Mutex<WatcherInfo>> so tests can inspect watcher state.
async fn spawn_watcher(
    dir: &TempDir,
    shared: &symforge::live_index::SharedIndex,
) -> Arc<Mutex<WatcherInfo>> {
    let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
    let root = dir.path().to_path_buf();
    let index_clone = Arc::clone(shared);
    let info_clone = Arc::clone(&watcher_info);

    tokio::spawn(async move {
        run_watcher(root, index_clone, info_clone).await;
    });

    // Wait for the registered watcher to finish its mandatory fresh-instance
    // reconciliation. A fixed sleep races that repair on slower Windows hosts
    // and can mutate the fixture before the event loop is ready to drain its
    // already-registered channel.
    tokio::time::timeout(Duration::from_secs(5), async {
        loop {
            if watcher_info.lock().state == WatcherState::Active {
                break;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .expect("watcher should become Active after fresh-instance reconciliation");

    watcher_info
}

/// Wait for the debounce window + processing margin.
async fn wait_debounce() {
    tokio::time::sleep(Duration::from_millis(500)).await;
}

// ---------------------------------------------------------------------------
// Test 1: FRSH-01, FRSH-03, FRSH-06 — modify a file → index updated
// ---------------------------------------------------------------------------

/// Prove that overwriting a file with new content causes the watcher to re-index it.
///
/// FRSH-01: file change is detected within 500ms.
/// FRSH-03: updated symbols are queryable immediately after re-index.
/// FRSH-06: editing a function name → the new name is returned from queries.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_detects_modify_and_updates_index() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Write initial file with fn hello()
    write_file(dir.path(), "src/hello.rs", "fn hello() {}");

    let shared = LiveIndex::load(dir.path()).unwrap();

    // Verify initial state
    {
        let index = shared.read();
        let file = index
            .get_file("src/hello.rs")
            .expect("src/hello.rs should be indexed");
        let names: Vec<&str> = file.symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(
            names.contains(&"hello"),
            "initial symbol 'hello' should exist: {names:?}"
        );
    }

    let _watcher_info = spawn_watcher(&dir, &shared).await;

    // Overwrite with fn hello_world()
    write_file(dir.path(), "src/hello.rs", "fn hello_world() {}");

    wait_debounce().await;

    // Verify index reflects the updated symbol
    {
        let index = shared.read();
        let file = index
            .get_file("src/hello.rs")
            .expect("src/hello.rs should still be in index");
        let names: Vec<&str> = file.symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(
            names.contains(&"hello_world"),
            "FRSH-01/03/06: updated symbol 'hello_world' should be in index after edit, got: {names:?}"
        );
        assert!(
            !names.contains(&"hello"),
            "FRSH-06: old symbol 'hello' must be gone after overwrite, got: {names:?}"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 2: FRSH-04 — create a new file → it appears in the index
// ---------------------------------------------------------------------------

/// Prove that creating a new source file causes the watcher to add it to the index.
///
/// FRSH-04: creating a new .rs file makes it appear in repo_outline within 500ms.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_indexes_new_file() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Write a seed file so the index loads with ≥1 file
    write_file(dir.path(), "src/existing.rs", "fn existing() {}");

    let shared = LiveIndex::load(dir.path()).unwrap();
    let initial_count = shared.read().file_count();

    let _watcher_info = spawn_watcher(&dir, &shared).await;

    // Create a brand-new file
    write_file(dir.path(), "src/new_file.rs", "fn new_function() {}");

    wait_debounce().await;

    // Verify the new file is now in the index
    {
        let index = shared.read();
        let new_count = index.file_count();
        assert_eq!(
            new_count,
            initial_count + 1,
            "FRSH-04: file_count should have increased by 1 after creating new_file.rs"
        );

        let file = index
            .get_file("src/new_file.rs")
            .expect("FRSH-04: src/new_file.rs should be in index after create");
        let names: Vec<&str> = file.symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(
            names.contains(&"new_function"),
            "FRSH-04: new file should have 'new_function' symbol, got: {names:?}"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 3: FRSH-05 — delete a file → it is removed from the index
// ---------------------------------------------------------------------------

/// Prove that deleting a source file causes the watcher to remove it from the index.
///
/// FRSH-05: deleting a .rs file removes it from the index within 500ms.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_removes_deleted_file() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    write_file(dir.path(), "src/to_delete.rs", "fn doomed() {}");
    write_file(dir.path(), "src/stable.rs", "fn keeper() {}");

    let shared = LiveIndex::load(dir.path()).unwrap();

    // Verify initial state — to_delete.rs is in the index
    {
        let index = shared.read();
        assert_eq!(index.file_count(), 2, "should have 2 files initially");
        assert!(
            index.get_file("src/to_delete.rs").is_some(),
            "src/to_delete.rs should be in index before delete"
        );
    }

    let _watcher_info = spawn_watcher(&dir, &shared).await;

    // Delete the file
    fs::remove_file(dir.path().join("src/to_delete.rs")).unwrap();

    wait_debounce().await;

    // Verify the file has been removed from the index
    {
        let index = shared.read();
        assert!(
            index.get_file("src/to_delete.rs").is_none(),
            "FRSH-05: src/to_delete.rs should be removed from index after deletion"
        );
        assert_eq!(
            index.file_count(),
            1,
            "FRSH-05: file_count should decrease to 1 after deletion"
        );
        // Stable file must remain
        assert!(
            index.get_file("src/stable.rs").is_some(),
            "src/stable.rs should still be in index after unrelated file was deleted"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 4: Content-hash skip — noop write does not corrupt the index
// ---------------------------------------------------------------------------

/// Prove that writing the same content to a file is handled safely.
///
/// When content is unchanged, the hash-skip optimization fires and skips tree-sitter.
/// The symbols after the write must be identical to the symbols before.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_hash_skip_on_noop_write() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    let content = "fn stable() {}";
    write_file(dir.path(), "src/stable.rs", content);

    let shared = LiveIndex::load(dir.path()).unwrap();

    // Capture initial symbol names
    let initial_symbols: Vec<String> = {
        let index = shared.read();
        let file = index.get_file("src/stable.rs").unwrap();
        file.symbols.iter().map(|s| s.name.clone()).collect()
    };

    let watcher_info = spawn_watcher(&dir, &shared).await;
    let events_before = watcher_info.lock().events_processed;

    // Overwrite with SAME content — hash-skip should fire
    write_file(dir.path(), "src/stable.rs", content);

    wait_debounce().await;

    // Symbols should be identical (hash-skip means no re-parse happened,
    // but even if it did, the result should be the same)
    {
        let index = shared.read();
        let file = index
            .get_file("src/stable.rs")
            .expect("src/stable.rs should still be indexed after noop write");
        let after_symbols: Vec<String> = file.symbols.iter().map(|s| s.name.clone()).collect();
        assert_eq!(
            initial_symbols, after_symbols,
            "hash-skip: symbols should be unchanged after writing identical content"
        );
    }

    // Verify watcher processed the event (counted it) or at minimum didn't crash
    let events_after = watcher_info.lock().events_processed;
    let _ = events_before; // events_after may or may not be > events_before (hash-skip counts the event)
    let _ = events_after;
}

// ---------------------------------------------------------------------------
// Test 5: RELY-03 — ENOENT handled gracefully, no panic, watcher stays active
// ---------------------------------------------------------------------------

/// Prove that deleting a file does not crash the watcher (RELY-03).
///
/// The watcher must handle the delete event gracefully: remove the file from
/// the index and remain in Active state (not Degraded).
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_enoent_handled_gracefully() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    write_file(dir.path(), "src/fragile.rs", "fn at_risk() {}");
    write_file(dir.path(), "src/anchor.rs", "fn anchor() {}");

    let shared = LiveIndex::load(dir.path()).unwrap();

    let watcher_info = spawn_watcher(&dir, &shared).await;

    // Delete fragile.rs — triggers ENOENT path in maybe_reindex
    fs::remove_file(dir.path().join("src/fragile.rs")).unwrap();

    wait_debounce().await;

    // Verify no panic (if we reach here, no panic occurred)
    // Verify file removed from index
    {
        let index = shared.read();
        assert!(
            index.get_file("src/fragile.rs").is_none(),
            "RELY-03: fragile.rs should be removed from index after deletion"
        );
    }

    // Verify watcher is still Active (RELY-03: no crash, graceful degradation path not taken)
    {
        let info = watcher_info.lock();
        assert_eq!(
            info.state,
            WatcherState::Active,
            "RELY-03: watcher should remain Active after ENOENT; got: {:?}",
            info.state
        );
    }
}

// ---------------------------------------------------------------------------
// Test 6: FRSH-02 — single file re-parse produces correct symbols
// ---------------------------------------------------------------------------

/// A moderate Rust source (~20 lines) shared by the correctness gate and the
/// `#[ignore]`-d perf smoke. Exercises a free function, a struct, and an impl
/// with three methods.
const FRSH_02_SOURCE: &str = r#"
/// A moderately complex function for benchmarking the parser.
pub fn compute_sum(items: &[u32]) -> u32 {
    let mut total = 0u32;
    for &item in items {
        if item % 2 == 0 {
            total += item;
        } else {
            total = total.saturating_add(item * 2);
        }
    }
    total
}

pub struct Accumulator {
    values: Vec<u32>,
    threshold: u32,
}

impl Accumulator {
    pub fn new(threshold: u32) -> Self {
        Self { values: Vec::new(), threshold }
    }

    pub fn push(&mut self, val: u32) {
        self.values.push(val);
    }

    pub fn result(&self) -> u32 {
        self.values.iter().copied().sum()
    }
}
"#;

/// FRSH-02 (correctness gate): a single-file re-parse succeeds and produces the
/// expected symbols. This asserts real behavior, not wall-clock timing — a
/// debug-profile latency SLA is host-dependent and was flaky in the gate
/// (WSL2 hit 81ms against a 50ms bound). The timing budget moved to the
/// `#[ignore]`-d perf smoke below, which only runs under scheduled/manual CI.
#[test]
fn test_single_file_reparse_produces_expected_symbols() {
    use symforge::domain::{FileOutcome, LanguageId};
    use symforge::parsing;

    let result = parsing::process_file("bench.rs", FRSH_02_SOURCE.as_bytes(), LanguageId::Rust);

    assert!(
        matches!(result.outcome, FileOutcome::Processed),
        "FRSH-02: single-file re-parse must succeed cleanly, got {:?}",
        result.outcome
    );
    assert!(
        result.parse_diagnostic.is_none(),
        "FRSH-02: clean source must not emit a parse diagnostic: {:?}",
        result.parse_diagnostic
    );

    // The parser must surface the top-level symbols we expect from the fixture.
    let names: Vec<&str> = result.symbols.iter().map(|s| s.name.as_str()).collect();
    for expected in ["compute_sum", "Accumulator", "new", "push", "result"] {
        assert!(
            names.contains(&expected),
            "FRSH-02: expected symbol `{expected}` in {names:?}"
        );
    }
}

/// FRSH-02 (perf smoke, `#[ignore]`-d): warmup + median-of-N parse latency with
/// a generous, platform-tolerant threshold. Excluded from the correctness gate;
/// runs only under the scheduled/manual perf CI (which runs `--ignored` smokes).
/// Uses the MEDIAN to absorb scheduler jitter on loaded/slow hosts, and a 250ms
/// ceiling — far above the ~1-10ms a healthy host shows — so it flags real
/// regressions (orders of magnitude) without failing on transient noise.
#[test]
#[ignore = "perf smoke (FRSH-02): runs under scheduled/manual perf CI only"]
fn test_single_file_reparse_perf_smoke() {
    use std::time::Instant;
    use symforge::domain::LanguageId;
    use symforge::parsing;

    let bytes = FRSH_02_SOURCE.as_bytes();

    // Warm up parser/grammar initialization and caches; discard these samples.
    for _ in 0..5 {
        let _ = parsing::process_file("bench.rs", bytes, LanguageId::Rust);
    }

    const SAMPLES: usize = 11;
    let mut timings: Vec<Duration> = Vec::with_capacity(SAMPLES);
    for _ in 0..SAMPLES {
        let start = Instant::now();
        let _ = parsing::process_file("bench.rs", bytes, LanguageId::Rust);
        timings.push(start.elapsed());
    }
    timings.sort_unstable();
    let median = timings[timings.len() / 2];

    assert!(
        median < Duration::from_millis(250),
        "FRSH-02: median single-file re-parse should stay well under 250ms, got {}ms (samples: {:?})",
        median.as_millis(),
        timings
    );
}

// ---------------------------------------------------------------------------
// Test 7: Watcher state reports Active after startup
// ---------------------------------------------------------------------------

/// Prove that WatcherInfo.state transitions to Active after run_watcher initializes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_state_reports_active() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();
    write_file(dir.path(), "src/code.rs", "fn main() {}");

    let shared = LiveIndex::load(dir.path()).unwrap();
    let watcher_info = spawn_watcher(&dir, &shared).await;

    // After spawn_watcher (which waits 100ms for initialization), state should be Active.
    // Allow up to 200ms more for slower CI environments.
    tokio::time::sleep(Duration::from_millis(200)).await;

    let state = watcher_info.lock().state.clone();
    assert_eq!(
        state,
        WatcherState::Active,
        "watcher state should be Active after initialization, got: {:?}",
        state
    );
}

// ---------------------------------------------------------------------------
// Test 9: Rename-replace pattern (editor atomic saves)
// ---------------------------------------------------------------------------

/// Prove that the watcher correctly handles rename-replace atomic saves.
///
/// Editors like VS Code and vim write files via `foo.rs.tmp` → `foo.rs` rename
/// to guarantee atomicity. `notify-debouncer-full` fires Create/Modify/Remove
/// events in quick succession for these; the final state of the index MUST
/// reflect the new content within the debounce window.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_atomic_save_via_rename_replace() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    // Initial content — indexed via a normal write.
    write_file(dir.path(), "src/atomic.rs", "fn initial() {}");

    let shared = LiveIndex::load(dir.path()).unwrap();

    // Verify initial state
    {
        let index = shared.read();
        let file = index
            .get_file("src/atomic.rs")
            .expect("src/atomic.rs should be indexed");
        let names: Vec<&str> = file.symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(
            names.contains(&"initial"),
            "initial symbol 'initial' should exist: {names:?}"
        );
    }

    let _watcher_info = spawn_watcher(&dir, &shared).await;

    // Simulate editor atomic-save: write to `.tmp` in the same directory,
    // then rename over the target. This is what VS Code and vim do.
    let target = dir.path().join("src/atomic.rs");
    let tmp = dir.path().join("src/atomic.rs.tmp");
    fs::write(&tmp, "fn replaced() {}").unwrap();
    fs::rename(&tmp, &target).unwrap();

    wait_debounce().await;

    // Index MUST reflect the new content: rename-replace is a legitimate
    // content change and the watcher must not lose track of the file.
    {
        let index = shared.read();
        let file = index
            .get_file("src/atomic.rs")
            .expect("rename-replace: src/atomic.rs should still be indexed");
        let names: Vec<&str> = file.symbols.iter().map(|s| s.name.as_str()).collect();
        assert!(
            names.contains(&"replaced"),
            "rename-replace: new symbol 'replaced' must be in index after atomic save, got: {names:?}"
        );
        assert!(
            !names.contains(&"initial"),
            "rename-replace: old symbol 'initial' must be gone after atomic save, got: {names:?}"
        );
    }

    // The `.tmp` sibling must not be indexed: its extension is unsupported
    // and it no longer exists on disk.
    {
        let index = shared.read();
        assert!(
            index.get_file("src/atomic.rs.tmp").is_none(),
            "rename-replace: .tmp file must not appear in index"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 10: Rename-replace with content-unchanged payload
// ---------------------------------------------------------------------------

/// Prove that a rename-replace whose payload is byte-identical to the current
/// indexed content does not corrupt the index.
///
/// vim sometimes atomically saves a file the user never edited (e.g. `:w`
/// on an unchanged buffer). The watcher sees Create/Modify/Remove bursts,
/// but the content hash is unchanged, so `maybe_reindex` should HashSkip
/// and leave the index untouched.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_atomic_save_noop_content() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    let content = "fn unchanged() {}";
    write_file(dir.path(), "src/noop.rs", content);

    let shared = LiveIndex::load(dir.path()).unwrap();

    let initial_symbols: Vec<String> = {
        let index = shared.read();
        let file = index.get_file("src/noop.rs").unwrap();
        file.symbols.iter().map(|s| s.name.clone()).collect()
    };

    let _watcher_info = spawn_watcher(&dir, &shared).await;

    // Atomic save whose payload matches the existing bytes.
    let target = dir.path().join("src/noop.rs");
    let tmp = dir.path().join("src/noop.rs.tmp");
    fs::write(&tmp, content).unwrap();
    fs::rename(&tmp, &target).unwrap();

    wait_debounce().await;

    {
        let index = shared.read();
        let file = index
            .get_file("src/noop.rs")
            .expect("rename-replace (noop): file should still be indexed");
        let after: Vec<String> = file.symbols.iter().map(|s| s.name.clone()).collect();
        assert_eq!(
            initial_symbols, after,
            "rename-replace (noop): symbols must be unchanged when bytes are identical"
        );
    }
}

// ---------------------------------------------------------------------------
// Test 8: Watcher ignores non-source files (e.g., README.md)
// ---------------------------------------------------------------------------

/// Prove that creating a genuinely non-indexable file does NOT add it to the
/// resident file map.
///
/// Under the repository knowledge index, text/markdown/config (including `.txt`
/// and `.csv`) are first-class knowledge sources and ARE indexed, so this test
/// uses binary content, which routes to a metadata-only disposition and never
/// enters the resident `file_count()`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_watcher_ignores_non_source_files() {
    let dir = tempfile::tempdir().unwrap();
    let src_dir = dir.path().join("src");
    fs::create_dir_all(&src_dir).unwrap();

    write_file(dir.path(), "src/code.rs", "fn main() {}");

    let shared = LiveIndex::load(dir.path()).unwrap();
    let initial_count = shared.read().file_count();

    let _watcher_info = spawn_watcher(&dir, &shared).await;

    // Binary content is sniffed to a metadata-only disposition and never joins
    // the resident file map, so these must not change file_count().
    fs::write(dir.path().join("blob.bin"), [0u8, 1, 2, 255, 254, 0, 42]).unwrap();
    fs::write(
        dir.path().join("image.png"),
        [0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x00, 0x1a],
    )
    .unwrap();

    wait_debounce().await;

    // Verify file count unchanged (.txt and .csv not indexed)
    {
        let index = shared.read();
        assert_eq!(
            index.file_count(),
            initial_count,
            "watcher should not index non-source files; count should remain {initial_count}, got {}",
            index.file_count()
        );
        assert!(
            index.get_file("notes.txt").is_none(),
            "notes.txt should NOT be in the index (unsupported extension)"
        );
        assert!(
            index.get_file("data.csv").is_none(),
            "data.csv should NOT be in the index (unsupported extension)"
        );
    }
}