1mod beacon;
8mod codec;
9
10pub use beacon::{
11 Beacon, Error, FlushResult, HttpRequest, DEFAULT_INGEST_URL, DEFAULT_QUEUE, MAX_BODY,
12 MAX_FRAMES, MAX_QUEUE,
13};
14pub use codec::{
15 deg_to_micro, encode_loc_frames, encode_track_start, encode_track_stop, format_uuid, ErrorCode,
16 Point, ServerEvt,
17};
18
19#[cfg(test)]
20mod tests {
21 use super::*;
22 use crate::codec::{decode_server_concat, S_ACK, S_TRACK_STARTED};
23
24 #[test]
25 fn flush_puts_auth_in_query() {
26 let mut b = Beacon::new("dev-1", "s ecret");
27 b.push(55.0, 37.0, 1_700_000_000_000).unwrap();
28 let req = b.flush_request().unwrap();
29 assert!(req.url.starts_with(DEFAULT_INGEST_URL));
30 assert!(req.url.contains("client-id=dev-1"));
31 assert!(req.url.contains("client-secret=s%20ecret"));
32 assert_eq!(req.body[0], 0x02);
33 assert!(req.body.contains(&0x04));
34 }
35
36 #[test]
37 fn ack_drops_prefix() {
38 let mut b = Beacon::new("d", "s");
39 b.push(55.0, 37.0, 0).unwrap();
40 b.push(55.0001, 37.0001, 0).unwrap();
41 let req = b.flush_request().unwrap();
42 assert_eq!(b.queue_len(), 2);
43 let mut ack = vec![S_ACK];
44 ack.extend_from_slice(&1u32.to_le_bytes());
45 b.handle_response(&ack).unwrap();
46 assert_eq!(b.last_acked(), 1);
47 assert_eq!(b.queue_len(), 1);
48 let _ = req;
49 }
50
51 #[test]
52 fn overflow_drops_oldest() {
53 let mut b = Beacon::new("d", "s").with_queue_cap(2).unwrap();
54 b.push(1.0, 2.0, 0).unwrap();
55 b.push(1.0, 2.0001, 0).unwrap();
56 b.push(1.0, 2.0002, 0).unwrap();
57 assert_eq!(b.queue_len(), 2);
58 assert!(!b.flush_request().unwrap().body.is_empty());
59 }
60
61 #[test]
62 fn stop_appended() {
63 let mut b = Beacon::new("d", "s");
64 b.request_stop();
65 let req = b.flush_request().unwrap();
66 assert_eq!(req.body, vec![0x03]);
67 }
68
69 #[test]
70 fn track_started_uuid() {
71 let mut body = vec![S_TRACK_STARTED];
72 body.extend_from_slice(&[0x11u8; 16]);
73 body.extend_from_slice(&0u16.to_le_bytes());
74 let evts = decode_server_concat(&body).unwrap();
75 match &evts[0] {
76 ServerEvt::TrackStarted { track_uid, .. } => {
77 assert_eq!(format_uuid(track_uid).len(), 36);
78 }
79 other => panic!("{other:?}"),
80 }
81 let mut b = Beacon::new("d", "s");
82 b.handle_response(&body).unwrap();
83 assert!(b.track_uid().is_some());
84 }
85
86 #[test]
87 fn empty_flush() {
88 let mut b = Beacon::new("d", "s");
89 assert!(b.flush_request().is_none());
90 }
91}