diff options
| author | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-06 06:42:49 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabua@gmail.com> | 2026-10-06 06:50:16 +0300 |
| commit | 2d3d230f5c98fcd0b579b836ccfc31354f8dd85e (patch) | |
| tree | 9a185a6bbf52b352f03fd4c267a93f5429f6f47b | |
| parent | 5b8f8c3be0a593fb1feaf0cbe91dd481e09365d5 (diff) | |
| download | genenetwork4-2d3d230f5c98fcd0b579b836ccfc31354f8dd85e.tar.gz | |
* tests/search_engine.rs (gn3 engine): pin the seam error contract.
Covers the SearchError variants tests/search.rs does not reach (it pins only InvalidQuery and Backend), plus the GN3_TIMEOUT_SECS -> reqwest timeout plumbing. Calls the Gn3 engine directly, so no auth or JWKS stub is needed; scenarios run in one sequential test function per the process-global-state rule in docs/SKILL.md.
| -rw-r--r-- | tests/search_engine.rs | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/search_engine.rs b/tests/search_engine.rs new file mode 100644 index 0000000..0663a6e --- /dev/null +++ b/tests/search_engine.rs @@ -0,0 +1,118 @@ +//! Engine-seam tests: pin the production of the `SearchError` variants +//! that `tests/search.rs` does not reach (it pins only `InvalidQuery` +//! via `&&` and `Backend` via its "boom" server), plus the +//! `GN3_TIMEOUT_SECS` -> reqwest timeout plumbing. +//! +//! These call the engine (`Gn3`) directly rather than through the +//! `/search` route, so no auth/JWKS stub is needed: the engine reads +//! only `GN3_URL` / `GN3_TIMEOUT_SECS`. A separate test binary is its +//! own process with its own environment, which keeps these env +//! mutations isolated from `tests/search.rs`; within this file the +//! scenarios run in one sequential test function, per the +//! process-global-state rule in docs/SKILL.md. + +use axum::Router; +use axum::routing::get; +use genenetwork4::search::{Engine, SearchError, SearchHit, gn3::Gn3}; +use std::time::{Duration, Instant}; +use tokio::net::TcpListener; + +/// The name of a result's variant, so a failed assertion can name what +/// it got without requiring `SearchHit: Debug`. +fn variant_name(result: &Result<Vec<SearchHit>, SearchError>) -> &'static str { + match result { + Ok(_) => "Ok", + Err(SearchError::InvalidQuery(_)) => "InvalidQuery", + Err(SearchError::Unreachable(_)) => "Unreachable", + Err(SearchError::Misbehaving(_)) => "Misbehaving", + Err(SearchError::Backend(_)) => "Backend", + Err(SearchError::Internal(_)) => "Internal", + } +} + +/// Serves an axum app on an ephemeral loopback port and returns its +/// base URL in the `.../api/` form the engine appends `search/` to. +async fn base_url_for(app: Router) -> String { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + tokio::spawn(async move { + axum::serve(listener, app).await.unwrap(); + }); + format!("http://127.0.0.1:{port}/api/") +} + +/// A 200 response whose body is not the hits JSON: the decode step in +/// the engine must surface as `Misbehaving`. +async fn serve_not_json() -> &'static str { + "this is not the hits array" +} + +/// A 200 response that stalls for 2s before answering, so a 1s +/// `GN3_TIMEOUT_SECS` fires first and the request surfaces as +/// `Unreachable`; without the timeout plumbing it would succeed. +async fn serve_slow() -> &'static str { + std::thread::sleep(Duration::from_secs(2)); + "[]" +} + +/// A bare TCP listener that accepts each connection and immediately +/// drops it, so the HTTP request fails with a transport error +/// (`Unreachable`) rather than any status. +async fn drop_server_base_url() -> String { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let port = listener.local_addr().unwrap().port(); + tokio::spawn(async move { + loop { + if let Ok((socket, _)) = listener.accept().await { + drop(socket); + } + } + }); + format!("http://127.0.0.1:{port}/api/") +} + +fn set(key: &str, value: &str) { + // SAFETY: this test binary has a single test function and no other + // thread reads these keys concurrently, so process-global env + // mutation is sound here (see the module doc comment). + unsafe { std::env::set_var(key, value) }; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn engine_produces_its_error_variants_and_honours_the_timeout() { + // Set before the first engine call: the shared reqwest client is + // built once, on first use, capturing this timeout. + set("GN3_TIMEOUT_SECS", "1"); + + // 1. Internal: an unparseable GN3_URL is a local misconfiguration + // and never reaches the network. + set("GN3_URL", ":::not a url"); + let result = Gn3.search("alcohol", "phenotype", 5).await; + assert_eq!(variant_name(&result), "Internal"); + + // 2. Misbehaving: a success status with a body that is not the hits + // JSON fails to decode. + let not_json_url = base_url_for(Router::new().route("/api/search/", get(serve_not_json))).await; + set("GN3_URL", ¬_json_url); + let result = Gn3.search("alcohol", "phenotype", 5).await; + assert_eq!(variant_name(&result), "Misbehaving"); + + // 3. Unreachable: the upstream drops the connection. + let drop_url = drop_server_base_url().await; + set("GN3_URL", &drop_url); + let result = Gn3.search("alcohol", "phenotype", 5).await; + assert_eq!(variant_name(&result), "Unreachable"); + + // 4. Timeout plumbing: with GN3_TIMEOUT_SECS=1, a 2s response is + // aborted well before it arrives, surfacing as Unreachable. + let slow_url = base_url_for(Router::new().route("/api/search/", get(serve_slow))).await; + set("GN3_URL", &slow_url); + let started = Instant::now(); + let result = Gn3.search("alcohol", "phenotype", 5).await; + let elapsed = started.elapsed(); + assert_eq!(variant_name(&result), "Unreachable"); + assert!( + elapsed < Duration::from_millis(1900), + "GN3_TIMEOUT_SECS=1 did not fire before the 2s response: {elapsed:?}" + ); +} |
