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
// 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 crate::lighting::effects::{EffectLayer, EffectType};
use crate::lighting::parser::types::LayerCommandType;
use crate::lighting::parser::*;

#[test]
fn test_layer_command_parsing() {
    // Test parsing layer commands in a show
    let content = r#"show "Layer Control Test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, layer: foreground

    @00:05.000
    release(layer: foreground, time: 2s)

    @00:10.000
    clear(layer: foreground)

    @00:15.000
    freeze(layer: background)

    @00:20.000
    unfreeze(layer: background)

    @00:25.000
    master(layer: midground, intensity: 50%, speed: 200%)
}"#;
    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Layer command parsing should succeed: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Layer Control Test").expect("Show should exist");

    // Check that cues were parsed
    assert_eq!(show.cues.len(), 6, "Should have 6 cues");

    // First cue has an effect, no layer commands
    assert_eq!(show.cues[0].effects.len(), 1);
    assert_eq!(show.cues[0].layer_commands.len(), 0);

    // Second cue: release command
    assert_eq!(show.cues[1].effects.len(), 0);
    assert_eq!(show.cues[1].layer_commands.len(), 1);
    let release_cmd = &show.cues[1].layer_commands[0];
    assert_eq!(release_cmd.command_type, LayerCommandType::Release);
    assert_eq!(release_cmd.layer, Some(EffectLayer::Foreground));
    assert_eq!(
        release_cmd.fade_time,
        Some(std::time::Duration::from_secs(2))
    );

    // Third cue: clear command
    let clear_cmd = &show.cues[2].layer_commands[0];
    assert_eq!(clear_cmd.command_type, LayerCommandType::Clear);
    assert_eq!(clear_cmd.layer, Some(EffectLayer::Foreground));

    // Fourth cue: freeze command
    let freeze_cmd = &show.cues[3].layer_commands[0];
    assert_eq!(freeze_cmd.command_type, LayerCommandType::Freeze);
    assert_eq!(freeze_cmd.layer, Some(EffectLayer::Background));

    // Fifth cue: unfreeze command
    let unfreeze_cmd = &show.cues[4].layer_commands[0];
    assert_eq!(unfreeze_cmd.command_type, LayerCommandType::Unfreeze);
    assert_eq!(unfreeze_cmd.layer, Some(EffectLayer::Background));

    // Sixth cue: master command
    let master_cmd = &show.cues[5].layer_commands[0];
    assert_eq!(master_cmd.command_type, LayerCommandType::Master);
    assert_eq!(master_cmd.layer, Some(EffectLayer::Midground));
    assert!((master_cmd.intensity.unwrap() - 0.5).abs() < 0.01);
    assert!((master_cmd.speed.unwrap() - 2.0).abs() < 0.01);
}

#[test]
fn test_clear_all_layers_command() {
    // Test parsing clear() without layer parameter (clears all layers)
    let content = r#"show "Clear All Test" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, layer: foreground
    back_wash: static color: "red", duration: 5s, layer: background

    @00:05.000
    clear()
}"#;
    let result = parse_light_shows(content);
    assert!(
        result.is_ok(),
        "Clear all command parsing should succeed: {:?}",
        result.err()
    );

    let shows = result.unwrap();
    let show = shows.get("Clear All Test").expect("Show should exist");

    // Check that cues were parsed
    assert_eq!(show.cues.len(), 2, "Should have 2 cues");

    // Second cue: clear all command (no layer parameter)
    assert_eq!(show.cues[1].effects.len(), 0);
    assert_eq!(show.cues[1].layer_commands.len(), 1);
    let clear_cmd = &show.cues[1].layer_commands[0];
    assert_eq!(clear_cmd.command_type, LayerCommandType::Clear);
    assert_eq!(clear_cmd.layer, None, "Clear all should have no layer");
}

