forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB978e685Tahinli3 months ago627 commits

saveload.rs956 lines, 38.6 KiBHistoryRawDownload
1//! Saving a project and opening it again: the timeline that comes back is the
2//! one that went in, a folder of media plus its `.edith` can be moved
3//! anywhere, and every way the files on disk can have changed underneath is a
4//! refusal that says which file and why.
5//!
6//! ```text
7//! cargo test -p engine --release --test saveload -- --nocapture
8//! ```
9
10use std::path::{Path, PathBuf};
11
12use engine::PlaybackSession;
13use engine::project::{Lane, Source};
14use engine::scratch::Scratch;
15
16fn asset(name: &str) -> PathBuf {
17 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
18 .join("../../assets")
19 .join(name)
20}
21
22/// A fresh directory to litter, deleted when the test that holds it ends.
23fn scratch(name: &str) -> Scratch {
24 Scratch::dir(&format!("ve_saveload_{name}"))
25}
26
27fn copy_in(dir: &Path, name: &str) -> PathBuf {
28 let to = dir.join(name);
29 std::fs::copy(asset(name), &to).expect("copy the fixture");
30 to
31}
32
33/// The whole of what a picked audio stream has to survive: a file's second
34/// language goes on the timeline beside its first, the project says so on
35/// disk, and reopening it plays the same two streams. This is the file-level
36/// half of what a library row does -- `place_stream_at` is the one door that
37/// row goes through.
38#[test]
39fn a_picked_audio_stream_lands_saves_and_comes_back() {
40 let dir = scratch("streams");
41 let media = copy_in(&dir, "test_multilang.mp4");
42 let mut session = PlaybackSession::open(&media).expect("open the fixture");
43 session.set_gain(0.0); // silent like the rest of the suites
44 let end = session.timeline_duration();
45
46 // Stream 1 is the French track, same rate and layout as stream 0, so it
47 // can join this timeline; it lands as a second source of the same file.
48 assert!(
49 session
50 .place_stream_at(end, &media, 1, None)
51 .expect("stream 1 matches the timeline")
52 );
53 assert_eq!(
54 session
55 .sources()
56 .iter()
57 .map(|s| s.audio_stream)
58 .collect::<Vec<_>>(),
59 [0, 1],
60 "the same file twice, on two streams"
61 );
62 assert_eq!(session.timeline_duration(), end * 2.0);
63
64 let path = dir.join("langs.edith");
65 session.save_project(&path).expect("save");
66 let text = std::fs::read_to_string(&path).expect("read back");
67 assert!(
68 text.contains("source 0 test_multilang.mp4")
69 && text.contains("source 1 test_multilang.mp4"),
70 "the streams did not reach the file:\n{text}"
71 );
72
73 let loaded = PlaybackSession::open_project(&path).expect("reopen the project");
74 assert_eq!(loaded.sources(), session.sources());
75 assert_eq!(loaded.timeline_duration(), session.timeline_duration());
76
77 // And the refusals, in the engine's own words: a stream that cannot share
78 // one output device with the timeline, and a file that is not on it.
79 let other = copy_in(&dir, "test_multiaudio.mp4");
80 let mut session = PlaybackSession::open(&other).expect("open the fixture");
81 session.set_gain(0.0);
82 // Its rate would be conformed now; its *layout* is what one output device
83 // cannot have two of, and the refusal says which.
84 let err = session
85 .place_stream_at(0.0, &other, 1, None)
86 .expect_err("a mono stream cannot join a stereo timeline")
87 .to_string();
88 assert_eq!(err, "audio 1 ch does not match the timeline's 2 ch");
89 assert!(
90 session
91 .place_stream_at(0.0, &media, 1, None)
92 .expect_err("a file that is not a source")
93 .to_string()
94 .contains("not on this timeline")
95 );
96 assert_eq!(session.sources().len(), 1, "a refusal added a source");
97 std::fs::remove_dir_all(&dir).expect("cleanup");
98}
99
100/// Both stand-in switches through the front door: what the picture is cut on
101/// and whether an import makes one at all are two settings, saved and reopened
102/// as the pair they are -- and a session that was never told keeps making them,
103/// which is what every project did before there was a switch.
104#[test]
105fn both_proxy_switches_survive_a_save_and_a_reopen() {
106 let dir = scratch("proxyswitches");
107 let media = copy_in(&dir, "test_av.mp4");
108 let mut session = PlaybackSession::open(&media).expect("open the fixture");
109 session.set_gain(0.0);
110 assert!(session.auto_proxies(), "a fresh project makes them by itself");
111 assert!(!session.proxies(), "and is cut on the film itself");
112
113 session.set_proxies(true);
114 session.set_auto_proxies(false);
115 let path = dir.join("switches.edith");
116 session.save_project(&path).expect("save");
117 let text = std::fs::read_to_string(&path).expect("read back");
118 assert!(text.contains("proxy on\nautoproxy off\n"), "{text}");
119
120 let loaded = PlaybackSession::open_project(&path).expect("reopen");
121 assert!(loaded.proxies(), "cut on the stand-ins came back");
122 assert!(!loaded.auto_proxies(), "and so did making none of them");
123
124 // ...and the default is the line left out, which is what a v12 file is.
125 session.set_auto_proxies(true);
126 session.save_project(&path).expect("save");
127 let text = std::fs::read_to_string(&path).expect("read back");
128 assert!(!text.contains("autoproxy"), "on is written by saying nothing");
129 assert!(
130 PlaybackSession::open_project(&path)
131 .expect("reopen")
132 .auto_proxies()
133 );
134 std::fs::remove_dir_all(&dir).expect("cleanup");
135}
136
137/// The export card's encoder pick through the same front door: which seat a
138/// delivery is written on is the project's, so it survives a save and a reopen
139/// -- and a project nobody picked for exports on whatever this machine has,
140/// which is what every project did before the row existed.
141#[test]
142fn the_encoder_seat_survives_a_save_and_a_reopen() {
143 use engine::export::EncoderSeat;
144
145 let dir = scratch("encoderseat");
146 let media = copy_in(&dir, "test_av.mp4");
147 let mut session = PlaybackSession::open(&media).expect("open the fixture");
148 session.set_gain(0.0);
149 assert_eq!(
150 session.encoder_seat(),
151 EncoderSeat::Auto,
152 "a fresh project takes the seat this machine has"
153 );
154
155 let path = dir.join("seat.edith");
156 for seat in [EncoderSeat::Software, EncoderSeat::Hardware] {
157 session.set_encoder_seat(seat);
158 session.save_project(&path).expect("save");
159 let text = std::fs::read_to_string(&path).expect("read back");
160 assert!(text.contains(&format!("encoder {}\n", seat.name())), "{text}");
161 assert_eq!(
162 PlaybackSession::open_project(&path)
163 .expect("reopen")
164 .encoder_seat(),
165 seat
166 );
167 }
168
169 // ...and the default is the line left out, which is what a v13 file is.
170 session.set_encoder_seat(EncoderSeat::Auto);
171 session.save_project(&path).expect("save");
172 let text = std::fs::read_to_string(&path).expect("read back");
173 assert!(!text.contains("encoder"), "auto is written by saying nothing");
174 assert_eq!(
175 PlaybackSession::open_project(&path)
176 .expect("reopen")
177 .encoder_seat(),
178 EncoderSeat::Auto
179 );
180 std::fs::remove_dir_all(&dir).expect("cleanup");
181}
182
183/// Why a trim's tail stops where the file ends: a clip reaching past the last
184/// frame of its source is a project that will not open again (`open_project`
185/// refuses it by name). Pulled in on both ends and then dragged out as far as
186/// the pointer can ask for, the save still comes back as itself.
187#[test]
188fn a_trimmed_timeline_saves_and_opens_again() {
189 use engine::project::Edge;
190
191 let dir = scratch("trim");
192 let media = copy_in(&dir, "test_av.mp4");
193 let mut session = PlaybackSession::open(&media).expect("open the fixture");
194 session.set_gain(0.0);
195 let whole = session.clip_at(0).expect("one clip");
196
197 assert!(session.trim_clip(Lane::V1, 0, Edge::End, whole.end() / 2));
198 assert!(session.trim_clip(Lane::V1, 0, Edge::Start, 5));
199 let path = dir.join("trimmed.edith");
200 session.save_project(&path).expect("save");
201 let loaded = PlaybackSession::open_project(&path).expect("a trimmed project opens");
202 assert_eq!(
203 loaded
204 .clip_at(0)
205 .map(|c| (c.start, c.in_frame, c.out_frame)),
206 Some((5, 5, whole.end() / 2)),
207 "the trimmed range is what came back"
208 );
209
210 // A hand that keeps pulling: the tail stops at the file's own last frame,
211 // and the save of *that* opens too.
212 assert!(session.trim_clip(Lane::V1, 0, Edge::End, u32::MAX));
213 assert_eq!(
214 session.clip_at(0).expect("still one clip").out_frame,
215 whole.out_frame,
216 "as far as the file goes and not one frame further"
217 );
218 session.save_project(&path).expect("save");
219 PlaybackSession::open_project(&path).expect("and it still opens");
220 std::fs::remove_dir_all(&dir).expect("cleanup");
221}
222
223/// Two files, four clips, one of them deleted, playhead at 3 s -- a timeline
224/// with something to lose in every field.
225fn edited(dir: &Path) -> PlaybackSession {
226 let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open");
227 // Imported into the library, then dragged onto the end -- the two acts a
228 // second file on the timeline takes.
229 let second = copy_in(dir, "test_av2.mp4");
230 session.import(&second).expect("import the second file");
231 let end = session.timeline_duration();
232 assert!(
233 session
234 .place_stream_at(end, &second, 0, None)
235 .expect("a file just imported is on this timeline"),
236 "drag it onto the end"
237 );
238 assert!(session.cut_at(2.0), "cut inside the first file");
239 assert!(session.cut_at(6.5), "cut inside the second file");
240 assert!(
241 session.delete_clip(Lane::V1, 1),
242 "drop the second half of the first"
243 );
244 session.seek(3.0);
245 assert_eq!(session.timeline_duration(), 6.0);
246 session
247}
248
249/// What has to survive a round trip, all of it public API.
250fn shape(session: &PlaybackSession) -> (Vec<(f64, f64, usize)>, Vec<Source>, f64) {
251 (
252 session.clip_spans_by_source(),
253 session.sources().to_vec(),
254 session.timeline_duration(),
255 )
256}
257
258/// The version-1 promise: a file written before the lanes existed still opens,
259/// as one grouped video+audio pair per clip laid end to end, and saving it
260/// again writes version 7 -- which reopens as the same timeline.
261#[test]
262fn a_version_1_project_loads_fully_grouped_and_saves_as_version_7() {
263 let dir = scratch("v1");
264 copy_in(&dir, "test_av.mp4");
265 let path = dir.join("old.edith");
266 // 60 frames of picture from two clips: [0,30) then [60,90) of the source.
267 std::fs::write(
268 &path,
269 "edith 1\nplayhead 45\nsource test_av.mp4\nclip 0 30 0\nclip 60 90 0\n",
270 )
271 .expect("write a v1 file");
272
273 let loaded = PlaybackSession::open_project(&path).expect("a v1 file still opens");
274 let fps = loaded.meta().frame_rate;
275 assert_eq!(
276 loaded.clip_spans_by_source(),
277 vec![(0.0, 30.0 / fps, 0), (30.0 / fps, 30.0 / fps, 0)],
278 "v1 clips queue up, the second starting where the first ended"
279 );
280 assert_eq!(
281 loaded.lane_spans_by_source(engine::project::Lane::A1),
282 loaded.clip_spans_by_source(),
283 "both lanes, fully grouped: a v1 timeline had no holes"
284 );
285 assert!(
286 (loaded.now() - 45.0 / fps).abs() < 1.0 / fps,
287 "playhead kept"
288 );
289
290 // Saved again it is v7, and v7 round-trips to the same timeline.
291 let v2 = dir.join("new.edith");
292 loaded.save_project(&v2).expect("save");
293 let text = std::fs::read_to_string(&v2).expect("read back");
294 assert!(text.starts_with("edith 20\n"), "{text}");
295 assert!(
296 text.contains("\nresolution 1280 720\n"),
297 "a project with no resolution of its own is saved at source 0's: {text}"
298 );
299 assert_eq!(
300 text.lines().filter(|l| l.starts_with("video ")).count(),
301 2,
302 "{text}"
303 );
304 assert_eq!(
305 text.lines().filter(|l| l.starts_with("audio ")).count(),
306 2,
307 "{text}"
308 );
309 let again = PlaybackSession::open_project(&v2).expect("open the v2 file");
310 assert_eq!(shape(&again), shape(&loaded));
311 // ...and re-saving it is byte-identical, so a round trip is stable.
312 let third = dir.join("third.edith");
313 again.save_project(&third).expect("save");
314 assert_eq!(std::fs::read(&third).expect("read"), text.as_bytes());
315
316 // And the same promise one version on: a v2 file names no audio stream and
317 // opens on stream 0, which is the only one that dialect could play.
318 let v2 = dir.join("v2.edith");
319 std::fs::write(
320 &v2,
321 "edith 2\nplayhead 0\nsource test_av.mp4\nvideo 0 0 30 0 0\naudio 0 0 30 0 0\n",
322 )
323 .expect("write a v2 file");
324 let loaded = PlaybackSession::open_project(&v2).expect("a v2 file still opens");
325 assert_eq!(loaded.sources().len(), 1);
326 assert_eq!(loaded.sources()[0].audio_stream, 0);
327 loaded.save_project(&v2).expect("save");
328 assert!(
329 std::fs::read_to_string(&v2)
330 .expect("read back")
331 .contains("source 0 test_av.mp4"),
332 "a re-saved v2 project says the stream it always meant"
333 );
334}
335
336/// What version 4 is for: a project file holds any number of lanes, in the
337/// order they are displayed in, and one holding nothing is still a lane. The
338/// timeline is built by hand because the format is what this checks -- no UI
339/// adds a lane yet -- and the file it writes back has to be the one it read.
340#[test]
341fn a_version_4_project_holds_more_than_two_lanes() {
342 use engine::project::{Lane, LaneKind};
343
344 let dir = scratch("v4_lanes");
345 copy_in(&dir, "test_av.mp4");
346 let path = dir.join("many.edith");
347 // V1, A1, V2, A2 in display order: a take grouped across V1 and A2 (link 3,
348 // one span on two lanes that are not a pair), a lone clip on V2, and an
349 // empty A1 that has only its own line to say it is there.
350 let text = "edith 4\nplayhead 0\nsource 0 test_av.mp4\n\
351 video 1 0 0 30 0 3\naudio 1\nvideo 2 40 0 20 0 -\naudio 2 0 0 30 0 3\n";
352 std::fs::write(&path, text).expect("write a v4 file");
353
354 let loaded = PlaybackSession::open_project(&path).expect("a four-lane project opens");
355 let v2 = Lane::new(LaneKind::Video, 1);
356 let a2 = Lane::new(LaneKind::Audio, 1);
357 assert_eq!(loaded.lane_clips(Lane::V1).len(), 1);
358 assert!(loaded.lane_clips(Lane::A1).is_empty(), "A1 holds nothing");
359 assert_eq!(
360 loaded
361 .lane_clips(v2)
362 .iter()
363 .map(|c| (c.start, c.end()))
364 .collect::<Vec<_>>(),
365 vec![(40, 60)],
366 "V2 is a lane of its own, gap and all"
367 );
368 assert_eq!(
369 loaded.lane_clips(a2)[0].link,
370 loaded.lane_clips(Lane::V1)[0].link,
371 "the group spans V1 and A2, which are not a pair"
372 );
373 assert_eq!(loaded.timeline_duration(), 60.0 / loaded.meta().frame_rate);
374
375 // ...and saving it writes the v7 twin of what it read: the lane list, its
376 // order and the empty lane all survive, each clip now saying it plays flat,
377 // ungraded and fitted, under a project resolution taken from the media.
378 let again = dir.join("again.edith");
379 loaded.save_project(&again).expect("save");
380 assert_eq!(
381 std::fs::read_to_string(&again).expect("read back"),
382 "edith 20\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
383 video 1 0 0 30 0 3 - - fit 1000 0 0 0 -\naudio 1\n\
384 video 2 40 0 20 0 - - - fit 1000 0 0 0 -\naudio 2 0 0 30 0 3 - - fit 1000 0 0 0 -\n",
385 "a four-lane project is written as it was read, three versions on"
386 );
387}
388
389/// What version 5 is for: a clip carries equalizer settings, the file names
390/// them once for however many clips share them, and the whole thing survives
391/// the engine's own door -- open, save, every curve and index of it intact one
392/// version on.
393#[test]
394fn a_version_5_project_carries_per_clip_equalizers() {
395 let dir = scratch("v5_eq");
396 copy_in(&dir, "test_av.mp4");
397 let path = dir.join("eq.edith");
398 // Two clips on one curve, one on another, one flat.
399 let text = "edith 5\nplayhead 0\nsource 0 test_av.mp4\n\
400 eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\
401 eq 12000.0:6.25:0.5:hs\n\
402 video 1 0 0 30 0 - 0\nvideo 1 30 30 60 0 - 1\n\
403 audio 1 0 0 30 0 - 0\naudio 1 30 30 60 0 - -\n";
404 std::fs::write(&path, text).expect("write a v5 file");
405
406 let loaded = PlaybackSession::open_project(&path).expect("a v5 project opens");
407 assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2);
408 let again = dir.join("again.edith");
409 loaded.save_project(&again).expect("save");
410 assert_eq!(
411 std::fs::read_to_string(&again).expect("read back"),
412 "edith 20\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
413 eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\
414 eq 12000.0:6.25:0.5:hs\n\
415 video 1 0 0 30 0 - 0 - fit 1000 0 0 0 -\nvideo 1 30 30 60 0 - 1 - fit 1000 0 0 0 -\n\
416 audio 1 0 0 30 0 - 0 - fit 1000 0 0 0 -\naudio 1 30 30 60 0 - - - fit 1000 0 0 0 -\n",
417 "the equalizer table and every clip's index survive a round trip"
418 );
419}
420
421/// What version 6 is for, the same claim one version on: a clip carries a
422/// colour grade beside its equalizer and the file names each once for however
423/// many clips share it. A format bump necessarily rewrites the file, so what is
424/// asserted here is the v7 twin of what was read; the byte-identity claim lives
425/// in `a_version_7_project_carries_a_resolution_and_fit_policies` below.
426#[test]
427fn a_version_6_project_carries_per_clip_colours() {
428 let dir = scratch("v6_color");
429 copy_in(&dir, "test_av.mp4");
430 let path = dir.join("color.edith");
431 // Two clips on one grade, one on another, one ungraded -- and an equalizer
432 // on a clip that is graded too, so the two tables are seen not to collide.
433 let text = "edith 6\nplayhead 0\nsource 0 test_av.mp4\n\
434 eq 80.0:-3.0:0.707:ls\n\
435 color 0.1:1.2:0.9:-0.3\n\
436 color -0.25:1.0:0.0:0.5\n\
437 video 1 0 0 30 0 - 0 0\nvideo 1 30 30 60 0 - - 1\n\
438 audio 1 0 0 30 0 - - 0\naudio 1 30 30 60 0 - - -\n";
439 std::fs::write(&path, text).expect("write a v6 file");
440
441 let loaded = PlaybackSession::open_project(&path).expect("a v6 project opens");
442 assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2);
443 let again = dir.join("again.edith");
444 loaded.save_project(&again).expect("save");
445 assert_eq!(
446 std::fs::read_to_string(&again).expect("read back"),
447 "edith 20\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
448 eq 80.0:-3.0:0.707:ls\n\
449 color 0.1:1.2:0.9:-0.3\n\
450 color -0.25:1.0:0.0:0.5\n\
451 video 1 0 0 30 0 - 0 0 fit 1000 0 0 0 -\nvideo 1 30 30 60 0 - - 1 fit 1000 0 0 0 -\n\
452 audio 1 0 0 30 0 - - 0 fit 1000 0 0 0 -\naudio 1 30 30 60 0 - - - fit 1000 0 0 0 -\n",
453 "the colour table and every clip's index survive a round trip"
454 );
455 // A dialect that could not say either one means the defaults, and those are
456 // what such a project always was: the media's own size, nothing letterboxed.
457 assert_eq!(loaded.resolution(), (1280, 720));
458 assert_eq!(
459 loaded.fit_of(engine::project::Lane::V1, 0),
460 engine::scale::FitPolicy::Fit
461 );
462}
463
464/// What version 7 is for: the project's picture size is its own, not source 0's,
465/// and each clip says how it meets it. Both survive the engine's door byte for
466/// byte -- and a project whose resolution is nobody's media size is exactly the
467/// case that could not be written down before.
468#[test]
469fn a_version_7_project_carries_a_resolution_and_fit_policies() {
470 let dir = scratch("v7_resolution");
471 copy_in(&dir, "test_av.mp4");
472 let path = dir.join("mixed.edith");
473 let text = "edith 7\nplayhead 0\nresolution 960 720\nsource 0 test_av.mp4\n\
474 video 1 0 0 30 0 - - - fill\nvideo 1 30 30 60 0 - - - center\n\
475 audio 1 0 0 60 0 - - - fit\n";
476 std::fs::write(&path, text).expect("write a v7 file");
477
478 let loaded = PlaybackSession::open_project(&path).expect("a v7 project opens");
479 assert_eq!(
480 loaded.resolution(),
481 (960, 720),
482 "the project's own size, which is not the media's 1280x720"
483 );
484 assert_eq!(
485 loaded.native_resolution(),
486 (1280, 720),
487 "...and the media's own size is still known"
488 );
489 let v1 = engine::project::Lane::V1;
490 assert_eq!(loaded.fit_of(v1, 0), engine::scale::FitPolicy::Fill);
491 assert_eq!(loaded.fit_of(v1, 1), engine::scale::FitPolicy::Center);
492
493 let again = dir.join("again.edith");
494 loaded.save_project(&again).expect("save");
495 assert_eq!(
496 std::fs::read_to_string(&again).expect("read back"),
497 "edith 20\nplayhead 0\nresolution 960 720\nfps 30.0\nsource 0 test_av.mp4\n\
498 video 1 0 0 30 0 - - - fill 1000 0 0 0 -\nvideo 1 30 30 60 0 - - - center 1000 0 0 0 -\n\
499 audio 1 0 0 60 0 - - - fit 1000 0 0 0 -\n",
500 "a v7 project is written back as the v8 it now is: the same clips, each \
501 saying it plays at real time, which is what it always played at"
502 );
503
504 // The refusals the new grammar adds, each naming its line.
505 for (bad, want) in [
506 (
507 "edith 7\nresolution 0 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n",
508 "0x720 is not a picture",
509 ),
510 (
511 "edith 7\nresolution 960 720\nresolution 960 720\nsource 0 test_av.mp4\n\
512 video 1 0 0 30 0 - - - fit\n",
513 "resolution belongs once",
514 ),
515 // Past 8K is refused by the same bound the keystroke has: unbounded,
516 // this line reached `open_black` and panicked on a capacity overflow.
517 (
518 "edith 7\nresolution 4294967295 4294967295\nsource 0 test_av.mp4\n\
519 video 1 0 0 30 0 - - - fit\n",
520 "4294967295x4294967295 is not a picture",
521 ),
522 (
523 "edith 7\nresolution 7681 1080\nsource 0 test_av.mp4\n\
524 video 1 0 0 30 0 - - - fit\n",
525 "7681x1080 is not a picture",
526 ),
527 (
528 "edith 7\nresolution 960 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - squish\n",
529 "not a fit policy",
530 ),
531 ] {
532 let path = dir.join("bad.edith");
533 std::fs::write(&path, bad).expect("write");
534 let err = PlaybackSession::open_project(&path)
535 .err()
536 .expect("a malformed v7 file must be refused")
537 .to_string();
538 assert!(err.contains(want), "wanted {want:?}, got {err:?}");
539 }
540}
541
542#[test]
543fn a_saved_project_reopens_as_the_same_timeline() {
544 let dir = scratch("round_trip");
545 let saved = edited(&dir);
546 let path = dir.join("edit.edith");
547 saved.save_project(&path).expect("save");
548
549 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
550 assert_eq!(shape(&loaded), shape(&saved), "clips, sources and duration");
551 assert!(
552 (loaded.now() - saved.now()).abs() < 1.0 / loaded.meta().frame_rate,
553 "playhead landed at {:.3}s, saved at {:.3}s",
554 loaded.now(),
555 saved.now()
556 );
557 assert!(!loaded.is_playing(), "a loaded project starts paused");
558 assert!(!loaded.undo(), "history is not saved");
559
560 // Saving what was loaded reproduces the file byte for byte -- the format
561 // has no state the round trip drops.
562 let again = dir.join("again.edith");
563 loaded
564 .save_project(&again)
565 .expect("save the loaded project");
566 assert_eq!(
567 std::fs::read(&path).unwrap(),
568 std::fs::read(&again).unwrap(),
569 "save -> load -> save is not a fixed point"
570 );
571 assert!(!path.with_extension("edith.part").exists(), "left a .part");
572}
573
574#[test]
575fn a_folder_of_media_and_its_project_can_be_moved() {
576 let from = scratch("relocate_from");
577 let saved = edited(&from);
578 saved.save_project(&from.join("edit.edith")).expect("save");
579 drop(saved);
580
581 let to = scratch("relocate_to");
582 for name in ["test_av.mp4", "test_av2.mp4", "edit.edith"] {
583 std::fs::copy(from.join(name), to.join(name)).expect("copy");
584 }
585 // The originals go away entirely: nothing absolute can be resolving here.
586 std::fs::remove_dir_all(&from).expect("remove the original folder");
587
588 let moved = PlaybackSession::open_project(&to.join("edit.edith")).expect("open the copy");
589 assert_eq!(moved.timeline_duration(), 6.0);
590 assert!(
591 moved.sources().iter().all(|s| s.path.starts_with(&to)),
592 "sources did not follow the folder: {:?}",
593 moved.sources()
594 );
595}
596
597#[test]
598fn orphan_sources_are_not_written() {
599 let dir = scratch("orphans");
600 let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open");
601 // An import that was never dragged onto a lane *is* the orphan: a library
602 // row with no clip naming it. (So is one whose clip was undone -- the same
603 // entry, reached the other way.)
604 session
605 .import(&copy_in(&dir, "test_av2.mp4"))
606 .expect("import");
607 assert_eq!(
608 session.sources().len(),
609 2,
610 "the orphan entry stays in-session"
611 );
612
613 let path = dir.join("orphan.edith");
614 session.save_project(&path).expect("save");
615 let text = std::fs::read_to_string(&path).expect("read back");
616 assert_eq!(
617 text.lines().filter(|l| l.starts_with("source ")).count(),
618 1,
619 "the orphan reached the file:\n{text}"
620 );
621
622 // And the load survives the orphan's file disappearing, which is the whole
623 // point of pruning it.
624 std::fs::remove_file(dir.join("test_av2.mp4")).expect("unlink the orphan");
625 let loaded = PlaybackSession::open_project(&path).expect("open the project");
626 assert_eq!(loaded.sources().len(), 1);
627 assert_eq!(loaded.timeline_duration(), 5.0);
628}
629
630#[test]
631fn a_source_that_vanished_is_refused_by_name() {
632 let dir = scratch("missing");
633 let session = edited(&dir);
634 let path = dir.join("edit.edith");
635 session.save_project(&path).expect("save");
636 drop(session);
637
638 let gone = dir.join("test_av2.mp4");
639 std::fs::remove_file(&gone).expect("unlink");
640 let err = PlaybackSession::open_project(&path)
641 .err()
642 .expect("a project whose source is gone must not open")
643 .to_string();
644 assert!(
645 err.contains("test_av2.mp4"),
646 "the refusal must name the file: {err}"
647 );
648
649 // The first source is no different -- it is simply refused earlier.
650 std::fs::remove_file(dir.join("test_av.mp4")).expect("unlink");
651 let err = PlaybackSession::open_project(&path)
652 .err()
653 .expect("no first source, no session")
654 .to_string();
655 assert!(err.contains("test_av.mp4"), "{err}");
656}
657
658#[test]
659fn a_source_that_shrank_is_refused_by_clip() {
660 let dir = scratch("shrunk");
661 copy_in(&dir, "test_av.mp4");
662 let path = dir.join("shrunk.edith");
663 // Hand-written: a clip that ran to frame 10000 of a 150-frame file, which
664 // is what re-encoding a source shorter leaves behind.
665 std::fs::write(
666 &path,
667 "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\nclip 30 10000 0\n",
668 )
669 .expect("write");
670 let err = PlaybackSession::open_project(&path)
671 .err()
672 .expect("a clip past the end of its file must not open")
673 .to_string();
674 assert!(
675 err.contains("clip 1") && err.contains("test_av.mp4") && err.contains("150"),
676 "the refusal must name the clip, the file and its length: {err}"
677 );
678
679 // One frame short of the end is still inside the file.
680 std::fs::write(
681 &path,
682 "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 150 0\n",
683 )
684 .expect("write");
685 let whole = PlaybackSession::open_project(&path).expect("the exact length is legal");
686 assert_eq!(whole.timeline_duration(), 5.0);
687}
688
689/// A refusal must *return*. Opening a project starts decoding source 0 before
690/// it has validated anything, so by the time a later source is refused the
691/// worker is several frames in -- and with a two-frame channel that nobody is
692/// draining, it is parked in `send`. Tearing that down in the wrong order joins
693/// a thread that is waiting for a receiver the same scope still holds, and the
694/// app hangs on a bad project file instead of showing the error.
695///
696/// The many sources are what make the park a certainty rather than a race: each
697/// one is opened and probed before the clip check that refuses, which is far
698/// longer than the few frames it takes to fill the channel.
699#[test]
700fn a_refused_project_does_not_hang_on_its_own_decoder() {
701 let dir = scratch("refusal_hang");
702 copy_in(&dir, "test_av.mp4");
703 let path = dir.join("refused.edith");
704 let mut text = String::from("edith 1\nplayhead 0\n");
705 for _ in 0..1024 {
706 text.push_str("source test_av.mp4\n");
707 }
708 // Legal until the last line, which runs off the end of a 150-frame file.
709 text.push_str("clip 0 150 0\nclip 0 10000 0\n");
710 std::fs::write(&path, &text).expect("write");
711
712 let (tx, rx) = std::sync::mpsc::sync_channel(1);
713 let probe = path.clone();
714 std::thread::spawn(move || {
715 let err = PlaybackSession::open_project(&probe)
716 .err()
717 .map(|e| e.to_string());
718 let _ = tx.send(err);
719 });
720 match rx.recv_timeout(std::time::Duration::from_secs(60)) {
721 Ok(Some(err)) => assert!(err.contains("150"), "refused, but not by name: {err}"),
722 Ok(None) => panic!("a clip past the end of its file must not open"),
723 Err(_) => panic!(
724 "open_project hung on a refusal: its own decode worker is parked in \
725 send and the teardown is waiting for it"
726 ),
727 }
728}
729
730#[test]
731fn a_project_that_names_itself_is_refused() {
732 let dir = scratch("self_reference");
733 let path = dir.join("ouroboros.edith");
734 std::fs::write(
735 &path,
736 "edith 1\nplayhead 0\nsource ouroboros.edith\nclip 0 30 0\n",
737 )
738 .expect("write");
739 let err = PlaybackSession::open_project(&path)
740 .err()
741 .expect("a project is not a video")
742 .to_string();
743 assert!(err.contains("ouroboros.edith"), "{err}");
744}
745
746#[test]
747fn malformed_files_are_numbered_errors_and_never_panics() {
748 let dir = scratch("malformed");
749 copy_in(&dir, "test_av.mp4");
750 let path = dir.join("bad.edith");
751 let good = "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\n";
752
753 for (text, want) in [
754 (
755 "edith 17\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n",
756 "line 3",
757 ),
758 // An eq index the table does not hold, and a band shape there is none.
759 (
760 "edith 5\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - 0\n",
761 "line 3",
762 ),
763 (
764 "edith 5\nsource 0 test_av.mp4\neq 80.0:0.0:0.707:band\nvideo 1 0 0 30 0 - 0\n",
765 "line 3",
766 ),
767 // A lane number that skips one of its kind: V2 was never declared.
768 (
769 "edith 4\nsource 0 test_av.mp4\nvideo 2 0 0 30 0 -\n",
770 "line 3",
771 ),
772 // A v2 source line in a v3 file: the stream field is not optional.
773 ("edith 3\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 2"),
774 ("not a project at all\n", "line 1"),
775 // Dialects do not mix: lane lines are v2's, `clip` is v1's.
776 ("edith 2\nsource test_av.mp4\nclip 0 30 0\n", "line 3"),
777 ("edith 1\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 3"),
778 ("edith 2\nsource test_av.mp4\nvideo 0 0 30 0\n", "line 3"),
779 (
780 "edith 2\nsource test_av.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n",
781 "line 4",
782 ),
783 ("edith 1\nsource test_av.mp4\nclip 0 30\n", "line 3"),
784 ("edith 1\nsource test_av.mp4\nclip 0 30 4\n", "line 3"),
785 ("edith 1\nsource test_av.mp4\nclip 30 30 0\n", "line 3"),
786 ("edith 1\nsource test_av.mp4\n\nclip 0 30 0\n", "line 3"),
787 ("edith 1\nsource test_av.mp4\nwhat 1\n", "line 3"),
788 // Crafted numbers: `start + len` used to wrap past the top of `u32`,
789 // and a wrapped end walks straight through the overlap check.
790 (
791 "edith 2\nsource test_av.mp4\nvideo 4294967290 0 30 0 -\n",
792 "line 3",
793 ),
794 ] {
795 std::fs::write(&path, text).expect("write");
796 let err = PlaybackSession::open_project(&path)
797 .err()
798 .unwrap_or_else(|| panic!("accepted {text:?}"))
799 .to_string();
800 assert!(err.starts_with(want), "{text:?} -> {err}");
801 }
802
803 // Every truncation of a good file: some are valid shorter projects, none
804 // may panic or be reported as anything but an error.
805 for cut in 0..good.len() {
806 std::fs::write(&path, &good[..cut]).expect("write");
807 let _ = PlaybackSession::open_project(&path);
808 }
809 // ...and a file that is not there at all.
810 std::fs::remove_file(&path).expect("unlink");
811 assert!(PlaybackSession::open_project(&path).is_err());
812}
813
814#[test]
815fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() {
816 let dir = scratch("mismatch");
817 copy_in(&dir, "test_av.mp4");
818 std::fs::copy(asset("test_ac3.mp4"), dir.join("test_av2.mp4")).expect("substitute");
819 let path = dir.join("swapped.edith");
820 std::fs::write(
821 &path,
822 "edith 1\nplayhead 0\nsource test_av.mp4\nsource test_av2.mp4\nclip 0 30 0\nclip 0 30 1\n",
823 )
824 .expect("write");
825 let err = PlaybackSession::open_project(&path)
826 .err()
827 .expect("a source that stopped matching must not open")
828 .to_string();
829 // The suffix is `import`'s own refusal, word for word. The substitute is
830 // mono, which is the shape of what is left: one output device carries one
831 // layout. A resolution of its own is placed on the project canvas, a frame
832 // rate of its own is read through `Rate`, a *sample* rate of its own is
833 // resampled at the decoder's door, a codec of its own opens its own decoder
834 // and a file with no sound plays silence -- none of those is a refusal any
835 // more.
836 assert_eq!(
837 err,
838 format!(
839 "source {}: audio 1 ch does not match the timeline's 2 ch",
840 dir.join("test_av2.mp4").display()
841 )
842 );
843}
844
845/// The mix a project was left at comes back with it: a track's fader, the
846/// master limiter and the rate the timeline was cut at. A fader that vanished
847/// on a reload would be a setting nobody could keep.
848#[test]
849fn the_mix_survives_a_save_and_an_open() {
850 use engine::project::{Lane, LaneKind};
851 let dir = scratch("mix");
852 let source = copy_in(&dir, "test_av.mp4");
853 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
854 let a2 = saved.add_lane(LaneKind::Audio);
855 assert!(saved.set_lane_gain_db(Lane::A1, -6.0));
856 assert!(saved.set_lane_gain_db(a2, 3.0));
857 let limiter = engine::limiter::Limiter {
858 ceiling_db: -2.0,
859 on: true,
860 };
861 assert!(saved.set_limiter(limiter));
862 let path = dir.join("mix.edith");
863 saved.save_project(&path).expect("save");
864
865 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
866 assert_eq!(loaded.lane_gain_db(Lane::A1), -6.0, "A1's fader");
867 assert_eq!(loaded.lane_gain_db(a2), 3.0, "A2's own, not A1's");
868 assert_eq!(loaded.limiter(), limiter);
869 assert!(!loaded.undo(), "a loaded mix is not an undo step");
870 // ...and saving it again is the same bytes: the format holds all of it.
871 let again = dir.join("again.edith");
872 loaded
873 .save_project(&again)
874 .expect("save the loaded project");
875 assert_eq!(
876 std::fs::read(&path).unwrap(),
877 std::fs::read(&again).unwrap(),
878 "save -> load -> save is not a fixed point"
879 );
880}
881
882/// The v11 bump's own round trip: the HDR rendition a project was left in comes
883/// back with it, and a file written before there were any is the reference one.
884/// A picked look that vanished on a reload would be a setting nobody could keep
885/// -- the mix test's claim, for the picture.
886#[test]
887fn the_tone_map_preset_survives_a_save_and_an_open() {
888 use engine::tonemap::Preset;
889 let dir = scratch("tone");
890 let source = copy_in(&dir, "test_av.mp4");
891 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
892 assert_eq!(saved.tone(), Preset::Reference, "nobody has picked yet");
893 assert!(saved.set_tone(Preset::Vivid));
894 let path = dir.join("tone.edith");
895 saved.save_project(&path).expect("save");
896
897 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
898 assert_eq!(loaded.tone(), Preset::Vivid);
899 assert!(!loaded.undo(), "a loaded rendition is not an undo step");
900 // ...and saving it again is the same bytes, as the mix is.
901 let again = dir.join("again.edith");
902 loaded
903 .save_project(&again)
904 .expect("save the loaded project");
905 assert_eq!(
906 std::fs::read(&path).unwrap(),
907 std::fs::read(&again).unwrap(),
908 "save -> load -> save is not a fixed point"
909 );
910
911 // A project written before v11 says nothing about a rendition, and means the
912 // published one -- the picture it was always shown in.
913 let old = dir.join("old.edith");
914 std::fs::write(
915 &old,
916 "edith 10\nplayhead 0\nresolution 1920 1080\nsource 0 test_av.mp4\n\
917 video 1 0 0 24 0 - - - fit 1000\naudio 1 0 0 24 0 - - - fit 1000\n",
918 )
919 .expect("write a v10 project");
920 let older = PlaybackSession::open_project(&old).expect("open the v10 project");
921 assert_eq!(older.tone(), Preset::Reference);
922}
923
924/// The other half of the v9 bump, and the debt it pays: a timeline holding
925/// nothing but a song has no file to take a frame rate from, so it used to come
926/// back at the canvas rate whatever it was cut at. The written `fps` is now
927/// preferred over that inference -- and a project whose scaffold *does* have a
928/// picture still takes the rate from the file, which is what every clip on it
929/// was conformed to.
930#[test]
931fn a_song_only_project_comes_back_at_the_rate_it_names() {
932 let dir = scratch("song_fps");
933 copy_in(&dir, "test_tone.mp3");
934 let path = dir.join("song.edith");
935 std::fs::write(
936 &path,
937 "edith 10\nplayhead 0\nresolution 1920 1080\nfps 24.0\nsource 0 test_tone.mp3\n\
938 video 1\naudio 1 0 0 24 0 - - - fit 1000\n",
939 )
940 .expect("write a v9 song project");
941 let loaded = PlaybackSession::open_project(&path).expect("a song-only project opens");
942 assert_eq!(loaded.meta().frame_rate, 24.0, "the rate the file names");
943 assert_eq!(loaded.timeline_duration(), 1.0, "one second at 24 fps");
944
945 // The same file without the line -- which is every dialect before v9 --
946 // still comes back at the canvas rate, so an old project is unchanged.
947 let old = dir.join("song8.edith");
948 std::fs::write(
949 &old,
950 "edith 8\nplayhead 0\nresolution 1920 1080\nsource 0 test_tone.mp3\n\
951 video 1\naudio 1 0 0 24 0 - - - fit 1000\n",
952 )
953 .expect("write a v8 song project");
954 let loaded = PlaybackSession::open_project(&old).expect("a v8 song project opens");
955 assert_eq!(loaded.meta().frame_rate, 30.0, "the canvas rate, as before");
956}