sqry-core 6.0.21

Core library for sqry - semantic code search engine
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
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
//! Pass 5: Cross-Language Linking — Connect placeholder nodes across files and languages.
//!
//! This module implements a post-build pass that links placeholder nodes (created by
//! per-file graph builders) to their real implementations across files and languages.
//!
//! # Overview
//!
//! During per-file graph building (Passes 1-4), language plugins create placeholder
//! nodes for cross-language references:
//! - **FFI**: Rust `extern "C" { fn puts(); }` creates a stub `extern::C::puts` node
//! - **HTTP**: JS `fetch("/api/users")` creates an `HttpRequest` edge to a stub module
//!
//! Pass 5 runs after all files are processed and connects these placeholders to the
//! actual target implementations:
//! - **FFI**: `extern::C::puts` → C function `puts` in another file
//! - **HTTP**: `fetch("/api/users")` source → `route::GET::/api/users` Endpoint node
//!
//! # Algorithm
//!
//! The pass operates in two phases:
//!
//! ## FFI Linking
//! 1. Scan all Function nodes for FFI declaration patterns (`extern::*`, `C.*`, etc.)
//! 2. Build a lookup map of C/C++ function names
//! 3. Match FFI declarations to C/C++ implementations
//! 4. Create `FfiCall` edges from declarations to implementations
//!
//! ## HTTP Linking
//! 1. Collect all `Endpoint` nodes (created by plugin route detection)
//! 2. Scan all `HttpRequest` edges
//! 3. Match requests to endpoints by HTTP method and normalized URL path
//! 4. Create new `HttpRequest` edges from request sources to matched endpoints

use std::collections::HashMap;

use crate::graph::node::Language;
use crate::graph::unified::concurrent::CodeGraph;
use crate::graph::unified::edge::EdgeKind;
use crate::graph::unified::edge::kind::{FfiConvention, HttpMethod};
use crate::graph::unified::file::FileId;
use crate::graph::unified::node::{NodeId, NodeKind};
use crate::graph::unified::storage::NodeEntry;

/// Statistics from Pass 5 execution.
#[derive(Debug, Clone, Default)]
pub struct Pass5Stats {
    /// Number of FFI declaration nodes scanned.
    pub ffi_declarations_scanned: usize,
    /// Number of FFI edges created (linking declarations to implementations).
    pub ffi_edges_created: usize,
    /// Number of HTTP request edges scanned.
    pub http_requests_scanned: usize,
    /// Number of HTTP request-to-endpoint matches found.
    pub http_endpoints_matched: usize,
    /// Total cross-language edges created (FFI + HTTP).
    pub total_edges_created: usize,
}

/// A pending cross-language edge to be added to the graph.
#[derive(Debug, Clone)]
struct PendingCrossLanguageEdge {
    /// Source node of the edge.
    source: NodeId,
    /// Target node of the edge.
    target: NodeId,
    /// Edge kind (`FfiCall` or `HttpRequest`).
    kind: EdgeKind,
    /// File containing the source node.
    file: FileId,
}

/// Run Pass 5: link cross-language edges in the completed graph.
///
/// This function operates on the fully-built `CodeGraph` after all files have been
/// processed through Passes 1-4. It scans for placeholder nodes and edges, then
/// creates new edges linking them to real implementations across files and languages.
///
/// # Arguments
///
/// * `graph` - Mutable reference to the completed code graph
///
/// # Returns
///
/// Statistics about the linking operation.
pub fn link_cross_language_edges(graph: &mut CodeGraph) -> Pass5Stats {
    let mut stats = Pass5Stats::default();
    let mut pending_edges: Vec<PendingCrossLanguageEdge> = Vec::new();

    // Phase 1: FFI linking
    link_ffi_edges(graph, &mut stats, &mut pending_edges);

    // Phase 2: HTTP linking
    link_http_edges(graph, &mut stats, &mut pending_edges);

    // Apply all pending edges to the graph
    for edge in &pending_edges {
        graph.edges_mut().add_edge_with_spans(
            edge.source,
            edge.target,
            edge.kind.clone(),
            edge.file,
            vec![],
        );
    }

    stats.total_edges_created = stats.ffi_edges_created + stats.http_endpoints_matched;

    if stats.total_edges_created > 0 {
        log::info!(
            "Pass 5: created {} cross-language edges ({} FFI, {} HTTP)",
            stats.total_edges_created,
            stats.ffi_edges_created,
            stats.http_endpoints_matched,
        );
    }

    stats
}

// ============================================================================
// FFI Linking
// ============================================================================

/// Collected FFI declaration from the graph.
#[derive(Debug)]
struct FfiDeclaration {
    /// The node ID of the FFI declaration.
    node_id: NodeId,
    /// The bare function name to match against (for example, `puts` from `extern::C::puts`).
    bare_name: String,
    /// The FFI calling convention.
    convention: FfiConvention,
    /// The file containing this declaration.
    file_id: FileId,
}