#[test]
fn test_t_dsl_parsing_errors() {
    // Test invalid DSL syntax
    let invalid_syntax = r#"show "Invalid Show" {
    @invalid_time
    front_wash: invalid_effect
}"#;
    let result = parse_light_shows(invalid_syntax);
    assert!(result.is_err(), "Invalid syntax should fail");

    // Test single unnamed show (allowed)
    let single_unnamed = r#"show {
    @00:00.000
    front_wash: static color: "blue", duration: 5s
}"#;
    let result = parse_light_shows(single_unnamed);
    assert!(
        result.is_ok(),
        "Single unnamed show should be allowed: {:?}",
        result.err()
    );
    let shows = result.unwrap();
    assert_eq!(
        shows.len(),
        1,
        "Expected exactly one show for single unnamed block"
    );

    // Test multiple shows where one is unnamed (should fail)
    let multiple_with_unnamed = r#"show "Named Show" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s
}

show {
    @00:05.000
    back_wash: static color: "red", duration: 5s
}"#;
    let result = parse_light_shows(multiple_with_unnamed);
    assert!(
        result.is_err(),
        "Missing show name should fail when multiple shows are defined"
    );

    // Test malformed time string
    let malformed_time = r#"show "Test Show" {
    @invalid_time
    front_wash: static color: "blue", duration: 5s
}"#;
    let result = parse_light_shows(malformed_time);
    assert!(result.is_err(), "Malformed time should fail");

    // Test empty content
    let empty_content = "";
    let result = parse_light_shows(empty_content);
    assert!(result.is_ok(), "Empty content should be OK");
    assert_eq!(result.unwrap().len(), 0);

    // Test content that looks like a show but has no valid shows
    let no_shows = r#"// This is a comment
some invalid content
not a show"#;
    let _result = parse_light_shows(no_shows);
    // The parser may fail on invalid content, which is acceptable
    // We just test that it doesn't panic
}

#[test]
fn test_dsl_edge_cases() {
    // Test empty show
    let empty_show = r#"show "Empty Show" { }"#;
    let result = parse_light_shows(empty_show);
    assert!(result.is_ok());
    let shows = result.unwrap();
    assert_eq!(shows.len(), 1);
    assert_eq!(shows["Empty Show"].cues.len(), 0);

    // Test show with overlapping cues
    let overlapping_cues = r#"show "Overlapping Show" {
    @00:05.000
    front_wash: static color: "blue", duration: 5s, dimmer: 60%
    
    @00:05.000
    back_wash: static color: "red", duration: 5s, dimmer: 80%
}"#;
    let result = parse_light_shows(overlapping_cues);
    assert!(result.is_ok());
    let shows = result.unwrap();
    assert_eq!(shows["Overlapping Show"].cues.len(), 2);

    // Test show with multiple effects in one cue
    let multiple_effects = r#"show "Multiple Effects" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 60%
    back_wash: static color: "red", duration: 5s, dimmer: 80%
}"#;
    let result = parse_light_shows(multiple_effects);
    assert!(result.is_ok());
    let shows = result.unwrap();
    assert_eq!(shows["Multiple Effects"].cues.len(), 1);
    assert_eq!(shows["Multiple Effects"].cues[0].effects.len(), 2);

    // Test show with missing parameters (duration is now required)
    let missing_params = r#"show "Missing Params" {
    @00:00.000
    front_wash: static
}"#;
    let result = parse_light_shows(missing_params);
    assert!(
        result.is_err(),
        "Static effect without duration should fail"
    );
}

