Skip to main content

aranet_types/
lib.rs

1#![deny(unsafe_code)]
2
3//! Platform-agnostic types for Aranet environmental sensors.
4//!
5//! This crate provides shared types used across all aranet crates
6//! including aranet-core, aranet-cli, aranet-tui, and aranet-gui.
7//!
8//! # Features
9//!
10//! - Core data types for sensor readings
11//! - Device information structures
12//! - UUID constants for BLE characteristics
13//! - Error types for data parsing
14//!
15//! # Example
16//!
17//! ```
18//! use aranet_types::{CurrentReading, Status, DeviceType};
19//!
20//! // Types can be used for parsing and serialization
21//! ```
22
23pub mod error;
24pub mod types;
25pub mod uuid;
26
27pub use error::{ParseError, ParseResult};
28pub use types::{
29    CurrentReading, CurrentReadingBuilder, DeviceInfo, DeviceInfoBuilder, DeviceType,
30    HistoryRecord, HistoryRecordBuilder, MIN_CURRENT_READING_BYTES, Status,
31};
32
33// Re-export uuid module with a clearer name to avoid confusion with the `uuid` crate.
34// The `uuids` alias is kept for backwards compatibility.
35pub use uuid as ble;
36#[doc(hidden)]
37pub use uuid as uuids;
38
39/// Unit tests for aranet-types.
40///
41/// # Test Coverage
42///
43/// This module provides comprehensive tests for all public types and parsing functions:
44///
45/// ## CurrentReading Tests
46/// - Parsing from valid 13-byte Aranet4 format
47/// - Parsing from valid 7-byte Aranet2 format
48/// - Error handling for insufficient bytes
49/// - Edge cases (all zeros, max values)
50/// - Builder pattern validation
51/// - Serialization/deserialization roundtrips
52///
53/// ## Status Enum Tests
54/// - Conversion from u8 values (0-3 and unknown)
55/// - Display and Debug formatting
56/// - Equality and ordering
57///
58/// ## DeviceType Tests
59/// - Conversion from u8 device codes (0xF1-0xF4)
60/// - Name-based detection from device names
61/// - Display formatting
62/// - Hash implementation for use in collections
63///
64/// ## DeviceInfo Tests
65/// - Clone and Debug implementations
66/// - Default values
67/// - Equality comparisons
68///
69/// ## HistoryRecord Tests
70/// - Clone and equality
71/// - Timestamp handling
72///
73/// ## ParseError Tests
74/// - Error message formatting
75/// - Equality comparisons
76/// - Helper constructors
77///
78/// ## BLE UUID Tests
79/// - Service UUID constants
80/// - Characteristic UUID constants
81///
82/// # Running Tests
83///
84/// ```bash
85/// cargo test -p aranet-types
86/// ```
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    // ========================================================================
92    // CurrentReading parsing tests
93    // ========================================================================
94
95    #[test]
96    fn test_parse_current_reading_from_valid_bytes() {
97        // Construct test bytes:
98        // CO2: 800 (0x0320 LE -> [0x20, 0x03])
99        // Temperature: 450 raw (22.5°C = 450/20) -> [0xC2, 0x01]
100        // Pressure: 10132 raw (1013.2 hPa = 10132/10) -> [0x94, 0x27]
101        // Humidity: 45
102        // Battery: 85
103        // Status: 1 (Green)
104        // Interval: 300 -> [0x2C, 0x01]
105        // Age: 120 -> [0x78, 0x00]
106        let bytes: [u8; 13] = [
107            0x20, 0x03, // CO2 = 800
108            0xC2, 0x01, // temp_raw = 450
109            0x94, 0x27, // pressure_raw = 10132
110            45,   // humidity
111            85,   // battery
112            1,    // status = Green
113            0x2C, 0x01, // interval = 300
114            0x78, 0x00, // age = 120
115        ];
116
117        let reading = CurrentReading::from_bytes(&bytes).unwrap();
118
119        assert_eq!(reading.co2, 800);
120        assert!((reading.temperature - 22.5).abs() < 0.01);
121        assert!((reading.pressure - 1013.2).abs() < 0.1);
122        assert_eq!(reading.humidity, 45);
123        assert_eq!(reading.battery, 85);
124        assert_eq!(reading.status, Status::Green);
125        assert_eq!(reading.interval, 300);
126        assert_eq!(reading.age, 120);
127    }
128
129    #[test]
130    fn test_parse_current_reading_from_insufficient_bytes() {
131        let bytes: [u8; 10] = [0; 10]; // Only 10 bytes, need 13
132
133        let result = CurrentReading::from_bytes(&bytes);
134
135        assert!(result.is_err());
136        let err = result.unwrap_err();
137        assert_eq!(
138            err,
139            ParseError::InsufficientBytes {
140                expected: 13,
141                actual: 10
142            }
143        );
144        assert!(err.to_string().contains("expected 13"));
145        assert!(err.to_string().contains("got 10"));
146    }
147
148    #[test]
149    fn test_parse_current_reading_zero_bytes() {
150        let bytes: [u8; 0] = [];
151
152        let result = CurrentReading::from_bytes(&bytes);
153        assert!(result.is_err());
154    }
155
156    #[test]
157    fn test_parse_current_reading_all_zeros() {
158        let bytes: [u8; 13] = [0; 13];
159
160        let reading = CurrentReading::from_bytes(&bytes).unwrap();
161        assert_eq!(reading.co2, 0);
162        assert!((reading.temperature - 0.0).abs() < 0.01);
163        assert!((reading.pressure - 0.0).abs() < 0.1);
164        assert_eq!(reading.humidity, 0);
165        assert_eq!(reading.battery, 0);
166        assert_eq!(reading.status, Status::Error);
167        assert_eq!(reading.interval, 0);
168        assert_eq!(reading.age, 0);
169    }
170
171    #[test]
172    fn test_parse_current_reading_max_values() {
173        let bytes: [u8; 13] = [
174            0xFF, 0xFF, // CO2 = 65535
175            0xFF, 0xFF, // temp_raw = 65535
176            0xFF, 0xFF, // pressure_raw = 65535
177            0xFF, // humidity = 255
178            0xFF, // battery = 255
179            3,    // status = Red
180            0xFF, 0xFF, // interval = 65535
181            0xFF, 0xFF, // age = 65535
182        ];
183
184        let reading = CurrentReading::from_bytes(&bytes).unwrap();
185        assert_eq!(reading.co2, 65535);
186        assert!((reading.temperature - 3276.75).abs() < 0.01); // 65535/20
187        assert!((reading.pressure - 6553.5).abs() < 0.1); // 65535/10
188        assert_eq!(reading.humidity, 255);
189        assert_eq!(reading.battery, 255);
190        assert_eq!(reading.interval, 65535);
191        assert_eq!(reading.age, 65535);
192    }
193
194    #[test]
195    fn test_parse_current_reading_high_co2_red_status() {
196        // 2000 ppm CO2 = Red status
197        let bytes: [u8; 13] = [
198            0xD0, 0x07, // CO2 = 2000
199            0xC2, 0x01, // temp
200            0x94, 0x27, // pressure
201            50, 80, 3, // Red status
202            0x2C, 0x01, 0x78, 0x00,
203        ];
204
205        let reading = CurrentReading::from_bytes(&bytes).unwrap();
206        assert_eq!(reading.co2, 2000);
207        assert_eq!(reading.status, Status::Red);
208    }
209
210    #[test]
211    fn test_parse_current_reading_moderate_co2_yellow_status() {
212        // 1200 ppm CO2 = Yellow status
213        let bytes: [u8; 13] = [
214            0xB0, 0x04, // CO2 = 1200
215            0xC2, 0x01, 0x94, 0x27, 50, 80, 2, // Yellow status
216            0x2C, 0x01, 0x78, 0x00,
217        ];
218
219        let reading = CurrentReading::from_bytes(&bytes).unwrap();
220        assert_eq!(reading.co2, 1200);
221        assert_eq!(reading.status, Status::Yellow);
222    }
223
224    #[test]
225    fn test_parse_current_reading_extra_bytes_ignored() {
226        // More than 13 bytes should work (extra bytes ignored)
227        let bytes: [u8; 16] = [
228            0x20, 0x03, 0xC2, 0x01, 0x94, 0x27, 45, 85, 1, 0x2C, 0x01, 0x78, 0x00, 0xAA, 0xBB, 0xCC,
229        ];
230
231        let reading = CurrentReading::from_bytes(&bytes).unwrap();
232        assert_eq!(reading.co2, 800);
233    }
234
235    // --- Status enum tests ---
236
237    #[test]
238    fn test_status_from_u8() {
239        assert_eq!(Status::from(0), Status::Error);
240        assert_eq!(Status::from(1), Status::Green);
241        assert_eq!(Status::from(2), Status::Yellow);
242        assert_eq!(Status::from(3), Status::Red);
243        // Unknown values should map to Error
244        assert_eq!(Status::from(4), Status::Error);
245        assert_eq!(Status::from(255), Status::Error);
246    }
247
248    #[test]
249    fn test_status_repr_values() {
250        assert_eq!(Status::Error as u8, 0);
251        assert_eq!(Status::Green as u8, 1);
252        assert_eq!(Status::Yellow as u8, 2);
253        assert_eq!(Status::Red as u8, 3);
254    }
255
256    #[test]
257    fn test_status_debug() {
258        assert_eq!(format!("{:?}", Status::Green), "Green");
259        assert_eq!(format!("{:?}", Status::Yellow), "Yellow");
260        assert_eq!(format!("{:?}", Status::Red), "Red");
261        assert_eq!(format!("{:?}", Status::Error), "Error");
262    }
263
264    #[test]
265    fn test_status_clone() {
266        let status = Status::Green;
267        // Status implements Copy, so we can just copy it
268        let cloned = status;
269        assert_eq!(status, cloned);
270    }
271
272    #[test]
273    fn test_status_copy() {
274        let status = Status::Red;
275        let copied = status; // Copy
276        assert_eq!(status, copied); // Original still valid
277    }
278
279    // --- DeviceType enum tests ---
280
281    #[test]
282    fn test_device_type_values() {
283        assert_eq!(DeviceType::Aranet4 as u8, 0xF1);
284        assert_eq!(DeviceType::Aranet2 as u8, 0xF2);
285        assert_eq!(DeviceType::AranetRadon as u8, 0xF3);
286        assert_eq!(DeviceType::AranetRadiation as u8, 0xF4);
287    }
288
289    #[test]
290    fn test_device_type_debug() {
291        assert_eq!(format!("{:?}", DeviceType::Aranet4), "Aranet4");
292        assert_eq!(format!("{:?}", DeviceType::Aranet2), "Aranet2");
293        assert_eq!(format!("{:?}", DeviceType::AranetRadon), "AranetRadon");
294        assert_eq!(
295            format!("{:?}", DeviceType::AranetRadiation),
296            "AranetRadiation"
297        );
298    }
299
300    #[test]
301    fn test_device_type_clone() {
302        let device_type = DeviceType::Aranet4;
303        // DeviceType implements Copy, so we can just copy it
304        let cloned = device_type;
305        assert_eq!(device_type, cloned);
306    }
307
308    #[test]
309    fn test_device_type_try_from_u8() {
310        assert_eq!(DeviceType::try_from(0xF1), Ok(DeviceType::Aranet4));
311        assert_eq!(DeviceType::try_from(0xF2), Ok(DeviceType::Aranet2));
312        assert_eq!(DeviceType::try_from(0xF3), Ok(DeviceType::AranetRadon));
313        assert_eq!(DeviceType::try_from(0xF4), Ok(DeviceType::AranetRadiation));
314    }
315
316    #[test]
317    fn test_device_type_try_from_u8_invalid() {
318        let result = DeviceType::try_from(0x00);
319        assert!(result.is_err());
320        assert_eq!(result.unwrap_err(), ParseError::UnknownDeviceType(0x00));
321
322        let result = DeviceType::try_from(0xFF);
323        assert!(result.is_err());
324        assert_eq!(result.unwrap_err(), ParseError::UnknownDeviceType(0xFF));
325    }
326
327    #[test]
328    fn test_device_type_display() {
329        assert_eq!(format!("{}", DeviceType::Aranet4), "Aranet4");
330        assert_eq!(format!("{}", DeviceType::Aranet2), "Aranet2");
331        assert_eq!(format!("{}", DeviceType::AranetRadon), "Aranet Radon");
332        assert_eq!(
333            format!("{}", DeviceType::AranetRadiation),
334            "Aranet Radiation"
335        );
336    }
337
338    #[test]
339    fn test_device_type_hash() {
340        use std::collections::HashSet;
341        let mut set = HashSet::new();
342        set.insert(DeviceType::Aranet4);
343        set.insert(DeviceType::Aranet2);
344        set.insert(DeviceType::Aranet4); // duplicate
345        assert_eq!(set.len(), 2);
346        assert!(set.contains(&DeviceType::Aranet4));
347        assert!(set.contains(&DeviceType::Aranet2));
348    }
349
350    #[test]
351    fn test_status_display() {
352        assert_eq!(format!("{}", Status::Error), "Error");
353        assert_eq!(format!("{}", Status::Green), "Good");
354        assert_eq!(format!("{}", Status::Yellow), "Moderate");
355        assert_eq!(format!("{}", Status::Red), "High");
356    }
357
358    #[test]
359    fn test_status_hash() {
360        use std::collections::HashSet;
361        let mut set = HashSet::new();
362        set.insert(Status::Green);
363        set.insert(Status::Yellow);
364        set.insert(Status::Green); // duplicate
365        assert_eq!(set.len(), 2);
366        assert!(set.contains(&Status::Green));
367        assert!(set.contains(&Status::Yellow));
368    }
369
370    // --- DeviceInfo tests ---
371
372    #[test]
373    fn test_device_info_creation() {
374        let info = types::DeviceInfo {
375            name: "Aranet4 12345".to_string(),
376            model: "Aranet4".to_string(),
377            serial: "12345".to_string(),
378            firmware: "v1.2.0".to_string(),
379            hardware: "1.0".to_string(),
380            software: "1.2.0".to_string(),
381            manufacturer: "SAF Tehnika".to_string(),
382        };
383
384        assert_eq!(info.name, "Aranet4 12345");
385        assert_eq!(info.serial, "12345");
386        assert_eq!(info.manufacturer, "SAF Tehnika");
387    }
388
389    #[test]
390    fn test_device_info_clone() {
391        let info = types::DeviceInfo {
392            name: "Test".to_string(),
393            model: "Model".to_string(),
394            serial: "123".to_string(),
395            firmware: "1.0".to_string(),
396            hardware: "1.0".to_string(),
397            software: "1.0".to_string(),
398            manufacturer: "Mfg".to_string(),
399        };
400
401        let cloned = info.clone();
402        assert_eq!(cloned.name, info.name);
403        assert_eq!(cloned.serial, info.serial);
404    }
405
406    #[test]
407    fn test_device_info_debug() {
408        let info = types::DeviceInfo {
409            name: "Aranet4".to_string(),
410            model: "".to_string(),
411            serial: "".to_string(),
412            firmware: "".to_string(),
413            hardware: "".to_string(),
414            software: "".to_string(),
415            manufacturer: "".to_string(),
416        };
417
418        let debug_str = format!("{:?}", info);
419        assert!(debug_str.contains("Aranet4"));
420    }
421
422    #[test]
423    fn test_device_info_default() {
424        let info = types::DeviceInfo::default();
425        assert_eq!(info.name, "");
426        assert_eq!(info.model, "");
427        assert_eq!(info.serial, "");
428        assert_eq!(info.firmware, "");
429        assert_eq!(info.hardware, "");
430        assert_eq!(info.software, "");
431        assert_eq!(info.manufacturer, "");
432    }
433
434    #[test]
435    fn test_device_info_equality() {
436        let info1 = types::DeviceInfo {
437            name: "Test".to_string(),
438            model: "Model".to_string(),
439            serial: "123".to_string(),
440            firmware: "1.0".to_string(),
441            hardware: "1.0".to_string(),
442            software: "1.0".to_string(),
443            manufacturer: "Mfg".to_string(),
444        };
445        let info2 = info1.clone();
446        let info3 = types::DeviceInfo {
447            name: "Different".to_string(),
448            ..info1.clone()
449        };
450        assert_eq!(info1, info2);
451        assert_ne!(info1, info3);
452    }
453
454    // --- HistoryRecord tests ---
455
456    #[test]
457    fn test_history_record_creation() {
458        use time::OffsetDateTime;
459
460        let record = types::HistoryRecord {
461            timestamp: OffsetDateTime::UNIX_EPOCH,
462            co2: 800,
463            temperature: 22.5,
464            pressure: 1013.2,
465            humidity: 45,
466            radon: None,
467            radiation_rate: None,
468            radiation_total: None,
469        };
470
471        assert_eq!(record.co2, 800);
472        assert!((record.temperature - 22.5).abs() < 0.01);
473        assert!((record.pressure - 1013.2).abs() < 0.1);
474        assert_eq!(record.humidity, 45);
475        assert!(record.radon.is_none());
476        assert!(record.radiation_rate.is_none());
477        assert!(record.radiation_total.is_none());
478    }
479
480    #[test]
481    fn test_history_record_clone() {
482        use time::OffsetDateTime;
483
484        let record = types::HistoryRecord {
485            timestamp: OffsetDateTime::UNIX_EPOCH,
486            co2: 500,
487            temperature: 20.0,
488            pressure: 1000.0,
489            humidity: 50,
490            radon: Some(100),
491            radiation_rate: Some(0.15),
492            radiation_total: Some(1.5),
493        };
494
495        let cloned = record.clone();
496        assert_eq!(cloned.co2, record.co2);
497        assert_eq!(cloned.humidity, record.humidity);
498        assert_eq!(cloned.radon, Some(100));
499        assert_eq!(cloned.radiation_rate, Some(0.15));
500        assert_eq!(cloned.radiation_total, Some(1.5));
501    }
502
503    #[test]
504    fn test_history_record_equality() {
505        use time::OffsetDateTime;
506
507        let record1 = types::HistoryRecord {
508            timestamp: OffsetDateTime::UNIX_EPOCH,
509            co2: 800,
510            temperature: 22.5,
511            pressure: 1013.2,
512            humidity: 45,
513            radon: None,
514            radiation_rate: None,
515            radiation_total: None,
516        };
517        let record2 = record1.clone();
518        assert_eq!(record1, record2);
519    }
520
521    #[test]
522    fn test_current_reading_equality() {
523        let reading1 = CurrentReading {
524            co2: 800,
525            temperature: 22.5,
526            pressure: 1013.2,
527            humidity: 45,
528            battery: 85,
529            status: Status::Green,
530            interval: 300,
531            age: 120,
532            captured_at: None,
533            radon: None,
534            radiation_rate: None,
535            radiation_total: None,
536            radon_avg_24h: None,
537            radon_avg_7d: None,
538            radon_avg_30d: None,
539        };
540        // CurrentReading implements Copy, so we can just copy it
541        let reading2 = reading1;
542        assert_eq!(reading1, reading2);
543    }
544
545    #[test]
546    fn test_min_current_reading_bytes_const() {
547        assert_eq!(MIN_CURRENT_READING_BYTES, 13);
548        // Ensure buffer of exact size works
549        let bytes = [0u8; MIN_CURRENT_READING_BYTES];
550        assert!(CurrentReading::from_bytes(&bytes).is_ok());
551        // Ensure buffer one byte short fails
552        let short_bytes = [0u8; MIN_CURRENT_READING_BYTES - 1];
553        assert!(CurrentReading::from_bytes(&short_bytes).is_err());
554    }
555
556    // --- ParseError tests ---
557
558    #[test]
559    fn test_parse_error_display() {
560        let err = ParseError::invalid_value("test message");
561        assert_eq!(err.to_string(), "Invalid value: test message");
562    }
563
564    #[test]
565    fn test_parse_error_insufficient_bytes() {
566        let err = ParseError::InsufficientBytes {
567            expected: 13,
568            actual: 5,
569        };
570        assert_eq!(err.to_string(), "Insufficient bytes: expected 13, got 5");
571    }
572
573    #[test]
574    fn test_parse_error_unknown_device_type() {
575        let err = ParseError::UnknownDeviceType(0xAB);
576        assert_eq!(err.to_string(), "Unknown device type: 0xAB");
577    }
578
579    #[test]
580    fn test_parse_error_invalid_value() {
581        let err = ParseError::InvalidValue("bad value".to_string());
582        assert_eq!(err.to_string(), "Invalid value: bad value");
583    }
584
585    #[test]
586    fn test_parse_error_debug() {
587        let err = ParseError::invalid_value("debug test");
588        let debug_str = format!("{:?}", err);
589        assert!(debug_str.contains("InvalidValue"));
590        assert!(debug_str.contains("debug test"));
591    }
592
593    #[test]
594    fn test_parse_error_equality() {
595        let err1 = ParseError::InsufficientBytes {
596            expected: 10,
597            actual: 5,
598        };
599        let err2 = ParseError::InsufficientBytes {
600            expected: 10,
601            actual: 5,
602        };
603        let err3 = ParseError::InsufficientBytes {
604            expected: 10,
605            actual: 6,
606        };
607        assert_eq!(err1, err2);
608        assert_ne!(err1, err3);
609    }
610
611    // --- Serialization tests ---
612
613    #[test]
614    fn test_current_reading_serialization() {
615        let reading = CurrentReading {
616            co2: 800,
617            temperature: 22.5,
618            pressure: 1013.2,
619            humidity: 45,
620            battery: 85,
621            status: Status::Green,
622            interval: 300,
623            age: 120,
624            captured_at: None,
625            radon: None,
626            radiation_rate: None,
627            radiation_total: None,
628            radon_avg_24h: None,
629            radon_avg_7d: None,
630            radon_avg_30d: None,
631        };
632
633        let json = serde_json::to_string(&reading).unwrap();
634        assert!(json.contains("\"co2\":800"));
635        assert!(json.contains("\"humidity\":45"));
636    }
637
638    #[test]
639    fn test_current_reading_deserialization() {
640        let json = r#"{"co2":800,"temperature":22.5,"pressure":1013.2,"humidity":45,"battery":85,"status":"Green","interval":300,"age":120,"radon":null,"radiation_rate":null,"radiation_total":null}"#;
641
642        let reading: CurrentReading = serde_json::from_str(json).unwrap();
643        assert_eq!(reading.co2, 800);
644        assert_eq!(reading.status, Status::Green);
645    }
646
647    #[test]
648    fn test_status_serialization() {
649        assert_eq!(serde_json::to_string(&Status::Green).unwrap(), "\"Green\"");
650        assert_eq!(
651            serde_json::to_string(&Status::Yellow).unwrap(),
652            "\"Yellow\""
653        );
654        assert_eq!(serde_json::to_string(&Status::Red).unwrap(), "\"Red\"");
655        assert_eq!(serde_json::to_string(&Status::Error).unwrap(), "\"Error\"");
656    }
657
658    #[test]
659    fn test_device_type_serialization() {
660        assert_eq!(
661            serde_json::to_string(&DeviceType::Aranet4).unwrap(),
662            "\"Aranet4\""
663        );
664        assert_eq!(
665            serde_json::to_string(&DeviceType::AranetRadon).unwrap(),
666            "\"AranetRadon\""
667        );
668    }
669
670    #[test]
671    fn test_device_info_serialization_roundtrip() {
672        let info = types::DeviceInfo {
673            name: "Test Device".to_string(),
674            model: "Model X".to_string(),
675            serial: "SN12345".to_string(),
676            firmware: "1.2.3".to_string(),
677            hardware: "2.0".to_string(),
678            software: "3.0".to_string(),
679            manufacturer: "Acme Corp".to_string(),
680        };
681
682        let json = serde_json::to_string(&info).unwrap();
683        let deserialized: types::DeviceInfo = serde_json::from_str(&json).unwrap();
684
685        assert_eq!(deserialized.name, info.name);
686        assert_eq!(deserialized.serial, info.serial);
687        assert_eq!(deserialized.manufacturer, info.manufacturer);
688    }
689
690    // --- New feature tests ---
691
692    #[test]
693    fn test_status_ordering() {
694        // Status should be ordered by severity
695        assert!(Status::Error < Status::Green);
696        assert!(Status::Green < Status::Yellow);
697        assert!(Status::Yellow < Status::Red);
698
699        // Test comparison operators
700        assert!(Status::Red > Status::Yellow);
701        assert!(Status::Yellow >= Status::Yellow);
702        assert!(Status::Green <= Status::Yellow);
703    }
704
705    #[test]
706    fn test_device_type_readings_characteristic() {
707        use crate::ble;
708
709        // Aranet4 uses the original characteristic
710        assert_eq!(
711            DeviceType::Aranet4.readings_characteristic(),
712            ble::CURRENT_READINGS_DETAIL
713        );
714
715        // Other devices use the alternate characteristic
716        assert_eq!(
717            DeviceType::Aranet2.readings_characteristic(),
718            ble::CURRENT_READINGS_DETAIL_ALT
719        );
720        assert_eq!(
721            DeviceType::AranetRadon.readings_characteristic(),
722            ble::CURRENT_READINGS_DETAIL_ALT
723        );
724        assert_eq!(
725            DeviceType::AranetRadiation.readings_characteristic(),
726            ble::CURRENT_READINGS_DETAIL_ALT
727        );
728    }
729
730    #[test]
731    fn test_device_type_from_name_word_boundary() {
732        // Should match at word boundaries
733        assert_eq!(
734            DeviceType::from_name("Aranet4 12345"),
735            Some(DeviceType::Aranet4)
736        );
737        assert_eq!(
738            DeviceType::from_name("My Aranet4"),
739            Some(DeviceType::Aranet4)
740        );
741
742        // Should match case-insensitively
743        assert_eq!(DeviceType::from_name("ARANET4"), Some(DeviceType::Aranet4));
744        assert_eq!(DeviceType::from_name("aranet2"), Some(DeviceType::Aranet2));
745
746        // Should match AranetRn+ naming convention (real device name format)
747        assert_eq!(
748            DeviceType::from_name("AranetRn+ 306B8"),
749            Some(DeviceType::AranetRadon)
750        );
751        assert_eq!(
752            DeviceType::from_name("aranetrn+ 12345"),
753            Some(DeviceType::AranetRadon)
754        );
755    }
756
757    #[test]
758    fn test_byte_size_constants() {
759        assert_eq!(MIN_CURRENT_READING_BYTES, 13);
760        assert_eq!(types::MIN_ARANET2_READING_BYTES, 7);
761        assert_eq!(types::MIN_RADON_READING_BYTES, 15);
762        assert_eq!(types::MIN_RADON_GATT_READING_BYTES, 18);
763        assert_eq!(types::MIN_RADIATION_READING_BYTES, 28);
764    }
765
766    #[test]
767    fn test_from_bytes_aranet2() {
768        // 7 bytes: temp(2), humidity(1), battery(1), status(1), interval(2)
769        let data = [
770            0x90, 0x01, // temp = 400 -> 20.0°C
771            0x32, // humidity = 50
772            0x55, // battery = 85
773            0x01, // status = Green
774            0x2C, 0x01, // interval = 300
775        ];
776
777        let reading = CurrentReading::from_bytes_aranet2(&data).unwrap();
778        assert_eq!(reading.co2, 0); // Aranet2 has no CO2
779        assert!((reading.temperature - 20.0).abs() < 0.1);
780        assert_eq!(reading.humidity, 50);
781        assert_eq!(reading.battery, 85);
782        assert_eq!(reading.status, Status::Green);
783        assert_eq!(reading.interval, 300);
784        assert_eq!(reading.pressure, 0.0); // Aranet2 has no pressure
785    }
786
787    #[test]
788    fn test_from_bytes_aranet2_insufficient() {
789        let data = [0u8; 6]; // Too short
790        let result = CurrentReading::from_bytes_aranet2(&data);
791        assert!(result.is_err());
792    }
793
794    #[test]
795    fn test_from_bytes_for_device() {
796        // Test dispatch to correct parser
797        let aranet4_data = [0u8; 13];
798        let result = CurrentReading::from_bytes_for_device(&aranet4_data, DeviceType::Aranet4);
799        assert!(result.is_ok());
800
801        let aranet2_data = [0u8; 7];
802        let result = CurrentReading::from_bytes_for_device(&aranet2_data, DeviceType::Aranet2);
803        assert!(result.is_ok());
804    }
805
806    #[test]
807    fn test_builder_with_captured_at() {
808        use time::OffsetDateTime;
809
810        let now = OffsetDateTime::now_utc();
811        let reading = CurrentReading::builder()
812            .co2(800)
813            .temperature(22.5)
814            .captured_at(now)
815            .build();
816
817        assert_eq!(reading.co2, 800);
818        assert_eq!(reading.captured_at, Some(now));
819    }
820
821    #[test]
822    fn test_builder_try_build_valid() {
823        let result = CurrentReading::builder()
824            .co2(800)
825            .temperature(22.5)
826            .pressure(1013.0)
827            .humidity(50)
828            .battery(85)
829            .try_build();
830
831        assert!(result.is_ok());
832    }
833
834    #[test]
835    fn test_builder_try_build_invalid_humidity() {
836        let result = CurrentReading::builder()
837            .humidity(150) // Invalid: > 100
838            .try_build();
839
840        assert!(result.is_err());
841        let err = result.unwrap_err();
842        assert!(err.to_string().contains("humidity"));
843    }
844
845    #[test]
846    fn test_builder_try_build_invalid_battery() {
847        let result = CurrentReading::builder()
848            .battery(120) // Invalid: > 100
849            .try_build();
850
851        assert!(result.is_err());
852        let err = result.unwrap_err();
853        assert!(err.to_string().contains("battery"));
854    }
855
856    #[test]
857    fn test_builder_try_build_invalid_temperature() {
858        let result = CurrentReading::builder()
859            .temperature(-50.0) // Invalid: < -40
860            .try_build();
861
862        assert!(result.is_err());
863        let err = result.unwrap_err();
864        assert!(err.to_string().contains("temperature"));
865    }
866
867    #[test]
868    fn test_builder_try_build_invalid_pressure() {
869        let result = CurrentReading::builder()
870            .temperature(22.0) // Valid temperature
871            .pressure(500.0) // Invalid: < 800
872            .try_build();
873
874        assert!(result.is_err());
875        let err = result.unwrap_err();
876        assert!(err.to_string().contains("pressure"));
877    }
878
879    #[test]
880    fn test_with_captured_at() {
881        use time::OffsetDateTime;
882
883        let reading = CurrentReading::builder().age(60).build();
884
885        let now = OffsetDateTime::now_utc();
886        let reading_with_time = reading.with_captured_at(now);
887
888        assert!(reading_with_time.captured_at.is_some());
889        // The captured_at should be approximately now - 60 seconds
890        let captured = reading_with_time.captured_at.unwrap();
891        let expected = now - time::Duration::seconds(60);
892        assert!((captured - expected).whole_seconds().abs() < 2);
893    }
894
895    #[test]
896    fn test_parse_error_invalid_value_helper() {
897        let err = ParseError::invalid_value("test error");
898        assert_eq!(err.to_string(), "Invalid value: test error");
899    }
900}
901
902/// Property-based tests using proptest.
903///
904/// These tests use randomized inputs to verify that parsing functions:
905/// 1. Never panic on any input (safety guarantee)
906/// 2. Correctly parse valid inputs (correctness guarantee)
907/// 3. Properly roundtrip through serialization (consistency guarantee)
908///
909/// # Test Categories
910///
911/// ## Panic Safety Tests
912/// - `parse_current_reading_never_panics`: Random bytes to `from_bytes`
913/// - `parse_aranet2_never_panics`: Random bytes to `from_bytes_aranet2`
914/// - `status_from_u8_never_panics`: Any u8 to Status
915/// - `device_type_try_from_never_panics`: Any u8 to DeviceType
916///
917/// ## Valid Input Tests
918/// - `parse_valid_aranet4_bytes`: Structured valid Aranet4 data
919/// - `parse_valid_aranet2_bytes`: Structured valid Aranet2 data
920///
921/// ## Roundtrip Tests
922/// - `current_reading_json_roundtrip`: JSON serialization consistency
923///
924/// # Running Property Tests
925///
926/// ```bash
927/// cargo test -p aranet-types proptests
928/// ```
929///
930/// To run with more test cases:
931/// ```bash
932/// PROPTEST_CASES=10000 cargo test -p aranet-types proptests
933/// ```
934#[cfg(test)]
935mod proptests {
936    use super::*;
937    use proptest::prelude::*;
938
939    proptest! {
940        /// Parsing random bytes should never panic - it may return Ok or Err,
941        /// but should always be safe to call.
942        #[test]
943        fn parse_current_reading_never_panics(data: Vec<u8>) {
944            let _ = CurrentReading::from_bytes(&data);
945        }
946
947        /// Parsing random bytes for Aranet2 should never panic.
948        #[test]
949        fn parse_aranet2_never_panics(data: Vec<u8>) {
950            let _ = CurrentReading::from_bytes_aranet2(&data);
951        }
952
953        /// Status conversion from any u8 should never panic.
954        #[test]
955        fn status_from_u8_never_panics(value: u8) {
956            let status = Status::from(value);
957            // Should always produce a valid Status variant
958            let _ = format!("{:?}", status);
959        }
960
961        /// DeviceType conversion should return Ok or Err, never panic.
962        #[test]
963        fn device_type_try_from_never_panics(value: u8) {
964            let _ = DeviceType::try_from(value);
965        }
966
967        /// Valid 13-byte input should always parse successfully for Aranet4.
968        #[test]
969        fn parse_valid_aranet4_bytes(
970            co2 in 0u16..10000u16,
971            temp_raw in 0u16..2000u16,
972            pressure_raw in 8000u16..12000u16,
973            humidity in 0u8..100u8,
974            battery in 0u8..100u8,
975            status_byte in 0u8..4u8,
976            interval in 60u16..3600u16,
977            age in 0u16..3600u16,
978        ) {
979            let mut data = [0u8; 13];
980            data[0..2].copy_from_slice(&co2.to_le_bytes());
981            data[2..4].copy_from_slice(&temp_raw.to_le_bytes());
982            data[4..6].copy_from_slice(&pressure_raw.to_le_bytes());
983            data[6] = humidity;
984            data[7] = battery;
985            data[8] = status_byte;
986            data[9..11].copy_from_slice(&interval.to_le_bytes());
987            data[11..13].copy_from_slice(&age.to_le_bytes());
988
989            let result = CurrentReading::from_bytes(&data);
990            prop_assert!(result.is_ok());
991
992            let reading = result.unwrap();
993            prop_assert_eq!(reading.co2, co2);
994            prop_assert_eq!(reading.humidity, humidity);
995            prop_assert_eq!(reading.battery, battery);
996            prop_assert_eq!(reading.interval, interval);
997            prop_assert_eq!(reading.age, age);
998        }
999
1000        /// Valid 7-byte input should always parse successfully for Aranet2.
1001        #[test]
1002        fn parse_valid_aranet2_bytes(
1003            temp_raw in 0u16..2000u16,
1004            humidity in 0u8..100u8,
1005            battery in 0u8..100u8,
1006            status_byte in 0u8..4u8,
1007            interval in 60u16..3600u16,
1008        ) {
1009            let mut data = [0u8; 7];
1010            data[0..2].copy_from_slice(&temp_raw.to_le_bytes());
1011            data[2] = humidity;
1012            data[3] = battery;
1013            data[4] = status_byte;
1014            data[5..7].copy_from_slice(&interval.to_le_bytes());
1015
1016            let result = CurrentReading::from_bytes_aranet2(&data);
1017            prop_assert!(result.is_ok());
1018
1019            let reading = result.unwrap();
1020            prop_assert_eq!(reading.humidity, humidity);
1021            prop_assert_eq!(reading.battery, battery);
1022            prop_assert_eq!(reading.interval, interval);
1023        }
1024
1025        /// JSON serialization roundtrip should preserve all values.
1026        #[test]
1027        fn current_reading_json_roundtrip(
1028            co2 in 0u16..10000u16,
1029            temperature in -20.0f32..60.0f32,
1030            pressure in 800.0f32..1200.0f32,
1031            humidity in 0u8..100u8,
1032            battery in 0u8..100u8,
1033            interval in 60u16..3600u16,
1034            age in 0u16..3600u16,
1035        ) {
1036            let reading = CurrentReading {
1037                co2,
1038                temperature,
1039                pressure,
1040                humidity,
1041                battery,
1042                status: Status::Green,
1043                interval,
1044                age,
1045                captured_at: None,
1046                radon: None,
1047                radiation_rate: None,
1048                radiation_total: None,
1049                radon_avg_24h: None,
1050                radon_avg_7d: None,
1051                radon_avg_30d: None,
1052            };
1053
1054            let json = serde_json::to_string(&reading).unwrap();
1055            let parsed: CurrentReading = serde_json::from_str(&json).unwrap();
1056
1057            prop_assert_eq!(parsed.co2, reading.co2);
1058            prop_assert_eq!(parsed.humidity, reading.humidity);
1059            prop_assert_eq!(parsed.battery, reading.battery);
1060            prop_assert_eq!(parsed.interval, reading.interval);
1061            prop_assert_eq!(parsed.age, reading.age);
1062        }
1063    }
1064}