/// Extract the bare function name and convention from an FFI qualified name.
///
/// Supported patterns:
/// - `extern::C::puts` → ("puts", C)
/// - `extern::stdcall::func` → ("func", Stdcall)
/// - `extern::system::func` → ("func", System)
/// - `C.puts` → ("puts", C)
/// - `native::jni::func` → ("func", C)
/// - `native::ctypes::func` → ("func", C)
/// - `native::cffi::func` → ("func", C)
/// - `native::ffi::func` → ("func", C)
/// - `native::panama::func` → ("func", C)
fn parse_ffi_qualified_name(qualified_name: &str) -> Option<(String, FfiConvention)> {
    if let Some(rest) = qualified_name.strip_prefix("extern::") {
        // Rust-style: extern::C::puts, extern::stdcall::func
        if let Some(pos) = rest.find("::") {
            let convention_str = &rest[..pos];
            let bare_name = &rest[pos + 2..];
            if bare_name.is_empty() {
                return None;
            }
            let convention = match convention_str {
                "cdecl" => FfiConvention::Cdecl,
                "stdcall" => FfiConvention::Stdcall,
                "fastcall" => FfiConvention::Fastcall,
                "system" => FfiConvention::System,
                _ => FfiConvention::C,
            };
            return Some((bare_name.to_string(), convention));
        }
    } else if let Some(rest) = qualified_name
        .strip_prefix("C.")
        .or_else(|| qualified_name.strip_prefix("C::"))
    {
        // Go CGo style: C.puts or C::puts
        if !rest.is_empty() {
            return Some((rest.to_string(), FfiConvention::C));
        }
    } else if let Some(rest) = qualified_name.strip_prefix("native::jni::") {
        // Java JNI style
        if !rest.is_empty() {
            return Some((rest.to_string(), FfiConvention::C));
        }
    } else if let Some(rest) = qualified_name
        .strip_prefix("native::ctypes::")
        .or_else(|| qualified_name.strip_prefix("native::cffi::"))
        .or_else(|| qualified_name.strip_prefix("native::ffi::"))
        .or_else(|| qualified_name.strip_prefix("native::panama::"))
    {
        // Python ctypes/cffi, PHP FFI, Java Panama style
        if !rest.is_empty() {
            return Some((rest.to_string(), FfiConvention::C));
        }
    }

    None
}

/// Convert a Java fully-qualified method name to JNI C function name.
///
/// Example: `com.example.Class.method` → `Java_com_example_Class_method`
///
/// JNI convention: replace dots with underscores and prepend `Java_`.
fn java_to_jni_c_name(java_name: &str) -> String {
    let joined_segments = java_name
        .split("::")
        .flat_map(|segment| segment.split('.'))
        .filter(|segment| !segment.is_empty())
        .collect::<Vec<_>>()
        .join("_");
    format!("Java_{joined_segments}")
}

/// Return whether a Java name has package/class qualification segments.
fn is_java_qualified_name(java_name: &str) -> bool {
    java_name.contains('.') || java_name.contains("::")
}

/// Return the last segment from a Java qualified method name.
fn java_method_name(java_name: &str) -> Option<&str> {
    java_name
        .rsplit("::")
        .next()
        .and_then(|segment| segment.rsplit('.').next())
        .filter(|segment| !segment.is_empty())
}

/// Collect FFI declarations from the graph and match them to C/C++ implementations.
fn link_ffi_edges(
    graph: &CodeGraph,
    stats: &mut Pass5Stats,
    pending: &mut Vec<PendingCrossLanguageEdge>,
) {
    // Step 1: Collect all FFI declarations
    let ffi_declarations = collect_ffi_declarations(graph, stats);
    if ffi_declarations.is_empty() {
        return;
    }

    // Step 2: Build C/C++ function lookup map
    let c_functions = build_c_function_map(graph);
    if c_functions.is_empty() {
        return;
    }

    // Step 3: Match FFI declarations to C/C++ implementations
    for decl in &ffi_declarations {
        // Try direct name match first
        let mut matched = try_ffi_match(decl, &c_functions, stats, pending);

        // For JNI declarations, try JNI-mangled name as fallback
        if !matched && is_java_qualified_name(&decl.bare_name) {
            let jni_name = java_to_jni_c_name(&decl.bare_name);
            let jni_decl = FfiDeclaration {
                node_id: decl.node_id,
                bare_name: jni_name,
                convention: decl.convention,
                file_id: decl.file_id,
            };
            matched = try_ffi_match(&jni_decl, &c_functions, stats, pending);
        }

        // For JNI declarations, also try bare method name (last segment after final dot)
        if !matched && let Some(method_name) = java_method_name(&decl.bare_name) {
            let bare_decl = FfiDeclaration {
                node_id: decl.node_id,
                bare_name: method_name.to_string(),
                convention: decl.convention,
                file_id: decl.file_id,
            };
            try_ffi_match(&bare_decl, &c_functions, stats, pending);
        }
    }
}

