//! Renders the real UI to PNG files for a visual check. Needs a GPU (or a //! software Vulkan driver), so it is ignored by default: //! //! REQLITE_RENDER_DIR=/tmp/shots cargo test -p reqlite-app --test render -- --ignored use std::sync::Arc; use std::time::{Duration, Instant}; use egui_kittest::Harness; use reqlite_app::action::Action; use reqlite_app::state::{RequestTab, ResponseTab}; use reqlite_app::ReqliteApp; use reqlite_core::http::{BodyStore, HttpClient, HttpError, ResolvedRequest, Response, Timings}; use reqlite_core::CancelToken; struct Canned; impl HttpClient for Canned { async fn send(&self, req: ResolvedRequest, _c: CancelToken) -> Result { let body = r#"{"id":42,"name":"Ada Lovelace","email":"ada@example.com","roles":["admin","author"],"active":true,"manager":null,"stats":{"posts":128,"score":9.75}}"#; Ok(Response { status: 200, reason: "OK".into(), http_version: "HTTP/2".into(), headers: vec![ ("content-type".into(), "application/json; charset=utf-8".into()), ("cache-control".into(), "no-store".into()), ("x-request-id".into(), "7f3a9c2e".into()), ], size: body.len() as u64, body: BodyStore::InMemory(Arc::new(body.as_bytes().to_vec())), timings: Timings { dns: Duration::from_millis(4), connect: Duration::from_millis(19), tls: Duration::from_millis(48), ttfb: Duration::from_millis(112), total: Duration::from_millis(118), }, effective_url: req.url, }) } } fn shoot(h: &mut Harness<'_, ReqliteApp>, name: &str) { let Ok(dir) = std::env::var("REQLITE_RENDER_DIR") else { return; }; std::fs::create_dir_all(&dir).unwrap(); for _ in 0..3 { h.step(); } let img = h.render().expect("render"); img.save(format!("{dir}/{name}.png")).unwrap(); } fn wait(h: &mut Harness<'_, ReqliteApp>, done: impl Fn(&ReqliteApp) -> bool) { let start = Instant::now(); while !done(h.state()) { assert!(start.elapsed() < Duration::from_secs(5)); h.step(); std::thread::sleep(Duration::from_millis(10)); } } #[test] #[ignore = "needs a GPU; run with --ignored"] fn screenshots() { let rt = tokio::runtime::Runtime::new().unwrap(); let dir = tempfile::tempdir().unwrap(); let root = dir.path().canonicalize().unwrap(); std::fs::create_dir(root.join("orders")).unwrap(); std::fs::write( root.join("users.http"), "### List users\nGET {{base_url}}/users?limit=20&sort=name\nAccept: application/json\n\n### Create user\n# @name create-user\nPOST {{base_url}}/users\nContent-Type: application/json\nAuthorization: Bearer {{token}}\n\n{\n \"name\": \"Ada\",\n \"team\": \"{{team}}\"\n}\n\n### Delete user\nDELETE {{base_url}}/users/{{id}}\n", ) .unwrap(); std::fs::write( root.join("orders/orders.http"), "### Get order\nGET {{base_url}}/orders/1\n", ) .unwrap(); std::fs::write( root.join("http-client.env.json"), r#"{"dev": {"base_url": "http://localhost:8080", "team": "core"}, "prod": {"base_url": "https://api.example.com"}}"#, ) .unwrap(); std::fs::write( root.join("http-client.private.env.json"), r#"{"dev": {"token": "dev-secret"}}"#, ) .unwrap(); let app = ReqliteApp::new(None, rt.handle().clone(), Arc::new(Canned), None); let mut h = Harness::builder() .with_size(egui::vec2(1280.0, 800.0)) .wgpu() .build_ui_state( |ui, app: &mut ReqliteApp| { egui_extras::install_image_loaders(ui.ctx()); app.frame(ui) }, app, ); shoot(&mut h, "01-welcome"); h.state_mut().dispatch(Action::OpenCollection(root.clone())); wait(&mut h, |a| { a.state.collections.first().is_some_and(|c| !c.envs.names().is_empty()) }); h.state_mut().dispatch(Action::SelectEnv(Some("dev".into()))); h.state_mut().dispatch(Action::OpenRequest { file: root.join("users.http"), index: 1, }); let id = h.state().state.active.unwrap(); h.state_mut().dispatch(Action::SetRequestTab(id, RequestTab::Body)); shoot(&mut h, "02-collection-body-tab"); h.state_mut().dispatch(Action::SetRequestTab(id, RequestTab::Headers)); h.state_mut().dispatch(Action::Send(id)); wait(&mut h, |a| { a.state .active_tab() .and_then(|t| t.response.as_ref()) .is_some_and(|r| r.prepared.is_some()) }); shoot(&mut h, "03-json-response"); h.state_mut().dispatch(Action::SearchOpen(id, true)); h.state_mut().dispatch(Action::SearchQuery(id, "a".into())); h.state_mut().dispatch(Action::SetRequestTab(id, RequestTab::Source)); shoot(&mut h, "04-source-and-search"); h.state_mut().dispatch(Action::SetResponseTab(id, ResponseTab::Timings)); h.state_mut().dispatch(Action::SetRequestTab(id, RequestTab::Auth)); shoot(&mut h, "05-auth-and-timings"); h.state_mut().dispatch(Action::OpenEnvEditor); shoot(&mut h, "06-env-editor"); h.state_mut().dispatch(Action::EnvEditorClose); let mut s = h.state().state.settings.clone(); s.theme = reqlite_app::settings::ThemeChoice::Light; h.state_mut().dispatch(Action::SettingsChanged(s)); h.state_mut().dispatch(Action::SetResponseTab(id, ResponseTab::Body)); h.state_mut().dispatch(Action::SetRequestTab(id, RequestTab::Params)); shoot(&mut h, "07-light-theme"); // A second collection and enough tabs to need the tab scroll bar. let mut s = h.state().state.settings.clone(); s.theme = reqlite_app::settings::ThemeChoice::Dark; h.state_mut().dispatch(Action::SettingsChanged(s)); let second = tempfile::tempdir().unwrap(); let root2 = second.path().canonicalize().unwrap(); std::fs::write( root2.join("billing.http"), "### Invoices\nGET {{base_url}}/invoices\n\n### Refund\nPOST {{base_url}}/refunds\n", ) .unwrap(); h.state_mut().dispatch(Action::OpenCollection(root2.clone())); wait(&mut h, |a| a.state.collections.len() == 2); for i in 0..2 { h.state_mut().dispatch(Action::OpenRequest { file: root2.join("billing.http"), index: i, }); } for i in 0..3 { h.state_mut().dispatch(Action::OpenRequest { file: root.join("users.http"), index: i, }); } for _ in 0..8 { h.state_mut().dispatch(Action::NewTab); } shoot(&mut h, "08-two-collections-many-tabs"); }