weavatrix-rust 1.0.2

Native repository intelligence for coding agents: 39 read-only MCP tools, evidence graph, cross-repository analysis, and lossless parsing
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
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use weavatrix_rust::{Analyzer, EdgeKind, NodeKind};

#[test]
fn extracts_primary_and_optional_language_facts() {
    let fixture = Fixture::new();
    fixture.write(
        "go/server.go",
        "package server\nimport \"net/http\"\nfunc run() { http.HandleFunc(\"/ready\", ready) }\n",
    );
    fixture.write("c/main.c", "int helper(void) { return 1; }\n");
    fixture.write(
        "cpp/main.cpp",
        "#include <vector>\nint compute() { return helper(); }\n",
    );
    fixture.write("ops/run.sh", "function deploy() {\n  kubectl apply\n}\n");
    fixture.write(
        "db/query.sql",
        "CREATE TABLE users(id bigint);\nSELECT * FROM users;\nUPDATE users SET id=2;\n",
    );
    fixture.write(
        "deploy/app.yaml",
        "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  name: api\n",
    );
    fixture.write(
        "web/router.js",
        "export function list() { return db.collection(\"users\"); }\nrouter.get(\"/items\", list);\nconst routes = {\n'/mapped': {\nPOST: list,\n},\n};\n",
    );
    fixture.write(
        "web/client.ts",
        "export class Client {}\nfunction publish(){ topic(\"orders\"); }\n",
    );
    fixture.write(
        "automation/app.py",
        "from flask import Flask\n@app.get(\"/jobs\")\ndef jobs():\n    return run()\n",
    );
    fixture.write(
        "warehouse/Controller.java",
        "public class Controller {\n@GetMapping(\"/stock\") public void stock() {}\n}\n",
    );
    fixture.write(
        "service/Controller.cs",
        "public class Controller {\n[HttpGet(\"/health\")] public void Health() {}\n}\n",
    );

    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    for language in [
        "go",
        "c",
        "cpp",
        "bash",
        "sql",
        "kubernetes",
        "javascript",
        "typescript",
        "python",
        "java",
        "csharp",
    ] {
        assert!(
            snapshot
                .capabilities
                .iter()
                .any(|capability| capability.id == format!("lang:{language}")),
            "missing {language} capability"
        );
    }
    for (kind, label) in [
        (NodeKind::Endpoint, "ANY /ready"),
        (NodeKind::Endpoint, "GET /items"),
        (NodeKind::Endpoint, "POST /mapped"),
        (NodeKind::Endpoint, "GET /jobs"),
        (NodeKind::Endpoint, "GET /stock"),
        (NodeKind::Endpoint, "GET /health"),
        (NodeKind::Table, "users"),
        (NodeKind::Collection, "users"),
        (NodeKind::Topic, "orders"),
        (NodeKind::KubernetesResource, "Deployment/api"),
    ] {
        assert!(
            snapshot
                .nodes
                .iter()
                .any(|node| node.kind == kind && node.label == label),
            "missing {kind:?} {label}"
        );
    }
    assert!(
        snapshot
            .edges
            .iter()
            .any(|edge| edge.kind == EdgeKind::Writes)
    );
    assert!(
        snapshot
            .edges
            .iter()
            .any(|edge| edge.kind == EdgeKind::Deploys)
    );
    assert!(
        snapshot
            .edges
            .iter()
            .any(|edge| edge.kind == EdgeKind::Publishes)
    );
}

#[test]
fn spring_class_and_method_mappings_form_the_served_route() {
    let fixture = Fixture::new();
    fixture.write(
        "warehouse/Controller.java",
        "@RestController\n@RequestMapping(\"warehouse\")\npublic class Controller {\n@GetMapping(\"/stock\") public void stock() {}\n@RequestMapping(\"summary\") public void summary() {}\n}\n",
    );
    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    for endpoint in ["GET /warehouse/stock", "ANY /warehouse/summary"] {
        assert!(
            snapshot
                .nodes
                .iter()
                .any(|node| node.kind == NodeKind::Endpoint && node.label == endpoint),
            "Spring class and method mappings must form the served path: {endpoint}"
        );
    }
    assert!(
        !snapshot
            .nodes
            .iter()
            .any(|node| node.kind == NodeKind::Endpoint && node.label == "ANY /warehouse"),
        "a class-level Spring mapping is a prefix, not a callable endpoint"
    );
}

