bacnet-objects 0.13.0

BACnet object model: traits, database, and standard object types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use super::super::*;
use bacnet_types::bitstring::LimitEnable;
use bacnet_types::enums::EventState;

// --- AnalogInput ---

#[test]
fn ai_read_present_value() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap(); // 62 = degrees-fahrenheit
    ai.set_present_value(72.5);
    let val = ai
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Real(72.5));
}

#[test]
fn ai_read_units() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let val = ai.read_property(PropertyIdentifier::UNITS, None).unwrap();
    assert_eq!(val, PropertyValue::Enumerated(62));
}

#[test]
fn ai_write_present_value_denied_when_in_service() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let result = ai.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(99.0),
        None,
    );
    assert!(result.is_err());
}

#[test]
fn ai_write_present_value_allowed_when_out_of_service() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::OUT_OF_SERVICE,
        None,
        PropertyValue::Boolean(true),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::PRESENT_VALUE,
        None,
        PropertyValue::Real(99.0),
        None,
    )
    .unwrap();
    let val = ai
        .read_property(PropertyIdentifier::PRESENT_VALUE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Real(99.0));
}

#[test]
fn ai_read_unknown_property() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let result = ai.read_property(PropertyIdentifier::PRIORITY_ARRAY, None);
    assert!(result.is_err());
}

#[test]
fn ai_read_event_state_default_normal() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let val = ai
        .read_property(PropertyIdentifier::EVENT_STATE, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Enumerated(EventState::NORMAL.to_raw()));
}

#[test]
fn ai_read_write_high_limit() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::HIGH_LIMIT,
        None,
        PropertyValue::Real(85.0),
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::HIGH_LIMIT, None)
            .unwrap(),
        PropertyValue::Real(85.0)
    );
}

#[test]
fn ai_read_write_low_limit() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::LOW_LIMIT,
        None,
        PropertyValue::Real(15.0),
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::LOW_LIMIT, None)
            .unwrap(),
        PropertyValue::Real(15.0)
    );
}

#[test]
fn ai_read_write_deadband() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.write_property(
        PropertyIdentifier::DEADBAND,
        None,
        PropertyValue::Real(2.5),
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::DEADBAND, None)
            .unwrap(),
        PropertyValue::Real(2.5)
    );
}

#[test]
fn ai_deadband_reject_negative() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let result = ai.write_property(
        PropertyIdentifier::DEADBAND,
        None,
        PropertyValue::Real(-1.0),
        None,
    );
    assert!(result.is_err());
}

#[test]
fn ai_read_write_limit_enable() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let enable_both = LimitEnable::all().to_bacnet();
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![enable_both],
        },
        None,
    )
    .unwrap();
    let val = ai
        .read_property(PropertyIdentifier::LIMIT_ENABLE, None)
        .unwrap();
    if let PropertyValue::BitString { data, .. } = val {
        assert_eq!(LimitEnable::from_bacnet(&data), LimitEnable::all());
    } else {
        panic!("Expected BitString");
    }
}

#[test]
fn ai_intrinsic_reporting_triggers_on_present_value_change() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    // Configure: high=80, low=20, deadband=2, both limits enabled
    ai.write_property(
        PropertyIdentifier::HIGH_LIMIT,
        None,
        PropertyValue::Real(80.0),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::LOW_LIMIT,
        None,
        PropertyValue::Real(20.0),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::DEADBAND,
        None,
        PropertyValue::Real(2.0),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![LimitEnable::all().to_bacnet()],
        },
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0xE0], // all transitions, MSB-first
        },
        None,
    )
    .unwrap();

    // Normal value — no transition
    ai.set_present_value(50.0);
    assert!(ai.evaluate_intrinsic_reporting().is_none());

    // Go above high limit
    ai.set_present_value(81.0);
    let proposal = ai.evaluate_intrinsic_reporting().unwrap();
    let outcome = crate::event::commit_test_proposal(&mut ai, proposal);
    let change = outcome.change;
    assert_eq!(change.from, EventState::NORMAL);
    assert_eq!(change.to, EventState::HIGH_LIMIT);

    // Verify event_state property reads correctly
    assert_eq!(
        ai.read_property(PropertyIdentifier::EVENT_STATE, None)
            .unwrap(),
        PropertyValue::Enumerated(EventState::HIGH_LIMIT.to_raw())
    );

    // Drop below deadband threshold → back to NORMAL
    ai.set_present_value(77.0);
    let change = ai.evaluate_intrinsic_reporting().unwrap().change;
    assert_eq!(change.to, EventState::NORMAL);
}