#[test]
fn test_dsl_performance_large_file() {
    // Create a large DSL file with many cues
    let mut large_content = String::new();
    large_content.push_str(r#"show "Large Show" {"#);

    for i in 0..100 {
        let time_ms = i * 1000; // 1 second intervals
        let minutes = time_ms / 60000;
        let seconds = (time_ms % 60000) / 1000;
        let milliseconds = time_ms % 1000;

        large_content.push_str(&format!(
            r#"
    @{:02}:{:02}.{:03}
    fixture_{}: static color: "blue", duration: 5s, dimmer: {}%"#,
            minutes,
            seconds,
            milliseconds,
            i,
            (i % 100)
        ));
    }

    large_content.push_str("\n}");

    // Test parsing performance
    let start = std::time::Instant::now();
    let result = parse_light_shows(&large_content);
    let duration = start.elapsed();

    assert!(result.is_ok(), "Large file should parse successfully");
    assert!(
        duration.as_millis() < 1000,
        "Parsing should be fast (< 1 second)"
    );

    let shows = result.unwrap();
    assert_eq!(shows.len(), 1);
    assert_eq!(shows["Large Show"].cues.len(), 100);
}

#[test]
fn test_whitespace_handling() {
    // Test zero whitespace
    let no_whitespace =
        r#"show"Test Show"{@00:00.000 front_wash:static color:"blue",duration:5s,dimmer:60%}"#;
    let result = parse_light_shows(no_whitespace);
    assert!(
        result.is_ok(),
        "Failed to parse DSL with zero whitespace: {:?}",
        result
    );

    let shows = result.unwrap();
    assert_eq!(shows.len(), 1);
    let show = shows.get("Test Show").unwrap();
    assert_eq!(show.cues.len(), 1);
    assert_eq!(show.cues[0].effects.len(), 1);

    // Test minimal whitespace (just what's needed)
    let minimal_whitespace = r#"show "Test Show" {
@00:00.000
front_wash: static color: "blue", duration: 5s, dimmer: 60%
}"#;
    let result = parse_light_shows(minimal_whitespace);
    assert!(
        result.is_ok(),
        "Failed to parse DSL with minimal whitespace: {:?}",
        result
    );

    let shows = result.unwrap();
    assert_eq!(shows.len(), 1);
    let show = shows.get("Test Show").unwrap();
    assert_eq!(show.cues.len(), 1);
    assert_eq!(show.cues[0].effects.len(), 1);

    // Test moderate whitespace
    let moderate_whitespace = r#"show "Test Show" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 60%
}"#;
    let result = parse_light_shows(moderate_whitespace);
    assert!(
        result.is_ok(),
        "Failed to parse DSL with moderate whitespace: {:?}",
        result
    );

    let shows = result.unwrap();
    assert_eq!(shows.len(), 1);
    let show = shows.get("Test Show").unwrap();
    assert_eq!(show.cues.len(), 1);
    assert_eq!(show.cues[0].effects.len(), 1);

    // Test excessive whitespace (this might fail due to grammar limitations)
    let excessive_whitespace = r#"
            show    "Test Show"    {
        @00:00.000
        front_wash    :    static
        color    :    "blue"    ,
        duration    :    5s    ,
        dimmer    :    60%
    }
    "#;
    let result = parse_light_shows(excessive_whitespace);
    // This might fail due to the grammar not handling excessive whitespace well
    if let Ok(shows) = result {
        assert_eq!(shows.len(), 1);
        let show = shows.get("Test Show").unwrap();
        assert_eq!(show.cues.len(), 1);
        assert_eq!(show.cues[0].effects.len(), 1);
    }

    // Test mixed whitespace (tabs, spaces, newlines)
    let mixed_whitespace = r#"show	"Test Show"	{
	@00:00.000
	front_wash	:	static
	color	:	"blue"	,
	duration	:	5s	,
	dimmer	:	60%
}"#;
    let result = parse_light_shows(mixed_whitespace);
    assert!(
        result.is_ok(),
        "Failed to parse DSL with mixed whitespace: {:?}",
        result
    );

    let shows = result.unwrap();
    assert_eq!(shows.len(), 1);
    let show = shows.get("Test Show").unwrap();
    assert_eq!(show.cues.len(), 1);
    assert_eq!(show.cues[0].effects.len(), 1);
}