#[test]
fn resolves_relative_imports_to_repository_files() {
    let fixture = Fixture::new();
    fixture.write(
        "web/client.ts",
        "import { helper } from \"./helper\";\nexport function run(){ helper(); }\n",
    );
    fixture.write("web/helper.ts", "export function helper() {}\n");
    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    assert!(snapshot.edges.iter().any(|edge| {
        edge.kind == EdgeKind::Imports
            && edge.source.as_str() == "file:web/client.ts"
            && edge.target.as_str() == "file:web/helper.ts"
    }));
}

#[test]
fn resolves_imports_inside_nested_python_and_plain_java_source_roots() {
    let fixture = Fixture::new();
    fixture.write(
        "apps/reporting/python/main.py",
        "from service import load_report\n",
    );
    fixture.write(
        "apps/reporting/python/service.py",
        "from utils import normalize\n\ndef load_report():\n    return normalize('x')\n",
    );
    fixture.write(
        "apps/reporting/python/utils.py",
        "def normalize(value):\n    return value\n",
    );
    fixture.write(
        "fixtures/java/src/api/UserReader.java",
        "package api;\nimport model.User;\npublic interface UserReader {}\n",
    );
    fixture.write(
        "fixtures/java/src/model/User.java",
        "package model;\npublic class User {}\n",
    );

    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    let import = |source: &str, target: &str| {
        snapshot.edges.iter().any(|edge| {
            edge.kind == EdgeKind::Imports
                && edge.source.as_str() == source
                && edge.target.as_str() == target
        })
    };
    for (source, target) in [
        (
            "file:apps/reporting/python/main.py",
            "file:apps/reporting/python/service.py",
        ),
        (
            "file:apps/reporting/python/service.py",
            "file:apps/reporting/python/utils.py",
        ),
        (
            "file:fixtures/java/src/api/UserReader.java",
            "file:fixtures/java/src/model/User.java",
        ),
    ] {
        assert!(
            import(source, target),
            "nested source-root import must resolve: {source} -> {target}"
        );
    }
}

