| 1 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 2 | pub struct CalleeGate { |
| 3 | callees: usize, |
| 4 | failed: usize, |
| 5 | joined: bool, |
| 6 | media_connected: bool, |
| 7 | } |
| 8 | |
| 9 | impl CalleeGate { |
| 10 | pub fn new(callees: usize) -> Self { |
| 11 | CalleeGate { |
| 12 | callees, |
| 13 | failed: 0, |
| 14 | joined: false, |
| 15 | media_connected: false, |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | pub fn is_waiting(&self) -> bool { |
| 20 | self.callees > 0 && !self.joined |
| 21 | } |
| 22 | |
| 23 | pub fn callees_failed(&mut self, count: usize) -> bool { |
| 24 | if !self.is_waiting() { |
| 25 | return false; |
| 26 | } |
| 27 | self.failed += count; |
| 28 | self.failed >= self.callees |
| 29 | } |
| 30 | |
| 31 | pub fn media_connected(&mut self) -> bool { |
| 32 | self.media_connected = true; |
| 33 | !self.is_waiting() |
| 34 | } |
| 35 | |
| 36 | pub fn callee_joined(&mut self) -> bool { |
| 37 | let was_waiting = self.is_waiting(); |
| 38 | self.joined = true; |
| 39 | was_waiting && self.media_connected |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn roster_has_callee<'a>( |
| 44 | participant_mris: impl IntoIterator<Item = &'a str>, |
| 45 | own_mri: &str, |
| 46 | ) -> bool { |
| 47 | participant_mris.into_iter().any(|mri| mri != own_mri) |
| 48 | } |
| 49 | |
| 50 | /// The Echo bot hangs up after playing the recording back; Teams then ends the test call. |
| 51 | #[derive(Debug, Default)] |
| 52 | pub struct EchoWatch { |
| 53 | seen: bool, |
| 54 | } |
| 55 | |
| 56 | impl EchoWatch { |
| 57 | pub fn bot_left<'a>(&mut self, participant_mris: impl IntoIterator<Item = &'a str>) -> bool { |
| 58 | if participant_mris |
| 59 | .into_iter() |
| 60 | .any(|mri| mri == crate::signaling::ECHO_BOT_MRI) |
| 61 | { |
| 62 | self.seen = true; |
| 63 | return false; |
| 64 | } |
| 65 | self.seen |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[cfg(test)] |
| 70 | mod tests { |
| 71 | use super::*; |
| 72 | |
| 73 | #[test] |
| 74 | fn a_roster_with_only_ourselves_has_no_callee() { |
| 75 | assert!(!roster_has_callee(["8:orgid:me"], "8:orgid:me")); |
| 76 | assert!(!roster_has_callee([], "8:orgid:me")); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn a_roster_with_someone_else_has_the_callee() { |
| 81 | assert!(roster_has_callee( |
| 82 | ["8:orgid:me", "8:orgid:them"], |
| 83 | "8:orgid:me" |
| 84 | )); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn media_waits_for_the_callee_when_required() { |
| 89 | let mut gate = CalleeGate::new(1); |
| 90 | assert!(!gate.media_connected()); |
| 91 | assert!(gate.is_waiting()); |
| 92 | assert!(gate.callee_joined()); |
| 93 | assert!(!gate.is_waiting()); |
| 94 | assert!(gate.media_connected()); |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn a_callee_before_media_does_not_fire_early() { |
| 99 | let mut gate = CalleeGate::new(1); |
| 100 | assert!(!gate.callee_joined()); |
| 101 | assert!(gate.media_connected()); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn the_call_fails_only_when_every_callee_failed() { |
| 106 | let mut gate = CalleeGate::new(3); |
| 107 | assert!(!gate.callees_failed(1)); |
| 108 | assert!(gate.callees_failed(2)); |
| 109 | let mut joined = CalleeGate::new(1); |
| 110 | joined.callee_joined(); |
| 111 | assert!(!joined.callees_failed(1)); |
| 112 | assert!(!CalleeGate::new(0).callees_failed(1)); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn an_unrequired_gate_never_waits() { |
| 117 | let mut gate = CalleeGate::new(0); |
| 118 | assert!(!gate.is_waiting()); |
| 119 | assert!(gate.media_connected()); |
| 120 | assert!(!gate.callee_joined()); |
| 121 | } |
| 122 | |
| 123 | #[test] |
| 124 | fn the_echo_call_ends_only_after_the_bot_was_there_and_left() { |
| 125 | let me = "8:orgid:me"; |
| 126 | let bot = crate::signaling::ECHO_BOT_MRI; |
| 127 | let mut watch = EchoWatch::default(); |
| 128 | assert!(!watch.bot_left([me])); |
| 129 | assert!(!watch.bot_left([me, bot])); |
| 130 | assert!(!watch.bot_left([me, bot])); |
| 131 | assert!(watch.bot_left([me])); |
| 132 | } |
| 133 | } |