/// Try to match an FFI declaration against the C function map.
///
/// Returns `true` if at least one match was found.
fn try_ffi_match(
    decl: &FfiDeclaration,
    c_functions: &HashMap<String, Vec<(NodeId, FileId)>>,
    stats: &mut Pass5Stats,
    pending: &mut Vec<PendingCrossLanguageEdge>,
) -> bool {
    let Some(targets) = c_functions.get(&decl.bare_name) else {
        return false;
    };
    let mut found = false;
    for &(target_node, target_file) in targets {
        // Skip same-file matches (these are already handled by per-file passes)
        if target_file == decl.file_id {
            continue;
        }
        found = true;
        stats.ffi_edges_created += 1;
        pending.push(PendingCrossLanguageEdge {
            source: decl.node_id,
            target: target_node,
            kind: EdgeKind::FfiCall {
                convention: decl.convention,
            },
            file: decl.file_id,
        });
    }
    found
}

/// Resolve the graph-structural identity for a node.
///
/// Cross-language linking operates on canonical graph names when available,
/// because semantic display names may intentionally collapse qualified markers
/// such as `extern::C::` or `native::jni::`.
fn entry_structural_name(graph: &CodeGraph, entry: &NodeEntry) -> Option<std::sync::Arc<str>> {
    entry
        .qualified_name
        .and_then(|qualified_name_id| graph.strings().resolve(qualified_name_id))
        .or_else(|| graph.strings().resolve(entry.name))
}

/// Collect FFI declaration nodes from the graph.
fn collect_ffi_declarations(graph: &CodeGraph, stats: &mut Pass5Stats) -> Vec<FfiDeclaration> {
    let mut declarations = Vec::new();
    let function_nodes = graph.indices().by_kind(NodeKind::Function);

    for &node_id in function_nodes {
        let Some(entry) = graph.nodes().get(node_id) else {
            continue;
        };

        let Some(name_str) = entry_structural_name(graph, entry) else {
            continue;
        };

        if let Some((bare_name, convention)) = parse_ffi_qualified_name(&name_str) {
            stats.ffi_declarations_scanned += 1;
            declarations.push(FfiDeclaration {
                node_id,
                bare_name,
                convention,
                file_id: entry.file,
            });
        }
    }

    declarations
}

/// Build a lookup map of C/C++ function names to their node IDs and file IDs.
fn build_c_function_map(graph: &CodeGraph) -> HashMap<String, Vec<(NodeId, FileId)>> {
    let mut map: HashMap<String, Vec<(NodeId, FileId)>> = HashMap::new();

    for lang in &[Language::C, Language::Cpp] {
        let files = graph.files().files_by_language(*lang);
        for (file_id, _path) in files {
            let file_nodes = graph.indices().by_file(file_id);
            for &node_id in file_nodes {
                let Some(entry) = graph.nodes().get(node_id) else {
                    continue;
                };
                if entry.kind != NodeKind::Function {
                    continue;
                }
                let Some(name_str) = entry_structural_name(graph, entry) else {
                    continue;
                };
                // Use the bare name (last segment after :: or the whole name)
                let bare_name = name_str
                    .rsplit("::")
                    .next()
                    .unwrap_or(&name_str)
                    .to_string();
                map.entry(bare_name).or_default().push((node_id, file_id));
            }
        }
    }

    map
}

// ============================================================================
// HTTP Linking
// ============================================================================

/// Parsed endpoint information from a route node.
#[derive(Debug)]
struct EndpointInfo {
    /// The node ID of the endpoint.
    node_id: NodeId,
    /// The HTTP method (GET, POST, etc.).
    method: HttpMethod,
    /// The normalized URL path.
    normalized_path: String,
    /// The file containing this endpoint.
    file_id: FileId,
}

/// Collected HTTP request edge information.
#[derive(Debug)]
struct HttpRequestInfo {
    /// The source node making the request.
    source_node: NodeId,
    /// The HTTP method.
    method: HttpMethod,
    /// The URL path (raw, before normalization).
    url_path: String,
    /// The file containing the request.
    file_id: FileId,
}

/// Parse a route endpoint qualified name into method and path.
///
/// Expected format: `route::GET::/api/users` or `route::POST::/api/items`
fn parse_endpoint_qualified_name(qualified_name: &str) -> Option<(HttpMethod, String)> {
    let rest = qualified_name.strip_prefix("route::")?;
    let sep = rest.find("::")?;
    let method_str = &rest[..sep];
    let path = &rest[sep + 2..];

    let method = match method_str {
        "GET" => HttpMethod::Get,
        "POST" => HttpMethod::Post,
        "PUT" => HttpMethod::Put,
        "DELETE" => HttpMethod::Delete,
        "PATCH" => HttpMethod::Patch,
        "HEAD" => HttpMethod::Head,
        "OPTIONS" => HttpMethod::Options,
        "ALL" => HttpMethod::All,
        _ => return None,
    };

    Some((method, path.to_string()))
}

