clipasm 0.5.0

A strict, typed language for compiling and rendering Video and Audio graphs
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
use std::fmt::Write as _;

use clipasm::compiler;

use super::support::*;

#[test]
fn alias_does_not_retroactively_merge_original_roots_after_join() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin as joined\nbad = $a::start + $b::end\nduring(timeline=$joined, range=$joined::start..$bad) { zoom_in(2%) }\n",
    );

    let error = compiler::compile(&workflow).expect_err("original roots remain distinct");
    assert_eq!(error.code, "E_TIMELINE_ROOT_MISMATCH");
    assert!(
        error
            .notes
            .iter()
            .any(|note| note.contains("left coordinate root") && note.contains("`$a` [0s..1s)"))
    );
    assert!(
        error
            .notes
            .iter()
            .any(|note| note.contains("right coordinate root") && note.contains("`$b` [0s..1s)"))
    );
}

#[test]
fn unknown_placement_reports_the_real_canonical_layout_tree() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s)\nimage(\"b.ppm\", 1s) as b\nconcat as joined\ntrim(value=$joined, range=$joined::missing)\n",
    );

    let error = compiler::compile(&workflow).expect_err("canonical layout diagnostic");
    assert_eq!(error.code, "E_UNKNOWN_TIMELINE_PLACEMENT");
    let layout = error.notes.join("\n");
    assert!(layout.contains("timeline layout for `$joined`"));
    assert!(layout.contains("├── <unnamed> (not directly addressable) [0s..1s)"));
    assert!(layout.contains("└── b [1s..2s)"));
}

#[test]
fn explicit_and_implicit_placement_names_do_not_shadow_each_other() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\n$a\nconcat as joined\ntrim(value=$joined, range=$joined::a)\n",
    );

    let error = compiler::compile(&workflow).expect_err("same spelling is ambiguous");
    assert_eq!(error.code, "E_AMBIGUOUS_TIMELINE_PLACEMENT");
    assert!(error.message.contains("2 placements named `a`"));
    assert_eq!(
        error
            .notes
            .join("\n")
            .matches("a (not directly addressable)")
            .count(),
        2
    );
}

#[test]
fn operation_owned_placement_names_reject_surviving_collisions() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"a.ppm\", 1s) as replacement\n  image(\"b.ppm\", 1s) as target\n} as edit\n$edit\nduring($edit::target) {\n  drop<Video>\n  image(\"c.ppm\", 500ms)\n} as revised\n",
    );

    let error = compiler::compile(&workflow).expect_err("reserved placement collision");
    assert_eq!(error.code, "E_TIMELINE_PLACEMENT_CONFLICT");
    assert!(error.message.contains("replacement"));
    assert!(error.notes.join("\n").contains("base timeline"));
}

#[test]
fn operation_owned_placement_name_may_replace_a_removed_collision() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"a.ppm\", 1s) as replacement\n  image(\"b.ppm\", 1s) as tail\n} as edit\n$edit\nduring($edit::replacement) {\n  drop<Video>\n  image(\"c.ppm\", 500ms)\n} as revised\ntrim(value=$revised, range=$revised::replacement)\n",
    );

    let compiled = compiler::compile(&workflow).expect("removed collision does not survive");
    assert_last_slice_range(&compiled, 0, 5);
}

#[test]
fn alias_can_address_joined_children_through_a_named_parent_root() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin as joined\nend = $joined::a::start + $joined::b::end\nduring(timeline=$joined, range=$joined::start..$end) { zoom_in(2%) }\n",
    );

    let compiled = compiler::compile(&workflow).expect("joined child alias");
    assert_eq!(
        compiled_document(&compiled)
            .operation("replace_range")
            .range(),
        (0, 20)
    );
}

#[test]
fn unnamed_composite_supports_direct_contextual_child_selectors() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\n{\n  image(\"a.ppm\", 1s) as a\n  image(\"b.ppm\", 1s) as b\n  concat\n}\nduring(range=$a::start..$b::end) { zoom_in(2%) }\n",
    );

    let compiled = compiler::compile(&workflow).expect("contextual unnamed composite selectors");
    assert_eq!(
        compiled.result_domain().expect("known domain").frames().0,
        20
    );
}

