Skip to main content

max / makenotwork

2.7 KB · 75 lines History Blame Raw
1 //! The published documentation tree, end to end.
2 //!
3 //! These two assertions were written when the docs half of the sitemap and the
4 //! search index were fixed, then dropped: the harness built its `DocLoader`
5 //! with `sections: vec![]`, so under test the site had no doc pages and neither
6 //! test could pass. The harness now loads the real tree through the same
7 //! `site_docs::build_doc_loader` production uses, so they hold again.
8 //!
9 //! Both are corpus-wide rather than example-based on purpose. A test naming one
10 //! slug pins that slug; these fail when any page stops being reachable, which is
11 //! the failure that actually happens when a doc is added or a section renamed.
12
13 use crate::harness::TestHarness;
14 use serde_json::Value;
15
16 /// Every page in the search index must be reachable. The index is what the
17 /// client-side filter searches, so a slug present here but not served is a
18 /// result that 404s when clicked.
19 #[tokio::test]
20 async fn every_indexed_doc_slug_is_served() {
21 let mut h = TestHarness::new().await;
22
23 let resp = h.client.get("/docs/search.json").await;
24 assert_eq!(resp.status, 200, "search index should be served");
25 let index: Vec<Value> = resp.json();
26 assert!(
27 !index.is_empty(),
28 "the search index is empty, which means the harness loaded no docs and \
29 this test is proving nothing"
30 );
31
32 for entry in &index {
33 let slug = entry["slug"].as_str().expect("each entry carries a slug");
34 let page = h.client.get(&format!("/docs/{slug}")).await;
35 assert_eq!(
36 page.status, 200,
37 "/docs/{slug} is in the search index but is not served"
38 );
39 }
40 }
41
42 /// The sitemap is how a crawler finds the docs at all. It carries one URL per
43 /// indexed page, and a doc missing from it is a doc that is published but
44 /// undiscoverable.
45 #[tokio::test]
46 async fn the_sitemap_carries_every_doc_url() {
47 let mut h = TestHarness::new().await;
48
49 let resp = h.client.get("/docs/search.json").await;
50 let index: Vec<Value> = resp.json();
51 let slugs: Vec<String> = index
52 .iter()
53 .map(|e| e["slug"].as_str().expect("slug").to_string())
54 .collect();
55 assert!(
56 !slugs.is_empty(),
57 "no docs loaded, nothing is being asserted"
58 );
59
60 let resp = h.client.get("/sitemap.xml").await;
61 assert_eq!(resp.status, 200, "sitemap should be served");
62 let xml = resp.text;
63
64 assert!(
65 xml.contains("/docs</loc>") || xml.contains("/docs<"),
66 "the docs index itself belongs in the sitemap"
67 );
68 for slug in &slugs {
69 assert!(
70 xml.contains(&format!("/docs/{slug}</loc>")),
71 "/docs/{slug} is published but missing from the sitemap"
72 );
73 }
74 }
75