Medlar/crates/app/tests/main/fixture.rs
2026-11-25 06:18:58 -06:00

138 lines
6.1 KiB
Rust

//! Downloads in every state, for pictures and tests.
#![allow(dead_code)]
use medlar_app::bridge::Lists;
use medlar_core::http::{PartState, PartView};
use medlar_core::model::{ErrorKind, Queue, State, Tag, Task, TaskError, TaskId, now};
use medlar_core::snapshot::{Counters, Snapshot, TaskView, Totals};
use medlar_core::speed::History;
use std::sync::Arc;
use std::time::Duration;
pub const MB: u64 = 1_000_000;
pub fn view(id: TaskId, name: &str, host: &str, state: State, size: u64, done: f64) -> TaskView {
let downloaded = (size as f64 * done) as u64;
let task = Task {
id,
url: format!("https://{host}/files/{name}"),
name: name.into(),
folder: "/home/you/Downloads".into(),
state,
position: id as f64,
size: Some(size),
downloaded,
added_at: now() - id * 3_600_000,
..Task::default()
};
TaskView {
task: Arc::new(task),
rev: id as u64,
size: Some(size),
downloaded,
speed: 0.0,
left: None,
parts: Arc::new([]),
connections: 0,
wanted: 0,
resumable: Some(true),
checked: 0.0,
retry_at: None,
fails: 0,
server: format!("{host}:443").into(),
history: Arc::default(),
log: Arc::default(),
counters: Counters::default(),
}
}
pub fn running(mut v: TaskView, speed: f64, parts: u64) -> TaskView {
let size = v.size.unwrap_or(0);
let each = size / parts;
let share = v.downloaded as f64 / size as f64;
v.parts = (0..parts)
.map(|i| PartView {
id: i as _,
start: i * each,
end: Some(if i + 1 == parts { size } else { (i + 1) * each }),
done: ((each as f64) * (share * (0.7 + 0.1 * (i % 5) as f64)).min(1.0)) as u64,
state: PartState::Running,
speed: speed / parts as f64,
fails: 0,
})
.collect();
(v.speed, v.connections, v.wanted) = (speed, parts as u32, parts as u32);
v.left = Some(Duration::from_secs_f64((size - v.downloaded) as f64 / speed));
v
}
pub fn failed(mut v: TaskView, kind: ErrorKind, detail: &str) -> TaskView {
let task = Arc::make_mut(&mut v.task);
task.error = Some(TaskError { kind: Some(kind), detail: detail.into() });
v
}
pub fn totals(views: &[TaskView]) -> Totals {
let mut history = History::default();
(0..150).for_each(|i| history.push(9.0e6 + 4.0e6 * (f64::from(i) * 0.13).sin() + 1.5e6 * (f64::from(i) * 0.71).cos()));
Totals {
running: views.iter().filter(|v| v.state() == State::Running).count(),
queued: views.iter().filter(|v| v.state() == State::Queued).count(),
speed: views.iter().map(|v| v.speed).sum(),
connections: views.iter().map(|v| v.connections).sum(),
history: Arc::new(history),
counters: Counters::default(),
}
}
pub fn all() -> Vec<TaskView> {
let long = "a-very-long-name-of-a-recording-that-nobody-shortened-2026-11-24-evening-session-part-03-final-corrected-version-with-subtitles-and-chapters-remastered.mkv";
let mut retry = failed(view(7, "dataset-2026.tar.zst", "data.example.net", State::Waiting, 8200 * MB, 0.33), ErrorKind::Busy, "data.example.net has too many requests right now (status 429).");
retry.retry_at = Some(now() + 42_000);
vec![
running(view(1, "archlinux-2026.11.01-x86_64.iso", "mirror.example.org", State::Running, 1180 * MB, 0.46), 8.4e6, 8),
running(view(2, long, "video.example.com", State::Running, 4310 * MB, 0.18), 2.1e6, 4),
view(3, "field-notes.pdf", "papers.example.edu", State::Finishing, 18 * MB, 1.0),
view(4, "holiday-photos.zip", "share.example.org", State::Queued, 640 * MB, 0.0),
view(5, "lecture-12.mp3", "audio.example.edu", State::Queued, 92 * MB, 0.0),
failed(view(6, "map-tiles.7z", "tiles.example.org", State::Waiting, 2300 * MB, 0.61), ErrorKind::Offline, "This computer is not connected."),
retry,
view(8, "podcast-archive.flac", "audio.example.edu", State::Paused, 410 * MB, 0.72),
failed(view(9, "report-final.docx", "files.example.com", State::Failed, 4 * MB, 0.5), ErrorKind::Expired, "files.example.com gave out the file before but refuses now (status 403). A new link for the same file continues the download."),
failed(view(10, "firmware-3.2.img", "vendor.example.com", State::Failed, 64 * MB, 0.9), ErrorKind::Changed, "vendor.example.com now has another version of the file."),
view(11, "wallpaper-forest.png", "images.example.org", State::Done, 12 * MB, 1.0),
view(12, "fonts-collection.zip", "share.example.org", State::Done, 210 * MB, 1.0),
view(13, "concert.mkv", "video.example.com", State::Gone, 2100 * MB, 1.0),
]
}
pub fn lists() -> Lists {
let queue = |id, name: &str| Queue { id, name: name.into(), position: id as f64, ..Queue::default() };
let tag = |id, name: &str, color: &str| Tag { id, name: name.into(), color: Some(color.into()) };
Lists { queues: vec![queue(1, ""), queue(2, "Night"), queue(3, "Work")], tags: vec![tag(1, "Study", "#3f7fd0"), tag(2, "Music", "#b5622b")] }
}
pub fn shot(views: Vec<TaskView>) -> Snapshot {
let totals = totals(&views);
let mut shot: Snapshot = views.into_iter().map(Arc::new).collect();
shot.totals = totals;
shot
}
/// Many, some of them running.
pub fn many(n: i64, rev: u64) -> Snapshot {
let names = ["video.mkv", "song.mp3", "photo.jpg", "book.pdf", "pack.zip", "tool.AppImage", "thing.bin"];
let states = [State::Done, State::Paused, State::Queued, State::Failed, State::Done, State::Done, State::Done];
let views = (1..=n).map(|id| {
let name = format!("{id:05}-{}", names[id as usize % names.len()]);
let host = format!("host{}.example.org", id % 40);
let mut v = if id <= 8 {
running(view(id, &name, &host, State::Running, 900 * MB, 0.2), 1.0e6 + rev as f64 * 1000.0, 8)
} else {
view(id, &name, &host, states[id as usize % states.len()], (id as u64 % 900 + 1) * MB, 0.4)
};
v.rev = rev;
v
});
shot(views.collect())
}