/// Normalize a URL path for matching.
///
/// - Strips leading and trailing slashes
/// - Collapses double slashes
/// - Normalizes parameter syntax: `{id}`, `<id>`, `[id]` → `:id`
/// - Strips query strings
#[must_use]
pub fn normalize_url_path(path: &str) -> String {
    // Strip query string
    let path = path.split('?').next().unwrap_or(path);

    // Strip leading/trailing slashes
    let path = path.trim_matches('/');

    // Split into segments, filter empty (collapses double slashes)
    let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();

    // Normalize parameter syntax in each segment
    let normalized: Vec<String> = segments
        .into_iter()
        .map(|seg| {
            // {id} → :id
            if seg.starts_with('{') && seg.ends_with('}') {
                return format!(":{}", &seg[1..seg.len() - 1]);
            }
            // <id> or <int:id> → :id
            if seg.starts_with('<') && seg.ends_with('>') {
                let inner = &seg[1..seg.len() - 1];
                // Handle typed params like <int:id>, <path:subpath>
                let param_name = if let Some(pos) = inner.find(':') {
                    &inner[pos + 1..]
                } else {
                    inner
                };
                return format!(":{param_name}");
            }
            // [id] → :id
            if seg.starts_with('[') && seg.ends_with(']') {
                return format!(":{}", &seg[1..seg.len() - 1]);
            }
            // Already :id format — keep as-is
            seg.to_string()
        })
        .collect();

    normalized.join("/")
}

/// Collect endpoints and HTTP requests, then match them.
fn link_http_edges(
    graph: &CodeGraph,
    stats: &mut Pass5Stats,
    pending: &mut Vec<PendingCrossLanguageEdge>,
) {
    // Step 1: Collect all endpoints
    let endpoints = collect_endpoints(graph);
    if endpoints.is_empty() {
        return;
    }

    // Build endpoint lookup: (method, normalized_path) → Vec<(NodeId, FileId)>
    let mut endpoint_map: HashMap<(HttpMethod, String), Vec<(NodeId, FileId)>> = HashMap::new();
    for ep in &endpoints {
        endpoint_map
            .entry((ep.method, ep.normalized_path.clone()))
            .or_default()
            .push((ep.node_id, ep.file_id));
    }

    // Step 2: Collect all HTTP request edges
    let requests = collect_http_requests(graph, stats);
    if requests.is_empty() {
        return;
    }

    // Step 3: Match requests to endpoints
    for req in &requests {
        let normalized = normalize_url_path(&req.url_path);

        // Exact match: same method and path
        try_http_match(req, &normalized, req.method, &endpoint_map, stats, pending);

        // Wildcard matching: if the request uses All, also check specific-method endpoints
        if req.method == HttpMethod::All {
            for specific_method in &[
                HttpMethod::Get,
                HttpMethod::Post,
                HttpMethod::Put,
                HttpMethod::Delete,
                HttpMethod::Patch,
                HttpMethod::Head,
                HttpMethod::Options,
            ] {
                try_http_match(
                    req,
                    &normalized,
                    *specific_method,
                    &endpoint_map,
                    stats,
                    pending,
                );
            }
        } else {
            // If request is a specific method, also check for All endpoints
            try_http_match(
                req,
                &normalized,
                HttpMethod::All,
                &endpoint_map,
                stats,
                pending,
            );
        }
    }
}

/// Try to match an HTTP request against endpoints with a given method and path.
fn try_http_match(
    req: &HttpRequestInfo,
    normalized_path: &str,
    lookup_method: HttpMethod,
    endpoint_map: &HashMap<(HttpMethod, String), Vec<(NodeId, FileId)>>,
    stats: &mut Pass5Stats,
    pending: &mut Vec<PendingCrossLanguageEdge>,
) {
    let key = (lookup_method, normalized_path.to_string());
    if let Some(targets) = endpoint_map.get(&key) {
        for &(target_node, target_file) in targets {
            // Skip same-file matches
            if target_file == req.file_id {
                continue;
            }
            stats.http_endpoints_matched += 1;
            pending.push(PendingCrossLanguageEdge {
                source: req.source_node,
                target: target_node,
                kind: EdgeKind::HttpRequest {
                    method: req.method,
                    url: None, // Cross-file edges don't carry the URL StringId
                },
                file: req.file_id,
            });
        }
    }
}

