mod common; use std::io::Read; use std::time::{Duration, Instant}; use aur_build::db::model::BuildState; use aur_build::db::builds; use aur_build::pipeline; use aur_build::review::{candidates, diff}; use common::*; fn queued(env: &Env, name: &str) -> String { aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); let c = candidates::pending_candidates(&env.db).unwrap().remove(0); let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); aur_build::review::approve::approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap(); let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap() } fn drain(mut r: impl Read, deadline: Duration) -> String { let mut buf = [0u8; 4096]; let mut out = Vec::new(); let start = Instant::now(); loop { if start.elapsed() > deadline { break; } match r.read(&mut buf) { Ok(0) => break, Ok(n) => out.extend_from_slice(&buf[..n]), Err(_) => break, } } String::from_utf8_lossy(&out).into_owned() } #[test] fn the_log_stream_delivers_bytes_as_they_are_appended_and_then_ends() { let (env, fx) = env_with_fixture("plain-make"); let web = Web::attach(env, fx); let id = queued(&web.env, "plain-make"); let log = web.env.cfg.build_log_path(&id); std::fs::create_dir_all(log.parent().unwrap()).unwrap(); std::fs::write(&log, "==> step materialize\n").unwrap(); let db = web.env.db.clone(); let id2 = id.clone(); let log2 = log.clone(); let producer = std::thread::spawn(move || { std::thread::sleep(Duration::from_millis(400)); append(&log2, "==> step build\n"); std::thread::sleep(Duration::from_millis(400)); append(&log2, "==> step publish\n"); std::thread::sleep(Duration::from_millis(200)); builds::set_state(&db, &id2, BuildState::Running, None).unwrap(); builds::set_state(&db, &id2, BuildState::Succeeded, None).unwrap(); }); let resp = ureq::get(&web.url(&format!("/builds/{id}/log.stream"))).call().expect("stream"); assert_eq!(resp.header("content-type").unwrap_or(""), "text/event-stream"); assert_eq!(resp.header("cache-control").unwrap_or(""), "no-cache"); let body = drain(resp.into_reader(), Duration::from_secs(15)); producer.join().unwrap(); assert!(body.contains("data: ==> step materialize"), "the first line is missing: {body}"); assert!(body.contains("data: ==> step build"), "appended bytes never arrived: {body}"); assert!(body.contains("data: ==> step publish"), "the tail stopped early: {body}"); assert!(body.contains("event: end"), "the stream never ended: {body}"); assert!(body.contains("\n\n"), "not event-framed: {body}"); } #[test] fn the_log_stream_is_404_for_a_build_that_does_not_exist() { let (env, fx) = env_with_fixture("plain-make"); let web = Web::attach(env, fx); let resp = ureq::get(&web.url("/builds/nope/log.stream")).call(); match resp { Err(ureq::Error::Status(404, _)) => {} other => panic!("expected 404, got {other:?}"), } } #[test] fn the_builds_table_refreshes_via_htmx_and_still_works_without_js() { let (env, fx) = env_with_fixture("plain-make"); let web = Web::attach(env, fx); let base = web.url("/builds"); queued(&web.env, "plain-make"); let page = ureq::get(&base).call().unwrap().into_string().unwrap(); assert!(page.contains("hx-trigger=\"every 3s\""), "no htmx refresh: {}", &page[..400.min(page.len())]); assert!(page.contains("hx-get=\"/builds?fragment=1\""), "the refresh has no target"); assert!(page.contains("