forge
Explore
Log in

Don't you dare go Hollow

CI page tests share their fixtures and merge into tables

Browse files at this commit

de06f7447c999a578e3cd71bfef8309f88afeab6Ahmet Kaan GümüşParent: 3bcf844
  • test
    • CI page tests share their fixtures and merge into tables
Show full message
✅ test: CI page tests share their fixtures and merge into tables

ci_tests.rs: 32 -> 18 tests. Access, run list, live pages, job log events,
log tail, log.txt and follower, artifacts, badge, and run page previews
now share one fixture per group, each scenario in its own block with its
own runs and names. Finish-based scenarios finish their job before the next
one starts, since finish_job claims the oldest queued job.

ci_badge.rs: 6 -> 5 tests. The two escaping tests merge into one.

No assertion was removed or weakened. Clippy was skipped per the coordinator.
crates/forge-web/src/pages/ci_badge.rsmodified+1 -5
@@ -262,16 +262,12 @@ mod tests {
262 use super::*;262 use super::*;
263 263
264 #[test]264 #[test]
265- fn escapes_markup_and_quotes() {265+ fn markup_and_quotes_are_escaped_and_never_reach_the_svg_raw() {
266 assert_eq!(266 assert_eq!(
267 escape_xml(r#"<a href="x">'&'</a>"#),267 escape_xml(r#"<a href="x">'&'</a>"#),
268 "&lt;a href=&quot;x&quot;&gt;&apos;&amp;&apos;&lt;/a&gt;"268 "&lt;a href=&quot;x&quot;&gt;&apos;&amp;&apos;&lt;/a&gt;"
269 );269 );
270 assert_eq!(escape_xml("a\u{1}b"), "a b");270 assert_eq!(escape_xml("a\u{1}b"), "a b");
271- }
272-
273- #[test]
274- fn hostile_label_never_reaches_the_markup_raw() {
275 let svg = render_badge("<script>alert(1)</script>\"", BadgeStatus::Passing);271 let svg = render_badge("<script>alert(1)</script>\"", BadgeStatus::Passing);
276 assert!(!svg.contains("<script"));272 assert!(!svg.contains("<script"));
277 assert!(svg.contains("&lt;script&gt;alert(1)&lt;/script&gt;&quot;"));273 assert!(svg.contains("&lt;script&gt;alert(1)&lt;/script&gt;&quot;"));
crates/forge-web/src/pages/ci_tests.rsmodified+872 -904
@@ -208,145 +208,149 @@ async fn job_of(fx: &Fixture, run: &CiRun) -> CiJob {
208 .expect("a job")208 .expect("a job")
209 }209 }
210 210
211+/// Access follows the repo's role on every route: anonymous and outsider requests are 404, readers see the pages and streams, and only writers see or use the controls. Each scenario makes its own run.
211 #[tokio::test]212 #[tokio::test]
212-async fn private_repo_is_not_found_to_outsiders_and_anonymous() {213+async fn access_follows_the_repo_role_on_every_route() {
213 let dir = tempfile::tempdir().expect("tempdir");214 let dir = tempfile::tempdir().expect("tempdir");
214 let fx = fixture(dir.path()).await;215 let fx = fixture(dir.path()).await;
215- let run = run_with(&fx, &["build"], false).await;216+ {
216- let job = job_of(&fx, &run).await;217+ let run = run_with(&fx, &["build"], false).await;
217- let urls = [218+ let job = job_of(&fx, &run).await;
218- RUN_BASE.to_owned(),219+ let urls = [
219- format!("{RUN_BASE}/runs/{}", run.number),220+ RUN_BASE.to_owned(),
220- format!("/tahinli/proj/ci/jobs/{}", job.id),221+ format!("{RUN_BASE}/runs/{}", run.number),
221- format!("/tahinli/proj/ci/jobs/{}/log.txt", job.id),222+ format!("/tahinli/proj/ci/jobs/{}", job.id),
222- format!("/tahinli/proj/ci/jobs/{}/artifacts/x.bin", job.id),223+ format!("/tahinli/proj/ci/jobs/{}/log.txt", job.id),
223- ];224+ format!("/tahinli/proj/ci/jobs/{}/artifacts/x.bin", job.id),
224- for url in &urls {225+ ];
225- let anon = get(&fx.app, url, None).await;226+ for url in &urls {
226- assert_eq!(anon.status, 404, "anonymous {url}");227+ let anon = get(&fx.app, url, None).await;
227- let outsider = get(&fx.app, url, Some(&cookie_for("carol-token"))).await;228+ assert_eq!(anon.status, 404, "anonymous {url}");
228- assert_eq!(outsider.status, 404, "outsider {url}");229+ let outsider = get(&fx.app, url, Some(&cookie_for("carol-token"))).await;
230+ assert_eq!(outsider.status, 404, "outsider {url}");
231+ }
232+ let reader = get(&fx.app, RUN_BASE, Some(&cookie_for("dave-token"))).await;
233+ assert_eq!(reader.status, 200);
229 }234 }
230- let reader = get(&fx.app, RUN_BASE, Some(&cookie_for("dave-token"))).await;
231- assert_eq!(reader.status, 200);
232-}
233 235
234-#[tokio::test]236+ {
235-async fn buttons_and_manual_form_need_write() {237+ let run = run_with(&fx, &["build"], false).await;
236- let dir = tempfile::tempdir().expect("tempdir");238+ let run_url = format!("{RUN_BASE}/runs/{}", run.number);
237- let fx = fixture(dir.path()).await;239+ let writer = get(&fx.app, &run_url, Some(&cookie_for("bob-token"))).await;
238- let run = run_with(&fx, &["build"], false).await;240+ assert!(writer.text().contains(">Re-run<"), "writer sees re-run");
239- let run_url = format!("{RUN_BASE}/runs/{}", run.number);241+ assert!(writer.text().contains(">Cancel<"), "writer sees cancel");
240- let writer = get(&fx.app, &run_url, Some(&cookie_for("bob-token"))).await;242+ let reader = get(&fx.app, &run_url, Some(&cookie_for("dave-token"))).await;
241- assert!(writer.text().contains(">Re-run<"), "writer sees re-run");243+ assert_eq!(reader.status, 200);
242- assert!(writer.text().contains(">Cancel<"), "writer sees cancel");244+ assert!(!reader.text().contains(">Re-run<"), "reader sees no re-run");
243- let reader = get(&fx.app, &run_url, Some(&cookie_for("dave-token"))).await;245+ assert!(!reader.text().contains(">Cancel<"), "reader sees no cancel");
244- assert_eq!(reader.status, 200);246+ let list = get(&fx.app, RUN_BASE, Some(&cookie_for("dave-token"))).await;
245- assert!(!reader.text().contains(">Re-run<"), "reader sees no re-run");247+ assert!(
246- assert!(!reader.text().contains(">Cancel<"), "reader sees no cancel");248+ !list.text().contains("Run workflow"),
247- let list = get(&fx.app, RUN_BASE, Some(&cookie_for("dave-token"))).await;249+ "reader has no manual form"
248- assert!(250+ );
249- !list.text().contains("Run workflow"),251+ let list = get(&fx.app, RUN_BASE, Some(&cookie_for("bob-token"))).await;
250- "reader has no manual form"252+ assert!(
251- );253+ list.text().contains("Run workflow"),
252- let list = get(&fx.app, RUN_BASE, Some(&cookie_for("bob-token"))).await;254+ "writer has the manual form"
253- assert!(255+ );
254- list.text().contains("Run workflow"),256+ let refused = post(
255- "writer has the manual form"257+ &fx.app,
256- );258+ &format!("{run_url}/cancel"),
257- let refused = post(259+ &cookie_for("dave-token"),
258- &fx.app,260+ &[],
259- &format!("{run_url}/cancel"),261+ )
260- &cookie_for("dave-token"),262+ .await;
261- &[],263+ assert_eq!(refused.status, 403, "reader cannot cancel");
262- )264+ let still = fx
263- .await;265+ .store
264- assert_eq!(refused.status, 403, "reader cannot cancel");266+ .run_by_id(&run.id)
265- let still = fx267+ .await
266- .store268+ .expect("run")
267- .run_by_id(&run.id)269+ .expect("run");
268- .await270+ assert_eq!(still.state, RunState::Queued);
269- .expect("run")271+ }
270- .expect("run");
271- assert_eq!(still.state, RunState::Queued);
272-}
273 272
274-#[tokio::test]273+ {
275-async fn run_list_pages_25_at_a_time() {274+ let run = run_with(&fx, &["build"], false).await;
276- let dir = tempfile::tempdir().expect("tempdir");275+ let job = job_of(&fx, &run).await;
277- let fx = fixture(dir.path()).await;276+ let urls = [
278- for _ in 0..30 {277+ format!("{RUN_BASE}/runs/{}/events", run.number),
279- run_with(&fx, &[], false).await;278+ format!("/tahinli/proj/ci/jobs/{}/events", job.id),
279+ format!("{RUN_BASE}/events"),
280+ ];
281+ for url in &urls {
282+ let anon = open(&fx.app, url, None).await;
283+ assert_eq!(anon.status().as_u16(), 404, "anonymous {url}");
284+ let outsider = open(&fx.app, url, Some(&cookie_for("carol-token"))).await;
285+ assert_eq!(outsider.status().as_u16(), 404, "outsider {url}");
286+ let reader = open(&fx.app, url, Some(&cookie_for("dave-token"))).await;
287+ assert_eq!(reader.status().as_u16(), 200, "reader {url}");
288+ }
280 }289 }
281- let first = get(&fx.app, RUN_BASE, Some(&cookie_for("tahinli-token"))).await;
282- let text = first.text();
283- assert_eq!(text.matches("href=\"/tahinli/proj/ci/runs/").count(), 25);
284- assert!(text.contains("?page=2"), "older link");
285- assert!(!text.contains(">Newer<"));
286- let second = get(
287- &fx.app,
288- &format!("{RUN_BASE}?page=2"),
289- Some(&cookie_for("tahinli-token")),
290- )
291- .await;
292- let text = second.text();
293- assert_eq!(text.matches("href=\"/tahinli/proj/ci/runs/").count(), 5);
294- assert!(text.contains(">Newer<"), "newer link");
295- assert!(!text.contains("?page=3"), "no older link on the last page");
296- let bogus = get(
297- &fx.app,
298- &format!("{RUN_BASE}?page=abc"),
299- Some(&cookie_for("tahinli-token")),
300- )
301- .await;
302- assert_eq!(bogus.status, 200);
303- assert_eq!(
304- bogus
305- .text()
306- .matches("href=\"/tahinli/proj/ci/runs/")
307- .count(),
308- 25
309- );
310 }290 }
311 291
312-/// The pages no longer reload themselves. A page that is not finished carries its live marker, and292+/// The run list pages 25 at a time and is live on page 1 only. Thirty runs make two pages; page 2 is a fixed slice, so it carries no live marker.
313-/// a finished one carries none.
314 #[tokio::test]293 #[tokio::test]
315-async fn pages_are_live_while_not_finished_and_never_meta_refresh() {294+async fn run_list_pages_and_its_live_marker() {
316 let dir = tempfile::tempdir().expect("tempdir");295 let dir = tempfile::tempdir().expect("tempdir");
317 let fx = fixture(dir.path()).await;296 let fx = fixture(dir.path()).await;
318- let run = run_with(&fx, &["build"], false).await;297+ {
319- let job = job_of(&fx, &run).await;298+ for _ in 0..30 {
320- let url = format!("/tahinli/proj/ci/jobs/{}", job.id);299+ run_with(&fx, &[], false).await;
321- let run_url = format!("{RUN_BASE}/runs/{}", run.number);300+ }
322- let job_marker = format!("data-live=\"{url}/events\"");301+ let first = get(&fx.app, RUN_BASE, Some(&cookie_for("tahinli-token"))).await;
323- // The split view is live on page 1 through the repo stream, whatever the run's state.302+ let text = first.text();
324- let run_marker = format!("data-live=\"{RUN_BASE}/events\"");303+ assert_eq!(text.matches("href=\"/tahinli/proj/ci/runs/").count(), 25);
304+ assert!(text.contains("?page=2"), "older link");
305+ assert!(!text.contains(">Newer<"));
306+ let second = get(
307+ &fx.app,
308+ &format!("{RUN_BASE}?page=2"),
309+ Some(&cookie_for("tahinli-token")),
310+ )
311+ .await;
312+ let text = second.text();
313+ assert_eq!(text.matches("href=\"/tahinli/proj/ci/runs/").count(), 5);
314+ assert!(text.contains(">Newer<"), "newer link");
315+ assert!(!text.contains("?page=3"), "no older link on the last page");
316+ let bogus = get(
317+ &fx.app,
318+ &format!("{RUN_BASE}?page=abc"),
319+ Some(&cookie_for("tahinli-token")),
320+ )
321+ .await;
322+ assert_eq!(bogus.status, 200);
323+ assert_eq!(
324+ bogus
325+ .text()
326+ .matches("href=\"/tahinli/proj/ci/runs/")
327+ .count(),
328+ 25
329+ );
330+ }
325 331
326- let queued = get(&fx.app, &url, Some(&cookie_for("tahinli-token"))).await;332+ {
327- assert!(queued.text().contains(&job_marker), "job live while queued");333+ let marker = format!("data-live=\"{RUN_BASE}/events\"");
328- let run_page = get(&fx.app, &run_url, Some(&cookie_for("tahinli-token"))).await;334+ let first = get(&fx.app, RUN_BASE, Some(&cookie_for("tahinli-token"))).await;
329- assert!(335+ assert!(first.text().contains(&marker), "page 1 is live");
330- run_page.text().contains(&run_marker),336+ let second = get(
331- "run live while queued"337+ &fx.app,
332- );338+ &format!("{RUN_BASE}?page=2"),
333- for page in [&queued, &run_page] {339+ Some(&cookie_for("tahinli-token")),
340+ )
341+ .await;
342+ assert!(!second.text().contains("data-live="), "page 2 is not live");
343+ let run = get(
344+ &fx.app,
345+ &format!("{RUN_BASE}/runs/30?page=2"),
346+ Some(&cookie_for("tahinli-token")),
347+ )
348+ .await;
334 assert!(349 assert!(
335- !page.text().contains("http-equiv=\"refresh\""),350+ !run.text().contains("data-live="),
336- "no meta refresh"351+ "a run on page 2 is not live"
337 );352 );
338 }353 }
339-
340- finish_job(&fx).await;
341- let done = get(&fx.app, &url, Some(&cookie_for("tahinli-token"))).await;
342- assert!(
343- !done.text().contains("data-live="),
344- "job not live when finished"
345- );
346- assert!(
347- !done.text().contains("http-equiv=\"refresh\""),
348- "no meta refresh"
349- );
350 }354 }
351 355
352 /// Opens an event stream without reading it. The response head carries the status.356 /// Opens an event stream without reading it. The response head carries the status.
@@ -396,126 +400,110 @@ where
396 }400 }
397 }401 }
398 402
403+/// The pages no longer reload themselves, and a job's live streams end with the job. Each scenario finishes its own job first, since `finish_job` claims the oldest queued job.
399 #[tokio::test]404 #[tokio::test]
400-async fn live_events_need_the_same_access_as_the_page() {405+async fn live_pages_and_streams_follow_a_job_to_its_end() {
401 let dir = tempfile::tempdir().expect("tempdir");406 let dir = tempfile::tempdir().expect("tempdir");
402 let fx = fixture(dir.path()).await;407 let fx = fixture(dir.path()).await;
403- let run = run_with(&fx, &["build"], false).await;
404- let job = job_of(&fx, &run).await;
405- let urls = [
406- format!("{RUN_BASE}/runs/{}/events", run.number),
407- format!("/tahinli/proj/ci/jobs/{}/events", job.id),
408- format!("{RUN_BASE}/events"),
409- ];
410- for url in &urls {
411- let anon = open(&fx.app, url, None).await;
412- assert_eq!(anon.status().as_u16(), 404, "anonymous {url}");
413- let outsider = open(&fx.app, url, Some(&cookie_for("carol-token"))).await;
414- assert_eq!(outsider.status().as_u16(), 404, "outsider {url}");
415- let reader = open(&fx.app, url, Some(&cookie_for("dave-token"))).await;
416- assert_eq!(reader.status().as_u16(), 200, "reader {url}");
417- }
418-}
419-
420-/// Each stream opens with its status. The run stream follows every bump; the job stream sends only
421-/// status changes, so a log append is silent, and it ends once the job is terminal.
422-#[tokio::test]
423-async fn live_events_open_with_the_status_and_follow_changes() {
424 use futures_util::StreamExt;408 use futures_util::StreamExt;
425 409
426- let dir = tempfile::tempdir().expect("tempdir");410+ {
427- let fx = fixture(dir.path()).await;411+ let run = run_with(&fx, &["build"], false).await;
428- let run = run_with(&fx, &["build"], false).await;412+ let job = job_of(&fx, &run).await;
429- let job = job_of(&fx, &run).await;413+ let url = format!("/tahinli/proj/ci/jobs/{}", job.id);
430- let tahinli = cookie_for("tahinli-token");414+ let run_url = format!("{RUN_BASE}/runs/{}", run.number);
415+ let job_marker = format!("data-live=\"{url}/events\"");
416+ // The split view is live on page 1 through the repo stream, whatever the run's state.
417+ let run_marker = format!("data-live=\"{RUN_BASE}/events\"");
418+
419+ let queued = get(&fx.app, &url, Some(&cookie_for("tahinli-token"))).await;
420+ assert!(queued.text().contains(&job_marker), "job live while queued");
421+ let run_page = get(&fx.app, &run_url, Some(&cookie_for("tahinli-token"))).await;
422+ assert!(
423+ run_page.text().contains(&run_marker),
424+ "run live while queued"
425+ );
426+ for page in [&queued, &run_page] {
427+ assert!(
428+ !page.text().contains("http-equiv=\"refresh\""),
429+ "no meta refresh"
430+ );
431+ }
431 432
432- let mut run_frames = open(433+ finish_job(&fx).await;
433- &fx.app,434+ let done = get(&fx.app, &url, Some(&cookie_for("tahinli-token"))).await;
434- &format!("{RUN_BASE}/runs/{}/events", run.number),435+ assert!(
435- Some(&tahinli),436+ !done.text().contains("data-live="),
436- )437+ "job not live when finished"
437- .await438+ );
438- .into_body()439+ assert!(
439- .into_data_stream();440+ !done.text().contains("http-equiv=\"refresh\""),
440- let first = next_event(&mut run_frames).await.expect("the run status");441+ "no meta refresh"
441- assert_eq!(442+ );
442- first,443+ }
443- format!("event: changed\ndata: {}\n\n", run.state.as_str()).into_bytes()
444- );
445 444
446- let mut job_frames = open(445+ {
447- &fx.app,446+ let run = run_with(&fx, &["build"], false).await;
448- &format!("/tahinli/proj/ci/jobs/{}/events", job.id),447+ let job = job_of(&fx, &run).await;
449- Some(&tahinli),448+ let tahinli = cookie_for("tahinli-token");
450- )
451- .await
452- .into_body()
453- .into_data_stream();
454- let first = next_event(&mut job_frames).await.expect("the job status");
455- assert_eq!(
456- first,
457- format!("event: changed\ndata: {}\n\n", job.state.as_str()).into_bytes()
458- );
459 449
460- // A bump with no new log bytes and no status change sends nothing.450+ let mut run_frames = open(
461- fx.live.bump_job(&job.id);451+ &fx.app,
462- let quiet = tokio::time::timeout(Duration::from_millis(200), job_frames.next()).await;452+ &format!("{RUN_BASE}/runs/{}/events", run.number),
463- assert!(quiet.is_err(), "a log append sends no status event");453+ Some(&tahinli),
454+ )
455+ .await
456+ .into_body()
457+ .into_data_stream();
458+ let first = next_event(&mut run_frames).await.expect("the run status");
459+ assert_eq!(
460+ first,
461+ format!("event: changed\ndata: {}\n\n", run.state.as_str()).into_bytes()
462+ );
464 463
465- // The run stream sends its status on every bump of the run.464+ let mut job_frames = open(
466- fx.live.bump_run(&run.id);465+ &fx.app,
467- let bumped = next_event(&mut run_frames).await.expect("the run status");466+ &format!("/tahinli/proj/ci/jobs/{}/events", job.id),
468- let now = fx467+ Some(&tahinli),
469- .store468+ )
470- .run_by_id(&run.id)
471 .await469 .await
472- .expect("run")470+ .into_body()
473- .expect("run");471+ .into_data_stream();
474- assert_eq!(472+ let first = next_event(&mut job_frames).await.expect("the job status");
475- bumped,473+ assert_eq!(
476- format!("event: changed\ndata: {}\n\n", now.state.as_str()).into_bytes()474+ first,
477- );475+ format!("event: changed\ndata: {}\n\n", job.state.as_str()).into_bytes()
476+ );
478 477
479- // The job finishes: its final status is sent, then the stream ends.478+ // A bump with no new log bytes and no status change sends nothing.
480- finish_job(&fx).await;479+ fx.live.bump_job(&job.id);
481- fx.live.close_job(&job.id);480+ let quiet = tokio::time::timeout(Duration::from_millis(200), job_frames.next()).await;
482- let last = next_event(&mut job_frames).await.expect("the final status");481+ assert!(quiet.is_err(), "a log append sends no status event");
483- assert_eq!(
484- last,
485- format!("event: changed\ndata: {}\n\n", JobState::Success.as_str()).into_bytes()
486- );
487- assert!(next_event(&mut job_frames).await.is_none(), "stream ends");
488-}
489 482
490-/// The run list is live on page 1 only: a new run can appear there at any time. Page 2 is a fixed483+ // The run stream sends its status on every bump of the run.
491-/// slice, so it carries no live marker.484+ fx.live.bump_run(&run.id);
492-#[tokio::test]485+ let bumped = next_event(&mut run_frames).await.expect("the run status");
493-async fn run_list_is_live_on_page_one_only() {486+ let now = fx
494- let dir = tempfile::tempdir().expect("tempdir");487+ .store
495- let fx = fixture(dir.path()).await;488+ .run_by_id(&run.id)
496- for _ in 0..30 {489+ .await
497- run_with(&fx, &[], false).await;490+ .expect("run")
491+ .expect("run");
492+ assert_eq!(
493+ bumped,
494+ format!("event: changed\ndata: {}\n\n", now.state.as_str()).into_bytes()
495+ );
496+
497+ // The job finishes: its final status is sent, then the stream ends.
498+ finish_job(&fx).await;
499+ fx.live.close_job(&job.id);
500+ let last = next_event(&mut job_frames).await.expect("the final status");
501+ assert_eq!(
502+ last,
503+ format!("event: changed\ndata: {}\n\n", JobState::Success.as_str()).into_bytes()
504+ );
505+ assert!(next_event(&mut job_frames).await.is_none(), "stream ends");
498 }506 }
499- let marker = format!("data-live=\"{RUN_BASE}/events\"");
500- let first = get(&fx.app, RUN_BASE, Some(&cookie_for("tahinli-token"))).await;
501- assert!(first.text().contains(&marker), "page 1 is live");
502- let second = get(
503- &fx.app,
504- &format!("{RUN_BASE}?page=2"),
505- Some(&cookie_for("tahinli-token")),
506- )
507- .await;
508- assert!(!second.text().contains("data-live="), "page 2 is not live");
509- let run = get(
510- &fx.app,
511- &format!("{RUN_BASE}/runs/30?page=2"),
512- Some(&cookie_for("tahinli-token")),
513- )
514- .await;
515- assert!(
516- !run.text().contains("data-live="),
517- "a run on page 2 is not live"
518- );
519 }507 }
520 508
521 /// The href of the list row marked as current, from the markup of the runs page.509 /// The href of the list row marked as current, from the markup of the runs page.
@@ -607,208 +595,199 @@ async fn run_list_events_follow_repo_changes() {
607 assert_eq!(first, b"event: changed\ndata: list\n\n".to_vec());595 assert_eq!(first, b"event: changed\ndata: list\n\n".to_vec());
608 }596 }
609 597
610-/// The job's log events: each append is sent as a `log` event with its bytes, the id is the offset598+/// The job's log stream: each append is a `log` event with its bytes, the id is the offset past them, bytes before `?from=` are not sent again, a long append is split into 64 KiB events, and a reconnect resumes after `Last-Event-ID`.
611-/// past them, and the bytes before `?from=` are not sent again.
612 #[tokio::test]599 #[tokio::test]
613-async fn job_events_send_each_log_append_as_a_log_event() {600+async fn job_log_events() {
614 let dir = tempfile::tempdir().expect("tempdir");601 let dir = tempfile::tempdir().expect("tempdir");
615 let fx = fixture(dir.path()).await;602 let fx = fixture(dir.path()).await;
616- let run = run_with(&fx, &["build"], false).await;603+ {
617- let job = job_of(&fx, &run).await;604+ let run = run_with(&fx, &["build"], false).await;
618- let path = log_path(&fx.data_dir, &job.id);605+ let job = job_of(&fx, &run).await;
619- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");606+ let path = log_path(&fx.data_dir, &job.id);
620- std::fs::write(&path, b"one\n").expect("log");607+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
608+ std::fs::write(&path, b"one\n").expect("log");
621 609
622- let mut frames = open(610+ let mut frames = open(
623- &fx.app,611+ &fx.app,
624- &format!("/tahinli/proj/ci/jobs/{}/events?from=4", job.id),612+ &format!("/tahinli/proj/ci/jobs/{}/events?from=4", job.id),
625- Some(&cookie_for("tahinli-token")),613+ Some(&cookie_for("tahinli-token")),
626- )614+ )
627- .await615+ .await
628- .into_body()616+ .into_body()
629- .into_data_stream();617+ .into_data_stream();
630- let first = next_event(&mut frames).await.expect("the status");618+ let first = next_event(&mut frames).await.expect("the status");
631- assert_eq!(619+ assert_eq!(
632- first,620+ first,
633- format!("event: changed\ndata: {}\n\n", job.state.as_str()).into_bytes()621+ format!("event: changed\ndata: {}\n\n", job.state.as_str()).into_bytes()
634- );622+ );
635 623
636- let mut file = std::fs::OpenOptions::new()624+ let mut file = std::fs::OpenOptions::new()
637- .append(true)625+ .append(true)
638- .open(&path)626+ .open(&path)
639- .expect("log");627+ .expect("log");
640- file.write_all(b"two\nthree\n").expect("append");628+ file.write_all(b"two\nthree\n").expect("append");
641- fx.live.bump_job(&job.id);629+ fx.live.bump_job(&job.id);
642- let log = next_event(&mut frames).await.expect("the log event");630+ let log = next_event(&mut frames).await.expect("the log event");
643- assert_eq!(631+ assert_eq!(
644- log,632+ log,
645- b"event: log\ndata: two\ndata: three\ndata: \nid: 14\n\n".to_vec()633+ b"event: log\ndata: two\ndata: three\ndata: \nid: 14\n\n".to_vec()
646- );634+ );
647-}635+ }
648 636
649-/// An append longer than 64 KiB is sent in several `log` events, in order.637+ {
650-#[tokio::test]638+ let run = run_with(&fx, &["build"], false).await;
651-async fn job_events_split_a_long_append_into_64_kib_events() {639+ let job = job_of(&fx, &run).await;
652- let dir = tempfile::tempdir().expect("tempdir");640+ let path = log_path(&fx.data_dir, &job.id);
653- let fx = fixture(dir.path()).await;641+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
654- let run = run_with(&fx, &["build"], false).await;642+ std::fs::write(&path, b"").expect("log");
655- let job = job_of(&fx, &run).await;
656- let path = log_path(&fx.data_dir, &job.id);
657- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
658- std::fs::write(&path, b"").expect("log");
659 643
660- let mut frames = open(644+ let mut frames = open(
661- &fx.app,645+ &fx.app,
662- &format!("/tahinli/proj/ci/jobs/{}/events", job.id),646+ &format!("/tahinli/proj/ci/jobs/{}/events", job.id),
663- Some(&cookie_for("tahinli-token")),647+ Some(&cookie_for("tahinli-token")),
664- )648+ )
665- .await649+ .await
666- .into_body()650+ .into_body()
667- .into_data_stream();651+ .into_data_stream();
668- next_event(&mut frames).await.expect("the status");652+ next_event(&mut frames).await.expect("the status");
669-653+
670- let long = "a".repeat(150 * 1024);654+ let long = "a".repeat(150 * 1024);
671- let mut file = std::fs::OpenOptions::new()655+ let mut file = std::fs::OpenOptions::new()
672- .append(true)656+ .append(true)
673- .open(&path)657+ .open(&path)
674- .expect("log");658+ .expect("log");
675- file.write_all(long.as_bytes()).expect("append");659+ file.write_all(long.as_bytes()).expect("append");
676- fx.live.bump_job(&job.id);660+ fx.live.bump_job(&job.id);
677-661+
678- let mut sizes = Vec::new();662+ let mut sizes = Vec::new();
679- for _ in 0..3 {663+ for _ in 0..3 {
680- let frame = next_event(&mut frames).await.expect("a log event");664+ let frame = next_event(&mut frames).await.expect("a log event");
681- let text = String::from_utf8(frame).expect("text");665+ let text = String::from_utf8(frame).expect("text");
682- assert!(text.starts_with("event: log\n"), "{text}");666+ assert!(text.starts_with("event: log\n"), "{text}");
683- let data = text667+ let data = text
684- .lines()668+ .lines()
685- .find_map(|line| line.strip_prefix("data: "))669+ .find_map(|line| line.strip_prefix("data: "))
686- .expect("data line");670+ .expect("data line");
687- sizes.push(data.len());671+ sizes.push(data.len());
688- if sizes.len() == 3 {672+ if sizes.len() == 3 {
689- assert!(673+ assert!(
690- text.contains("id: 153600\n"),674+ text.contains("id: 153600\n"),
691- "the last id is the end offset"675+ "the last id is the end offset"
692- );676+ );
677+ }
693 }678 }
679+ assert_eq!(sizes, vec![64 * 1024, 64 * 1024, 150 * 1024 - 128 * 1024]);
694 }680 }
695- assert_eq!(sizes, vec![64 * 1024, 64 * 1024, 150 * 1024 - 128 * 1024]);
696-}
697 681
698-/// A reconnect sends `Last-Event-ID`, and the stream resumes after that offset, not at `?from=`.682+ {
699-#[tokio::test]683+ let run = run_with(&fx, &["build"], false).await;
700-async fn job_events_resume_after_the_last_event_id() {684+ let job = job_of(&fx, &run).await;
701- let dir = tempfile::tempdir().expect("tempdir");685+ let path = log_path(&fx.data_dir, &job.id);
702- let fx = fixture(dir.path()).await;686+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
703- let run = run_with(&fx, &["build"], false).await;687+ std::fs::write(&path, b"one\ntwo\n").expect("log");
704- let job = job_of(&fx, &run).await;688+
705- let path = log_path(&fx.data_dir, &job.id);689+ let request = Request::builder()
706- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");690+ .method("GET")
707- std::fs::write(&path, b"one\ntwo\n").expect("log");691+ .uri(format!("/tahinli/proj/ci/jobs/{}/events?from=0", job.id))
708-692+ .header("cookie", cookie_for("tahinli-token"))
709- let request = Request::builder()693+ .header("last-event-id", "4")
710- .method("GET")694+ .body(Body::from(String::new()))
711- .uri(format!("/tahinli/proj/ci/jobs/{}/events?from=0", job.id))695+ .expect("request");
712- .header("cookie", cookie_for("tahinli-token"))696+ let mut frames = fx.app.handle(request).await.into_body().into_data_stream();
713- .header("last-event-id", "4")697+ next_event(&mut frames).await.expect("the status");
714- .body(Body::from(String::new()))698+ let log = next_event(&mut frames).await.expect("the log event");
715- .expect("request");699+ assert_eq!(log, b"event: log\ndata: two\ndata: \nid: 8\n\n".to_vec());
716- let mut frames = fx.app.handle(request).await.into_body().into_data_stream();700+ }
717- next_event(&mut frames).await.expect("the status");
718- let log = next_event(&mut frames).await.expect("the log event");
719- assert_eq!(log, b"event: log\ndata: two\ndata: \nid: 8\n\n".to_vec());
720 }701 }
721 702
722-/// The job page's log tail says where its text ends, so the events stream starts from there.703+/// The job page's log tail says where its text ends, tails the last 512 KiB, and a cut log starts at its own line number.
723 #[tokio::test]704 #[tokio::test]
724-async fn job_page_tail_carries_its_offset() {705+async fn job_page_log_tail() {
725 let dir = tempfile::tempdir().expect("tempdir");706 let dir = tempfile::tempdir().expect("tempdir");
726 let fx = fixture(dir.path()).await;707 let fx = fixture(dir.path()).await;
727- let run = run_with(&fx, &["build"], false).await;708+ {
728- let job = job_of(&fx, &run).await;709+ let run = run_with(&fx, &["build"], false).await;
729- let path = log_path(&fx.data_dir, &job.id);710+ let job = job_of(&fx, &run).await;
730- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");711+ let path = log_path(&fx.data_dir, &job.id);
731- std::fs::write(&path, b"one\ntwo\n").expect("log");712+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
713+ std::fs::write(&path, b"one\ntwo\n").expect("log");
732 714
733- let page = get(715+ let page = get(
734- &fx.app,716+ &fx.app,
735- &format!("/tahinli/proj/ci/jobs/{}", job.id),717+ &format!("/tahinli/proj/ci/jobs/{}", job.id),
736- Some(&cookie_for("tahinli-token")),718+ Some(&cookie_for("tahinli-token")),
737- )719+ )
738- .await;720+ .await;
739- let text = page.text();721+ let text = page.text();
740- assert!(722+ assert!(
741- text.contains("data-live-log=\"true\" data-offset=\"8\""),723+ text.contains("data-live-log=\"true\" data-offset=\"8\""),
742- "tail offset"724+ "tail offset"
743- );725+ );
744- assert!(726+ assert!(
745- text.contains(&format!("data-live-state=\"{}\"", job.state.as_str())),727+ text.contains(&format!("data-live-state=\"{}\"", job.state.as_str())),
746- "rendered status"728+ "rendered status"
747- );729+ );
748-}730+ }
749 731
750-#[tokio::test]732+ {
751-async fn job_page_tails_the_log_at_512_kib() {733+ let run = run_with(&fx, &["build"], false).await;
752- let dir = tempfile::tempdir().expect("tempdir");734+ let job = job_of(&fx, &run).await;
753- let fx = fixture(dir.path()).await;735+ let path = log_path(&fx.data_dir, &job.id);
754- let run = run_with(&fx, &["build"], false).await;736+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
755- let job = job_of(&fx, &run).await;737+ let mut body = b"HEAD-MARK\n".to_vec();
756- let path = log_path(&fx.data_dir, &job.id);738+ let line = format!("{}\n", "x".repeat(99));
757- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");739+ for _ in 0..6_000 {
758- let mut body = b"HEAD-MARK\n".to_vec();740+ body.extend_from_slice(line.as_bytes());
759- let line = format!("{}\n", "x".repeat(99));741+ }
760- for _ in 0..6_000 {742+ body.extend_from_slice(b"TAIL-MARK\n");
761- body.extend_from_slice(line.as_bytes());743+ std::fs::write(&path, &body).expect("log");
744+ let page = get(
745+ &fx.app,
746+ &format!("/tahinli/proj/ci/jobs/{}", job.id),
747+ Some(&cookie_for("tahinli-token")),
748+ )
749+ .await;
750+ let text = page.text();
751+ assert!(text.contains("TAIL-MARK"));
752+ assert!(!text.contains("HEAD-MARK"));
753+ assert!(text.contains("Showing the last 512 KiB"));
754+ assert!(
755+ page.body.len() < 530 * 1024,
756+ "page is {} bytes",
757+ page.body.len()
758+ );
762 }759 }
763- body.extend_from_slice(b"TAIL-MARK\n");
764- std::fs::write(&path, &body).expect("log");
765- let page = get(
766- &fx.app,
767- &format!("/tahinli/proj/ci/jobs/{}", job.id),
768- Some(&cookie_for("tahinli-token")),
769- )
770- .await;
771- let text = page.text();
772- assert!(text.contains("TAIL-MARK"));
773- assert!(!text.contains("HEAD-MARK"));
774- assert!(text.contains("Showing the last 512 KiB"));
775- assert!(
776- page.body.len() < 530 * 1024,
777- "page is {} bytes",
778- page.body.len()
779- );
780-}
781 760
782-#[tokio::test]761+ {
783-async fn log_txt_is_the_full_file_for_a_finished_job() {762+ let run = run_with(&fx, &["build"], false).await;
784- let dir = tempfile::tempdir().expect("tempdir");763+ let job = job_of(&fx, &run).await;
785- let fx = fixture(dir.path()).await;764+ let path = log_path(&fx.data_dir, &job.id);
786- let run = run_with(&fx, &["build"], false).await;765+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
787- let job = job_of(&fx, &run).await;766+ let mut file = std::fs::File::create(&path).unwrap();
788- let path = log_path(&fx.data_dir, &job.id);767+ // 11000 lines of 100 bytes cut the 512 KiB tail somewhere past line 5000.
789- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");768+ for n in 0..11_000 {
790- std::fs::write(&path, b"line one\nline two\n").expect("log");769+ writeln!(file, "{n:0>98}").unwrap();
791- finish_job(&fx).await;770+ }
792- let res = get(771+ drop(file);
793- &fx.app,772+ let page = get(
794- &format!("/tahinli/proj/ci/jobs/{}/log.txt", job.id),773+ &fx.app,
795- Some(&cookie_for("tahinli-token")),774+ &format!("/tahinli/proj/ci/jobs/{}", job.id),
796- )775+ Some(&cookie_for("dave-token")),
797- .await;776+ )
798- assert_eq!(res.status, 200);777+ .await
799- assert_eq!(res.body, b"line one\nline two\n");778+ .text();
800- assert_eq!(779+ let marker = "data-first=\"";
801- res.headers780+ let at = page.find(marker).expect("data-first") + marker.len();
802- .get("content-type")781+ let first: u64 = page[at..].split('"').next().unwrap().parse().unwrap();
803- .and_then(|v| v.to_str().ok()),782+ assert!(first > 5000, "first line {first}");
804- Some("text/plain; charset=utf-8")783+ let after = &page[at..];
805- );784+ let text = &after[after.find('>').unwrap() + 1..];
806- assert_eq!(785+ let expected = format!("{:0>98}", first - 1);
807- res.headers786+ assert!(
808- .get("x-content-type-options")787+ text.starts_with(&expected),
809- .and_then(|v| v.to_str().ok()),788+ "the tail starts at its own line number"
810- Some("nosniff")789+ );
811- );790+ }
812 }791 }
813 792
814 /// The next chunk a follower sends. Fails the test when none comes within five seconds.793 /// The next chunk a follower sends. Fails the test when none comes within five seconds.
@@ -820,101 +799,197 @@ async fn next_chunk(
820 .expect("a chunk within five seconds")799 .expect("a chunk within five seconds")
821 }800 }
822 801
823-/// A follower sends each append as soon as the job's signal is bumped. The wait is far longer802+/// `log.txt` is the full file of a finished job, and a log follower is woken by bumps and ends once the job is terminal.
824-/// than the timeouts here, so a chunk that arrives in time was woken by the bump, not by a timer.
825 #[tokio::test]803 #[tokio::test]
826-async fn log_follower_is_woken_by_bumps_and_ends_on_close() {804+async fn log_txt_and_log_follower() {
827- use futures_util::StreamExt;
828-
829 let dir = tempfile::tempdir().expect("tempdir");805 let dir = tempfile::tempdir().expect("tempdir");
830 let fx = fixture(dir.path()).await;806 let fx = fixture(dir.path()).await;
831- let run = run_with(&fx, &["build"], false).await;807+ use futures_util::StreamExt;
832- let job = job_of(&fx, &run).await;808+
833- let path = log_path(&fx.data_dir, &job.id);809+ {
834- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");810+ let run = run_with(&fx, &["build"], false).await;
835- std::fs::write(&path, b"one\n").expect("log");811+ let job = job_of(&fx, &run).await;
836- let live = Arc::new(CiLive::default());812+ let path = log_path(&fx.data_dir, &job.id);
837- let mut stream = Box::pin(super::ci_job::follow_log(813+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
838- fx.store.clone(),814+ std::fs::write(&path, b"line one\nline two\n").expect("log");
839- Arc::clone(&live),815+ finish_job(&fx).await;
840- job.id.clone(),816+ let res = get(
841- path.clone(),817+ &fx.app,
842- ));818+ &format!("/tahinli/proj/ci/jobs/{}/log.txt", job.id),
843- let (tx, mut chunks) = tokio::sync::mpsc::unbounded_channel();819+ Some(&cookie_for("tahinli-token")),
844- tokio::spawn(async move {820+ )
845- while let Some(Ok(chunk)) = stream.next().await {821+ .await;
846- if tx.send(chunk).is_err() {822+ assert_eq!(res.status, 200);
847- break;823+ assert_eq!(res.body, b"line one\nline two\n");
824+ assert_eq!(
825+ res.headers
826+ .get("content-type")
827+ .and_then(|v| v.to_str().ok()),
828+ Some("text/plain; charset=utf-8")
829+ );
830+ assert_eq!(
831+ res.headers
832+ .get("x-content-type-options")
833+ .and_then(|v| v.to_str().ok()),
834+ Some("nosniff")
835+ );
836+ }
837+
838+ {
839+ let run = run_with(&fx, &["build"], false).await;
840+ let job = job_of(&fx, &run).await;
841+ let path = log_path(&fx.data_dir, &job.id);
842+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
843+ std::fs::write(&path, b"one\n").expect("log");
844+ let live = Arc::new(CiLive::default());
845+ let mut stream = Box::pin(super::ci_job::follow_log(
846+ fx.store.clone(),
847+ Arc::clone(&live),
848+ job.id.clone(),
849+ path.clone(),
850+ ));
851+ let (tx, mut chunks) = tokio::sync::mpsc::unbounded_channel();
852+ tokio::spawn(async move {
853+ while let Some(Ok(chunk)) = stream.next().await {
854+ if tx.send(chunk).is_err() {
855+ break;
856+ }
848 }857 }
849- }858+ });
850- });859+ assert_eq!(
851- assert_eq!(860+ next_chunk(&mut chunks).await.as_deref(),
852- next_chunk(&mut chunks).await.as_deref(),861+ Some(&b"one\n"[..])
853- Some(&b"one\n"[..])862+ );
854- );
855 863
856- let mut file = std::fs::OpenOptions::new()864+ let mut file = std::fs::OpenOptions::new()
857- .append(true)865+ .append(true)
858- .open(&path)866+ .open(&path)
859- .expect("log");867+ .expect("log");
860- file.write_all(b"two\n").expect("append");868+ file.write_all(b"two\n").expect("append");
861- live.bump_job(&job.id);869+ live.bump_job(&job.id);
862- assert_eq!(870+ assert_eq!(
863- next_chunk(&mut chunks).await.as_deref(),871+ next_chunk(&mut chunks).await.as_deref(),
864- Some(&b"two\n"[..])872+ Some(&b"two\n"[..])
865- );873+ );
866 874
867- finish_job(&fx).await;875+ finish_job(&fx).await;
868- live.close_job(&job.id);876+ live.close_job(&job.id);
869- let end = tokio::time::timeout(Duration::from_secs(5), chunks.recv())877+ let end = tokio::time::timeout(Duration::from_secs(5), chunks.recv())
870- .await878+ .await
871- .expect("the stream ends once the job is terminal");879+ .expect("the stream ends once the job is terminal");
872- assert!(end.is_none());880+ assert!(end.is_none());
881+ }
873 }882 }
874 883
884+/// Artifact downloads send attachment headers, and a run's artifacts list and zip for readers only.
875 #[tokio::test]885 #[tokio::test]
876-async fn artifact_download_sends_attachment_headers() {886+async fn artifact_downloads_and_zip() {
877 let dir = tempfile::tempdir().expect("tempdir");887 let dir = tempfile::tempdir().expect("tempdir");
878 let fx = fixture(dir.path()).await;888 let fx = fixture(dir.path()).await;
879- let run = run_with(&fx, &["build"], false).await;889+ {
880- let job = job_of(&fx, &run).await;890+ let run = run_with(&fx, &["build"], false).await;
881- let payload = b"artifact bytes";891+ let job = job_of(&fx, &run).await;
882- let artifact = fx892+ let payload = b"artifact bytes";
883- .store893+ let artifact = fx
884- .add_artifact(&job.id, "report.txt", payload.len() as i64, SHA)894+ .store
885- .await895+ .add_artifact(&job.id, "report.txt", payload.len() as i64, SHA)
886- .expect("artifact row");896+ .await
887- let mut writer = fx897+ .expect("artifact row");
888- .blobs898+ let mut writer = fx
889- .begin(&artifact_key(&artifact.id), 1024)899+ .blobs
890- .await900+ .begin(&artifact_key(&artifact.id), 1024)
891- .expect("begin");901+ .await
892- writer.push(payload).await.expect("push");902+ .expect("begin");
893- writer.finish().await.expect("finish");903+ writer.push(payload).await.expect("push");
894- let res = get(904+ writer.finish().await.expect("finish");
895- &fx.app,905+ let res = get(
896- &format!("/tahinli/proj/ci/jobs/{}/artifacts/report.txt", job.id),906+ &fx.app,
897- Some(&cookie_for("tahinli-token")),907+ &format!("/tahinli/proj/ci/jobs/{}/artifacts/report.txt", job.id),
898- )908+ Some(&cookie_for("tahinli-token")),
899- .await;909+ )
900- assert_eq!(res.status, 200);910+ .await;
901- assert_eq!(res.body, payload);911+ assert_eq!(res.status, 200);
902- let header = |name: &str| {912+ assert_eq!(res.body, payload);
903- res.headers913+ let header = |name: &str| {
904- .get(name)914+ res.headers
905- .and_then(|v| v.to_str().ok())915+ .get(name)
906- .map(str::to_owned)916+ .and_then(|v| v.to_str().ok())
907- };917+ .map(str::to_owned)
908- assert_eq!(918+ };
909- header("content-disposition").as_deref(),919+ assert_eq!(
910- Some("attachment; filename=\"report.txt\"")920+ header("content-disposition").as_deref(),
911- );921+ Some("attachment; filename=\"report.txt\"")
912- assert_eq!(header("content-length").as_deref(), Some("14"));922+ );
913- assert_eq!(header("x-content-type-options").as_deref(), Some("nosniff"));923+ assert_eq!(header("content-length").as_deref(), Some("14"));
914- assert_eq!(924+ assert_eq!(header("x-content-type-options").as_deref(), Some("nosniff"));
915- header("content-type").as_deref(),925+ assert_eq!(
916- Some("application/octet-stream")926+ header("content-type").as_deref(),
917- );927+ Some("application/octet-stream")
928+ );
929+ }
930+
931+ {
932+ let run = run_with(&fx, &["build", "test"], false).await;
933+ let jobs = fx.store.jobs_for_run(&run.id).await.unwrap();
934+ for (job, name, payload) in [
935+ (&jobs[0], "report.txt", &b"alpha"[..]),
936+ (&jobs[1], "out.bin", &b"beta"[..]),
937+ ] {
938+ let artifact = fx
939+ .store
940+ .add_artifact(&job.id, name, payload.len() as i64, SHA)
941+ .await
942+ .expect("artifact row");
943+ let mut writer = fx
944+ .blobs
945+ .begin(&artifact_key(&artifact.id), 1024)
946+ .await
947+ .expect("begin");
948+ writer.push(payload).await.expect("push");
949+ writer.finish().await.expect("finish");
950+ }
951+ let reader = cookie_for("dave-token");
952+ let run_url = format!("{RUN_BASE}/runs/{}", run.number);
953+ let page = get(&fx.app, &run_url, Some(&reader)).await.text();
954+ assert!(page.contains("Download all (.zip)"));
955+ assert!(page.contains("artifacts.zip"));
956+ assert!(page.contains("report.txt") && page.contains("out.bin"));
957+
958+ let res = get(&fx.app, &format!("{run_url}/artifacts.zip"), Some(&reader)).await;
959+ assert_eq!(res.status, 200);
960+ assert_eq!(
961+ res.headers
962+ .get("content-type")
963+ .and_then(|v| v.to_str().ok()),
964+ Some("application/zip")
965+ );
966+ let mut archive =
967+ zip::ZipArchive::new(std::io::Cursor::new(res.body.clone())).expect("zip");
968+ assert_eq!(archive.len(), 2);
969+ let mut text = String::new();
970+ std::io::Read::read_to_string(
971+ &mut archive.by_name("build/report.txt").expect("entry"),
972+ &mut text,
973+ )
974+ .expect("read");
975+ assert_eq!(text, "alpha");
976+
977+ let outsider = get(
978+ &fx.app,
979+ &format!("{run_url}/artifacts.zip"),
980+ Some(&cookie_for("carol-token")),
981+ )
982+ .await;
983+ assert_eq!(outsider.status, 404, "a private repo stays private");
984+ let empty = run_with(&fx, &["lint"], false).await;
985+ let none = get(
986+ &fx.app,
987+ &format!("{RUN_BASE}/runs/{}/artifacts.zip", empty.number),
988+ Some(&reader),
989+ )
990+ .await;
991+ assert_eq!(none.status, 404);
992+ }
918 }993 }
919 994
920 #[tokio::test]995 #[tokio::test]
@@ -1061,83 +1136,104 @@ async fn manual_run_refuses_an_unknown_branch() {
1061 let reader = post(1136 let reader = post(
1062 &fx.app,1137 &fx.app,
1063 RUN_BASE,1138 RUN_BASE,
1064- &cookie_for("dave-token"),1139+ &cookie_for("dave-token"),
1065- &[("branch", "main")],1140+ &[("branch", "main")],
1066- )1141+ )
1067- .await;1142+ .await;
1068- assert_eq!(reader.status, 403, "reader cannot start a manual run");1143+ assert_eq!(reader.status, 403, "reader cannot start a manual run");
1069-}1144+}
1070-1145+
1071-#[tokio::test]1146+/// The badge follows read access, and a repo with no run answers 200 with "no status". It runs before any run exists on the private repo.
1072-async fn badge_follows_read_access_and_reports_the_latest_run() {1147+#[tokio::test]
1073- let dir = tempfile::tempdir().expect("tempdir");1148+async fn badge_follows_read_access_and_has_no_run_before_one_exists() {
1074- let fx = fixture(dir.path()).await;1149+ let dir = tempfile::tempdir().expect("tempdir");
1075- let open = fx1150+ let fx = fixture(dir.path()).await;
1076- .store1151+ {
1077- .create_repo(&fx.tahinli, "open", "", Visibility::Public, "main")1152+ let reader = Some(cookie_for("dave-token"));
1078- .await1153+ let plain = get(&fx.app, "/tahinli/proj/badge.svg", reader.as_deref())
1079- .expect("public repo");1154+ .await
1080- let svg_path = "/tahinli/proj/badge.svg";1155+ .text();
1081-1156+ assert!(plain.contains(">build</text>"), "{plain}");
1082- // The private repo: a run exists, but only readers see its badge.1157+ assert!(plain.contains(">no status</text>"));
1083- run_with(&fx, &["build"], false).await;1158+ let labelled = get(
1084- finish_job(&fx).await;1159+ &fx.app,
1085- assert_eq!(get(&fx.app, svg_path, None).await.status, 404, "anonymous");1160+ "/tahinli/proj/badge.svg?label=Nightly",
1086- let outsider = get(&fx.app, svg_path, Some(&cookie_for("carol-token"))).await;1161+ reader.as_deref(),
1087- assert_eq!(outsider.status, 404, "outsider");
1088- let reader = get(&fx.app, svg_path, Some(&cookie_for("dave-token"))).await;
1089- assert_eq!(reader.status, 200, "reader");
1090- assert!(reader.text().contains("passing"));
1091- assert_eq!(
1092- reader
1093- .headers
1094- .get("content-type")
1095- .and_then(|v| v.to_str().ok()),
1096- Some("image/svg+xml")
1097- );
1098- assert_eq!(
1099- reader
1100- .headers
1101- .get("cache-control")
1102- .and_then(|v| v.to_str().ok()),
1103- Some("no-cache, max-age=0")
1104- );
1105-
1106- // The public repo: anyone sees the badge. Unknown branches and jobs have no status.
1107- let needs: &[String] = &[];
1108- let specs = [NewJob {
1109- name: "build",
1110- spec_json: SPEC,
1111- needs,
1112- }];
1113- fx.store
1114- .create_run(
1115- &open.id,
1116- "push",
1117- "refs/heads/main",
1118- SHA,
1119- None,
1120- &fx.tahinli,
1121- false,
1122- &specs,
1123- None,
1124 )1162 )
1125 .await1163 .await
1126- .expect("run");1164+ .text();
1127- finish_job(&fx).await;1165+ assert!(labelled.contains(">Nightly</text>"), "{labelled}");
1128- let anon = get(&fx.app, "/tahinli/open/badge.svg", None).await;1166+ assert!(labelled.contains(">no status</text>"));
1129- assert_eq!(anon.status, 200);1167+ }
1130- assert!(anon.text().contains(">passing</text>"));1168+
1131- let unknown = get(&fx.app, "/tahinli/open/badge.svg?branch=nope", None).await;1169+ {
1132- assert_eq!(unknown.status, 200);1170+ let open = fx
1133- assert!(unknown.text().contains(">no status</text>"));1171+ .store
1134- let job = get(&fx.app, "/tahinli/open/badge.svg?job=build", None).await;1172+ .create_repo(&fx.tahinli, "open", "", Visibility::Public, "main")
1135- assert!(job.text().contains(">build</text>"));1173+ .await
1136- assert!(job.text().contains(">passing</text>"));1174+ .expect("public repo");
1137- let missing_job = get(&fx.app, "/tahinli/open/badge.svg?job=lint", None).await;1175+ let svg_path = "/tahinli/proj/badge.svg";
1138- assert!(missing_job.text().contains(">no status</text>"));1176+
1139- let unknown_repo = get(&fx.app, "/tahinli/nothere/badge.svg", None).await;1177+ // The private repo: a run exists, but only readers see its badge.
1140- assert_eq!(unknown_repo.status, 404);1178+ run_with(&fx, &["build"], false).await;
1179+ finish_job(&fx).await;
1180+ assert_eq!(get(&fx.app, svg_path, None).await.status, 404, "anonymous");
1181+ let outsider = get(&fx.app, svg_path, Some(&cookie_for("carol-token"))).await;
1182+ assert_eq!(outsider.status, 404, "outsider");
1183+ let reader = get(&fx.app, svg_path, Some(&cookie_for("dave-token"))).await;
1184+ assert_eq!(reader.status, 200, "reader");
1185+ assert!(reader.text().contains("passing"));
1186+ assert_eq!(
1187+ reader
1188+ .headers
1189+ .get("content-type")
1190+ .and_then(|v| v.to_str().ok()),
1191+ Some("image/svg+xml")
1192+ );
1193+ assert_eq!(
1194+ reader
1195+ .headers
1196+ .get("cache-control")
1197+ .and_then(|v| v.to_str().ok()),
1198+ Some("no-cache, max-age=0")
1199+ );
1200+
1201+ // The public repo: anyone sees the badge. Unknown branches and jobs have no status.
1202+ let needs: &[String] = &[];
1203+ let specs = [NewJob {
1204+ name: "build",
1205+ spec_json: SPEC,
1206+ needs,
1207+ }];
1208+ fx.store
1209+ .create_run(
1210+ &open.id,
1211+ "push",
1212+ "refs/heads/main",
1213+ SHA,
1214+ None,
1215+ &fx.tahinli,
1216+ false,
1217+ &specs,
1218+ None,
1219+ )
1220+ .await
1221+ .expect("run");
1222+ finish_job(&fx).await;
1223+ let anon = get(&fx.app, "/tahinli/open/badge.svg", None).await;
1224+ assert_eq!(anon.status, 200);
1225+ assert!(anon.text().contains(">passing</text>"));
1226+ let unknown = get(&fx.app, "/tahinli/open/badge.svg?branch=nope", None).await;
1227+ assert_eq!(unknown.status, 200);
1228+ assert!(unknown.text().contains(">no status</text>"));
1229+ let job = get(&fx.app, "/tahinli/open/badge.svg?job=build", None).await;
1230+ assert!(job.text().contains(">build</text>"));
1231+ assert!(job.text().contains(">passing</text>"));
1232+ let missing_job = get(&fx.app, "/tahinli/open/badge.svg?job=lint", None).await;
1233+ assert!(missing_job.text().contains(">no status</text>"));
1234+ let unknown_repo = get(&fx.app, "/tahinli/nothere/badge.svg", None).await;
1235+ assert_eq!(unknown_repo.status, 404);
1236+ }
1141 }1237 }
1142 1238
1143 /// Writes `.forge/ci.toml` with `text` into a new commit of the bare repo and returns its id.1239 /// Writes `.forge/ci.toml` with `text` into a new commit of the bare repo and returns its id.
@@ -1203,199 +1299,230 @@ async fn run_at(fx: &Fixture, commit: &str) {
1203 .expect("run");1299 .expect("run");
1204 }1300 }
1205 1301
1302+/// The badge label comes from `?label=`, the job or the workflow name, trimmed and escaped, with "build" as the fallback. Each scenario finishes its own run, so the latest run is the one it made.
1206 #[tokio::test]1303 #[tokio::test]
1207-async fn badge_label_comes_from_the_query_job_or_workflow_name() {1304+async fn badge_label_comes_from_the_query_job_or_name() {
1208 let dir = tempfile::tempdir().expect("tempdir");1305 let dir = tempfile::tempdir().expect("tempdir");
1209 let fx = fixture(dir.path()).await;1306 let fx = fixture(dir.path()).await;
1210- let bare = repo_path(&fx.data_dir, &fx.repo.id).expect("repo path");1307+ {
1211- let commit = commit_ci_toml(1308+ let bare = repo_path(&fx.data_dir, &fx.repo.id).expect("repo path");
1212- &bare,1309+ let commit = commit_ci_toml(
1213- "name = \" release-check \"\n[jobs.build]\nimage = \"i\"\nrun = \"r\"\n",1310+ &bare,
1214- );1311+ "name = \" release-check \"\n[jobs.build]\nimage = \"i\"\nrun = \"r\"\n",
1215- run_at(&fx, &commit).await;1312+ );
1216- finish_job(&fx).await;1313+ run_at(&fx, &commit).await;
1217- let path = "/tahinli/proj/badge.svg";1314+ finish_job(&fx).await;
1218- let reader = Some(cookie_for("dave-token"));1315+ let path = "/tahinli/proj/badge.svg";
1219- let named = get(&fx.app, path, reader.as_deref()).await.text();1316+ let reader = Some(cookie_for("dave-token"));
1220- assert!(named.contains(">release-check</text>"), "name: {named}");1317+ let named = get(&fx.app, path, reader.as_deref()).await.text();
1221- assert!(named.contains(">passing</text>"));1318+ assert!(named.contains(">release-check</text>"), "name: {named}");
1319+ assert!(named.contains(">passing</text>"));
1320+
1321+ let job = get(&fx.app, &format!("{path}?job=build"), reader.as_deref())
1322+ .await
1323+ .text();
1324+ assert!(job.contains(">build</text>"), "job beats name: {job}");
1222 1325
1223- let job = get(&fx.app, &format!("{path}?job=build"), reader.as_deref())1326+ let label = get(&fx.app, &format!("{path}?label=Ship"), reader.as_deref())
1224- .await1327+ .await
1225- .text();1328+ .text();
1226- assert!(job.contains(">build</text>"), "job beats name: {job}");1329+ assert!(label.contains(">Ship</text>"), "label beats name: {label}");
1227 1330
1228- let label = get(&fx.app, &format!("{path}?label=Ship"), reader.as_deref())1331+ let both = get(
1332+ &fx.app,
1333+ &format!("{path}?job=build&label=Ship"),
1334+ reader.as_deref(),
1335+ )
1229 .await1336 .await
1230 .text();1337 .text();
1231- assert!(label.contains(">Ship</text>"), "label beats name: {label}");1338+ assert!(both.contains(">Ship</text>"), "label beats job: {both}");
1232-1339+ assert!(both.contains(">passing</text>"), "status is the job's");
1233- let both = get(1340+ }
1234- &fx.app,
1235- &format!("{path}?job=build&label=Ship"),
1236- reader.as_deref(),
1237- )
1238- .await
1239- .text();
1240- assert!(both.contains(">Ship</text>"), "label beats job: {both}");
1241- assert!(both.contains(">passing</text>"), "status is the job's");
1242-}
1243 1341
1244-#[tokio::test]1342+ {
1245-async fn badge_label_is_trimmed_escaped_and_empty_means_absent() {1343+ run_with(&fx, &["build"], false).await;
1246- let dir = tempfile::tempdir().expect("tempdir");1344+ finish_job(&fx).await;
1247- let fx = fixture(dir.path()).await;1345+ let path = "/tahinli/proj/badge.svg";
1248- run_with(&fx, &["build"], false).await;1346+ let reader = Some(cookie_for("dave-token"));
1249- finish_job(&fx).await;1347+ let padded = get(
1250- let path = "/tahinli/proj/badge.svg";1348+ &fx.app,
1251- let reader = Some(cookie_for("dave-token"));1349+ &format!("{path}?label=%20deploy%20"),
1252- let padded = get(1350+ reader.as_deref(),
1253- &fx.app,1351+ )
1254- &format!("{path}?label=%20deploy%20"),
1255- reader.as_deref(),
1256- )
1257- .await
1258- .text();
1259- assert!(padded.contains(">deploy</text>"), "{padded}");
1260- let empty = get(&fx.app, &format!("{path}?label="), reader.as_deref())
1261 .await1352 .await
1262 .text();1353 .text();
1263- assert!(1354+ assert!(padded.contains(">deploy</text>"), "{padded}");
1264- empty.contains(">build</text>"),1355+ let empty = get(&fx.app, &format!("{path}?label="), reader.as_deref())
1265- "empty label is absent: {empty}"1356+ .await
1266- );1357+ .text();
1267- let blank = get(&fx.app, &format!("{path}?label=%20%20"), reader.as_deref())1358+ assert!(
1359+ empty.contains(">build</text>"),
1360+ "empty label is absent: {empty}"
1361+ );
1362+ let blank = get(&fx.app, &format!("{path}?label=%20%20"), reader.as_deref())
1363+ .await
1364+ .text();
1365+ assert!(
1366+ blank.contains(">build</text>"),
1367+ "blank label is absent: {blank}"
1368+ );
1369+ let escaped = get(
1370+ &fx.app,
1371+ "/tahinli/proj/badge.svg?label=%3Cb%3E%20a%20%26%20b%20%3E",
1372+ reader.as_deref(),
1373+ )
1268 .await1374 .await
1269 .text();1375 .text();
1270- assert!(1376+ assert!(escaped.contains("&lt;b&gt; a &amp; b &gt;"), "{escaped}");
1271- blank.contains(">build</text>"),1377+ assert!(
1272- "blank label is absent: {blank}"1378+ !escaped.contains("<b>"),
1273- );1379+ "raw markup must not reach the SVG"
1274- let escaped = get(1380+ );
1275- &fx.app,1381+ }
1276- "/tahinli/proj/badge.svg?label=%3Cb%3E%20a%20%26%20b%20%3E",
1277- reader.as_deref(),
1278- )
1279- .await
1280- .text();
1281- assert!(escaped.contains("&lt;b&gt; a &amp; b &gt;"), "{escaped}");
1282- assert!(
1283- !escaped.contains("<b>"),
1284- "raw markup must not reach the SVG"
1285- );
1286-}
1287 1382
1288-#[tokio::test]1383+ {
1289-async fn badge_label_defaults_to_build_without_a_usable_workflow_name() {1384+ let bare = repo_path(&fx.data_dir, &fx.repo.id).expect("repo path");
1290- let dir = tempfile::tempdir().expect("tempdir");1385+ // A run whose config has an empty name: the name is ignored.
1291- let fx = fixture(dir.path()).await;1386+ let commit = commit_ci_toml(
1292- let bare = repo_path(&fx.data_dir, &fx.repo.id).expect("repo path");1387+ &bare,
1293- // A run whose config has an empty name: the name is ignored.1388+ "name = \" \"\n[jobs.build]\nimage = \"i\"\nrun = \"r\"\n",
1294- let commit = commit_ci_toml(1389+ );
1295- &bare,1390+ run_at(&fx, &commit).await;
1296- "name = \" \"\n[jobs.build]\nimage = \"i\"\nrun = \"r\"\n",1391+ finish_job(&fx).await;
1297- );1392+ let reader = Some(cookie_for("dave-token"));
1298- run_at(&fx, &commit).await;1393+ let svg = get(&fx.app, "/tahinli/proj/badge.svg", reader.as_deref())
1299- finish_job(&fx).await;1394+ .await
1300- let reader = Some(cookie_for("dave-token"));1395+ .text();
1301- let svg = get(&fx.app, "/tahinli/proj/badge.svg", reader.as_deref())1396+ assert!(svg.contains(">build</text>"), "{svg}");
1302- .await1397+ }
1303- .text();
1304- assert!(svg.contains(">build</text>"), "{svg}");
1305 }1398 }
1306 1399
1400+/// The pages say whether a run is counting. A running run carries the start of each counting duration, and the run page previews each job's last log line and opens a failed job.
1307 #[tokio::test]1401 #[tokio::test]
1308-async fn badge_without_any_run_uses_the_label_or_build() {1402+async fn running_runs_and_run_page_previews() {
1309 let dir = tempfile::tempdir().expect("tempdir");1403 let dir = tempfile::tempdir().expect("tempdir");
1310 let fx = fixture(dir.path()).await;1404 let fx = fixture(dir.path()).await;
1311- let reader = Some(cookie_for("dave-token"));1405+ {
1312- let plain = get(&fx.app, "/tahinli/proj/badge.svg", reader.as_deref())1406+ run_with(&fx, &["test"], false).await;
1313- .await1407+ let token = fx
1314- .text();1408+ .store
1315- assert!(plain.contains(">build</text>"), "{plain}");1409+ .create_registration_token(&fx.tahinli, 24)
1316- assert!(plain.contains(">no status</text>"));1410+ .await
1317- let labelled = get(1411+ .expect("token");
1318- &fx.app,1412+ let (runner, _) = fx
1319- "/tahinli/proj/badge.svg?label=Nightly",1413+ .store
1320- reader.as_deref(),1414+ .register_runner(&token, "runner-1", &["linux".to_owned()])
1321- )1415+ .await
1322- .await1416+ .expect("runner");
1323- .text();1417+ let (job, _, _) = fx
1324- assert!(labelled.contains(">Nightly</text>"), "{labelled}");1418+ .store
1325- assert!(labelled.contains(">no status</text>"));1419+ .claim_job(&runner)
1326-}1420+ .await
1421+ .expect("claim")
1422+ .expect("a queued job");
1423+ let cookie = cookie_for("tahinli-token");
1424+ let html = get(&fx.app, RUN_BASE, Some(&cookie))
1425+ .await
1426+ .text()
1427+ .to_owned();
1428+ assert!(
1429+ html.contains("data-now=\""),
1430+ "the server clock is on the page"
1431+ );
1432+ assert!(
1433+ html.contains("data-fm=\"{m}m {s}s\""),
1434+ "the duration format is on the page"
1435+ );
1436+ assert!(
1437+ html.contains("class=\"ci-row-dur\" data-since=\""),
1438+ "the list duration counts"
1439+ );
1440+ assert!(html.contains("data-tick=\"grow\""), "a running bar grows");
1441+ assert!(
1442+ html.contains("data-tick=\"rest\""),
1443+ "the hatched remainder follows the clock"
1444+ );
1445+ assert!(
1446+ html.contains("data-live-gantt=\"true\""),
1447+ "the timeline is live"
1448+ );
1449+ assert!(
1450+ html.contains("class=\"ci-job-dur\" data-since=\""),
1451+ "the job duration counts"
1452+ );
1327 1453
1328-/// A running run carries what the client's local clock needs: the start of each duration that is1454+ fx.store
1329-/// still counting, the growing boxes of the list bar and the timeline, and the server's clock. A1455+ .finish_job(&job.id, JobState::Success, Some(0), None)
1330-/// finished run carries none of it, so the ticker stops.1456+ .await
1331-#[tokio::test]1457+ .expect("finish");
1332-async fn running_runs_carry_ticker_attributes_and_finished_ones_do_not() {1458+ let html = get(&fx.app, RUN_BASE, Some(&cookie))
1333- let dir = tempfile::tempdir().expect("tempdir");1459+ .await
1334- let fx = fixture(dir.path()).await;1460+ .text()
1335- run_with(&fx, &["test"], false).await;1461+ .to_owned();
1336- let token = fx1462+ assert!(
1337- .store1463+ !html.contains("data-since="),
1338- .create_registration_token(&fx.tahinli, 24)1464+ "nothing counts once the run is done"
1339- .await1465+ );
1340- .expect("token");1466+ assert!(
1341- let (runner, _) = fx1467+ !html.contains("data-tick="),
1342- .store1468+ "nothing grows once the run is done"
1343- .register_runner(&token, "runner-1", &["linux".to_owned()])1469+ );
1344- .await1470+ }
1345- .expect("runner");
1346- let (job, _, _) = fx
1347- .store
1348- .claim_job(&runner)
1349- .await
1350- .expect("claim")
1351- .expect("a queued job");
1352- let cookie = cookie_for("tahinli-token");
1353- let html = get(&fx.app, RUN_BASE, Some(&cookie))
1354- .await
1355- .text()
1356- .to_owned();
1357- assert!(
1358- html.contains("data-now=\""),
1359- "the server clock is on the page"
1360- );
1361- assert!(
1362- html.contains("data-fm=\"{m}m {s}s\""),
1363- "the duration format is on the page"
1364- );
1365- assert!(
1366- html.contains("class=\"ci-row-dur\" data-since=\""),
1367- "the list duration counts"
1368- );
1369- assert!(html.contains("data-tick=\"grow\""), "a running bar grows");
1370- assert!(
1371- html.contains("data-tick=\"rest\""),
1372- "the hatched remainder follows the clock"
1373- );
1374- assert!(
1375- html.contains("data-live-gantt=\"true\""),
1376- "the timeline is live"
1377- );
1378- assert!(
1379- html.contains("class=\"ci-job-dur\" data-since=\""),
1380- "the job duration counts"
1381- );
1382 1471
1383- fx.store1472+ {
1384- .finish_job(&job.id, JobState::Success, Some(0), None)1473+ let run = run_with(&fx, &["build", "test", "lint"], false).await;
1385- .await1474+ let token = fx
1386- .expect("finish");1475+ .store
1387- let html = get(&fx.app, RUN_BASE, Some(&cookie))1476+ .create_registration_token(&fx.tahinli, 24)
1388- .await1477+ .await
1389- .text()1478+ .expect("token");
1390- .to_owned();1479+ let (runner, _) = fx
1391- assert!(1480+ .store
1392- !html.contains("data-since="),1481+ .register_runner(&token, "runner-1", &["linux".to_owned()])
1393- "nothing counts once the run is done"1482+ .await
1394- );1483+ .expect("runner");
1395- assert!(1484+ for _ in 0..3 {
1396- !html.contains("data-tick="),1485+ fx.store
1397- "nothing grows once the run is done"1486+ .claim_job(&runner)
1398- );1487+ .await
1488+ .expect("claim")
1489+ .expect("a queued job");
1490+ }
1491+ let jobs = fx.store.jobs_for_run(&run.id).await.expect("jobs");
1492+ for job in &jobs {
1493+ let path = log_path(&fx.data_dir, &job.id);
1494+ std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
1495+ std::fs::write(
1496+ &path,
1497+ format!("early\n\u{1b}[32mlast of {}\u{1b}[0m\n\n", job.name),
1498+ )
1499+ .expect("log");
1500+ }
1501+ let lint = jobs.iter().find(|job| job.name == "lint").expect("lint");
1502+ fx.store
1503+ .finish_job(&lint.id, JobState::Failure, Some(1), None)
1504+ .await
1505+ .expect("fail");
1506+ let page = get(
1507+ &fx.app,
1508+ &format!("{RUN_BASE}/runs/{}", run.number),
1509+ Some(&cookie_for("tahinli-token")),
1510+ )
1511+ .await;
1512+ assert_eq!(page.status, 200);
1513+ let html = page.text();
1514+ for name in ["build", "test", "lint"] {
1515+ assert!(
1516+ html.contains(&format!("title=\"last of {name}\"")),
1517+ "{name}: {html}"
1518+ );
1519+ }
1520+ assert!(!html.contains('\u{1b}'));
1521+ assert_eq!(html.matches("data-job-row=").count(), 3);
1522+ assert_eq!(html.matches("data-open=\"true\"").count(), 1);
1523+ assert!(html.contains(&format!("data-job-row=\"{}\" data-open=\"true\"", lint.id)));
1524+ assert!(html.contains("data-job-all"));
1525+ }
1399 }1526 }
1400 1527
1401 #[tokio::test]1528 #[tokio::test]
@@ -1515,71 +1642,6 @@ async fn a_run_with_no_failed_jobs_has_nothing_to_rerun() {
1515 );1642 );
1516 }1643 }
1517 1644
1518-#[tokio::test]
1519-async fn run_artifacts_list_and_zip() {
1520- let dir = tempfile::tempdir().expect("tempdir");
1521- let fx = fixture(dir.path()).await;
1522- let run = run_with(&fx, &["build", "test"], false).await;
1523- let jobs = fx.store.jobs_for_run(&run.id).await.unwrap();
1524- for (job, name, payload) in [
1525- (&jobs[0], "report.txt", &b"alpha"[..]),
1526- (&jobs[1], "out.bin", &b"beta"[..]),
1527- ] {
1528- let artifact = fx
1529- .store
1530- .add_artifact(&job.id, name, payload.len() as i64, SHA)
1531- .await
1532- .expect("artifact row");
1533- let mut writer = fx
1534- .blobs
1535- .begin(&artifact_key(&artifact.id), 1024)
1536- .await
1537- .expect("begin");
1538- writer.push(payload).await.expect("push");
1539- writer.finish().await.expect("finish");
1540- }
1541- let reader = cookie_for("dave-token");
1542- let run_url = format!("{RUN_BASE}/runs/{}", run.number);
1543- let page = get(&fx.app, &run_url, Some(&reader)).await.text();
1544- assert!(page.contains("Download all (.zip)"));
1545- assert!(page.contains("artifacts.zip"));
1546- assert!(page.contains("report.txt") && page.contains("out.bin"));
1547-
1548- let res = get(&fx.app, &format!("{run_url}/artifacts.zip"), Some(&reader)).await;
1549- assert_eq!(res.status, 200);
1550- assert_eq!(
1551- res.headers
1552- .get("content-type")
1553- .and_then(|v| v.to_str().ok()),
1554- Some("application/zip")
1555- );
1556- let mut archive = zip::ZipArchive::new(std::io::Cursor::new(res.body.clone())).expect("zip");
1557- assert_eq!(archive.len(), 2);
1558- let mut text = String::new();
1559- std::io::Read::read_to_string(
1560- &mut archive.by_name("build/report.txt").expect("entry"),
1561- &mut text,
1562- )
1563- .expect("read");
1564- assert_eq!(text, "alpha");
1565-
1566- let outsider = get(
1567- &fx.app,
1568- &format!("{run_url}/artifacts.zip"),
1569- Some(&cookie_for("carol-token")),
1570- )
1571- .await;
1572- assert_eq!(outsider.status, 404, "a private repo stays private");
1573- let empty = run_with(&fx, &["lint"], false).await;
1574- let none = get(
1575- &fx.app,
1576- &format!("{RUN_BASE}/runs/{}/artifacts.zip", empty.number),
1577- Some(&reader),
1578- )
1579- .await;
1580- assert_eq!(none.status, 404);
1581-}
1582-
1583 #[tokio::test]1645 #[tokio::test]
1584 async fn config_warnings_show_on_the_run_page() {1646 async fn config_warnings_show_on_the_run_page() {
1585 let dir = tempfile::tempdir().expect("tempdir");1647 let dir = tempfile::tempdir().expect("tempdir");
@@ -1608,97 +1670,3 @@ async fn config_warnings_show_on_the_run_page() {
1608 assert!(page.contains("on.schedule"));1670 assert!(page.contains("on.schedule"));
1609 assert!(page.contains("Config warnings"));1671 assert!(page.contains("Config warnings"));
1610 }1672 }
1611-
1612-#[tokio::test]
1613-async fn job_page_log_carries_the_first_line_number_of_a_cut_log() {
1614- let dir = tempfile::tempdir().expect("tempdir");
1615- let fx = fixture(dir.path()).await;
1616- let run = run_with(&fx, &["build"], false).await;
1617- let job = job_of(&fx, &run).await;
1618- let path = log_path(&fx.data_dir, &job.id);
1619- std::fs::create_dir_all(path.parent().unwrap()).unwrap();
1620- let mut file = std::fs::File::create(&path).unwrap();
1621- // 11000 lines of 100 bytes cut the 512 KiB tail somewhere past line 5000.
1622- for n in 0..11_000 {
1623- writeln!(file, "{n:0>98}").unwrap();
1624- }
1625- drop(file);
1626- let page = get(
1627- &fx.app,
1628- &format!("/tahinli/proj/ci/jobs/{}", job.id),
1629- Some(&cookie_for("dave-token")),
1630- )
1631- .await
1632- .text();
1633- let marker = "data-first=\"";
1634- let at = page.find(marker).expect("data-first") + marker.len();
1635- let first: u64 = page[at..].split('"').next().unwrap().parse().unwrap();
1636- assert!(first > 5000, "first line {first}");
1637- let after = &page[at..];
1638- let text = &after[after.find('>').unwrap() + 1..];
1639- let expected = format!("{:0>98}", first - 1);
1640- assert!(
1641- text.starts_with(&expected),
1642- "the tail starts at its own line number"
1643- );
1644-}
1645-
1646-/// The run page previews the last log line of every running job in its row, and a failed job's row
1647-/// starts expanded.
1648-#[tokio::test]
1649-async fn run_page_previews_each_job_and_expands_failed_ones() {
1650- let dir = tempfile::tempdir().expect("tempdir");
1651- let fx = fixture(dir.path()).await;
1652- let run = run_with(&fx, &["build", "test", "lint"], false).await;
1653- let token = fx
1654- .store
1655- .create_registration_token(&fx.tahinli, 24)
1656- .await
1657- .expect("token");
1658- let (runner, _) = fx
1659- .store
1660- .register_runner(&token, "runner-1", &["linux".to_owned()])
1661- .await
1662- .expect("runner");
1663- for _ in 0..3 {
1664- fx.store
1665- .claim_job(&runner)
1666- .await
1667- .expect("claim")
1668- .expect("a queued job");
1669- }
1670- let jobs = fx.store.jobs_for_run(&run.id).await.expect("jobs");
1671- for job in &jobs {
1672- let path = log_path(&fx.data_dir, &job.id);
1673- std::fs::create_dir_all(path.parent().expect("log dir")).expect("log dir");
1674- std::fs::write(
1675- &path,
1676- format!("early\n\u{1b}[32mlast of {}\u{1b}[0m\n\n", job.name),
1677- )
1678- .expect("log");
1679- }
1680- let lint = jobs.iter().find(|job| job.name == "lint").expect("lint");
1681- fx.store
1682- .finish_job(&lint.id, JobState::Failure, Some(1), None)
1683- .await
1684- .expect("fail");
1685- let page = get(
1686- &fx.app,
1687- &format!("{RUN_BASE}/runs/{}", run.number),
1688- Some(&cookie_for("tahinli-token")),
1689- )
1690- .await;
1691- assert_eq!(page.status, 200);
1692- let html = page.text();
1693- for name in ["build", "test", "lint"] {
1694- assert!(
1695- html.contains(&format!("title=\"last of {name}\"")),
1696- "{name}: {html}"
1697- );
1698- }
1699- assert!(!html.contains('\u{1b}'));
1700- assert_eq!(html.matches("data-job-row=").count(), 3);
1701- assert_eq!(html.matches("data-open=\"true\"").count(), 1);
1702- assert!(html.contains(&format!("data-job-row=\"{}\" data-open=\"true\"", lint.id)));
1703- assert!(html.contains("data-job-all"));
1704-}