//! Route-level native-engine scenarios: the `/search` HTTP surface with //! `SEARCH_ENGINE=native`, proving the engine's typed errors keep the //! route's contract end-to-end (status class + code), and that valid //! queries serve. This is where the lane's "status class verified //! route-level" requirements live (C3.4's Q52 `mean:abc..def` → 422 //! `invalid_query`; C3.6's Q42 `Hs:foo:1M` → 422 `invalid_query`). //! //! Hermetic except for the index: auth is a local JWKS stub (the //! tests/search.rs pattern) and tokens are minted with the committed //! test key; the native engine needs no gn3. The dev index is required, //! so the test skips loudly when `XAPIAN_DB_PATH` does not point at one //! (per AMENDMENT B the dev index is a class-level reference, never a //! recorded-production-count source). //! //! One sequential test function per binary: `SEARCH_ENGINE`, //! `XAPIAN_DB_PATH` and `AUTH_SERVER_URL` are process-global. use axum::Router; use axum::body::Body; use axum::http::{Request, StatusCode}; use axum::routing::get; use http_body_util::BodyExt; use jsonwebtoken::{Algorithm, EncodingKey, Header, encode}; use serde::Serialize; use serde_json::Value; use tower::ServiceExt; /// Committed test-only key material, same files `tests/search.rs` uses. const TEST_JWKS: &str = include_str!("../test/data/test_jwks.json"); const TEST_PRIVATE_KEY: &str = include_str!("../test/data/test_rsa_private.pem"); async fn serve_jwks() -> (StatusCode, &'static str) { (StatusCode::OK, TEST_JWKS) } fn set(key: &str, value: &str) { // SAFETY: this test binary has a single test function and no other // thread reads these keys concurrently (see the module doc). unsafe { std::env::set_var(key, value) }; } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn native_engine_route_level_error_and_match_classes() { let dev_index = std::env::var("XAPIAN_DB_PATH").unwrap_or_default(); if dev_index.is_empty() || !std::fs::metadata(&dev_index) .map(|m| m.is_dir()) .unwrap_or(false) { eprintln!( "SKIPPED: XAPIAN_DB_PATH is not set to an index directory \ (set it to the dev index for the route-level native scenarios)" ); return; } // The gn-auth stand-in, so token verification needs no network. let jwks_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let jwks_port = jwks_listener.local_addr().unwrap().port(); let jwks_app = Router::new().route("/auth/public-jwks", get(serve_jwks)); tokio::spawn(async move { axum::serve(jwks_listener, jwks_app).await.unwrap(); }); let auth_url = format!("http://127.0.0.1:{jwks_port}"); set("AUTH_SERVER_URL", &auth_url); set("SEARCH_ENGINE", "native"); set("XAPIAN_DB_PATH", &dev_index); eprintln!("route-level native scenarios against dev index: {dev_index}"); #[derive(Serialize)] struct Claims { sub: String, scope: String, iss: String, iat: u64, exp: u64, } let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_secs(); let claims = Claims { sub: "native-route-test-user".to_string(), scope: "profile".to_string(), iss: auth_url.clone(), iat: now, exp: now + 3600, }; let key = EncodingKey::from_rsa_pem(TEST_PRIVATE_KEY.as_bytes()).unwrap(); let token = encode(&Header::new(Algorithm::RS256), &claims, &key).unwrap(); async fn get_json(uri: &str, token: &str) -> (axum::http::StatusCode, Value) { let request = Request::builder() .uri(uri) .header("Authorization", format!("Bearer {token}")) .body(Body::empty()) .unwrap(); let response = genenetwork4::app().oneshot(request).await.unwrap(); let status = response.status(); let body = response.into_body().collect().await.unwrap().to_bytes(); (status, serde_json::from_slice(&body).unwrap_or(Value::Null)) } // 1. Q52's status class, route-level: the recorded production 400 // "Unknown range operation" surfaces as 422 invalid_query. let (status, body) = get_json("/search?q=mean%3Aabc..def&type=phenotype", &token).await; assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "Q52 class"); assert_eq!(body["code"], "invalid_query"); assert_eq!(body["detail"], "Unknown range operation"); eprintln!(" Q52 mean:abc..def -> 422 invalid_query (route-level)"); // 2. Q42's status class, route-level (AMENDMENT E): the recorded // production 500 (mset-time InvalidOperationError on the invalid // Hs location) is re-baselined to the blessed 422 invalid_query; // the native engine's app-level guard detects the invalid leaf // before any mset call — no crash, no crash-mimicry. let (status, body) = get_json("/search?q=Hs%3Afoo%3A1M&type=phenotype", &token).await; assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY, "Q42 class"); assert_eq!(body["code"], "invalid_query"); eprintln!(" Q42 Hs:foo:1M -> 422 invalid_query (route-level)"); // 3. A valid range query serves: 200 with hits (dev count printed as // REFERENCE ONLY, AMENDMENT B — never compared to production). let (status, body) = get_json("/search?q=mean%3A10..20&type=phenotype", &token).await; assert_eq!(status, StatusCode::OK, "mean:10..20 class"); let hits = body["data"].as_array().cloned().unwrap_or_default(); eprintln!( " mean:10..20 -> 200, dev_hits={} (reference only)", hits.len() ); // 4. Free text still serves through the same configuration. let (status, body) = get_json("/search?q=alcohol&type=phenotype", &token).await; assert_eq!(status, StatusCode::OK, "alcohol class"); let hits = body["data"].as_array().cloned().unwrap_or_default(); eprintln!(" alcohol -> 200, dev_hits={} (reference only)", hits.len()); }