Cotton code
170 lines · 6.3 KB · RustRaw
use super::*;const NOW: &str = "2026-09-28T12:00:00.000Z";fn act(kind: DoneKind, id: &str, at: &str) -> DoneAct {    DoneAct {        kind,        id: id.to_string(),        text: format!("text of {id}"),        filed_as: None,        at: at.to_string(),        count: 1,        opens: None,        by: None,        listed: Vec::new(),    }}fn ids(rows: &[DoneAct]) -> Vec<&str> {    rows.iter().map(|row| row.id.as_str()).collect()}#[test]fn newest_first_across_every_kind() {    let acts = [        act(DoneKind::Filed, "t1", "2026-09-27T08:00:00.000Z"),        act(DoneKind::Remembered, "m1", "2026-09-28T09:00:00.000Z"),        act(DoneKind::CouldNotRead, "d1", "2026-09-26T10:00:00.000Z"),    ];    assert_eq!(ids(&done_rows(&acts, NOW, 8)), ["m1", "t1", "d1"]);}/// Parsed, never lexical: "+05:30" is EARLIER than the "Z" string it sorts after.#[test]fn instants_sort_by_time_not_by_spelling() {    let acts = [        act(DoneKind::Filed, "zulu", "2026-09-28T06:00:00.000Z"),        act(DoneKind::Filed, "ist", "2026-09-28T10:00:00.000+05:30"),    ];    assert_eq!(ids(&done_rows(&acts, NOW, 8)), ["zulu", "ist"]);}#[test]fn only_the_last_two_weeks_and_at_most_the_limit() {    let acts = [        act(DoneKind::Filed, "old", "2026-09-13T11:59:00.000Z"),        act(DoneKind::Filed, "edge", "2026-09-14T12:00:00.000Z"),        act(DoneKind::Filed, "a", "2026-09-28T11:00:00.000Z"),        act(DoneKind::Filed, "b", "2026-09-28T10:00:00.000Z"),    ];    assert_eq!(ids(&done_rows(&acts, NOW, 8)), ["a", "b", "edge"]);    assert_eq!(ids(&done_rows(&acts, NOW, 2)), ["a", "b"]);}/// An unparseable instant is dropped rather than placed: placing it would be/// inventing when it happened.#[test]fn an_act_with_no_readable_time_is_left_out() {    let acts = [        act(DoneKind::Filed, "bad", "yesterday"),        act(DoneKind::Filed, "good", "2026-09-28T11:00:00.000Z"),    ];    assert_eq!(ids(&done_rows(&acts, NOW, 8)), ["good"]);    assert!(done_rows(&acts, "not a time", 8).is_empty());}/// RULE: every row on Done for you carries exactly one action — a filing, a remembered entry and a city fill are undone, a document that could not be read is changed on its own page; nothing there waits on the owner.#[test]fn every_kind_has_its_one_action() {    assert_eq!(done_action(DoneKind::Filed), DoneAction::Undo);    assert_eq!(done_action(DoneKind::Remembered), DoneAction::Undo);    assert_eq!(done_action(DoneKind::CouldNotRead), DoneAction::Change);    assert_eq!(done_action(DoneKind::CityFilled), DoneAction::Undo);    // A day of junk filing is taken back sender by sender, on the Senders page.    assert_eq!(done_action(DoneKind::JunkFiled), DoneAction::Change);}#[test]fn a_row_opens_the_record_it_is_about() {    assert_eq!(        done_path(&act(DoneKind::Filed, "t1", NOW)),        "/thoughts/thought/t1"    );    assert_eq!(done_path(&act(DoneKind::Remembered, "m1", NOW)), "/memory/m1");    assert_eq!(        done_path(&act(DoneKind::CouldNotRead, "d1", NOW)),        "/documents/document/d1"    );}#[test]fn a_filing_says_what_it_became() {    let filed = |kind: Option<&str>| DoneAct {        filed_as: kind.map(str::to_string),        ..act(DoneKind::Filed, "t", NOW)    };    assert_eq!(done_label(&filed(Some("todo")), &[]), "Filed as a todo");    assert_eq!(done_label(&filed(Some("event")), &[]), "Filed as an event");    assert_eq!(done_label(&filed(Some("person_note")), &[]), "Filed as a note on a person");    let row = LineRow { set: SET.into(), line: "done.filed.todo".into(), words: "Now a todo".into() };    assert_eq!(done_label(&filed(Some("todo")), &[row]), "Now a todo", "a rewording is a row");    assert_eq!(done_label(&filed(Some("mystery")), &[]), "Filed as a record");    assert_eq!(done_label(&filed(None), &[]), "Filed as a record");    assert_eq!(        done_label(&act(DoneKind::Remembered, "m", NOW), &[]),        "Remembered"    );    assert_eq!(        done_label(&act(DoneKind::CouldNotRead, "d", NOW), &[]),        "Could not read"    );}fn fill(person: &str, name: &str, city: &str, at: &str) -> CityFill {    CityFill {        person_id: person.to_string(),        name: name.to_string(),        city: city.to_string(),        at: at.to_string(),    }}/// RULE: a pass of the city fill is ONE row — its instant is the row's id (what the undo names), its people are named in the order read, and the rest are counted.#[test]fn a_city_fill_is_one_row_per_pass_naming_its_people() {    const EARLY: &str = "2026-09-27T08:00:00.000Z";    let acts = city_fill_acts(&[        fill("p1", "Cassius Brandt", "London", NOW),        fill("p2", "Jelena Vuković", "Belgrade", EARLY),        fill("p3", "Maren Holt", "Oslo", NOW),        fill("p4", "Ines Arroyo", "Madrid", NOW),        fill("p5", "Tomas Reyes", "Lisbon", NOW),    ]);    assert_eq!(acts.len(), 2);    let many = &acts[0];    assert_eq!((many.kind, many.id.as_str(), many.at.as_str()), (DoneKind::CityFilled, NOW, NOW));    assert_eq!(done_text(many, &[]), "Cassius Brandt — London, Maren Holt — Oslo and 2 more");    assert_eq!(many.count, 4);    assert_eq!(done_label(many, &[]), "Filled in 4 cities from their time zones");    assert_eq!(done_path(many), "/people");    let one = &acts[1];    assert_eq!((one.id.as_str(), done_text(one, &[]).as_str()), (EARLY, "Jelena Vuković — Belgrade"));    assert_eq!(done_label(one, &[]), "Filled in a city from the time zone");    assert_eq!(done_path(one), "/people/person/p2");}#[test]fn a_city_fill_of_exactly_the_named_people_counts_nothing_more() {    let acts = city_fill_acts(&[        fill("p1", "Cassius Brandt", "London", NOW),        fill("p3", " Maren Holt ", "Oslo", NOW),    ]);    assert_eq!(done_text(&acts[0], &[]), "Cassius Brandt — London, Maren Holt — Oslo");    assert_eq!(done_label(&acts[0], &[]), "Filled in 2 cities from their time zones");    assert!(city_fill_acts(&[]).is_empty());}#[test]fn a_city_fill_sorts_among_the_other_acts_by_its_instant() {    let mut acts = vec![        act(DoneKind::Filed, "t1", "2026-09-27T08:00:00.000Z"),        act(DoneKind::Remembered, "m1", "2026-09-28T09:00:00.000Z"),    ];    acts.extend(city_fill_acts(&[fill("p1", "Cassius Brandt", "London", "2026-09-28T07:00:00.000Z")]));    assert_eq!(        ids(&done_rows(&acts, NOW, 8)),        ["m1", "2026-09-28T07:00:00.000Z", "t1"]    );}