pmc-whirlwind 0.3.0

whirlwind is a collaborative Reaper project sync tool for podcast co-editors. It uses Cloudflare R2 for storage and synchronization.
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
use uuid::Uuid;

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Returns true when a line opens a new block (`<KEYWORD ...`).
fn opens_block(line: &str) -> bool {
    line.trim_start().starts_with('<')
}

/// Returns true when a line is a bare closing tag (`>`), ending a block.
fn closes_block(line: &str) -> bool {
    line.trim() == ">"
}

/// Build the `<ITEM>` text to insert into a named track.
fn item_block(file_path: &str, duration_secs: f64, position_secs: f64) -> String {
    let iguid = format!("{{{}}}", Uuid::new_v4().to_string().to_uppercase());
    let guid = format!("{{{}}}", Uuid::new_v4().to_string().to_uppercase());
    let name = std::path::Path::new(file_path)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or(file_path);
    format!(
        "    <ITEM\n      POSITION {position_secs}\n      SNAPOFFS 0\n      LENGTH {duration_secs}\n      LOOP 0\n      ALLTAKES 0\n      FADEIN 1 0 0 1 0 0 0\n      FADEOUT 1 0 0 1 0 0 0\n      MUTE 0 0\n      SEL 0\n      IGUID {iguid}\n      IID 1\n      NAME {name}\n      VOLPAN 1 0 1 -1\n      SOFFS 0\n      PLAYRATE 1 1 0 -1 0 0.0025\n      CHANMODE 0\n      GUID {guid}\n      <SOURCE WAVE\n        FILE \"{file_path}\"\n      >\n    >"
    )
}

/// Scan forward from `start` (inclusive) and return the index of the line
/// that closes the block opened at `start` (depth returns to 0).
///
/// `<FXCHAIN` blocks are treated as opaque — depth is still tracked so we
/// find the correct closing `>`, but we do not look for named sub-blocks
/// inside them.
fn find_block_end(lines: &[&str], start: usize) -> usize {
    let mut depth: usize = 1; // we are already inside the block at `start`
    let mut i = start + 1;
    let mut inside_fxchain = false;

    while i < lines.len() {
        let trimmed = lines[i].trim();

        if !inside_fxchain && trimmed.starts_with("<FXCHAIN") {
            inside_fxchain = true;
            depth += 1;
        } else if inside_fxchain {
            if trimmed == ">" {
                depth -= 1;
                if depth == 1 {
                    inside_fxchain = false;
                } else if depth == 0 {
                    return i;
                }
            } else if opens_block(lines[i]) {
                depth += 1;
            }
        } else if opens_block(lines[i]) {
            depth += 1;
        } else if closes_block(lines[i]) {
            depth -= 1;
            if depth == 0 {
                return i;
            }
        }
        i += 1;
    }
    // Malformed input — return last line index.
    lines.len().saturating_sub(1)
}

/// Find the first direct-child `<ITEM` block within a track, returning its
/// `(start_line, end_line)` range (inclusive). Returns `None` if absent.
fn find_direct_item(
    lines: &[&str],
    track_start: usize,
    track_end: usize,
) -> Option<(usize, usize)> {
    let mut depth: usize = 1;
    let mut inside_fxchain = false;

    for i in (track_start + 1)..=track_end {
        if i >= lines.len() {
            break;
        }
        let trimmed = lines[i].trim();

        if !inside_fxchain && trimmed.starts_with("<FXCHAIN") {
            inside_fxchain = true;
            depth += 1;
            continue;
        }

        if inside_fxchain {
            if trimmed == ">" {
                depth -= 1;
                if depth == 1 {
                    inside_fxchain = false;
                }
            } else if opens_block(lines[i]) {
                depth += 1;
            }
            continue;
        }

        if depth == 1 && trimmed.starts_with("<ITEM") {
            let item_end = find_block_end(lines, i);
            return Some((i, item_end));
        }

        if opens_block(lines[i]) {
            depth += 1;
        } else if closes_block(lines[i]) {
            depth -= 1;
            if depth == 0 {
                break;
            }
        }
    }
    None
}

/// Extract the track name value from a `NAME` attribute line (trimmed).
///
/// Handles both Reaper's quoted form (`NAME "track name"`) and unquoted form
/// (`NAME trackname`). Returns `None` if the line is not a NAME attribute.
fn parse_name_value(trimmed: &str) -> Option<&str> {
    let rest = trimmed.strip_prefix("NAME ")?;
    if let Some(inner) = rest.strip_prefix('"') {
        inner.strip_suffix('"')
    } else {
        Some(rest)
    }
}

