From ff450e5e38a5fb502ea640e8500a298f62d1c9d4 Mon Sep 17 00:00:00 2001 From: astar76 Date: Thu, 26 Nov 2026 02:20:19 -0600 Subject: [PATCH] S044: details panel and when-done power tests; Power injected into the when-done picker Co-Authored-By: Claude Opus 5.5 --- crates/app/src/clipboard.rs | 12 +++ crates/app/src/views/schedule.rs | 13 ++- crates/app/tests/main.rs | 141 +++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 4 deletions(-) diff --git a/crates/app/src/clipboard.rs b/crates/app/src/clipboard.rs index 2e1929e..6b82430 100644 --- a/crates/app/src/clipboard.rs +++ b/crates/app/src/clipboard.rs @@ -114,6 +114,18 @@ impl Watch { mod tests { use super::news; + /// S044: what the clipboard of the session in the environment holds, read the way the watcher reads it. + /// Run by hand inside a session: `MEDLAR_CLIP_EXPECT= cargo test -p medlar --lib live_clipboard -- --ignored`. + #[test] + #[ignore] + fn live_clipboard() { + let want = std::env::var("MEDLAR_CLIP_EXPECT").unwrap(); + let mut board = arboard::Clipboard::new().expect("the clipboard opens"); + let got = board.get_text(); + eprintln!("WAYLAND_DISPLAY={:?} DISPLAY={:?} got={got:?}", std::env::var("WAYLAND_DISPLAY"), std::env::var("DISPLAY")); + assert_eq!(got.unwrap_or_default(), want); + } + /// S037: an empty clipboard at the start (no text) must not swallow the first link copied. #[test] fn first_copy_after_empty_start_is_news() { diff --git a/crates/app/src/views/schedule.rs b/crates/app/src/views/schedule.rs index 4ff78a5..38edb22 100644 --- a/crates/app/src/views/schedule.rs +++ b/crates/app/src/views/schedule.rs @@ -200,11 +200,13 @@ pub struct Done { working: Option>>, can: Option>>, allowed: Vec, + /// What shuts down or suspends; logind, or a fake in tests. + pub power: std::sync::Arc, } impl Default for Done { fn default() -> Done { - Done { watcher: Watcher::default(), action: 0, scope: 0, even_failed: false, seconds: 60.0, program: String::new(), counting: None, working: None, can: None, allowed: vec![] } + Done { watcher: Watcher::default(), action: 0, scope: 0, even_failed: false, seconds: 60.0, program: String::new(), counting: None, working: None, can: None, allowed: vec![], power: std::sync::Arc::new(Logind) } } } @@ -241,8 +243,8 @@ impl Done { /// The picker, for the sheet. pub fn picker(&mut self, ui: &mut Ui, t: &Theme, queues: &[(QueueId, String)]) { if self.can.is_none() && self.allowed.is_empty() { - self.can = Some(bridge::beside(|| { - let p = Logind; + let p = self.power.clone(); + self.can = Some(bridge::beside(move || { (1..=6).map(|i| action_of(i, "").is_some_and(|a| p.can(&a))).collect() })); } @@ -371,7 +373,10 @@ impl Done { runtime.block_on(run::run(&spec, &Vars::default())).map(|_| ()) })); } - other => self.working = Some(bridge::beside(move || Logind.perform(&other))), + other => { + let power = self.power.clone(); + self.working = Some(bridge::beside(move || power.perform(&other))); + } } } } diff --git a/crates/app/tests/main.rs b/crates/app/tests/main.rs index d1822d2..a6a5519 100644 --- a/crates/app/tests/main.rs +++ b/crates/app/tests/main.rs @@ -548,3 +548,144 @@ fn a_list_that_cannot_be_opened_says_so() { } assert!(!h.get_by_label("Try again").accesskit_node().is_disabled()); } + +// S044: the details panel (S017 5, 6, 7, 8, 15, 16) and the when-done caller (S021 19). + +/// Clicks the last control with this label, the one on top. +fn press(h: &mut H, label: &str) { + let pos = h.get_all_by_label(label).last().unwrap_or_else(|| panic!("no {label}")).rect().center(); + click(h, pos); +} + +fn details(views: Vec, id: TaskId) -> H { + let mut h = harness(views); + h.run_steps(3); + app(&mut h).main.details.open = Some(id); + h.run_steps(30); + h +} + +#[test] +fn details_close_when_their_task_is_removed() { + let mut h = details(all(), 12); + assert_eq!(app(&mut h).main.details.open, Some(12)); + set(&mut h, all().into_iter().filter(|v| v.id() != 12).collect()); + h.run_steps(30); + assert_eq!(app(&mut h).main.details.open, None, "the panel of a removed task closes"); +} + +#[test] +fn details_explain_an_error_with_its_fix() { + let v = failed(view(9, "report-final.docx", "files.example.com", State::Failed, 4_000_000, 0.5), ErrorKind::Login, "files.example.com wants a login (status 401)."); + let mut h = details(vec![v], 9); + for label in ["What failed", "Why", "What you can do", "Sign in", "Try again"] { + assert!(h.get_all_by_label(label).next().is_some(), "{label} is shown"); + } + assert!(h.query_by_label_contains("wants a login").is_some(), "the reason is shown"); + press(&mut h, "Try again"); + assert!(sent(&mut h).iter().any(|c| matches!(c, Command::Resume(9) | Command::Start(9))), "Try again starts it"); +} + +#[test] +fn details_offer_repairs_for_a_bad_checksum() { + let mut v = failed(view(4, "image.iso", "a.example.org", State::Failed, 4_000_000, 1.0), ErrorKind::Checksum, "The file does not match its checksum."); + Arc::make_mut(&mut v.task).mirrors = vec!["https://b.example.org/image.iso".into()]; + let mut h = details(vec![v], 4); + press(&mut h, "Checksum"); + assert!(h.query_by_label("Does not match its checksum").is_some()); + assert!(h.query_by_label("Download the damaged pieces again").is_none(), "no repair without pieces"); + press(&mut h, "Start over from another mirror"); + assert!(sent(&mut h).contains_cmd(|c| matches!(c, Command::OtherMirror(4)))); +} + +#[test] +fn details_see_a_file_gone_and_download_it_again() { + let dir = tempfile::tempdir().unwrap(); + let mut v = view(11, "wallpaper-forest.png", "images.example.org", State::Done, 12_000_000, 1.0); + Arc::make_mut(&mut v.task).folder = dir.path().to_path_buf(); + let mut h = details(vec![v], 11); + let start = Instant::now(); + while h.query_by_label("The file is gone").is_none() { + assert!(start.elapsed() < Duration::from_secs(10), "a missing file is seen within one look"); + h.run_steps(5); + std::thread::sleep(Duration::from_millis(100)); + } + press(&mut h, "Download again"); + assert!(sent(&mut h).contains_cmd(|c| matches!(c, Command::Redownload(11)))); +} + +#[test] +fn details_hide_secret_headers() { + let mut v = view(5, "lecture-12.mp3", "audio.example.edu", State::Paused, 92_000_000, 0.2); + Arc::make_mut(&mut v.task).headers = vec![("Cookie".into(), "session=abc123secret".into()), ("Referer".into(), "https://audio.example.edu/".into())]; + let mut h = details(vec![v], 5); + press(&mut h, "Headers"); + assert!(h.query_by_label("hidden").is_some(), "the cookie is hidden"); + assert!(h.query_by_label_contains("abc123secret").is_none(), "the cookie value never shows"); + assert!(h.query_by_label_contains("audio.example.edu/").is_some(), "plain headers show"); +} + +#[test] +fn details_take_a_pasted_checksum() { + let v = view(12, "fonts-collection.zip", "share.example.org", State::Done, 210_000_000, 1.0); + let mut h = details(vec![v], 12); + press(&mut h, "Checksum"); + press(&mut h, "Paste a checksum"); + h.input_mut().events.push(Event::Text("not-a-sum".into())); + h.run_steps(10); + assert!(h.query_by_label("This is not a checksum Medlar knows.").is_some(), "bad text is refused"); + assert!(h.query_by_label("Use and check").is_none()); + let mut h = details(vec![view(12, "fonts-collection.zip", "share.example.org", State::Done, 210_000_000, 1.0)], 12); + press(&mut h, "Checksum"); + press(&mut h, "Paste a checksum"); + let sum = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + h.input_mut().events.push(Event::Text(sum.into())); + h.run_steps(10); + assert!(h.query_by_label("This is not a checksum Medlar knows.").is_none()); + press(&mut h, "Use and check"); + let got = sent(&mut h); + assert!(got.contains_cmd(|c| matches!(c, Command::Edit { id: 12, .. })), "the checksum is saved"); + assert!(got.contains_cmd(|c| matches!(c, Command::Verify(12))), "a done file is checked at once"); +} + +trait Has { + fn contains_cmd(&self, f: impl Fn(&Command) -> bool) -> bool; +} + +impl Has for Vec { + fn contains_cmd(&self, f: impl Fn(&Command) -> bool) -> bool { + self.iter().any(f) + } +} + +struct FakePower(Arc>>); + +impl medlar_core::power::Power for FakePower { + fn can(&self, _: &medlar_core::power::Action) -> bool { + true + } + fn perform(&self, action: &medlar_core::power::Action) -> Result<(), String> { + self.0.lock().unwrap().push(action.clone()); + Ok(()) + } +} + +#[test] +fn when_done_shuts_down_through_the_power_it_was_given() { + use medlar_core::power::{Action, Scope, WhenDone}; + let calls = Arc::new(std::sync::Mutex::new(vec![])); + let mut h = harness(vec![view(1, "a.zip", "a.example.org", State::Running, 1000, 0.5)]); + h.run_steps(3); + let done = &mut app(&mut h).main.control.done; + done.power = Arc::new(FakePower(calls.clone())); + let _ = done.watcher.arm(WhenDone { action: Action::Shutdown, scope: Scope::All, even_failed: false, countdown: Duration::ZERO }); + h.run_steps(10); + assert!(calls.lock().unwrap().is_empty(), "nothing fires while a download runs"); + set(&mut h, vec![view(1, "a.zip", "a.example.org", State::Done, 1000, 1.0)]); + let start = Instant::now(); + while calls.lock().unwrap().is_empty() && start.elapsed() < Duration::from_secs(5) { + h.run_steps(5); + std::thread::sleep(Duration::from_millis(50)); + } + assert_eq!(*calls.lock().unwrap(), vec![Action::Shutdown], "the fake got the shutdown, once"); +}