about summary refs log tree commit diff
path: root/tests/links.rs
blob: 5291008e6ba42711745c706b2fe2e9a39c6cacb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! 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<String> = 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::<Value>(&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<String> = 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<String> = 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:?}"
    );
}