Skip to main content

code_system_graph_core/
source_http.rs

1//! Dependency-free, evidence-first extraction of focused HTTP and test declarations.
2//!
3//! The parsers in this module recognize only framework-specific syntax and only promote literal
4//! methods and paths to confirmed observations. Dynamic expressions are retained as ambiguous or
5//! incomplete observations so downstream linking cannot mistake missing evidence for an exact
6//! contract.
7
8use std::cmp::Ordering;
9use std::collections::{BTreeMap, BTreeSet};
10
11use serde::{Deserialize, Serialize};
12
13use crate::router_mounts::mount_observation;
14use crate::{ExtractionLimitExceeded, ExtractionTracker};
15
16mod brace_clients;
17mod brace_flows;
18mod brace_lexer;
19mod brace_tests;
20mod python_flows;
21mod rust_bindings;
22mod rust_flows;
23mod rust_routers;
24mod rust_test_clients;
25
26pub(crate) use brace_clients::{BraceClients, collect_brace_clients, java_method_lines};
27use python_flows::{PythonScopes, keyword_argument, record_fixture_requests};
28use rust_flows::RustScopes;
29use rust_routers::{RustRouters, join_prefixes};
30
31const HTTP_METHODS: [&str; 8] = [
32    "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE",
33];
34
35/// Source language understood by the focused parsers.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
37#[serde(rename_all = "snake_case")]
38pub enum SourceLanguage {
39    /// JavaScript source.
40    JavaScript,
41    /// TypeScript source.
42    TypeScript,
43    /// Rust source.
44    Rust,
45    /// Python source.
46    Python,
47    /// Go source.
48    Go,
49    /// Java source.
50    Java,
51}
52
53/// Framework whose syntax supplied the direct evidence.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
55#[serde(rename_all = "snake_case")]
56pub enum SourceFramework {
57    /// Browser or runtime Fetch API calls.
58    Fetch,
59    /// Axios HTTP client calls.
60    Axios,
61    /// Express route declarations.
62    Express,
63    /// Fastify route declarations.
64    Fastify,
65    /// `NestJS` controller declarations.
66    NestJs,
67    /// Next.js App Router file-route declarations.
68    NextJs,
69    /// Axum router declarations.
70    Axum,
71    /// Actix Web route declarations and route attributes.
72    ActixWeb,
73    /// Utoipa operation declarations, including Actix routes wrapped by `cfg_attr`.
74    Utoipa,
75    /// Reqwest client calls.
76    Reqwest,
77    /// Rust built-in `#[test]` functions.
78    RustTest,
79    /// Tokio `#[tokio::test]` functions.
80    TokioTest,
81    /// Rstest `#[rstest]` functions.
82    Rstest,
83    /// `FastAPI` route decorators.
84    FastApi,
85    /// Flask route decorators.
86    Flask,
87    /// Python requests calls.
88    Requests,
89    /// Python HTTPX calls.
90    Httpx,
91    /// Python aiohttp client-session calls.
92    AioHttp,
93    /// Statically declared Python HTTP method/path registries.
94    PythonHttpRegistry,
95    /// Factory Boy model and sub-factory declarations.
96    FactoryBoy,
97    /// Pytest test functions.
98    Pytest,
99    /// unittest `TestCase` methods.
100    Unittest,
101    /// Canonical `test_` methods in a Python class whose runner inheritance is resolved elsewhere.
102    PythonTest,
103    /// Go standard-library `net/http`.
104    GoNetHttp,
105    /// Gin route declarations.
106    Gin,
107    /// Chi route declarations.
108    Chi,
109    /// Spring MVC route declarations.
110    SpringMvc,
111    /// Spring `WebClient` calls.
112    WebClient,
113    /// Feign client declarations.
114    Feign,
115    /// Spring `RestTemplate` calls.
116    RestTemplate,
117    /// Starlette and `FastAPI` `TestClient`, and HTTPX clients bound to an ASGI or WSGI app.
118    TestClient,
119    /// Flask `app.test_client()` requests.
120    FlaskTestClient,
121    /// Jest tests.
122    Jest,
123    /// Vitest tests.
124    Vitest,
125    /// Mocha tests.
126    Mocha,
127    /// Playwright tests and `APIRequestContext` requests.
128    Playwright,
129    /// Supertest `request(app)` requests.
130    Supertest,
131    /// Go `testing` test functions.
132    GoTest,
133    /// Go `net/http/httptest` requests and servers.
134    Httptest,
135    /// `JUnit` `@Test` methods.
136    JUnit,
137    /// Spring `MockMvc` requests.
138    MockMvc,
139    /// REST Assured requests.
140    RestAssured,
141    /// Spring `WebTestClient` requests.
142    WebTestClient,
143    /// Spring Boot `TestRestTemplate` requests.
144    TestRestTemplate,
145    /// Axum `Router` requests sent with `tower::ServiceExt::oneshot`.
146    AxumOneshot,
147    /// Actix Web `test::TestRequest` requests.
148    ActixTest,
149}
150
151impl SourceFramework {
152    /// Whether requests of this client run in-process against the application of their own
153    /// repository.
154    #[must_use]
155    pub fn is_in_process_client(self) -> bool {
156        matches!(
157            self,
158            Self::TestClient
159                | Self::FlaskTestClient
160                | Self::Supertest
161                | Self::Httptest
162                | Self::MockMvc
163                | Self::RestAssured
164                | Self::WebTestClient
165                | Self::TestRestTemplate
166                | Self::AxumOneshot
167                | Self::ActixTest
168        )
169    }
170}
171
172/// Repository-boundary role represented by an observation.
173#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
174#[serde(rename_all = "snake_case")]
175pub enum SourceRole {
176    /// An HTTP operation served by the repository.
177    Provider,
178    /// An HTTP operation invoked by the repository.
179    Consumer,
180    /// A declared test case.
181    Test,
182    /// A data factory with one statically declared model target.
183    Factory,
184    /// A router mounted under a path prefix on another router or on the application root.
185    Mount,
186    /// A call from the enclosing function to another function of the repository.
187    Call,
188    /// A function, such as a pytest fixture, that returns an in-process test client.
189    Client,
190}
191
192/// Source-level reference to a router or function, resolved per repository.
193#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
194#[serde(rename_all = "snake_case")]
195pub enum SymbolRef {
196    /// A router bound to a name in the declaring file; `default` names a default export.
197    Local(String),
198    /// A router imported from another module, with the module specifier as written.
199    Import {
200        /// Module specifier, such as `./routes/users` or `app.api.users`.
201        module: String,
202        /// Imported name; `default` for default exports.
203        name: String,
204    },
205    /// The router built, returned, or configured by a function defined in the declaring file.
206    Function(String),
207    /// A function referenced by its path as written, such as `users::routes` or `routes.Register`.
208    Call(String),
209    /// A pytest fixture injected by parameter name, declared in the same file or in the nearest
210    /// enclosing `conftest.py`.
211    Fixture(String),
212    /// A parameter of the enclosing function, by name and zero-based position after receivers.
213    Parameter {
214        /// Declared parameter name.
215        name: String,
216        /// Position among the parameters a caller passes.
217        index: usize,
218    },
219}
220
221/// Client URL assembled from literal text and runtime values.
222#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
223pub struct UrlTemplate {
224    /// Parts in source order; adjacent text parts are merged.
225    pub parts: Vec<UrlPart>,
226}
227
228/// One part of a [`UrlTemplate`].
229#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
230#[serde(rename_all = "snake_case")]
231pub enum UrlPart {
232    /// Literal text.
233    Text(String),
234    /// A parameter of the enclosing function, by name and zero-based position after receivers.
235    Parameter {
236        /// Declared parameter name.
237        name: String,
238        /// Position among the parameters a caller passes.
239        index: usize,
240    },
241    /// Any other runtime value, named when it is a plain identifier.
242    Value(Option<String>),
243}
244
245/// Call recorded for repository-level client-wrapper and test-helper correlation.
246#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
247pub struct CallSite {
248    /// Called function.
249    pub callee: SymbolRef,
250    /// Arguments in source order, up to the last string expression.
251    #[serde(default, skip_serializing_if = "Vec::is_empty")]
252    pub arguments: Vec<CallArgument>,
253}
254
255/// One argument of a [`CallSite`].
256#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
257pub struct CallArgument {
258    /// Keyword under which the argument is passed.
259    #[serde(default, skip_serializing_if = "Option::is_none")]
260    pub keyword: Option<String>,
261    /// String value; `None` when the argument is not a string expression.
262    #[serde(default, skip_serializing_if = "Option::is_none")]
263    pub value: Option<UrlTemplate>,
264}
265
266/// Epistemic state of a source observation.
267#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
268#[serde(rename_all = "snake_case")]
269pub enum SourceEpistemicStatus {
270    /// Method and path, when applicable, are supported by exact literal syntax.
271    Confirmed,
272    /// A relevant framework operation was found, but a dynamic expression prevents exact identity.
273    Ambiguous,
274    /// Required method, path, or symbol evidence was absent.
275    Incomplete,
276}
277
278/// Inclusive one-based source range supporting an observation.
279#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
280pub struct SourceLineRange {
281    /// First line containing direct evidence.
282    pub start: u32,
283    /// Last line containing direct evidence.
284    pub end: u32,
285}
286
287/// Machine-readable limitation attached to an observation.
288#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
289#[serde(rename_all = "snake_case")]
290pub enum SourceWarning {
291    /// The HTTP method is computed rather than represented by a supported literal or method name.
292    DynamicMethod,
293    /// The URL or route path is computed rather than represented by one literal.
294    DynamicPath,
295    /// A literal URL is not an absolute URL or root-relative path.
296    UnsupportedLiteralPath,
297    /// The framework declaration did not expose an implementation symbol.
298    MissingSymbol,
299    /// Tree-sitter recovered from at least one syntax error in the source artifact.
300    SyntaxErrorRecovery,
301    /// A documentation-oriented declaration may drift from executable framework registration.
302    AdvisoryDeclaration,
303}
304
305/// Conversion-ready source evidence for an HTTP boundary, implementation, or test case.
306///
307/// Confirmed provider observations have enough method, path, and symbol evidence to construct an
308/// HTTP provider plus an implementation anchor. Confirmed consumers have exact method/path
309/// evidence. Test observations intentionally omit method/path; downstream correlation can join
310/// them to consumer observations with the same `symbol_name`.
311#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
312pub struct SourceObservation {
313    /// Language in which the evidence was found.
314    pub language: SourceLanguage,
315    /// Framework whose syntax was recognized.
316    pub framework: SourceFramework,
317    /// Provider, consumer, or test role.
318    pub role: SourceRole,
319    /// Canonical upper-case HTTP method when exactly known.
320    pub method: Option<String>,
321    /// Canonical normalized HTTP path when exactly known.
322    pub path: Option<String>,
323    /// Enclosing implementation symbol or test name when known.
324    pub symbol_name: Option<String>,
325    /// Related model symbol for declarations such as Factory Boy `Meta.model`.
326    pub related_symbol: Option<String>,
327    /// Repository-relative module path that declares `related_symbol`, when statically imported.
328    pub related_path: Option<String>,
329    /// Lower-case `host[:port]` named by an absolute consumer URL.
330    #[serde(default, skip_serializing_if = "Option::is_none")]
331    pub authority: Option<String>,
332    /// Router a provider route is registered on, the router mounted by a mount observation, or
333    /// the parameter through which a consumer receives its client.
334    #[serde(default, skip_serializing_if = "Option::is_none")]
335    pub router: Option<SymbolRef>,
336    /// Router receiving a mount observation; `None` mounts at the application root.
337    #[serde(default, skip_serializing_if = "Option::is_none")]
338    pub mount_parent: Option<SymbolRef>,
339    /// Consumer URL that depends on parameters of the enclosing function.
340    #[serde(default, skip_serializing_if = "Option::is_none")]
341    pub url: Option<UrlTemplate>,
342    /// Called function and arguments of a call observation.
343    #[serde(default, skip_serializing_if = "Option::is_none")]
344    pub call: Option<CallSite>,
345    /// Inclusive source range containing the direct evidence.
346    pub lines: SourceLineRange,
347    /// Epistemic state of this observation.
348    pub status: SourceEpistemicStatus,
349    /// Normalized confidence in the inclusive range from zero to one.
350    pub confidence: f32,
351    /// Deterministically ordered extraction limitations.
352    pub warnings: Vec<SourceWarning>,
353}
354
355pub(crate) struct SourceObservationCollector<'a> {
356    observations: Vec<SourceObservation>,
357    tracker: Option<&'a mut ExtractionTracker>,
358    error: Option<ExtractionLimitExceeded>,
359}
360
361impl<'a> SourceObservationCollector<'a> {
362    pub(crate) fn unbounded() -> Self {
363        Self {
364            observations: Vec::new(),
365            tracker: None,
366            error: None,
367        }
368    }
369
370    pub(crate) fn bounded(tracker: &'a mut ExtractionTracker) -> Self {
371        Self {
372            observations: Vec::new(),
373            tracker: Some(tracker),
374            error: None,
375        }
376    }
377
378    pub(crate) fn push(&mut self, observation: SourceObservation) {
379        if self.error.is_some() {
380            return;
381        }
382        if let Some(tracker) = self.tracker.as_deref_mut()
383            && let Err(error) = tracker
384                .charge_observation(1)
385                .and_then(|()| charge_source_observation_values(&observation, tracker))
386        {
387            self.error = Some(error);
388            return;
389        }
390        self.observations.push(observation);
391    }
392
393    pub(crate) fn into_result(self) -> Result<Vec<SourceObservation>, ExtractionLimitExceeded> {
394        match self.error {
395            Some(error) => Err(error),
396            None => Ok(self.observations),
397        }
398    }
399
400    pub(crate) fn into_unbounded(self) -> Vec<SourceObservation> {
401        self.observations
402    }
403
404    pub(crate) fn has_role(&self, role: SourceRole) -> bool {
405        self.observations
406            .iter()
407            .any(|observation| observation.role == role)
408    }
409}
410
411pub(crate) fn charge_source_observation_values(
412    observation: &SourceObservation,
413    tracker: &mut ExtractionTracker,
414) -> Result<(), ExtractionLimitExceeded> {
415    if let Some(method) = &observation.method {
416        tracker.charge_identifier(method)?;
417    }
418    if let Some(path) = &observation.path {
419        tracker.charge_portable_path(path)?;
420    }
421    for symbol in [
422        observation.symbol_name.as_deref(),
423        observation.related_symbol.as_deref(),
424    ]
425    .into_iter()
426    .flatten()
427    {
428        tracker.charge_identifier(symbol)?;
429    }
430    if let Some(path) = &observation.related_path {
431        tracker.charge_portable_path(path)?;
432    }
433    let call_values = observation
434        .call
435        .iter()
436        .flat_map(|call| &call.arguments)
437        .filter_map(|argument| argument.value.as_ref());
438    for template in observation.url.iter().chain(call_values) {
439        for part in &template.parts {
440            match part {
441                UrlPart::Text(text) => tracker.charge_string(text)?,
442                UrlPart::Parameter { name, .. } | UrlPart::Value(Some(name)) => {
443                    tracker.charge_identifier(name)?;
444                }
445                UrlPart::Value(None) => {}
446            }
447        }
448    }
449    Ok(())
450}
451
452/// Parses the mandatory focused Rust framework matrix.
453///
454/// Supported syntax comprises Axum `route` declarations, Actix Web route attributes and builder
455/// routes, Reqwest convenience/request calls, and built-in, Tokio, and rstest test attributes.
456/// The returned vector is sorted and deduplicated deterministically.
457#[must_use]
458pub fn parse_rust_source(source: &str) -> Vec<SourceObservation> {
459    let collector = collect_rust_source(source, SourceObservationCollector::unbounded());
460    finish(collector.into_unbounded())
461}
462
463fn collect_rust_source<'a>(
464    source: &str,
465    mut observations: SourceObservationCollector<'a>,
466) -> SourceObservationCollector<'a> {
467    let tokens = lex_rust(source);
468    let functions = rust_functions(&tokens);
469    let routers = RustRouters::new(&tokens, &functions);
470    parse_rust_attributes(&tokens, &mut observations);
471    if has_ident(&tokens, "axum") {
472        parse_axum_routes(&tokens, &routers, &mut observations);
473        for mount in routers.mounts(false) {
474            observations.push(mount);
475        }
476    }
477    if has_ident(&tokens, "actix_web") {
478        parse_actix_builder_routes(&tokens, &routers, &mut observations);
479        for mount in routers.mounts(true) {
480            observations.push(mount);
481        }
482    }
483    let scopes = RustScopes::new(&tokens, &functions);
484    rust_test_clients::parse_rust_test_requests(&tokens, &functions, &scopes, &mut observations);
485    let clients = if has_ident(&tokens, "reqwest") {
486        let receivers = rust_bindings::reqwest_receiver_tokens(&tokens, &functions);
487        parse_reqwest_calls(&tokens, &functions, &receivers, &scopes, &mut observations);
488        receivers
489    } else {
490        BTreeSet::new()
491    };
492    let declares_tests = observations.has_role(SourceRole::Test) || has_ident(&tokens, "test");
493    scopes.record_calls(&clients, declares_tests, &mut observations);
494    observations
495}
496
497/// Parses focused Rust facts while charging each attempted observation before retention.
498///
499/// # Errors
500///
501/// Returns [`ExtractionLimitExceeded`] before an observation or one of its values exceeds the
502/// effective per-artifact budget.
503pub fn parse_rust_source_with_tracker(
504    source: &str,
505    tracker: &mut ExtractionTracker,
506) -> Result<Vec<SourceObservation>, ExtractionLimitExceeded> {
507    let observations = collect_rust_source(source, SourceObservationCollector::bounded(tracker));
508    Ok(finish(observations.into_result()?))
509}
510
511/// Parses the mandatory focused Python framework matrix.
512///
513/// Supported syntax comprises `FastAPI` and Flask decorators, requests, HTTPX and aiohttp
514/// convenience/client calls, static HTTP registries, Factory Boy model declarations, pytest
515/// functions, unittest `TestCase` methods, and canonical class-level `test_` methods whose
516/// indirect runner inheritance cannot be proven in one file. Comments and string contents are
517/// lexically excluded from recognition. The returned vector is sorted and deduplicated.
518#[must_use]
519pub fn parse_python_source(source: &str) -> Vec<SourceObservation> {
520    let collector = collect_python_source(source, SourceObservationCollector::unbounded());
521    finish(collector.into_unbounded())
522}
523
524fn collect_python_source<'a>(
525    source: &str,
526    mut observations: SourceObservationCollector<'a>,
527) -> SourceObservationCollector<'a> {
528    let tokens = lex_python(source);
529    let functions = python_functions(source, &tokens);
530    let contexts = PythonContexts::discover(&tokens);
531    let scopes = PythonScopes::new(&tokens, &functions);
532    parse_python_routes(&tokens, &contexts, &mut observations);
533    parse_python_router_mounts(&tokens, &contexts, &mut observations);
534    parse_python_http_registries(&tokens, &mut observations);
535    parse_python_route_calls(&tokens, &contexts, &mut observations);
536    parse_python_http_calls(&tokens, &functions, &contexts, &scopes, &mut observations);
537    parse_python_client_fixtures(&tokens, &functions, &contexts, &mut observations);
538    parse_python_factories(source, &tokens, &mut observations);
539    parse_python_tests(source, &tokens, &mut observations);
540    let clients = contexts
541        .request_modules
542        .iter()
543        .chain(&contexts.request_clients)
544        .chain(&contexts.httpx_modules)
545        .chain(&contexts.httpx_clients)
546        .chain(&contexts.aiohttp_modules)
547        .chain(&contexts.aiohttp_clients)
548        .chain(contexts.direct_calls.keys())
549        .chain(contexts.test_clients.keys())
550        .map(String::as_str)
551        .collect::<BTreeSet<_>>();
552    let declares_tests = has_ident(&tokens, "pytest")
553        || has_ident(&tokens, "unittest")
554        || functions
555            .iter()
556            .any(|function| function.name.starts_with("test_"));
557    scopes.record_calls(
558        &python_router_imports(&tokens),
559        &clients,
560        declares_tests,
561        &mut observations,
562    );
563    record_fixture_requests(&scopes, &mut observations);
564    observations
565}
566
567/// Parses focused Python facts while charging each attempted observation before retention.
568///
569/// # Errors
570///
571/// Returns [`ExtractionLimitExceeded`] before an observation or one of its values exceeds the
572/// effective per-artifact budget.
573pub fn parse_python_source_with_tracker(
574    source: &str,
575    tracker: &mut ExtractionTracker,
576) -> Result<Vec<SourceObservation>, ExtractionLimitExceeded> {
577    let observations = collect_python_source(source, SourceObservationCollector::bounded(tracker));
578    Ok(finish(observations.into_result()?))
579}
580
581/// Normalizes a literal path using the same slash and trailing-separator rules as HTTP contracts.
582///
583/// Query strings and fragments must be removed by the caller before invoking this function.
584#[must_use]
585pub fn normalize_source_http_path(path: &str) -> String {
586    let trimmed = path.trim();
587    if trimmed.is_empty() || trimmed == "/" {
588        return "/".to_owned();
589    }
590    let mut normalized = String::with_capacity(trimmed.len() + 1);
591    if !trimmed.starts_with('/') {
592        normalized.push('/');
593    }
594    let mut previous_slash = false;
595    for character in trimmed.chars() {
596        if character == '/' {
597            if !previous_slash {
598                normalized.push('/');
599            }
600            previous_slash = true;
601        } else {
602            normalized.push(character);
603            previous_slash = false;
604        }
605    }
606    while normalized.len() > 1 && normalized.ends_with('/') {
607        normalized.pop();
608    }
609    normalized
610}
611
612#[derive(Debug, Clone, PartialEq, Eq)]
613enum TokenKind {
614    Ident(String),
615    Literal(Option<String>),
616    /// Raw body of a JavaScript template literal, including `${...}` substitutions.
617    Template(String),
618    Punct(char),
619}
620
621#[derive(Debug, Clone, PartialEq, Eq)]
622struct Token {
623    kind: TokenKind,
624    line: u32,
625    end_line: u32,
626}
627
628impl Token {
629    fn is_ident(&self, expected: &str) -> bool {
630        matches!(&self.kind, TokenKind::Ident(value) if value == expected)
631    }
632
633    fn is_punct(&self, expected: char) -> bool {
634        self.kind == TokenKind::Punct(expected)
635    }
636
637    fn literal(&self) -> Option<&str> {
638        match &self.kind {
639            TokenKind::Literal(Some(value)) => Some(value),
640            _ => None,
641        }
642    }
643
644    fn ident(&self) -> Option<&str> {
645        match &self.kind {
646            TokenKind::Ident(value) => Some(value),
647            _ => None,
648        }
649    }
650}
651
652#[derive(Debug, Clone)]
653struct FunctionSpan {
654    name: String,
655    start_token: usize,
656    body_start_token: usize,
657    end_token: usize,
658}
659
660fn has_ident(tokens: &[Token], name: &str) -> bool {
661    tokens.iter().any(|token| token.is_ident(name))
662}
663
664fn lex_rust(source: &str) -> Vec<Token> {
665    let characters = source.chars().collect::<Vec<_>>();
666    let mut tokens = Vec::new();
667    let mut index = 0;
668    let mut line = 1_u32;
669    while index < characters.len() {
670        match characters[index] {
671            '\n' => {
672                line += 1;
673                index += 1;
674            }
675            character if character.is_whitespace() => index += 1,
676            '/' if characters.get(index + 1) == Some(&'/') => {
677                index += 2;
678                while index < characters.len() && characters[index] != '\n' {
679                    index += 1;
680                }
681            }
682            '/' if characters.get(index + 1) == Some(&'*') => {
683                index += 2;
684                let mut depth = 1_u32;
685                while index < characters.len() && depth > 0 {
686                    if characters[index] == '\n' {
687                        line += 1;
688                        index += 1;
689                    } else if characters[index] == '/' && characters.get(index + 1) == Some(&'*') {
690                        depth += 1;
691                        index += 2;
692                    } else if characters[index] == '*' && characters.get(index + 1) == Some(&'/') {
693                        depth -= 1;
694                        index += 2;
695                    } else {
696                        index += 1;
697                    }
698                }
699            }
700            'r' if rust_raw_string_start(&characters, index).is_some() => {
701                let start_line = line;
702                let (next, value) = consume_rust_raw_string(&characters, index, &mut line);
703                tokens.push(Token {
704                    kind: TokenKind::Literal(value),
705                    line: start_line,
706                    end_line: line,
707                });
708                index = next;
709            }
710            '"' => {
711                let start_line = line;
712                let (next, value) = consume_quoted(&characters, index, '"', false, &mut line);
713                tokens.push(Token {
714                    kind: TokenKind::Literal(value),
715                    line: start_line,
716                    end_line: line,
717                });
718                index = next;
719            }
720            '\'' if rust_char_literal_end(&characters, index).is_some() => {
721                let (next, _) = consume_quoted(&characters, index, '\'', false, &mut line);
722                index = next;
723            }
724            character if is_ident_start(character) => {
725                let start = index;
726                index += 1;
727                while index < characters.len() && is_ident_continue(characters[index]) {
728                    index += 1;
729                }
730                tokens.push(Token {
731                    kind: TokenKind::Ident(characters[start..index].iter().collect()),
732                    line,
733                    end_line: line,
734                });
735            }
736            character => {
737                tokens.push(Token {
738                    kind: TokenKind::Punct(character),
739                    line,
740                    end_line: line,
741                });
742                index += 1;
743            }
744        }
745    }
746    tokens
747}
748
749fn rust_char_literal_end(characters: &[char], index: usize) -> Option<usize> {
750    let value = index.checked_add(1)?;
751    let closing = if characters.get(value) == Some(&'\\') {
752        match characters.get(value + 1)? {
753            'u' if characters.get(value + 2) == Some(&'{') => {
754                (value + 3..characters.len()).find(|cursor| characters[*cursor] == '}')? + 1
755            }
756            'x' => value.checked_add(4)?,
757            _ => value.checked_add(2)?,
758        }
759    } else {
760        value.checked_add(1)?
761    };
762    (characters.get(closing) == Some(&'\'')).then_some(closing + 1)
763}
764
765fn rust_raw_string_start(characters: &[char], index: usize) -> Option<usize> {
766    let mut cursor = index + 1;
767    while characters.get(cursor) == Some(&'#') {
768        cursor += 1;
769    }
770    (characters.get(cursor) == Some(&'"')).then_some(cursor - index - 1)
771}
772
773fn consume_rust_raw_string(
774    characters: &[char],
775    index: usize,
776    line: &mut u32,
777) -> (usize, Option<String>) {
778    let hashes = rust_raw_string_start(characters, index).unwrap_or_default();
779    let content_start = index + hashes + 2;
780    let mut cursor = content_start;
781    while cursor < characters.len() {
782        if characters[cursor] == '\n' {
783            *line += 1;
784        }
785        if characters[cursor] == '"'
786            && (0..hashes).all(|offset| characters.get(cursor + 1 + offset) == Some(&'#'))
787        {
788            return (
789                cursor + hashes + 1,
790                Some(characters[content_start..cursor].iter().collect()),
791            );
792        }
793        cursor += 1;
794    }
795    (cursor, None)
796}
797
798fn lex_python(source: &str) -> Vec<Token> {
799    let characters = source.chars().collect::<Vec<_>>();
800    let mut tokens = Vec::new();
801    let mut index = 0;
802    let mut line = 1_u32;
803    while index < characters.len() {
804        match characters[index] {
805            '\n' => {
806                line += 1;
807                index += 1;
808            }
809            character if character.is_whitespace() => index += 1,
810            '#' => {
811                while index < characters.len() && characters[index] != '\n' {
812                    index += 1;
813                }
814            }
815            prefix @ ('r' | 'R' | 'u' | 'U')
816                if matches!(characters.get(index + 1), Some('"' | '\'')) =>
817            {
818                let start_line = line;
819                let quote = characters[index + 1];
820                let triple = characters.get(index + 2) == Some(&quote)
821                    && characters.get(index + 3) == Some(&quote);
822                let (next, value) =
823                    consume_quoted(&characters, index + 1, quote, triple, &mut line);
824                let _ = prefix;
825                tokens.push(Token {
826                    kind: TokenKind::Literal(value),
827                    line: start_line,
828                    end_line: line,
829                });
830                index = next;
831            }
832            quote @ ('"' | '\'') => {
833                let start_line = line;
834                let triple = characters.get(index + 1) == Some(&quote)
835                    && characters.get(index + 2) == Some(&quote);
836                let (next, value) = consume_quoted(&characters, index, quote, triple, &mut line);
837                tokens.push(Token {
838                    kind: TokenKind::Literal(value),
839                    line: start_line,
840                    end_line: line,
841                });
842                index = next;
843            }
844            character if is_ident_start(character) => {
845                let start = index;
846                index += 1;
847                while index < characters.len() && is_ident_continue(characters[index]) {
848                    index += 1;
849                }
850                tokens.push(Token {
851                    kind: TokenKind::Ident(characters[start..index].iter().collect()),
852                    line,
853                    end_line: line,
854                });
855            }
856            character => {
857                tokens.push(Token {
858                    kind: TokenKind::Punct(character),
859                    line,
860                    end_line: line,
861                });
862                index += 1;
863            }
864        }
865    }
866    tokens
867}
868
869fn consume_quoted(
870    characters: &[char],
871    index: usize,
872    quote: char,
873    triple: bool,
874    line: &mut u32,
875) -> (usize, Option<String>) {
876    let delimiter_width = if triple { 3 } else { 1 };
877    let mut cursor = index + delimiter_width;
878    let mut value = String::new();
879    let mut valid = true;
880    while cursor < characters.len() {
881        if characters[cursor] == '\n' {
882            *line += 1;
883            if !triple {
884                valid = false;
885            }
886        }
887        let closes = if triple {
888            characters.get(cursor) == Some(&quote)
889                && characters.get(cursor + 1) == Some(&quote)
890                && characters.get(cursor + 2) == Some(&quote)
891        } else {
892            characters.get(cursor) == Some(&quote)
893        };
894        if closes {
895            return (
896                cursor + delimiter_width,
897                if valid { Some(value) } else { None },
898            );
899        }
900        if characters[cursor] == '\\' && !triple {
901            let Some(escaped) = characters.get(cursor + 1).copied() else {
902                return (characters.len(), None);
903            };
904            match escaped {
905                '\\' => value.push('\\'),
906                '"' => value.push('"'),
907                '\'' => value.push('\''),
908                'n' => value.push('\n'),
909                'r' => value.push('\r'),
910                't' => value.push('\t'),
911                _ => valid = false,
912            }
913            cursor += 2;
914        } else {
915            value.push(characters[cursor]);
916            cursor += 1;
917        }
918    }
919    (cursor, None)
920}
921
922fn is_ident_start(character: char) -> bool {
923    character == '_' || character.is_alphabetic()
924}
925
926fn is_ident_continue(character: char) -> bool {
927    character == '_' || character.is_alphanumeric()
928}
929
930fn matching(tokens: &[Token], open: usize, left: char, right: char) -> Option<usize> {
931    if !tokens.get(open).is_some_and(|token| token.is_punct(left)) {
932        return None;
933    }
934    let mut depth = 0_u32;
935    for (index, token) in tokens.iter().enumerate().skip(open) {
936        if token.is_punct(left) {
937            depth += 1;
938        } else if token.is_punct(right) {
939            depth -= 1;
940            if depth == 0 {
941                return Some(index);
942            }
943        }
944    }
945    None
946}
947
948fn rust_functions(tokens: &[Token]) -> Vec<FunctionSpan> {
949    let mut functions = Vec::new();
950    for (index, token) in tokens.iter().enumerate() {
951        if !token.is_ident("fn") {
952            continue;
953        }
954        let Some(name) = tokens.get(index + 1).and_then(Token::ident) else {
955            continue;
956        };
957        let Some(open) = (index + 2..tokens.len())
958            .find(|candidate| tokens[*candidate].is_punct('{') || tokens[*candidate].is_punct(';'))
959        else {
960            continue;
961        };
962        if tokens[open].is_punct(';') {
963            continue;
964        }
965        let end = matching(tokens, open, '{', '}').unwrap_or(tokens.len().saturating_sub(1));
966        functions.push(FunctionSpan {
967            name: name.to_owned(),
968            start_token: index,
969            body_start_token: open,
970            end_token: end,
971        });
972    }
973    functions
974}
975
976/// Position of the smallest function span containing token `index`.
977fn innermost_function(functions: &[FunctionSpan], index: usize) -> Option<usize> {
978    functions
979        .iter()
980        .enumerate()
981        .filter(|(_, function)| function.start_token <= index && index <= function.end_token)
982        .min_by_key(|(_, function)| function.end_token - function.start_token)
983        .map(|(position, _)| position)
984}
985
986/// Splits `start..end` at top-level `separator` punctuation.
987fn split_operands(
988    tokens: &[Token],
989    start: usize,
990    end: usize,
991    separator: char,
992) -> Vec<(usize, usize)> {
993    let mut operands = Vec::new();
994    let mut depth = 0_i32;
995    let mut operand_start = start;
996    for (index, token) in tokens.iter().enumerate().take(end).skip(start) {
997        match token.kind {
998            TokenKind::Punct('(' | '[' | '{') => depth += 1,
999            TokenKind::Punct(')' | ']' | '}') => depth -= 1,
1000            TokenKind::Punct(character) if character == separator && depth == 0 => {
1001                operands.push((operand_start, index));
1002                operand_start = index + 1;
1003            }
1004            _ => {}
1005        }
1006    }
1007    operands.push((operand_start, end));
1008    operands
1009}
1010
1011fn enclosing_symbol(functions: &[FunctionSpan], token_index: usize) -> Option<String> {
1012    functions
1013        .iter()
1014        .filter(|function| function.start_token <= token_index && token_index <= function.end_token)
1015        .min_by_key(|function| function.end_token - function.start_token)
1016        .map(|function| function.name.clone())
1017}
1018
1019fn parse_rust_attributes(tokens: &[Token], observations: &mut SourceObservationCollector<'_>) {
1020    let mut index = 0;
1021    while index + 2 < tokens.len() {
1022        if !tokens[index].is_punct('#') || !tokens[index + 1].is_punct('[') {
1023            index += 1;
1024            continue;
1025        }
1026        let Some(close) = matching(tokens, index + 1, '[', ']') else {
1027            break;
1028        };
1029        let Some((function_index, name, signature_end)) = rust_function_after(tokens, close) else {
1030            index = close + 1;
1031            continue;
1032        };
1033        let path = attribute_path(tokens, index + 2, close);
1034        let framework = match path.as_slice() {
1035            [name] if name == "test" => Some(SourceFramework::RustTest),
1036            [first, second] if first == "tokio" && second == "test" => {
1037                Some(SourceFramework::TokioTest)
1038            }
1039            [first, second]
1040                if (first == "actix_web" || first == "actix_rt") && second == "test" =>
1041            {
1042                Some(SourceFramework::RustTest)
1043            }
1044            [name] if name == "rstest" => Some(SourceFramework::Rstest),
1045            _ => None,
1046        };
1047        if let Some(framework) = framework {
1048            observations.push(confirmed_test(
1049                SourceLanguage::Rust,
1050                framework,
1051                name.clone(),
1052                tokens[index].line,
1053                tokens[signature_end].end_line,
1054            ));
1055        }
1056        parse_utoipa_attribute(
1057            tokens,
1058            index,
1059            close,
1060            function_index,
1061            signature_end,
1062            &name,
1063            observations,
1064        );
1065        if has_ident(tokens, "actix_web") {
1066            parse_actix_attribute(
1067                tokens,
1068                index,
1069                close,
1070                function_index,
1071                signature_end,
1072                name,
1073                &path,
1074                observations,
1075            );
1076        }
1077        index = close + 1;
1078    }
1079}
1080
1081fn parse_utoipa_attribute(
1082    tokens: &[Token],
1083    start: usize,
1084    close: usize,
1085    function_index: usize,
1086    signature_end: usize,
1087    symbol: &str,
1088    observations: &mut SourceObservationCollector<'_>,
1089) {
1090    let Some(utoipa_index) = (start..close).find(|index| tokens[*index].is_ident("utoipa")) else {
1091        return;
1092    };
1093    if !tokens[utoipa_index + 1..close]
1094        .iter()
1095        .any(|token| token.is_ident("path"))
1096    {
1097        return;
1098    }
1099    let method = tokens[utoipa_index..close]
1100        .iter()
1101        .filter_map(Token::ident)
1102        .find_map(canonical_method)
1103        .map(str::to_owned);
1104    let explicit_path = parse_keyword_string_values(tokens, utoipa_index, close, "path")
1105        .into_iter()
1106        .next();
1107    let context_path = parse_keyword_string_values(tokens, utoipa_index, close, "context_path")
1108        .into_iter()
1109        .next();
1110    let actix = rust_actix_route_between(tokens, close.saturating_add(1), function_index);
1111    let method = method.or_else(|| actix.as_ref().and_then(|(method, _)| method.clone()));
1112    let path = explicit_path.or_else(|| {
1113        let (context, (_, route)) = (context_path?, actix?);
1114        Some(normalize_source_http_path(&format!("{context}{route}")))
1115    });
1116    let lines = SourceLineRange {
1117        start: tokens[start].line,
1118        end: tokens[signature_end].end_line,
1119    };
1120    let mut observation = http_from_literal(
1121        SourceLanguage::Rust,
1122        SourceFramework::Utoipa,
1123        SourceRole::Provider,
1124        method,
1125        path.as_deref(),
1126        Some(symbol.to_owned()),
1127        lines,
1128        true,
1129    );
1130    if observation.status == SourceEpistemicStatus::Confirmed {
1131        observation.confidence = 0.75;
1132        observation
1133            .warnings
1134            .push(SourceWarning::AdvisoryDeclaration);
1135    }
1136    observations.push(observation);
1137}
1138
1139fn rust_actix_route_between(
1140    tokens: &[Token],
1141    start: usize,
1142    end: usize,
1143) -> Option<(Option<String>, String)> {
1144    let mut index = start;
1145    while index + 3 < end {
1146        if !tokens[index].is_punct('#') || !tokens[index + 1].is_punct('[') {
1147            index += 1;
1148            continue;
1149        }
1150        let close = matching(tokens, index + 1, '[', ']')?;
1151        if close > end {
1152            return None;
1153        }
1154        let method = tokens
1155            .get(index + 2)
1156            .and_then(Token::ident)
1157            .and_then(canonical_method)
1158            .map(str::to_owned);
1159        let open = (index + 2..close).find(|candidate| tokens[*candidate].is_punct('('));
1160        let path = open
1161            .and_then(|open| first_argument(open, close))
1162            .and_then(|argument| tokens.get(argument))
1163            .and_then(Token::literal)
1164            .and_then(route_literal_path)?;
1165        return Some((method, path));
1166    }
1167    None
1168}
1169
1170fn rust_function_after(tokens: &[Token], close: usize) -> Option<(usize, String, usize)> {
1171    let mut index = close + 1;
1172    while index < tokens.len() && index <= close + 24 {
1173        if tokens[index].is_punct('#') {
1174            if tokens
1175                .get(index + 1)
1176                .is_some_and(|token| token.is_punct('['))
1177            {
1178                index = matching(tokens, index + 1, '[', ']')? + 1;
1179                continue;
1180            }
1181            return None;
1182        }
1183        if tokens[index].is_ident("fn") {
1184            let name = tokens.get(index + 1)?.ident()?.to_owned();
1185            let end = (index + 2..tokens.len())
1186                .find(|candidate| {
1187                    tokens[*candidate].is_punct('{') || tokens[*candidate].is_punct(';')
1188                })
1189                .unwrap_or(index + 1);
1190            return Some((index, name, end));
1191        }
1192        index += 1;
1193    }
1194    None
1195}
1196
1197fn attribute_path(tokens: &[Token], start: usize, end: usize) -> Vec<String> {
1198    let mut path = Vec::new();
1199    let mut index = start;
1200    while index < end && !tokens[index].is_punct('(') {
1201        if let Some(name) = tokens[index].ident() {
1202            path.push(name.to_owned());
1203        }
1204        index += 1;
1205    }
1206    path
1207}
1208
1209#[expect(
1210    clippy::too_many_arguments,
1211    reason = "Attribute evidence is passed without allocation"
1212)]
1213fn parse_actix_attribute(
1214    tokens: &[Token],
1215    start: usize,
1216    close: usize,
1217    _function_index: usize,
1218    signature_end: usize,
1219    symbol: String,
1220    path: &[String],
1221    observations: &mut SourceObservationCollector<'_>,
1222) {
1223    let direct_method = match path {
1224        [name] => canonical_method(name),
1225        _ => None,
1226    };
1227    if direct_method.is_none() && path != ["route"] {
1228        return;
1229    }
1230    let open = (start + 2..close).find(|index| tokens[*index].is_punct('('));
1231    let literal = open
1232        .and_then(|open| first_argument(open, close))
1233        .and_then(|argument| tokens.get(argument))
1234        .and_then(Token::literal);
1235    let methods = direct_method
1236        .into_iter()
1237        .map(str::to_owned)
1238        .chain(
1239            parse_keyword_string_values(tokens, start, close, "method")
1240                .into_iter()
1241                .filter_map(|method| canonical_method(&method).map(str::to_owned)),
1242        )
1243        .collect::<BTreeSet<_>>();
1244    if methods.is_empty() {
1245        observations.push(inexact_http(
1246            SourceLanguage::Rust,
1247            SourceFramework::ActixWeb,
1248            SourceRole::Provider,
1249            None,
1250            literal.and_then(route_literal_path),
1251            Some(symbol),
1252            SourceLineRange {
1253                start: tokens[start].line,
1254                end: tokens[signature_end].end_line,
1255            },
1256            SourceWarning::DynamicMethod,
1257        ));
1258        return;
1259    }
1260    for method in methods {
1261        let mut observation = http_from_literal(
1262            SourceLanguage::Rust,
1263            SourceFramework::ActixWeb,
1264            SourceRole::Provider,
1265            Some(method),
1266            literal,
1267            Some(symbol.clone()),
1268            SourceLineRange {
1269                start: tokens[start].line,
1270                end: tokens[signature_end].end_line,
1271            },
1272            true,
1273        );
1274        observation.router = Some(SymbolRef::Function(symbol.clone()));
1275        observations.push(observation);
1276    }
1277}
1278
1279fn parse_axum_routes(
1280    tokens: &[Token],
1281    routers: &RustRouters<'_>,
1282    observations: &mut SourceObservationCollector<'_>,
1283) {
1284    for (index, token) in tokens.iter().enumerate() {
1285        if !token.is_ident("route")
1286            || !tokens
1287                .get(index.wrapping_sub(1))
1288                .is_some_and(|token| token.is_punct('.'))
1289            || !tokens
1290                .get(index + 1)
1291                .is_some_and(|token| token.is_punct('('))
1292        {
1293            continue;
1294        }
1295        let Some(close) = matching(tokens, index + 1, '(', ')') else {
1296            continue;
1297        };
1298        let arguments = top_level_arguments(tokens, index + 1, close);
1299        if arguments.len() < 2 {
1300            continue;
1301        }
1302        let literal = tokens.get(arguments[0]).and_then(Token::literal);
1303        let router = routers.chain_owner(index - 1).router;
1304        let methods = method_calls(tokens, arguments[1], close);
1305        for (method, handler) in methods {
1306            let mut observation = http_from_literal(
1307                SourceLanguage::Rust,
1308                SourceFramework::Axum,
1309                SourceRole::Provider,
1310                Some(method),
1311                literal,
1312                handler,
1313                SourceLineRange {
1314                    start: token.line,
1315                    end: tokens[close].end_line,
1316                },
1317                true,
1318            );
1319            observation.router = Some(router.clone());
1320            observations.push(observation);
1321        }
1322    }
1323}
1324
1325fn parse_actix_builder_routes(
1326    tokens: &[Token],
1327    routers: &RustRouters<'_>,
1328    observations: &mut SourceObservationCollector<'_>,
1329) {
1330    for (index, token) in tokens.iter().enumerate() {
1331        if !token.is_ident("route")
1332            || !tokens
1333                .get(index + 1)
1334                .is_some_and(|token| token.is_punct('('))
1335        {
1336            continue;
1337        }
1338        let Some(close) = matching(tokens, index + 1, '(', ')') else {
1339            continue;
1340        };
1341        let arguments = top_level_arguments(tokens, index + 1, close);
1342        if arguments.len() < 2 || !contains_ident(tokens, arguments[1], close, "web") {
1343            continue;
1344        }
1345        let owner =
1346            (index > 0 && tokens[index - 1].is_punct('.')).then(|| routers.chain_owner(index - 1));
1347        let literal = tokens.get(arguments[0]).and_then(Token::literal);
1348        let literal = match owner.as_ref().and_then(|owner| owner.prefix.as_deref()) {
1349            Some(prefix) => join_prefixes(Some(prefix), literal),
1350            None => literal.map(str::to_owned),
1351        };
1352        for (method, handler) in method_calls(tokens, arguments[1], close) {
1353            let mut observation = http_from_literal(
1354                SourceLanguage::Rust,
1355                SourceFramework::ActixWeb,
1356                SourceRole::Provider,
1357                Some(method),
1358                literal.as_deref(),
1359                handler,
1360                SourceLineRange {
1361                    start: token.line,
1362                    end: tokens[close].end_line,
1363                },
1364                true,
1365            );
1366            observation.router = owner.as_ref().map(|owner| owner.router.clone());
1367            observations.push(observation);
1368        }
1369    }
1370    for (index, token) in tokens.iter().enumerate() {
1371        if !token.is_ident("resource")
1372            || !tokens
1373                .get(index.wrapping_sub(1))
1374                .is_some_and(|token| token.is_punct(':'))
1375            || !tokens
1376                .get(index + 1)
1377                .is_some_and(|token| token.is_punct('('))
1378        {
1379            continue;
1380        }
1381        let Some(resource_close) = matching(tokens, index + 1, '(', ')') else {
1382            continue;
1383        };
1384        let end = (resource_close + 1..tokens.len())
1385            .find(|candidate| tokens[*candidate].is_punct(';'))
1386            .unwrap_or(resource_close);
1387        if !contains_ident(tokens, resource_close + 1, end, "route") {
1388            continue;
1389        }
1390        let literal = tokens.get(index + 2).and_then(Token::literal);
1391        let router = tokens
1392            .get(resource_close + 1)
1393            .is_some_and(|token| token.is_punct('.'))
1394            .then(|| routers.chain_owner(resource_close + 1).router);
1395        for (method, handler) in method_calls(tokens, resource_close + 1, end) {
1396            let mut observation = http_from_literal(
1397                SourceLanguage::Rust,
1398                SourceFramework::ActixWeb,
1399                SourceRole::Provider,
1400                Some(method),
1401                literal,
1402                handler,
1403                SourceLineRange {
1404                    start: token.line,
1405                    end: tokens[end].end_line,
1406                },
1407                true,
1408            );
1409            observation.router.clone_from(&router);
1410            observations.push(observation);
1411        }
1412    }
1413}
1414
1415fn parse_reqwest_calls(
1416    tokens: &[Token],
1417    functions: &[FunctionSpan],
1418    reqwest_receivers: &BTreeSet<usize>,
1419    scopes: &RustScopes<'_>,
1420    observations: &mut SourceObservationCollector<'_>,
1421) {
1422    for index in 0..tokens.len() {
1423        let Some(method_index) = reqwest_method_index(tokens, index, reqwest_receivers) else {
1424            continue;
1425        };
1426        let Some(method_name) = tokens.get(method_index).and_then(Token::ident) else {
1427            continue;
1428        };
1429        if !tokens
1430            .get(method_index + 1)
1431            .is_some_and(|token| token.is_punct('('))
1432        {
1433            continue;
1434        }
1435        let Some(close) = matching(tokens, method_index + 1, '(', ')') else {
1436            continue;
1437        };
1438        let arguments = top_level_arguments(tokens, method_index + 1, close);
1439        let (method, path_argument) = if method_name == "request" {
1440            (
1441                arguments.first().and_then(|argument| {
1442                    let method_end = arguments
1443                        .get(1)
1444                        .map_or(close, |second| second.saturating_sub(1));
1445                    rust_method_expression(tokens, *argument, method_end)
1446                }),
1447                arguments.get(1).copied(),
1448            )
1449        } else {
1450            (
1451                canonical_method(method_name).map(str::to_owned),
1452                arguments.first().copied(),
1453            )
1454        };
1455        if method.is_none() && method_name != "request" {
1456            continue;
1457        }
1458        let template = path_argument.map(|argument| {
1459            let end = arguments
1460                .iter()
1461                .find(|start| **start > argument)
1462                .map_or(close, |next| next - 1);
1463            scopes.template(argument, end, index)
1464        });
1465        let literal = template.as_ref().and_then(UrlTemplate::client_literal);
1466        let mut observation = http_from_literal(
1467            SourceLanguage::Rust,
1468            SourceFramework::Reqwest,
1469            SourceRole::Consumer,
1470            method,
1471            literal.as_deref(),
1472            enclosing_symbol(functions, index),
1473            SourceLineRange {
1474                start: tokens[index].line,
1475                end: tokens[close].end_line,
1476            },
1477            false,
1478        );
1479        if method_name == "request" && observation.method.is_none() {
1480            observation.status = SourceEpistemicStatus::Incomplete;
1481            observation.confidence = 0.0;
1482            observation.warnings.push(SourceWarning::DynamicMethod);
1483            observation.warnings.sort();
1484            observation.warnings.dedup();
1485        }
1486        observation.url = template.filter(UrlTemplate::has_parameters);
1487        observations.push(observation);
1488    }
1489}
1490
1491/// Index of the method name called at token `index` on the `reqwest` or `reqwest::blocking`
1492/// module, or on a Reqwest client receiver.
1493fn reqwest_method_index(
1494    tokens: &[Token],
1495    index: usize,
1496    reqwest_receivers: &BTreeSet<usize>,
1497) -> Option<usize> {
1498    let path_separator = |at: usize| {
1499        tokens.get(at).is_some_and(|token| token.is_punct(':'))
1500            && tokens.get(at + 1).is_some_and(|token| token.is_punct(':'))
1501    };
1502    if tokens[index].is_ident("reqwest") && path_separator(index + 1) {
1503        let direct = index + 3;
1504        let blocking = tokens
1505            .get(direct)
1506            .is_some_and(|token| token.is_ident("blocking"))
1507            && path_separator(direct + 1);
1508        return Some(if blocking { direct + 3 } else { direct });
1509    }
1510    (reqwest_receivers.contains(&index)
1511        && tokens
1512            .get(index + 1)
1513            .is_some_and(|token| token.is_punct('.')))
1514    .then_some(index + 2)
1515}
1516
1517fn rust_method_expression(tokens: &[Token], start: usize, end: usize) -> Option<String> {
1518    if let Some(literal) = tokens.get(start).and_then(Token::literal) {
1519        return canonical_method(literal).map(str::to_owned);
1520    }
1521    (start..end)
1522        .filter_map(|index| tokens[index].ident())
1523        .find_map(|name| canonical_method(name).map(str::to_owned))
1524}
1525
1526fn method_calls(tokens: &[Token], start: usize, end: usize) -> Vec<(String, Option<String>)> {
1527    let mut methods = Vec::new();
1528    for index in start..end {
1529        let Some(name) = tokens[index].ident() else {
1530            continue;
1531        };
1532        let Some(method) = canonical_method(name) else {
1533            continue;
1534        };
1535        if !tokens
1536            .get(index + 1)
1537            .is_some_and(|token| token.is_punct('('))
1538        {
1539            continue;
1540        }
1541        let direct_handler = tokens
1542            .get(index + 2)
1543            .and_then(Token::ident)
1544            .map(str::to_owned);
1545        let actix_handler = (index + 2..end)
1546            .find(|candidate| {
1547                tokens[*candidate].is_ident("to")
1548                    && tokens
1549                        .get(*candidate + 1)
1550                        .is_some_and(|token| token.is_punct('('))
1551            })
1552            .and_then(|to| tokens.get(to + 2))
1553            .and_then(Token::ident)
1554            .map(str::to_owned);
1555        let handler = direct_handler.or(actix_handler);
1556        methods.push((method.to_owned(), handler));
1557    }
1558    methods
1559}
1560
1561fn contains_ident(tokens: &[Token], start: usize, end: usize, name: &str) -> bool {
1562    tokens
1563        .get(start..end)
1564        .is_some_and(|slice| slice.iter().any(|token| token.is_ident(name)))
1565}
1566
1567fn first_argument(open: usize, close: usize) -> Option<usize> {
1568    (open + 1 < close).then_some(open + 1)
1569}
1570
1571fn top_level_arguments(tokens: &[Token], open: usize, close: usize) -> Vec<usize> {
1572    if open + 1 >= close {
1573        return Vec::new();
1574    }
1575    let mut arguments = vec![open + 1];
1576    let mut round = 0_i32;
1577    let mut square = 0_i32;
1578    let mut curly = 0_i32;
1579    for (index, token) in tokens.iter().enumerate().take(close).skip(open + 1) {
1580        match token.kind {
1581            TokenKind::Punct('(') => round += 1,
1582            TokenKind::Punct(')') => round -= 1,
1583            TokenKind::Punct('[') => square += 1,
1584            TokenKind::Punct(']') => square -= 1,
1585            TokenKind::Punct('{') => curly += 1,
1586            TokenKind::Punct('}') => curly -= 1,
1587            TokenKind::Punct(',')
1588                if round == 0 && square == 0 && curly == 0 && index + 1 < close =>
1589            {
1590                arguments.push(index + 1);
1591            }
1592            _ => {}
1593        }
1594    }
1595    arguments
1596}
1597
1598fn parse_keyword_string_values(
1599    tokens: &[Token],
1600    start: usize,
1601    end: usize,
1602    keyword: &str,
1603) -> Vec<String> {
1604    let mut values = Vec::new();
1605    for index in start..end {
1606        if !tokens[index].is_ident(keyword)
1607            || !tokens
1608                .get(index + 1)
1609                .is_some_and(|token| token.is_punct('='))
1610        {
1611            continue;
1612        }
1613        let value_start = index + 2;
1614        if let Some(value) = tokens.get(value_start).and_then(Token::literal) {
1615            values.push(value.to_owned());
1616        } else if tokens
1617            .get(value_start)
1618            .is_some_and(|token| token.is_punct('['))
1619        {
1620            let close = matching(tokens, value_start, '[', ']').unwrap_or(end);
1621            values.extend(
1622                tokens[value_start + 1..close]
1623                    .iter()
1624                    .filter_map(Token::literal)
1625                    .map(str::to_owned),
1626            );
1627        }
1628    }
1629    values
1630}
1631
1632fn canonical_method(name: &str) -> Option<&'static str> {
1633    HTTP_METHODS
1634        .iter()
1635        .copied()
1636        .find(|method| method.eq_ignore_ascii_case(name))
1637}
1638
1639/// Call from function `function` recorded for repository-level flow composition.
1640pub(crate) fn call_observation(
1641    language: SourceLanguage,
1642    callee: SymbolRef,
1643    arguments: Vec<CallArgument>,
1644    function: String,
1645    lines: SourceLineRange,
1646) -> SourceObservation {
1647    let framework = match language {
1648        SourceLanguage::Python => SourceFramework::PythonTest,
1649        SourceLanguage::Rust => SourceFramework::RustTest,
1650        SourceLanguage::TypeScript | SourceLanguage::JavaScript => SourceFramework::Fetch,
1651        SourceLanguage::Go => SourceFramework::GoNetHttp,
1652        SourceLanguage::Java => SourceFramework::SpringMvc,
1653    };
1654    SourceObservation {
1655        language,
1656        framework,
1657        role: SourceRole::Call,
1658        method: None,
1659        path: None,
1660        symbol_name: Some(function),
1661        related_symbol: None,
1662        related_path: None,
1663        authority: None,
1664        router: None,
1665        mount_parent: None,
1666        url: None,
1667        call: Some(CallSite { callee, arguments }),
1668        lines,
1669        status: SourceEpistemicStatus::Confirmed,
1670        confidence: 1.0,
1671        warnings: Vec::new(),
1672    }
1673}
1674
1675/// Confirmed consumer of `template` issued from `caller`, with the client coordinates of
1676/// `origin`; `None` when the template does not yield an exact method and path.
1677pub(crate) fn instantiated_consumer(
1678    origin: &SourceObservation,
1679    template: &UrlTemplate,
1680    caller: &str,
1681    lines: SourceLineRange,
1682) -> Option<SourceObservation> {
1683    let literal = template.client_literal()?;
1684    let observation = http_from_literal(
1685        origin.language,
1686        origin.framework,
1687        SourceRole::Consumer,
1688        origin.method.clone(),
1689        Some(&literal),
1690        Some(caller.to_owned()),
1691        lines,
1692        false,
1693    );
1694    (observation.status == SourceEpistemicStatus::Confirmed).then_some(observation)
1695}
1696
1697fn confirmed_test(
1698    language: SourceLanguage,
1699    framework: SourceFramework,
1700    name: String,
1701    start: u32,
1702    end: u32,
1703) -> SourceObservation {
1704    SourceObservation {
1705        language,
1706        framework,
1707        role: SourceRole::Test,
1708        method: None,
1709        path: None,
1710        symbol_name: Some(name),
1711        related_symbol: None,
1712        related_path: None,
1713        authority: None,
1714        router: None,
1715        mount_parent: None,
1716        url: None,
1717        call: None,
1718        lines: SourceLineRange { start, end },
1719        status: SourceEpistemicStatus::Confirmed,
1720        confidence: 1.0,
1721        warnings: Vec::new(),
1722    }
1723}
1724
1725#[expect(
1726    clippy::too_many_arguments,
1727    reason = "Observation fields remain explicit at evidence sites"
1728)]
1729fn http_from_literal(
1730    language: SourceLanguage,
1731    framework: SourceFramework,
1732    role: SourceRole,
1733    method: Option<String>,
1734    literal: Option<&str>,
1735    symbol_name: Option<String>,
1736    lines: SourceLineRange,
1737    route: bool,
1738) -> SourceObservation {
1739    let (path, authority) = literal.map_or((None, None), |value| {
1740        if route {
1741            (route_literal_path(value), None)
1742        } else {
1743            client_literal_identity(value)
1744        }
1745    });
1746    let warning = if literal.is_none() {
1747        Some(SourceWarning::DynamicPath)
1748    } else if path.is_none() {
1749        Some(SourceWarning::UnsupportedLiteralPath)
1750    } else {
1751        None
1752    };
1753    let status = if path.is_some() && method.is_some() {
1754        SourceEpistemicStatus::Confirmed
1755    } else {
1756        SourceEpistemicStatus::Ambiguous
1757    };
1758    let mut observation = SourceObservation {
1759        language,
1760        framework,
1761        role,
1762        method,
1763        path,
1764        symbol_name,
1765        related_symbol: None,
1766        related_path: None,
1767        authority,
1768        router: None,
1769        mount_parent: None,
1770        url: None,
1771        call: None,
1772        lines,
1773        status,
1774        confidence: if status == SourceEpistemicStatus::Confirmed {
1775            1.0
1776        } else {
1777            0.0
1778        },
1779        warnings: warning.into_iter().collect(),
1780    };
1781    if role == SourceRole::Provider && observation.symbol_name.is_none() {
1782        observation.status = SourceEpistemicStatus::Incomplete;
1783        observation.confidence = 0.0;
1784        observation.warnings.push(SourceWarning::MissingSymbol);
1785    }
1786    observation
1787}
1788
1789#[expect(
1790    clippy::too_many_arguments,
1791    reason = "Observation fields remain explicit at evidence sites"
1792)]
1793fn inexact_http(
1794    language: SourceLanguage,
1795    framework: SourceFramework,
1796    role: SourceRole,
1797    method: Option<String>,
1798    path: Option<String>,
1799    symbol_name: Option<String>,
1800    lines: SourceLineRange,
1801    warning: SourceWarning,
1802) -> SourceObservation {
1803    SourceObservation {
1804        language,
1805        framework,
1806        role,
1807        method,
1808        path,
1809        symbol_name,
1810        related_symbol: None,
1811        related_path: None,
1812        authority: None,
1813        router: None,
1814        mount_parent: None,
1815        url: None,
1816        call: None,
1817        lines,
1818        status: SourceEpistemicStatus::Incomplete,
1819        confidence: 0.0,
1820        warnings: vec![warning],
1821    }
1822}
1823
1824fn route_literal_path(value: &str) -> Option<String> {
1825    (value.starts_with('/') && !value.contains(['?', '#']))
1826        .then(|| normalize_source_http_path(value))
1827}
1828
1829fn client_literal_identity(value: &str) -> (Option<String>, Option<String>) {
1830    if value.starts_with('/') {
1831        return (
1832            Some(normalize_source_http_path(
1833                value.split(['?', '#']).next().unwrap_or(value),
1834            )),
1835            None,
1836        );
1837    }
1838    let after_authority = value
1839        .strip_prefix("http://")
1840        .or_else(|| value.strip_prefix("https://"));
1841    let Some(after_authority) = after_authority else {
1842        return (None, None);
1843    };
1844    if after_authority.is_empty() || after_authority.starts_with('/') {
1845        return (None, None);
1846    }
1847    let separator = after_authority.find('/');
1848    let path = separator.map_or("/", |index| &after_authority[index..]);
1849    (
1850        Some(normalize_source_http_path(
1851            path.split(['?', '#']).next().unwrap_or(path),
1852        )),
1853        crate::routes::url_authority(value),
1854    )
1855}
1856
1857#[derive(Debug, Default)]
1858struct PythonContexts {
1859    fastapi_apps: BTreeSet<String>,
1860    route_prefixes: BTreeMap<String, String>,
1861    flask_apps: BTreeSet<String>,
1862    request_modules: BTreeSet<String>,
1863    httpx_modules: BTreeSet<String>,
1864    aiohttp_modules: BTreeSet<String>,
1865    request_clients: BTreeSet<String>,
1866    httpx_clients: BTreeSet<String>,
1867    aiohttp_clients: BTreeSet<String>,
1868    direct_calls: BTreeMap<String, (SourceFramework, String)>,
1869    string_constants: BTreeMap<String, String>,
1870    test_clients: BTreeMap<String, SourceFramework>,
1871}
1872
1873impl PythonContexts {
1874    fn discover(tokens: &[Token]) -> Self {
1875        let mut contexts = Self::default();
1876        contexts.request_modules.insert("requests".to_owned());
1877        contexts.httpx_modules.insert("httpx".to_owned());
1878        contexts.aiohttp_modules.insert("aiohttp".to_owned());
1879        discover_python_import_aliases(tokens, "requests", &mut contexts.request_modules);
1880        discover_python_import_aliases(tokens, "httpx", &mut contexts.httpx_modules);
1881        discover_python_import_aliases(tokens, "aiohttp", &mut contexts.aiohttp_modules);
1882        discover_python_direct_calls(
1883            tokens,
1884            "requests",
1885            SourceFramework::Requests,
1886            &mut contexts.direct_calls,
1887        );
1888        discover_python_direct_calls(
1889            tokens,
1890            "httpx",
1891            SourceFramework::Httpx,
1892            &mut contexts.direct_calls,
1893        );
1894        let request_session_imported = python_imports_item(tokens, "requests", "Session");
1895        let httpx_client_imported = python_imports_item(tokens, "httpx", "Client")
1896            || python_imports_item(tokens, "httpx", "AsyncClient");
1897        let aiohttp_client_imported = python_imports_item(tokens, "aiohttp", "ClientSession");
1898        for index in 0..tokens.len() {
1899            let Some(name) = tokens[index].ident() else {
1900                continue;
1901            };
1902            if !tokens
1903                .get(index + 1)
1904                .is_some_and(|token| token.is_punct('='))
1905            {
1906                continue;
1907            }
1908            let end = python_expression_end(tokens, index + 2);
1909            if let Some(value) =
1910                python_static_string_expression(tokens, index + 2, end, &contexts.string_constants)
1911            {
1912                contexts.string_constants.insert(name.to_owned(), value);
1913            }
1914            if let Some(framework) = python_test_client(tokens, index + 2, end) {
1915                contexts.test_clients.insert(name.to_owned(), framework);
1916                continue;
1917            }
1918            if contains_ident(tokens, index + 2, end, "FastAPI")
1919                || contains_ident(tokens, index + 2, end, "APIRouter")
1920            {
1921                contexts.fastapi_apps.insert(name.to_owned());
1922                if contains_ident(tokens, index + 2, end, "APIRouter")
1923                    && let Some(prefix) =
1924                        python_constructor_prefix(tokens, index + 2, end, "prefix")
1925                {
1926                    contexts.route_prefixes.insert(name.to_owned(), prefix);
1927                }
1928            }
1929            if contains_ident(tokens, index + 2, end, "Flask")
1930                || contains_ident(tokens, index + 2, end, "Blueprint")
1931            {
1932                contexts.flask_apps.insert(name.to_owned());
1933                if contains_ident(tokens, index + 2, end, "Blueprint")
1934                    && let Some(prefix) =
1935                        python_constructor_prefix(tokens, index + 2, end, "url_prefix")
1936                {
1937                    contexts.route_prefixes.insert(name.to_owned(), prefix);
1938                }
1939            }
1940            if contains_ident(tokens, index + 2, end, "Session")
1941                && (contains_any(tokens, index + 2, end, &contexts.request_modules)
1942                    || request_session_imported)
1943            {
1944                contexts.request_clients.insert(name.to_owned());
1945            }
1946            if (contains_ident(tokens, index + 2, end, "Client")
1947                || contains_ident(tokens, index + 2, end, "AsyncClient"))
1948                && (contains_any(tokens, index + 2, end, &contexts.httpx_modules)
1949                    || httpx_client_imported)
1950            {
1951                contexts.httpx_clients.insert(name.to_owned());
1952            }
1953            if contains_ident(tokens, index + 2, end, "ClientSession")
1954                && (contains_any(tokens, index + 2, end, &contexts.aiohttp_modules)
1955                    || aiohttp_client_imported)
1956            {
1957                contexts.aiohttp_clients.insert(name.to_owned());
1958            }
1959        }
1960        contexts.discover_with_targets(tokens, aiohttp_client_imported);
1961        contexts
1962    }
1963
1964    /// Clients bound by `with ... as NAME`.
1965    fn discover_with_targets(&mut self, tokens: &[Token], aiohttp_client_imported: bool) {
1966        for index in 0..tokens.len() {
1967            if !tokens[index].is_ident("as") {
1968                continue;
1969            }
1970            let Some(name) = tokens.get(index + 1).and_then(Token::ident) else {
1971                continue;
1972            };
1973            let start = index.saturating_sub(32);
1974            let with = (start..index)
1975                .rev()
1976                .find(|candidate| tokens[*candidate].is_ident("with"))
1977                .unwrap_or(start);
1978            if let Some(framework) = python_test_client(tokens, with, index) {
1979                self.test_clients.insert(name.to_owned(), framework);
1980            } else if aiohttp_client_imported
1981                && tokens[start..index]
1982                    .iter()
1983                    .any(|token| token.is_ident("ClientSession"))
1984            {
1985                self.aiohttp_clients.insert(name.to_owned());
1986            }
1987        }
1988    }
1989
1990    /// Client receivers and the framework of each.
1991    fn http_receivers(&self) -> BTreeMap<&str, SourceFramework> {
1992        self.request_modules
1993            .iter()
1994            .chain(&self.request_clients)
1995            .map(|name| (name.as_str(), SourceFramework::Requests))
1996            .chain(
1997                self.httpx_modules
1998                    .iter()
1999                    .chain(&self.httpx_clients)
2000                    .map(|name| (name.as_str(), SourceFramework::Httpx)),
2001            )
2002            .chain(
2003                self.aiohttp_clients
2004                    .iter()
2005                    .map(|name| (name.as_str(), SourceFramework::AioHttp)),
2006            )
2007            .chain(
2008                self.test_clients
2009                    .iter()
2010                    .map(|(name, framework)| (name.as_str(), *framework)),
2011            )
2012            .collect()
2013    }
2014}
2015
2016/// In-process test client constructed by the expression in `start..end`.
2017fn python_test_client(tokens: &[Token], start: usize, end: usize) -> Option<SourceFramework> {
2018    if contains_ident(tokens, start, end, "TestClient") {
2019        return Some(SourceFramework::TestClient);
2020    }
2021    let flask = (start.max(1)..end).any(|index| {
2022        tokens[index].is_ident("test_client")
2023            && tokens[index - 1].is_punct('.')
2024            && tokens
2025                .get(index + 1)
2026                .is_some_and(|token| token.is_punct('('))
2027    });
2028    if flask {
2029        return Some(SourceFramework::FlaskTestClient);
2030    }
2031    let httpx = contains_ident(tokens, start, end, "AsyncClient")
2032        || contains_ident(tokens, start, end, "Client");
2033    let in_process = contains_ident(tokens, start, end, "ASGITransport")
2034        || contains_ident(tokens, start, end, "WSGITransport")
2035        || (start.max(1)..end).any(|index| {
2036            tokens[index].is_ident("app")
2037                && (tokens[index - 1].is_punct('(') || tokens[index - 1].is_punct(','))
2038                && tokens
2039                    .get(index + 1)
2040                    .is_some_and(|token| token.is_punct('='))
2041        });
2042    (httpx && in_process).then_some(SourceFramework::TestClient)
2043}
2044
2045/// Functions, such as pytest fixtures, that return or yield an in-process test client.
2046fn parse_python_client_fixtures(
2047    tokens: &[Token],
2048    functions: &[FunctionSpan],
2049    contexts: &PythonContexts,
2050    observations: &mut SourceObservationCollector<'_>,
2051) {
2052    for index in 0..tokens.len() {
2053        if !tokens[index].is_ident("return") && !tokens[index].is_ident("yield") {
2054            continue;
2055        }
2056        let Some(function) = innermost_function(functions, index) else {
2057            continue;
2058        };
2059        let end = python_expression_end(tokens, index + 1);
2060        let named = tokens
2061            .get(index + 1)
2062            .and_then(Token::ident)
2063            .filter(|_| end == index + 2)
2064            .and_then(|name| contexts.test_clients.get(name).copied());
2065        let Some(framework) = named.or_else(|| python_test_client(tokens, index + 1, end)) else {
2066            continue;
2067        };
2068        let mut observation = confirmed_test(
2069            SourceLanguage::Python,
2070            framework,
2071            functions[function].name.clone(),
2072            tokens[index].line,
2073            tokens[index].end_line,
2074        );
2075        observation.role = SourceRole::Client;
2076        observations.push(observation);
2077    }
2078}
2079
2080/// Normalized `keyword=` string of the first call between `start` and `end`.
2081fn python_constructor_prefix(
2082    tokens: &[Token],
2083    start: usize,
2084    end: usize,
2085    keyword: &str,
2086) -> Option<String> {
2087    let open = (start..end).find(|candidate| tokens[*candidate].is_punct('('))?;
2088    let close = matching(tokens, open, '(', ')')?;
2089    let prefix = parse_keyword_string_values(tokens, open, close, keyword)
2090        .into_iter()
2091        .next()?;
2092    Some(normalize_source_http_path(&prefix))
2093}
2094
2095fn python_imports_item(tokens: &[Token], module: &str, item: &str) -> bool {
2096    tokens.iter().enumerate().any(|(index, token)| {
2097        token.is_ident("from")
2098            && tokens
2099                .get(index + 1)
2100                .is_some_and(|token| token.is_ident(module))
2101            && tokens
2102                .get(index + 2)
2103                .is_some_and(|token| token.is_ident("import"))
2104            && (index + 3..tokens.len())
2105                .take_while(|candidate| tokens[*candidate].line == token.line)
2106                .any(|candidate| tokens[candidate].is_ident(item))
2107    })
2108}
2109
2110fn discover_python_direct_calls(
2111    tokens: &[Token],
2112    module: &str,
2113    framework: SourceFramework,
2114    calls: &mut BTreeMap<String, (SourceFramework, String)>,
2115) {
2116    for index in 0..tokens.len() {
2117        if !tokens[index].is_ident("from")
2118            || !tokens
2119                .get(index + 1)
2120                .is_some_and(|token| token.is_ident(module))
2121            || !tokens
2122                .get(index + 2)
2123                .is_some_and(|token| token.is_ident("import"))
2124        {
2125            continue;
2126        }
2127        let mut cursor = index + 3;
2128        while cursor < tokens.len() && tokens[cursor].line == tokens[index].line {
2129            let Some(imported) = tokens[cursor].ident() else {
2130                cursor += 1;
2131                continue;
2132            };
2133            if canonical_method(imported).is_none() && imported != "request" {
2134                cursor += 1;
2135                continue;
2136            }
2137            let (local, advance) = if tokens
2138                .get(cursor + 1)
2139                .is_some_and(|token| token.is_ident("as"))
2140            {
2141                (
2142                    tokens
2143                        .get(cursor + 2)
2144                        .and_then(Token::ident)
2145                        .unwrap_or(imported),
2146                    3,
2147                )
2148            } else {
2149                (imported, 1)
2150            };
2151            calls.insert(local.to_owned(), (framework, imported.to_owned()));
2152            cursor += advance;
2153        }
2154    }
2155}
2156
2157fn python_expression_end(tokens: &[Token], start: usize) -> usize {
2158    let mut round = 0_i32;
2159    let mut square = 0_i32;
2160    let mut curly = 0_i32;
2161    let mut previous_line = tokens.get(start).map_or(0, |token| token.line);
2162    for (index, token) in tokens.iter().enumerate().skip(start) {
2163        if index > start && token.line > previous_line && round == 0 && square == 0 && curly == 0 {
2164            return index;
2165        }
2166        if token.is_punct(';') && round == 0 && square == 0 && curly == 0 {
2167            return index;
2168        }
2169        match token.kind {
2170            TokenKind::Punct('(') => round += 1,
2171            TokenKind::Punct(')') => round -= 1,
2172            TokenKind::Punct('[') => square += 1,
2173            TokenKind::Punct(']') => square -= 1,
2174            TokenKind::Punct('{') => curly += 1,
2175            TokenKind::Punct('}') => curly -= 1,
2176            _ => {}
2177        }
2178        previous_line = token.line;
2179    }
2180    tokens.len()
2181}
2182
2183fn discover_python_import_aliases(tokens: &[Token], module: &str, aliases: &mut BTreeSet<String>) {
2184    for index in 0..tokens.len() {
2185        if !tokens[index].is_ident("import")
2186            || !tokens
2187                .get(index + 1)
2188                .is_some_and(|token| token.is_ident(module))
2189        {
2190            continue;
2191        }
2192        if tokens
2193            .get(index + 2)
2194            .is_some_and(|token| token.is_ident("as"))
2195            && let Some(alias) = tokens.get(index + 3).and_then(Token::ident)
2196        {
2197            aliases.insert(alias.to_owned());
2198        }
2199    }
2200}
2201
2202fn contains_any(tokens: &[Token], start: usize, end: usize, names: &BTreeSet<String>) -> bool {
2203    tokens.get(start..end).is_some_and(|slice| {
2204        slice
2205            .iter()
2206            .filter_map(Token::ident)
2207            .any(|name| names.contains(name))
2208    })
2209}
2210
2211fn python_static_string_expression(
2212    tokens: &[Token],
2213    start: usize,
2214    end: usize,
2215    constants: &BTreeMap<String, String>,
2216) -> Option<String> {
2217    let mut output = String::new();
2218    let mut found = false;
2219    for token in tokens.get(start..end)? {
2220        if let Some(value) = token.literal() {
2221            output.push_str(value);
2222            found = true;
2223        } else if let Some(name) = token.ident() {
2224            output.push_str(constants.get(name)?);
2225            found = true;
2226        } else if !matches!(token.kind, TokenKind::Punct('+' | '(' | ')' | '[' | ']')) {
2227            return None;
2228        }
2229    }
2230    found.then_some(output)
2231}
2232
2233fn python_functions(source: &str, tokens: &[Token]) -> Vec<FunctionSpan> {
2234    let line_count = saturating_u32(source.lines().count().max(1));
2235    let mut functions = Vec::new();
2236    for index in 0..tokens.len() {
2237        if !tokens[index].is_ident("def") {
2238            continue;
2239        }
2240        let Some(name) = tokens.get(index + 1).and_then(Token::ident) else {
2241            continue;
2242        };
2243        let indent = source_indent(source, tokens[index].line);
2244        let end_line = python_block_end(source, tokens[index].line, indent, line_count);
2245        let end_token = (index + 1..tokens.len())
2246            .find(|candidate| tokens[*candidate].line > end_line)
2247            .unwrap_or(tokens.len())
2248            .saturating_sub(1);
2249        functions.push(FunctionSpan {
2250            name: name.to_owned(),
2251            start_token: index,
2252            body_start_token: index,
2253            end_token,
2254        });
2255    }
2256    functions
2257}
2258
2259fn source_indent(source: &str, line: u32) -> usize {
2260    source
2261        .lines()
2262        .nth(line.saturating_sub(1) as usize)
2263        .map_or(0, |value| {
2264            value
2265                .chars()
2266                .take_while(|character| character.is_whitespace())
2267                .count()
2268        })
2269}
2270
2271fn python_block_end(source: &str, start: u32, indent: usize, fallback: u32) -> u32 {
2272    for (offset, line) in source.lines().enumerate().skip(start as usize) {
2273        let trimmed = line.trim();
2274        if trimmed.is_empty() || trimmed.starts_with('#') {
2275            continue;
2276        }
2277        let candidate_indent = line
2278            .chars()
2279            .take_while(|character| character.is_whitespace())
2280            .count();
2281        if candidate_indent <= indent {
2282            return saturating_u32(offset);
2283        }
2284    }
2285    fallback
2286}
2287
2288fn parse_python_routes(
2289    tokens: &[Token],
2290    contexts: &PythonContexts,
2291    observations: &mut SourceObservationCollector<'_>,
2292) {
2293    for index in 0..tokens.len() {
2294        if !tokens[index].is_punct('@') {
2295            continue;
2296        }
2297        let Some((receiver, decorator, open)) = python_decorator_call(tokens, index) else {
2298            continue;
2299        };
2300        let Some(close) = matching(tokens, open, '(', ')') else {
2301            continue;
2302        };
2303        let Some((name, signature_end)) = python_function_after(tokens, close) else {
2304            continue;
2305        };
2306        let framework = if contexts.fastapi_apps.contains(receiver) {
2307            SourceFramework::FastApi
2308        } else if contexts.flask_apps.contains(receiver) {
2309            SourceFramework::Flask
2310        } else {
2311            continue;
2312        };
2313        let path_start = top_level_arguments(tokens, open, close).first().copied();
2314        let resolved_path = path_start.and_then(|start| {
2315            python_static_string_expression(
2316                tokens,
2317                start,
2318                python_argument_end(tokens, start, close),
2319                &contexts.string_constants,
2320            )
2321        });
2322        let raw_path = resolved_path.or_else(|| {
2323            tokens
2324                .get(open + 1)
2325                .and_then(Token::literal)
2326                .map(str::to_owned)
2327        });
2328        let prefixed_path = raw_path.as_ref().map(|path| {
2329            contexts.route_prefixes.get(receiver).map_or_else(
2330                || path.clone(),
2331                |prefix| normalize_source_http_path(&format!("{prefix}{path}")),
2332            )
2333        });
2334        let literal = prefixed_path.as_deref();
2335        let methods = python_route_methods(tokens, open, close, decorator, framework);
2336        let router = Some(SymbolRef::Local(receiver.to_owned()));
2337        if methods.is_empty() {
2338            let mut observation = inexact_http(
2339                SourceLanguage::Python,
2340                framework,
2341                SourceRole::Provider,
2342                None,
2343                literal.and_then(route_literal_path),
2344                Some(name),
2345                SourceLineRange {
2346                    start: tokens[index].line,
2347                    end: tokens[signature_end].end_line,
2348                },
2349                SourceWarning::DynamicMethod,
2350            );
2351            observation.router = router;
2352            observations.push(observation);
2353        } else {
2354            for method in methods {
2355                let mut observation = http_from_literal(
2356                    SourceLanguage::Python,
2357                    framework,
2358                    SourceRole::Provider,
2359                    Some(method),
2360                    literal,
2361                    Some(name.clone()),
2362                    SourceLineRange {
2363                        start: tokens[index].line,
2364                        end: tokens[signature_end].end_line,
2365                    },
2366                    true,
2367                );
2368                observation.router.clone_from(&router);
2369                observations.push(observation);
2370            }
2371        }
2372    }
2373}
2374
2375/// Records `include_router` and `register_blueprint` mounts on known applications and routers.
2376fn parse_python_router_mounts(
2377    tokens: &[Token],
2378    contexts: &PythonContexts,
2379    observations: &mut SourceObservationCollector<'_>,
2380) {
2381    let imports = python_router_imports(tokens);
2382    for index in 0..tokens.len().saturating_sub(3) {
2383        let Some(receiver) = tokens[index].ident() else {
2384            continue;
2385        };
2386        let (framework, keyword) = match tokens[index + 2].ident() {
2387            Some("include_router") if contexts.fastapi_apps.contains(receiver) => {
2388                (SourceFramework::FastApi, "prefix")
2389            }
2390            Some("register_blueprint") if contexts.flask_apps.contains(receiver) => {
2391                (SourceFramework::Flask, "url_prefix")
2392            }
2393            _ => continue,
2394        };
2395        if !tokens[index + 1].is_punct('.') || !tokens[index + 3].is_punct('(') {
2396            continue;
2397        }
2398        let open = index + 3;
2399        let Some(close) = matching(tokens, open, '(', ')') else {
2400            continue;
2401        };
2402        let Some(&first) = top_level_arguments(tokens, open, close).first() else {
2403            continue;
2404        };
2405        let mut dotted = Vec::new();
2406        let mut cursor = first;
2407        while let Some(name) = tokens.get(cursor).and_then(Token::ident) {
2408            dotted.push(name);
2409            if !tokens
2410                .get(cursor + 1)
2411                .is_some_and(|token| token.is_punct('.'))
2412            {
2413                break;
2414            }
2415            cursor += 2;
2416        }
2417        let Some(child) = python_router_reference(&dotted, contexts, &imports) else {
2418            continue;
2419        };
2420        let prefix = parse_keyword_string_values(tokens, open, close, keyword)
2421            .into_iter()
2422            .next();
2423        observations.push(mount_observation(
2424            SourceLanguage::Python,
2425            framework,
2426            child,
2427            Some(SymbolRef::Local(receiver.to_owned())),
2428            prefix.as_deref(),
2429            SourceLineRange {
2430                start: tokens[index].line,
2431                end: tokens[close].end_line,
2432            },
2433        ));
2434    }
2435}
2436
2437/// Routes registered by `FastAPI` `add_api_route(path, endpoint)` and Flask
2438/// `add_url_rule(rule, endpoint, view_func)` calls.
2439fn parse_python_route_calls(
2440    tokens: &[Token],
2441    contexts: &PythonContexts,
2442    observations: &mut SourceObservationCollector<'_>,
2443) {
2444    for index in 0..tokens.len().saturating_sub(3) {
2445        let Some(receiver) = tokens[index].ident() else {
2446            continue;
2447        };
2448        if !tokens[index + 1].is_punct('.') || !tokens[index + 3].is_punct('(') {
2449            continue;
2450        }
2451        let (framework, path_keyword, handler_keyword, handler_position) =
2452            match tokens[index + 2].ident() {
2453                Some("add_api_route") if contexts.fastapi_apps.contains(receiver) => {
2454                    (SourceFramework::FastApi, "path", "endpoint", 1)
2455                }
2456                Some("add_url_rule") if contexts.flask_apps.contains(receiver) => {
2457                    (SourceFramework::Flask, "rule", "view_func", 2)
2458                }
2459                _ => continue,
2460            };
2461        let open = index + 3;
2462        let Some(close) = matching(tokens, open, '(', ')') else {
2463            continue;
2464        };
2465        let arguments = top_level_arguments(tokens, open, close)
2466            .into_iter()
2467            .map(|start| {
2468                let end = python_argument_end(tokens, start, close);
2469                match keyword_argument(tokens, start, end) {
2470                    Some((keyword, value)) => (Some(keyword), value, end),
2471                    None => (None, start, end),
2472                }
2473            })
2474            .collect::<Vec<_>>();
2475        let positional = arguments
2476            .iter()
2477            .filter(|(keyword, ..)| keyword.is_none())
2478            .collect::<Vec<_>>();
2479        let path = arguments
2480            .iter()
2481            .find(|(keyword, ..)| keyword.as_deref() == Some(path_keyword))
2482            .or_else(|| positional.first().copied())
2483            .and_then(|(_, start, end)| {
2484                python_static_string_expression(tokens, *start, *end, &contexts.string_constants)
2485            });
2486        let handler = arguments
2487            .iter()
2488            .find(|(keyword, ..)| keyword.as_deref() == Some(handler_keyword))
2489            .or_else(|| positional.get(handler_position).copied())
2490            .and_then(|(_, start, end)| {
2491                (*start..*end)
2492                    .map(|token| {
2493                        tokens[token]
2494                            .ident()
2495                            .or_else(|| tokens[token].is_punct('.').then_some("."))
2496                    })
2497                    .collect::<Option<String>>()
2498            });
2499        let mut methods =
2500            python_route_methods(tokens, open, close, "route", SourceFramework::Flask);
2501        if methods.is_empty() {
2502            methods.insert("GET".to_owned());
2503        }
2504        for method in methods {
2505            let mut observation = http_from_literal(
2506                SourceLanguage::Python,
2507                framework,
2508                SourceRole::Provider,
2509                Some(method),
2510                path.as_deref(),
2511                handler.clone(),
2512                SourceLineRange {
2513                    start: tokens[index].line,
2514                    end: tokens[close].end_line,
2515                },
2516                true,
2517            );
2518            observation.router = Some(SymbolRef::Local(receiver.to_owned()));
2519            observations.push(observation);
2520        }
2521    }
2522}
2523
2524fn python_router_reference(
2525    dotted: &[&str],
2526    contexts: &PythonContexts,
2527    imports: &BTreeMap<String, (String, String)>,
2528) -> Option<SymbolRef> {
2529    let (head, rest) = dotted.split_first()?;
2530    if let Some((module, name)) = imports.get(*head) {
2531        let name = std::iter::once(name.as_str())
2532            .filter(|name| !name.is_empty())
2533            .chain(rest.iter().copied())
2534            .collect::<Vec<_>>()
2535            .join(".");
2536        return (!name.is_empty()).then(|| SymbolRef::Import {
2537            module: module.clone(),
2538            name,
2539        });
2540    }
2541    (rest.is_empty()
2542        && (contexts.fastapi_apps.contains(*head) || contexts.flask_apps.contains(*head)))
2543    .then(|| SymbolRef::Local((*head).to_owned()))
2544}
2545
2546/// Maps local names to `(module, imported name)`; `import a.b as c` binds `c` to `(a.b, "")`.
2547fn python_router_imports(tokens: &[Token]) -> BTreeMap<String, (String, String)> {
2548    let mut imports = BTreeMap::new();
2549    for index in 0..tokens.len() {
2550        if tokens[index].is_ident("import")
2551            && (index == 0 || tokens[index - 1].line != tokens[index].line)
2552        {
2553            let mut cursor = index + 1;
2554            let mut module = String::new();
2555            while let Some(token) = tokens
2556                .get(cursor)
2557                .filter(|token| token.line == tokens[index].line)
2558            {
2559                match (token.ident(), token.is_punct('.')) {
2560                    (Some(name), _) if name != "as" => module.push_str(name),
2561                    (None, true) => module.push('.'),
2562                    _ => break,
2563                }
2564                cursor += 1;
2565            }
2566            if tokens.get(cursor).is_some_and(|token| token.is_ident("as"))
2567                && let Some(alias) = tokens.get(cursor + 1).and_then(Token::ident)
2568            {
2569                imports.insert(alias.to_owned(), (module, String::new()));
2570            }
2571            continue;
2572        }
2573        if !tokens[index].is_ident("from") {
2574            continue;
2575        }
2576        let Some(import_index) = (index + 1..tokens.len())
2577            .take_while(|candidate| tokens[*candidate].line == tokens[index].line)
2578            .find(|candidate| tokens[*candidate].is_ident("import"))
2579        else {
2580            continue;
2581        };
2582        let module = tokens[index + 1..import_index]
2583            .iter()
2584            .map(|token| token.ident().unwrap_or("."))
2585            .collect::<String>();
2586        let parenthesized = tokens
2587            .get(import_index + 1)
2588            .is_some_and(|token| token.is_punct('('));
2589        let end = if parenthesized {
2590            matching(tokens, import_index + 1, '(', ')').unwrap_or(import_index + 1)
2591        } else {
2592            (import_index + 1..tokens.len())
2593                .find(|candidate| tokens[*candidate].line != tokens[index].line)
2594                .unwrap_or(tokens.len())
2595        };
2596        let mut cursor = import_index + 1;
2597        while cursor < end {
2598            let Some(imported) = tokens[cursor].ident() else {
2599                cursor += 1;
2600                continue;
2601            };
2602            let aliased = tokens
2603                .get(cursor + 1)
2604                .is_some_and(|token| token.is_ident("as"));
2605            let local = if aliased {
2606                tokens
2607                    .get(cursor + 2)
2608                    .and_then(Token::ident)
2609                    .unwrap_or(imported)
2610            } else {
2611                imported
2612            };
2613            imports.insert(local.to_owned(), (module.clone(), imported.to_owned()));
2614            cursor += if aliased { 3 } else { 1 };
2615        }
2616    }
2617    imports
2618}
2619
2620fn python_decorator_call(tokens: &[Token], at: usize) -> Option<(&str, &str, usize)> {
2621    let mut names = Vec::new();
2622    let mut cursor = at.saturating_add(1);
2623    loop {
2624        names.push(tokens.get(cursor)?.ident()?);
2625        if tokens
2626            .get(cursor + 1)
2627            .is_some_and(|token| token.is_punct('.'))
2628        {
2629            cursor = cursor.saturating_add(2);
2630            continue;
2631        }
2632        break;
2633    }
2634    let open = cursor.saturating_add(1);
2635    if names.len() < 2 || !tokens.get(open).is_some_and(|token| token.is_punct('(')) {
2636        return None;
2637    }
2638    Some((names[names.len() - 2], names[names.len() - 1], open))
2639}
2640
2641fn python_argument_end(tokens: &[Token], start: usize, close: usize) -> usize {
2642    let mut round = 0_i32;
2643    let mut square = 0_i32;
2644    let mut curly = 0_i32;
2645    for (index, token) in tokens.iter().enumerate().take(close).skip(start) {
2646        match token.kind {
2647            TokenKind::Punct('(') => round += 1,
2648            TokenKind::Punct(')') => round -= 1,
2649            TokenKind::Punct('[') => square += 1,
2650            TokenKind::Punct(']') => square -= 1,
2651            TokenKind::Punct('{') => curly += 1,
2652            TokenKind::Punct('}') => curly -= 1,
2653            TokenKind::Punct(',') if round == 0 && square == 0 && curly == 0 => return index,
2654            _ => {}
2655        }
2656    }
2657    close
2658}
2659
2660fn python_function_after(tokens: &[Token], close: usize) -> Option<(String, usize)> {
2661    let mut index = close + 1;
2662    while index < tokens.len() && tokens[index].line <= tokens[close].line.saturating_add(12) {
2663        if tokens[index].is_ident("async") {
2664            index += 1;
2665            continue;
2666        }
2667        if tokens[index].is_ident("def") {
2668            let name = tokens.get(index + 1)?.ident()?.to_owned();
2669            let end = (index + 2..tokens.len())
2670                .find(|candidate| tokens[*candidate].is_punct(':'))
2671                .unwrap_or(index + 1);
2672            return Some((name, end));
2673        }
2674        if tokens[index].is_punct('@') {
2675            return None;
2676        }
2677        index += 1;
2678    }
2679    None
2680}
2681
2682fn python_route_methods(
2683    tokens: &[Token],
2684    open: usize,
2685    close: usize,
2686    decorator: &str,
2687    framework: SourceFramework,
2688) -> BTreeSet<String> {
2689    if let Some(method) = canonical_method(decorator) {
2690        return BTreeSet::from([method.to_owned()]);
2691    }
2692    let mut methods = parse_keyword_string_values(tokens, open, close, "methods")
2693        .into_iter()
2694        .filter_map(|method| canonical_method(&method).map(str::to_owned))
2695        .collect::<BTreeSet<_>>();
2696    if framework == SourceFramework::Flask && decorator == "route" && methods.is_empty() {
2697        methods.insert("GET".to_owned());
2698    }
2699    methods
2700}
2701
2702fn parse_python_http_registries(
2703    tokens: &[Token],
2704    observations: &mut SourceObservationCollector<'_>,
2705) {
2706    for index in 0..tokens.len() {
2707        let Some(name) = tokens[index].ident() else {
2708            continue;
2709        };
2710        if !name
2711            .chars()
2712            .all(|character| character.is_ascii_uppercase() || character == '_')
2713            || !tokens
2714                .get(index + 1)
2715                .is_some_and(|token| token.is_punct('='))
2716            || !tokens
2717                .get(index + 2)
2718                .is_some_and(|token| token.is_punct('{'))
2719        {
2720            continue;
2721        }
2722        let open = index + 2;
2723        let Some(close) = matching(tokens, open, '{', '}') else {
2724            continue;
2725        };
2726        parse_python_http_registry_dict(tokens, open, close, "", observations, 0);
2727    }
2728}
2729
2730fn parse_python_http_registry_dict(
2731    tokens: &[Token],
2732    open: usize,
2733    close: usize,
2734    prefix: &str,
2735    observations: &mut SourceObservationCollector<'_>,
2736    depth: usize,
2737) {
2738    if depth >= 32 {
2739        return;
2740    }
2741    let mut cursor = open.saturating_add(1);
2742    while cursor < close {
2743        while cursor < close && tokens[cursor].is_punct(',') {
2744            cursor += 1;
2745        }
2746        let Some(key) = tokens.get(cursor).and_then(Token::literal) else {
2747            cursor += 1;
2748            continue;
2749        };
2750        if !tokens
2751            .get(cursor + 1)
2752            .is_some_and(|token| token.is_punct(':'))
2753        {
2754            cursor += 1;
2755            continue;
2756        }
2757        let value_start = cursor + 2;
2758        let value_end = python_argument_end(tokens, value_start, close);
2759        parse_python_http_registry_value(
2760            tokens,
2761            value_start,
2762            value_end,
2763            prefix,
2764            key,
2765            observations,
2766            depth,
2767        );
2768        cursor = value_end.saturating_add(1);
2769    }
2770}
2771
2772fn parse_python_http_registry_value(
2773    tokens: &[Token],
2774    start: usize,
2775    end: usize,
2776    prefix: &str,
2777    key: &str,
2778    observations: &mut SourceObservationCollector<'_>,
2779    depth: usize,
2780) {
2781    if tokens.get(start).is_some_and(|token| token.is_punct('[')) {
2782        let Some(close) = matching(tokens, start, '[', ']').filter(|close| *close <= end) else {
2783            return;
2784        };
2785        let arguments = top_level_arguments(tokens, start, close);
2786        let Some(method) = arguments
2787            .first()
2788            .and_then(|argument| tokens.get(*argument))
2789            .and_then(Token::literal)
2790            .and_then(canonical_method)
2791        else {
2792            return;
2793        };
2794        let Some(path) = arguments
2795            .get(1)
2796            .and_then(|argument| tokens.get(*argument))
2797            .and_then(Token::literal)
2798        else {
2799            return;
2800        };
2801        let combined = canonical_python_registry_path(prefix, path);
2802        observations.push(http_from_literal(
2803            SourceLanguage::Python,
2804            SourceFramework::PythonHttpRegistry,
2805            SourceRole::Consumer,
2806            Some(method.to_owned()),
2807            Some(&combined),
2808            Some(key.to_owned()),
2809            SourceLineRange {
2810                start: tokens[start].line,
2811                end: tokens[close].end_line,
2812            },
2813            false,
2814        ));
2815        return;
2816    }
2817    if !tokens.get(start).is_some_and(|token| token.is_punct('(')) {
2818        return;
2819    }
2820    let Some(close) = matching(tokens, start, '(', ')').filter(|close| *close <= end) else {
2821        return;
2822    };
2823    let arguments = top_level_arguments(tokens, start, close);
2824    let Some(segment) = arguments
2825        .first()
2826        .and_then(|argument| tokens.get(*argument))
2827        .and_then(Token::literal)
2828    else {
2829        return;
2830    };
2831    let Some(dictionary) = arguments.get(1).copied().filter(|argument| {
2832        tokens
2833            .get(*argument)
2834            .is_some_and(|token| token.is_punct('{'))
2835    }) else {
2836        return;
2837    };
2838    let Some(dictionary_close) = matching(tokens, dictionary, '{', '}') else {
2839        return;
2840    };
2841    let nested_prefix = canonical_python_registry_path(prefix, segment);
2842    parse_python_http_registry_dict(
2843        tokens,
2844        dictionary,
2845        dictionary_close,
2846        &nested_prefix,
2847        observations,
2848        depth.saturating_add(1),
2849    );
2850}
2851
2852fn canonical_python_registry_path(prefix: &str, segment: &str) -> String {
2853    let joined = format!("{prefix}{segment}")
2854        .replace("{{", "{")
2855        .replace("}}", "}");
2856    normalize_source_http_path(&joined)
2857}
2858
2859fn parse_python_factories(
2860    source: &str,
2861    tokens: &[Token],
2862    observations: &mut SourceObservationCollector<'_>,
2863) {
2864    let imported_paths = python_imported_symbol_paths(tokens);
2865    let fallback = saturating_u32(source.lines().count().max(1));
2866    for index in 0..tokens.len() {
2867        if !tokens[index].is_ident("class") {
2868            continue;
2869        }
2870        let Some(factory_name) = tokens.get(index + 1).and_then(Token::ident) else {
2871            continue;
2872        };
2873        let signature_end = (index + 2..tokens.len())
2874            .find(|candidate| tokens[*candidate].is_punct(':'))
2875            .unwrap_or(index + 1);
2876        if !tokens
2877            .get(index + 2..signature_end)
2878            .is_some_and(|signature| {
2879                signature
2880                    .iter()
2881                    .filter_map(Token::ident)
2882                    .any(|base| base == "Factory" || base.ends_with("Factory"))
2883            })
2884        {
2885            continue;
2886        }
2887        let start_line = tokens[index].line;
2888        let end_line = python_block_end(
2889            source,
2890            start_line,
2891            source_indent(source, start_line),
2892            fallback,
2893        );
2894        let model_assignment = (signature_end + 1..tokens.len())
2895            .take_while(|candidate| tokens[*candidate].line <= end_line)
2896            .find_map(|candidate| {
2897                if tokens[candidate].is_ident("model")
2898                    && tokens
2899                        .get(candidate + 1)
2900                        .is_some_and(|token| token.is_punct('='))
2901                {
2902                    Some((
2903                        tokens.get(candidate + 2).and_then(Token::ident)?,
2904                        tokens[candidate].line,
2905                    ))
2906                } else {
2907                    None
2908                }
2909            });
2910        let Some((model_name, model_line)) = model_assignment else {
2911            continue;
2912        };
2913        observations.push(SourceObservation {
2914            language: SourceLanguage::Python,
2915            framework: SourceFramework::FactoryBoy,
2916            role: SourceRole::Factory,
2917            method: None,
2918            path: None,
2919            symbol_name: Some(factory_name.to_owned()),
2920            related_symbol: Some(model_name.to_owned()),
2921            related_path: imported_paths.get(model_name).cloned(),
2922            authority: None,
2923            router: None,
2924            mount_parent: None,
2925            url: None,
2926            call: None,
2927            lines: SourceLineRange {
2928                start: start_line,
2929                end: model_line,
2930            },
2931            status: SourceEpistemicStatus::Confirmed,
2932            confidence: 1.0,
2933            warnings: Vec::new(),
2934        });
2935    }
2936}
2937
2938fn python_imported_symbol_paths(tokens: &[Token]) -> BTreeMap<String, String> {
2939    let mut output = BTreeMap::new();
2940    for index in 0..tokens.len() {
2941        if !tokens[index].is_ident("from") {
2942            continue;
2943        }
2944        let import_index = (index + 1..tokens.len())
2945            .take_while(|candidate| tokens[*candidate].line == tokens[index].line)
2946            .find(|candidate| tokens[*candidate].is_ident("import"));
2947        let Some(import_index) = import_index else {
2948            continue;
2949        };
2950        let module = tokens[index + 1..import_index]
2951            .iter()
2952            .filter_map(Token::ident)
2953            .collect::<Vec<_>>()
2954            .join("/");
2955        if module.is_empty() {
2956            continue;
2957        }
2958        let target_path = format!("{module}.py");
2959        let mut cursor = import_index + 1;
2960        while cursor < tokens.len() && tokens[cursor].line == tokens[index].line {
2961            let Some(imported) = tokens[cursor].ident() else {
2962                cursor += 1;
2963                continue;
2964            };
2965            let (local, advance) = if tokens
2966                .get(cursor + 1)
2967                .is_some_and(|token| token.is_ident("as"))
2968            {
2969                (
2970                    tokens
2971                        .get(cursor + 2)
2972                        .and_then(Token::ident)
2973                        .unwrap_or(imported),
2974                    3,
2975                )
2976            } else {
2977                (imported, 1)
2978            };
2979            output.insert(local.to_owned(), target_path.clone());
2980            cursor += advance;
2981        }
2982    }
2983    output
2984}
2985
2986fn parse_python_http_calls(
2987    tokens: &[Token],
2988    functions: &[FunctionSpan],
2989    contexts: &PythonContexts,
2990    scopes: &PythonScopes<'_>,
2991    observations: &mut SourceObservationCollector<'_>,
2992) {
2993    let receivers = contexts.http_receivers();
2994    for index in 0..tokens.len() {
2995        let Some(receiver) = tokens[index].ident() else {
2996            continue;
2997        };
2998        let member = || {
2999            tokens
3000                .get(index + 1)
3001                .is_some_and(|token| token.is_punct('.'))
3002                .then(|| tokens.get(index + 2).and_then(Token::ident))
3003                .flatten()
3004        };
3005        let mut parameter = None;
3006        let (framework, call, open) = if let Some(framework) = receivers.get(receiver).copied() {
3007            let Some(call) = member() else {
3008                continue;
3009            };
3010            (framework, call, index + 3)
3011        } else if let Some((framework, call)) = contexts.direct_calls.get(receiver) {
3012            (*framework, call.as_str(), index + 1)
3013        } else if let Some(call) = member().filter(|call| canonical_method(call).is_some())
3014            && let Some(received) = python_received_client(tokens, index, scopes)
3015        {
3016            parameter = Some(received);
3017            (SourceFramework::TestClient, call, index + 3)
3018        } else {
3019            continue;
3020        };
3021        if !tokens.get(open).is_some_and(|token| token.is_punct('(')) {
3022            continue;
3023        }
3024        let Some(close) = matching(tokens, open, '(', ')') else {
3025            continue;
3026        };
3027        let arguments = top_level_arguments(tokens, open, close);
3028        let (method, path_argument) = if call == "request" {
3029            (
3030                arguments
3031                    .first()
3032                    .and_then(|argument| tokens.get(*argument))
3033                    .and_then(Token::literal)
3034                    .and_then(canonical_method)
3035                    .map(str::to_owned),
3036                arguments.get(1).copied(),
3037            )
3038        } else {
3039            (
3040                canonical_method(call).map(str::to_owned),
3041                arguments.first().copied(),
3042            )
3043        };
3044        if method.is_none() && call != "request" {
3045            continue;
3046        }
3047        let template = path_argument.map(|argument| {
3048            let end = python_argument_end(tokens, argument, close);
3049            let start = keyword_argument(tokens, argument, end)
3050                .filter(|(keyword, _)| keyword == "url")
3051                .map_or(argument, |(_, value)| value);
3052            scopes.template(start, end, index)
3053        });
3054        let literal = template.as_ref().and_then(UrlTemplate::client_literal);
3055        let mut observation = http_from_literal(
3056            SourceLanguage::Python,
3057            framework,
3058            SourceRole::Consumer,
3059            method,
3060            literal.as_deref(),
3061            enclosing_symbol(functions, index),
3062            SourceLineRange {
3063                start: tokens[index].line,
3064                end: tokens[close].end_line,
3065            },
3066            false,
3067        );
3068        observation.url = template.filter(UrlTemplate::has_parameters);
3069        if let Some(received) = parameter {
3070            if !literal.as_deref().is_some_and(|path| path.starts_with('/')) {
3071                continue;
3072            }
3073            observation.router = Some(received);
3074            observation.status = SourceEpistemicStatus::Ambiguous;
3075            observation.confidence = 0.0;
3076        }
3077        if call == "request" && observation.method.is_none() {
3078            observation.status = SourceEpistemicStatus::Incomplete;
3079            observation.confidence = 0.0;
3080            observation.warnings.push(SourceWarning::DynamicMethod);
3081            observation.warnings.sort();
3082            observation.warnings.dedup();
3083        }
3084        observations.push(observation);
3085    }
3086}
3087
3088/// Parameter of the enclosing function through which the call at `index` receives its client.
3089fn python_received_client(
3090    tokens: &[Token],
3091    index: usize,
3092    scopes: &PythonScopes<'_>,
3093) -> Option<SymbolRef> {
3094    if index > 0 && tokens[index - 1].is_punct('.') {
3095        return None;
3096    }
3097    let name = tokens[index].ident()?;
3098    let function = scopes.innermost(index)?;
3099    let position = scopes
3100        .parameters_of(function)
3101        .iter()
3102        .position(|parameter| parameter == name)?;
3103    Some(SymbolRef::Parameter {
3104        name: name.to_owned(),
3105        index: position,
3106    })
3107}
3108
3109fn parse_python_tests(
3110    source: &str,
3111    tokens: &[Token],
3112    observations: &mut SourceObservationCollector<'_>,
3113) {
3114    let unittest_classes = python_unittest_classes(source, tokens);
3115    let python_classes = python_class_ranges(source, tokens);
3116    for index in 0..tokens.len() {
3117        if !tokens[index].is_ident("def") {
3118            continue;
3119        }
3120        let Some(name) = tokens.get(index + 1).and_then(Token::ident) else {
3121            continue;
3122        };
3123        if !name.starts_with("test_") {
3124            continue;
3125        }
3126        let signature_end = (index + 2..tokens.len())
3127            .find(|candidate| tokens[*candidate].is_punct(':'))
3128            .unwrap_or(index + 1);
3129        let line = tokens[index].line;
3130        let is_unittest = unittest_classes
3131            .iter()
3132            .any(|range| range.start <= line && line <= range.end);
3133        let indent = source_indent(source, line);
3134        let is_class_method = python_classes.iter().any(|(range, body_indent)| {
3135            range.start <= line && line <= range.end && indent == *body_indent
3136        });
3137        if !is_unittest && indent != 0 && !is_class_method {
3138            continue;
3139        }
3140        observations.push(confirmed_test(
3141            SourceLanguage::Python,
3142            if is_unittest {
3143                SourceFramework::Unittest
3144            } else if is_class_method {
3145                SourceFramework::PythonTest
3146            } else {
3147                SourceFramework::Pytest
3148            },
3149            name.to_owned(),
3150            line,
3151            tokens[signature_end].end_line,
3152        ));
3153    }
3154}
3155
3156fn python_class_ranges(source: &str, tokens: &[Token]) -> Vec<(SourceLineRange, usize)> {
3157    let fallback = saturating_u32(source.lines().count().max(1));
3158    let mut classes = Vec::new();
3159    for token in tokens.iter().filter(|token| token.is_ident("class")) {
3160        let class_indent = source_indent(source, token.line);
3161        let end = python_block_end(source, token.line, class_indent, fallback);
3162        let body_indent = source
3163            .lines()
3164            .enumerate()
3165            .skip(usize::try_from(token.line).unwrap_or(usize::MAX))
3166            .take(usize::try_from(end.saturating_sub(token.line)).unwrap_or(usize::MAX))
3167            .find_map(|(index, line)| {
3168                let trimmed = line.trim();
3169                let line_number = saturating_u32(index + 1);
3170                let indent = source_indent(source, line_number);
3171                (!trimmed.is_empty() && !trimmed.starts_with('#') && indent > class_indent)
3172                    .then_some(indent)
3173            });
3174        if let Some(body_indent) = body_indent {
3175            classes.push((
3176                SourceLineRange {
3177                    start: token.line,
3178                    end,
3179                },
3180                body_indent,
3181            ));
3182        }
3183    }
3184    classes
3185}
3186
3187fn python_unittest_classes(source: &str, tokens: &[Token]) -> Vec<SourceLineRange> {
3188    if !has_ident(tokens, "unittest") {
3189        return Vec::new();
3190    }
3191    let fallback = saturating_u32(source.lines().count().max(1));
3192    let mut classes = Vec::new();
3193    for index in 0..tokens.len() {
3194        if !tokens[index].is_ident("class") {
3195            continue;
3196        }
3197        let signature_end = (index + 1..tokens.len())
3198            .find(|candidate| tokens[*candidate].is_punct(':'))
3199            .unwrap_or(index);
3200        if !contains_ident(tokens, index + 1, signature_end, "TestCase") {
3201            continue;
3202        }
3203        let start = tokens[index].line;
3204        classes.push(SourceLineRange {
3205            start,
3206            end: python_block_end(source, start, source_indent(source, start), fallback),
3207        });
3208    }
3209    classes
3210}
3211
3212fn saturating_u32(value: usize) -> u32 {
3213    u32::try_from(value).unwrap_or(u32::MAX)
3214}
3215
3216fn finish(mut observations: Vec<SourceObservation>) -> Vec<SourceObservation> {
3217    for observation in &mut observations {
3218        observation.warnings.sort();
3219        observation.warnings.dedup();
3220    }
3221    observations.sort_by(compare_observations);
3222    observations.dedup();
3223    observations
3224}
3225
3226fn compare_observations(left: &SourceObservation, right: &SourceObservation) -> Ordering {
3227    (
3228        left.lines,
3229        left.language,
3230        left.framework,
3231        left.role,
3232        &left.method,
3233        &left.path,
3234        &left.symbol_name,
3235        &left.related_symbol,
3236        &left.related_path,
3237        left.status,
3238        &left.warnings,
3239    )
3240        .cmp(&(
3241            right.lines,
3242            right.language,
3243            right.framework,
3244            right.role,
3245            &right.method,
3246            &right.path,
3247            &right.symbol_name,
3248            &right.related_symbol,
3249            &right.related_path,
3250            right.status,
3251            &right.warnings,
3252        ))
3253        .then_with(|| {
3254            (
3255                &left.authority,
3256                &left.router,
3257                &left.mount_parent,
3258                &left.url,
3259                &left.call,
3260            )
3261                .cmp(&(
3262                    &right.authority,
3263                    &right.router,
3264                    &right.mount_parent,
3265                    &right.url,
3266                    &right.call,
3267                ))
3268        })
3269}
3270
3271#[cfg(test)]
3272mod tests {
3273    use super::{
3274        SourceEpistemicStatus, SourceFramework, SourceObservation, SourceRole, SourceWarning, SymbolRef, UrlPart, parse_python_source
3275    };
3276
3277    /// HTTP, route, and test facts without call observations.
3278    fn parse_rust_source(source: &str) -> Vec<SourceObservation> {
3279        super::parse_rust_source(source)
3280            .into_iter()
3281            .filter(|item| item.role != SourceRole::Call)
3282            .collect()
3283    }
3284
3285    #[test]
3286    fn rust_reqwest_urls_should_resolve_constants_format_strings_and_wrapper_parameters() {
3287        let source = r#"
3288use reqwest::Client;
3289const BASE: &str = "http://orders:8080";
3290async fn get_order(client: &Client, id: u64) {
3291    client.get(format!("{BASE}/orders/{id}")).send().await;
3292}
3293async fn post_json(client: &Client, path: &str) {
3294    client.post(format!("{}{}", BASE, path)).send().await;
3295}
3296async fn refund(client: &Client, id: &str) {
3297    post_json(client, &format!("/orders/{id}/refunds")).await;
3298}
3299"#;
3300        let result = super::parse_rust_source(source);
3301        let consumers = result
3302            .iter()
3303            .filter(|item| item.role == SourceRole::Consumer)
3304            .map(|item| {
3305                (
3306                    item.symbol_name.as_deref(),
3307                    item.path.as_deref(),
3308                    item.url.is_some(),
3309                )
3310            })
3311            .collect::<Vec<_>>();
3312        let calls = result
3313            .iter()
3314            .filter_map(|item| item.call.as_ref())
3315            .map(|call| (call.callee.clone(), call.arguments.len()))
3316            .collect::<Vec<_>>();
3317
3318        assert_eq!(
3319            consumers,
3320            [
3321                (Some("get_order"), Some("/orders/{id}"), true),
3322                (Some("post_json"), None, true),
3323            ]
3324        );
3325        assert_eq!(calls, [(SymbolRef::Call("post_json".to_owned()), 2)]);
3326    }
3327
3328    #[test]
3329    fn axum_should_extract_multiline_route_and_handler() {
3330        let source = r#"
3331use axum::{Router, routing::post};
3332fn router() {
3333    Router::new().route(
3334        "/api//orders/",
3335        post(create_order),
3336    );
3337}
3338"#;
3339        let result = parse_rust_source(source);
3340
3341        assert!(matches!(
3342            result.as_slice(),
3343            [item]
3344                if item.framework == SourceFramework::Axum
3345                    && item.role == SourceRole::Provider
3346                    && item.method.as_deref() == Some("POST")
3347                    && item.path.as_deref() == Some("/api/orders")
3348                    && item.symbol_name.as_deref() == Some("create_order")
3349                    && item.lines.start == 4
3350                    && item.lines.end == 7
3351        ));
3352    }
3353
3354    #[test]
3355    fn actix_attributes_should_extract_direct_and_route_methods() {
3356        let source = r#"
3357use actix_web::{get, route};
3358#[get("/health")]
3359async fn health() {}
3360#[route(
3361    "/orders",
3362    method = "POST",
3363)]
3364async fn create() {}
3365"#;
3366        let result = parse_rust_source(source);
3367
3368        assert_eq!(
3369            result
3370                .iter()
3371                .map(|item| (item.method.as_deref(), item.path.as_deref()))
3372                .collect::<Vec<_>>(),
3373            vec![
3374                (Some("GET"), Some("/health")),
3375                (Some("POST"), Some("/orders"))
3376            ]
3377        );
3378    }
3379
3380    #[test]
3381    fn actix_builder_should_require_web_route_evidence() {
3382        let source = r#"
3383use actix_web::{web, App};
3384fn app() {
3385    App::new().route("/users", web::put().to(update_user));
3386    App::new().service(
3387        web::resource("/orders").route(web::post().to(create_order)),
3388    );
3389}
3390"#;
3391        let result = parse_rust_source(source);
3392
3393        assert_eq!(
3394            result
3395                .iter()
3396                .filter(|item| item.role == SourceRole::Provider)
3397                .map(|item| (
3398                    item.framework,
3399                    item.method.as_deref(),
3400                    item.symbol_name.as_deref()
3401                ))
3402                .collect::<Vec<_>>(),
3403            vec![
3404                (SourceFramework::ActixWeb, Some("PUT"), Some("update_user")),
3405                (
3406                    SourceFramework::ActixWeb,
3407                    Some("POST"),
3408                    Some("create_order")
3409                ),
3410            ]
3411        );
3412    }
3413
3414    #[test]
3415    fn utoipa_should_extract_explicit_and_contextual_actix_paths() {
3416        let source = r#"
3417use actix_web::{get, post};
3418
3419#[cfg_attr(feature = "openapi", utoipa::path(post, path = "/api/orders"))]
3420#[post("/orders")]
3421async fn create_order() {}
3422
3423#[cfg_attr(feature = "openapi", utoipa::path(
3424    get,
3425    context_path = "/api/users"
3426))]
3427#[get("/{id}")]
3428async fn get_user() {}
3429"#;
3430        let result = parse_rust_source(source);
3431
3432        assert!(result.iter().any(|item| {
3433            item.framework == SourceFramework::Utoipa
3434                && item.symbol_name.as_deref() == Some("create_order")
3435                && item.method.as_deref() == Some("POST")
3436                && item.path.as_deref() == Some("/api/orders")
3437        }));
3438        assert!(result.iter().any(|item| {
3439            item.framework == SourceFramework::Utoipa
3440                && item.symbol_name.as_deref() == Some("get_user")
3441                && item.method.as_deref() == Some("GET")
3442                && item.path.as_deref() == Some("/api/users/{id}")
3443        }));
3444        assert!(result.iter().any(|item| {
3445            item.framework == SourceFramework::ActixWeb
3446                && item.symbol_name.as_deref() == Some("create_order")
3447                && item.path.as_deref() == Some("/orders")
3448                && (item.confidence - 1.0).abs() < f32::EPSILON
3449        }));
3450        assert!(result.iter().any(|item| {
3451            item.framework == SourceFramework::Utoipa
3452                && item.symbol_name.as_deref() == Some("create_order")
3453                && (item.confidence - 0.75).abs() < f32::EPSILON
3454                && item.warnings.contains(&SourceWarning::AdvisoryDeclaration)
3455        }));
3456    }
3457
3458    #[test]
3459    fn reqwest_should_extract_convenience_client_and_request_calls() {
3460        let source = r#"
3461use reqwest::{Client, Method};
3462async fn send() {
3463    reqwest::get("https://example.test/health?full=1").await;
3464    reqwest::blocking::get("https://example.test/ready");
3465    let client = Client::new();
3466    client.post("/orders").send().await;
3467    client.request(Method::DELETE, "https://example.test/orders/7").send().await;
3468}
3469"#;
3470        let result = parse_rust_source(source);
3471
3472        assert_eq!(
3473            result
3474                .iter()
3475                .map(|item| (item.method.as_deref(), item.path.as_deref()))
3476                .collect::<Vec<_>>(),
3477            vec![
3478                (Some("GET"), Some("/health")),
3479                (Some("GET"), Some("/ready")),
3480                (Some("POST"), Some("/orders")),
3481                (Some("DELETE"), Some("/orders/7")),
3482            ]
3483        );
3484    }
3485
3486    #[test]
3487    fn reqwest_should_recognize_injected_client_parameters() {
3488        let source = r#"
3489use reqwest::Client;
3490async fn synchronize(client: &Client, qualified: &reqwest::Client) {
3491    client.post("https://orders.test/v1/orders").send().await;
3492    qualified.get("https://payments.test/v1/payments").send().await;
3493}
3494"#;
3495        let result = parse_rust_source(source);
3496
3497        assert_eq!(
3498            result
3499                .iter()
3500                .map(|item| (item.method.as_deref(), item.path.as_deref()))
3501                .collect::<Vec<_>>(),
3502            vec![
3503                (Some("POST"), Some("/v1/orders")),
3504                (Some("GET"), Some("/v1/payments")),
3505            ]
3506        );
3507    }
3508
3509    #[test]
3510    fn reqwest_should_preserve_explicit_lifetimes_in_client_parameters() {
3511        let source = r#"
3512use reqwest::Client;
3513async fn synchronize<'request>(client: &'request Client) {
3514    client.post("https://orders.test/v1/orders").send().await;
3515}
3516"#;
3517        let result = parse_rust_source(source);
3518
3519        assert!(result.iter().any(|item| {
3520            item.framework == SourceFramework::Reqwest
3521                && item.method.as_deref() == Some("POST")
3522                && item.path.as_deref() == Some("/v1/orders")
3523        }));
3524    }
3525
3526    #[test]
3527    fn absolute_consumer_url_should_record_its_authority() {
3528        let source = r#"
3529use reqwest::Client;
3530async fn synchronize(client: &Client) {
3531    client.get("https://User@Third-Party.example:8443/health?probe=1").send().await;
3532    client.get("/internal-health").send().await;
3533}
3534"#;
3535        let result = parse_rust_source(source);
3536        let external = result
3537            .iter()
3538            .find(|item| item.path.as_deref() == Some("/health"))
3539            .expect("absolute call");
3540        let internal = result
3541            .iter()
3542            .find(|item| item.path.as_deref() == Some("/internal-health"))
3543            .expect("relative call");
3544
3545        assert_eq!(external.status, SourceEpistemicStatus::Confirmed);
3546        assert_eq!(
3547            external.authority.as_deref(),
3548            Some("third-party.example:8443")
3549        );
3550        assert_eq!(external.warnings, []);
3551        assert_eq!(internal.status, SourceEpistemicStatus::Confirmed);
3552        assert_eq!(internal.authority, None);
3553    }
3554
3555    #[test]
3556    fn reqwest_should_recognize_builder_chains_and_typed_self_fields() {
3557        let source = r#"
3558use reqwest::Client;
3559use std::time::Duration;
3560
3561struct Api {
3562    client: Client,
3563}
3564
3565impl Api {
3566    fn new() -> Self {
3567        let client = Client::builder()
3568            .timeout(Duration::from_secs(5))
3569            .build()
3570            .expect("valid client configuration");
3571        Self { client }
3572    }
3573
3574    async fn synchronize(&self) {
3575        self.client.get("https://inventory.test/v1/inventory").send().await;
3576    }
3577}
3578"#;
3579        let result = parse_rust_source(source);
3580
3581        assert!(matches!(
3582            result.as_slice(),
3583            [observation]
3584                if observation.method.as_deref() == Some("GET")
3585                    && observation.path.as_deref() == Some("/v1/inventory")
3586        ));
3587    }
3588
3589    #[test]
3590    fn unrelated_grouped_import_should_not_promote_client_to_reqwest() {
3591        let source = r#"
3592use my_http::{reqwest, Client};
3593async fn diagnostic() {
3594    let client = Client::new();
3595    client.get("https://inventory.test/v1/diagnostic");
3596}
3597"#;
3598        let result = parse_rust_source(source);
3599
3600        assert_eq!(result.len(), 0);
3601    }
3602
3603    #[test]
3604    fn reqwest_should_not_reuse_receiver_names_across_functions() {
3605        let source = r#"
3606use reqwest::Client;
3607struct FakeClient;
3608impl FakeClient { fn get(&self, _path: &str) {} }
3609fn local_only(client: &FakeClient) {
3610    client.get("https://inventory.test/v1/diagnostic");
3611}
3612async fn remote() {
3613    let client = Client::new();
3614    client.get("https://inventory.test/v1/inventory").send().await;
3615}
3616"#;
3617        let result = parse_rust_source(source);
3618
3619        assert_eq!(result.len(), 1);
3620        assert_eq!(result[0].path.as_deref(), Some("/v1/inventory"));
3621    }
3622
3623    #[test]
3624    fn reqwest_import_should_not_leak_across_inline_modules() {
3625        let source = r#"
3626mod real { use reqwest::Client; }
3627mod fake {
3628    struct Client;
3629    impl Client { fn get(&self, _path: &str) {} }
3630    fn probe(client: &Client) { client.get("/health"); }
3631}
3632"#;
3633
3634        assert_eq!(parse_rust_source(source), []);
3635    }
3636
3637    #[test]
3638    fn top_level_reqwest_import_should_survive_an_inline_test_module() {
3639        let source = r#"
3640use reqwest::Client;
3641async fn probe() {
3642    let client = Client::new();
3643    client.get("/health").send().await;
3644}
3645#[cfg(test)] mod tests { fn smoke() {} }
3646"#;
3647
3648        assert!(parse_rust_source(source).iter().any(|item| {
3649            item.framework == SourceFramework::Reqwest && item.path.as_deref() == Some("/health")
3650        }));
3651    }
3652
3653    #[test]
3654    fn reqwest_import_inside_inline_module_should_remain_visible_locally() {
3655        let source = r#"
3656mod api {
3657    use reqwest::Client as HttpClient;
3658    async fn probe() {
3659        let client = HttpClient::new();
3660        client.get("/health").send().await;
3661    }
3662}
3663"#;
3664
3665        assert!(parse_rust_source(source).iter().any(|item| {
3666            item.framework == SourceFramework::Reqwest && item.path.as_deref() == Some("/health")
3667        }));
3668    }
3669
3670    #[test]
3671    fn reqwest_self_fields_should_not_leak_across_sibling_modules() {
3672        let source = r#"
3673mod real {
3674    use reqwest::Client;
3675    struct Api { client: Client }
3676    impl Api {
3677        async fn probe(&self) { self.client.get("/real").send().await; }
3678    }
3679}
3680mod fake {
3681    struct Client;
3682    impl Client { fn get(&self, _path: &str) {} }
3683    struct Api { client: Client }
3684    impl Api {
3685        fn probe(&self) { self.client.get("/fake"); }
3686    }
3687}
3688"#;
3689
3690        let result = parse_rust_source(source);
3691        assert_eq!(result.len(), 1);
3692        assert_eq!(result[0].path.as_deref(), Some("/real"));
3693    }
3694
3695    #[test]
3696    fn reqwest_should_respect_lexical_shadowing() {
3697        let source = r#"
3698use reqwest::Client;
3699struct FakeClient;
3700impl FakeClient { fn get(&self, _path: &str) {} }
3701async fn synchronize() {
3702    let client = Client::new();
3703    client.get("https://inventory.test/v1/inventory").send().await;
3704    {
3705        let client = FakeClient;
3706        client.get("https://inventory.test/v1/diagnostic");
3707    }
3708    client.get("https://payments.test/v1/payments").send().await;
3709}
3710"#;
3711        let result = parse_rust_source(source);
3712
3713        assert_eq!(
3714            result
3715                .iter()
3716                .map(|item| item.path.as_deref())
3717                .collect::<Vec<_>>(),
3718            vec![Some("/v1/inventory"), Some("/v1/payments")]
3719        );
3720    }
3721
3722    #[test]
3723    fn reqwest_should_not_promote_wrappers_that_consume_a_client() {
3724        let source = r#"
3725use reqwest::Client;
3726struct FakeClient;
3727impl FakeClient {
3728    fn new(_client: Client) -> Self { Self }
3729    fn get(&self, _path: &str) {}
3730}
3731async fn diagnostic() {
3732    let fake = FakeClient::new(Client::new());
3733    fake.get("https://inventory.test/v1/diagnostic");
3734}
3735"#;
3736        let result = parse_rust_source(source);
3737
3738        assert_eq!(result.len(), 0);
3739    }
3740
3741    #[test]
3742    fn reqwest_should_recognize_import_aliases_and_exact_type_annotations() {
3743        let source = r#"
3744use reqwest::Client as HttpClient;
3745async fn synchronize(injected: &HttpClient) {
3746    let constructed: HttpClient = factory();
3747    injected.get("https://inventory.test/v1/inventory").send().await;
3748    constructed.post("https://orders.test/v1/orders").send().await;
3749}
3750"#;
3751        let result = parse_rust_source(source);
3752
3753        assert_eq!(
3754            result
3755                .iter()
3756                .map(|item| (item.method.as_deref(), item.path.as_deref()))
3757                .collect::<Vec<_>>(),
3758            vec![
3759                (Some("GET"), Some("/v1/inventory")),
3760                (Some("POST"), Some("/v1/orders")),
3761            ]
3762        );
3763    }
3764
3765    #[test]
3766    fn reqwest_should_recognize_blocking_clients_and_generic_self_fields() {
3767        let source = r#"
3768use reqwest::blocking::Client as BlockingClient;
3769use reqwest::blocking::{Client as GroupedClient, Response};
3770struct Api<T> { client: BlockingClient, marker: T }
3771impl<T> Api<T> {
3772    fn synchronize(&self) {
3773        self.client.get("https://inventory.test/v1/inventory").send();
3774    }
3775}
3776fn qualified() {
3777    let client = reqwest::blocking::Client::new();
3778    client.post("https://orders.test/v1/orders").send();
3779}
3780fn grouped() {
3781    let client = GroupedClient::new();
3782    client.delete("https://orders.test/v1/obsolete").send();
3783}
3784"#;
3785        let result = parse_rust_source(source);
3786
3787        assert_eq!(result.len(), 3);
3788        assert!(
3789            result
3790                .iter()
3791                .any(|item| item.path.as_deref() == Some("/v1/inventory"))
3792        );
3793        assert!(
3794            result
3795                .iter()
3796                .any(|item| item.path.as_deref() == Some("/v1/orders"))
3797        );
3798        assert!(
3799            result
3800                .iter()
3801                .any(|item| item.path.as_deref() == Some("/v1/obsolete"))
3802        );
3803    }
3804
3805    #[test]
3806    fn reqwest_should_recognize_exact_local_type_aliases() {
3807        let source = r#"
3808use reqwest::Client as HttpClient;
3809type ServiceClient = HttpClient;
3810async fn synchronize(client: &ServiceClient) {
3811    client.get("https://inventory.test/v1/inventory").send().await;
3812}
3813"#;
3814        let result = parse_rust_source(source);
3815
3816        assert!(matches!(
3817            result.as_slice(),
3818            [observation]
3819                if observation.method.as_deref() == Some("GET")
3820                    && observation.path.as_deref() == Some("/v1/inventory")
3821        ));
3822    }
3823
3824    #[test]
3825    fn reqwest_should_track_braced_typed_closure_parameters() {
3826        let source = r#"
3827use reqwest::Client;
3828async fn synchronize() {
3829    let operation = |client: &Client| {
3830        client.get("https://inventory.test/v1/inventory");
3831    };
3832}
3833"#;
3834        let result = parse_rust_source(source);
3835
3836        assert!(matches!(
3837            result.as_slice(),
3838            [observation] if observation.path.as_deref() == Some("/v1/inventory")
3839        ));
3840    }
3841
3842    #[test]
3843    fn reqwest_should_treat_destructuring_as_lexical_shadowing() {
3844        let source = r#"
3845use reqwest::Client;
3846struct FakeClient;
3847impl FakeClient { fn get(&self, _path: &str) {} }
3848async fn synchronize(client: &Client, pair: (FakeClient, usize)) {
3849    let (client, _value) = pair;
3850    client.get("https://inventory.test/v1/diagnostic");
3851}
3852"#;
3853        let result = parse_rust_source(source);
3854
3855        assert_eq!(result.len(), 0);
3856    }
3857
3858    #[test]
3859    fn reqwest_should_conservatively_handle_binding_reassignment() {
3860        let source = r#"
3861use reqwest::Client;
3862async fn synchronize() {
3863    let mut client = Client::new();
3864    client = factory();
3865    client.get("https://inventory.test/v1/unknown");
3866    client = Client::new();
3867    client.get("https://inventory.test/v1/inventory");
3868}
3869"#;
3870        let result = parse_rust_source(source);
3871
3872        assert_eq!(
3873            result
3874                .iter()
3875                .map(|item| item.path.as_deref())
3876                .collect::<Vec<_>>(),
3877            vec![Some("/v1/inventory")]
3878        );
3879    }
3880
3881    #[test]
3882    fn rust_test_attributes_should_cover_builtin_tokio_and_rstest() {
3883        let source = r"
3884#[test]
3885fn plain() {}
3886#[tokio::test]
3887async fn asynchronous() {}
3888#[rstest]
3889#[case(1)]
3890fn parameterized(#[case] value: u8) {}
3891";
3892        let result = parse_rust_source(source);
3893
3894        assert_eq!(
3895            result
3896                .iter()
3897                .map(|item| (item.framework, item.symbol_name.as_deref()))
3898                .collect::<Vec<_>>(),
3899            vec![
3900                (SourceFramework::RustTest, Some("plain")),
3901                (SourceFramework::TokioTest, Some("asynchronous")),
3902                (SourceFramework::Rstest, Some("parameterized")),
3903            ]
3904        );
3905    }
3906
3907    #[test]
3908    fn rust_dynamic_route_should_never_claim_exact_path() {
3909        let source = r"
3910use axum::{Router, routing::get};
3911fn router(path: &str) {
3912    Router::new().route(path, get(handler));
3913}
3914";
3915        let result = parse_rust_source(source);
3916
3917        assert!(matches!(
3918            result.as_slice(),
3919            [item]
3920                if item.path.is_none()
3921                    && item.status == SourceEpistemicStatus::Ambiguous
3922                    && item.warnings == [SourceWarning::DynamicPath]
3923        ));
3924    }
3925
3926    #[test]
3927    fn rust_comments_strings_and_unrelated_route_methods_should_be_ignored() {
3928        let source = r##"
3929// use axum; Router::new().route("/fake", get(fake));
3930const TEXT: &str = r#"reqwest::get("https://example.test/fake")"#;
3931struct Router;
3932impl Router {
3933    fn route(&self, path: &str, handler: usize) {}
3934}
3935fn ordinary() {
3936    Router.route("/fake", handler);
3937}
3938"##;
3939        let result = parse_rust_source(source);
3940
3941        assert_eq!(result, Vec::new());
3942    }
3943
3944    #[test]
3945    fn fastapi_should_extract_method_and_api_route_decorators() {
3946        let source = r#"
3947from fastapi import FastAPI
3948app = FastAPI()
3949@app.get("/users/{user_id}")
3950async def get_user(user_id: str):
3951    pass
3952@app.api_route(
3953    "/orders",
3954    methods=["POST", "PUT"],
3955)
3956def orders():
3957    pass
3958"#;
3959        let result = parse_python_source(source);
3960
3961        assert_eq!(
3962            result
3963                .iter()
3964                .filter(|item| item.role == SourceRole::Provider)
3965                .map(|item| (item.method.as_deref(), item.path.as_deref()))
3966                .collect::<Vec<_>>(),
3967            vec![
3968                (Some("GET"), Some("/users/{user_id}")),
3969                (Some("POST"), Some("/orders")),
3970                (Some("PUT"), Some("/orders")),
3971            ]
3972        );
3973    }
3974
3975    #[test]
3976    fn fastapi_should_compose_static_router_prefixes() {
3977        let source = r#"
3978from fastapi import APIRouter
3979router = APIRouter(prefix="/api/v1/projects")
3980
3981@router.get("/{project_id}")
3982async def get_project(project_id: int):
3983    pass
3984"#;
3985        let result = parse_python_source(source);
3986
3987        assert!(result.iter().any(|item| {
3988            item.framework == SourceFramework::FastApi
3989                && item.method.as_deref() == Some("GET")
3990                && item.path.as_deref() == Some("/api/v1/projects/{project_id}")
3991                && item.symbol_name.as_deref() == Some("get_project")
3992        }));
3993    }
3994
3995    #[test]
3996    fn flask_should_extract_default_and_explicit_methods() {
3997        let source = r#"
3998from flask import Flask
3999app = Flask(__name__)
4000@app.route("/health")
4001def health():
4002    pass
4003@app.route(
4004    "/orders",
4005    methods=["POST", "DELETE"],
4006)
4007def orders():
4008    pass
4009"#;
4010        let result = parse_python_source(source);
4011
4012        assert_eq!(
4013            result
4014                .iter()
4015                .filter(|item| item.role == SourceRole::Provider)
4016                .map(|item| item.method.as_deref())
4017                .collect::<Vec<_>>(),
4018            vec![Some("GET"), Some("DELETE"), Some("POST")]
4019        );
4020    }
4021
4022    #[test]
4023    fn flask_should_resolve_instance_app_and_static_path_concatenation() {
4024        let source = r#"
4025from flask import Flask
4026URL = "/servers/remitee"
4027
4028class Remitee:
4029    def __init__(self):
4030        self.app = Flask(__name__)
4031
4032    def setup_routes(self):
4033        @self.app.route(URL + "/api/Payments/<payment_id>", methods=["GET"])
4034        def payment(payment_id):
4035            pass
4036"#;
4037        let result = parse_python_source(source);
4038
4039        assert!(result.iter().any(|item| {
4040            item.framework == SourceFramework::Flask
4041                && item.role == SourceRole::Provider
4042                && item.method.as_deref() == Some("GET")
4043                && item.path.as_deref() == Some("/servers/remitee/api/Payments/<payment_id>")
4044        }));
4045    }
4046
4047    #[test]
4048    fn python_http_registry_should_flatten_nested_static_operations() {
4049        let source = r#"
4050METHODS = {
4051    "public": ("/api/v1", {
4052        "accounts": ("/accounts", {
4053            "get_accounts": ["GET", ""],
4054            "get_account": ["GET", "/{{accounts_id}}"],
4055        }),
4056    }),
4057}
4058"#;
4059        let result = parse_python_source(source);
4060        let operations = result
4061            .iter()
4062            .filter(|item| item.framework == SourceFramework::PythonHttpRegistry)
4063            .map(|item| (item.method.as_deref(), item.path.as_deref()))
4064            .collect::<Vec<_>>();
4065
4066        assert_eq!(
4067            operations,
4068            vec![
4069                (Some("GET"), Some("/api/v1/accounts")),
4070                (Some("GET"), Some("/api/v1/accounts/{accounts_id}")),
4071            ]
4072        );
4073    }
4074
4075    #[test]
4076    fn factory_boy_should_link_factory_to_imported_model() {
4077        let source = r"
4078import factory
4079from src.clases.Account import Account
4080
4081class AccountFactory(factory.Factory):
4082    class Meta:
4083        model = Account
4084";
4085        let result = parse_python_source(source);
4086
4087        assert!(result.iter().any(|item| {
4088            item.framework == SourceFramework::FactoryBoy
4089                && item.role == SourceRole::Factory
4090                && item.symbol_name.as_deref() == Some("AccountFactory")
4091                && item.related_symbol.as_deref() == Some("Account")
4092                && item.related_path.as_deref() == Some("src/clases/Account.py")
4093                && item.status == SourceEpistemicStatus::Confirmed
4094        }));
4095    }
4096
4097    #[test]
4098    fn requests_should_extract_module_alias_and_session_calls() {
4099        let source = r#"
4100import requests as rq
4101from requests import Session, post as create
4102def send():
4103    rq.get("https://example.test/status")
4104    create("/orders")
4105    session = Session()
4106    session.request("PATCH", "/orders/4")
4107"#;
4108        let result = parse_python_source(source);
4109
4110        assert_eq!(
4111            result
4112                .iter()
4113                .map(|item| (item.framework, item.method.as_deref(), item.path.as_deref()))
4114                .collect::<Vec<_>>(),
4115            vec![
4116                (SourceFramework::Requests, Some("GET"), Some("/status")),
4117                (SourceFramework::Requests, Some("POST"), Some("/orders")),
4118                (SourceFramework::Requests, Some("PATCH"), Some("/orders/4")),
4119            ]
4120        );
4121    }
4122
4123    #[test]
4124    fn aiohttp_should_extract_client_session_context_calls() {
4125        let source = r#"
4126from aiohttp import ClientSession
4127
4128async def load():
4129    async with ClientSession() as session:
4130        await session.get("/accounts")
4131"#;
4132        let result = parse_python_source(source);
4133
4134        assert!(result.iter().any(|item| {
4135            item.framework == SourceFramework::AioHttp
4136                && item.role == SourceRole::Consumer
4137                && item.method.as_deref() == Some("GET")
4138                && item.path.as_deref() == Some("/accounts")
4139        }));
4140    }
4141
4142    #[test]
4143    fn httpx_should_extract_module_sync_and_async_clients() {
4144        let source = r#"
4145import httpx
4146from httpx import AsyncClient, patch as update
4147def direct():
4148    httpx.delete("https://example.test/items/1")
4149    update("/items/1")
4150async def clients():
4151    client = AsyncClient()
4152    client.put("/items/1")
4153"#;
4154        let result = parse_python_source(source);
4155
4156        assert_eq!(
4157            result
4158                .iter()
4159                .map(|item| (item.framework, item.method.as_deref()))
4160                .collect::<Vec<_>>(),
4161            vec![
4162                (SourceFramework::Httpx, Some("DELETE")),
4163                (SourceFramework::Httpx, Some("PATCH")),
4164                (SourceFramework::Httpx, Some("PUT")),
4165            ]
4166        );
4167    }
4168
4169    #[test]
4170    fn python_tests_should_distinguish_pytest_and_unittest() {
4171        let source = r"
4172import unittest
4173def test_pytest_case():
4174    pass
4175class ApiTests(unittest.TestCase):
4176    def test_unittest_case(self):
4177        pass
4178";
4179        let result = parse_python_source(source);
4180
4181        assert_eq!(
4182            result
4183                .iter()
4184                .map(|item| (item.framework, item.symbol_name.as_deref()))
4185                .collect::<Vec<_>>(),
4186            vec![
4187                (SourceFramework::Pytest, Some("test_pytest_case")),
4188                (SourceFramework::Unittest, Some("test_unittest_case")),
4189            ]
4190        );
4191    }
4192
4193    #[test]
4194    fn indirect_python_test_class_should_preserve_generic_test_evidence() {
4195        let source = r#"
4196import requests
4197
4198class PaymentCases(SharedTestBase):
4199    def test_payment(self):
4200        requests.post("/payments")
4201"#;
4202        let result = parse_python_source(source);
4203
4204        assert!(result.iter().any(|item| {
4205            item.framework == SourceFramework::PythonTest
4206                && item.role == SourceRole::Test
4207                && item.symbol_name.as_deref() == Some("test_payment")
4208        }));
4209        assert!(result.iter().any(|item| {
4210            item.framework == SourceFramework::Requests
4211                && item.role == SourceRole::Consumer
4212                && item.symbol_name.as_deref() == Some("test_payment")
4213                && item.path.as_deref() == Some("/payments")
4214        }));
4215    }
4216
4217    #[test]
4218    fn python_dynamic_url_and_method_should_remain_explicitly_incomplete() {
4219        let source = r"
4220import httpx
4221def send(method, base, path):
4222    httpx.request(method, base + path)
4223";
4224        let result = parse_python_source(source);
4225
4226        assert!(matches!(
4227            result.as_slice(),
4228            [item]
4229                if item.method.is_none()
4230                    && item.path.is_none()
4231                    && item.status == SourceEpistemicStatus::Incomplete
4232                    && item.warnings
4233                        == [SourceWarning::DynamicMethod, SourceWarning::DynamicPath]
4234        ));
4235    }
4236
4237    fn consumer_paths(source: &str) -> Vec<(Option<String>, Option<String>, bool)> {
4238        parse_python_source(source)
4239            .into_iter()
4240            .filter(|item| item.role == SourceRole::Consumer)
4241            .map(|item| (item.symbol_name, item.path, item.url.is_some()))
4242            .collect()
4243    }
4244
4245    #[test]
4246    fn python_url_expressions_should_resolve_whole_segment_values_and_scoped_names() {
4247        let source = r#"
4248import requests
4249BASE_URL = "http://orders:8080"
4250API = BASE_URL + "/api"
4251def fetch(user_id):
4252    requests.get(f"https://example.test/users/{user_id}")
4253def report(name):
4254    requests.get(f"/reports/{name}.csv")
4255def scoped(order):
4256    url = f"{API}/orders/{order.id}"
4257    requests.get(url)
4258def formatted(sku):
4259    requests.get("{}/items/{}".format(API, sku))
4260    requests.delete("/items/%s" % sku)
4261"#;
4262
4263        assert_eq!(
4264            consumer_paths(source),
4265            [
4266                (
4267                    Some("fetch".to_owned()),
4268                    Some("/users/{user_id}".to_owned()),
4269                    true
4270                ),
4271                (Some("report".to_owned()), None, true),
4272                (
4273                    Some("scoped".to_owned()),
4274                    Some("/api/orders/{id}".to_owned()),
4275                    false
4276                ),
4277                (
4278                    Some("formatted".to_owned()),
4279                    Some("/api/items/{sku}".to_owned()),
4280                    true
4281                ),
4282                (
4283                    Some("formatted".to_owned()),
4284                    Some("/items/{sku}".to_owned()),
4285                    true
4286                ),
4287            ]
4288        );
4289    }
4290
4291    #[test]
4292    fn python_test_clients_should_be_recognized_and_parameter_clients_left_for_composition() {
4293        let source = r#"
4294import httpx
4295import pytest
4296from fastapi.testclient import TestClient
4297from app import app, flask_app
4298
4299client = TestClient(app)
4300
4301def test_direct():
4302    client.get("/items")
4303
4304class OrdersTest(unittest.TestCase):
4305    def setUp(self):
4306        self.web = flask_app.test_client()
4307
4308    def test_flask(self):
4309        self.web.post("/orders")
4310
4311async def test_async():
4312    async with httpx.AsyncClient(app=app, base_url="http://test") as api:
4313        await api.delete("/orders/1")
4314
4315@pytest.fixture
4316def api_client():
4317    return TestClient(app)
4318
4319def test_injected(api_client):
4320    api_client.put("/orders/2")
4321"#;
4322        let result = parse_python_source(source);
4323        let consumers = result
4324            .iter()
4325            .filter(|item| item.role == SourceRole::Consumer)
4326            .map(|item| {
4327                (
4328                    item.framework,
4329                    item.method.clone().unwrap_or_default(),
4330                    item.path.clone().unwrap_or_default(),
4331                    item.status,
4332                    item.router.clone(),
4333                )
4334            })
4335            .collect::<Vec<_>>();
4336        let clients = result
4337            .iter()
4338            .filter(|item| item.role == SourceRole::Client)
4339            .map(|item| (item.framework, item.symbol_name.clone().unwrap_or_default()))
4340            .collect::<Vec<_>>();
4341
4342        assert_eq!(
4343            consumers,
4344            [
4345                (
4346                    SourceFramework::TestClient,
4347                    "GET".to_owned(),
4348                    "/items".to_owned(),
4349                    SourceEpistemicStatus::Confirmed,
4350                    None
4351                ),
4352                (
4353                    SourceFramework::FlaskTestClient,
4354                    "POST".to_owned(),
4355                    "/orders".to_owned(),
4356                    SourceEpistemicStatus::Confirmed,
4357                    None
4358                ),
4359                (
4360                    SourceFramework::TestClient,
4361                    "DELETE".to_owned(),
4362                    "/orders/1".to_owned(),
4363                    SourceEpistemicStatus::Confirmed,
4364                    None
4365                ),
4366                (
4367                    SourceFramework::TestClient,
4368                    "PUT".to_owned(),
4369                    "/orders/2".to_owned(),
4370                    SourceEpistemicStatus::Ambiguous,
4371                    Some(SymbolRef::Parameter {
4372                        name: "api_client".to_owned(),
4373                        index: 0
4374                    })
4375                ),
4376            ]
4377        );
4378        assert_eq!(
4379            clients,
4380            [(SourceFramework::TestClient, "api_client".to_owned())]
4381        );
4382    }
4383
4384    #[test]
4385    fn python_imperative_routes_should_name_their_handlers() {
4386        let source = r#"
4387from fastapi import APIRouter
4388from flask import Flask
4389from orders import views
4390
4391router = APIRouter()
4392router.add_api_route("/orders", views.list_orders, methods=["GET", "POST"])
4393router.add_api_route(path="/health", endpoint=health)
4394app = Flask(__name__)
4395app.add_url_rule("/legacy/<int:id>", "legacy", views.legacy)
4396"#;
4397        let providers = parse_python_source(source)
4398            .into_iter()
4399            .filter(|item| item.role == SourceRole::Provider)
4400            .map(|item| {
4401                format!(
4402                    "{} {} -> {}",
4403                    item.method.unwrap_or_default(),
4404                    item.path.unwrap_or_default(),
4405                    item.symbol_name.unwrap_or_default()
4406                )
4407            })
4408            .collect::<Vec<_>>();
4409
4410        assert_eq!(
4411            providers,
4412            [
4413                "GET /orders -> views.list_orders",
4414                "POST /orders -> views.list_orders",
4415                "GET /health -> health",
4416                "GET /legacy/<int:id> -> views.legacy",
4417            ]
4418        );
4419    }
4420
4421    #[test]
4422    fn rust_test_requests_should_be_recognized_for_axum_oneshot_and_actix() {
4423        let source = r#"
4424use axum::http::{Method, Request};
4425use tower::ServiceExt;
4426
4427#[tokio::test]
4428async fn creates_order() {
4429    let request = Request::builder()
4430        .method(Method::POST)
4431        .uri("/orders")
4432        .body(Body::empty())
4433        .unwrap();
4434    app().oneshot(request).await.unwrap();
4435}
4436
4437#[actix_web::test]
4438async fn reads_order() {
4439    let id = 42;
4440    let request = test::TestRequest::get().uri(&format!("/orders/{}", id)).to_request();
4441}
4442"#;
4443        let facts = parse_rust_source(source)
4444            .into_iter()
4445            .map(|item| {
4446                format!(
4447                    "{:?} {:?} {} {} {}",
4448                    item.role,
4449                    item.framework,
4450                    item.symbol_name.unwrap_or_default(),
4451                    item.method.unwrap_or_default(),
4452                    item.path.unwrap_or_default()
4453                )
4454            })
4455            .filter(|fact| !fact.starts_with("Call"))
4456            .collect::<Vec<_>>();
4457
4458        assert_eq!(
4459            facts,
4460            [
4461                "Test TokioTest creates_order  ",
4462                "Consumer AxumOneshot creates_order POST /orders",
4463                "Test RustTest reads_order  ",
4464                "Consumer ActixTest reads_order GET /orders/{value}",
4465            ]
4466        );
4467    }
4468
4469    #[test]
4470    fn python_wrappers_calls_and_fixture_requests_should_be_recorded() {
4471        let source = r#"
4472import pytest
4473import requests
4474from tests.helpers import create_order
4475
4476def get_json(path):
4477    return requests.get(BASE + path)
4478
4479@pytest.fixture
4480def order(client):
4481    return create_order(client, "/orders")
4482
4483def test_reads_order(order, api):
4484    api.read_order(order)
4485    get_json("/orders/7")
4486"#;
4487        let result = parse_python_source(source);
4488        let calls = result
4489            .iter()
4490            .filter_map(|item| {
4491                let call = item.call.as_ref()?;
4492                Some((
4493                    item.symbol_name.clone()?,
4494                    call.callee.clone(),
4495                    call.arguments
4496                        .iter()
4497                        .filter_map(|argument| argument.value.as_ref()?.client_literal())
4498                        .collect::<Vec<_>>(),
4499                ))
4500            })
4501            .collect::<Vec<_>>();
4502        let wrapper = result
4503            .iter()
4504            .find(|item| item.role == SourceRole::Consumer)
4505            .and_then(|item| item.url.clone());
4506
4507        assert_eq!(
4508            wrapper.map(|url| url.parts),
4509            Some(vec![
4510                UrlPart::Value(Some("BASE".to_owned())),
4511                UrlPart::Parameter {
4512                    name: "path".to_owned(),
4513                    index: 0
4514                },
4515            ])
4516        );
4517        assert_eq!(
4518            calls,
4519            [
4520                (
4521                    "order".to_owned(),
4522                    SymbolRef::Fixture("client".to_owned()),
4523                    vec![]
4524                ),
4525                (
4526                    "order".to_owned(),
4527                    SymbolRef::Import {
4528                        module: "tests.helpers".to_owned(),
4529                        name: "create_order".to_owned()
4530                    },
4531                    vec!["/orders".to_owned()]
4532                ),
4533                (
4534                    "test_reads_order".to_owned(),
4535                    SymbolRef::Fixture("api".to_owned()),
4536                    vec![]
4537                ),
4538                (
4539                    "test_reads_order".to_owned(),
4540                    SymbolRef::Fixture("order".to_owned()),
4541                    vec![]
4542                ),
4543                (
4544                    "test_reads_order".to_owned(),
4545                    SymbolRef::Call("api.read_order".to_owned()),
4546                    vec![]
4547                ),
4548                (
4549                    "test_reads_order".to_owned(),
4550                    SymbolRef::Local("get_json".to_owned()),
4551                    vec!["/orders/7".to_owned()]
4552                ),
4553            ]
4554        );
4555    }
4556
4557    #[test]
4558    fn python_comments_docstrings_and_unknown_clients_should_be_ignored() {
4559        let source = r#"
4560"""@app.get("/fake")
4561def test_fake(): pass
4562requests.get("https://example.test/fake")
4563"""
4564# import requests
4565# requests.post("/fake")
4566class Client:
4567    def get(self, path):
4568        pass
4569client = Client()
4570client.get("/not-httpx")
4571"#;
4572        let result = parse_python_source(source);
4573
4574        assert_eq!(result, Vec::new());
4575    }
4576
4577    #[test]
4578    fn ordering_should_be_source_stable_and_independent_of_method_list_order() {
4579        let source = r#"
4580from fastapi import FastAPI
4581app = FastAPI()
4582@app.api_route("/z", methods=["PUT", "GET", "POST"])
4583def endpoint():
4584    pass
4585"#;
4586        let first = parse_python_source(source);
4587        let second = parse_python_source(source);
4588
4589        assert_eq!(
4590            (
4591                first
4592                    .iter()
4593                    .map(|item| item.method.as_deref())
4594                    .collect::<Vec<_>>(),
4595                &first
4596            ),
4597            ((vec![Some("GET"), Some("POST"), Some("PUT")]), &second)
4598        );
4599    }
4600}