Cotton code
167 lines · 9.5 KB · RustRaw
use super::*;fn words() -> Words {    Words {        queued: "Queued".into(),        queued_waiting: "Queued · waiting for your Mac".into(),        progress: "{n}%".into(),        fetching: "Downloading".into(),        done: "Done · {size}".into(),        done_unsized: "Done".into(),        done_many: "Done · {n} files · {size}".into(),        opens_in: "opens in {app}".into(),        saved: "Done · saved to Files · no app here opens it".into(),        failed: "Failed · {why}".into(),        failed_unsaid: "Failed · Cotton could not download this".into(),        cancelled: "Cancelled".into(),        open: "Open".into(),        show: "Show".into(),        retry: "Retry".into(),        unnamed: "The download".into(),        row: "{state}: {name}".into(),        done_short: "Done".into(),        failed_short: "Failed".into(),        apps: [("cinemas", "Cinemas"), ("music", "Music"), ("books", "Books"), ("photos", "Photos")].into_iter().map(|(a, b)| (a.to_string(), b.to_string())).collect(),    }}fn fetch(state: &str) -> Fetch {    Fetch { id: "f1".into(), state: state.into(), progress: None, title: None, error: None, of_bytes: None, files: Vec::new(), waited_ms: None }}fn landed(name: &str, bytes: f64, kind: &str) -> Landed {    Landed { id: format!("ff-{name}"), name: name.into(), bytes: Some(bytes), kind: Some(kind.into()), record: None }}/// RULE (DLCHAT, 22 Nov 2026): a waiting download says Queued, and offers nothing.#[test]fn a_waiting_download_says_queued() {    let said = line(&fetch("queued"), &words());    assert_eq!((said.state.as_str(), said.text.as_str(), said.act), ("queued", "Queued", None));    let mut named = fetch("queued");    named.title = Some("garden-notes.epub".into());    assert_eq!((line(&named, &words()).name.as_str(), line(&named, &words()).row.as_str()), ("garden-notes.epub", "Queued: garden-notes.epub"));    assert_eq!(line(&fetch("a-state-from-later"), &words()).text, "Queued", "a state this build does not know waits");    // RULE (owner, 22 Nov 2026): queued a minute and no fetcher has taken it, it says it waits for the Mac; before that, Queued.    let mut waiting = fetch("queued");    waiting.waited_ms = Some(UNCLAIMED_MS - 1);    assert_eq!(line(&waiting, &words()).text, "Queued");    waiting.waited_ms = Some(UNCLAIMED_MS);    let said = line(&waiting, &words());    assert_eq!((said.text.as_str(), said.row.as_str()), ("Queued · waiting for your Mac", "Queued: The download"));    let mut on = fetch("fetching");    on.waited_ms = Some(10 * UNCLAIMED_MS);    assert_eq!(line(&on, &words()).text, "Downloading", "a fetcher has it");}/// RULE: a download on its way says its percentage, rounded down and never 100 before it is done; with no figure yet it says it is downloading.#[test]fn a_download_on_its_way_says_its_percentage_and_never_claims_more() {    let mut on = fetch("fetching");    on.progress = Some(42.9);    let said = line(&on, &words());    assert_eq!((said.state.as_str(), said.text.as_str(), said.percent, said.row.as_str()), ("progress", "42%", Some(42), "42%: The download"));    on.progress = Some(99.97);    assert_eq!(line(&on, &words()).text, "99%");    on.state = "landing".into();    on.progress = Some(100.0);    assert_eq!(line(&on, &words()).text, "99%", "landing is not done");    on.progress = None;    assert_eq!(line(&on, &words()).text, "Downloading");    on.progress = Some(f64::NAN);    assert_eq!(line(&on, &words()).text, "Downloading");}/// RULE (the drawing, you-grouped.html): a done download is its file's name, and under it Done, its size and the Cotton app it opens in, with Open; where nothing on the phone opens it the phone draws "saved to Files" and Show instead; many files are counted; with no size known it says Done; with no file name, the row's title, else the kit's word. The list's last line is "Done: <name>".#[test]fn a_done_download_names_its_file_and_size_and_offers_open() {    let mut done = fetch("done");    done.files = vec![landed("river-walk-talk.mp4", 412_300_000.0, "film")];    let said = line(&done, &words());    assert_eq!((said.name.as_str(), said.text.as_str(), said.row.as_str()), ("river-walk-talk.mp4", "Done · 412 MB · opens in Cinemas", "Done: river-walk-talk.mp4"));    assert_eq!(said.act, Some(Act { act: "open".into(), label: "Open".into() }));    assert_eq!(said.opens, Some(Opens { app: "cinemas".into(), name: "Cinemas".into() }));    assert_eq!((said.size.as_deref(), said.count), (Some("412 MB"), Some(1)));    assert_eq!(said.unopened, Some(Unopened { text: "Done · saved to Files · no app here opens it".into(), act: Act { act: "show".into(), label: "Show".into() } }));    done.files.push(landed("Second.mkv", 1_064_000_000.0, "film"));    assert_eq!(line(&done, &words()).text, "Done · 2 files · 1.4 GB · opens in Cinemas");    let mut tool = fetch("done");    tool.files = vec![landed("plotter-v3.bin", 2_000_000.0, "document")];    let said = line(&tool, &words());    assert_eq!((said.text.as_str(), said.opens.is_none()), ("Done · 2.0 MB", true), "no Cotton app plays a document");    let mut bare = fetch("done");    bare.title = Some("An  Invented\nShort".into());    assert_eq!((line(&bare, &words()).name.as_str(), line(&bare, &words()).text.as_str()), ("An Invented Short", "Done"));    bare.of_bytes = Some(5_400_000.0);    assert_eq!(line(&bare, &words()).text, "Done · 5.4 MB");    assert_eq!(line(&fetch("done"), &words()).name, "The download");}/// RULE: a failed download says the fetcher's reason on one line and offers Retry; one that gave none says so in the kit's words; a cancelled one says Cancelled and offers Retry.#[test]fn a_failed_download_says_why_and_offers_retry() {    let mut failed = fetch("failed");    failed.error = Some("The page asks for a sign-in.\n".into());    let said = line(&failed, &words());    assert_eq!(said.text, "Failed · The page asks for a sign-in.");    assert_eq!(said.row, "Failed: The download");    assert_eq!(said.act, Some(Act { act: "retry".into(), label: "Retry".into() }));    failed.error = Some("  ".into());    assert_eq!(line(&failed, &words()).text, "Failed · Cotton could not download this");    let cancelled = line(&fetch("cancelled"), &words());    assert_eq!((cancelled.text.as_str(), cancelled.act.map(|act| act.act)), ("Cancelled", Some("retry".to_string())));}/// RULE: a size is said in decimal units, never more than it is.#[test]fn a_size_is_said_as_a_person_reads_it() {    assert_eq!(size(0.0).as_deref(), Some("1 KB"));    assert_eq!(size(820_400.0).as_deref(), Some("820 KB"));    assert_eq!(size(4_290_000.0).as_deref(), Some("4.2 MB"));    assert_eq!(size(38_900_000.0).as_deref(), Some("38 MB"));    assert_eq!(size(1_999_000_000.0).as_deref(), Some("1.9 GB"));    assert_eq!(size(-1.0), None);    assert_eq!(size(f64::INFINITY), None);}/// RULE: a film or an episode opens in Cinemas, a song in Music, a book in Books, a photo in Photos; a document, and a kind this build does not know, in the phone's own viewer.#[test]fn each_kind_opens_in_the_cotton_app_that_plays_it() {    assert_eq!(opener("film"), Some(("cinemas", "film")));    assert_eq!(opener("episode"), Some(("cinemas", "film_episode")));    assert_eq!(opener("song"), Some(("music", "song")));    assert_eq!(opener("book"), Some(("books", "book")));    assert_eq!(opener("photo"), Some(("photos", "photo")));    assert_eq!(opener("document"), None);    assert_eq!(opener("hologram"), None);}/// RULE: a link the phone queues itself is spelled into its row as the Worker's own rule spells it (fetch.ts sourceKindOf, wantsOf, provisionalTitle), since the object takes a sealed row's kind as the device says it: a magnet or a .torrent is a torrent, a file's extension a file, anything else a page; a page wants its video, the rest their files; the first title is a magnet's display name, else the file's name, else the host and path.#[test]fn a_link_is_spelled_into_its_row_as_the_worker_spells_it() {    assert_eq!(source_kind("magnet:?xt=urn:btih:abc&dn=Invented"), "torrent");    assert_eq!(source_kind("https://t.example/Invented.TORRENT?k=1"), "torrent");    assert_eq!(source_kind("https://files.example/a/Invented%20Film.MKV"), "file");    assert_eq!(source_kind("https://files.example/a/notes.docx"), "page", "an extension the Mac does not take as a file");    assert_eq!(source_kind("https://video.example/watch?v=invented.mp4"), "page", "the query is not the path");    assert_eq!(source_kind("https://video.example/"), "page");    assert_eq!(source_kind("not a link"), "page");    assert_eq!((wants("page"), wants("torrent"), wants("file")), ("video", "files", "files"));    assert_eq!(provisional_title("magnet:?xt=urn:btih:abc&dn=An+Invented%20Show"), "An Invented Show");    assert_eq!(provisional_title("magnet:?xt=urn:btih:abc"), "A torrent");    assert_eq!(provisional_title("magnet:?xt=urn:btih:abc&dn=%E0%A4"), "%E0%A4", "a name that is no UTF-8 stays as it came");    assert_eq!(provisional_title("https://files.example/a/Invented%20Film.mkv"), "Invented Film.mkv");    assert_eq!(provisional_title("https://Video.Example/watch?v=1"), "video.example/watch");    assert_eq!(provisional_title("https://video.example/"), "video.example");    let row = queued("https://files.example/a/invented.epub", "2026-11-22T10:00:00.000Z");    assert_eq!(        row,        serde_json::json!({ "source": "https://files.example/a/invented.epub", "source_kind": "file", "wants": "files", "state": "queued", "added_on": "2026-11-22T10:00:00.000Z", "title": "invented.epub" })    );}