Cotton code
83 lines · 4.2 KB · RustRaw
use cotton_core::kit::AppKit;use super::*;const TORRENT: &[u8] = b"d8:announce20:http://t.example/ann13:announce-listll20:http://t.example/annel18:udp://u.example:80ee4:infod6:lengthi5e4:name7:a b.txt12:piece lengthi16e6:pieces20:aaaaaaaaaaaaaaaaaaaaee";/// RULE (8 Nov 2026): the app whose kit keeps the queue/// and no reading pile takes every link as a download; every other app keeps/// its pages, and a magnet or a `.torrent`'s link is a download in all of them.#[test]fn only_the_app_with_no_reading_pile_takes_every_link() {    let downloads = AppKit::named("downloads").expect("ships");    assert!(takes_every_link(Some(&downloads)));    assert!(!takes_every_link(None), "Cotton's whole kit keeps its pages");    assert!(!takes_every_link(AppKit::named("books").as_ref()));    let page = "https://example.com/watch?v=1";    assert_eq!(fetch_links(true, Some(page), None), [page]);    assert_eq!(fetch_links(false, Some(page), None), Vec::<String>::new(), "a page stays a page");    let magnet = "magnet:?xt=urn:btih:abc";    let torrent = "https://example.com/x.TORRENT?k=1";    for every_link in [true, false] {        assert_eq!(fetch_links(every_link, Some(magnet), None), [magnet]);        assert_eq!(fetch_links(every_link, None, Some(&format!(" {torrent}\n"))), [torrent]);    }}#[test]fn text_gives_each_link_once_and_prose_alone_gives_none() {    let text = "see https://a.example/1 and magnet:?xt=urn:btih:abc, also https://a.example/1";    assert_eq!(fetch_links(true, None, Some(text)), ["https://a.example/1", "magnet:?xt=urn:btih:abc"]);    assert_eq!(fetch_links(false, None, Some(text)), Vec::<String>::new(), "prose in Cotton goes on to its pages");    assert_eq!(fetch_links(true, None, Some("no address here")), Vec::<String>::new());    assert_eq!(fetch_links(true, Some("ftp://a.example/x"), None), Vec::<String>::new());}/// RULE: a `.torrent` file is named by the SHA-1 of its `info` dictionary as/// written, so the magnet is the same swarm as the file. A file's own address/// beside it, or a file of any other kind, is no download.#[test]fn a_torrent_file_is_its_magnet() {    let start = TORRENT.windows(4).position(|window| window == b"info").unwrap() + 4;    let hash = hex::encode(Sha1::digest(&TORRENT[start..TORRENT.len() - 1]));    assert_eq!(        magnet_of(TORRENT).unwrap(),        format!("magnet:?xt=urn:btih:{hash}&dn=a%20b.txt&tr=http%3A%2F%2Ft.example%2Fann&tr=udp%3A%2F%2Fu.example%3A80")    );    let dir = tempfile::tempdir().unwrap();    let path = dir.path().join("staged-copy");    std::fs::write(&path, TORRENT).unwrap();    let file = path.to_str().unwrap();    let request = json!({ "file": file, "fileName": "A.Torrent", "url": "https://a.example/1" });    assert_eq!(sources(true, &request).unwrap(), [magnet_of(TORRENT).unwrap()]);    assert!(sources(false, &request).unwrap().is_empty(), "Cotton files it as it did");    assert!(sources(true, &json!({ "file": file, "fileName": "a.pdf", "url": "https://a.example/1" })).unwrap().is_empty());}#[test]fn bytes_that_are_no_torrent_are_refused_in_the_kits_words() {    for bad in [&b""[..], b"hello", b"d4:infod4:name1:aee", b"i1e"] {        assert_eq!(magnet_of(bad), None, "{bad:?}");    }    let deep = vec![b'l'; 1_000_000];    assert_eq!(magnet_of(&deep), None, "nesting is only a bad file");    let dir = tempfile::tempdir().unwrap();    let path = dir.path().join("x.torrent");    std::fs::write(&path, b"nope").unwrap();    let refused = sources(true, &json!({ "file": path.to_str().unwrap() })).unwrap_err();    assert_eq!(refused.code, "refused");    assert!(!refused.message.is_empty(), "the kit says why");}/// RULE (12 Nov 2026): in the app that takes every/// link a share holding none has no other branch, so the answer says why/// nothing was queued; in every other app an empty answer says nothing and/// the share goes on to its other branches.#[test]fn a_share_with_no_link_says_so_only_where_every_link_is_taken() {    let said = nothing_said(true, &[]).expect("Downloads says it");    assert!(!said.is_empty() && !said.contains("share."), "the kit's words: {said}");    assert_eq!(nothing_said(false, &[]), None, "Cotton's share goes on to its pages");    assert_eq!(nothing_said(true, &["magnet:?xt=urn:btih:abc".to_string()]), None);}