Cotton code
207 lines · 13.7 KB · RustRaw
//! The seek line: its ticks and words, the slip for a dragged place, a jump//! (exact, snapped to a tick, or the contents' chapter), and the one Back.//! Every book here is synthetic.use serde_json::{json, Value};use super::super::call_on;use crate::shell::client::phone_tests::attached_on_a_clock;use crate::shell::client::ClientController;fn ask(client: &ClientController, method: &str, request: Value) -> Value {    call_on(client, method, &request).unwrap_or_else(|error| panic!("{method}: {error:?}"))}fn a_book(client: &ClientController, format: &str) -> String {    let key = format!("{}/books/{}.{format}", crate::fixture::WORKSPACE, "d".repeat(64));    let made = crate::phone::records::call(client, "records.create", &json!({"table": "book", "data": {"title": "The Salt Clock", "format": format, "status": "reading", "file": key}})).expect("a book");    ["/record/id", "/id"].iter().find_map(|pointer| made.pointer(pointer).and_then(Value::as_str)).expect("its id").to_string()}fn epub_at(href: &str, total: f64, position: i64) -> Value {    json!({"href": href, "type": "application/xhtml+xml", "locations": {"progression": 0.2, "totalProgression": total, "position": position}})}/// An EPUB of four chapters (the third with a section inside it), open at chapter two.fn an_open_epub(client: &ClientController) -> String {    let book = a_book(client, "epub");    let contents = json!({        "toc": [            {"title": "One", "href": "c1.xhtml"},            {"title": "Two", "href": "c2.xhtml"},            {"title": "Three", "href": "c3.xhtml"},            {"title": "Three, a section", "href": "c3.xhtml#later"},            {"title": "Four", "href": "c4.xhtml"},        ],        "reading_order": ["c1.xhtml", "c2.xhtml", "c3.xhtml", "c4.xhtml"],        "starts": [0.0, 0.25, 0.5, 0.8],    });    ask(client, "reader.moved", json!({"book": book, "at": epub_at("c2.xhtml", 0.3, 30), "contents": contents, "active": true}));    book}fn locator(answer: &Value) -> Value {    serde_json::from_str(answer["locator"].as_str().unwrap_or_else(|| panic!("a locator: {answer}"))).unwrap()}// RULE: reader.seek answers the line for an open EPUB: where the page stands, each chapter start where its resource begins (sections of one resource share its start) with the chapter the page is in marked, no page count, no Back, the slip's words for here (chapter and percent) and the line's words, and one step a chapter each way.#[test]fn the_line_holds_the_chapters_and_where_the_page_stands() {    let (_dir, client, _clock) = attached_on_a_clock();    let book = an_open_epub(&client);    let line = ask(&client, "reader.seek", json!({"book": book}));    assert_eq!(line["here"], json!(0.3));    assert_eq!(line["pages"], Value::Null);    assert_eq!(line["back"], Value::Null);    let ticks: Vec<(f64, &str, bool)> = line["ticks"].as_array().unwrap().iter().map(|tick| (tick["at"].as_f64().unwrap(), tick["title"].as_str().unwrap(), tick["here"].as_bool().unwrap())).collect();    assert_eq!(ticks, [(0.0, "One", false), (0.25, "Two", true), (0.5, "Three", false), (0.5, "Three, a section", false), (0.8, "Four", false)]);    assert_eq!(line["words"]["here"], "Two · 30%");    assert_eq!(line["words"]["line"], "Place in the book");    assert_eq!(line["words"]["contents"], "Contents");    assert_eq!(line["steps"], json!({"previous": 0.25, "next": 0.5}));}// RULE: the slip says the chapter and percent of a dragged place in a book with no page numbers, the chapter and page in one with them (page = 1 + share × (pages − 1)), and the page or percent alone before the first chapter or in a book whose contents the reader never told.#[test]fn the_slip_says_the_chapter_and_the_page_or_percent() {    let (_dir, client, _clock) = attached_on_a_clock();    let book = an_open_epub(&client);    let say = |client: &ClientController, book: &str, at: f64| ask(client, "reader.seek.say", json!({"book": book, "at": at}))["words"].as_str().unwrap().to_string();    assert_eq!(say(&client, &book, 0.0), "One · 0%");    assert_eq!(say(&client, &book, 0.62), "Three · 62%");    assert_eq!(say(&client, &book, 0.999), "Four · 99%");    let pdf = a_book(&client, "pdf");    let contents = json!({"toc": [{"title": "Preface", "href": "orchard.pdf#page=3"}, {"title": "Chapter 12", "href": "orchard.pdf#page=148"}], "reading_order": ["orchard.pdf"], "starts": [0.0], "pages": 201});    let first = json!({"href": "orchard.pdf", "type": "application/pdf", "locations": {"position": 1, "totalProgression": 0.0}});    ask(&client, "reader.moved", json!({"book": pdf, "at": first, "contents": contents, "active": true}));    assert_eq!(say(&client, &pdf, 147.0 / 200.0), "Chapter 12 · page 148");    assert_eq!(say(&client, &pdf, 0.0), "Page 1", "before the first chapter");    let line = ask(&client, "reader.seek", json!({"book": pdf}));    assert_eq!(line["pages"], json!(201));    assert_eq!(line["ticks"][1]["at"], json!(147.0 / 200.0));    assert_eq!(line["words"]["here"], "Page 1");    let untold = a_book(&client, "epub");    assert_eq!(say(&client, &untold, 0.5), "50%");    assert_eq!(call_on(&client, "reader.seek.say", &json!({"book": book, "at": 1.5})).expect_err("off the line").code, "bad_request");}// RULE: a jump to a place lands exactly in the resource that holds it, at its share of that resource; a tap within eight points of a tick lands on its chapter's start, and one farther lands exactly; the contents' tick jump lands on the chapter's start (a section's at its anchor); a PDF's jump names the page.#[test]fn a_jump_lands_exactly_or_on_a_chapter_start() {    let (_dir, client, _clock) = attached_on_a_clock();    let book = an_open_epub(&client);    let exact = ask(&client, "reader.seek.go", json!({"book": book, "at": 0.65}));    assert_eq!(locator(&exact), json!({"href": "c3.xhtml", "type": "application/xhtml+xml", "locations": {"progression": 0.5, "totalProgression": 0.65}}));    // 0.51 on a 400-point line is 4 points from Three's start: it snaps.    let snapped = ask(&client, "reader.seek.go", json!({"book": book, "at": 0.51, "width": 400.0}));    assert_eq!(locator(&snapped), json!({"href": "c3.xhtml", "type": "application/xhtml+xml", "locations": {"progression": 0.0, "totalProgression": 0.5}}));    // 0.53 is 12 points away: it lands where it fell.    let far = ask(&client, "reader.seek.go", json!({"book": book, "at": 0.53, "width": 400.0}));    assert_eq!(locator(&far)["locations"]["totalProgression"], json!(0.53));    // A drag's release names no width: it never snaps.    let released = ask(&client, "reader.seek.go", json!({"book": book, "at": 0.51}));    assert_eq!(locator(&released)["locations"]["totalProgression"], json!(0.51));    let section = ask(&client, "reader.seek.go", json!({"book": book, "tick": 3}));    assert_eq!(locator(&section), json!({"href": "c3.xhtml", "type": "application/xhtml+xml", "locations": {"progression": 0.0, "totalProgression": 0.5, "fragments": ["later"]}}));    assert_eq!(call_on(&client, "reader.seek.go", &json!({"book": book, "tick": 9})).expect_err("no such tick").code, "bad_request");    assert_eq!(call_on(&client, "reader.seek.go", &json!({"book": book, "tick": 1, "at": 0.2})).expect_err("one or the other").code, "bad_request");    let pdf = a_book(&client, "pdf");    let contents = json!({"toc": [], "reading_order": ["orchard.pdf"], "starts": [0.0], "pages": 201});    ask(&client, "reader.moved", json!({"book": pdf, "at": {"href": "orchard.pdf", "type": "application/pdf", "locations": {"position": 41, "totalProgression": 0.2}}, "contents": contents, "active": true}));    let jumped = ask(&client, "reader.seek.go", json!({"book": pdf, "at": 0.735}));    assert_eq!(locator(&jumped), json!({"href": "orchard.pdf", "type": "application/pdf", "locations": {"position": 148, "totalProgression": 0.735}}));    assert_eq!(jumped["back"]["words"], "Back to page 41");    let closed = a_book(&client, "epub");    assert_eq!(call_on(&client, "reader.seek.go", &json!({"book": closed, "at": 0.5})).expect_err("not open").code, "bad_request");}// RULE: a jump keeps the place it left as Back ("Back to N%" without page numbers); the jump's landing is no turn, a refinement on the same page is none either, and the second page turned after it retires Back; reader.moved answers Back as it stands.#[test]fn back_retires_after_two_pages_turned() {    let (_dir, client, _clock) = attached_on_a_clock();    let book = an_open_epub(&client);    let went = ask(&client, "reader.seek.go", json!({"book": book, "at": 0.65}));    assert_eq!(went["back"]["words"], "Back to 30%");    assert_eq!(went["back"]["at"], json!(0.3));    assert_eq!(went["back"]["ms_left"], json!(20_000));    let moved = |at: Value| ask(&client, "reader.moved", json!({"book": book, "at": at, "active": true}));    assert!(moved(epub_at("c3.xhtml", 0.65, 65))["back"].is_object(), "the landing");    assert!(moved(epub_at("c3.xhtml", 0.6501, 65))["back"].is_object(), "the same page, refined");    assert!(moved(epub_at("c3.xhtml", 0.66, 66))["back"].is_object(), "one page turned");    assert_eq!(moved(epub_at("c3.xhtml", 0.67, 67))["back"], Value::Null, "two pages turned");    assert_eq!(ask(&client, "reader.seek", json!({"book": book}))["back"], Value::Null);    assert_eq!(ask(&client, "reader.seek.back", json!({"book": book})), json!({"locator": null, "back": null}));}// RULE: Back stands twenty seconds (ms_left counts down) and is gone after; a second jump while it stands keeps the first place left and starts its time again; reader.seek.back returns there and clears it.#[test]fn back_keeps_the_first_place_for_twenty_seconds() {    let (_dir, client, clock) = attached_on_a_clock();    let book = an_open_epub(&client);    ask(&client, "reader.seek.go", json!({"book": book, "at": 0.65}));    ask(&client, "reader.moved", json!({"book": book, "at": epub_at("c3.xhtml", 0.65, 65), "active": true}));    clock.advance(chrono::Duration::seconds(15));    assert_eq!(ask(&client, "reader.seek", json!({"book": book}))["back"]["ms_left"], json!(5_000));    let again = ask(&client, "reader.seek.go", json!({"book": book, "at": 0.9}));    assert_eq!(again["back"]["words"], "Back to 30%", "the finger stays in the old place");    assert_eq!(again["back"]["ms_left"], json!(20_000));    ask(&client, "reader.moved", json!({"book": book, "at": epub_at("c4.xhtml", 0.9, 90), "active": true}));    clock.advance(chrono::Duration::seconds(10));    let returned = ask(&client, "reader.seek.back", json!({"book": book}));    assert_eq!(locator(&returned), epub_at("c2.xhtml", 0.3, 30));    assert_eq!(returned["back"], Value::Null);    assert_eq!(ask(&client, "reader.seek", json!({"book": book}))["back"], Value::Null, "one level only");    ask(&client, "reader.seek.go", json!({"book": book, "at": 0.1}));    clock.advance(chrono::Duration::seconds(20));    assert_eq!(ask(&client, "reader.seek", json!({"book": book}))["back"], Value::Null, "twenty seconds");}// RULE: the contents' starts are one share per resource of the reading order, 0…1 and in order, and pages a positive count; anything else is refused.#[test]fn the_contents_starts_and_pages_are_checked() {    let (_dir, client, _clock) = attached_on_a_clock();    let book = a_book(&client, "epub");    for refused in [        json!({"toc": [], "reading_order": ["a", "b"], "starts": [0.0]}),        json!({"toc": [], "reading_order": ["a", "b"], "starts": [0.5, 0.2]}),        json!({"toc": [], "reading_order": ["a"], "starts": [1.5]}),        json!({"toc": [], "reading_order": ["a"], "pages": 0}),    ] {        let error = call_on(&client, "reader.moved", &json!({"book": book, "at": epub_at("a", 0.1, 1), "contents": refused, "active": true})).expect_err("refused");        assert_eq!(error.code, "bad_request", "{refused}");    }}// RULE: two pages turned inside one Readium position are two turns: a position is not a page in an EPUB on iOS, where several screens share one, so Back retires on the spot the page stands at moving twice.#[test]fn back_retires_after_two_pages_turned_inside_one_position() {    let (_dir, client, _clock) = attached_on_a_clock();    let book = an_open_epub(&client);    ask(&client, "reader.seek.go", json!({"book": book, "at": 0.65}));    let moved = |at: Value| ask(&client, "reader.moved", json!({"book": book, "at": at, "active": true}));    assert!(moved(epub_at("c3.xhtml", 0.65, 65))["back"].is_object(), "the landing");    assert!(moved(epub_at("c3.xhtml", 0.6501, 65))["back"].is_object(), "the same page, refined");    assert!(moved(epub_at("c3.xhtml", 0.653, 65))["back"].is_object(), "one page turned, the position the same");    assert_eq!(moved(epub_at("c3.xhtml", 0.656, 65))["back"], Value::Null, "two pages turned, the position the same");}// RULE: a book with pages stands on the line where its page does, whatever share its reader says: a PDF on its chapter's first page is in that chapter, by the line's words and its marked tick.#[test]fn a_pdf_stands_where_its_page_does() {    let (_dir, client, _clock) = attached_on_a_clock();    let pdf = a_book(&client, "pdf");    let contents = json!({"toc": [{"title": "Eight", "href": "orchard.pdf#page=22"}, {"title": "Nine", "href": "orchard.pdf#page=25"}], "reading_order": ["orchard.pdf"], "starts": [0.0], "pages": 36});    // Readium on iOS: page 25 of 36 is 24/36, short of the tick at 24/35.    let at = json!({"href": "orchard.pdf", "type": "application/pdf", "locations": {"position": 25, "totalProgression": 24.0 / 36.0}});    ask(&client, "reader.moved", json!({"book": pdf, "at": at, "contents": contents, "active": true}));    let line = ask(&client, "reader.seek", json!({"book": pdf}));    assert_eq!(line["words"]["here"], "Nine · page 25");    assert_eq!(line["here"], json!(24.0 / 35.0));    assert_eq!(line["ticks"][1]["here"], true);    assert_eq!(line["ticks"][0]["here"], false);}