rto-graph 1.6.0

Provenance-tagged codebase knowledge graph store for Roteiro
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
//! Integration tests for the content-addressed sync engine against real git
//! fixture repositories built with the `git` CLI.
//!
//! Covers the Stage 2 Definition of Done: a cold sync populates the store; an
//! unchanged tree is a no-op that extracts nothing; a single-file change
//! re-extracts exactly one blob; and the content-addressed cache is reused
//! across independent stores (the property that makes it worktree/branch
//! correct).

use std::path::{Path, PathBuf};
use std::process::Command;

use rto_graph::{
    EdgeKind, FileNodeExtractor, ObjectCache, Registry, Repo, Store, sync, sync_worktree,
};

/// Run `git` in `dir` with hermetic identity/signing settings, asserting success.
fn git(dir: &Path, args: &[&str]) {
    let status = Command::new("git")
        .args([
            "-c",
            "user.name=Test",
            "-c",
            "user.email=test@example.com",
            "-c",
            "commit.gpgsign=false",
            "-c",
            "init.defaultBranch=main",
        ])
        .args(args)
        .current_dir(dir)
        .status()
        .expect("failed to run git (is it installed?)");
    assert!(status.success(), "git {args:?} failed");
}

fn write(dir: &Path, rel: &str, content: &str) {
    let path = dir.join(rel);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).expect("mkdir");
    }
    std::fs::write(path, content).expect("write file");
}

/// Create a fresh temp directory unique to `name`, removing any stale copy.
fn fresh_dir(name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("roteiro-sync-{}-{}", std::process::id(), name));
    std::fs::remove_dir_all(&dir).ok();
    std::fs::create_dir_all(&dir).expect("create temp dir");
    dir
}

/// Open a cache under the repository's common git dir (shared across worktrees).
fn cache_for(repo: &Repo) -> ObjectCache {
    ObjectCache::open(repo.common_dir().join("roteiro/objects")).expect("open cache")
}