#[test]
fn ai_read_reliability_default() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let val = ai
        .read_property(PropertyIdentifier::RELIABILITY, None)
        .unwrap();
    assert_eq!(val, PropertyValue::Enumerated(0)); // NO_FAULT_DETECTED
}

#[test]
fn ai_description_read_write() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    // Default description is empty
    let val = ai
        .read_property(PropertyIdentifier::DESCRIPTION, None)
        .unwrap();
    assert_eq!(val, PropertyValue::CharacterString(String::new()));
    // Write a description
    ai.write_property(
        PropertyIdentifier::DESCRIPTION,
        None,
        PropertyValue::CharacterString("Zone temperature sensor".into()),
        None,
    )
    .unwrap();
    let val = ai
        .read_property(PropertyIdentifier::DESCRIPTION, None)
        .unwrap();
    assert_eq!(
        val,
        PropertyValue::CharacterString("Zone temperature sensor".into())
    );
}

#[test]
fn ai_set_description_convenience() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    ai.set_description("Supply air temperature");
    assert_eq!(
        ai.read_property(PropertyIdentifier::DESCRIPTION, None)
            .unwrap(),
        PropertyValue::CharacterString("Supply air temperature".into())
    );
}

#[test]
fn ai_description_in_property_list() {
    let ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    assert!(ai
        .property_list()
        .contains(&PropertyIdentifier::DESCRIPTION));
}

#[test]
fn ai_is_createable_matches_factory() {
    use crate::traits::BACnetObject;
    let ai = AnalogInputObject::new(1, "ai-1", 95).unwrap();
    assert!(ai.is_createable(), "AnalogInput is factory-constructable");
}

#[test]
fn ai_is_writable_property_mirrors_write_property() {
    use crate::traits::BACnetObject;
    let ai = AnalogInputObject::new(1, "ai-1", 95).unwrap();
    // Event properties accepted via write_generic_event_properties! / write_analog_event_properties! — the old
    // heuristic omitted these (false-negatives).
    assert!(ai.is_writable_property(PropertyIdentifier::LIMIT_ENABLE));
    assert!(ai.is_writable_property(PropertyIdentifier::NOTIFY_TYPE));
    assert!(ai.is_writable_property(PropertyIdentifier::TIME_DELAY));
    assert!(ai.is_writable_property(PropertyIdentifier::EVENT_ENABLE));
    assert!(ai.is_writable_property(PropertyIdentifier::NOTIFICATION_CLASS));
    // Common + RELIABILITY + COV_INCREMENT.
    assert!(ai.is_writable_property(PropertyIdentifier::OUT_OF_SERVICE));
    assert!(ai.is_writable_property(PropertyIdentifier::OBJECT_NAME));
    assert!(ai.is_writable_property(PropertyIdentifier::DESCRIPTION));
    assert!(ai.is_writable_property(PropertyIdentifier::RELIABILITY));
    assert!(ai.is_writable_property(PropertyIdentifier::COV_INCREMENT));
    // PRESENT_VALUE accepted when out-of-service.
    assert!(ai.is_writable_property(PropertyIdentifier::PRESENT_VALUE));
    // Read-only despite being an event property: Acked_Transitions is modified only by the
    // AcknowledgeAlarm service, which ORs the acknowledged bit in where a property write
    // would assign — so an assignable arm could both fabricate and erase acknowledgments,
    // and GetAlarmSummary / GetEventInformation read the field straight off the object.
    // Asserted on both halves because this test is the mirror check: the #222 macro split
    // silently turned this write into Ok(()) on all three analog types, and nothing here
    // caught it.
    let mut ai_mut = AnalogInputObject::new(2, "ai-2", 95).unwrap();
    assert!(!ai.is_writable_property(PropertyIdentifier::ACKED_TRANSITIONS));
    assert!(ai_mut
        .write_property(
            PropertyIdentifier::ACKED_TRANSITIONS,
            None,
            bacnet_types::primitives::PropertyValue::BitString {
                unused_bits: 5,
                data: vec![0x80],
            },
            None,
        )
        .is_err());

    // Universal read-only.
    assert!(!ai.is_writable_property(PropertyIdentifier::OBJECT_IDENTIFIER));
    assert!(!ai.is_writable_property(PropertyIdentifier::OBJECT_TYPE));
    assert!(!ai.is_writable_property(PropertyIdentifier::PROPERTY_LIST));
    assert!(!ai.is_writable_property(PropertyIdentifier::STATUS_FLAGS));
    // Not accepted by AI write_property.
    assert!(!ai.is_writable_property(PropertyIdentifier::PRIORITY_ARRAY));
    assert!(!ai.is_writable_property(PropertyIdentifier::RELINQUISH_DEFAULT));
}