#[test]
#[allow(clippy::too_many_lines)]
fn resolves_language_specific_repository_imports() {
    let fixture = Fixture::new();
    // Rust: crate/super/bare module paths across a workspace member.
    fixture.write("src/lib.rs", "mod wlan;\npub use wlan::scan;\n");
    fixture.write("src/wlan.rs", "pub fn scan() {}\n");
    fixture.write(
        "adapters/esp/src/lib.rs",
        "use crate::ble::Driver;\nmod ble;\n",
    );
    fixture.write(
        "adapters/esp/src/ble.rs",
        "use super::x::Y;\npub struct Driver;\n",
    );
    // Python: absolute local package import plus class inheritance.
    fixture.write("pkg/__init__.py", "\n");
    fixture.write(
        "pkg/errors.py",
        "class Base(Exception):\n    pass\nclass Derived(Base):\n    pass\n",
    );
    fixture.write("job.py", "import pkg.errors\nfrom pkg import errors\n");
    // Go: grouped imports through the repository module path plus const block.
    fixture.write(
        "kafkareader/reader.go",
        "package kafkareader\nfunc Read() {}\n",
    );
    let module = fixture
        .root
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap()
        .to_owned();
    fixture.write(
        "flowspec/flowspec.go",
        &format!(
            "package flowspec\nimport (\n\tkr \"edgehawk.com/{module}/kafkareader\"\n)\nconst (\n\tPROTOCOL = 1\n\tSRC_ADDR = 2\n)\nfunc parse() {{ kr.Read() }}\n"
        ),
    );
    // Java: classpath import, field, and no call-chain method false positive.
    fixture.write(
        "src/main/java/com/x/Helper.java",
        "package com.x;\npublic class Helper {}\n",
    );
    fixture.write(
        "src/main/java/com/x/Service.java",
        "package com.x;\nimport com.x.Helper;\nimport static com.x.Helper.help;\npublic class Service {\nprivate final Helper helper = null;\npublic void run() {\nitems.forEach(item -> {\n});\n}\n}\n",
    );
    // JavaScript: CommonJS require and multi-line import closers.
    fixture.write("services/util.js", "module.exports = {};\n");
    fixture.write(
        "app.js",
        "const util = require('./services/util');\nimport {\n  a,\n} from './services/util';\n",
    );

    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    let import = |source: &str, target: &str| {
        snapshot.edges.iter().any(|edge| {
            edge.kind == EdgeKind::Imports
                && edge.source.as_str() == source
                && edge.target.as_str() == target
        })
    };
    if cfg!(feature = "lang-rust") {
        assert!(
            import(
                "file:adapters/esp/src/lib.rs",
                "file:adapters/esp/src/ble.rs"
            ),
            "rust crate:: import must resolve inside the containing crate"
        );
    }
    assert!(
        import("file:job.py", "file:pkg/errors.py"),
        "python absolute module import must resolve to the file"
    );
    assert!(
        import("file:job.py", "file:pkg/__init__.py"),
        "python package import must resolve to __init__"
    );
    assert!(
        import("file:flowspec/flowspec.go", "file:kafkareader/reader.go"),
        "go module-path import must resolve to package files"
    );
    assert!(
        import(
            "file:src/main/java/com/x/Service.java",
            "file:src/main/java/com/x/Helper.java"
        ),
        "java classpath import must resolve through the source root"
    );
    assert!(
        import("file:app.js", "file:services/util.js"),
        "commonjs require must resolve to the file"
    );

    let symbol = |label: &str, kind: &NodeKind| {
        snapshot
            .nodes
            .iter()
            .any(|node| node.label == label && node.kind == *kind)
    };
    assert!(
        symbol("PROTOCOL", &NodeKind::Constant),
        "go const block members are symbols"
    );
    assert!(
        symbol("helper", &NodeKind::Custom("field".to_owned())),
        "java fields are symbols"
    );
    assert!(
        !symbol("forEach", &NodeKind::Method),
        "call chains are not method declarations"
    );
    assert!(
        snapshot
            .edges
            .iter()
            .any(|edge| edge.kind == EdgeKind::Inherits && edge.target.as_str().contains("Base")),
        "python class bases produce inherits evidence"
    );
}

/// Dead-code review must answer "unreachable from any way in", not "nothing
/// imports it": the latter flags a package's own executables and its CI.
#[test]
fn dead_code_starts_from_declared_entry_points() {
    use blazingly_json::json;
    use weavatrix_rust::{Weavatrix, tools};

    let fixture = Fixture::new();
    fixture.write(
        "package.json",
        r#"{"name":"app","main":"src/index.js","bin":{"app":"bin/cli.js"}}"#,
    );
    fixture.write(
        "src/index.js",
        "import { serve } from './server.js';\nexport const boot = () => serve();\n",
    );
    fixture.write("src/server.js", "export function serve(){ return 1; }\n");
    fixture.write("bin/cli.js", "import '../src/index.js';\n");
    fixture.write(
        ".github/workflows/ci.yml",
        "name: CI\njobs:\n  build:\n    runs-on: ubuntu-latest\n",
    );
    fixture.write(
        "src/orphan.js",
        "export function forgotten(){ return 2; }\n",
    );

    let mut engine = Weavatrix::open(&fixture.root).unwrap();
    let report = tools::call(&mut engine, "find_dead_code", json!({"top_n": 50})).unwrap();
    let candidates = report["candidates"]
        .as_array()
        .map(|items| {
            items
                .iter()
                .filter_map(|item| item["node"]["id"].as_str().map(str::to_owned))
                .collect::<Vec<_>>()
        })
        .unwrap_or_default();

    for reachable in [
        "file:package.json",
        "file:bin/cli.js",
        "file:src/index.js",
        "file:src/server.js",
        "file:.github/workflows/ci.yml",
    ] {
        assert!(
            !candidates.iter().any(|id| id == reachable),
            "{reachable} must not be reported as dead, got {candidates:?}"
        );
    }
    assert!(
        candidates.iter().any(|id| id == "file:src/orphan.js"),
        "a genuinely unreachable module is still reported, got {candidates:?}"
    );
    assert!(
        report["entry_points"]
            .as_array()
            .is_some_and(|entries| entries.len() >= 2),
        "the entry points used are reported so the claim is auditable"
    );
}

