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
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
// 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 notify_debouncer_mini::{new_debouncer, DebouncedEventKind};
use parking_lot::Mutex;
use serde_json::json;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
use tracing::{error, info, warn};

use crate::lighting::system::LightingSystem;
use crate::lighting::timeline::LightingTimeline;
use crate::lighting::validation::validate_light_shows;
use crate::lighting::EffectEngine;

/// Handle to a running file watcher, shuts down when dropped.
pub struct WatcherHandle {
    _watcher: notify_debouncer_mini::Debouncer<notify::RecommendedWatcher>,
}

/// Starts watching the given light show files for changes.
///
/// On file change (debounced 300ms):
/// 1. Re-parses the .light file
/// 2. Builds a new LightingTimeline
/// 3. Swaps it into current_song_timeline
/// 4. Reconstructs state at current_song_time
/// 5. Sends a reload notification over WebSocket
pub fn start_watching(
    file_paths: Vec<PathBuf>,
    effect_engine: Arc<Mutex<EffectEngine>>,
    current_song_timeline: Arc<Mutex<Option<LightingTimeline>>>,
    current_song_time: Arc<AtomicU64>,
    lighting_system: Option<Arc<Mutex<LightingSystem>>>,
    lighting_config: Option<crate::config::Lighting>,
    broadcast_tx: broadcast::Sender<String>,
) -> Result<WatcherHandle, Box<dyn std::error::Error>> {
    // Canonicalize paths so they match what the OS reports in events.
    let paths: Vec<PathBuf> = file_paths
        .iter()
        .map(|p| p.canonicalize().unwrap_or_else(|_| p.clone()))
        .collect();

    let (tx, rx) = std::sync::mpsc::channel();
    let mut debouncer = new_debouncer(Duration::from_millis(300), tx)?;

    // Watch each file path
    for path in &paths {
        if let Some(parent) = path.parent() {
            debouncer
                .watcher()
                .watch(parent, notify::RecursiveMode::NonRecursive)?;
        }
    }

    // Spawn a thread to handle file change events
    let effect_engine = effect_engine.clone();
    let current_song_timeline = current_song_timeline.clone();
    let current_song_time = current_song_time.clone();

    std::thread::spawn(move || {
        for events in rx {
            match events {
                Ok(events) => {
                    // Check if any of our watched files changed
                    let relevant = events.iter().any(|event| {
                        if event.kind != DebouncedEventKind::Any {
                            return false;
                        }
                        let event_path = event
                            .path
                            .canonicalize()
                            .unwrap_or_else(|_| event.path.clone());
                        paths.contains(&event_path)
                    });
                    if !relevant {
                        continue;
                    }

                    info!("Light show file changed, reloading...");

                    match reload_timeline(
                        &paths,
                        &effect_engine,
                        &current_song_timeline,
                        &current_song_time,
                        lighting_system.as_ref(),
                        lighting_config.as_ref(),
                    ) {
                        Ok(()) => {
                            info!("Light show reloaded successfully");
                            let msg = json!({
                                "type": "reload",
                                "status": "ok",
                            });
                            let _ = broadcast_tx.send(msg.to_string());
                        }
                        Err(e) => {
                            warn!("Light show reload failed: {}", e);
                            let msg = json!({
                                "type": "reload",
                                "status": "error",
                                "error": e.to_string(),
                            });
                            let _ = broadcast_tx.send(msg.to_string());
                        }
                    }
                }
                Err(e) => {
                    error!("File watcher error: {:?}", e);
                }
            }
        }
    });

    Ok(WatcherHandle {
        _watcher: debouncer,
    })
}

