//! `decide` is a pure function over sixteen inputs, so it is tested over all //! sixteen rather than over the handful somebody thought of. use super::{decide, Delivery, DeliveryFacts, ImageState}; /// Every image a report can have, including having none. fn every_image() -> Vec> { vec![ None, Some(ImageState::Queued), Some(ImageState::Arrived), Some(ImageState::Refused("the upload was refused".to_string())), ] } /// The whole table, written out. Each row is `(row_queued, row_refused, image)` /// against the state it must produce — so a change to the precedence rule has /// to be argued for sixteen times, not slipped past the three cases a spot /// check would cover. #[test] fn every_combination_of_the_three_facts_decides_one_state() { let mut seen = 0; for row_queued in [false, true] { for row_refused in [None, Some("the server said no".to_string())] { for image in every_image() { let facts = DeliveryFacts { row_queued, row_refused: row_refused.clone(), row_last_error: None, image: image.clone(), }; let (state, reason) = decide(&facts); let expected = if row_refused.is_some() || matches!(image, Some(ImageState::Refused(_))) { Delivery::Refused } else if row_queued || image == Some(ImageState::Queued) { Delivery::Waiting } else { Delivery::Sent }; assert_eq!( state, expected, "row_queued={row_queued} row_refused={row_refused:?} image={image:?}" ); // A refusal always carries its reason; nothing else ever does. assert_eq!( reason.is_some(), state == Delivery::Refused, "only a refusal carries a reason: {facts:?}" ); seen += 1; } } } assert_eq!(seen, 16, "the table is 2 × 2 × 4 and all of it is exercised"); } /// The precedence that matters most, stated on its own so it cannot be lost in /// the table above: a row the server threw out never becomes patient. #[test] fn a_refused_row_outranks_a_queued_one_and_keeps_the_server_s_own_words() { let facts = DeliveryFacts { row_queued: true, row_refused: Some("cotton_feedback has no column `mood`".to_string()), row_last_error: None, image: Some(ImageState::Queued), }; let (state, reason) = decide(&facts); assert_eq!(state, Delivery::Refused); assert_eq!( reason.as_deref(), Some("cotton_feedback has no column `mood`"), "the reason a person reads is the SERVER's, never one invented here" ); } /// The row got there and only the picture is still climbing: that is a wait, /// and it is the commonest one — a report is one small row and a screenshot is /// megabytes. #[test] fn a_report_whose_picture_has_not_landed_is_still_waiting() { let facts = DeliveryFacts { row_queued: false, row_refused: None, row_last_error: None, image: Some(ImageState::Queued), }; assert_eq!(decide(&facts), (Delivery::Waiting, None)); } /// A picture the store gave up on is a refusal, not a wait — nothing is going /// to retry it, so "waiting" would be a sentence that never comes true. #[test] fn a_picture_the_ledger_gave_up_on_is_a_refusal() { let facts = DeliveryFacts { row_queued: false, row_refused: None, row_last_error: None, image: Some(ImageState::Refused("413 — too large".to_string())), }; let (state, reason) = decide(&facts); assert_eq!(state, Delivery::Refused); assert_eq!(reason.as_deref(), Some("413 — too large")); } /// The ordinary case, and the one the whole screen exists to be able to say /// truthfully. #[test] fn a_report_with_nothing_queued_and_nothing_refused_is_sent() { assert_eq!( decide(&DeliveryFacts::default()), (Delivery::Sent, None), "a report with no picture and nothing queued is at the server" ); assert_eq!( decide(&DeliveryFacts { row_queued: false, row_refused: None, row_last_error: None, image: Some(ImageState::Arrived), }), (Delivery::Sent, None) ); } /// The three words are the wire's, and the app switches on them. #[test] fn the_three_states_spell_themselves_the_way_the_app_reads_them() { assert_eq!(Delivery::Sent.as_str(), "sent"); assert_eq!(Delivery::Waiting.as_str(), "waiting"); assert_eq!(Delivery::Refused.as_str(), "refused"); }