forge
Explore
Log in

Don't you dare go Hollow

the router tests stop needing a bundler the checkout never ran: the new landing assertions load a real asset bundle, which a dev machine has under target/debug/assets but a fresh CI checkout does not — the deploy bundles only after Test, so every page-rendering test panicked on the load; the setup now falls back to a one-entry manifest written beside the stylesheet build.rs already compiled, keyed by the layout's own asset! declaration (the id folds the declaring file in, so a fresh declaration here would catalog a different id than any page resolves), one temp dir per setup so concurrent tests never rewrite one manifest under each other

Browse files at this commit

0c0ea32f92d47f1df10382104decdac050e6667fAhmet Kaan GümüşParent: cc56357
crates/im-web/src/directory.rsmodified+90 -45
@@ -6,8 +6,8 @@
6 6
7 use topcoat::context::Cx;7 use topcoat::context::Cx;
8 use topcoat::router::content::Json;8 use topcoat::router::content::Json;
9-use topcoat::router::{StatusCode, route};
10 use topcoat::router::response::IntoResponse as _;9 use topcoat::router::response::IntoResponse as _;
10+use topcoat::router::{StatusCode, route};
11 11
12 use crate::server;12 use crate::server;
13 13
@@ -28,7 +28,10 @@ struct DirectoryMember {
28 #[route(GET "/directory")]28 #[route(GET "/directory")]
29 async fn directory(cx: &Cx) -> topcoat::Result<topcoat::router::response::Response> {29 async fn directory(cx: &Cx) -> topcoat::Result<topcoat::router::response::Response> {
30 if !server::valid_app(cx).await {30 if !server::valid_app(cx).await {
31- return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({ "error": "invalid_client" })))31+ return (
32+ StatusCode::UNAUTHORIZED,
33+ Json(serde_json::json!({ "error": "invalid_client" })),
34+ )
32 .into_response(cx);35 .into_response(cx);
33 }36 }
34 let store = server::app(cx).store.clone();37 let store = server::app(cx).store.clone();
@@ -54,7 +57,10 @@ async fn directory(cx: &Cx) -> topcoat::Result<topcoat::router::response::Respon
54 #[route(GET "/family")]57 #[route(GET "/family")]
55 async fn family(cx: &Cx) -> topcoat::Result<topcoat::router::response::Response> {58 async fn family(cx: &Cx) -> topcoat::Result<topcoat::router::response::Response> {
56 if !server::valid_app(cx).await {59 if !server::valid_app(cx).await {
57- return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({ "error": "invalid_client" })))60+ return (
61+ StatusCode::UNAUTHORIZED,
62+ Json(serde_json::json!({ "error": "invalid_client" })),
63+ )
58 .into_response(cx);64 .into_response(cx);
59 }65 }
60 let store = server::app(cx).store.clone();66 let store = server::app(cx).store.clone();
@@ -68,12 +74,14 @@ mod tests {
68 use std::sync::Arc;74 use std::sync::Arc;
69 75
70 use im_core::accounts::{create_invite, create_user_from_invite};76 use im_core::accounts::{create_invite, create_user_from_invite};
71- use im_core::oidc::create_client;
72 use im_core::model::ClientId;77 use im_core::model::ClientId;
78+ use im_core::oidc::create_client;
73 use im_core::store::Store;79 use im_core::store::Store;
74 use topcoat::asset::RouterBuilderAssetExt as _;80 use topcoat::asset::RouterBuilderAssetExt as _;
75 use topcoat::cookie::RouterBuilderCookieExt as _;81 use topcoat::cookie::RouterBuilderCookieExt as _;
76- use topcoat::router::{Body, Router, RouterBuilderDiscoverExt as _, StatusCode, header, to_bytes};82+ use topcoat::router::{
83+ Body, Router, RouterBuilderDiscoverExt as _, StatusCode, header, to_bytes,
84+ };
77 85
78 use crate::config::Config;86 use crate::config::Config;
79 use crate::server::{self, SESSION_COOKIE};87 use crate::server::{self, SESSION_COOKIE};
@@ -87,9 +95,10 @@ mod tests {
87 95
88 async fn setup() -> Setup {96 async fn setup() -> Setup {
89 let store = Arc::new(Store::open(Path::new(":memory:")).await.unwrap());97 let store = Arc::new(Store::open(Path::new(":memory:")).await.unwrap());
90- let (client_id, secret) = create_client(&store, "tasks", vec!["http://app/callback".into()])98+ let (client_id, secret) =
91- .await99+ create_client(&store, "tasks", vec!["http://app/callback".into()])
92- .unwrap();100+ .await
101+ .unwrap();
93 let admin_invite = create_invite(&store, "ada@example.com", None, true)102 let admin_invite = create_invite(&store, "ada@example.com", None, true)
94 .await103 .await
95 .unwrap();104 .unwrap();
@@ -149,13 +158,12 @@ mod tests {
149 .discover()158 .discover()
150 .cookies()159 .cookies()
151 // The pages shell renders its stylesheet link through the asset160 // The pages shell renders its stylesheet link through the asset
152- // catalog, so the tests render pages against the real bundle —161+ // catalog, so the tests render pages against a real bundle: the
153- // the same one `serve` loads beside the executable.162+ // one `serve` loads beside the executable where it exists, and
154- .assets(topcoat::asset::AssetBundle::load_dir(concat!(163+ // otherwise a one-entry bundle folded from the stylesheet
155- env!("CARGO_MANIFEST_DIR"),164+ // build.rs already compiled — a fresh checkout runs `cargo test`
156- "/../../target/debug/assets"165+ // before any bundling step, and no test reads the bytes.
157- ))166+ .assets(test_assets())
158- .unwrap())
159 .app_context(app)167 .app_context(app)
160 .build();168 .build();
161 Setup {169 Setup {
@@ -166,6 +174,49 @@ mod tests {
166 }174 }
167 }175 }
168 176
177+ /// The bundle the router renders against. A dev machine carries the
178+ /// real one under `target/debug/assets`; on one that never ran the
179+ /// bundler (CI), a minimal manifest is written beside a copy of the
180+ /// stylesheet build.rs produced, so page GETs resolve their `<link>`
181+ /// without any deploy-time step.
182+ fn test_assets() -> topcoat::asset::AssetBundle {
183+ // The layout's own declaration: `asset!` folds the declaring source
184+ // file into the id, so a fresh one here would catalog a different
185+ // id than any rendered page resolves.
186+ let style = crate::layout::STYLE;
187+ let dev = concat!(env!("CARGO_MANIFEST_DIR"), "/../../target/debug/assets");
188+ if std::path::Path::new(dev).join("manifest.toml").is_file() {
189+ return topcoat::asset::AssetBundle::load_dir(dev).unwrap();
190+ }
191+ // One directory per call: tests run on shared threads, and two
192+ // setups rewriting one manifest can interleave a half-written file
193+ // under the other's load.
194+ static N: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
195+ let dir = std::env::temp_dir().join(format!(
196+ "im-web-test-assets-{}-{}",
197+ std::process::id(),
198+ N.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
199+ ));
200+ std::fs::create_dir_all(&dir).unwrap();
201+ std::fs::copy(
202+ concat!(env!("CARGO_MANIFEST_DIR"), "/assets/main.css"),
203+ dir.join("main.css"),
204+ )
205+ .unwrap();
206+ topcoat::asset::Manifest {
207+ version: topcoat::asset::MANIFEST_VERSION,
208+ assets: vec![topcoat::asset::ManifestEntry {
209+ id: style.id(),
210+ file: "main.css".into(),
211+ hash: "0".repeat(64),
212+ content_type: "text/css".into(),
213+ }],
214+ }
215+ .save(dir.join("manifest.toml"))
216+ .unwrap();
217+ topcoat::asset::AssetBundle::load_dir(&dir).unwrap()
218+ }
219+
169 fn basic(client_id: &str, secret: &str) -> String {220 fn basic(client_id: &str, secret: &str) -> String {
170 use base64::Engine as _;221 use base64::Engine as _;
171 format!(222 format!(
@@ -187,9 +238,7 @@ mod tests {
187 238
188 /// The config's service list as the domain module's own shape — the one239 /// The config's service list as the domain module's own shape — the one
189 /// conversion the boot-time seed needs.240 /// conversion the boot-time seed needs.
190- fn core_services(241+ fn core_services(config: &[crate::config::Service]) -> Vec<im_core::services::Service> {
191- config: &[crate::config::Service],
192- ) -> Vec<im_core::services::Service> {
193 config242 config
194 .iter()243 .iter()
195 .map(|service| im_core::services::Service {244 .map(|service| im_core::services::Service {
@@ -228,7 +277,11 @@ mod tests {
228 }277 }
229 278
230 /// A top-level GET (the RP-initiated logout's shape), same answer triple.279 /// A top-level GET (the RP-initiated logout's shape), same answer triple.
231- async fn get_location(router: &Router, uri: &str, cookie: &str) -> (StatusCode, Option<String>) {280+ async fn get_location(
281+ router: &Router,
282+ uri: &str,
283+ cookie: &str,
284+ ) -> (StatusCode, Option<String>) {
232 let response = router285 let response = router
233 .handle(286 .handle(
234 http::Request::builder()287 http::Request::builder()
@@ -305,9 +358,7 @@ mod tests {
305 #[tokio::test]358 #[tokio::test]
306 async fn the_directory_refuses_a_browser_and_an_unknown_client_alike() {359 async fn the_directory_refuses_a_browser_and_an_unknown_client_alike() {
307 let Setup {360 let Setup {
308- router,361+ router, client_id, ..
309- client_id,
310- ..
311 } = setup().await;362 } = setup().await;
312 let (status, _) = get(&router, None).await;363 let (status, _) = get(&router, None).await;
313 assert_eq!(status, StatusCode::UNAUTHORIZED);364 assert_eq!(status, StatusCode::UNAUTHORIZED);
@@ -487,16 +538,20 @@ mod tests {
487 .collect::<Vec<_>>();538 .collect::<Vec<_>>();
488 assert_eq!(keys, vec!["in", "im", "iz", "wiki"], "{body}");539 assert_eq!(keys, vec!["in", "im", "iz", "wiki"], "{body}");
489 assert_eq!(family[3]["url"], "http://127.0.0.1:99");540 assert_eq!(family[3]["url"], "http://127.0.0.1:99");
490- assert!(get_page(&router, "/?section=profile", &cookie)541+ assert!(
542+ get_page(&router, "/?section=profile", &cookie)
543+ .await
544+ .contains(r#"action="/services/add""#)
545+ );
546+ assert!(
547+ !get_page(
548+ &router,
549+ "/?section=profile",
550+ &format!("{SESSION_COOKIE}={}", ben_session.expose())
551+ )
491 .await552 .await
492- .contains(r#"action="/services/add""#));553+ .contains(r#"action="/services/add""#)
493- assert!(!get_page(554+ );
494- &router,
495- "/?section=profile",
496- &format!("{SESSION_COOKIE}={}", ben_session.expose())
497- )
498- .await
499- .contains(r#"action="/services/add""#));
500 555
501 // Edit rewrites name and address; move up swaps with the neighbor.556 // Edit rewrites name and address; move up swaps with the neighbor.
502 let (_status, location, _) = post_form(557 let (_status, location, _) = post_form(
@@ -507,13 +562,8 @@ mod tests {
507 )562 )
508 .await;563 .await;
509 assert_eq!(location.as_deref(), Some("/?ok=services"));564 assert_eq!(location.as_deref(), Some("/?ok=services"));
510- let (_status, location, _) = post_form(565+ let (_status, location, _) =
511- &router,566+ post_form(&router, "/services/move", "key=wiki&dir=up", Some(&cookie)).await;
512- "/services/move",
513- "key=wiki&dir=up",
514- Some(&cookie),
515- )
516- .await;
517 assert_eq!(location.as_deref(), Some("/?ok=services"));567 assert_eq!(location.as_deref(), Some("/?ok=services"));
518 let (_, body) = get_family(&router, Some(basic(&client_id, &secret))).await;568 let (_, body) = get_family(&router, Some(basic(&client_id, &secret))).await;
519 let family = serde_json::from_str::<serde_json::Value>(&body).unwrap();569 let family = serde_json::from_str::<serde_json::Value>(&body).unwrap();
@@ -541,13 +591,8 @@ mod tests {
541 assert_eq!(location.as_deref(), Some("http://127.0.0.1:98/"));591 assert_eq!(location.as_deref(), Some("http://127.0.0.1:98/"));
542 592
543 // And once the row is gone, the same origin is refused to the door.593 // And once the row is gone, the same origin is refused to the door.
544- let (_status, location, _) = post_form(594+ let (_status, location, _) =
545- &router,595+ post_form(&router, "/services/remove", "key=wiki", Some(&cookie)).await;
546- "/services/remove",
547- "key=wiki",
548- Some(&cookie),
549- )
550- .await;
551 assert_eq!(location.as_deref(), Some("/?ok=services"));596 assert_eq!(location.as_deref(), Some("/?ok=services"));
552 let (_, body) = get_family(&router, Some(basic(&client_id, &secret))).await;597 let (_, body) = get_family(&router, Some(basic(&client_id, &secret))).await;
553 let family = serde_json::from_str::<serde_json::Value>(&body).unwrap();598 let family = serde_json::from_str::<serde_json::Value>(&body).unwrap();
crates/im-web/src/layout.rsmodified+1 -1
@@ -11,7 +11,7 @@ use crate::i18n::{Key, Lang, lang_of, t};
11 use crate::server;11 use crate::server;
12 12
13 /// `style/main.scss`, compiled by `build.rs` into `assets/main.css`.13 /// `style/main.scss`, compiled by `build.rs` into `assets/main.css`.
14-const STYLE: Asset = asset!("assets/main.css");14+pub(crate) const STYLE: Asset = asset!("assets/main.css");
15 15
16 /// The mark, in the word as it is in the name: lower case, the Turkish16 /// The mark, in the word as it is in the name: lower case, the Turkish
17 /// tittle. Text-only here — the auth card is the whole chrome.17 /// tittle. Text-only here — the auth card is the whole chrome.