/// Re-parses light show files, builds a new timeline, and swaps it in.
fn reload_timeline(
    file_paths: &[PathBuf],
    effect_engine: &Arc<Mutex<EffectEngine>>,
    current_song_timeline: &Arc<Mutex<Option<LightingTimeline>>>,
    current_song_time: &Arc<AtomicU64>,
    lighting_system: Option<&Arc<Mutex<LightingSystem>>>,
    lighting_config: Option<&crate::config::Lighting>,
) -> Result<(), Box<dyn std::error::Error>> {
    let mut all_shows = Vec::new();

    for path in file_paths {
        let content = std::fs::read_to_string(path)
            .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;

        let shows = crate::lighting::parser::parse_light_shows(&content)
            .map_err(|e| format!("Parse error in {}: {}", path.display(), e))?;

        // Validate if lighting config is available
        if let Some(lc) = lighting_config {
            validate_light_shows(&shows, Some(lc))
                .map_err(|e| format!("Validation error in {}: {}", path.display(), e))?;
        }

        for (_, show) in shows {
            all_shows.push(show);
        }
    }

    if all_shows.is_empty() {
        return Err("No shows found after re-parse".into());
    }

    let mut new_timeline = LightingTimeline::new(all_shows);

    // Get current song time
    let song_time = Duration::from_nanos(current_song_time.load(Ordering::Relaxed));

    // Calculate the timeline update before acquiring the engine lock.
    let timeline_update = if song_time > Duration::ZERO {
        new_timeline.start_at(song_time)
    } else {
        new_timeline.start();
        crate::lighting::timeline::TimelineUpdate::default()
    };

    // Pre-resolve group names while the engine lock is NOT held
    // to avoid deadlocking with the lighting system lock.
    let resolved_effects_with_elapsed: Vec<_> = {
        let mut sorted: Vec<_> = timeline_update.effects_with_elapsed.values().collect();
        sorted.sort_by_key(|(effect, _)| effect.cue_time.unwrap_or(Duration::ZERO));
        sorted
            .into_iter()
            .map(|(effect, elapsed)| {
                (
                    resolve_effect_groups(lighting_system, effect.clone()),
                    *elapsed,
                )
            })
            .collect()
    };
    let mut resolved_effects: Vec<_> = timeline_update.effects;
    resolved_effects.sort_by_key(|e| if e.id.starts_with("seq_") { 0 } else { 1 });
    let resolved_effects: Vec<_> = resolved_effects
        .into_iter()
        .map(|e| resolve_effect_groups(lighting_system, e))
        .collect();

    // Extract tempo map before moving new_timeline into the lock.
    let tempo_map = new_timeline.tempo_map().cloned();

    // Swap the timeline first, so the main effects loop won't generate
    // updates from the old timeline while the engine has new state.
    {
        let mut current = current_song_timeline.lock();
        *current = Some(new_timeline);
    }

    // Atomically stop + apply the new state under a single lock to prevent
    // the sampler loop from seeing an intermediate empty state (flash).
    {
        info!("Watcher acquiring effect_engine lock for reload...");
        let mut engine = effect_engine.lock();
        info!("Watcher acquired effect_engine lock.");
        engine.set_tempo_map(tempo_map);
        engine.stop_all_effects();

        if song_time > Duration::ZERO {
            for cmd in &timeline_update.layer_commands {
                engine.apply_layer_command(cmd);
            }
            for seq_name in &timeline_update.stop_sequences {
                engine.stop_sequence(seq_name);
            }
            for (effect, elapsed_time) in resolved_effects_with_elapsed {
                if let Err(e) = engine.start_effect_with_elapsed(effect, elapsed_time) {
                    error!("Failed to start lighting effect with elapsed time: {}", e);
                }
            }
        }
        for effect in resolved_effects {
            if let Err(e) = engine.start_effect(effect) {
                error!("Failed to start lighting effect: {}", e);
            }
        }
        info!("Watcher releasing effect_engine lock.");
    }

    Ok(())
}

