use super::*; use crate::messages::app::list::lines; // RULE: A timer is off, a day, a week or ninety days; a message written under it goes that long after it was written, and the words say who set it and never promise more than they can. #[test] fn a_timer_says_its_length_who_set_it_and_what_it_cannot_do() { let l = lines(); assert_eq!(expires_at(1_000, 86_400), Some(86_401_000)); assert_eq!(expires_at(1_000, 0), None); assert_eq!(length(&l, 604_800), "7 days"); assert_eq!(event_line(&l, 604_800, None), "You turned on disappearing messages. New messages will disappear 7 days after they're sent."); assert_eq!(event_line(&l, 86_400, Some("Asha Example")), "Asha Example turned on disappearing messages. New messages will disappear 24 hours after they're sent."); assert_eq!(event_line(&l, 0, Some("Asha Example")), "Asha Example turned off disappearing messages."); let on = card(&l, 7_776_000, true); assert_eq!(on.label.as_deref(), Some("Disappearing messages: 90 days")); assert!(on.note.contains("screenshot"), "{}", on.note); assert_eq!(on.choices.iter().map(|c| (c.seconds, c.on)).collect::>(), [(0, false), (86_400, false), (604_800, false), (7_776_000, true)]); let off = card(&l, 0, false); assert!(off.label.is_none() && off.choices.is_empty() && off.note.contains("screenshot")); } // RULE: A WhatsApp chat's timer is the person's own copy's: its note says so on and off, and it can always be set. #[test] fn a_whatsapp_chats_timer_says_it_is_only_your_copy() { let l = lines(); for seconds in [0, 604_800] { let own = own_card(&l, seconds); assert_eq!(own.note, "Only your copy in Cotton. WhatsApp keeps its own."); assert_eq!(own.choices.len(), 4); assert_eq!(own.label.is_some(), seconds > 0); } } // RULE: On a WhatsApp chat a message goes by the timer standing when it was sent: nothing sent before the timer was turned on, nothing while it was off, and a message sent while it was on goes at its time even after it is turned off. #[test] fn a_whatsapp_message_goes_by_the_timer_standing_when_it_was_sent() { const DAY: i64 = 86_400_000; // Nothing on, nothing due. assert!(due_spans(&[], 100 * DAY, Some(100 * DAY)).is_empty()); assert!(due_spans(&[(10 * DAY, 0)], 100 * DAY, Some(100 * DAY)).is_empty()); // On at day 10 for a day; now day 20: sent from day 10 to day 19 (inclusive) is due. assert_eq!(due_spans(&[(10 * DAY, 86_400)], 20 * DAY, Some(20 * DAY)), [(10 * DAY, 19 * DAY + 1)]); // Not a day on yet: nothing is due. assert!(due_spans(&[(10 * DAY, 86_400)], 10 * DAY + 5, Some(10 * DAY + 5)).is_empty()); // On at day 10, off at day 12, on for a week at day 15; now day 30. let lines = [(10 * DAY, 86_400), (12 * DAY, 0), (15 * DAY, 604_800)]; assert_eq!(due_spans(&lines, 30 * DAY, Some(30 * DAY)), [(10 * DAY, 12 * DAY), (15 * DAY, 23 * DAY + 1)]); } // RULE (ABSENCE 2, 26 Nov 2026): a timer span whose end this copy does not hold (the newest line is on) erases nothing // until the copy knows there is no later line (`open_end` None: not caught up, or something of the chat parked); then // only up to the newest message this copy holds in the chat (`open_end`): a later "off" line would have come with it. #[test] fn a_span_with_no_end_held_erases_nothing_until_the_copy_knows() { const DAY: i64 = 86_400_000; let lines = [(10 * DAY, 0), (12 * DAY, 86_400)]; assert!(due_spans(&lines, 30 * DAY, None).is_empty(), "no end known: nothing"); // Caught up, the newest message held sent at day 18: up to it, never to now. assert_eq!(due_spans(&lines, 30 * DAY, Some(18 * DAY + 1)), [(12 * DAY, 18 * DAY + 1)]); // A span whose end is held is due as before, whatever the copy knows. let ended = [(10 * DAY, 86_400), (12 * DAY, 0)]; assert_eq!(due_spans(&ended, 30 * DAY, None), [(10 * DAY, 12 * DAY)]); } // RULE: a chat answers the next moment a message of it goes, so the phone asks again then and the core sweeps it: a // message goes by the timer line at or before it (one turned on later never takes it back in time); a span's earliest // held message is its next; a message already due, a timer off, or nothing held answers no moment. #[test] fn the_next_moment_a_message_goes_is_answered() { let day = 86_400_000_i64; let lines = [(10 * day, 86_400), (15 * day, 0), (20 * day, 604_800)]; assert_eq!(due_at(&lines, 9 * day), None, "before any timer"); assert_eq!(due_at(&lines, 12 * day), Some(13 * day), "a day after, under the first line"); assert_eq!(due_at(&lines, 16 * day), None, "the timer was off"); assert_eq!(due_at(&lines, 21 * day), Some(28 * day), "a week after, under the third"); let now = 21 * day + 5; assert_eq!(waiting_spans(&lines, now), [(20 * day, i64::MAX, 604_800)], "only the week's line still has messages to go"); assert_eq!(next_due(&lines, &[20 * day + 1_000, 21 * day], now), Some(27 * day + 1_000)); assert_eq!(next_due(&lines, &[12 * day], now), None, "due already: the sweep takes it, no moment to wait for"); assert_eq!(next_due(&lines, &[], now), None); assert_eq!(wait_until(27 * day, now), 6 * day - 5); assert_eq!(wait_until(now - 10, now), 1); }