#[test]
fn cold_then_noop_then_single_file_change() {
    let dir = fresh_dir("incremental");
    git(&dir, &["init", "-q"]);
    write(&dir, "a.txt", "alpha\n");
    write(&dir, "b.txt", "beta\n");
    write(&dir, "src/c.rs", "fn main() {}\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "initial"]);

    let repo = Repo::discover(&dir).expect("discover");
    assert!(repo.git_dir().exists(), "git dir should resolve");
    let cache = cache_for(&repo);
    let mut store = Store::open_in_memory().expect("store");
    let ex = FileNodeExtractor;

    // (a) Cold sync populates the store; every blob is a cache miss.
    let r1 = sync(&mut store, &repo, &cache, &ex).expect("cold sync");
    assert!(!r1.no_op);
    assert_eq!(r1.blobs_total, 3);
    assert_eq!(r1.blobs_extracted, 3);
    assert_eq!(r1.blobs_cached, 0);
    assert_eq!(r1.nodes, 3);
    assert!(store.get_node("file:a.txt").expect("get").is_some());
    assert!(store.get_node("file:src/c.rs").expect("get").is_some());

    // (b) Re-sync with an unchanged tree is a no-op: zero blobs touched.
    let r2 = sync(&mut store, &repo, &cache, &ex).expect("noop sync");
    assert!(r2.no_op);
    assert_eq!(r2.blobs_extracted, 0);
    assert_eq!(r2.nodes, 3);
    assert_eq!(r1.tree, r2.tree);

    // (c) Change one file and commit: the incremental fast path touches *only*
    // the changed blob (tree-diff against the last-synced tree), carrying the two
    // unchanged files' facts forward from the store rather than re-reading them.
    write(&dir, "b.txt", "beta changed\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "edit b"]);

    let r3 = sync(&mut store, &repo, &cache, &ex).expect("incremental sync");
    assert!(!r3.no_op);
    assert_eq!(r3.blobs_total, 3, "the whole tree is still reported");
    assert_eq!(
        r3.blobs_extracted, 1,
        "only the changed blob is re-extracted"
    );
    assert_eq!(
        r3.blobs_cached, 0,
        "the incremental path processes only the changed blob; unchanged facts \
         are carried forward from the store, not re-read"
    );
    assert_eq!(
        r3.nodes, 3,
        "unchanged file nodes survive the incremental sync"
    );
    assert_ne!(r1.tree, r3.tree);

    std::fs::remove_dir_all(&dir).expect("cleanup");
}

#[test]
fn removed_file_drops_its_node() {
    let dir = fresh_dir("removal");
    git(&dir, &["init", "-q"]);
    write(&dir, "keep.txt", "keep\n");
    write(&dir, "gone.txt", "gone\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "two files"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let mut store = Store::open_in_memory().expect("store");
    let ex = FileNodeExtractor;

    sync(&mut store, &repo, &cache, &ex).expect("sync");
    assert!(store.get_node("file:gone.txt").expect("get").is_some());

    git(&dir, &["rm", "-q", "gone.txt"]);
    git(&dir, &["commit", "-q", "-m", "remove gone"]);

    let r = sync(&mut store, &repo, &cache, &ex).expect("sync after rm");
    assert_eq!(r.nodes, 1);
    assert!(store.get_node("file:gone.txt").expect("get").is_none());
    assert!(store.get_node("file:keep.txt").expect("get").is_some());

    std::fs::remove_dir_all(&dir).expect("cleanup");
}

#[test]
fn duplicate_content_at_distinct_paths_stays_distinct() {
    // Two files with identical content share a single git blob oid (git dedupes
    // by content). Keying the cache by oid alone would collapse them into one
    // node; keying by (path, oid) keeps them separate.
    let dir = fresh_dir("dup-content");
    git(&dir, &["init", "-q"]);
    write(&dir, "a.txt", "same\n");
    write(&dir, "b.txt", "same\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "duplicate content"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let mut store = Store::open_in_memory().expect("store");
    let ex = FileNodeExtractor;

    let r = sync(&mut store, &repo, &cache, &ex).expect("sync");
    // Both blobs are extracted despite sharing an oid, and both nodes exist.
    assert_eq!(r.blobs_total, 2);
    assert_eq!(
        r.blobs_extracted, 2,
        "identical-content files must not share a cache entry"
    );
    assert_eq!(r.nodes, 2);
    assert!(store.get_node("file:a.txt").expect("get a").is_some());
    assert!(store.get_node("file:b.txt").expect("get b").is_some());

    std::fs::remove_dir_all(&dir).expect("cleanup");
}

#[test]
fn rust_extraction_produces_derived_graph_with_cross_file_calls() {
    // A two-file Rust project: `main` in one file calls `helper` defined in the
    // other. Extraction is per-file, so this call can only be linked at assembly
    // time — exactly what the sync engine's call resolution does.
    let dir = fresh_dir("rust-derive");
    git(&dir, &["init", "-q"]);
    write(
        &dir,
        "src/main.rs",
        "mod util;\nfn main() {\n    util::helper();\n}\n",
    );
    write(&dir, "src/util.rs", "pub fn helper() -> u32 {\n    42\n}\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "two rust files"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let mut store = Store::open_in_memory().expect("store");

    let r = sync(&mut store, &repo, &cache, &Registry::default()).expect("sync");
    assert!(!r.no_op);

    // Symbol nodes are derived from both files.
    assert!(
        store
            .get_node("sym:rust:src/main.rs#main")
            .expect("m")
            .is_some()
    );
    assert!(
        store
            .get_node("sym:rust:src/util.rs#helper")
            .expect("h")
            .is_some()
    );

    // The file `defines` its top-level function.
    let defines = store.edges_from("file:src/main.rs").expect("defines");
    assert!(
        defines
            .iter()
            .any(|e| e.kind == EdgeKind::Defines && e.dst == "sym:rust:src/main.rs#main"),
    );

    // The cross-file call `main -> helper` is resolved to a derived `calls` edge.
    let calls = store
        .edges_from("sym:rust:src/main.rs#main")
        .expect("calls");
    assert!(
        calls
            .iter()
            .any(|e| e.kind == EdgeKind::Calls && e.dst == "sym:rust:src/util.rs#helper"),
        "cross-file call main -> helper should resolve",
    );

    // A second sync over the unchanged tree is a cache-stable no-op.
    let r2 = sync(&mut store, &repo, &cache, &Registry::default()).expect("resync");
    assert!(r2.no_op);
    assert_eq!(r2.nodes, r.nodes);
    assert_eq!(r2.edges, r.edges);

    std::fs::remove_dir_all(&dir).expect("cleanup");
}

#[test]
fn dirty_overlay_previews_uncommitted_edits() {
    // The working-tree sync overlays uncommitted edits to tracked files on top
    // of the committed graph, so a symbol added but not yet committed is visible.
    let dir = fresh_dir("dirty-overlay");
    git(&dir, &["init", "-q"]);
    write(&dir, "main.rs", "fn main() {}\n");
    write(&dir, "util.rs", "pub fn helper() {}\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "committed"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let mut store = Store::open_in_memory().expect("store");

    // A clean working tree: no overlay, committed symbols present.
    let r0 = sync_worktree(&mut store, &repo, &cache, &Registry::default()).expect("clean");
    assert_eq!(r0.blobs_dirty, 0);
    assert!(
        store
            .get_node("sym:rust:util.rs#helper")
            .expect("h")
            .is_some()
    );
    assert!(
        store
            .get_node("sym:rust:util.rs#added")
            .expect("a")
            .is_none()
    );

    // Edit a tracked file WITHOUT committing: the new symbol is previewed.
    write(&dir, "util.rs", "pub fn helper() {}\npub fn added() {}\n");
    let r1 = sync_worktree(&mut store, &repo, &cache, &Registry::default()).expect("dirty");
    assert!(!r1.no_op);
    assert_eq!(r1.blobs_dirty, 1);
    assert_eq!(
        r1.blobs_extracted, 0,
        "committed blobs still come from cache"
    );
    assert!(
        store
            .get_node("sym:rust:util.rs#added")
            .expect("added")
            .is_some(),
        "uncommitted symbol should be previewed",
    );

    // Re-running with the same dirty state is a no-op.
    let r2 = sync_worktree(&mut store, &repo, &cache, &Registry::default()).expect("resync");
    assert!(r2.no_op);
    assert_eq!(r2.blobs_dirty, 1);

    // Deleting a tracked file in the working tree drops its symbols.
    std::fs::remove_file(dir.join("util.rs")).expect("rm");
    let r3 = sync_worktree(&mut store, &repo, &cache, &Registry::default()).expect("deleted");
    assert!(!r3.no_op);
    assert!(
        store
            .get_node("sym:rust:util.rs#helper")
            .expect("h2")
            .is_none()
    );

    // A committed-only sync supersedes the overlay, restoring the HEAD view.
    let r4 = sync(&mut store, &repo, &cache, &Registry::default()).expect("committed");
    assert!(!r4.no_op);
    assert_eq!(r4.blobs_dirty, 0);
    assert!(
        store
            .get_node("sym:rust:util.rs#helper")
            .expect("h3")
            .is_some()
    );
    assert!(
        store
            .get_node("sym:rust:util.rs#added")
            .expect("a3")
            .is_none()
    );

    std::fs::remove_dir_all(&dir).expect("cleanup");
}

#[test]
fn cache_is_reused_across_independent_stores() {
    let dir = fresh_dir("cache-reuse");
    git(&dir, &["init", "-q"]);
    write(&dir, "one.txt", "1\n");
    write(&dir, "two.txt", "2\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "init"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let ex = FileNodeExtractor;

    // First store populates the shared cache.
    let mut store_a = Store::open_in_memory().expect("store a");
    let ra = sync(&mut store_a, &repo, &cache, &ex).expect("sync a");
    assert_eq!(ra.blobs_extracted, 2);
    assert_eq!(ra.blobs_cached, 0);

    // A second, independent store syncing the same tree extracts nothing — it
    // reuses the content-addressed cache. This is what makes the cache correct
    // across worktrees/branches that share blobs.
    let mut store_b = Store::open_in_memory().expect("store b");
    let rb = sync(&mut store_b, &repo, &cache, &ex).expect("sync b");
    assert_eq!(rb.blobs_extracted, 0, "shared blobs are cache hits");
    assert_eq!(rb.blobs_cached, 2);
    assert_eq!(rb.nodes, 2);

    std::fs::remove_dir_all(&dir).expect("cleanup");
}

#[test]
fn diff_trees_reports_added_modified_deleted_and_prunes_unchanged() {
    // The incremental-sync primitive: diffing two tree oids yields exactly the
    // changed paths (added/modified with new oid, deleted), and never mentions an
    // unchanged file — gix prunes equal subtrees, so cost tracks the change.
    let dir = fresh_dir("difftrees");
    git(&dir, &["init", "-q"]);
    write(&dir, "src/a.rs", "fn a() {}\n");
    write(&dir, "src/b.rs", "fn b() {}\n");
    write(&dir, "vendor/keep.rs", "fn keep() {}\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "init"]);
    let t1 = Repo::discover(&dir)
        .expect("discover")
        .head_tree_id()
        .expect("t1");

    // Modify a, delete b, add c; leave the whole vendor/ subtree untouched.
    write(&dir, "src/a.rs", "fn a() { let _x = 1; }\n");
    std::fs::remove_file(dir.join("src/b.rs")).expect("rm b");
    write(&dir, "src/c.rs", "fn c() {}\n");
    git(&dir, &["add", "-A"]);
    git(&dir, &["commit", "-q", "-m", "changes"]);
    let repo = Repo::discover(&dir).expect("rediscover");
    let t2 = repo.head_tree_id().expect("t2");

    let diff = repo.diff_trees(&t1, &t2).expect("diff");
    let changed: Vec<&str> = diff.changed.iter().map(|b| b.path.as_str()).collect();
    assert_eq!(
        changed,
        ["src/a.rs", "src/c.rs"],
        "a modified + c added, sorted"
    );
    assert_eq!(diff.deleted, ["src/b.rs"], "b deleted");
    assert!(
        !changed.contains(&"vendor/keep.rs"),
        "the unchanged subtree is pruned, not reported"
    );
    // The reported oid for the added file matches its committed blob.
    let c_oid = &diff
        .changed
        .iter()
        .find(|b| b.path == "src/c.rs")
        .unwrap()
        .oid;
    let head = repo.walk_blobs().expect("walk");
    let c_head = head
        .iter()
        .find(|b| b.path == "src/c.rs")
        .expect("c in head");
    assert_eq!(&c_head.oid, c_oid, "diff oid matches the tree blob oid");

    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn incremental_sync_matches_a_full_rebuild() {
    // The incremental committed sync must produce byte-for-byte the same derived
    // graph as a full sync — including the hard case where a change in ONE file
    // flips cross-file call resolution in an UNCHANGED file (a name that was
    // unique becomes ambiguous, so a `calls` edge must disappear).
    let dir = fresh_dir("incr-equiv");
    git(&dir, &["init", "-q"]);
    write(&dir, "a.rs", "pub fn helper() -> u32 {\n    1\n}\n");
    write(&dir, "b.rs", "pub fn caller() {\n    helper();\n}\n"); // unique helper → edge
    write(&dir, "gone.rs", "pub fn gone() {}\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "init"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let reg = Registry::default();

    // Cold sync (full) — records the tree + env so the next sync can go incremental.
    let mut incr = Store::open(&dir.join(".git/incr.db")).expect("open incr");
    sync(&mut incr, &repo, &cache, &reg).expect("cold sync");

    // Change set: delete gone.rs, add d.rs with a SECOND `helper` (ambiguity flip).
    // b.rs is left UNCHANGED — its caller→helper edge must still disappear.
    std::fs::remove_file(dir.join("gone.rs")).expect("rm");
    write(&dir, "d.rs", "pub fn helper() -> u32 {\n    2\n}\n");
    git(&dir, &["add", "-A"]);
    git(&dir, &["commit", "-q", "-m", "flip"]);
    let repo = Repo::discover(&dir).expect("rediscover");

    let r = sync(&mut incr, &repo, &cache, &reg).expect("incremental sync");
    // Proof the fast path ran: it touched only the changed blob (d.rs added),
    // not every file in the tree.
    assert!(
        r.blobs_extracted + r.blobs_cached <= 1,
        "incremental should process only changed blobs, saw {}",
        r.blobs_extracted + r.blobs_cached
    );

    // A fresh store at the same HEAD takes the full path (no prior sync state).
    let mut full = Store::open(&dir.join(".git/full.db")).expect("open full");
    sync(&mut full, &repo, &cache, &reg).expect("full sync");

    // Canonicalise and compare the two derived graphs.
    let canon = |fs: rto_graph::FactSet| -> (Vec<String>, Vec<String>) {
        let mut ns: Vec<String> = fs
            .nodes
            .iter()
            .map(|n| format!("{}|{}|{}", n.key, n.kind.as_str(), n.provenance.as_str()))
            .collect();
        let mut es: Vec<String> = fs
            .edges
            .iter()
            .map(|e| {
                format!(
                    "{}|{}|{}|{}",
                    e.kind.as_str(),
                    e.src,
                    e.dst,
                    e.provenance.as_str()
                )
            })
            .collect();
        ns.sort();
        es.sort();
        (ns, es)
    };
    let ci = canon(incr.export_factset().expect("export incr"));
    let cf = canon(full.export_factset().expect("export full"));
    assert_eq!(ci, cf, "incremental sync must equal a full rebuild");

    // Sanity: the now-ambiguous caller→helper edge is gone in both, gone.rs dropped.
    // Canonical edge form is "<kind>|<src>|<dst>|<prov>", so a `calls` edge starts
    // with "calls|" — constrain the check to that kind, not any edge mentioning both.
    assert!(
        !cf.1
            .iter()
            .any(|e| e.starts_with("calls|") && e.contains("#caller") && e.contains("#helper")),
        "the ambiguous calls edge must be dropped: {:?}",
        cf.1
    );
    assert!(
        !cf.0.iter().any(|n| n.contains("gone.rs")),
        "deleted file dropped"
    );

    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn untracked_overlay_includes_new_files_respecting_gitignore() {
    // The working-tree sync overlays brand-new *untracked* files (not only edits
    // to tracked files) so `check`/`review` see new-but-unstaged work — while
    // honouring `.gitignore`.
    let dir = fresh_dir("untracked-overlay");
    git(&dir, &["init", "-q"]);
    write(&dir, "main.rs", "fn main() {}\n");
    write(&dir, ".gitignore", "ignored.rs\n");
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "committed"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let mut store = Store::open_in_memory().expect("store");

    // Baseline: clean tree, only the committed symbol.
    let r0 = sync_worktree(&mut store, &repo, &cache, &Registry::default()).expect("clean");
    assert_eq!(r0.blobs_dirty, 0);

    // A brand-new untracked file (never `git add`ed) and a git-ignored one.
    write(&dir, "fresh.rs", "pub fn brand_new() {}\n");
    write(&dir, "ignored.rs", "pub fn hidden() {}\n");

    let r = sync_worktree(&mut store, &repo, &cache, &Registry::default()).expect("untracked");
    assert!(!r.no_op);
    assert_eq!(
        r.blobs_dirty, 1,
        "only the untracked (non-ignored) file is new"
    );
    assert!(
        store
            .get_node("sym:rust:fresh.rs#brand_new")
            .expect("q")
            .is_some(),
        "untracked new file should be overlaid into the graph",
    );
    assert!(
        store
            .get_node("sym:rust:ignored.rs#hidden")
            .expect("q2")
            .is_none(),
        "git-ignored file must not be ingested",
    );

    // The low-level API classifies correctly: the new file is untracked, the
    // ignored and tracked files are not.
    let untracked = repo.untracked_files().expect("untracked");
    assert!(untracked.contains(&"fresh.rs".to_owned()));
    assert!(
        !untracked.iter().any(|p| p == "ignored.rs"),
        "ignored excluded"
    );
    assert!(
        !untracked.iter().any(|p| p == "main.rs"),
        "tracked excluded"
    );

    // Removing the untracked file returns to the committed baseline.
    std::fs::remove_file(dir.join("fresh.rs")).expect("rm");
    sync_worktree(&mut store, &repo, &cache, &Registry::default()).expect("removed");
    assert!(
        store
            .get_node("sym:rust:fresh.rs#brand_new")
            .expect("q3")
            .is_none(),
        "removing the untracked file drops its symbols",
    );

    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn scope_aware_calls_disambiguate_ambiguous_names() {
    // Two functions named `render`: a free `a::render` and a method `S::render`.
    // The bare name is ambiguous, so simple-name-only resolution would link
    // *neither* caller. The qualifier (`a::render()`) and the `self` receiver
    // (`self.render()`) each pick out exactly one target.
    let dir = fresh_dir("scope-calls");
    git(&dir, &["init", "-q"]);
    write(
        &dir,
        "src/lib.rs",
        "mod a {\n    pub fn render() {}\n}\n\
         struct S;\n\
         impl S {\n    \
         fn render(&self) {}\n    \
         fn go(&self) {\n        self.render();\n    }\n}\n\
         fn drive() {\n    a::render();\n}\n",
    );
    git(&dir, &["add", "."]);
    git(&dir, &["commit", "-q", "-m", "ambiguous render"]);

    let repo = Repo::discover(&dir).expect("discover");
    let cache = cache_for(&repo);
    let mut store = Store::open_in_memory().expect("store");
    sync(&mut store, &repo, &cache, &Registry::default()).expect("sync");

    let calls_from = |key: &str| -> Vec<String> {
        let mut v: Vec<String> = store
            .edges_from(key)
            .expect("edges")
            .into_iter()
            .filter(|e| e.kind == EdgeKind::Calls)
            .map(|e| e.dst)
            .collect();
        v.sort();
        v
    };

    // `drive` calls `a::render()` → the module function, not `S::render`.
    assert_eq!(
        calls_from("sym:rust:src/lib.rs#drive"),
        ["sym:rust:src/lib.rs#a::render"],
        "a::render() must bind to the module fn, not the method",
    );
    // `S::go` calls `self.render()` → the same impl's method, not `a::render`.
    assert_eq!(
        calls_from("sym:rust:src/lib.rs#S::go"),
        ["sym:rust:src/lib.rs#S::render"],
        "self.render() must bind to S::render, not the module fn",
    );

    std::fs::remove_dir_all(&dir).ok();
}