/// Resolves group names in an effect's target_fixtures to actual fixture names.
fn resolve_effect_groups(
    lighting_system: Option<&Arc<Mutex<LightingSystem>>>,
    effect: crate::lighting::EffectInstance,
) -> crate::lighting::EffectInstance {
    if let Some(ls) = lighting_system {
        let mut system = ls.lock();
        system.resolve_effect_groups(effect)
    } else {
        effect
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn reload_timeline_with_valid_file_at_zero() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_ok());
        assert!(timeline.lock().is_some());
    }

    #[test]
    fn reload_timeline_with_valid_file_at_nonzero() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
    @00:05.000
    front_wash: static color: "red", duration: 5s, dimmer: 50%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(Duration::from_secs(3).as_nanos() as u64));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_ok());
        assert!(timeline.lock().is_some());
    }

    #[test]
    fn reload_timeline_with_missing_file() {
        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));

        let result = reload_timeline(
            &[PathBuf::from("/nonexistent/show.light")],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn reload_timeline_with_invalid_dsl() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("bad.light");
        std::fs::write(&dsl_path, "this is not valid DSL {").unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn reload_timeline_with_validation() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();

        // Lighting config with "front_wash" defined so validation passes
        let lighting_config = crate::config::Lighting::new(
            None,
            Some({
                let mut fixtures = std::collections::HashMap::new();
                fixtures.insert("front_wash".to_string(), "Generic_Dimmer @ 1:1".to_string());
                fixtures
            }),
            None,
            None,
        );

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            Some(&lighting_config),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn reload_timeline_validation_failure() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    unknown_fixture: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();

        // Lighting config WITHOUT "unknown_fixture" — validation will fail
        let lighting_config = crate::config::Lighting::new(
            None,
            Some({
                let mut fixtures = std::collections::HashMap::new();
                fixtures.insert("front_wash".to_string(), "Generic_Dimmer @ 1:1".to_string());
                fixtures
            }),
            None,
            None,
        );

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            Some(&lighting_config),
        );
        assert!(result.is_err());
    }

    #[test]
    fn reload_timeline_with_lighting_system() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();

        let mut ls = LightingSystem::new();
        let _ = ls.load(
            &crate::config::Lighting::new(None, None, None, None),
            tmp_dir.path(),
        );
        let lighting_system = Arc::new(Mutex::new(ls));

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(Duration::from_secs(1).as_nanos() as u64));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            Some(&lighting_system),
            None,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn reload_timeline_empty_shows_file() {
        // A DSL file that has no show blocks should give "No shows found" error
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("empty.light");
        // Write a valid DSL file but with empty content (no show blocks)
        std::fs::write(&dsl_path, "// just a comment\n").unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("No shows found"));
    }

    #[test]
    fn reload_timeline_at_nonzero_with_layer_commands() {
        // Use a DSL file with a "clear()" command to cover the layer_commands path
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
    @00:01.000
    clear()
    @00:02.000
    front_wash: static color: "red", duration: 5s, dimmer: 50%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        // Set song_time > 0 to exercise the layer_commands/stop_sequences path
        let song_time = Arc::new(AtomicU64::new(Duration::from_secs(3).as_nanos() as u64));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn reload_timeline_at_nonzero_with_stop_sequence() {
        // Use a DSL file with a stop sequence command
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"
sequence "flash" {
    @00:00.000
    front_wash: static color: "white", duration: 5s, dimmer: 100%
    @00:00.500
    front_wash: static color: "black", duration: 5s, dimmer: 0%
}

show "test" {
    @00:00.000
    sequence "flash"
    @00:01.000
    stop sequence "flash"
    @00:02.000
    front_wash: static color: "red", duration: 5s, dimmer: 50%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        // Set song_time past the stop sequence cue
        let song_time = Arc::new(AtomicU64::new(Duration::from_secs(3).as_nanos() as u64));

        let result = reload_timeline(
            &[dsl_path],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn resolve_effect_groups_without_system() {
        let effect = crate::lighting::EffectInstance::new(
            "test".to_string(),
            crate::lighting::effects::EffectType::Static {
                parameters: std::collections::HashMap::new(),
                duration: Duration::ZERO,
            },
            vec!["group1".to_string()],
            None,
            None,
            None,
        );

        let resolved = resolve_effect_groups(None, effect);
        assert_eq!(resolved.target_fixtures, vec!["group1".to_string()]);
    }

    #[test]
    fn resolve_effect_groups_with_system() {
        let ls = LightingSystem::new();
        let lighting_system = Arc::new(Mutex::new(ls));

        let effect = crate::lighting::EffectInstance::new(
            "test".to_string(),
            crate::lighting::effects::EffectType::Static {
                parameters: std::collections::HashMap::new(),
                duration: Duration::ZERO,
            },
            vec!["group1".to_string()],
            None,
            None,
            None,
        );

        let _resolved = resolve_effect_groups(Some(&lighting_system), effect);
    }

    #[test]
    fn reload_multiple_files() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path1 = tmp_dir.path().join("show1.light");
        let dsl_path2 = tmp_dir.path().join("show2.light");
        std::fs::write(
            &dsl_path1,
            r#"show "show1" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();
        std::fs::write(
            &dsl_path2,
            r#"show "show2" {
    @00:01.000
    rear_wash: static color: "red", duration: 5s, dimmer: 50%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));

        let result = reload_timeline(
            &[dsl_path1, dsl_path2],
            &effect_engine,
            &timeline,
            &song_time,
            None,
            None,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn start_watching_and_trigger_reload() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));
        let (tx, mut rx) = broadcast::channel(16);

        let _handle = start_watching(
            vec![dsl_path.clone()],
            effect_engine,
            timeline.clone(),
            song_time,
            None,
            None,
            tx,
        )
        .unwrap();

        // Modify the file to trigger a reload event
        std::thread::sleep(Duration::from_millis(500));
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "red", duration: 5s, dimmer: 50%
}"#,
        )
        .unwrap();

        // Wait for the debounced event (300ms debounce + processing time)
        // Check for a broadcast message within a reasonable timeout
        let start = std::time::Instant::now();
        let mut received = false;
        while start.elapsed() < Duration::from_secs(5) {
            match rx.try_recv() {
                Ok(msg) => {
                    assert!(
                        msg.contains("reload"),
                        "Expected reload message, got: {}",
                        msg
                    );
                    received = true;
                    break;
                }
                Err(broadcast::error::TryRecvError::Empty) => {
                    std::thread::sleep(Duration::from_millis(100));
                }
                Err(e) => panic!("Unexpected error: {:?}", e),
            }
        }
        assert!(received, "Should have received a reload broadcast message");

        // Timeline should have been updated
        assert!(
            timeline.lock().is_some(),
            "Timeline should be set after reload"
        );
    }

    #[test]
    fn start_watching_reload_error_path() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));
        let (tx, mut rx) = broadcast::channel(16);

        let _handle = start_watching(
            vec![dsl_path.clone()],
            effect_engine,
            timeline,
            song_time,
            None,
            None,
            tx,
        )
        .unwrap();

        // Wait for watcher to be established
        std::thread::sleep(Duration::from_millis(500));

        // Corrupt the file to trigger a reload error
        std::fs::write(&dsl_path, "invalid DSL content {{{").unwrap();

        // Wait for the error broadcast
        let start = std::time::Instant::now();
        while start.elapsed() < Duration::from_secs(5) {
            match rx.try_recv() {
                Ok(msg) => {
                    assert!(
                        msg.contains("error"),
                        "Expected error reload message, got: {}",
                        msg
                    );
                    break;
                }
                Err(broadcast::error::TryRecvError::Empty) => {
                    std::thread::sleep(Duration::from_millis(100));
                }
                Err(e) => panic!("Unexpected error: {:?}", e),
            }
        }
    }

    #[test]
    fn start_watching_returns_handle() {
        let tmp_dir = tempfile::tempdir().unwrap();
        let dsl_path = tmp_dir.path().join("show.light");
        std::fs::write(
            &dsl_path,
            r#"show "test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 100%
}"#,
        )
        .unwrap();

        let effect_engine = Arc::new(Mutex::new(EffectEngine::new()));
        let timeline = Arc::new(Mutex::new(None));
        let song_time = Arc::new(AtomicU64::new(0));
        let (tx, _rx) = broadcast::channel(16);

        let result = start_watching(
            vec![dsl_path],
            effect_engine,
            timeline,
            song_time,
            None,
            None,
            tx,
        );
        assert!(result.is_ok());

        // WatcherHandle drops here, stopping the watcher
    }
}