/// Return true if the TRACK block from `track_start` to `track_end` (inclusive)
/// contains a `NAME` line (quoted or unquoted) matching `track_name` at depth 1.
fn track_has_name(lines: &[&str], track_start: usize, track_end: usize, track_name: &str) -> bool {
    let mut depth: usize = 1;
    let mut inside_fxchain = false;

    for i in (track_start + 1)..=track_end {
        if i >= lines.len() {
            break;
        }
        let trimmed = lines[i].trim();

        if !inside_fxchain && trimmed.starts_with("<FXCHAIN") {
            inside_fxchain = true;
            depth += 1;
            continue;
        }

        if inside_fxchain {
            if trimmed == ">" {
                depth -= 1;
                if depth == 1 {
                    inside_fxchain = false;
                }
            } else if opens_block(lines[i]) {
                depth += 1;
            }
            continue;
        }

        if opens_block(lines[i]) {
            depth += 1;
        } else if closes_block(lines[i]) {
            depth -= 1;
            if depth == 0 {
                break;
            }
        } else if depth == 1
            && let Some(name) = parse_name_value(trimmed)
            && name == track_name
        {
            return true;
        }
    }
    false
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Insert or replace the `<ITEM>` block in an existing named track.
///
/// - If the track has no existing `<ITEM>`, inserts one before the track's closing `>`.
/// - If the track already has an `<ITEM>` (e.g. a placeholder), replaces it.
/// - Returns the string unchanged if the named track is not found.
pub fn set_track_item(
    rpp: &str,
    track_name: &str,
    file_path: &str,
    duration_secs: f64,
    position_secs: f64,
) -> String {
    let lines: Vec<&str> = rpp.lines().collect();
    let n = lines.len();
    let mut out: Vec<String> = Vec::with_capacity(n + 20);
    let mut i = 0;

    while i < n {
        let line = lines[i];

        if line.trim_start().starts_with("<TRACK") {
            let track_end = find_block_end(&lines, i);

            if track_has_name(&lines, i, track_end, track_name) {
                let existing_item = find_direct_item(&lines, i, track_end);
                // Emit track lines, skipping any existing <ITEM> block.
                for (k, line) in lines.iter().enumerate().take(track_end).skip(i) {
                    if let Some((s, e)) = existing_item
                        && k >= s
                        && k <= e
                    {
                        continue;
                    }
                    out.push((*line).to_string());
                }
                // Insert new <ITEM> block before the closing >.
                let item = item_block(file_path, duration_secs, position_secs);
                for item_line in item.lines() {
                    out.push(item_line.to_string());
                }
                out.push(lines[track_end].to_string()); // closing >
            } else {
                for line in lines.iter().take(track_end + 1).skip(i) {
                    out.push(line.to_string());
                }
            }
            i = track_end + 1;
        } else {
            out.push(line.to_string());
            i += 1;
        }
    }

    let mut result = out.join("\n");
    if rpp.ends_with('\n') {
        result.push('\n');
    }
    result
}

/// Update the `POSITION` of the `<ITEM>` in a named track (used for outro
/// placement). Returns the string unchanged if the track or item is not found.
pub fn set_item_position(rpp: &str, track_name: &str, position_secs: f64) -> String {
    let lines: Vec<&str> = rpp.lines().collect();
    let n = lines.len();
    let mut out: Vec<String> = lines.iter().map(|s| s.to_string()).collect();
    let mut i = 0;

    while i < n {
        let line = lines[i];

        if line.trim_start().starts_with("<TRACK") {
            let track_end = find_block_end(&lines, i);

            if track_has_name(&lines, i, track_end, track_name) {
                // Find the first <ITEM inside this track and update its POSITION.
                let mut depth: usize = 1;
                let mut j = i + 1;
                let mut inside_fxchain = false;
                let mut updated = false;
                let mut in_item = false;
                let mut item_depth: usize = 0;

                while j <= track_end && !updated {
                    let sl = lines[j];
                    let trimmed = sl.trim();

                    if !inside_fxchain && trimmed.starts_with("<FXCHAIN") {
                        inside_fxchain = true;
                        depth += 1;
                    } else if inside_fxchain {
                        if trimmed == ">" {
                            depth -= 1;
                            if depth == 1 {
                                inside_fxchain = false;
                            }
                        } else if opens_block(sl) {
                            depth += 1;
                        }
                    } else if !in_item && trimmed.starts_with("<ITEM") {
                        in_item = true;
                        item_depth = depth + 1;
                        depth += 1;
                    } else if in_item && depth == item_depth && trimmed.starts_with("POSITION") {
                        let indent = &sl[..sl.len() - sl.trim_start().len()];
                        out[j] = format!("{indent}POSITION {position_secs}");
                        updated = true;
                    } else if opens_block(sl) {
                        depth += 1;
                    } else if closes_block(sl) {
                        depth -= 1;
                        if in_item && depth < item_depth {
                            in_item = false;
                        }
                    }
                    j += 1;
                }
            }
            i = track_end + 1;
        } else {
            i += 1;
        }
    }

    let mut result = out.join("\n");
    if rpp.ends_with('\n') {
        result.push('\n');
    }
    result
}

/// Append new plain TRACK blocks (no FX chain) before the closing `>` of the
/// project root.
pub fn insert_tracks(rpp: &str, tracks: &[String]) -> String {
    if tracks.is_empty() {
        return rpp.to_string();
    }

    let lines: Vec<&str> = rpp.lines().collect();
    let n = lines.len();

    // Find the last bare `>` — the project root closing tag.
    let close_idx = match (0..n).rev().find(|&k| lines[k].trim() == ">") {
        Some(idx) => idx,
        None => return rpp.to_string(),
    };

    let mut out: Vec<String> = Vec::with_capacity(n + tracks.len() * 20);
    for line in lines.iter().take(close_idx) {
        out.push(line.to_string());
    }
    for track in tracks {
        for tl in track.lines() {
            out.push(tl.to_string());
        }
    }
    out.push(lines[close_idx].to_string());

    let mut result = out.join("\n");
    if rpp.ends_with('\n') {
        result.push('\n');
    }
    result
}

/// Set or replace the project end marker. Inserts if absent.
///
/// Marker line format: `MARKER 1 {end_secs} "End" 0 0 1 R {GUID} 0`
pub fn set_end_marker(rpp: &str, end_secs: f64) -> String {
    let guid = format!("{{{}}}", Uuid::new_v4().to_string().to_uppercase());
    let new_marker = format!("MARKER 1 {end_secs} \"End\" 0 0 1 R {guid} 0");

    let lines: Vec<&str> = rpp.lines().collect();
    let n = lines.len();
    let mut replaced = false;
    let mut out: Vec<String> = Vec::with_capacity(n + 1);

    for line in &lines {
        if !replaced && line.trim().starts_with("MARKER 1 ") {
            let indent = &line[..line.len() - line.trim_start().len()];
            out.push(format!("{indent}{new_marker}"));
            replaced = true;
        } else {
            out.push(line.to_string());
        }
    }

    if !replaced {
        // Insert before the final `>`.
        let close_idx = (0..out.len())
            .rev()
            .find(|&k| out[k].trim() == ">")
            .unwrap_or(out.len());
        out.insert(close_idx, new_marker);
    }

    let mut result = out.join("\n");
    if rpp.ends_with('\n') {
        result.push('\n');
    }
    result
}

/// Replace the `FILE "..."` path inside the `<SOURCE WAVE>` block of the first
/// `<ITEM>` in a named track.
///
/// Returns the string unchanged if the named track is not found or has no ITEM.
pub fn set_source_file(rpp: &str, track_name: &str, file_path: &str) -> String {
    let lines: Vec<&str> = rpp.lines().collect();
    let n = lines.len();
    let mut out: Vec<String> = lines.iter().map(|s| s.to_string()).collect();
    let mut i = 0;

    while i < n {
        let line = lines[i];
        if line.trim_start().starts_with("<TRACK") {
            let track_end = find_block_end(&lines, i);
            if track_has_name(&lines, i, track_end, track_name) {
                if let Some((item_start, item_end)) = find_direct_item(&lines, i, track_end) {
                    // Find <SOURCE block inside the ITEM.
                    let mut source_found = false;
                    let mut j = item_start + 1;
                    while j < item_end {
                        if lines[j].trim().starts_with("<SOURCE") {
                            source_found = true;
                            let source_end = find_block_end(&lines, j);
                            // Replace FILE line inside <SOURCE>.
                            let mut file_found = false;
                            for k in (j + 1)..source_end {
                                if k >= n {
                                    break;
                                }
                                let sl = lines[k];
                                if sl.trim().starts_with("FILE ") {
                                    let indent = &sl[..sl.len() - sl.trim_start().len()];
                                    out[k] = format!("{indent}FILE \"{file_path}\"");
                                    file_found = true;
                                    break;
                                }
                            }
                            if !file_found {
                                eprintln!(
                                    "Warning: set_source_file: no FILE line found in <SOURCE> for track '{track_name}'"
                                );
                            }
                            break;
                        }
                        j += 1;
                    }
                    if !source_found {
                        eprintln!(
                            "Warning: set_source_file: no <SOURCE> block found in <ITEM> for track '{track_name}'"
                        );
                    }
                }
                i = track_end + 1;
            } else {
                i = track_end + 1;
            }
        } else {
            i += 1;
        }
    }

    let mut result = out.join("\n");
    if rpp.ends_with('\n') {
        result.push('\n');
    }
    result
}

/// Return the LENGTH (in seconds) of the first `<ITEM>` in the named track.
///
/// Returns `0.0` if the track or item is not found or the LENGTH cannot be parsed.
pub fn get_track_item_length(rpp: &str, track_name: &str) -> f64 {
    let lines: Vec<&str> = rpp.lines().collect();
    let n = lines.len();
    let mut i = 0;

    while i < n {
        let line = lines[i];
        if line.trim_start().starts_with("<TRACK") {
            let track_end = find_block_end(&lines, i);
            if track_has_name(&lines, i, track_end, track_name) {
                if let Some((item_start, item_end)) = find_direct_item(&lines, i, track_end) {
                    for line in lines.iter().take(item_end + 1).skip(item_start + 1) {
                        if let Some(rest) = line.trim().strip_prefix("LENGTH ") {
                            return rest.parse::<f64>().unwrap_or(0.0);
                        }
                    }
                }
                eprintln!(
                    "Warning: get_track_item_length: no LENGTH found for track '{track_name}'"
                );
                return 0.0;
            }
            i = track_end + 1;
        } else {
            i += 1;
        }
    }
    0.0
}

/// Build a plain `<TRACK>` block (no FX chain) for an unmatched audio file.
///
/// The track name is derived from the filename stem. The GUID is a fresh UUID v4.
pub fn build_plain_track(file_path: &str, duration_secs: f64) -> String {
    let track_guid = format!("{{{}}}", Uuid::new_v4().to_string().to_uppercase());
    let iguid = format!("{{{}}}", Uuid::new_v4().to_string().to_uppercase());
    let item_guid = format!("{{{}}}", Uuid::new_v4().to_string().to_uppercase());
    let stem = std::path::Path::new(file_path)
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or(file_path);
    let name = std::path::Path::new(file_path)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or(file_path);
    format!(
        "<TRACK {track_guid}\n  NAME {stem}\n  <ITEM\n    POSITION 0\n    SNAPOFFS 0\n    LENGTH {duration_secs}\n    LOOP 0\n    ALLTAKES 0\n    FADEIN 1 0 0 1 0 0 0\n    FADEOUT 1 0 0 1 0 0 0\n    MUTE 0 0\n    SEL 0\n    IGUID {iguid}\n    IID 1\n    NAME {name}\n    VOLPAN 1 0 1 -1\n    SOFFS 0\n    PLAYRATE 1 1 0 -1 0 0.0025\n    CHANMODE 0\n    GUID {item_guid}\n    <SOURCE WAVE\n      FILE \"{file_path}\"\n    >\n  >\n>"
    )
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

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

    // -----------------------------------------------------------------------
    // set_track_item
    // -----------------------------------------------------------------------

    #[test]
    fn set_track_item_inserts_into_named_track() {
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME "erik"
  <FXCHAIN
    SHOW 0
    <VST "VST3: ReaEQ" reaEQ.vst3 0 "" >
      BASE64BLOB==
    >
  >
>
>"#;
        let result = set_track_item(rpp, "erik", "audio/erik-ep42.wav", 3612.5, 0.0);
        assert!(
            result.contains(r#"FILE "audio/erik-ep42.wav""#),
            "FILE path missing:\n{result}"
        );
        assert!(
            result.contains("LENGTH 3612.5"),
            "LENGTH missing:\n{result}"
        );
        assert!(
            result.contains("<FXCHAIN"),
            "FXCHAIN was dropped:\n{result}"
        );
    }

    #[test]
    fn set_track_item_no_op_when_track_not_found() {
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME "other-track"
>
>"#;
        let result = set_track_item(rpp, "erik", "audio/erik-ep42.wav", 3612.5, 0.0);
        assert_eq!(result, rpp, "Should return input unchanged");
    }

    #[test]
    fn set_track_item_replaces_existing_item() {
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME "mike"
  <FXCHAIN
    SHOW 0
  >
  <ITEM
    POSITION 0
    LENGTH 30
    <SOURCE WAVE
      FILE "audio/placeholder.wav"
    >
  >
>
>"#;
        // Track has a placeholder <ITEM> — set_track_item must replace it.
        let result = set_track_item(rpp, "mike", "audio/ep42-mike.wav", 3600.0, 0.0);
        assert!(
            result.contains(r#"FILE "audio/ep42-mike.wav""#),
            "New FILE must appear:\n{result}"
        );
        assert!(
            !result.contains(r#"FILE "audio/placeholder.wav""#),
            "Old FILE must be gone:\n{result}"
        );
        assert!(
            result.contains("LENGTH 3600"),
            "New LENGTH must appear:\n{result}"
        );
        assert!(
            result.contains("<FXCHAIN"),
            "FXCHAIN must be preserved:\n{result}"
        );
        assert_eq!(
            result.matches("<ITEM").count(),
            1,
            "Exactly one <ITEM> expected:\n{result}"
        );
    }

    #[test]
    fn set_track_item_exact_name_match_does_not_match_prefix() {
        // looking for "erik" must not insert into a track named "erik-mic"
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME "erik-mic"
>
>"#;
        let result = set_track_item(rpp, "erik", "audio/erik.wav", 100.0, 0.0);
        assert_eq!(
            result, rpp,
            "Should not match 'erik-mic' when searching for 'erik'"
        );
    }

    #[test]
    fn set_track_item_handles_unquoted_track_name() {
        // Reaper saves simple names without quotes: NAME erik (not NAME "erik")
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME erik
>
>"#;
        let result = set_track_item(rpp, "erik", "audio/ep42-erik.wav", 3600.0, 0.0);
        assert!(
            result.contains(r#"FILE "audio/ep42-erik.wav""#),
            "Should insert into unquoted-name track:\n{result}"
        );
    }

    // -----------------------------------------------------------------------
    // set_item_position
    // -----------------------------------------------------------------------

    #[test]
    fn set_item_position_updates_correct_track() {
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME "outro"
  <ITEM
    POSITION 0
    LENGTH 30
  >
>
<TRACK {BBB}
  NAME "intro"
  <ITEM
    POSITION 0
    LENGTH 10
  >
>
>"#;
        let result = set_item_position(rpp, "outro", 55.5);
        assert!(
            result.contains("POSITION 55.5"),
            "Expected POSITION 55.5:\n{result}"
        );
        let count_55 = result.matches("POSITION 55.5").count();
        let count_0 = result.matches("POSITION 0").count();
        assert_eq!(count_55, 1, "Should only update one track:\n{result}");
        assert_eq!(count_0, 1, "Intro POSITION should remain 0:\n{result}");
    }

    // -----------------------------------------------------------------------
    // insert_tracks
    // -----------------------------------------------------------------------

    #[test]
    fn insert_tracks_appends_before_root_close() {
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME "host"
>
>"#;
        let track = build_plain_track("audio/guest.wav", 1800.0);
        let tracks = vec![track];
        let result = insert_tracks(rpp, &tracks);

        let lines: Vec<&str> = result.lines().collect();
        let last = lines.last().copied().unwrap_or("");
        assert_eq!(last, ">", "Final line should be project root close");
        assert!(
            result.contains(r#"FILE "audio/guest.wav""#),
            "Inserted track missing:\n{result}"
        );
        assert!(
            !result.contains("<FXCHAIN"),
            "Plain tracks must not contain FXCHAIN:\n{result}"
        );
    }

    // -----------------------------------------------------------------------
    // set_end_marker
    // -----------------------------------------------------------------------

    #[test]
    fn set_end_marker_inserts_when_absent() {
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
<TRACK {AAA}
  NAME "host"
>
>"#;
        let result = set_end_marker(rpp, 3610.0);
        assert!(
            result.contains("MARKER 1 3610"),
            "Marker missing:\n{result}"
        );
    }

    #[test]
    fn set_end_marker_replaces_existing() {
        let rpp = r#"<REAPER_PROJECT 0.1 "6.0" 1234567890
MARKER 1 100 "End" 0 0 1 R {OLD-GUID} 0
<TRACK {AAA}
  NAME "host"
>
>"#;
        let result = set_end_marker(rpp, 3610.0);
        let marker_count = result.matches("MARKER 1 ").count();
        assert_eq!(
            marker_count, 1,
            "Should only have one MARKER 1 line:\n{result}"
        );
        assert!(
            result.contains("MARKER 1 3610"),
            "Updated marker missing:\n{result}"
        );
        assert!(
            !result.contains("MARKER 1 100"),
            "Old marker should be gone:\n{result}"
        );
    }
}