clasp-core 4.5.0

Core types and encoding for CLASP protocol
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
//! Codec tests for Clasp core
//! Tests both binary encoding (default) and backward compatibility with MessagePack

use clasp_core::{
    codec, HelloMessage, Message, PublishMessage, SetMessage, SignalType, SubscribeMessage, Ttl,
    Value, WelcomeMessage,
};

#[test]
fn test_encode_decode_hello() {
    let msg = Message::Hello(HelloMessage {
        version: 1,
        name: "Test Client".to_string(),
        features: vec!["param".to_string(), "event".to_string()],
        capabilities: None,
        token: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Hello(hello) => {
            assert_eq!(hello.version, 1);
            assert_eq!(hello.name, "Test Client");
            assert_eq!(hello.features.len(), 2);
        }
        _ => panic!("Expected Hello message"),
    }
}

#[test]
fn test_encode_decode_welcome() {
    let msg = Message::Welcome(WelcomeMessage {
        version: 1,
        session: "sess-123".to_string(),
        name: "Test Server".to_string(),
        features: vec!["param".to_string()],
        time: 1234567890,
        token: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Welcome(welcome) => {
            assert_eq!(welcome.session, "sess-123");
            assert_eq!(welcome.time, 1234567890);
        }
        _ => panic!("Expected Welcome message"),
    }
}

#[test]
fn test_encode_decode_set() {
    let msg = Message::Set(SetMessage {
        address: "/test/path".to_string(),
        value: Value::Float(1.25),
        revision: Some(1),
        lock: false,
        unlock: false,
        ttl: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert_eq!(set.address, "/test/path");
            match set.value {
                Value::Float(f) => assert!((f - 1.25).abs() < 0.001),
                _ => panic!("Expected Float value"),
            }
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_encode_decode_publish() {
    let msg = Message::Publish(PublishMessage {
        address: "/test/event".to_string(),
        signal: Some(SignalType::Event),
        value: None,
        payload: Some(Value::String("hello".to_string())),
        samples: None,
        rate: None,
        id: None,
        phase: None,
        timestamp: Some(123456),
        timeline: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Publish(pub_msg) => {
            assert_eq!(pub_msg.address, "/test/event");
            assert_eq!(pub_msg.signal, Some(SignalType::Event));
        }
        _ => panic!("Expected Publish message"),
    }
}

#[test]
fn test_encode_decode_subscribe() {
    let msg = Message::Subscribe(SubscribeMessage {
        id: 42,
        pattern: "/test/*".to_string(),
        types: vec![SignalType::Param, SignalType::Event],
        options: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Subscribe(sub) => {
            assert_eq!(sub.id, 42);
            assert_eq!(sub.pattern, "/test/*");
        }
        _ => panic!("Expected Subscribe message"),
    }
}

#[test]
fn test_value_types() {
    // Test all value types roundtrip
    // Note: Bytes may deserialize as Array due to MessagePack + serde(untagged) ambiguity
    // so we skip testing Bytes separately here
    let values = vec![
        Value::Null,
        Value::Bool(true),
        Value::Bool(false),
        Value::Int(42),
        Value::Int(-1000),
        Value::Float(1.2345),
        Value::String("hello world".to_string()),
        Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)]),
        Value::Map(
            vec![
                ("key1".to_string(), Value::Int(1)),
                ("key2".to_string(), Value::String("value".to_string())),
            ]
            .into_iter()
            .collect(),
        ),
    ];

    for value in values {
        let msg = Message::Set(SetMessage {
            address: "/test".to_string(),
            value: value.clone(),
            revision: None,
            lock: false,
            unlock: false,
            ttl: None,
        });

        let encoded = codec::encode(&msg).expect("encode failed");
        let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

        match decoded {
            Message::Set(set) => {
                // Note: Float comparison needs epsilon
                match (&value, &set.value) {
                    (Value::Float(a), Value::Float(b)) => assert!((a - b).abs() < 0.0001),
                    _ => assert_eq!(value, set.value),
                }
            }
            _ => panic!("Expected Set message"),
        }
    }
}

// ============================================================================
// Binary Encoding Tests
// ============================================================================

#[test]
fn test_v3_set_message_size() {
    // Binary encoding should produce smaller messages than MessagePack
    let msg = Message::Set(SetMessage {
        address: "/lights/living/brightness".to_string(),
        value: Value::Float(0.75),
        revision: None,
        lock: false,
        unlock: false,
        ttl: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");

    // Binary SET format: type(1) + flags(1) + addr_len(2) + addr(25) + value(9) = 38 bytes
    // v2 MessagePack: ~69 bytes due to named keys
    // Target: < 50 bytes for typical SET message
    assert!(
        encoded.len() < 50,
        "Binary SET message should be < 50 bytes, got {} bytes",
        encoded.len()
    );
}

#[test]
fn test_v3_set_message_with_revision() {
    let msg = Message::Set(SetMessage {
        address: "/test".to_string(),
        value: Value::Float(1.0),
        revision: Some(42),
        lock: false,
        unlock: false,
        ttl: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert_eq!(set.revision, Some(42));
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_v3_set_message_with_lock() {
    let msg = Message::Set(SetMessage {
        address: "/test".to_string(),
        value: Value::Bool(true),
        revision: None,
        lock: true,
        unlock: false,
        ttl: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert!(set.lock);
            assert!(!set.unlock);
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_v3_set_message_string_value() {
    let msg = Message::Set(SetMessage {
        address: "/label".to_string(),
        value: Value::String("Hello World".to_string()),
        revision: None,
        lock: false,
        unlock: false,
        ttl: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert_eq!(set.value, Value::String("Hello World".to_string()));
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_v3_encoding_starts_with_message_type() {
    // Binary format: payload first byte should be message type code
    // Note: encode() returns a frame, payload starts after header (magic + flags + len = 4 bytes)
    let set_msg = Message::Set(SetMessage {
        address: "/test".to_string(),
        value: Value::Float(1.0),
        revision: None,
        lock: false,
        unlock: false,
        ttl: None,
    });

    let encoded = codec::encode(&set_msg).expect("encode failed");
    // Frame header: magic (0x53) + flags (1) + length (2) = 4 bytes
    // Payload starts at offset 4
    assert_eq!(encoded[0], 0x53, "Frame magic byte should be 0x53");
    assert_eq!(encoded[4], 0x21, "SET payload should start with 0x21");

    let hello_msg = Message::Hello(HelloMessage {
        version: 1,
        name: "Test".to_string(),
        features: vec![],
        capabilities: None,
        token: None,
    });

    let encoded = codec::encode(&hello_msg).expect("encode failed");
    assert_eq!(encoded[0], 0x53, "Frame magic byte should be 0x53");
    assert_eq!(encoded[4], 0x01, "HELLO payload should start with 0x01");
}

#[test]
fn test_v3_benchmark_set_encoding() {
    // Verify encoding is reasonably fast (note: debug builds are slower)
    use std::time::Instant;

    let msg = Message::Set(SetMessage {
        address: "/lights/living/brightness".to_string(),
        value: Value::Float(0.75),
        revision: Some(1),
        lock: false,
        unlock: false,
        ttl: None,
    });

    let iterations = 100_000;
    let start = Instant::now();

    for _ in 0..iterations {
        let _ = codec::encode(&msg).expect("encode failed");
    }

    let elapsed = start.elapsed();
    let per_msg_ns = elapsed.as_nanos() / iterations as u128;

    // Target: < 2000ns per message (0.5M msg/s) in debug builds
    // Release builds should achieve < 200ns (5M+ msg/s)
    assert!(
        per_msg_ns < 2000,
        "Binary SET encoding should be < 2000ns (debug), got {}ns",
        per_msg_ns
    );

    let msgs_per_sec = 1_000_000_000 / per_msg_ns;
    println!(
        "Binary SET encoding: {}ns/msg = {:.2} million msg/s",
        per_msg_ns,
        msgs_per_sec as f64 / 1_000_000.0
    );

    // Decode benchmark
    let encoded = codec::encode(&msg).expect("encode failed");
    let start = Instant::now();

    for _ in 0..iterations {
        let _ = codec::decode(&encoded).expect("decode failed");
    }

    let elapsed = start.elapsed();
    let per_msg_ns = elapsed.as_nanos() / iterations as u128;
    let msgs_per_sec = 1_000_000_000 / per_msg_ns;
    println!(
        "Binary SET decoding: {}ns/msg = {:.2} million msg/s",
        per_msg_ns,
        msgs_per_sec as f64 / 1_000_000.0
    );
}

// ============================================================================
// Gesture Signal Type Tests
// ============================================================================

use clasp_core::GesturePhase;

#[test]
fn test_encode_decode_gesture_start() {
    let msg = Message::Publish(PublishMessage {
        address: "/input/touch".to_string(),
        signal: Some(SignalType::Gesture),
        value: None,
        payload: Some(Value::Map(
            vec![
                ("x".to_string(), Value::Float(0.5)),
                ("y".to_string(), Value::Float(0.3)),
                ("pressure".to_string(), Value::Float(0.8)),
            ]
            .into_iter()
            .collect(),
        )),
        samples: None,
        rate: None,
        id: Some(1),
        phase: Some(GesturePhase::Start),
        timestamp: Some(1704067200),
        timeline: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Publish(pub_msg) => {
            assert_eq!(pub_msg.address, "/input/touch");
            assert_eq!(pub_msg.signal, Some(SignalType::Gesture));
            assert_eq!(pub_msg.id, Some(1));
            assert_eq!(pub_msg.phase, Some(GesturePhase::Start));
            assert!(pub_msg.timestamp.is_some());
        }
        _ => panic!("Expected Publish message"),
    }
}

#[test]
fn test_encode_decode_gesture_move() {
    let msg = Message::Publish(PublishMessage {
        address: "/input/touch".to_string(),
        signal: Some(SignalType::Gesture),
        value: None,
        payload: Some(Value::Map(
            vec![
                ("x".to_string(), Value::Float(0.6)),
                ("y".to_string(), Value::Float(0.4)),
            ]
            .into_iter()
            .collect(),
        )),
        samples: None,
        rate: None,
        id: Some(1),
        phase: Some(GesturePhase::Move),
        timestamp: Some(1704067201),
        timeline: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Publish(pub_msg) => {
            assert_eq!(pub_msg.signal, Some(SignalType::Gesture));
            assert_eq!(pub_msg.id, Some(1));
            assert_eq!(pub_msg.phase, Some(GesturePhase::Move));
        }
        _ => panic!("Expected Publish message"),
    }
}

#[test]
fn test_encode_decode_gesture_end() {
    let msg = Message::Publish(PublishMessage {
        address: "/input/touch".to_string(),
        signal: Some(SignalType::Gesture),
        value: None,
        payload: Some(Value::Map(
            vec![
                ("x".to_string(), Value::Float(0.7)),
                ("y".to_string(), Value::Float(0.5)),
            ]
            .into_iter()
            .collect(),
        )),
        samples: None,
        rate: None,
        id: Some(1),
        phase: Some(GesturePhase::End),
        timestamp: Some(1704067202),
        timeline: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Publish(pub_msg) => {
            assert_eq!(pub_msg.id, Some(1));
            assert_eq!(pub_msg.phase, Some(GesturePhase::End));
        }
        _ => panic!("Expected Publish message"),
    }
}

#[test]
fn test_encode_decode_gesture_cancel() {
    let msg = Message::Publish(PublishMessage {
        address: "/input/touch".to_string(),
        signal: Some(SignalType::Gesture),
        value: None,
        payload: None,
        samples: None,
        rate: None,
        id: Some(1),
        phase: Some(GesturePhase::Cancel),
        timestamp: Some(1704067203),
        timeline: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Publish(pub_msg) => {
            assert_eq!(pub_msg.id, Some(1));
            assert_eq!(pub_msg.phase, Some(GesturePhase::Cancel));
        }
        _ => panic!("Expected Publish message"),
    }
}

#[test]
fn test_gesture_all_phases_roundtrip() {
    // Test that all gesture phases encode/decode correctly
    let phases = [
        GesturePhase::Start,
        GesturePhase::Move,
        GesturePhase::End,
        GesturePhase::Cancel,
    ];

    for (i, phase) in phases.iter().enumerate() {
        let msg = Message::Publish(PublishMessage {
            address: "/gesture/test".to_string(),
            signal: Some(SignalType::Gesture),
            value: None,
            payload: Some(Value::Float(i as f64)),
            samples: None,
            rate: None,
            id: Some(42),
            phase: Some(*phase),
            timestamp: Some(1000 + i as u64),
            timeline: None,
        });

        let encoded = codec::encode(&msg).expect("encode failed");
        let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

        match decoded {
            Message::Publish(pub_msg) => {
                assert_eq!(
                    pub_msg.phase,
                    Some(*phase),
                    "Phase mismatch for {:?}",
                    phase
                );
                assert_eq!(pub_msg.id, Some(42));
            }
            _ => panic!("Expected Publish message"),
        }
    }
}

#[test]
fn test_gesture_different_ids() {
    // Test that gesture IDs are preserved correctly
    for gesture_id in [0u32, 1, 100, 1000, u32::MAX] {
        let msg = Message::Publish(PublishMessage {
            address: "/input/multitouch".to_string(),
            signal: Some(SignalType::Gesture),
            value: None,
            payload: Some(Value::Null),
            samples: None,
            rate: None,
            id: Some(gesture_id),
            phase: Some(GesturePhase::Start),
            timestamp: None,
            timeline: None,
        });

        let encoded = codec::encode(&msg).expect("encode failed");
        let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

        match decoded {
            Message::Publish(pub_msg) => {
                assert_eq!(
                    pub_msg.id,
                    Some(gesture_id),
                    "ID mismatch for {}",
                    gesture_id
                );
            }
            _ => panic!("Expected Publish message"),
        }
    }
}

// ============================================================================
// Timeline Signal Type Tests
// ============================================================================

use clasp_core::{EasingType, TimelineData, TimelineKeyframe};

#[test]
fn test_timeline_data_creation() {
    let timeline = TimelineData::new(vec![
        TimelineKeyframe {
            time: 0,
            value: Value::Float(0.0),
            easing: EasingType::Linear,
            bezier: None,
        },
        TimelineKeyframe {
            time: 1_000_000,
            value: Value::Float(1.0),
            easing: EasingType::EaseOut,
            bezier: None,
        },
    ]);

    assert_eq!(timeline.keyframes.len(), 2);
    assert_eq!(timeline.duration(), 1_000_000);
    assert!(!timeline.loop_);
}

#[test]
fn test_timeline_with_loop() {
    let timeline = TimelineData::new(vec![
        TimelineKeyframe {
            time: 0,
            value: Value::Float(0.0),
            easing: EasingType::Linear,
            bezier: None,
        },
        TimelineKeyframe {
            time: 2_000_000,
            value: Value::Float(1.0),
            easing: EasingType::Step,
            bezier: None,
        },
    ])
    .with_loop(true);

    assert!(timeline.loop_);
}

#[test]
fn test_timeline_with_start_time() {
    let timeline = TimelineData::new(vec![TimelineKeyframe {
        time: 0,
        value: Value::Int(100),
        easing: EasingType::EaseInOut,
        bezier: None,
    }])
    .with_start_time(1_704_067_200_000_000);

    assert_eq!(timeline.start_time, Some(1_704_067_200_000_000));
}

#[test]
fn test_encode_decode_timeline_publish() {
    let timeline = TimelineData::new(vec![
        TimelineKeyframe {
            time: 0,
            value: Value::Float(0.0),
            easing: EasingType::Linear,
            bezier: None,
        },
        TimelineKeyframe {
            time: 500_000,
            value: Value::Float(0.5),
            easing: EasingType::EaseIn,
            bezier: None,
        },
        TimelineKeyframe {
            time: 1_000_000,
            value: Value::Float(1.0),
            easing: EasingType::EaseOut,
            bezier: None,
        },
    ])
    .with_loop(true)
    .with_start_time(1_704_067_200_000_000);

    let msg = Message::Publish(PublishMessage {
        address: "/lights/dimmer".to_string(),
        signal: Some(SignalType::Timeline),
        value: None,
        payload: None,
        samples: None,
        rate: None,
        id: None,
        phase: None,
        timestamp: Some(1704067200),
        timeline: Some(timeline.clone()),
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Publish(pub_msg) => {
            assert_eq!(pub_msg.address, "/lights/dimmer");
            assert_eq!(pub_msg.signal, Some(SignalType::Timeline));
            // Note: timeline data is encoded in payload via MessagePack,
            // so we check timestamp is preserved
            assert_eq!(pub_msg.timestamp, Some(1704067200));
        }
        _ => panic!("Expected Publish message"),
    }
}

#[test]
fn test_all_easing_types() {
    let easings = [
        EasingType::Linear,
        EasingType::EaseIn,
        EasingType::EaseOut,
        EasingType::EaseInOut,
        EasingType::Step,
        EasingType::CubicBezier,
    ];

    for easing in easings {
        let kf = TimelineKeyframe {
            time: 0,
            value: Value::Float(0.0),
            easing,
            bezier: if easing == EasingType::CubicBezier {
                Some([0.42, 0.0, 0.58, 1.0])
            } else {
                None
            },
        };

        // Verify the keyframe can be created with each easing type
        assert_eq!(kf.easing, easing);
    }
}

#[test]
fn test_timeline_cubic_bezier() {
    let timeline = TimelineData::new(vec![
        TimelineKeyframe {
            time: 0,
            value: Value::Float(0.0),
            easing: EasingType::CubicBezier,
            bezier: Some([0.42, 0.0, 0.58, 1.0]), // "ease" curve
        },
        TimelineKeyframe {
            time: 1_000_000,
            value: Value::Float(1.0),
            easing: EasingType::Linear,
            bezier: None,
        },
    ]);

    assert_eq!(timeline.keyframes[0].bezier, Some([0.42, 0.0, 0.58, 1.0]));
}

// ============================================================================
// Per-Message TTL Tests
// ============================================================================

#[test]
fn test_set_with_sliding_ttl() {
    let msg = Message::Set(SetMessage {
        address: "/test/ttl".to_string(),
        value: Value::Float(1.0),
        revision: None,
        lock: false,
        unlock: false,
        ttl: Some(Ttl::Sliding(60)),
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert_eq!(set.address, "/test/ttl");
            assert_eq!(set.ttl, Some(Ttl::Sliding(60)));
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_set_with_absolute_ttl() {
    let msg = Message::Set(SetMessage {
        address: "/test/ttl".to_string(),
        value: Value::Int(42),
        revision: Some(5),
        lock: false,
        unlock: false,
        ttl: Some(Ttl::Absolute(300)),
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert_eq!(set.revision, Some(5));
            assert_eq!(set.ttl, Some(Ttl::Absolute(300)));
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_set_with_never_ttl() {
    let msg = Message::Set(SetMessage {
        address: "/test/ttl".to_string(),
        value: Value::Bool(true),
        revision: None,
        lock: false,
        unlock: false,
        ttl: Some(Ttl::Never),
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert_eq!(set.ttl, Some(Ttl::Never));
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_set_without_ttl_backward_compat() {
    let msg = Message::Set(SetMessage {
        address: "/test/no-ttl".to_string(),
        value: Value::Float(0.5),
        revision: None,
        lock: false,
        unlock: false,
        ttl: None,
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert_eq!(set.address, "/test/no-ttl");
            assert_eq!(set.ttl, None);
        }
        _ => panic!("Expected Set message"),
    }
}

#[test]
fn test_set_with_ttl_and_lock() {
    let msg = Message::Set(SetMessage {
        address: "/test/locked-ttl".to_string(),
        value: Value::String("hello".to_string()),
        revision: Some(10),
        lock: true,
        unlock: false,
        ttl: Some(Ttl::Sliding(3600)),
    });

    let encoded = codec::encode(&msg).expect("encode failed");
    let (decoded, _frame) = codec::decode(&encoded).expect("decode failed");

    match decoded {
        Message::Set(set) => {
            assert!(set.lock);
            assert_eq!(set.revision, Some(10));
            assert_eq!(set.ttl, Some(Ttl::Sliding(3600)));
            assert_eq!(set.value, Value::String("hello".to_string()));
        }
        _ => panic!("Expected Set message"),
    }
}