Skip to main content

alef_e2e/codegen/rust/
mock_server.rs

1//! Mock server source generation for Rust e2e tests.
2
3use std::fmt::Write as FmtWrite;
4
5use alef_core::hash::{self, CommentStyle};
6
7use crate::config::E2eConfig;
8use crate::escape::rust_raw_string;
9use crate::fixture::Fixture;
10
11/// Emit mock server setup lines into a test function body.
12///
13/// Builds `MockRoute` objects from the fixture's `mock_response` (single-response schema)
14/// or `input.mock_responses` (array schema for multiple responses per fixture).
15/// The resulting `mock_server` variable is in scope for the rest of the test function.
16pub fn render_mock_server_setup(out: &mut String, fixture: &Fixture, e2e_config: &E2eConfig) {
17    // Try array schema first: input.mock_responses
18    let mut routes = Vec::new();
19
20    if let Some(mock_responses) = fixture.input.get("mock_responses").and_then(|v| v.as_array()) {
21        // Array schema: input.mock_responses[{ path, status_code, headers, body_inline, ... }]
22        let call_config = e2e_config.resolve_call(fixture.call.as_deref());
23        let default_path = call_config.path.as_deref().unwrap_or("/");
24        let default_method = call_config.method.as_deref().unwrap_or("POST");
25
26        for response in mock_responses {
27            if let Ok(obj) = serde_json::from_value::<serde_json::Map<String, serde_json::Value>>(response.clone()) {
28                let path = obj
29                    .get("path")
30                    .and_then(|v| v.as_str())
31                    .unwrap_or(default_path)
32                    .to_string();
33                let method = obj
34                    .get("method")
35                    .and_then(|v| v.as_str())
36                    .unwrap_or(default_method)
37                    .to_string();
38                let status: u16 = obj.get("status_code").and_then(|v| v.as_u64()).unwrap_or(200) as u16;
39
40                let headers: Vec<(String, String)> = obj
41                    .get("headers")
42                    .and_then(|v| v.as_object())
43                    .map(|h| {
44                        let mut entries: Vec<_> = h
45                            .iter()
46                            .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("").to_string()))
47                            .collect();
48                        entries.sort_by(|a, b| a.0.cmp(&b.0));
49                        entries
50                    })
51                    .unwrap_or_default();
52
53                let body_str = if let Some(body_inline) = obj.get("body_inline").and_then(|v| v.as_str()) {
54                    rust_raw_string(body_inline)
55                } else {
56                    // Note: body_file support would require fixture-dir context at codegen time.
57                    // For now, we emit a placeholder; the standalone binary handles body_file.
58                    rust_raw_string("{}")
59                };
60
61                let delay_ms = obj.get("delay_ms").and_then(|v| v.as_u64());
62
63                routes.push((path, method, status, body_str, headers, delay_ms));
64            }
65        }
66    } else if let Some(mock) = fixture.mock_response.as_ref() {
67        // Single-response schema: mock_response { status, body, stream_chunks, headers }
68        let call_config = e2e_config.resolve_call(fixture.call.as_deref());
69        let path = call_config.path.as_deref().unwrap_or("/");
70        let method = call_config.method.as_deref().unwrap_or("POST");
71
72        let status = mock.status;
73
74        // Render headers map as a Vec<(String, String)> literal for stable iteration order.
75        let mut header_entries: Vec<(&String, &String)> = mock.headers.iter().collect();
76        header_entries.sort_by(|a, b| a.0.cmp(b.0));
77        let header_tuples: Vec<(String, String)> = header_entries
78            .into_iter()
79            .map(|(k, v)| (k.clone(), v.clone()))
80            .collect();
81
82        let body_str = match &mock.body {
83            Some(b) => {
84                let s = serde_json::to_string(b).unwrap_or_default();
85                rust_raw_string(&s)
86            }
87            None => rust_raw_string("{}"),
88        };
89
90        // Handle streaming separately within the single-response case.
91        if let Some(chunks) = &mock.stream_chunks {
92            // Streaming SSE response.
93            let _ = writeln!(out, "    let mock_route = MockRoute {{");
94            let _ = writeln!(out, "        path: \"{path}\",");
95            let _ = writeln!(out, "        method: \"{method}\",");
96            let _ = writeln!(out, "        status: {status},");
97            let _ = writeln!(out, "        body: String::new(),");
98            let _ = writeln!(out, "        is_streaming: true,");
99            let _ = writeln!(out, "        stream_chunks: vec![");
100            for chunk in chunks {
101                let chunk_str = match chunk {
102                    serde_json::Value::String(s) => rust_raw_string(s),
103                    other => {
104                        let s = serde_json::to_string(other).unwrap_or_default();
105                        rust_raw_string(&s)
106                    }
107                };
108                let _ = writeln!(out, "            {chunk_str}.to_string(),");
109            }
110            let _ = writeln!(out, "        ],");
111            let _ = writeln!(out, "        headers: vec![");
112            for (name, value) in &header_tuples {
113                let n = rust_raw_string(name);
114                let v = rust_raw_string(value);
115                let _ = writeln!(out, "            ({n}.to_string(), {v}.to_string()),");
116            }
117            let _ = writeln!(out, "        ],");
118            let _ = writeln!(out, "        delay_ms: None,");
119            let _ = writeln!(out, "    }};");
120            let _ = writeln!(out, "    let mock_server = MockServer::start(vec![mock_route]).await;");
121            return;
122        }
123
124        routes.push((
125            path.to_string(),
126            method.to_string(),
127            status,
128            body_str,
129            header_tuples,
130            None,
131        ));
132    } else {
133        return;
134    }
135
136    // Emit all routes (array schema produces multiple; single schema produces one).
137    if routes.len() == 1 {
138        let (path, method, status, body_str, header_entries, delay_ms) = routes.pop().unwrap();
139        let delay_literal = match delay_ms {
140            Some(ms) => format!("Some({ms})"),
141            None => "None".to_string(),
142        };
143        let _ = writeln!(out, "    let mock_route = MockRoute {{");
144        let _ = writeln!(out, "        path: \"{path}\",");
145        let _ = writeln!(out, "        method: \"{method}\",");
146        let _ = writeln!(out, "        status: {status},");
147        let _ = writeln!(out, "        body: {body_str}.to_string(),");
148        let _ = writeln!(out, "        is_streaming: false,");
149        let _ = writeln!(out, "        stream_chunks: vec![],");
150        let _ = writeln!(out, "        headers: vec![");
151        for (name, value) in &header_entries {
152            let n = rust_raw_string(name);
153            let v = rust_raw_string(value);
154            let _ = writeln!(out, "            ({n}.to_string(), {v}.to_string()),");
155        }
156        let _ = writeln!(out, "        ],");
157        let _ = writeln!(out, "        delay_ms: {delay_literal},");
158        let _ = writeln!(out, "    }};");
159        let _ = writeln!(out, "    let mock_server = MockServer::start(vec![mock_route]).await;");
160    } else {
161        // Multiple routes from array schema.
162        let _ = writeln!(out, "    let mut mock_routes = vec![];");
163        for (path, method, status, body_str, header_entries, delay_ms) in routes {
164            let delay_literal = match delay_ms {
165                Some(ms) => format!("Some({ms})"),
166                None => "None".to_string(),
167            };
168            let _ = writeln!(out, "    mock_routes.push(MockRoute {{");
169            let _ = writeln!(out, "        path: \"{path}\",");
170            let _ = writeln!(out, "        method: \"{method}\",");
171            let _ = writeln!(out, "        status: {status},");
172            let _ = writeln!(out, "        body: {body_str}.to_string(),");
173            let _ = writeln!(out, "        is_streaming: false,");
174            let _ = writeln!(out, "        stream_chunks: vec![],");
175            let _ = writeln!(out, "        headers: vec![");
176            for (name, value) in &header_entries {
177                let n = rust_raw_string(name);
178                let v = rust_raw_string(value);
179                let _ = writeln!(out, "            ({n}.to_string(), {v}.to_string()),");
180            }
181            let _ = writeln!(out, "        ],");
182            let _ = writeln!(out, "        delay_ms: {delay_literal},");
183            let _ = writeln!(out, "    }});");
184        }
185        let _ = writeln!(out, "    let mock_server = MockServer::start(mock_routes).await;");
186    }
187}
188
189/// Generate the complete `mock_server.rs` module source.
190pub fn render_mock_server_module() -> String {
191    // This is parameterized Axum mock server code identical in structure to
192    // liter-llm's mock_server.rs but without any project-specific imports.
193    //
194    // The module is included via `mod mock_server;` in every integration-test
195    // binary that needs MockRoute/MockServer, but only fixtures using
196    // `MockServer::start` actually invoke `start()` / `handle_request()`. Each
197    // integration test compiles as a separate binary, so the unused-in-some
198    // helpers would otherwise trip `-D dead_code` under
199    // `cargo test`/`cargo clippy`. The crate-level `#![allow(dead_code)]`
200    // mirrors the pattern used by other generated helper modules
201    // (e.g. `tests/common.rs`).
202    hash::header(CommentStyle::DoubleSlash)
203        + r#"//
204// Minimal axum-based mock HTTP server for e2e tests.
205
206#![allow(dead_code)]
207
208use std::net::SocketAddr;
209use std::sync::Arc;
210
211use axum::Router;
212use axum::body::Body;
213use axum::extract::State;
214use axum::http::{Request, StatusCode};
215use axum::response::{IntoResponse, Response};
216use tokio::net::TcpListener;
217
218/// A single mock route: match by path + method, return a configured response.
219#[derive(Clone, Debug)]
220pub struct MockRoute {
221    /// URL path to match, e.g. `"/v1/chat/completions"`.
222    pub path: &'static str,
223    /// HTTP method to match, e.g. `"POST"` or `"GET"`.
224    pub method: &'static str,
225    /// HTTP status code to return.
226    pub status: u16,
227    /// Response body JSON string (used when `is_streaming` is false).
228    pub body: String,
229    /// Whether this route serves a streaming SSE response.
230    /// True even when `stream_chunks` is empty (e.g. an empty stream still sends
231    /// `data: [DONE]\n\n` with the correct `content-type: text/event-stream` header).
232    pub is_streaming: bool,
233    /// Ordered SSE data payloads for streaming responses.
234    /// Each entry becomes `data: <chunk>\n\n` in the response.
235    /// A final `data: [DONE]\n\n` is always appended.
236    pub stream_chunks: Vec<String>,
237    /// Response headers to apply (name, value) pairs.
238    /// Multiple entries with the same name produce multiple header lines.
239    pub headers: Vec<(String, String)>,
240    /// Optional artificial response delay in milliseconds. Used by timeout-error
241    /// fixtures to force a client-side request timeout.
242    pub delay_ms: Option<u64>,
243}
244
245struct ServerState {
246    routes: Vec<MockRoute>,
247}
248
249pub struct MockServer {
250    /// Base URL of the mock server, e.g. `"http://127.0.0.1:54321"`.
251    pub url: String,
252    handle: tokio::task::JoinHandle<()>,
253}
254
255impl MockServer {
256    /// Start a mock server with the given routes.  Binds to a random port on
257    /// localhost and returns immediately once the server is listening.
258    pub async fn start(routes: Vec<MockRoute>) -> Self {
259        let state = Arc::new(ServerState { routes });
260
261        let app = Router::new().fallback(handle_request).with_state(state);
262
263        let listener = TcpListener::bind("127.0.0.1:0")
264            .await
265            .expect("Failed to bind mock server port");
266        let addr: SocketAddr = listener.local_addr().expect("Failed to get local addr");
267        let url = format!("http://{addr}");
268
269        let handle = tokio::spawn(async move {
270            axum::serve(listener, app).await.expect("Mock server failed");
271        });
272
273        MockServer { url, handle }
274    }
275
276    /// Stop the mock server.
277    pub fn shutdown(self) {
278        self.handle.abort();
279    }
280}
281
282impl Drop for MockServer {
283    fn drop(&mut self) {
284        self.handle.abort();
285    }
286}
287
288async fn handle_request(State(state): State<Arc<ServerState>>, req: Request<Body>) -> Response {
289    let path = req.uri().path().to_owned();
290    let method = req.method().as_str().to_uppercase();
291
292    for route in &state.routes {
293        // Match on method and either exact path or path prefix (route.path is a prefix of the
294        // request path, separated by a '/' boundary). This allows a single route registered at
295        // "/v1/batches" to match requests to "/v1/batches/abc123" or
296        // "/v1/batches/abc123/cancel".
297        let path_matches = path == route.path
298            || (path.starts_with(route.path)
299                && path.as_bytes().get(route.path.len()) == Some(&b'/'));
300        if path_matches && route.method.to_uppercase() == method {
301            if let Some(delay_ms) = route.delay_ms {
302                tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
303            }
304            let status =
305                StatusCode::from_u16(route.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
306
307            if route.is_streaming {
308                // Build SSE body: data: <chunk>\n\n ... data: [DONE]\n\n
309                // Note: stream_chunks may be empty for an empty-stream fixture; we still
310                // emit `data: [DONE]\n\n` with the correct SSE headers so clients do not hang.
311                let mut sse = String::new();
312                for chunk in &route.stream_chunks {
313                    sse.push_str("data: ");
314                    sse.push_str(chunk);
315                    sse.push_str("\n\n");
316                }
317                sse.push_str("data: [DONE]\n\n");
318
319                let mut builder = Response::builder()
320                    .status(status)
321                    .header("content-type", "text/event-stream")
322                    .header("cache-control", "no-cache");
323                for (name, value) in &route.headers {
324                    builder = builder.header(name, value);
325                }
326                return builder.body(Body::from(sse)).unwrap().into_response();
327            }
328
329            let mut builder =
330                Response::builder().status(status).header("content-type", "application/json");
331            for (name, value) in &route.headers {
332                builder = builder.header(name, value);
333            }
334            return builder.body(Body::from(route.body.clone())).unwrap().into_response();
335        }
336    }
337
338    // No matching route → 404.
339    Response::builder()
340        .status(StatusCode::NOT_FOUND)
341        .body(Body::from(format!("No mock route for {method} {path}")))
342        .unwrap()
343        .into_response()
344}
345"#
346}
347
348/// Generate the `tests/common.rs` module for Rust e2e tests.
349///
350/// The module spawns the standalone mock-server binary once per test process
351/// and exposes it via `mock_server_url()` which reads the `MOCK_SERVER_URL` env var.
352/// This allows tests that use mock_url arguments to access the server dynamically
353/// without panicking on unset env vars.
354///
355/// The module:
356/// - Spawns `target/release/mock-server` with the fixtures directory as an argument
357/// - Reads stdout lines looking for `MOCK_SERVER_URL=http://...` and `MOCK_SERVERS={...}`
358/// - Sets environment variables: `MOCK_SERVER_URL` and `MOCK_SERVER_<FIXTURE_ID>` for each entry
359/// - Drains remaining stdout in a background thread to prevent blocking
360/// - Uses `OnceLock` to ensure the server is spawned exactly once
361pub fn render_common_module() -> String {
362    hash::header(CommentStyle::DoubleSlash)
363        + r#"//
364// Auto-spawned mock server setup for e2e tests.
365// This module is auto-generated and should not be edited manually.
366
367use std::sync::OnceLock;
368
369static MOCK_SERVER_URL: OnceLock<String> = OnceLock::new();
370
371/// Get the mock server URL, spawning the server if not already running.
372///
373/// The server is spawned once per test process and reused by all tests.
374/// On first call, this function:
375/// - Spawns the `target/release/mock-server` binary
376/// - Reads `MOCK_SERVER_URL=http://...` from its stdout
377/// - Parses `MOCK_SERVERS={...}` JSON and sets env vars for per-fixture servers
378/// - Sets `MOCK_SERVER_URL` env var globally
379/// - Drains remaining stdout in a background thread
380///
381/// Subsequent calls return the cached URL without spawning again.
382pub fn mock_server_url() -> &'static str {
383    MOCK_SERVER_URL.get_or_init(|| {
384        let mock_server_bin = concat!(
385            env!("CARGO_MANIFEST_DIR"),
386            "/target/release/mock-server"
387        );
388        let fixtures_dir = concat!(
389            env!("CARGO_MANIFEST_DIR"),
390            "/../../fixtures"
391        );
392
393        // Spawn the mock-server binary with fixtures directory as argument.
394        let mut child = std::process::Command::new(mock_server_bin)
395            .arg(fixtures_dir)
396            .stdout(std::process::Stdio::piped())
397            .stdin(std::process::Stdio::piped())
398            .spawn()
399            .expect("Failed to spawn mock-server binary");
400
401        let stdout = child.stdout.take().expect("Failed to get stdout");
402        let stdin = child.stdin.take().expect("Failed to get stdin");
403
404        let mut url = String::new();
405        let mut line_buffer = String::new();
406        let mut line_count = 0;
407
408        // Read startup lines from the mock server.
409        // Expected: MOCK_SERVER_URL=http://... then MOCK_SERVERS={...json...}
410        // The server prints one line per loaded fixture before the markers, so the
411        // ceiling has to be high enough to clear hundreds of "loaded route" lines.
412        // We bail on the first `MOCK_SERVERS=` line (always emitted last) rather than
413        // relying on the line cap.
414        use std::io::BufRead;
415        let mut reader = std::io::BufReader::new(stdout);
416
417        while line_count < 2048 {
418            line_buffer.clear();
419            match reader.read_line(&mut line_buffer) {
420                Ok(0) => break,  // EOF
421                Ok(_) => {
422                    let line = line_buffer.trim();
423                    if line.starts_with("MOCK_SERVER_URL=") {
424                        url = line.strip_prefix("MOCK_SERVER_URL=")
425                            .unwrap_or("")
426                            .to_string();
427                    } else if line.starts_with("MOCK_SERVERS=") {
428                        let json_str = line.strip_prefix("MOCK_SERVERS=")
429                            .unwrap_or("{}");
430                        // Parse the JSON map and set env vars for each entry.
431                        if let Ok(servers) = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(json_str) {
432                            for (fid, furl) in servers {
433                                if let serde_json::Value::String(url_str) = furl {
434                                    let env_key = format!("MOCK_SERVER_{}", fid.to_uppercase());
435                                    std::env::set_var(&env_key, &url_str);
436                                }
437                            }
438                        }
439                        std::env::set_var("MOCK_SERVERS", json_str);
440                        // We have seen both lines; stop reading.
441                        break;
442                    }
443                    line_count += 1;
444                }
445                Err(_) => break,
446            }
447        }
448
449        // Set the main URL env var globally.
450        std::env::set_var("MOCK_SERVER_URL", &url);
451
452        // Drain remaining stdout in a background thread to prevent the server from blocking.
453        std::thread::spawn(move || {
454            let _ = std::io::copy(&mut reader.into_inner(), &mut std::io::sink());
455        });
456
457        // Keep stdin alive for the test process lifetime — the mock-server treats
458        // stdin EOF as the parent's shutdown signal, so dropping the handle would
459        // make it exit before any test connects.
460        Box::leak(Box::new(stdin));
461
462        // Return the URL for this process.
463        url
464    }).as_str()
465}
466"#
467}
468
469/// Generate the `src/main.rs` for the standalone mock server binary.
470///
471/// The binary:
472/// - Reads all `*.json` fixture files from a fixtures directory (default `../../fixtures`).
473/// - For each fixture that has a `mock_response` field, registers a route at
474///   `/fixtures/{fixture_id}` returning the configured status/body/SSE chunks.
475/// - Binds to `127.0.0.1:0` (random port), prints `MOCK_SERVER_URL=http://...`
476///   to stdout, then waits until stdin is closed for clean teardown.
477/// - Fixtures that declare host-root paths (`/robots.txt`, `/sitemap.*`, etc.) get their
478///   own dedicated listener so the crawler can fetch them from the host root.  A second
479///   line `MOCK_SERVERS={...}` (sorted JSON object) maps fixture_id → base URL for those.
480///
481/// This binary is intended for cross-language e2e suites (WASM, Node) that
482/// spawn it as a child process and read the URL from its stdout.
483pub fn render_mock_server_binary() -> String {
484    hash::header(CommentStyle::DoubleSlash)
485        + r#"//
486// Standalone mock HTTP server binary for cross-language e2e tests.
487// Reads fixture JSON files and serves mock responses on /fixtures/{fixture_id}.
488//
489// Usage: mock-server [fixtures-dir]
490//   fixtures-dir defaults to "../../fixtures"
491//
492// Prints `MOCK_SERVER_URL=http://127.0.0.1:<port>` to stdout once listening,
493// then optionally `MOCK_SERVERS={...}` with per-fixture URLs for host-root fixtures,
494// then blocks until stdin is closed (parent process exit triggers cleanup).
495
496use std::collections::HashMap;
497use std::io::{self, BufRead};
498use std::net::SocketAddr;
499use std::path::Path;
500use std::sync::Arc;
501
502use axum::Router;
503use axum::body::Body;
504use axum::extract::State;
505use axum::http::{Request, StatusCode};
506use axum::response::{IntoResponse, Response};
507use serde::Deserialize;
508use tokio::net::TcpListener;
509
510// ---------------------------------------------------------------------------
511// Fixture types (mirrors alef-e2e's fixture.rs for runtime deserialization)
512// Supports both schemas:
513//   liter-llm: mock_response: { status, body, stream_chunks }
514//   spikard:   http.expected_response: { status_code, body, headers }
515// ---------------------------------------------------------------------------
516
517#[derive(Debug, Deserialize)]
518struct MockResponse {
519    status: u16,
520    #[serde(default)]
521    body: Option<serde_json::Value>,
522    #[serde(default)]
523    stream_chunks: Option<Vec<serde_json::Value>>,
524    #[serde(default)]
525    headers: HashMap<String, String>,
526    /// Optional artificial delay (milliseconds) applied before sending the response.
527    /// Used by timeout-error fixtures to force the client request to time out.
528    #[serde(default)]
529    delay_ms: Option<u64>,
530}
531
532#[derive(Debug, Deserialize)]
533struct HttpExpectedResponse {
534    status_code: u16,
535    #[serde(default)]
536    body: Option<serde_json::Value>,
537    #[serde(default)]
538    headers: HashMap<String, String>,
539}
540
541#[derive(Debug, Deserialize)]
542struct HttpFixture {
543    expected_response: HttpExpectedResponse,
544}
545
546#[derive(Debug, Deserialize)]
547struct Fixture {
548    id: String,
549    #[serde(default)]
550    mock_response: Option<MockResponse>,
551    #[serde(default)]
552    http: Option<HttpFixture>,
553    /// Array-form fixture schema. `input.mock_responses[i] = { path?, status_code, headers, body_inline | body_file }`.
554    /// Used by kreuzcrawl-style fixtures that mock multiple URLs per fixture (e.g. a page +
555    /// `/robots.txt` + `/sitemap.xml`).
556    #[serde(default)]
557    input: Option<serde_json::Value>,
558}
559
560/// A single resolved mock response with its serving path and whether it is a host-root path.
561struct ResolvedRoute {
562    /// The namespaced path under which this route is registered in the shared server,
563    /// e.g. `/fixtures/robots_disallow_path` or `/fixtures/robots_disallow_path/assets/style.css`.
564    path: String,
565    /// The original fixture-declared path (before namespacing), e.g. `/robots.txt` or `/assets/style.css`.
566    original_path: String,
567    response: MockResponse,
568    /// Body bytes (pre-loaded from body_file or body_inline).
569    body_bytes: Vec<u8>,
570}
571
572/// Returns true for paths that the crawler fetches from the host root rather than
573/// under a fixture-namespaced prefix.  These require a dedicated per-fixture listener.
574fn is_host_root_path(path: &str) -> bool {
575    path.starts_with("/robots") || path.starts_with("/sitemap")
576}
577
578impl Fixture {
579    /// Bridge both schemas into a unified MockResponse.
580    fn as_mock_response(&self) -> Option<MockResponse> {
581        if let Some(mock) = &self.mock_response {
582            return Some(MockResponse {
583                status: mock.status,
584                body: mock.body.clone(),
585                stream_chunks: mock.stream_chunks.clone(),
586                headers: mock.headers.clone(),
587                delay_ms: mock.delay_ms,
588            });
589        }
590        if let Some(http) = &self.http {
591            return Some(MockResponse {
592                status: http.expected_response.status_code,
593                body: http.expected_response.body.clone(),
594                stream_chunks: None,
595                headers: http.expected_response.headers.clone(),
596                delay_ms: None,
597            });
598        }
599        None
600    }
601
602    /// Resolve every mock response this fixture defines.
603    ///
604    /// Returns single-element output for the legacy `mock_response` / `http` schemas, and one
605    /// element per array entry for the kreuzcrawl-style `input.mock_responses` schema. For the
606    /// array schema, each element may declare its own `path` (defaulting to `/fixtures/{id}`),
607    /// and the body source can be either `body_inline` (string) or `body_file` (path relative
608    /// to the fixtures dir, loaded at startup).
609    ///
610    /// Paths that are NOT host-root paths (e.g. `/robots.txt`, `/sitemap.xml`) are namespaced
611    /// under `/fixtures/{id}` so that fixtures sharing common paths (like `/`, `/a`, `/b`) do
612    /// not collide in the shared route table.
613    fn as_routes(&self, fixtures_dir: &Path) -> Vec<ResolvedRoute> {
614        let mut routes = Vec::new();
615        let default_path = format!("/fixtures/{}", self.id);
616
617        if let Some(mock) = self.as_mock_response() {
618            let body_bytes = mock
619                .body
620                .as_ref()
621                .map(|b| match b {
622                    serde_json::Value::String(s) => s.as_bytes().to_vec(),
623                    other => serde_json::to_string(other).unwrap_or_default().into_bytes(),
624                })
625                .unwrap_or_default();
626            routes.push(ResolvedRoute {
627                path: default_path.clone(),
628                original_path: default_path.clone(),
629                response: mock,
630                body_bytes,
631            });
632        }
633
634        if let Some(input) = &self.input {
635            if let Some(arr) = input.get("mock_responses").and_then(|v| v.as_array()) {
636                for entry in arr {
637                    let original_path = entry
638                        .get("path")
639                        .and_then(|v| v.as_str())
640                        .unwrap_or("/")
641                        .to_string();
642
643                    // Namespace under /fixtures/<id> unless this is a host-root path.
644                    let namespaced_path = if is_host_root_path(&original_path) {
645                        original_path.clone()
646                    } else if original_path == "/" {
647                        format!("/fixtures/{}", self.id)
648                    } else {
649                        format!("/fixtures/{}{}", self.id, original_path)
650                    };
651
652                    let status: u16 = entry.get("status_code").and_then(|v| v.as_u64()).unwrap_or(200) as u16;
653                    let headers: HashMap<String, String> = entry
654                        .get("headers")
655                        .and_then(|v| v.as_object())
656                        .map(|h| {
657                            h.iter()
658                                .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("").to_string()))
659                                .collect()
660                        })
661                        .unwrap_or_default();
662
663                    // Load body bytes — use read() not read_to_string() to support binary files.
664                    let body_bytes: Vec<u8> = if let Some(inline) = entry.get("body_inline").and_then(|v| v.as_str()) {
665                        inline.as_bytes().to_vec()
666                    } else if let Some(file) = entry.get("body_file").and_then(|v| v.as_str()) {
667                        // body_file is resolved relative to `<fixtures>/responses/` first,
668                        // falling back to `<fixtures>/` for projects that store body assets at
669                        // the fixtures root rather than under a `responses/` subdir.
670                        let candidates = [fixtures_dir.join("responses").join(file), fixtures_dir.join(file)];
671                        let mut loaded = None;
672                        for abs in &candidates {
673                            if let Ok(bytes) = std::fs::read(abs) {
674                                loaded = Some(bytes);
675                                break;
676                            }
677                        }
678                        match loaded {
679                            Some(b) => b,
680                            None => {
681                                eprintln!(
682                                    "warning: cannot read body_file {} (tried {} and {})",
683                                    file,
684                                    candidates[0].display(),
685                                    candidates[1].display()
686                                );
687                                Vec::new()
688                            }
689                        }
690                    } else {
691                        Vec::new()
692                    };
693
694                    let delay_ms = entry.get("delay_ms").and_then(|v| v.as_u64());
695
696                    routes.push(ResolvedRoute {
697                        path: namespaced_path,
698                        original_path,
699                        response: MockResponse {
700                            status,
701                            body: None,
702                            stream_chunks: None,
703                            headers,
704                            delay_ms,
705                        },
706                        body_bytes,
707                    });
708                }
709            }
710        }
711
712        routes
713    }
714}
715
716// ---------------------------------------------------------------------------
717// Route table
718// ---------------------------------------------------------------------------
719
720#[derive(Clone, Debug)]
721struct MockRoute {
722    status: u16,
723    body: Vec<u8>,
724    /// Whether this route serves a streaming SSE response.
725    /// True even when `stream_chunks` is empty (e.g. an empty stream still sends
726    /// `data: [DONE]\n\n` with the correct `content-type: text/event-stream` header).
727    is_streaming: bool,
728    stream_chunks: Vec<String>,
729    headers: Vec<(String, String)>,
730    /// Optional artificial delay applied before the handler returns. When set,
731    /// the handler `tokio::time::sleep`s for this many milliseconds before
732    /// constructing the response — used by timeout-error fixtures to force
733    /// client-side request timeouts.
734    delay_ms: Option<u64>,
735}
736
737type RouteTable = Arc<HashMap<String, MockRoute>>;
738
739// ---------------------------------------------------------------------------
740// Axum handler
741// ---------------------------------------------------------------------------
742
743async fn handle_request(State(routes): State<RouteTable>, req: Request<Body>) -> Response {
744    let path = req.uri().path().to_owned();
745
746    // Try exact match first
747    if let Some(route) = routes.get(&path) {
748        if let Some(delay_ms) = route.delay_ms {
749            tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
750        }
751        return serve_route(route);
752    }
753
754    // Try prefix match: find a route that is a prefix of the request path
755    // This allows /fixtures/basic_chat/v1/chat/completions to match /fixtures/basic_chat
756    for (route_path, route) in routes.iter() {
757        if path.starts_with(route_path) && (path.len() == route_path.len() || path.as_bytes()[route_path.len()] == b'/') {
758            if let Some(delay_ms) = route.delay_ms {
759                tokio::time::sleep(std::time::Duration::from_millis(delay_ms)).await;
760            }
761            return serve_route(route);
762        }
763    }
764
765    Response::builder()
766        .status(StatusCode::NOT_FOUND)
767        .body(Body::from(format!("No mock route for {path}")))
768        .unwrap()
769        .into_response()
770}
771
772fn serve_route(route: &MockRoute) -> Response {
773    let status = StatusCode::from_u16(route.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
774
775    if route.is_streaming {
776        // Note: stream_chunks may be empty for an empty-stream fixture; we still emit
777        // `data: [DONE]\n\n` with the correct SSE headers so clients do not hang.
778        let mut sse = String::new();
779        for chunk in &route.stream_chunks {
780            sse.push_str("data: ");
781            sse.push_str(chunk);
782            sse.push_str("\n\n");
783        }
784        sse.push_str("data: [DONE]\n\n");
785
786        let mut builder = Response::builder()
787            .status(status)
788            .header("content-type", "text/event-stream")
789            .header("cache-control", "no-cache");
790        for (name, value) in &route.headers {
791            builder = builder.header(name, value);
792        }
793        return builder.body(Body::from(sse)).unwrap().into_response();
794    }
795
796    // Only set the default content-type if the fixture does not override it.
797    // Inspect the first non-whitespace byte to detect JSON vs binary vs plain text.
798    let has_content_type = route.headers.iter().any(|(k, _)| k.to_lowercase() == "content-type");
799    let mut builder = Response::builder().status(status);
800    if !has_content_type {
801        let first_nonws = route.body.iter().find(|&&b| b != b' ' && b != b'\t' && b != b'\n' && b != b'\r');
802        let default_ct = match first_nonws {
803            Some(&b'{') | Some(&b'[') => "application/json",
804            _ => "text/plain",
805        };
806        builder = builder.header("content-type", default_ct);
807    }
808    for (name, value) in &route.headers {
809        // Skip content-encoding headers — the mock server returns uncompressed bodies.
810        // Sending a content-encoding without actually encoding the body would cause
811        // clients to fail decompression.
812        if name.to_lowercase() == "content-encoding" {
813            continue;
814        }
815        // The <<absent>> sentinel means this header must NOT be present in the
816        // real server response — do not emit it from the mock server either.
817        if value == "<<absent>>" {
818            continue;
819        }
820        // Replace the <<uuid>> sentinel with a real UUID v4 so clients can
821        // assert the header value matches the UUID pattern.
822        if value == "<<uuid>>" {
823            let uuid = format!(
824                "{:08x}-{:04x}-4{:03x}-{:04x}-{:012x}",
825                rand_u32(),
826                rand_u16(),
827                rand_u16() & 0x0fff,
828                (rand_u16() & 0x3fff) | 0x8000,
829                rand_u48(),
830            );
831            builder = builder.header(name, uuid);
832            continue;
833        }
834        builder = builder.header(name, value);
835    }
836    builder.body(Body::from(route.body.clone())).unwrap().into_response()
837}
838
839/// Generate a pseudo-random u32 using the current time nanoseconds.
840fn rand_u32() -> u32 {
841    use std::time::{SystemTime, UNIX_EPOCH};
842    let ns = SystemTime::now()
843        .duration_since(UNIX_EPOCH)
844        .map(|d| d.subsec_nanos())
845        .unwrap_or(0);
846    ns ^ (ns.wrapping_shl(13)) ^ (ns.wrapping_shr(17))
847}
848
849fn rand_u16() -> u16 {
850    (rand_u32() & 0xffff) as u16
851}
852
853fn rand_u48() -> u64 {
854    ((rand_u32() as u64) << 16) | (rand_u16() as u64)
855}
856
857// ---------------------------------------------------------------------------
858// Fixture loading
859// ---------------------------------------------------------------------------
860
861/// Intermediate fixture-loading result: shared route table plus per-fixture host-root data.
862struct LoadedRoutes {
863    /// Routes namespaced under /fixtures/<id> for the shared listener.
864    shared: HashMap<String, MockRoute>,
865    /// For each fixture that has host-root routes: fixture_id → route table at host root.
866    per_fixture: HashMap<String, HashMap<String, MockRoute>>,
867}
868
869fn load_routes(fixtures_dir: &Path) -> LoadedRoutes {
870    let mut shared = HashMap::new();
871    let mut per_fixture: HashMap<String, HashMap<String, MockRoute>> = HashMap::new();
872    load_routes_recursive(fixtures_dir, fixtures_dir, &mut shared, &mut per_fixture);
873    LoadedRoutes { shared, per_fixture }
874}
875
876fn load_routes_recursive(
877    dir: &Path,
878    fixtures_root: &Path,
879    shared: &mut HashMap<String, MockRoute>,
880    per_fixture: &mut HashMap<String, HashMap<String, MockRoute>>,
881) {
882    let entries = match std::fs::read_dir(dir) {
883        Ok(e) => e,
884        Err(err) => {
885            eprintln!("warning: cannot read directory {}: {err}", dir.display());
886            return;
887        }
888    };
889
890    let mut paths: Vec<_> = entries.filter_map(|e| e.ok()).map(|e| e.path()).collect();
891    paths.sort();
892
893    for path in paths {
894        if path.is_dir() {
895            load_routes_recursive(&path, fixtures_root, shared, per_fixture);
896        } else if path.extension().is_some_and(|ext| ext == "json") {
897            let filename = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
898            if filename == "schema.json" || filename.starts_with('_') {
899                continue;
900            }
901            let content = match std::fs::read_to_string(&path) {
902                Ok(c) => c,
903                Err(err) => {
904                    eprintln!("warning: cannot read {}: {err}", path.display());
905                    continue;
906                }
907            };
908            let fixtures: Vec<Fixture> = if content.trim_start().starts_with('[') {
909                match serde_json::from_str(&content) {
910                    Ok(v) => v,
911                    Err(err) => {
912                        eprintln!("warning: cannot parse {}: {err}", path.display());
913                        continue;
914                    }
915                }
916            } else {
917                match serde_json::from_str::<Fixture>(&content) {
918                    Ok(f) => vec![f],
919                    Err(err) => {
920                        eprintln!("warning: cannot parse {}: {err}", path.display());
921                        continue;
922                    }
923                }
924            };
925
926            for fixture in fixtures {
927                let resolved_routes = fixture.as_routes(fixtures_root);
928                // A fixture needs host-root routing if either:
929                //  1. it serves a path the crawler fetches at host root (/robots*, /sitemap*), OR
930                //  2. it returns a 3xx Location header pointing to a host-root path inside the
931                //     same fixture (the engine resolves the Location against the host, not the
932                //     /fixtures/<id>/ namespace, so host-root serving is required for the
933                //     follow-up GET to hit the correct route).
934                let has_intra_fixture_redirect = resolved_routes.iter().any(|r| {
935                    // 3xx with relative Location header
936                    let location_redirect = (300..400).contains(&r.response.status)
937                        && r.response.headers.iter().any(|(name, value)| {
938                            name.eq_ignore_ascii_case("location") && value.starts_with('/')
939                        });
940                    // Refresh header with url=/...
941                    let refresh_redirect = r.response.headers.iter().any(|(name, value)| {
942                        if !name.eq_ignore_ascii_case("refresh") {
943                            return false;
944                        }
945                        let lower = value.to_ascii_lowercase();
946                        lower
947                            .find("url=")
948                            .map(|idx| value[idx + 4..].trim_start().starts_with('/'))
949                            .unwrap_or(false)
950                    });
951                    // HTML meta-refresh tag pointing to /...
952                    let body_lower_lossy = String::from_utf8_lossy(&r.body_bytes).to_ascii_lowercase();
953                    let meta_refresh = body_lower_lossy
954                        .split("http-equiv=\"refresh\"")
955                        .nth(1)
956                        .and_then(|s| s.split("content=").nth(1))
957                        .map(|s| {
958                            let trimmed = s.trim_start_matches(['"', '\'']);
959                            trimmed.contains("url=/")
960                        })
961                        .unwrap_or(false);
962                    location_redirect || refresh_redirect || meta_refresh
963                });
964                let has_host_root = has_intra_fixture_redirect
965                    || resolved_routes.iter().any(|r| is_host_root_path(&r.original_path));
966
967                for resolved in resolved_routes {
968                    let is_streaming = resolved.response.stream_chunks.is_some();
969                    let stream_chunks = resolved.response
970                        .stream_chunks
971                        .unwrap_or_default()
972                        .into_iter()
973                        .map(|c| match c {
974                            serde_json::Value::String(s) => s,
975                            other => serde_json::to_string(&other).unwrap_or_default(),
976                        })
977                        .collect();
978                    let mut headers: Vec<(String, String)> = resolved.response.headers.into_iter().collect();
979                    headers.sort_by(|a, b| a.0.cmp(&b.0));
980
981                    let mock_route = MockRoute {
982                        status: resolved.response.status,
983                        body: resolved.body_bytes,
984                        is_streaming,
985                        stream_chunks,
986                        headers,
987                        delay_ms: resolved.response.delay_ms,
988                    };
989
990                    // Always insert into the shared namespaced table.
991                    shared.insert(resolved.path.clone(), mock_route.clone());
992
993                    // For fixtures with host-root routes, also build a per-fixture table
994                    // where routes are mounted at their original (un-namespaced) paths.
995                    if has_host_root {
996                        per_fixture
997                            .entry(fixture.id.clone())
998                            .or_default()
999                            .insert(resolved.original_path.clone(), mock_route);
1000                    }
1001                }
1002            }
1003        }
1004    }
1005}
1006
1007// ---------------------------------------------------------------------------
1008// Entry point
1009// ---------------------------------------------------------------------------
1010
1011#[tokio::main]
1012async fn main() {
1013    let fixtures_dir_arg = std::env::args().nth(1).unwrap_or_else(|| "../../fixtures".to_string());
1014    let fixtures_dir = Path::new(&fixtures_dir_arg);
1015
1016    let loaded = load_routes(fixtures_dir);
1017    eprintln!("mock-server: loaded {} shared routes from {}", loaded.shared.len(), fixtures_dir.display());
1018
1019    // Shared namespaced server.
1020    let shared_table: RouteTable = Arc::new(loaded.shared);
1021    let shared_app = Router::new().fallback(handle_request).with_state(shared_table);
1022
1023    let shared_listener = TcpListener::bind("127.0.0.1:0")
1024        .await
1025        .expect("mock-server: failed to bind shared port");
1026    let shared_addr: SocketAddr = shared_listener.local_addr().expect("mock-server: failed to get shared local addr");
1027
1028    // Per-fixture listeners for host-root routes (robots.txt, sitemap.xml, etc.).
1029    // Sorted by fixture_id for deterministic output.
1030    let mut fixture_ids: Vec<String> = loaded.per_fixture.keys().cloned().collect();
1031    fixture_ids.sort();
1032
1033    let mut fixture_urls: HashMap<String, String> = HashMap::new();
1034    for fixture_id in &fixture_ids {
1035        let routes = loaded.per_fixture[fixture_id].clone();
1036        eprintln!("mock-server: fixture {} has {} host-root routes", fixture_id, routes.len());
1037        let table: RouteTable = Arc::new(routes);
1038        let app = Router::new().fallback(handle_request).with_state(table);
1039        let listener = TcpListener::bind("127.0.0.1:0")
1040            .await
1041            .expect("mock-server: failed to bind per-fixture port");
1042        let addr: SocketAddr = listener.local_addr().expect("mock-server: failed to get per-fixture local addr");
1043        fixture_urls.insert(fixture_id.clone(), format!("http://{addr}"));
1044        tokio::spawn(async move {
1045            axum::serve(listener, app).await.expect("mock-server: per-fixture server error");
1046        });
1047    }
1048
1049    // Print the shared URL so the parent process can read it.
1050    println!("MOCK_SERVER_URL=http://{shared_addr}");
1051
1052    // Always print MOCK_SERVERS=... (empty `{}` when there are no host-root
1053    // fixtures) so parent parsers — which read until they see this sentinel
1054    // line — never block on a readline that never comes.
1055    let mut sorted_pairs: Vec<(&String, &String)> = fixture_urls.iter().collect();
1056    sorted_pairs.sort_by_key(|(k, _)| k.as_str());
1057    let json_entries: Vec<String> = sorted_pairs
1058        .iter()
1059        .map(|(k, v)| format!("\"{}\":\"{}\"", k, v))
1060        .collect();
1061    println!("MOCK_SERVERS={{{}}}", json_entries.join(","));
1062
1063    // Flush stdout explicitly so the parent does not block waiting.
1064    use std::io::Write;
1065    std::io::stdout().flush().expect("mock-server: failed to flush stdout");
1066
1067    // Spawn the shared server in the background.
1068    tokio::spawn(async move {
1069        axum::serve(shared_listener, shared_app).await.expect("mock-server: shared server error");
1070    });
1071
1072    // Block until stdin is closed — the parent process controls lifetime.
1073    let stdin = io::stdin();
1074    let mut lines = stdin.lock().lines();
1075    while lines.next().is_some() {}
1076}
1077"#
1078}
1079
1080#[cfg(test)]
1081mod tests {
1082    use super::*;
1083
1084    #[test]
1085    fn render_mock_server_module_contains_struct_definition() {
1086        let out = render_mock_server_module();
1087        assert!(out.contains("pub struct MockRoute"));
1088        assert!(out.contains("pub struct MockServer"));
1089    }
1090
1091    #[test]
1092    fn render_mock_server_binary_contains_main() {
1093        let out = render_mock_server_binary();
1094        assert!(out.contains("async fn main()"));
1095        assert!(out.contains("MOCK_SERVER_URL=http://"));
1096    }
1097
1098    #[test]
1099    fn render_common_module_has_expected_symbols() {
1100        let src = render_common_module();
1101        assert!(src.contains("pub fn mock_server_url"), "missing mock_server_url");
1102        assert!(src.contains("OnceLock"), "missing OnceLock");
1103        assert!(src.contains("MOCK_SERVER_URL"), "missing MOCK_SERVER_URL");
1104        assert!(src.contains("MOCK_SERVERS"), "missing MOCK_SERVERS");
1105        assert!(src.contains("serde_json"), "missing serde_json parsing");
1106    }
1107}