#[test]
fn unnamed_join_result_supplies_context_to_the_next_timeline_consumer() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin { concat }\ntrim(range=$a::start..$b::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("unnamed joined timeline context");
    assert_eq!(
        compiled.result_domain().expect("known domain").frames().0,
        20
    );
}

#[test]
fn join_without_body_concat_still_supplies_context_to_a_named_consumer() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin as joined\ntrim(value=$joined, range=$a::start..$b::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("body-concat finalizer supplies context");
    assert_last_slice_range(&compiled, 0, 20);
}

#[test]
fn join_body_has_no_aggregate_timeline_before_finalization() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin {\n  point = $a::start + $b::start\n  concat\n  trim(range=$a::start..$point)\n} as joined\n",
    );

    let error = compiler::compile(&workflow).expect_err("join body inputs remain separate roots");
    assert_eq!(error.code, "E_TIMELINE_ROOT_MISMATCH");
}

#[test]
fn join_body_can_create_a_contextual_root_before_finalization() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin {\n  concat\n  trim(range=$a::start..$b::end)\n} as joined\n",
    );

    let compiled = compiler::compile(&workflow).expect("body-created contextual root");
    assert_eq!(
        compiled.result_domain().expect("known domain").frames().0,
        20
    );
}

#[test]
fn contextual_selector_can_skip_unique_named_ancestors() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\nconcat as pair\nimage(\"c.ppm\", 1s) as c\nconcat as edit\ntrim(value=$edit, range=$a::start..$c::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("unique descendant shorthand");
    assert_last_slice_range(&compiled, 0, 30);
}

#[test]
fn contextual_selector_can_match_a_unique_nested_suffix_path() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\nconcat as pair\nimage(\"x.ppm\", 1s) as x\nconcat as section\nimage(\"c.ppm\", 1s) as c\nconcat as edit\ntrim(value=$edit, range=$pair::a::start..$c::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("unique nested suffix shorthand");
    assert_last_slice_range(&compiled, 0, 40);
}

#[test]
fn contextual_selector_rejects_an_ambiguous_descendant_name() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nclip { $a } as first_pair\nclip { $a } as second_pair\n{\n  $first_pair\n  $second_pair\n  concat\n} as edit\ntrim(value=$edit, range=$a::start..$edit::end)\n",
    );

    let error = compiler::compile(&workflow).expect_err("ambiguous descendant shorthand");
    assert_eq!(error.code, "E_AMBIGUOUS_TIMELINE_PLACEMENT");
    assert!(error.message.contains('a'));
}

#[test]
fn contextual_selector_handles_shared_layout_dags_without_expanding_occurrences() {
    let mut source = String::from(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"leaf.ppm\", 100ms) as leaf\ndrop<Video>\n",
    );
    let mut previous = "leaf".to_owned();
    for depth in 0..32 {
        let next = format!("level_{depth}");
        writeln!(
            source,
            "clip {{\n  ${previous}\n  ${previous}\n}} as {next}"
        )
        .expect("write shared layout level");
        previous = next;
    }
    writeln!(source, "${previous}").expect("write final reference");
    writeln!(
        source,
        "trim(value=${previous}, range=$leaf::start..$leaf::end)"
    )
    .expect("write contextual selector");
    let (_directory, workflow) = project(&source);

    let error = compiler::compile(&workflow).expect_err("shared leaf occurs many times");
    assert_eq!(error.code, "E_AMBIGUOUS_TIMELINE_PLACEMENT");
    assert!(error.message.contains("multiple placements"));
}

#[test]
fn contextual_selector_rejects_same_level_duplicate_names() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\n$a\nconcat as edit\ntrim(value=$edit, range=$a::start..$edit::end)\n",
    );

    let error = compiler::compile(&workflow).expect_err("same-level contextual ambiguity");
    assert_eq!(error.code, "E_AMBIGUOUS_TIMELINE_PLACEMENT");
    assert!(error.message.contains("matches multiple placements"));
}