#[test]
fn dead_code_excludes_inline_cfg_test_modules_in_product_files() {
    use blazingly_json::json;
    use weavatrix_rust::{Weavatrix, tools};

    let fixture = Fixture::new();
    fixture.write(
        "cargo-blazingly/src/main.rs",
        "fn main() {}\nfn genuinely_unused() {}\n\n#[cfg(test)]\nmod tests {\n    #[test]\n    fn embedded_test() {}\n\n    fn helper_for_test() {}\n}\n\n#[cfg(not(not(test)))]\nfn double_negated_test() {}\n",
    );

    let mut engine = Weavatrix::open(&fixture.root).unwrap();
    let candidates = |report: &blazingly_json::Value| {
        report["candidates"]
            .as_array()
            .into_iter()
            .flatten()
            .filter_map(|item| item["node"]["label"].as_str().map(str::to_owned))
            .collect::<Vec<_>>()
    };
    let production = tools::call(&mut engine, "find_dead_code", json!({"top_n": 50})).unwrap();
    let with_tests = tools::call(
        &mut engine,
        "find_dead_code",
        json!({"top_n": 50, "include_tests": true}),
    )
    .unwrap();

    let production = candidates(&production);
    let with_tests = candidates(&with_tests);
    assert!(
        production.iter().any(|label| label == "genuinely_unused"),
        "real production dead code must remain visible, got {production:?}"
    );
    for test_symbol in ["embedded_test", "helper_for_test", "double_negated_test"] {
        assert!(
            !production.iter().any(|label| label == test_symbol),
            "{test_symbol} inherits #[cfg(test)] and must not be a production candidate"
        );
        assert!(
            with_tests.iter().any(|label| label == test_symbol),
            "include_tests must reveal {test_symbol}, got {with_tests:?}"
        );
    }
}