// ──────────────────────────────────────────────────────────────────────────
// #255 — Notify_Type production validation and Event_Enable / Limit_Enable
// bit-string width validation on the shared write macros.
// ──────────────────────────────────────────────────────────────────────────

fn assert_property_error_code(result: Result<(), Error>, code: bacnet_types::enums::ErrorCode) {
    match result.unwrap_err() {
        Error::Protocol {
            class,
            code: actual,
        } => {
            assert_eq!(
                class,
                bacnet_types::enums::ErrorClass::PROPERTY.to_raw() as u32
            );
            assert_eq!(actual, code.to_raw() as u32);
        }
        other => panic!("expected protocol error, got {other:?}"),
    }
}

/// BACnetNotifyType is a closed {alarm(0), event(1), ack-notification(2)}
/// production (Clause 21). An out-of-production write is PROPERTY /
/// VALUE_OUT_OF_RANGE (Clause 15.9.1.3) and leaves the stored value untouched.
#[test]
fn ai_notify_type_rejects_out_of_production_values() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::NOTIFY_TYPE, None)
            .unwrap(),
        PropertyValue::Enumerated(0)
    );

    for out_of_production in [3u32, 99, u32::MAX] {
        assert_property_error_code(
            ai.write_property(
                PropertyIdentifier::NOTIFY_TYPE,
                None,
                PropertyValue::Enumerated(out_of_production),
                None,
            ),
            bacnet_types::enums::ErrorCode::VALUE_OUT_OF_RANGE,
        );
        assert_eq!(
            ai.read_property(PropertyIdentifier::NOTIFY_TYPE, None)
                .unwrap(),
            PropertyValue::Enumerated(0),
            "a refused Notify_Type write must leave the value untouched ({out_of_production})"
        );
    }
    for in_production in [0u32, 1, 2] {
        ai.write_property(
            PropertyIdentifier::NOTIFY_TYPE,
            None,
            PropertyValue::Enumerated(in_production),
            None,
        )
        .expect("named Notify_Type values must be accepted");
    }
}

/// BACnetEventTransitionBits is a 3-bit production (Clause 21); its canonical
/// encoding is one content octet with 5 unused bits. A write declaring any
/// other shape is PROPERTY / INVALID_DATA_ENCODING (Clause 15.9.1.3), not a
/// value to mask and normalize.
#[test]
fn ai_event_enable_rejects_noncanonical_bit_strings() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let canonical = ai
        .read_property(PropertyIdentifier::EVENT_ENABLE, None)
        .unwrap();

    for (unused_bits, data) in [
        (0u8, vec![0xFFu8]),     // 8-bit string where the production defines 3
        (5u8, vec![0xFF, 0xFF]), // two content octets
        (4u8, vec![0xF0u8]),     // half-octet string
        (5u8, vec![]),           // no content octet
    ] {
        assert_property_error_code(
            ai.write_property(
                PropertyIdentifier::EVENT_ENABLE,
                None,
                PropertyValue::BitString { unused_bits, data },
                None,
            ),
            bacnet_types::enums::ErrorCode::INVALID_DATA_ENCODING,
        );
        assert_eq!(
            ai.read_property(PropertyIdentifier::EVENT_ENABLE, None)
                .unwrap(),
            canonical,
            "a refused Event_Enable write must leave the value untouched"
        );
    }

    // The canonical full-width shape stays accepted.
    ai.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0xA0], // to-offnormal + to-normal, MSB-first
        },
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::EVENT_ENABLE, None)
            .unwrap(),
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0xA0],
        }
    );
}