#[test]
fn alias_cannot_borrow_an_unnamed_composite_as_its_parent_root() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\n{\n  image(\"a.ppm\", 1s) as a\n  image(\"b.ppm\", 1s) as b\n  concat\n}\nbad = $a::start + $b::end\nduring(range=$a::start..$bad) { zoom_in(2%) }\n",
    );

    let error = compiler::compile(&workflow).expect_err("aliases require an explicit parent root");
    assert_eq!(error.code, "E_TIMELINE_ROOT_MISMATCH");
}

#[test]
fn anonymous_concat_layers_do_not_change_named_child_paths() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\nconcat\nconcat as joined\ntrim(value=$joined, range=$joined::a::start..$joined::b::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("transparent anonymous concat");
    assert_last_slice_range(&compiled, 0, 20);
}

#[test]
fn join_body_anonymous_concat_does_not_change_named_child_paths() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin {\n  concat\n} as joined\ntrim(value=$joined, range=$joined::a::start..$joined::b::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("transparent join body concat");
    assert_last_slice_range(&compiled, 0, 20);
}

#[test]
fn anonymous_concat_regrouping_is_associative_for_layout_paths() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\nconcat\nimage(\"c.ppm\", 1s) as c\nconcat as joined\ntrim(value=$joined, range=$joined::a::start..$joined::c::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("associative anonymous concat layout");
    assert_last_slice_range(&compiled, 0, 30);
}

#[test]
fn explicit_named_concat_remains_a_selector_boundary() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\nconcat as pair\nimage(\"c.ppm\", 1s) as c\nconcat as joined\ntrim(value=$joined, range=$joined::pair::a::start..$joined::c::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("named concat boundary");
    assert_last_slice_range(&compiled, 0, 30);
}

#[test]
fn explicit_named_concat_cannot_be_skipped_in_a_selector_path() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\nconcat as pair\nimage(\"c.ppm\", 1s) as c\nconcat as joined\ntrim(value=$joined, range=$joined::a)\n",
    );

    let error = compiler::compile(&workflow).expect_err("named boundary must not flatten");
    assert_eq!(error.code, "E_UNKNOWN_TIMELINE_PLACEMENT");
}

#[test]
fn join_body_names_work_on_either_side_of_anonymous_concat() {
    for body in ["concat as j\n  concat", "concat\n  concat as j"] {
        let source = format!(
            "clipasm 1\nconfig {{ video {{ width = 64\nheight = 64\nfps = 10 }} }}\nimage(\"a.ppm\", 1s) as a\nimage(\"b.ppm\", 1s) as b\njoin {{\n  {body}\n}}\nend = $j::a::start + $j::b::end\ntrim(value=$j, range=$j::start..$end)\n"
        );
        let (_directory, workflow) = project(&source);
        let compiled = compiler::compile(&workflow).expect("stable join body name");
        assert_last_slice_range(&compiled, 0, 20);
    }
}

#[test]
fn anonymous_transition_layout_flattens_into_an_outer_concat() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s)\nimage(\"b.ppm\", 1s)\ncrossfade(400ms)\nimage(\"c.ppm\", 1s)\nconcat as edit\ntrim(value=$edit, range=$edit::overlap)\n",
    );

    let compiled = compiler::compile(&workflow).expect("transparent unnamed transition");
    assert_last_slice_range(&compiled, 6, 10);
}

#[test]
fn named_transition_layout_remains_nested_in_an_outer_concat() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s)\nimage(\"b.ppm\", 1s)\ncrossfade(400ms) as transition\nimage(\"c.ppm\", 1s)\nconcat as edit\ntrim(value=$edit, range=$edit::transition::overlap)\n",
    );

    let compiled = compiler::compile(&workflow).expect("named transition boundary");
    assert_last_slice_range(&compiled, 6, 10);
}

