Cotton code
100 lines · 5.4 KB · RustRaw
use super::*;use serde_json::json;const KOLKATA: &str = "Asia/Kolkata";fn at(value: &str) -> Option<MomentSplit> {    held(Some(&json!(value)), KOLKATA).unwrap()}fn keys(held: Option<&MomentSplit>) -> Vec<(String, bool)> {    time_choices(held, &[]).into_iter().map(|choice| (choice.key, choice.chosen)).collect()}#[test]fn the_fan_runs_no_time_the_hours_then_pick_a_time() {    let day = at("2026-10-23T18:30:00.000Z");    assert_eq!(day.as_ref().unwrap().time, None, "local midnight is a day with no time");    let labels: Vec<String> = time_choices(day.as_ref(), &[]).into_iter().map(|choice| choice.label).collect();    assert_eq!(labels, ["No time", "Morning", "9:30", "Noon", "Evening", "Pick a time"]);    assert_eq!(keys(day.as_ref())[0], ("none".to_string(), true));    assert_eq!(keys(None)[0], ("none".to_string(), true));}#[test]fn the_records_own_time_is_chosen_and_stands_in_its_place_among_the_hours() {    let noon = at("2026-10-24T06:30:00.000Z");    assert!(keys(noon.as_ref()).contains(&("12:00".to_string(), true)));    assert_eq!(keys(noon.as_ref()).iter().filter(|(_, chosen)| *chosen).count(), 1);    let half_ten = at("2026-10-24T05:00:00.000Z");    let fan = keys(half_ten.as_ref());    assert_eq!(fan.len(), 7);    assert_eq!(fan[1], ("09:00".to_string(), false));    assert_eq!(fan[2], ("09:30".to_string(), false));    assert_eq!(fan[3], ("10:30".to_string(), true));}#[test]fn a_new_day_keeps_the_hour_and_a_new_hour_keeps_the_day() {    let nine = at("2026-10-24T03:30:00.000Z");    let write = |date, time| written(nine.as_ref(), date, time, KOLKATA, "2026-10-23").unwrap();    assert_eq!(write(Some(Some("2026-10-30")), None).as_deref(), Some("2026-10-30T03:30:00.000Z"));    assert_eq!(write(None, Some(Some("18:00"))).as_deref(), Some("2026-10-24T12:30:00.000Z"));    assert_eq!(write(None, Some(None)).as_deref(), Some("2026-10-23T18:30:00.000Z"), "no time is local midnight");    assert_eq!(write(Some(None), None), None);}#[test]fn a_bare_day_reads_as_no_time_and_an_hour_alone_falls_on_today() {    let bare = at("2026-10-24");    assert_eq!(bare, Some(MomentSplit { date: "2026-10-24".into(), time: None }));    let write = |current: Option<&MomentSplit>, date, time| written(current, date, time, KOLKATA, "2026-10-23").unwrap();    assert_eq!(write(bare.as_ref(), None, Some(Some("09:00"))).as_deref(), Some("2026-10-24T03:30:00.000Z"));    assert_eq!(write(None, None, Some(Some("09:00"))).as_deref(), Some("2026-10-23T03:30:00.000Z"));    assert_eq!(write(None, None, Some(None)), None, "no time on no day is still no date");}#[test]fn a_day_or_hour_that_is_not_one_is_refused() {    assert!(matches!(written(None, Some(Some("24 Oct")), None, KOLKATA, "2026-10-23"), Err(MomentSplitError::NotACalendarDay(_))));    assert!(matches!(written(None, None, Some(Some("9am")), KOLKATA, "2026-10-23"), Err(MomentSplitError::NotAClockTime(_))));}#[test]fn time_leaves_take_their_words_from_the_database_lines() {    let rows = [LineRow { set: "editing".into(), line: "when.times.18:00".into(), words: "Synthetic evening".into() }];    let choices = time_choices(None, &rows);    assert_eq!(choices.iter().find(|choice| choice.key == "18:00").unwrap().label, "Synthetic evening");    assert_eq!(choices.iter().filter_map(|choice| choice.time.as_deref()).collect::<Vec<_>>(),        ["09:00", "09:30", "12:00", "18:00"]);}/// RULE: a column that holds a calendar day is written a bare day by When,/// and takes no time. WHY: Money's When wrote local midnight as an instant/// into a transaction's `occurred_on`; the money engine refused it ("must be/// a valid calendar day") and the held Rent receipt never moved.#[test]fn a_column_holding_days_is_written_a_bare_day_and_takes_no_time() {    let definition = |tool: &str, table: &str| {        crate::definitions::builtin_table_definition(table).cloned().unwrap_or_else(|| {            let package = crate::kit::package(tool).expect("the kit holds the tool").expect("it decodes").package;            package.tables.into_iter().find(|held| held.id == table).expect("the package carries the table")        })    };    let money = definition("money", "transaction");    assert!(holds_days(&money, "occurred_on"));    assert!(holds_days(&money, "due_date"));    assert!(!holds_days(&definition("todos", "todo"), "due_at"), "a todo's due holds an instant");    assert!(holds_days(&definition("people", "person"), "birthday"), "a birthday is a yearly day");    let document = crate::kit::table("document").expect("the document table");    assert!(holds_days(&document, "period_start") && holds_days(&document, "period_end"), "a made table's plain date takes a day, never an instant (a statement's period)");    let rent = at("2026-11-09");    assert_eq!(written_day(rent.as_ref(), Some(Some("2026-11-10")), None).unwrap().as_deref(), Some("2026-11-10"));    assert_eq!(written_day(rent.as_ref(), Some(Some("2026-11-10")), Some(None)).unwrap().as_deref(), Some("2026-11-10"), "No time is the day alone");    assert_eq!(written_day(rent.as_ref(), None, Some(None)).unwrap().as_deref(), Some("2026-11-09"));    assert_eq!(written_day(rent.as_ref(), Some(None), None).unwrap(), None);    assert_eq!(written_day(rent.as_ref(), Some(Some("2026-11-10")), Some(Some("09:30"))), Err(DayWhenError::TakesNoTime));    assert!(matches!(written_day(rent.as_ref(), Some(Some("10 Nov")), None), Err(DayWhenError::Day(MomentSplitError::NotACalendarDay(_)))));}