diff --git a/crates/app/tests/add.rs b/crates/app/tests/add.rs index aaec987..4b7d68e 100644 --- a/crates/app/tests/add.rs +++ b/crates/app/tests/add.rs @@ -486,3 +486,79 @@ fn a_start_time_is_kept() { until(&mut b.h, "the start", |h| Bench::state(h, id).is_some_and(|s| s.0 == State::Done)); assert!(medlar_core::model::now() >= at); } + +// S044: links from outside (S024 3, 9, 10, 14, 15). + +#[test] +fn a_medlar_link_always_opens_the_dialog() { + let mut h = harness(all()); + let (tx, rx) = std::sync::mpsc::channel(); + let app = app(&mut h); + app.settings.add_without_asking = true; + app.settings.connector = false; + app.inbox = medlar_app::views::incoming::Inbox::new(Some(rx), vec![]); + tx.send(vec!["medlar:https://a.example.org/files/f.zip".into()]).unwrap(); + h.run_steps(60); + assert_eq!(now(&mut h).text, "https://a.example.org/files/f.zip", "the dialog holds the link"); + assert!(sent(&mut h).iter().all(|c| !matches!(c, Command::Add(_))), "nothing is added silently"); +} + +fn free_port() -> u16 { + std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port() +} + +/// Nacre's body (crates/core/tests/connector_nacre.json) with another link, posted the way Nacre posts it. +fn post_like_nacre(port: u16, url: &str, name: &str, size: u64) -> u16 { + use std::io::{Read, Write}; + let body = include_str!("../../core/tests/connector_nacre.json") + .replace("https://example.com/a.zip", url) + .replace("\"filename\":\"a.zip\"", &format!("\"filename\":\"{name}\"")) + .replace("\"size\":null", &format!("\"size\":{size}")); + let mut s = std::net::TcpStream::connect(("127.0.0.1", port)).unwrap(); + let req = format!("POST / HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()); + s.write_all(req.as_bytes()).unwrap(); + let mut answer = String::new(); + let _ = s.read_to_string(&mut answer); + answer.split(' ').nth(1).and_then(|c| c.parse().ok()).unwrap_or(0) +} + +/// A download whose link expired and that waits for a new one, with the port on a spare number. +fn expired(b: &mut Bench, path: &str) -> (TaskId, u16) { + let port = free_port(); + app(&mut b.h).settings.connector_port = port; + app(&mut b.h).settings.connector = true; + let id = b.half(path); + b.server.set_faults(path, Faults { expire_requests: Some(0), ..Faults::default() }); + let manager = app(&mut b.h).main.bridge.manager().unwrap(); + manager.send(Command::Resume(id)).wait().unwrap(); + until(&mut b.h, "expired", |h| Bench::state(h, id).is_some_and(|s| s.0 == State::Failed)); + manager.send(Command::AwaitLink(id)).wait().unwrap(); + until(&mut b.h, "waiting", |h| Bench::state(h, id).is_some_and(|s| s.0 == State::Waiting)); + b.server.clear_all_faults(); + (id, port) +} + +#[test] +fn a_fresh_link_from_the_browser_continues_an_expired_download() { + let mut b = Bench::new(); + let path = "/data/7/4m/f.bin"; + let (id, port) = expired(&mut b, path); + let had = Bench::state(&b.h, id).unwrap().1; + assert!(had > 0); + assert_eq!(post_like_nacre(port, &b.server.url(&format!("{path}?token=new")), "f.bin", 4 << 20), 200, "the port takes Nacre's body"); + until(&mut b.h, "the toast", |h| h.query_by_label_contains("It goes on").is_some()); + until(&mut b.h, "done", |h| Bench::state(h, id).is_some_and(|s| s.0 == State::Done)); + assert!(app(&mut b.h).main.adding.now().is_none(), "no dialog"); + let file = Path::new(&app(&mut b.h).settings.download_folder).join("f.bin"); + assert!(std::fs::read(file).unwrap() == expected(7, 0, 4 << 20), "the file is whole and the same"); +} + +#[test] +fn a_fresh_link_to_another_file_is_refused() { + let mut b = Bench::new(); + let (id, port) = expired(&mut b, "/data/7/4m/f.bin"); + assert_eq!(post_like_nacre(port, &b.server.url("/data/8/4m/f.bin?token=new"), "f.bin", 4 << 20), 200); + until(&mut b.h, "the error toast", |h| h.query_by_label_contains("was not used").is_some()); + assert!(app(&mut b.h).main.adding.now().is_none(), "no dialog"); + assert_eq!(Bench::state(&b.h, id).unwrap().0, State::Waiting, "the download keeps waiting"); +}