79 lines · 3.9 KB · RustRaw
use super::*;use crate::messages::{MessagePollOption, MessagePollVoter};fn lines() -> LineSet { super::super::list::lines()}fn option(text: &str, votes: f64) -> MessagePollOption { MessagePollOption { text: text.into(), votes }}fn voter(who: &str, options: &[u32], at: &str) -> MessagePollVoter { MessagePollVoter { who: who.into(), options: options.to_vec(), at: Some(at.into()) }}fn named(who: &str) -> String { match who { "15550100001" => "Asha Example".into(), "15550100002" => "Ravi Example".into(), other => format!("+{other}"), }}/// RULE (FEEDBACK-3 "polls shown"): a link poll counts its voters: each option its count, its bar as a part of the/// people who voted, and who chose it, "You" first, then in the order they voted; the total counts each voter once/// however many options they chose; the owner's choice is marked; a number with no name is said as the number.#[test]fn a_link_poll_counts_who_voted_for_what() { let poll = MessagePoll { question: "Dinner on Friday?".into(), options: vec![option("Thai", 0.0), option("Pizza", 0.0), option("Stay in", 0.0)], selectable: Some(0), voters: vec![ voter("15550100002", &[0, 1], "2026-11-23T10:02:00.000Z"), voter("15550100001", &[0], "2026-11-23T10:01:00.000Z"), voter("me", &[1], "2026-11-23T10:05:00.000Z"), voter("15550109999", &[0], "2026-11-23T10:06:00.000Z"), voter("15550100003", &[], "2026-11-23T10:07:00.000Z"), ], }; let card = card(&lines(), &poll, &named); assert_eq!(card.question, "Dinner on Friday?"); assert_eq!(card.choose.as_deref(), Some("Select one or more")); assert_eq!(card.said, "4 votes", "four people voted; the one who took theirs back is not counted"); let thai = &card.options[0]; assert_eq!((thai.votes, thai.percent, thai.said.as_str(), thai.mine), (3, 75, "3 votes", false)); assert_eq!(thai.voters, vec!["Asha Example", "Ravi Example", "+15550109999"]); let pizza = &card.options[1]; assert_eq!((pizza.votes, pizza.percent, pizza.mine), (2, 50, true)); assert_eq!(pizza.voters, vec!["You", "Ravi Example"], "You first"); let stay = &card.options[2]; assert_eq!((stay.votes, stay.percent, stay.said.as_str()), (0, 0, "No votes")); assert!(stay.voters.is_empty()); assert_eq!(card.spoken, "Poll: Dinner on Friday? Select one or more. Thai, 3 votes. Pizza, 2 votes, your choice. Stay in, No votes. 4 votes");}/// RULE: one vote is "1 vote"; a poll nobody voted on yet says so; one that takes one choice says "Select one".#[test]fn a_new_poll_says_no_votes_yet_and_how_many_to_choose() { let poll = MessagePoll { question: "Which day?".into(), options: vec![option("Sat", 0.0), option("Sun", 0.0)], selectable: Some(1), voters: vec![] }; let before = card(&lines(), &poll, &named); assert_eq!(before.choose.as_deref(), Some("Select one")); assert_eq!(before.said, "No votes yet"); assert!(before.options.iter().all(|o| o.percent == 0 && o.votes == 0)); let voted = MessagePoll { voters: vec![voter("15550100001", &[1], "2026-11-23T10:00:00.000Z")], ..poll }; let after = card(&lines(), &voted, &named); assert_eq!((after.options[1].said.as_str(), after.options[1].percent, after.said.as_str()), ("1 vote", 100, "1 vote"));}/// RULE: an exported poll keeps its totals and names nobody (an export never says who voted): no "Select" line.#[test]fn an_exported_poll_keeps_its_totals_and_names_nobody() { let poll = MessagePoll { question: "Trek or beach?".into(), options: vec![option("Trek", 3.0), option("Beach", 1.0)], ..Default::default() }; let card = card(&lines(), &poll, &named); assert_eq!(card.choose, None); assert_eq!(card.said, "4 votes"); assert_eq!((card.options[0].votes, card.options[0].percent), (3, 75)); assert!(card.options.iter().all(|o| o.voters.is_empty() && !o.mine));}