/// BACnetLimitEnable is a 2-bit production (Clause 21): its canonical encoding
/// declares 6 unused bits, so even Event_Enable's valid 3-bit shape must be
/// refused here as PROPERTY / INVALID_DATA_ENCODING.
#[test]
fn ai_limit_enable_rejects_noncanonical_bit_strings() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    let canonical = ai
        .read_property(PropertyIdentifier::LIMIT_ENABLE, None)
        .unwrap();

    for (unused_bits, data) in [
        (0u8, vec![0xFFu8]),     // 8-bit string where the production defines 2
        (5u8, vec![0xE0u8]),     // Event_Enable's 3-bit shape must not pass
        (6u8, vec![0xC0, 0x00]), // extra content octet
        (6u8, vec![]),           // no content octet
    ] {
        assert_property_error_code(
            ai.write_property(
                PropertyIdentifier::LIMIT_ENABLE,
                None,
                PropertyValue::BitString { unused_bits, data },
                None,
            ),
            bacnet_types::enums::ErrorCode::INVALID_DATA_ENCODING,
        );
        assert_eq!(
            ai.read_property(PropertyIdentifier::LIMIT_ENABLE, None)
                .unwrap(),
            canonical,
            "a refused Limit_Enable write must leave the value untouched"
        );
    }

    // The canonical 2-bit shape stays accepted and lands MSB-first.
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![0xC0], // both limits enabled
        },
        None,
    )
    .unwrap();
    assert_eq!(
        ai.read_property(PropertyIdentifier::LIMIT_ENABLE, None)
            .unwrap(),
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![LimitEnable::all().to_bacnet()],
        }
    );
}

// ──────────────────────────────────────────────────────────────────────────
// #225 — Time_Delay_Normal on the OUT_OF_RANGE family (AI/AO/AV).
// ──────────────────────────────────────────────────────────────────────────

fn assert_tdn_protocol_error(result: Result<(), Error>, code: bacnet_types::enums::ErrorCode) {
    match result.unwrap_err() {
        Error::Protocol {
            class,
            code: actual,
        } => {
            assert_eq!(
                class,
                bacnet_types::enums::ErrorClass::PROPERTY.to_raw() as u32
            );
            assert_eq!(actual, code.to_raw() as u32);
        }
        other => panic!("expected protocol error, got {other:?}"),
    }
}

/// The property round-trips on all three analog types and is advertised in
/// both `property_list` and `is_writable_property` (the Clause 12 tables make
/// it O-coded, so writability mirrors Time_Delay — permitted, and the only
/// way the Clause 13.3 delay asymmetry is commissionable).
#[test]
fn analog_time_delay_normal_round_trips_and_matches_pics() {
    for (mut object, label) in [
        (
            Box::new(AnalogInputObject::new(1, "AI-1", 62).unwrap()) as Box<dyn BACnetObject>,
            "AI",
        ),
        (
            Box::new(AnalogOutputObject::new(1, "AO-1", 62).unwrap()) as Box<dyn BACnetObject>,
            "AO",
        ),
        (
            Box::new(AnalogValueObject::new(1, "AV-1", 62).unwrap()) as Box<dyn BACnetObject>,
            "AV",
        ),
    ] {
        object
            .write_property(
                PropertyIdentifier::TIME_DELAY_NORMAL,
                None,
                PropertyValue::Unsigned(9),
                None,
            )
            .unwrap_or_else(|e| panic!("{label}: Time_Delay_Normal write rejected: {e:?}"));
        assert_eq!(
            object
                .read_property(PropertyIdentifier::TIME_DELAY_NORMAL, None)
                .unwrap(),
            PropertyValue::Unsigned(9),
            "{label}: Time_Delay_Normal must read back what was written"
        );
        assert!(
            object
                .property_list()
                .contains(&PropertyIdentifier::TIME_DELAY_NORMAL),
            "{label}: Time_Delay_Normal must be advertised in Property_List"
        );
        assert!(
            object.is_writable_property(PropertyIdentifier::TIME_DELAY_NORMAL),
            "{label}: Time_Delay_Normal must be advertised writable"
        );

        // Validation mirrors Time_Delay: Unsigned within the u32 span, with
        // the Clause 15.9.1.3 pairings on refusal and the value untouched.
        assert_tdn_protocol_error(
            object.write_property(
                PropertyIdentifier::TIME_DELAY_NORMAL,
                None,
                PropertyValue::Enumerated(9),
                None,
            ),
            bacnet_types::enums::ErrorCode::INVALID_DATA_TYPE,
        );
        assert_tdn_protocol_error(
            object.write_property(
                PropertyIdentifier::TIME_DELAY_NORMAL,
                None,
                PropertyValue::Unsigned(u32::MAX as u64 + 1),
                None,
            ),
            bacnet_types::enums::ErrorCode::VALUE_OUT_OF_RANGE,
        );
        assert_eq!(
            object
                .read_property(PropertyIdentifier::TIME_DELAY_NORMAL, None)
                .unwrap(),
            PropertyValue::Unsigned(9),
            "{label}: refused Time_Delay_Normal writes must leave the value untouched"
        );
    }
}

