//! Every `next` link a response offers must be followable. //! //! `next` is the API's promise that following a link is a `GET` that answers //! 200 -- that is what makes it safe for a client (or an agent) to walk the //! graph without knowing the path vocabulary. A link that needs a parameter we //! did not fill in (`/groups/X/phewas` without a locus is a 422) breaks that //! promise, and it fails silently: the client, not the server, sees the error. //! So the walk is a test. use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; /// Follow `next` from a seed, up to `depth` hops, and report every link that /// did not answer 200. async fn walk(seed: &str, token: &str, depth: usize) -> Vec<(String, u16)> { let mut broken = Vec::new(); let mut frontier = vec![seed.to_string()]; let mut seen: Vec = Vec::new(); for _ in 0..depth { let mut next_frontier = Vec::new(); for path in frontier { let response = common::get_with_token("GET (link walk)", &path, token).await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); if status != StatusCode::OK { broken.push((path.clone(), status.as_u16())); continue; } // Only envelopes carry `next`; an image or a plain body does not. let Ok(envelope) = serde_json::from_slice::(&bytes) else { continue; }; for action in envelope["next"].as_array().into_iter().flatten() { let Some(href) = action["href"].as_str() else { continue }; // A template ("{marker}") is a link to be completed by a // caller that knows a marker; it is not walkable as it stands. if href.contains('{') || seen.iter().any(|known| known == href) { continue; } seen.push(href.to_string()); next_frontier.push(href.to_string()); } } frontier = next_frontier; if frontier.is_empty() { break; } } broken } #[tokio::test] async fn the_genotype_surface_leads_somewhere() { let Some(token) = common::auth_token().await else { eprintln!("skipping the_genotype_surface_leads_somewhere: set GN4_TEST_AUTH_TOKEN"); return; }; for seed in [ "/groups/BXD", "/groups/BXD/genotypes", "/groups/BXD/genotypes/BXD.4", "/groups/BXD/genotypes/BXD.4/frequencies", "/groups/BXD/genotypes/BXD.4/markers?limit=2", "/groups/unlisted/genotypes/BXD.4/frequencies", ] { let broken = walk(seed, &token, 2).await; assert!(broken.is_empty(), "from {seed}: {broken:?}"); } } #[tokio::test] async fn the_trait_and_listing_surfaces_lead_somewhere() { let Some(token) = common::auth_token().await else { eprintln!("skipping the_trait_and_listing_surfaces_lead_somewhere: set GN4_TEST_AUTH_TOKEN"); return; }; // `/search` is one of the seeds, and it answers 502 when the search // backend is not configured -- which is the deployment's business, not // this test's: `XAPIAN_DB_PATH` points nowhere in a bare shell, so skip // rather than report a broken link graph. let config = genenetwork4::config::Config::load(); if config.search_engine != "native" || !std::path::Path::new(&config.xapian_db_path).is_dir() { eprintln!( "skipping the_trait_and_listing_surfaces_lead_somewhere: no search index (SEARCH_ENGINE={}, XAPIAN_DB_PATH={})", config.search_engine, config.xapian_db_path ); return; } for seed in [ "/version", "/groups", "/species", "/species/mouse", "/mapping-methods", "/search?q=albumin", "/groups/BXD/traits/10001", "/groups/BXD/traits/10001/values", "/groups/BXD/strains", ] { let broken = walk(seed, &token, 2).await; assert!(broken.is_empty(), "from {seed}: {broken:?}"); } } /// A marker's own calls, which is where a caller checks a peak before /// believing it. #[tokio::test] async fn a_marker_leads_to_its_tally_and_its_phewas() { let Some(token) = common::auth_token().await else { eprintln!("skipping a_marker_leads_to_its_tally_and_its_phewas: set GN4_TEST_AUTH_TOKEN"); return; }; let response = common::get_with_token( "GET /groups/BXD/genotypes/BXD.4/frequencies", "/groups/BXD/genotypes/BXD.4/frequencies", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let links: Vec = envelope["next"] .as_array() .unwrap() .iter() .map(|action| action["href"].as_str().unwrap().to_string()) .collect(); // frequencies -> markers, and both marker calls assert!( links.iter().any(|href| href.ends_with("/markers")), "frequencies lead to the marker list: {links:?}" ); assert!( links .iter() .any(|href| href.contains("{marker}/location")), "frequencies lead to a marker's location: {links:?}" ); // and the marker's own answer leads back let marker = "rs6269442"; let response = common::get_with_token( "GET /groups/BXD/genotypes/BXD.4/markers/{marker}/location", &format!("/groups/BXD/genotypes/BXD.4/markers/{marker}/location"), &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let links: Vec = envelope["next"] .as_array() .unwrap() .iter() .map(|action| action["href"].as_str().unwrap().to_string()) .collect(); assert!( links .iter() .any(|href| href.ends_with(&format!("{marker}/frequencies"))), "a location leads to that marker's tally: {links:?}" ); assert!( links.iter().any(|href| href.contains("phewas?locus=")), "a marker leads to the PheWAS at it: {links:?}" ); } /// A scan's reply carries the forward hint that reads the peak for what it /// is: how many animals sit in each genotype class there. Without it, "LOD /// 2.93" is the same sentence whether it rests on two animals or a hundred. #[tokio::test] async fn a_scan_points_at_the_class_counts_behind_its_peak() { let Some(token) = common::auth_token().await else { eprintln!("skipping a_scan_points_at_the_class_counts_behind_its_peak: no token"); return; }; if !common::db_available() { eprintln!("skipping a_scan_points_at_the_class_counts_behind_its_peak: no database"); return; } let response = common::get_with_token( "GET /groups/BXD/traits/10001/values", "/groups/BXD/traits/10001/values", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let samples: Value = common::data(&bytes); let body = serde_json::json!({ "group": "BXD", "genotype_version": "BXD.4", "trait_name": "BXD::10001", "samples": samples, }); let response = common::post_json_with_token("POST /compute/rqtl", "/compute/rqtl", &token, &body).await; assert_eq!(response.status(), StatusCode::OK); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let envelope: Value = serde_json::from_slice(&bytes).unwrap(); let links: Vec<&str> = envelope["next"] .as_array() .unwrap() .iter() .map(|action| action["href"].as_str().unwrap()) .collect(); let hint = links .iter() .find(|href| href.contains("/classes?trait=")) .unwrap_or_else(|| panic!("a scan offers the class counts at its peak: {links:?}")); assert!( hint.starts_with("/groups/BXD/genotypes/BXD.4/markers/"), "the hint names this scan's own version and marker: {hint}" ); assert!( hint.contains("{trait_id}"), "the trait id is the caller's to fill in: {hint}" ); // And the marker it names is the peak the reply reported. // /compute/rqtl's rows live under data.markers. let rows = envelope["data"]["markers"].as_array().unwrap(); let peak = rows .iter() .max_by(|left, right| { left[3] .as_f64() .unwrap() .partial_cmp(&right[3].as_f64().unwrap()) .unwrap() }) .unwrap(); assert!( hint.contains(peak[0].as_str().unwrap()), "the hint is at the peak marker {}: {hint}", peak[0] ); } /// The parental design of a family, and what a mapping does with it. #[tokio::test] async fn a_groups_parental_design_says_what_a_mapping_drops() { let Some(token) = common::auth_token().await else { eprintln!("skipping a_groups_parental_design_says_what_a_mapping_drops: no token"); return; }; let config = genenetwork4::config::Config::load(); if !std::path::Path::new(&config.genotype_files_dir).is_dir() { eprintln!("skipping a_groups_parental_design_says_what_a_mapping_drops: no genotype files"); return; } let response = common::get_with_token( "GET /groups/BXD/parents-and-f1s", "/groups/BXD/parents-and-f1s", &token, ) .await; let status = response.status(); let bytes = response.into_body().collect().await.unwrap().to_bytes(); let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); assert_eq!(status, axum::http::StatusCode::OK, "{body}"); let design = &body["data"]; // GN2 unpacks ParInfo as f1, f12, maternal, paternal -- the roles must // match that, not the other order. assert_eq!(design["maternal"]["strain"], "C57BL/6J", "{design}"); assert_eq!(design["paternal"]["strain"], "DBA/2J", "{design}"); let f1s: Vec<&str> = design["f1s"] .as_array() .unwrap() .iter() .map(|role| role["strain"].as_str().unwrap()) .collect(); assert_eq!(f1s, vec!["B6D2F1", "D2B6F1"], "{design}"); assert_eq!(design["source"], "metadata"); assert!(design["excluded_by_name"].as_array().unwrap().len() > 0, "{design}"); // The reply has to say what a mapping then does with them. let notes = body["notes"].as_array().unwrap(); assert!( notes.iter().any(|note| note.as_str().unwrap_or_default().contains("include_parents_f1")), "{notes:?}" ); }