/// Tools whose schema offers `include_tests` / `include_classified` must
/// actually apply them: an advertised parameter that is ignored is a schema
/// that lies about the answer.
#[test]
fn production_first_filters_are_applied_by_the_tools_that_advertise_them() {
    use blazingly_json::json;
    use weavatrix_rust::{Weavatrix, tools};

    let fixture = Fixture::new();
    fixture.write(
        "src/service.js",
        "export function serve(){ return 1; }\nrouter.get('/live', serve);\n",
    );
    fixture.write(
        "src/__test__/service.test.js",
        "import { serve } from '../service.js';\nrouter.get('/only-in-tests', serve);\nexport function check(){ return serve(); }\n",
    );
    let mut engine = Weavatrix::open(&fixture.root).unwrap();

    let labels = |value: &blazingly_json::Value, pointer: &str| {
        value
            .pointer(pointer)
            .and_then(|items| items.as_array())
            .map(|items| {
                items
                    .iter()
                    .filter_map(|item| {
                        item.get("node")
                            .unwrap_or(item)
                            .get("label")
                            .and_then(|label| label.as_str())
                            .map(str::to_owned)
                    })
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default()
    };

    for (tool, pointer) in [("god_nodes", "/hubs"), ("hot_path_review", "/candidates")] {
        let production = tools::call(&mut engine, tool, json!({"top_n": 50})).unwrap();
        let with_tests = tools::call(
            &mut engine,
            tool,
            json!({"top_n": 50, "include_tests": true}),
        )
        .unwrap();
        let production = labels(&production, pointer);
        let with_tests = labels(&with_tests, pointer);
        assert!(
            !production.iter().any(|label| label.contains("test")),
            "{tool} must not rank test evidence by default, got {production:?}"
        );
        assert!(
            with_tests.len() >= production.len(),
            "{tool} include_tests must widen the answer, got {with_tests:?}"
        );
    }

    let production = tools::call(&mut engine, "list_endpoints", json!({})).unwrap();
    let production = labels(&production, "/endpoints");
    assert!(
        production.iter().any(|label| label.contains("/live")),
        "a production route stays listed, got {production:?}"
    );
    assert!(
        !production
            .iter()
            .any(|label| label.contains("/only-in-tests")),
        "a route declared only in a test is not a production endpoint, got {production:?}"
    );
    let with_tests = tools::call(
        &mut engine,
        "list_endpoints",
        json!({"include_tests": true}),
    )
    .unwrap();
    assert!(
        labels(&with_tests, "/endpoints")
            .iter()
            .any(|label| label.contains("/only-in-tests")),
        "include_tests reveals the test-only route"
    );
}

#[test]
fn resolves_the_module_aliases_a_project_declares() {
    let fixture = Fixture::new();
    fixture.write(
        "tsconfig.json",
        r#"{
  // Comments and trailing commas are normal in this file.
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@app/*": ["src/app/*"],
      "@shared": ["src/shared/index.ts"],
    },
  },
}"#,
    );
    fixture.write(
        "package.json",
        r##"{"name":"root","workspaces":["packages/*"],"imports":{"#config/*":"./src/config/*"}}"##,
    );
    fixture.write("src/app/service.ts", "export function serve() {}\n");
    fixture.write("src/shared/index.ts", "export const shared = 1;\n");
    fixture.write("src/config/db.ts", "export const url = '';\n");
    fixture.write("src/base/root.ts", "export const root = 1;\n");
    fixture.write("packages/ui/package.json", r#"{"name":"@acme/ui"}"#);
    fixture.write("packages/ui/index.ts", "export const Button = 1;\n");
    fixture.write(
        "src/entry.ts",
        "import { serve } from '@app/service';\nimport { shared } from '@shared';\nimport { url } from '#config/db';\nimport { Button } from '@acme/ui';\nimport { root } from 'src/base/root';\nimport { missing } from './nowhere';\nexport const use = [serve, shared, url, Button, root, missing];\n",
    );

    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    let imports = |target: &str| {
        snapshot.edges.iter().any(|edge| {
            edge.kind == EdgeKind::Imports
                && edge.source.as_str() == "file:src/entry.ts"
                && edge.target.as_str() == target
        })
    };
    for (target, form) in [
        ("file:src/app/service.ts", "tsconfig paths wildcard"),
        ("file:src/shared/index.ts", "tsconfig paths exact mapping"),
        ("file:src/config/db.ts", "package.json subpath import"),
        ("file:packages/ui/index.ts", "workspace package name"),
        ("file:src/base/root.ts", "tsconfig baseUrl"),
    ] {
        assert!(imports(target), "{form} must resolve to {target}");
    }
    assert!(
        !snapshot
            .nodes
            .iter()
            .any(|node| node.id.as_str().starts_with("package:") && node.label.starts_with('@')),
        "an aliased local import must not be recorded as an external package"
    );
    let unresolved = snapshot
        .diagnostics
        .iter()
        .filter(|diagnostic| diagnostic.code == "import.unresolved")
        .collect::<Vec<_>>();
    assert_eq!(
        unresolved.len(),
        1,
        "the one genuinely missing target is reported, got {unresolved:?}"
    );
    assert!(
        unresolved[0].message.contains("./nowhere"),
        "the diagnostic names the specifier, got {:?}",
        unresolved[0].message
    );
}

#[test]
fn resolves_typescript_runtime_extensions_without_overriding_real_javascript() {
    let fixture = Fixture::new();
    fixture.write(
        "src/entry.ts",
        "import './plain.js';\nimport './component.jsx';\nimport './module.js';\nimport './common.jsx';\nimport './exact.js';\n",
    );
    fixture.write("src/plain.ts", "export const plain = true;\n");
    fixture.write("src/component.tsx", "export const component = true;\n");
    fixture.write("src/module.mts", "export const moduleValue = true;\n");
    fixture.write("src/common.cts", "export const common = true;\n");
    fixture.write("src/exact.js", "export const runtime = true;\n");
    fixture.write("src/exact.ts", "export const source = true;\n");

    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    let imports = snapshot
        .edges
        .iter()
        .filter(|edge| {
            edge.kind == EdgeKind::Imports && edge.source.as_str() == "file:src/entry.ts"
        })
        .map(|edge| edge.target.as_str())
        .collect::<Vec<_>>();

    for target in [
        "file:src/plain.ts",
        "file:src/component.tsx",
        "file:src/module.mts",
        "file:src/common.cts",
        "file:src/exact.js",
    ] {
        assert!(
            imports.contains(&target),
            "runtime specifier must resolve to {target}, got {imports:?}"
        );
    }
    assert!(
        !imports.contains(&"file:src/exact.ts"),
        "an exact JavaScript target must win over its TypeScript sibling"
    );
    assert!(
        snapshot
            .diagnostics
            .iter()
            .all(|diagnostic| diagnostic.code != "import.unresolved"),
        "all runtime specifiers should resolve, got {:?}",
        snapshot.diagnostics
    );
}

#[test]
fn resolves_imports_through_re_export_barrels() {
    let fixture = Fixture::new();
    fixture.write("src/shared/Button.tsx", "export function Button() {}\n");
    fixture.write("src/shared/Input.tsx", "export function Input() {}\n");
    fixture.write(
        "src/shared/index.ts",
        "export { Button } from './Button';\nexport * from './Input';\n",
    );
    fixture.write(
        "src/app/App.tsx",
        "import { Button, Input } from '../shared';\nexport function App() { return Button(); }\n",
    );
    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    let edge = |kind: EdgeKind, source: &str, target: &str| {
        snapshot.edges.iter().any(|item| {
            item.kind == kind && item.source.as_str() == source && item.target.as_str() == target
        })
    };
    assert!(
        edge(
            EdgeKind::ReExports,
            "file:src/shared/index.ts",
            "file:src/shared/Button.tsx"
        ),
        "named re-export is recorded as re-export evidence"
    );
    assert!(
        edge(
            EdgeKind::ReExports,
            "file:src/shared/index.ts",
            "file:src/shared/Input.tsx"
        ),
        "star re-export is recorded as re-export evidence"
    );
    assert!(
        edge(
            EdgeKind::Imports,
            "file:src/app/App.tsx",
            "file:src/shared/index.ts"
        ),
        "the barrel itself stays an import target"
    );
    for defining in ["file:src/shared/Button.tsx", "file:src/shared/Input.tsx"] {
        assert!(
            edge(EdgeKind::Imports, "file:src/app/App.tsx", defining),
            "barrel import must reach {defining} through the re-export chain"
        );
    }
}

#[test]
fn resolves_express_mount_chains_to_full_paths() {
    let fixture = Fixture::new();
    fixture.write(
        "services/users/router.js",
        "const express = require('express');\nconst router = express.Router();\nrouter.get('/', list);\nrouter.get('/:id', read);\nmodule.exports = router;\n",
    );
    fixture.write(
        "services/api.js",
        "const usersRouter = require('./users/router');\nconst api = require('express').Router();\napi.use('/users', usersRouter);\nmodule.exports = api;\n",
    );
    fixture.write(
        "app.js",
        "const api = require('./services/api');\napp.use('/api', api);\n",
    );
    let snapshot = Analyzer::default().analyze(&fixture.root).unwrap();
    for label in ["GET /api/users", "GET /api/users/:id"] {
        assert!(
            snapshot
                .nodes
                .iter()
                .any(|node| node.kind == NodeKind::Endpoint && node.label == label),
            "missing mounted endpoint {label}"
        );
    }
    assert!(
        snapshot
            .nodes
            .iter()
            .any(|node| node.kind == NodeKind::Endpoint && node.label == "GET /"),
        "locally declared endpoint evidence is preserved"
    );
}

struct Fixture {
    root: PathBuf,
}

impl Fixture {
    fn new() -> Self {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let root = std::env::temp_dir().join(format!(
            "weavatrix-languages-{}-{nonce}",
            std::process::id()
        ));
        fs::create_dir_all(&root).unwrap();
        Self { root }
    }

    fn write(&self, relative: &str, contents: &str) {
        let path = self.root.join(relative);
        fs::create_dir_all(path.parent().unwrap_or(Path::new("."))).unwrap();
        fs::write(path, contents).unwrap();
    }
}

impl Drop for Fixture {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.root);
    }
}