/// Clause 13.3 supplies pTimeDelay when pTimeDelayNormal is absent —
/// an object with no Time_Delay_Normal
/// ever written reads back the effective (fallback) delay.
#[test]
fn analog_time_delay_normal_defaults_to_time_delay_when_unwritten() {
    for (mut object, label) in [
        (
            Box::new(AnalogInputObject::new(1, "AI-1", 62).unwrap()) as Box<dyn BACnetObject>,
            "AI",
        ),
        (
            Box::new(AnalogOutputObject::new(1, "AO-1", 62).unwrap()) as Box<dyn BACnetObject>,
            "AO",
        ),
        (
            Box::new(AnalogValueObject::new(1, "AV-1", 62).unwrap()) as Box<dyn BACnetObject>,
            "AV",
        ),
    ] {
        assert_eq!(
            object
                .read_property(PropertyIdentifier::TIME_DELAY_NORMAL, None)
                .unwrap(),
            object
                .read_property(PropertyIdentifier::TIME_DELAY, None)
                .unwrap(),
            "{label}: unwritten Time_Delay_Normal reads back Time_Delay"
        );
        object
            .write_property(
                PropertyIdentifier::TIME_DELAY,
                None,
                PropertyValue::Unsigned(11),
                None,
            )
            .unwrap();
        assert_eq!(
            object
                .read_property(PropertyIdentifier::TIME_DELAY_NORMAL, None)
                .unwrap(),
            PropertyValue::Unsigned(11),
            "{label}: the fallback tracks Time_Delay"
        );
    }
}

/// Clause 13.3.6's asymmetry at object level, commissioned entirely over
/// `write_property`: the HIGH_LIMIT indication (a) waits Time_Delay, the
/// NORMAL indication (e) waits Time_Delay_Normal.
#[test]
fn ai_time_delay_normal_gates_the_return_to_normal() {
    let mut ai = AnalogInputObject::new(1, "AI-1", 62).unwrap();
    for (property, value) in [
        (PropertyIdentifier::HIGH_LIMIT, 80.0),
        (PropertyIdentifier::LOW_LIMIT, 20.0),
        (PropertyIdentifier::DEADBAND, 2.0),
    ] {
        ai.write_property(property, None, PropertyValue::Real(value), None)
            .unwrap();
    }
    ai.write_property(
        PropertyIdentifier::LIMIT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 6,
            data: vec![LimitEnable::all().to_bacnet()],
        },
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::TIME_DELAY,
        None,
        PropertyValue::Unsigned(2),
        None,
    )
    .unwrap();
    ai.write_property(
        PropertyIdentifier::TIME_DELAY_NORMAL,
        None,
        PropertyValue::Unsigned(4),
        None,
    )
    .unwrap();

    ai.set_present_value(81.0);
    assert_eq!(ai.evaluate_intrinsic_reporting(), None);
    assert_eq!(ai.tick_intrinsic_reporting(), None);
    let proposal = ai.tick_intrinsic_reporting().unwrap();
    let outcome = crate::event::commit_test_proposal(&mut ai, proposal);
    assert_eq!(
        outcome.change.to,
        EventState::HIGH_LIMIT,
        "condition (a): HIGH_LIMIT after Time_Delay = 2"
    );

    ai.set_present_value(50.0); // back inside (high_limit - deadband) = 78.0
    assert_eq!(
        ai.evaluate_intrinsic_reporting(),
        None,
        "condition (e): return to NORMAL only seeds the countdown"
    );
    for _ in 0..3 {
        assert_eq!(
            ai.tick_intrinsic_reporting(),
            None,
            "Time_Delay_Normal = 4 must hold the NORMAL state off"
        );
    }
    let outcome = ai.tick_intrinsic_reporting().unwrap();
    assert_eq!(outcome.change.from, EventState::HIGH_LIMIT);
    assert_eq!(outcome.change.to, EventState::NORMAL);
}