mtrack 0.12.0

A multitrack audio and MIDI player for live performances.
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
// Copyright (C) 2026 Michael Wilson <mike@mdwn.dev>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//

use super::common::*;
#[cfg(test)]
use crate::lighting::effects::*;
use crate::lighting::engine::EffectEngine;
use std::collections::HashMap;
use std::time::Duration;

#[test]
fn test_chase_pattern_linear_left_to_right() {
    let mut engine = EffectEngine::new();

    // Create 4 fixtures for testing
    for i in 1..=4 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), 1);
        channels.insert("red".to_string(), 2);
        channels.insert("green".to_string(), 3);
        channels.insert("blue".to_string(), 4);

        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            (i - 1) * 4 + 1,
            "RGB_Par".to_string(),
            channels,
            Some(20.0),
        );
        engine.register_fixture(fixture);
    }

    let chase_effect = create_effect_with_layering(
        "chase_linear_ltr".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(2.0), // 2 Hz for easy testing
            direction: ChaseDirection::LeftToRight,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
            "fixture_4".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // Test chase sequence: fixture_1 -> fixture_2 -> fixture_3 -> fixture_4
    // At t=0ms (start) - fixture_1 should be ON
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON

    // At t=125ms (1/4 cycle) - fixture_2 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap();
    let fixture2_cmd = commands.iter().find(|cmd| cmd.channel == 5).unwrap(); // fixture_2 dimmer
    assert_eq!(fixture2_cmd.value, 255); // Should be ON

    // At t=250ms (1/2 cycle) - fixture_3 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 125ms more = 250ms total
    let fixture3_cmd = commands.iter().find(|cmd| cmd.channel == 9).unwrap(); // fixture_3 dimmer
    assert_eq!(fixture3_cmd.value, 255); // Should be ON

    // At t=375ms (3/4 cycle) - fixture_4 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 125ms more = 375ms total
    let fixture4_cmd = commands.iter().find(|cmd| cmd.channel == 13).unwrap(); // fixture_4 dimmer
    assert_eq!(fixture4_cmd.value, 255); // Should be ON

    // At t=500ms (full cycle) - fixture_1 should be ON again
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 125ms more = 500ms total
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON again
}
#[test]
fn test_chase_pattern_linear_right_to_left() {
    let mut engine = EffectEngine::new();

    // Create 4 fixtures for testing
    for i in 1..=4 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), 1);
        channels.insert("red".to_string(), 2);
        channels.insert("green".to_string(), 3);
        channels.insert("blue".to_string(), 4);

        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            (i - 1) * 4 + 1,
            "RGB_Par".to_string(),
            channels,
            Some(20.0),
        );
        engine.register_fixture(fixture);
    }

    let chase_effect = create_effect_with_layering(
        "chase_linear_rtl".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(2.0), // 2 Hz for easy testing
            direction: ChaseDirection::RightToLeft,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
            "fixture_4".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // Test chase sequence: fixture_4 -> fixture_3 -> fixture_2 -> fixture_1
    // At t=0ms (start) - fixture_4 should be ON
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let fixture4_cmd = commands.iter().find(|cmd| cmd.channel == 13).unwrap(); // fixture_4 dimmer
    assert_eq!(fixture4_cmd.value, 255); // Should be ON

    // At t=125ms (1/4 cycle) - fixture_3 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap();
    let fixture3_cmd = commands.iter().find(|cmd| cmd.channel == 9).unwrap(); // fixture_3 dimmer
    assert_eq!(fixture3_cmd.value, 255); // Should be ON

    // At t=250ms (1/2 cycle) - fixture_2 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 125ms more = 250ms total
    let fixture2_cmd = commands.iter().find(|cmd| cmd.channel == 5).unwrap(); // fixture_2 dimmer
    assert_eq!(fixture2_cmd.value, 255); // Should be ON

    // At t=375ms (3/4 cycle) - fixture_1 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 125ms more = 375ms total
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON
}
#[test]
fn test_chase_pattern_snake() {
    let mut engine = EffectEngine::new();

    // Create 4 fixtures for testing
    for i in 1..=4 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), 1);
        channels.insert("red".to_string(), 2);
        channels.insert("green".to_string(), 3);
        channels.insert("blue".to_string(), 4);

        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            (i - 1) * 4 + 1,
            "RGB_Par".to_string(),
            channels,
            Some(20.0),
        );
        engine.register_fixture(fixture);
    }

    let chase_effect = create_effect_with_layering(
        "chase_snake".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Snake,
            speed: TempoAwareSpeed::Fixed(2.0), // 2 Hz for easy testing
            direction: ChaseDirection::LeftToRight,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
            "fixture_4".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // Test snake pattern: fixture_1 -> fixture_2 -> fixture_3 -> fixture_4 -> fixture_3 -> fixture_2 -> fixture_1
    // Snake pattern has 6 positions: [0,1,2,3,2,1] for 4 fixtures
    // Each position lasts 500ms/6 = 83.33ms

    // At t=0ms (start) - fixture_1 should be ON
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON

    // At t=125ms (1/6 cycle) - fixture_2 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap();
    let fixture2_cmd = commands.iter().find(|cmd| cmd.channel == 5).unwrap();
    assert_eq!(fixture2_cmd.value, 255); // Should be ON

    // At t=250ms (2/6 cycle) - fixture_3 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 125+125=250ms total
    let fixture3_cmd = commands.iter().find(|cmd| cmd.channel == 9).unwrap();
    assert_eq!(fixture3_cmd.value, 255); // Should be ON

    // At t=375ms (3/6 cycle) - fixture_4 should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 250+125=375ms total
    let fixture4_cmd = commands.iter().find(|cmd| cmd.channel == 13).unwrap();
    assert_eq!(fixture4_cmd.value, 255); // Should be ON

    // At t=500ms (4/6 cycle) - fixture_3 should be ON (snake back)
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 375+125=500ms total
    let fixture3_cmd = commands.iter().find(|cmd| cmd.channel == 9).unwrap();
    assert_eq!(fixture3_cmd.value, 255); // Should be ON

    // At t=625ms (5/6 cycle) - fixture_2 should be ON (snake back)
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 500+125=625ms total
    let fixture2_cmd = commands.iter().find(|cmd| cmd.channel == 5).unwrap();
    assert_eq!(fixture2_cmd.value, 255); // Should be ON

    // At t=750ms (6/6 cycle) - fixture_1 should be ON again
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 625+125=750ms total
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON again
}
#[test]
fn test_chase_pattern_random() {
    let mut engine = EffectEngine::new();

    // Create 4 fixtures for testing
    for i in 1..=4 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), 1);
        channels.insert("red".to_string(), 2);
        channels.insert("green".to_string(), 3);
        channels.insert("blue".to_string(), 4);

        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            (i - 1) * 4 + 1,
            "RGB_Par".to_string(),
            channels,
            Some(20.0),
        );
        engine.register_fixture(fixture);
    }

    let chase_effect = create_effect_with_layering(
        "chase_random".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Random,
            speed: TempoAwareSpeed::Fixed(2.0), // 2 Hz for easy testing
            direction: ChaseDirection::LeftToRight, // Direction doesn't matter for random
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
            "fixture_4".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // Test random pattern - should have some fixture ON at each time point
    // At t=0ms - some fixture should be ON
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let on_fixtures: Vec<_> = commands.iter().filter(|cmd| cmd.value == 255).collect();
    assert_eq!(on_fixtures.len(), 1); // Exactly one fixture should be ON

    // At t=125ms - some fixture should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap();
    let on_fixtures: Vec<_> = commands.iter().filter(|cmd| cmd.value == 255).collect();
    assert_eq!(on_fixtures.len(), 1); // Exactly one fixture should be ON

    // At t=250ms - some fixture should be ON
    let commands = engine.update(Duration::from_millis(125), None).unwrap(); // 125ms more = 250ms total
    let on_fixtures: Vec<_> = commands.iter().filter(|cmd| cmd.value == 255).collect();
    assert_eq!(on_fixtures.len(), 1); // Exactly one fixture should be ON
}
#[test]
fn test_chase_direction_vertical() {
    let mut engine = EffectEngine::new();

    // Create 4 fixtures for testing
    for i in 1..=4 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), 1);
        channels.insert("red".to_string(), 2);
        channels.insert("green".to_string(), 3);
        channels.insert("blue".to_string(), 4);

        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            (i - 1) * 4 + 1,
            "RGB_Par".to_string(),
            channels,
            Some(20.0),
        );
        engine.register_fixture(fixture);
    }

    // Test TopToBottom
    let chase_effect = create_effect_with_layering(
        "chase_ttb".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(2.0),
            direction: ChaseDirection::TopToBottom,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
            "fixture_4".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // TopToBottom should behave like LeftToRight (fixture_1 -> fixture_2 -> fixture_3 -> fixture_4)
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON
}
#[test]
fn test_chase_direction_circular() {
    let mut engine = EffectEngine::new();

    // Create 4 fixtures for testing
    for i in 1..=4 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), 1);
        channels.insert("red".to_string(), 2);
        channels.insert("green".to_string(), 3);
        channels.insert("blue".to_string(), 4);

        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            (i - 1) * 4 + 1,
            "RGB_Par".to_string(),
            channels,
            Some(20.0),
        );
        engine.register_fixture(fixture);
    }

    // Test Clockwise
    let chase_effect = create_effect_with_layering(
        "chase_cw".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(2.0),
            direction: ChaseDirection::Clockwise,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
            "fixture_4".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // Clockwise should behave like LeftToRight (fixture_1 -> fixture_2 -> fixture_3 -> fixture_4)
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON
}
#[test]
fn test_chase_speed_variations() {
    let mut engine = EffectEngine::new();

    // Create 3 fixtures for testing
    for i in 1..=3 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), 1);
        channels.insert("red".to_string(), 2);
        channels.insert("green".to_string(), 3);
        channels.insert("blue".to_string(), 4);

        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            (i - 1) * 4 + 1,
            "RGB_Par".to_string(),
            channels,
            Some(20.0),
        );
        engine.register_fixture(fixture);
    }

    // Test slow speed (0.5 Hz)
    let slow_chase = create_effect_with_layering(
        "chase_slow".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(0.5), // 0.5 Hz - 2 second cycle
            direction: ChaseDirection::LeftToRight,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(slow_chase).unwrap();

    // At t=0ms - fixture_1 should be ON
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should be ON

    // At t=600ms (1/3 cycle) - fixture_1 should still be ON
    let commands = engine.update(Duration::from_millis(600), None).unwrap();
    let fixture1_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture1_cmd.value, 255); // Should still be ON

    // At t=1200ms (2/3 cycle) - fixture_2 should be ON
    let commands = engine.update(Duration::from_millis(600), None).unwrap(); // 600ms more = 1200ms total
    let fixture2_cmd = commands.iter().find(|cmd| cmd.channel == 5).unwrap();
    assert_eq!(fixture2_cmd.value, 255); // Should be ON
}
#[test]
fn test_chase_single_fixture() {
    let mut engine = EffectEngine::new();

    // Create single fixture
    let mut channels = HashMap::new();
    channels.insert("dimmer".to_string(), 1);
    channels.insert("red".to_string(), 2);
    channels.insert("green".to_string(), 3);
    channels.insert("blue".to_string(), 4);

    let fixture = FixtureInfo::new(
        "single_fixture".to_string(),
        1,
        1,
        "RGB_Par".to_string(),
        channels,
        Some(20.0),
    );
    engine.register_fixture(fixture);

    let chase_effect = create_effect_with_layering(
        "chase_single".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(2.0),
            direction: ChaseDirection::LeftToRight,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec!["single_fixture".to_string()],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // With single fixture, it should always be ON
    let commands = engine.update(Duration::from_millis(0), None).unwrap();
    let fixture_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture_cmd.value, 255); // Should be ON

    // At any time, single fixture should be ON
    let commands = engine.update(Duration::from_millis(500), None).unwrap();
    let fixture_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    assert_eq!(fixture_cmd.value, 255); // Should be ON
}
#[test]
fn test_chase_rgb_only_fixtures() {
    let mut engine = EffectEngine::new();

    // Create RGB-only fixtures
    for i in 1..=3 {
        let mut channels = HashMap::new();
        channels.insert("red".to_string(), 1);
        channels.insert("green".to_string(), 2);
        channels.insert("blue".to_string(), 3);
        // No dimmer channel!

        let fixture = FixtureInfo::new(
            format!("rgb_fixture_{}", i),
            1,
            (i - 1) * 3 + 1,
            "RGB_Par".to_string(),
            channels,
            None,
        );
        engine.register_fixture(fixture);
    }

    let chase_effect = create_effect_with_layering(
        "chase_rgb".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(2.0),
            direction: ChaseDirection::LeftToRight,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "rgb_fixture_1".to_string(),
            "rgb_fixture_2".to_string(),
            "rgb_fixture_3".to_string(),
        ],
        EffectLayer::Background,
        BlendMode::Replace,
    );

    engine.start_effect(chase_effect).unwrap();

    // Test that RGB channels are used for chase (white chase)
    let commands = engine.update(Duration::from_millis(0), None).unwrap();

    // fixture_1 should have all RGB channels ON
    let red_cmd = commands.iter().find(|cmd| cmd.channel == 1).unwrap();
    let green_cmd = commands.iter().find(|cmd| cmd.channel == 2).unwrap();
    let blue_cmd = commands.iter().find(|cmd| cmd.channel == 3).unwrap();
    assert_eq!(red_cmd.value, 255);
    assert_eq!(green_cmd.value, 255);
    assert_eq!(blue_cmd.value, 255);

    // At t=167ms (1/3 cycle) - fixture_2 should be ON
    let commands = engine.update(Duration::from_millis(167), None).unwrap();
    let red_cmd = commands.iter().find(|cmd| cmd.channel == 4).unwrap(); // fixture_2 red
    let green_cmd = commands.iter().find(|cmd| cmd.channel == 5).unwrap(); // fixture_2 green
    let blue_cmd = commands.iter().find(|cmd| cmd.channel == 6).unwrap(); // fixture_2 blue
    assert_eq!(red_cmd.value, 255);
    assert_eq!(green_cmd.value, 255);
    assert_eq!(blue_cmd.value, 255);
}
#[test]
fn test_chase_effect_crossfade() {
    let mut engine = EffectEngine::new();

    // Create test fixtures
    for i in 1..=4 {
        let mut channels = HashMap::new();
        channels.insert("dimmer".to_string(), i);
        let fixture = FixtureInfo::new(
            format!("fixture_{}", i),
            1,
            i,
            "Dimmer".to_string(),
            channels,
            None,
        );
        engine.register_fixture(fixture);
    }

    // Create chase effect with crossfades
    let mut chase_effect = create_effect_with_timing(
        "chase_test".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Linear,
            speed: TempoAwareSpeed::Fixed(1.0), // 1 cycle per second
            direction: ChaseDirection::LeftToRight,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "fixture_1".to_string(),
            "fixture_2".to_string(),
            "fixture_3".to_string(),
            "fixture_4".to_string(),
        ],
        EffectLayer::Midground,
        BlendMode::Replace,
        Some(Duration::from_secs(1)), // fade_in: 1s
        Some(Duration::from_secs(1)), // fade_out: 1s
    );
    chase_effect.hold_time = Some(Duration::from_secs(2)); // 2s hold time

    engine.start_effect(chase_effect).unwrap();

    // Test fade in phase - chase should be dimmed
    let commands = engine.update(Duration::from_millis(500), None).unwrap();
    let active_fixture = commands.iter().find(|cmd| cmd.value > 0);
    assert!(active_fixture.is_some());
    if let Some(cmd) = active_fixture {
        assert!(cmd.value > 0 && cmd.value < 255); // Dimmed chase during fade in
    }

    // Test full intensity phase - chase should be at full brightness
    let commands = engine.update(Duration::from_secs(2), None).unwrap();
    let active_fixture = commands.iter().find(|cmd| cmd.value > 0);
    assert!(active_fixture.is_some());
    if let Some(cmd) = active_fixture {
        assert_eq!(cmd.value, 255); // Full brightness during full intensity
    }

    // Test fade out phase - chase should be dimmed (at 3.5s total: 0.5s into down_time)
    let commands = engine.update(Duration::from_millis(1000), None).unwrap(); // 2.5s + 1s = 3.5s
    let active_fixture = commands.iter().find(|cmd| cmd.value > 0);
    assert!(active_fixture.is_some());
    if let Some(cmd) = active_fixture {
        assert!(cmd.value > 0 && cmd.value < 255); // Dimmed chase during fade out
    }

    // Test effect end - should be no commands (at 4s total)
    let commands = engine.update(Duration::from_millis(500), None).unwrap(); // 3.5s + 0.5s = 4s
    assert!(commands.is_empty()); // Effect should be finished
}
#[test]
fn test_random_chase_pattern_visibility() {
    // Test to replicate the issue where random pattern chase doesn't show up
    let mut engine = EffectEngine::new();

    // Register 8 fixtures like in the user's setup
    for i in 1..=8 {
        let mut channels = HashMap::new();
        channels.insert("red".to_string(), 1);
        channels.insert("green".to_string(), 2);
        channels.insert("blue".to_string(), 3);
        let fixture = FixtureInfo::new(
            format!("Brick{}", i),
            1,
            (i - 1) * 4 + 1,
            "Astera-PixelBrick".to_string(),
            channels,
            Some(25.0),
        );
        engine.register_fixture(fixture);
    }

    // Create a random pattern chase effect on background layer
    let mut random_chase = EffectInstance::new(
        "random_chase".to_string(),
        EffectType::Chase {
            pattern: ChasePattern::Random,
            speed: TempoAwareSpeed::Fixed(3.0), // 3 cycles per second
            direction: ChaseDirection::LeftToRight,
            transition: CycleTransition::Snap,
            duration: Duration::from_secs(10),
        },
        vec![
            "Brick1".to_string(),
            "Brick2".to_string(),
            "Brick3".to_string(),
            "Brick4".to_string(),
            "Brick5".to_string(),
            "Brick6".to_string(),
            "Brick7".to_string(),
            "Brick8".to_string(),
        ],
        None,
        Some(Duration::from_secs(4)), // hold_time: 4 seconds
        None,
    );
    random_chase.layer = EffectLayer::Background;
    random_chase.blend_mode = BlendMode::Replace;

    engine.start_effect(random_chase).unwrap();

    // Update engine and check that we get DMX commands
    let mut total_commands = 0;
    let mut active_fixtures: std::collections::HashSet<usize> = std::collections::HashSet::new();

    // Check over multiple time points to see if pattern is advancing
    for _step in 0..20 {
        let cmds = engine.update(Duration::from_millis(50), None).unwrap();
        total_commands += cmds.len();

        // Track which fixtures have non-zero values (active)
        // For PixelBrick, red channel is at address, green at address+1, blue at address+2
        for cmd in cmds {
            if cmd.value > 0 {
                // Find which fixture this command belongs to
                for i in 1..=8 {
                    let expected_address = (i - 1) * 4 + 1;
                    // Check if this command is for any channel of this fixture
                    if cmd.universe == 1
                        && cmd.channel >= expected_address
                        && cmd.channel < expected_address + 4
                    {
                        active_fixtures.insert(i as usize);
                    }
                }
            }
        }
    }

    // Verify that we got some commands
    assert!(
        total_commands > 0,
        "Expected some DMX commands, got {}",
        total_commands
    );

    // Verify that multiple fixtures were activated (pattern should advance)
    assert!(active_fixtures.len() > 1,
                "Expected multiple fixtures to be active (pattern advancing), but only {} fixture(s) were active: {:?}", 
                active_fixtures.len(), active_fixtures);

    // Verify that the pattern order is not sequential (should be random)
    // The shuffle for 8 fixtures produces [6, 7, 0, 1, 2, 3, 4, 5]
    // So we should see Brick7, Brick8, Brick1, etc. - not just Brick1, Brick2, etc.
    let fixture_order: Vec<usize> = active_fixtures.iter().copied().collect();
    let is_sequential = fixture_order.windows(2).all(|w| w[1] == w[0] + 1);
    assert!(
        !is_sequential || fixture_order.len() < 3,
        "Pattern appears to be sequential (not random). Active fixtures: {:?}",
        fixture_order
    );
}