#[test]
fn anonymous_replacement_layout_flattens_into_an_outer_concat() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 2s)\nduring(1s..2s) {\n  drop<Video>\n  image(\"b.ppm\", 500ms)\n}\nimage(\"c.ppm\", 1s)\nconcat as edit\ntrim(value=$edit, range=$edit::replacement)\n",
    );

    let compiled = compiler::compile(&workflow).expect("transparent unnamed replacement");
    assert_last_slice_range(&compiled, 10, 15);
}

#[test]
fn media_dependent_marker_range_compiles_without_reading_the_video() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nvideo(\"missing.mkv\") as source\ntrim(range=($source::start + 200ms)..($source::end - 300ms))\n",
    );

    let compiled = compiler::compile(&workflow).expect("deferred marker compilation");
    assert!(compiled.result_domain().is_none());
    assert!(compiled_document(&compiled).has_operation("slice"));
}

#[test]
fn media_dependent_during_inherits_its_requested_extent_without_reading_media() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nvideo(\"missing.mkv\") as source\nduring(range=($source::start + 200ms)..($source::end - 300ms)) {\n  drop<Video>\n  image(\"replacement.ppm\")\n}\n",
    );

    let compiled = compiler::compile(&workflow).expect("deferred during compilation");
    assert!(compiled.result_domain().is_none());
    let document = compiled_document(&compiled);
    let operations = document.operation_names();
    assert!(operations.contains(&"deferred_image_video"));
    assert!(operations.contains(&"replace_range"));
}

#[test]
fn crossfade_exposes_before_after_and_overlap_regions() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s)\nimage(\"b.ppm\", 1s)\ncrossfade(400ms) as transition\ntrim(range=$transition::overlap)\n",
    );

    let compiled = compiler::compile(&workflow).expect("crossfade overlap marker");
    assert_eq!(
        compiled.result_domain().expect("known overlap").frames().0,
        4
    );
    assert_eq!(
        compiled_document(&compiled).operation("slice").range(),
        (6, 10)
    );
}

#[test]
fn crossfade_overlap_middle_uses_exact_coordinate_arithmetic() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s)\nimage(\"b.ppm\", 1s)\ncrossfade(400ms) as transition\ntrim(range=$transition::overlap::middle..$transition::overlap::end)\n",
    );

    let compiled = compiler::compile(&workflow).expect("crossfade overlap midpoint");
    assert_eq!(
        compiled
            .result_domain()
            .expect("known half-overlap")
            .frames()
            .0,
        2
    );
}

#[test]
fn crossfade_input_regions_retain_nested_marker_layouts() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"a.ppm\", 1s) as title\n} as first\n$first\nimage(\"b.ppm\", 1s)\ncrossfade(400ms) as transition\ntrim(range=$transition::before::title)\n",
    );

    let compiled = compiler::compile(&workflow).expect("nested crossfade input marker");
    assert_eq!(
        compiled.result_domain().expect("known title").frames().0,
        10
    );
}

#[test]
fn flash_cut_exposes_sequential_before_and_after_regions() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"a.ppm\", 1s)\nimage(\"b.ppm\", 2s)\nflash_cut(400ms) as transition\ntrim(range=$transition::after)\n",
    );

    let compiled = compiler::compile(&workflow).expect("flash-cut after marker");
    assert_eq!(
        compiled.result_domain().expect("known after").frames().0,
        20
    );
}

#[test]
fn during_splices_unaffected_base_placements_and_rebases_the_suffix() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"intro.ppm\", 1s) as intro\n  image(\"main.ppm\", 2s) as main\n  image(\"credits.ppm\", 1s) as credits\n} as edit\n$edit\nduring(range=$edit::main) {\n  drop<Video>\n  image(\"replacement.ppm\", 1s)\n} as revised\ntrim(range=$revised::credits)\n",
    );

    let compiled = compiler::compile(&workflow).expect("spliced suffix marker");
    assert_eq!(
        compiled.result_domain().expect("known credits").frames().0,
        10
    );
    assert_eq!(
        compiled_document(&compiled).last_operation("slice").range(),
        (20, 30)
    );
}

