Cotton code
82 lines · 3.9 KB · RustRaw
use super::*;fn utc(s: &str) -> DateTime<Utc> {    DateTime::parse_from_rfc3339(s).unwrap().with_timezone(&Utc)}const ZONE: &str = "Asia/Kolkata";fn now() -> DateTime<Utc> {    utc("2026-11-24T10:00:00Z")}// RULE: the file's own recorded time comes first, in either spelling.#[test]fn the_files_own_time_comes_first() {    let facts = Facts { name: Some("Recording 2026-11-01 09.00.00.m4a"), recorded: Some("20261120T103112.000Z"), modified_ms: Some(utc("2026-11-22T00:00:00Z").timestamp_millis()) };    assert_eq!(taken_at(&facts, ZONE, now()), utc("2026-11-20T10:31:12Z"));    let facts = Facts { recorded: Some("2026-11-19T08:00:00+05:30"), ..Facts::default() };    assert_eq!(taken_at(&facts, ZONE, now()), utc("2026-11-19T02:30:00Z"));}// RULE: an empty metadata date (1904, 1970) or one after now is not one.#[test]fn an_empty_or_later_date_is_not_one() {    let modified = utc("2026-11-21T12:00:00Z").timestamp_millis();    for said in ["19040101T000000.000Z", "19700101T000000.000Z", "20271201T000000.000Z", "nonsense"] {        let facts = Facts { recorded: Some(said), modified_ms: Some(modified), ..Facts::default() };        assert_eq!(taken_at(&facts, ZONE, now()), utc("2026-11-21T12:00:00Z"), "{said}");    }}// RULE: a date and time in the name is read in his zone, and beats the file's last-changed time.#[test]fn a_date_and_time_in_the_name_is_his_wall_clock() {    let modified = Some(utc("2026-11-23T00:00:00Z").timestamp_millis());    let cases = [        ("Recording 2026-11-20 10.31.12.m4a", "2026-11-20T05:01:12Z"),        ("Recording_20261120_103112.m4a", "2026-11-20T05:01:12Z"),        ("Voice 261120_103112.m4a", "2026-11-20T05:01:12Z"),        ("2026-11-20T10-31.wav", "2026-11-20T05:01:00Z"),    ];    for (name, at) in cases {        let facts = Facts { name: Some(name), modified_ms: modified, ..Facts::default() };        assert_eq!(taken_at(&facts, ZONE, now()), utc(at), "{name}");    }}// RULE: the file's last-changed time is its date, unless it is the copy just made to bring it in.#[test]fn the_files_changed_time_unless_it_is_the_copy() {    let facts = Facts { name: Some("Invented memo.m4a"), modified_ms: Some(utc("2026-11-18T07:15:00Z").timestamp_millis()), ..Facts::default() };    assert_eq!(taken_at(&facts, ZONE, now()), utc("2026-11-18T07:15:00Z"));    let copy = Facts { name: Some("Invented memo.m4a"), modified_ms: Some(now().timestamp_millis() - 5_000), ..Facts::default() };    assert_eq!(taken_at(&copy, ZONE, now()), now());}// RULE: a date alone in the name is that day: at the file's own time when it falls on it, else noon.#[test]fn a_date_alone_is_that_day() {    let same_day = Facts { name: Some("AUD-20261120-WA0003.opus"), modified_ms: Some(utc("2026-11-20T13:40:00Z").timestamp_millis()), ..Facts::default() };    assert_eq!(taken_at(&same_day, ZONE, now()), utc("2026-11-20T13:40:00Z"));    let copied = Facts { name: Some("PTT-20261120-WA0001.opus"), modified_ms: Some(now().timestamp_millis()), ..Facts::default() };    assert_eq!(taken_at(&copied, ZONE, now()), utc("2026-11-20T06:30:00Z"));}// RULE: nothing known is now.#[test]fn nothing_known_is_now() {    assert_eq!(taken_at(&Facts::default(), ZONE, now()), now());    assert_eq!(taken_at(&Facts { name: Some("Memo 12.m4a"), ..Facts::default() }, ZONE, now()), now());}// RULE: a date he sets is his wall clock, never after now; the picker shows it back the same way.#[test]fn a_date_set_by_hand() {    let at = set_by_hand("2026-11-20", "10:31", ZONE, now()).unwrap();    assert_eq!(at, utc("2026-11-20T05:01:00Z"));    assert_eq!(picker(at, ZONE), ("2026-11-20".to_string(), "10:31".to_string()));    assert_eq!(set_by_hand("2026-11-25", "09:00", ZONE, now()), Err(Refused::Later));    assert_eq!(set_by_hand("2026-13-01", "09:00", ZONE, now()), Err(Refused::Unreadable));    assert_eq!(set_by_hand("2026-11-20", "25:00", ZONE, now()), Err(Refused::Unreadable));}