#[test]
fn test_extreme_whitespace_handling() {
    // Test with very long whitespace sequences
    let long_whitespace = format!(
        r#"show "Test Show" {{{}@00:00.000{}front_wash: static color: "blue", duration: 5s, dimmer: 60%{}}}"#,
        " ".repeat(50),
        " ".repeat(50),
        " ".repeat(50)
    );
    let result = parse_light_shows(&long_whitespace);
    assert!(
        result.is_ok(),
        "Failed to parse DSL with long whitespace: {:?}",
        result
    );

    // Test with mixed whitespace characters
    let mixed_whitespace = r#"show	"Test Show"	{
		@00:00.000
		front_wash:	static	color:	"blue",	duration:	5s,	dimmer:	60%
	}"#;
    let result = parse_light_shows(mixed_whitespace);
    assert!(
        result.is_ok(),
        "Failed to parse DSL with mixed whitespace: {:?}",
        result
    );

    // Test with newlines in various places
    let newline_whitespace = r#"show
"Test Show"
{
@00:00.000
front_wash:
static
color:
"blue",
duration:
5s,
dimmer:
60%
}"#;
    let result = parse_light_shows(newline_whitespace);
    assert!(
        result.is_ok(),
        "Failed to parse DSL with newline whitespace: {:?}",
        result
    );
}

#[test]
fn test_comprehensive_dsl_parsing() {
    // Test a comprehensive DSL file that uses various parameter types
    let comprehensive_dsl = r#"show "Comprehensive Light Show" {
    @00:00.000
    front_wash: static color: "blue", duration: 5s, dimmer: 60%
    
    @00:05.000
    back_wash: static color: "red", duration: 5s, dimmer: 80%
    
    @00:10.000
    strobe_lights: static color: "green", duration: 5s, dimmer: 100%
    
    @00:15.000
    moving_heads: static color: "white", duration: 5s, dimmer: 50%
    
    @00:20.000
    dimmer_test: static color: "yellow", duration: 5s, dimmer: 75%
    
    @00:25.000
    rainbow_effect: static color: "cyan", duration: 5s, dimmer: 90%
    
    @00:30.000
    pulse_lights: static color: "magenta", duration: 5s, dimmer: 25%
    
    @00:35.000
    color_cycle: static color: "orange", duration: 5s, dimmer: 85%
    
    @00:40.000
    complex_chase: static color: "purple", duration: 5s, dimmer: 95%
    
    @00:45.000
    strobe_variation: static color: "black", duration: 5s, dimmer: 0%
    
    @00:50.000
    down_time: static color: "white", duration: 5s, dimmer: 100%
}"#;

    let result = parse_light_shows(comprehensive_dsl);
    if let Err(e) = &result {
        println!("DSL parsing error: {}", e);
    }
    assert!(
        result.is_ok(),
        "Comprehensive DSL should parse successfully"
    );

    let shows = result.unwrap();
    assert_eq!(shows.len(), 1);
    let show = shows.get("Comprehensive Light Show").unwrap();
    assert_eq!(show.cues.len(), 11);

    // Verify that different effect types are parsed
    let first_cue = &show.cues[0];
    assert_eq!(first_cue.effects.len(), 1);
    // Check that it's a static effect (we can't directly compare struct variants)
    match &first_cue.effects[0].effect_type {
        EffectType::Static { .. } => {} // This is what we expect
        _ => panic!("Expected static effect"),
    }

    // Verify that parameters are parsed correctly
    let static_effect = &first_cue.effects[0];
    // Check that the effect type has the expected parameters
    match &static_effect.effect_type {
        crate::lighting::effects::EffectType::Static { parameters, .. } => {
            assert!(
                parameters.contains_key("color")
                    || parameters.contains_key("red")
                    || parameters.contains_key("green")
                    || parameters.contains_key("blue")
            );
            assert!(parameters.contains_key("dimmer"));
        }
        _ => panic!("Expected static effect"),
    }
}