#[test]
fn during_drops_base_placements_intersecting_the_replaced_range() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"intro.ppm\", 1s) as intro\n  image(\"main.ppm\", 2s) as main\n  image(\"credits.ppm\", 1s) as credits\n} as edit\n$edit\nduring(range=$edit::main) {\n  drop<Video>\n  image(\"replacement.ppm\", 1s)\n} as revised\ntrim(range=$revised::main)\n",
    );

    let error = compiler::compile(&workflow).expect_err("replaced placement must disappear");
    assert_eq!(error.code, "E_UNKNOWN_TIMELINE_PLACEMENT");
}

#[test]
fn during_body_seed_preserves_the_selected_nested_layout() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"intro.ppm\", 1s) as intro\n  clip {\n    image(\"a.ppm\", 1s) as a\n    image(\"b.ppm\", 1s) as b\n  } as main\n  $main\n  image(\"outro.ppm\", 1s) as outro\n} as edit\n$edit\nduring(range=$edit::main) {\n  trim(range=$b::start..$b::end)\n}\n",
    );

    let compiled = compiler::compile(&workflow).expect("selected layout is available in the body");
    assert_eq!(
        compiled.result_domain().expect("known domain").frames().0,
        30
    );
}

#[test]
fn no_op_during_preserves_selected_children_under_replacement() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"intro.ppm\", 1s) as intro\n  image(\"main.ppm\", 2s) as main\n  image(\"outro.ppm\", 1s) as outro\n} as edit\n$edit\nduring(range=$edit::main) as revised\ndrop<Video>\ntrim(value=$revised, range=$revised::replacement::main)\n",
    );

    let compiled = compiler::compile(&workflow).expect("selected child survives under replacement");
    assert_eq!(
        compiled
            .result_domain()
            .expect("known selected child")
            .frames()
            .0,
        20
    );
}

#[test]
fn during_layout_orders_replacement_between_surviving_siblings() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"intro.ppm\", 1s) as intro\n  image(\"main.ppm\", 2s) as main\n  image(\"outro.ppm\", 1s) as outro\n} as edit\n$edit\nduring(range=$edit::main) as revised\ntrim(value=$revised, range=$revised::missing)\n",
    );

    let error = compiler::compile(&workflow).expect_err("unknown placement shows canonical order");
    assert_eq!(error.code, "E_UNKNOWN_TIMELINE_PLACEMENT");
    let layout = error
        .notes
        .iter()
        .find(|note| note.contains("timeline layout for `$revised`"))
        .expect("layout note");
    let intro = layout.find("intro").expect("intro placement");
    let replacement = layout.find("replacement").expect("replacement placement");
    let outro = layout.find("outro").expect("outro placement");
    assert!(intro < replacement && replacement < outro, "{layout}");
}

#[test]
fn during_exposes_the_replacement_and_its_nested_layout() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nimage(\"base.ppm\", 3s)\nduring(1s..2s) {\n  drop<Video>\n  clip {\n    image(\"lead.ppm\", 500ms) as lead\n    image(\"body.ppm\", 1500ms) as body\n  } as inserted\n  $inserted\n} as revised\ntrim(range=$revised::replacement::lead)\n",
    );

    let compiled = compiler::compile(&workflow).expect("nested replacement marker");
    assert_eq!(compiled.result_domain().expect("known lead").frames().0, 5);
}

#[test]
fn during_symbolic_replacement_rebases_later_placements_exactly() {
    let (_directory, workflow) = project(
        "clipasm 1\nconfig { video { width = 64\nheight = 64\nfps = 10 } }\nclip {\n  image(\"intro.ppm\", 1s) as intro\n  video(\"missing.mkv\") as main\n  image(\"credits.ppm\", 1s) as credits\n} as edit\n$edit\nduring(range=$edit::main) {\n  drop<Video>\n  image(\"replacement.ppm\", 1s)\n} as revised\ntrim(range=$revised::credits)\n",
    );

    let compiled = compiler::compile(&workflow).expect("symbolic replacement splice");
    assert_eq!(
        compiled_document(&compiled).last_operation("slice").range(),
        (20, 30)
    );
}