/// Collect all Endpoint nodes from the graph.
fn collect_endpoints(graph: &CodeGraph) -> Vec<EndpointInfo> {
    let mut endpoints = Vec::new();
    let endpoint_nodes = graph.indices().by_kind(NodeKind::Endpoint);

    for &node_id in endpoint_nodes {
        let Some(entry) = graph.nodes().get(node_id) else {
            continue;
        };
        let Some(name_str) = entry_structural_name(graph, entry) else {
            continue;
        };

        if let Some((method, path)) = parse_endpoint_qualified_name(&name_str) {
            let normalized = normalize_url_path(&path);
            endpoints.push(EndpointInfo {
                node_id,
                method,
                normalized_path: normalized,
                file_id: entry.file,
            });
        }
    }

    endpoints
}

/// Collect all HTTP request edges from the graph.
///
/// Scans the edge delta buffer directly (O(E)) instead of iterating all nodes
/// and calling `edges_from()` on each (which would be O(N * E) due to per-node
/// delta scans — catastrophic on large graphs).
fn collect_http_requests(graph: &CodeGraph, stats: &mut Pass5Stats) -> Vec<HttpRequestInfo> {
    let mut requests = Vec::new();

    let forward = graph.edges().forward();
    let delta = forward.delta();

    for edge in delta.iter() {
        if !edge.is_add() {
            continue;
        }
        if let EdgeKind::HttpRequest { method, url } = &edge.kind {
            stats.http_requests_scanned += 1;
            if let Some(url_id) = url
                && let Some(url_str) = graph.strings().resolve(*url_id)
            {
                let Some(source_entry) = graph.nodes().get(edge.source) else {
                    continue;
                };
                requests.push(HttpRequestInfo {
                    source_node: edge.source,
                    method: *method,
                    url_path: url_str.to_string(),
                    file_id: source_entry.file,
                });
            }
        }
    }

    requests
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    // ---- URL normalization tests ----

    #[test]
    fn test_normalize_url_path_basic() {
        assert_eq!(normalize_url_path("/api/users"), "api/users");
        assert_eq!(normalize_url_path("/api/users/"), "api/users");
        assert_eq!(normalize_url_path("api/users"), "api/users");
    }

    #[test]
    fn test_normalize_url_path_params() {
        assert_eq!(normalize_url_path("/api/users/{id}"), "api/users/:id");
        assert_eq!(normalize_url_path("/api/users/<id>"), "api/users/:id");
        assert_eq!(normalize_url_path("/api/users/[id]"), "api/users/:id");
        assert_eq!(normalize_url_path("/api/users/:id"), "api/users/:id");
    }

    #[test]
    fn test_normalize_url_path_double_slashes() {
        assert_eq!(normalize_url_path("/api//users"), "api/users");
        assert_eq!(normalize_url_path("///api///users///"), "api/users");
    }

    #[test]
    fn test_normalize_url_path_query_string() {
        assert_eq!(
            normalize_url_path("/api/users?page=1&limit=10"),
            "api/users"
        );
    }

    #[test]
    fn test_normalize_url_path_empty() {
        assert_eq!(normalize_url_path("/"), "");
        assert_eq!(normalize_url_path(""), "");
    }

    // ---- FFI qualified name parsing tests ----

    #[test]
    fn test_parse_ffi_rust_extern_c() {
        let result = parse_ffi_qualified_name("extern::C::puts");
        assert_eq!(result, Some(("puts".to_string(), FfiConvention::C)));
    }

    #[test]
    fn test_parse_ffi_rust_extern_stdcall() {
        let result = parse_ffi_qualified_name("extern::stdcall::MessageBoxA");
        assert_eq!(
            result,
            Some(("MessageBoxA".to_string(), FfiConvention::Stdcall))
        );
    }

    #[test]
    fn test_parse_ffi_go_cgo() {
        let result = parse_ffi_qualified_name("C.puts");
        assert_eq!(result, Some(("puts".to_string(), FfiConvention::C)));
    }

    #[test]
    fn test_parse_ffi_java_jni() {
        let result = parse_ffi_qualified_name("native::jni::Java_MyClass_doStuff");
        assert_eq!(
            result,
            Some(("Java_MyClass_doStuff".to_string(), FfiConvention::C))
        );
    }

    #[test]
    fn test_parse_ffi_python_ctypes() {
        let result = parse_ffi_qualified_name("native::ctypes::calculate");
        assert_eq!(result, Some(("calculate".to_string(), FfiConvention::C)));
    }

    #[test]
    fn test_parse_ffi_python_cffi() {
        let result = parse_ffi_qualified_name("native::cffi::calculate");
        assert_eq!(result, Some(("calculate".to_string(), FfiConvention::C)));
    }

    #[test]
    fn test_parse_ffi_php_ffi() {
        let result = parse_ffi_qualified_name("native::ffi::crypto_encrypt");
        assert_eq!(
            result,
            Some(("crypto_encrypt".to_string(), FfiConvention::C))
        );
    }

    #[test]
    fn test_parse_ffi_java_panama() {
        let result = parse_ffi_qualified_name("native::panama::nativeLinker");
        assert_eq!(result, Some(("nativeLinker".to_string(), FfiConvention::C)));
    }

    #[test]
    fn test_parse_ffi_not_ffi() {
        assert!(parse_ffi_qualified_name("main").is_none());
        assert!(parse_ffi_qualified_name("module::func").is_none());
        assert!(parse_ffi_qualified_name("").is_none());
    }

    #[test]
    fn test_parse_ffi_edge_cases() {
        // Empty bare name
        assert!(parse_ffi_qualified_name("extern::C::").is_none());
        assert!(parse_ffi_qualified_name("C.").is_none());
    }

    // ---- Endpoint qualified name parsing tests ----

    #[test]
    fn test_parse_endpoint_get() {
        let result = parse_endpoint_qualified_name("route::GET::/api/users");
        assert_eq!(result, Some((HttpMethod::Get, "/api/users".to_string())));
    }

    #[test]
    fn test_parse_endpoint_post() {
        let result = parse_endpoint_qualified_name("route::POST::/api/items");
        assert_eq!(result, Some((HttpMethod::Post, "/api/items".to_string())));
    }

    #[test]
    fn test_parse_endpoint_all_methods() {
        assert!(matches!(
            parse_endpoint_qualified_name("route::PUT::/x"),
            Some((HttpMethod::Put, _))
        ));
        assert!(matches!(
            parse_endpoint_qualified_name("route::DELETE::/x"),
            Some((HttpMethod::Delete, _))
        ));
        assert!(matches!(
            parse_endpoint_qualified_name("route::PATCH::/x"),
            Some((HttpMethod::Patch, _))
        ));
        assert!(matches!(
            parse_endpoint_qualified_name("route::HEAD::/x"),
            Some((HttpMethod::Head, _))
        ));
        assert!(matches!(
            parse_endpoint_qualified_name("route::OPTIONS::/x"),
            Some((HttpMethod::Options, _))
        ));
    }

    #[test]
    fn test_parse_endpoint_invalid() {
        assert!(parse_endpoint_qualified_name("not_a_route").is_none());
        assert!(parse_endpoint_qualified_name("route::INVALID::/x").is_none());
    }

    // ---- Graph-level unit tests ----

    #[test]
    fn test_link_cross_language_edges_empty_graph() {
        let mut graph = CodeGraph::new();
        let stats = link_cross_language_edges(&mut graph);
        assert_eq!(stats.total_edges_created, 0);
        assert_eq!(stats.ffi_declarations_scanned, 0);
        assert_eq!(stats.http_requests_scanned, 0);
    }

    #[test]
    fn test_ffi_matching_with_real_graph() {
        use crate::graph::unified::build::helper::GraphBuildHelper;
        use crate::graph::unified::build::staging::StagingGraph;
        use std::path::PathBuf;

        let mut graph = CodeGraph::new();

        // File 1: Rust file with extern declaration
        let rust_path = PathBuf::from("/test/bindings.rs");
        {
            let mut staging = StagingGraph::new();
            let mut helper = GraphBuildHelper::new(&mut staging, &rust_path, Language::Rust);
            let _extern_fn = helper.add_function("extern::C::calculate_sum", None, false, false);
            commit_staging_to_graph(&mut graph, &rust_path, Language::Rust, staging);
        }

        // File 2: C file with implementation
        let c_path = PathBuf::from("/test/math.c");
        {
            let mut staging = StagingGraph::new();
            let mut helper = GraphBuildHelper::new(&mut staging, &c_path, Language::C);
            let _c_fn = helper.add_function("calculate_sum", None, false, false);
            commit_staging_to_graph(&mut graph, &c_path, Language::C, staging);
        }

        // Run Pass 5
        let stats = link_cross_language_edges(&mut graph);

        assert_eq!(stats.ffi_declarations_scanned, 1);
        assert_eq!(stats.ffi_edges_created, 1);
        assert_eq!(stats.total_edges_created, 1);
    }

    #[test]
    fn test_http_matching_with_real_graph() {
        use crate::graph::unified::build::helper::GraphBuildHelper;
        use crate::graph::unified::build::staging::StagingGraph;
        use crate::graph::unified::edge::kind::HttpMethod;
        use std::path::PathBuf;

        let mut graph = CodeGraph::new();

        // File 1: Python server with endpoint
        let server_path = PathBuf::from("/test/server.py");
        {
            let mut staging = StagingGraph::new();
            let mut helper = GraphBuildHelper::new(&mut staging, &server_path, Language::Python);
            let _endpoint = helper.add_endpoint("route::GET::/api/users", None);
            commit_staging_to_graph(&mut graph, &server_path, Language::Python, staging);
        }

        // File 2: JavaScript client with HTTP request
        let client_path = PathBuf::from("/test/client.js");
        {
            let mut staging = StagingGraph::new();
            let mut helper =
                GraphBuildHelper::new(&mut staging, &client_path, Language::JavaScript);
            let fetch_fn = helper.add_function("fetchUsers", None, true, false);
            let target = helper.add_module("http::/api/users", None);
            helper.add_http_request_edge(fetch_fn, target, HttpMethod::Get, Some("/api/users"));
            commit_staging_to_graph(&mut graph, &client_path, Language::JavaScript, staging);
        }

        // Run Pass 5
        let stats = link_cross_language_edges(&mut graph);

        assert_eq!(stats.http_requests_scanned, 1);
        assert_eq!(stats.http_endpoints_matched, 1);
        assert_eq!(stats.total_edges_created, 1);
    }

    #[test]
    fn test_ffi_no_match_same_file() {
        use crate::graph::unified::build::helper::GraphBuildHelper;
        use crate::graph::unified::build::staging::StagingGraph;
        use std::path::PathBuf;

        let mut graph = CodeGraph::new();

        // Same file has both extern and implementation — should NOT create cross-file edge
        let path = PathBuf::from("/test/combined.c");
        {
            let mut staging = StagingGraph::new();
            let mut helper = GraphBuildHelper::new(&mut staging, &path, Language::C);
            let _extern_fn = helper.add_function("extern::C::my_func", None, false, false);
            let _impl_fn = helper.add_function("my_func", None, false, false);
            commit_staging_to_graph(&mut graph, &path, Language::C, staging);
        }

        let stats = link_cross_language_edges(&mut graph);
        assert_eq!(stats.ffi_edges_created, 0);
    }

    /// Helper to commit a staging graph to the main CodeGraph.
    ///
    /// This mirrors the logic in `entrypoint.rs::process_file` but simplified for tests.
    fn commit_staging_to_graph(
        graph: &mut CodeGraph,
        path: &std::path::Path,
        language: Language,
        mut staging: crate::graph::unified::build::staging::StagingGraph,
    ) {
        let file_id = graph
            .files_mut()
            .register_with_language(path, Some(language))
            .expect("register file");

        staging.apply_file_id(file_id);

        let string_remap = staging
            .commit_strings(graph.strings_mut())
            .expect("commit strings");
        staging
            .apply_string_remap(&string_remap)
            .expect("apply string remap");

        let node_id_mapping = staging
            .commit_nodes(graph.nodes_mut())
            .expect("commit nodes");

        // Update indices
        let index_entries: Vec<_> = node_id_mapping
            .values()
            .filter_map(|&actual_id| {
                graph.nodes().get(actual_id).map(|entry| {
                    (
                        actual_id,
                        entry.kind,
                        entry.name,
                        entry.qualified_name,
                        entry.file,
                    )
                })
            })
            .collect();
        for (node_id, kind, name, qualified_name, file) in index_entries {
            graph
                .indices_mut()
                .add(node_id, kind, name, qualified_name, file);
        }

        // Commit edges
        let edges = staging.get_remapped_edges(&node_id_mapping);
        for edge in edges {
            graph.edges_mut().add_edge_with_spans(
                edge.source,
                edge.target,
                edge.kind.clone(),
                file_id,
                edge.spans.clone(),
            );
        }
    }

    // ---- F1: Go CGo C:: prefix tests ----

    #[test]
    fn test_parse_ffi_go_cgo_double_colon() {
        let result = parse_ffi_qualified_name("C::puts");
        assert_eq!(result, Some(("puts".to_string(), FfiConvention::C)));
    }

    #[test]
    fn test_parse_ffi_go_cgo_double_colon_empty() {
        assert!(parse_ffi_qualified_name("C::").is_none());
    }

    // ---- F2: JNI name demangling tests ----

    #[test]
    fn test_java_to_jni_c_name() {
        assert_eq!(
            java_to_jni_c_name("com.example.Class.method"),
            "Java_com_example_Class_method"
        );
        assert_eq!(
            java_to_jni_c_name("MyClass.doStuff"),
            "Java_MyClass_doStuff"
        );
        assert_eq!(
            java_to_jni_c_name("com::example::Class::method"),
            "Java_com_example_Class_method"
        );
    }

    #[test]
    fn test_ffi_jni_demangling_with_real_graph() {
        use crate::graph::unified::build::helper::GraphBuildHelper;
        use crate::graph::unified::build::staging::StagingGraph;
        use std::path::PathBuf;

        let mut graph = CodeGraph::new();

        // File 1: Java JNI declaration with fully-qualified name
        let java_path = PathBuf::from("/test/MyClass.java");
        {
            let mut staging = StagingGraph::new();
            let mut helper = GraphBuildHelper::new(&mut staging, &java_path, Language::Java);
            let _jni_fn = helper.add_function(
                "native::jni::com.example.MyClass.doStuff",
                None,
                false,
                false,
            );
            commit_staging_to_graph(&mut graph, &java_path, Language::Java, staging);
        }

        // File 2: C file with JNI-mangled implementation
        let c_path = PathBuf::from("/test/jni_impl.c");
        {
            let mut staging = StagingGraph::new();
            let mut helper = GraphBuildHelper::new(&mut staging, &c_path, Language::C);
            let _c_fn = helper.add_function("Java_com_example_MyClass_doStuff", None, false, false);
            commit_staging_to_graph(&mut graph, &c_path, Language::C, staging);
        }

        let stats = link_cross_language_edges(&mut graph);
        assert_eq!(stats.ffi_declarations_scanned, 1);
        assert_eq!(stats.ffi_edges_created, 1);
    }

    // ---- F4: ALL endpoint parsing tests ----

    #[test]
    fn test_parse_endpoint_all_method() {
        let result = parse_endpoint_qualified_name("route::ALL::/health");
        assert_eq!(result, Some((HttpMethod::All, "/health".to_string())));
    }

    #[test]
    fn test_http_matching_all_method_endpoint() {
        use crate::graph::unified::build::helper::GraphBuildHelper;
        use crate::graph::unified::build::staging::StagingGraph;
        use crate::graph::unified::edge::kind::HttpMethod;
        use std::path::PathBuf;

        let mut graph = CodeGraph::new();

        // File 1: Server with ALL endpoint
        let server_path = PathBuf::from("/test/server.ts");
        {
            let mut staging = StagingGraph::new();
            let mut helper =
                GraphBuildHelper::new(&mut staging, &server_path, Language::TypeScript);
            let _endpoint = helper.add_endpoint("route::ALL::/health", None);
            commit_staging_to_graph(&mut graph, &server_path, Language::TypeScript, staging);
        }

        // File 2: Client making a specific GET request
        let client_path = PathBuf::from("/test/client.js");
        {
            let mut staging = StagingGraph::new();
            let mut helper =
                GraphBuildHelper::new(&mut staging, &client_path, Language::JavaScript);
            let fetch_fn = helper.add_function("checkHealth", None, true, false);
            let target = helper.add_module("http::/health", None);
            helper.add_http_request_edge(fetch_fn, target, HttpMethod::Get, Some("/health"));
            commit_staging_to_graph(&mut graph, &client_path, Language::JavaScript, staging);
        }

        let stats = link_cross_language_edges(&mut graph);
        // A specific GET request should match an ALL endpoint
        assert_eq!(stats.http_requests_scanned, 1);
        assert_eq!(stats.http_endpoints_matched, 1);
    }

    #[test]
    fn test_http_matching_all_method_request() {
        use crate::graph::unified::build::helper::GraphBuildHelper;
        use crate::graph::unified::build::staging::StagingGraph;
        use crate::graph::unified::edge::kind::HttpMethod;
        use std::path::PathBuf;

        let mut graph = CodeGraph::new();

        // File 1: Server with specific GET endpoint
        let server_path = PathBuf::from("/test/server.py");
        {
            let mut staging = StagingGraph::new();
            let mut helper = GraphBuildHelper::new(&mut staging, &server_path, Language::Python);
            let _endpoint = helper.add_endpoint("route::GET::/api/data", None);
            commit_staging_to_graph(&mut graph, &server_path, Language::Python, staging);
        }

        // File 2: Client making an ALL request (wildcard)
        let client_path = PathBuf::from("/test/client.js");
        {
            let mut staging = StagingGraph::new();
            let mut helper =
                GraphBuildHelper::new(&mut staging, &client_path, Language::JavaScript);
            let fetch_fn = helper.add_function("fetchData", None, true, false);
            let target = helper.add_module("http::/api/data", None);
            helper.add_http_request_edge(fetch_fn, target, HttpMethod::All, Some("/api/data"));
            commit_staging_to_graph(&mut graph, &client_path, Language::JavaScript, staging);
        }

        let stats = link_cross_language_edges(&mut graph);
        // An ALL request should match a specific GET endpoint
        assert_eq!(stats.http_requests_scanned, 1);
        assert_eq!(stats.http_endpoints_matched, 1);
    }

    // ---- F6: Typed URL params tests ----

    #[test]
    fn test_normalize_url_path_typed_params() {
        assert_eq!(normalize_url_path("/api/users/<int:id>"), "api/users/:id");
        assert_eq!(
            normalize_url_path("/files/<path:subpath>"),
            "files/:subpath"
        );
        // Regular angle bracket params still work
        assert_eq!(normalize_url_path("/api/users/<id>"), "api/users/:id");
    }
}