76 lines · 4.4 KB · RustRaw
use super::*;use crate::calling::machine::{step, Call, Callee, Event};use crate::calling::wire::{Joined, MediaState, Started};const ROOM: &str = "0123456789abcdef0123456789abcdef";const NUMBER: &str = "+919000000202";const NS: &str = "11111111-1111-4111-8111-111111111111";const T0: i64 = 1_762_934_400_000;fn out_call() -> Call { let dial = Event::Dial { uuid: "U".into(), callees: vec![Callee { number: NUMBER.into(), person: Some("p-1".into()), name: None }], video: true, group: None }; let started = Event::Started(Started { room: ROOM.into(), credential: "ccr_x".into(), ice_servers: vec![], ttl: 0, ring_for: 45, mode: Default::default(), me: None, group: None }); let (call, _) = step(&Call::idle(), dial, T0); step(&call, started, T0 + 200).0}fn in_call() -> Call { let (call, _) = step(&Call::idle(), Event::Incoming { room: ROOM.into(), video: false }, T0); let joined = Joined { room: ROOM.into(), from: NUMBER.into(), video: false, credential: "ccr_y".into(), ice_servers: vec![], ttl: 0, mode: Default::default(), me: None, group: None }; step(&call, Event::Joined { answer: joined, person: None, name: None }, T0 + 100).0}fn ended(call: &Call, frame: serde_json::Value, at: i64) -> Call { step(call, Event::Frame(serde_json::from_value(frame).unwrap()), at).0}// RULE: an answered call's row is answered, its length from live to end, its kind, reason and the other number, under the id every device of this database makes.#[test]fn an_answered_call_leaves_its_row() { let connecting = ended(&out_call(), serde_json::json!({"t": "state", "state": "connecting"}), T0 + 9_000); let live = step(&connecting, Event::Media(MediaState::Connected), T0 + 10_000).0; let over = step(&live, Event::HangUp, T0 + 135_500).0; let row = history_row(&over, NS).expect("a row"); assert_eq!(row.id, cotton_rules::ids::derived_id("cotton_call_log", &[NS, "cotton", ROOM])); assert_eq!((row.source.as_str(), row.provider.as_str(), row.external_id.as_str()), ("cotton", "cotton", ROOM)); assert_eq!((row.direction.as_str(), row.outcome.as_str(), row.kind.as_str()), ("out", "answered", "video")); assert_eq!(row.duration_s, 125); assert_eq!(row.ended_reason, "hung_up"); assert_eq!(row.handle.as_deref(), Some(NUMBER)); assert_eq!(row.person_id.as_deref(), Some("p-1")); assert_eq!(row.started_at, "2025-11-12T08:00:00.000Z"); assert!(row.live); assert_eq!(row.who, None, "a call of two names its person, not a list"); assert!(!row.missed, "a call made here is never missed");}// RULE: unanswered calls say what happened in the table's four words, from each side.#[test]fn unanswered_outcomes() { let cases = [("no_answer", "missed"), ("declined", "missed"), ("busy", "missed"), ("failed", "failed")]; for (reason, outcome) in cases { let call = ended(&out_call(), serde_json::json!({"t": "ended", "reason": reason}), T0 + 45_000); assert_eq!(history_row(&call, NS).unwrap().outcome, outcome, "{reason}"); } let cancelled = step(&out_call(), Event::HangUp, T0 + 3_000).0; assert_eq!(history_row(&cancelled, NS).unwrap().outcome, "cancelled"); let missed = ended(&in_call(), serde_json::json!({"t": "ended", "reason": "cancelled"}), T0 + 5_000); let row = history_row(&missed, NS).unwrap(); assert_eq!((row.direction.as_str(), row.outcome.as_str(), row.duration_s), ("in", "missed", 0)); assert!(row.missed, "a ring the caller gave up on is in Missed"); let declined = step(&in_call(), Event::Decline, T0 + 2_000).0; assert_eq!(history_row(&declined, NS).unwrap().outcome, "missed"); assert!(!history_row(&declined, NS).unwrap().missed, "a ring declined here was seen, not missed");}// RULE: two devices of the callee that both missed the ring land one row; a device whose ring another took writes none; a call not over writes none.#[test]fn one_row_per_room() { let a = history_row(&ended(&in_call(), serde_json::json!({"t": "ended", "reason": "missed"}), T0 + 45_000), NS).unwrap(); let b = history_row(&step(&in_call(), Event::Tick, T0 + 60_000).0, NS).unwrap(); assert_eq!(a.id, b.id); for elsewhere in ["answered_elsewhere", "declined_elsewhere"] { assert!(history_row(&ended(&in_call(), serde_json::json!({"t": "ended", "reason": elsewhere}), T0 + 3_000), NS).is_none(), "{elsewhere}"); } assert!(history_row(&out_call(), NS).is_none());}