forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

saveload.rs2065 lines, 82.7 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::{GainKey, Lane, LaneKind, 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 refusal left, in the engine's own words: a file that is not on
78 // this timeline. Its *layout* (stream 1 is mono, the timeline stereo) is no
79 // longer one (DEBT #46): the mono stream up-mixes onto both channels.
80 let other = copy_in(&dir, "test_multiaudio.mp4");
81 let mut session = PlaybackSession::open(&other).expect("open the fixture");
82 session.set_gain(0.0);
83 session
84 .place_stream_at(0.0, &other, 1, None)
85 .expect("a mono stream up-mixes onto a stereo timeline");
86 assert!(
87 session
88 .place_stream_at(0.0, &media, 1, None)
89 .expect_err("a file that is not a source")
90 .to_string()
91 .contains("not on this timeline")
92 );
93 assert_eq!(
94 session.sources().len(),
95 2,
96 "the mono stream landed, the not-a-source refusal added nothing more"
97 );
98 std::fs::remove_dir_all(&dir).expect("cleanup");
99}
100
101/// Both stand-in switches through the front door: what the picture is cut on
102/// and whether an import makes one at all are two settings, saved and reopened
103/// as the pair they are -- and a fresh session makes none until it is told to.
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 none 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!(
129 !text.contains("autoproxy"),
130 "on is written by saying nothing"
131 );
132 assert!(
133 PlaybackSession::open_project(&path)
134 .expect("reopen")
135 .auto_proxies()
136 );
137 std::fs::remove_dir_all(&dir).expect("cleanup");
138}
139
140/// The export card's encoder pick through the same front door: which seat a
141/// delivery is written on is the project's, so it survives a save and a reopen
142/// -- and a project nobody picked for exports on whatever this machine has,
143/// which is what every project did before the row existed.
144#[test]
145fn the_encoder_seat_survives_a_save_and_a_reopen() {
146 use engine::export::EncoderSeat;
147
148 let dir = scratch("encoderseat");
149 let media = copy_in(&dir, "test_av.mp4");
150 let mut session = PlaybackSession::open(&media).expect("open the fixture");
151 session.set_gain(0.0);
152 assert_eq!(
153 session.encoder_seat(),
154 EncoderSeat::Auto,
155 "a fresh project takes the seat this machine has"
156 );
157
158 let path = dir.join("seat.edith");
159 for seat in [EncoderSeat::Software, EncoderSeat::Hardware] {
160 session.set_encoder_seat(seat);
161 session.save_project(&path).expect("save");
162 let text = std::fs::read_to_string(&path).expect("read back");
163 assert!(
164 text.contains(&format!("encoder {}\n", seat.name())),
165 "{text}"
166 );
167 assert_eq!(
168 PlaybackSession::open_project(&path)
169 .expect("reopen")
170 .encoder_seat(),
171 seat
172 );
173 }
174
175 // ...and the default is the line left out, which is what a v13 file is.
176 session.set_encoder_seat(EncoderSeat::Auto);
177 session.save_project(&path).expect("save");
178 let text = std::fs::read_to_string(&path).expect("read back");
179 assert!(
180 !text.contains("encoder"),
181 "auto is written by saying nothing"
182 );
183 assert_eq!(
184 PlaybackSession::open_project(&path)
185 .expect("reopen")
186 .encoder_seat(),
187 EncoderSeat::Auto
188 );
189 std::fs::remove_dir_all(&dir).expect("cleanup");
190}
191
192/// Why a trim's tail stops where the file ends: a clip reaching past the last
193/// frame of its source is a project that will not open again (`open_project`
194/// refuses it by name). Pulled in on both ends and then dragged out as far as
195/// the pointer can ask for, the save still comes back as itself.
196#[test]
197fn a_trimmed_timeline_saves_and_opens_again() {
198 use engine::project::Edge;
199
200 let dir = scratch("trim");
201 let media = copy_in(&dir, "test_av.mp4");
202 let mut session = PlaybackSession::open(&media).expect("open the fixture");
203 session.set_gain(0.0);
204 let whole = session.clip_at(0).expect("one clip");
205
206 assert!(session.trim_clip(Lane::V1, 0, Edge::End, whole.end() / 2));
207 assert!(session.trim_clip(Lane::V1, 0, Edge::Start, 5));
208 let path = dir.join("trimmed.edith");
209 session.save_project(&path).expect("save");
210 let loaded = PlaybackSession::open_project(&path).expect("a trimmed project opens");
211 assert_eq!(
212 loaded
213 .clip_at(0)
214 .map(|c| (c.start, c.in_frame, c.out_frame)),
215 Some((5, 5, whole.end() / 2)),
216 "the trimmed range is what came back"
217 );
218
219 // A hand that keeps pulling: the tail stops at the file's own last frame,
220 // and the save of *that* opens too.
221 assert!(session.trim_clip(Lane::V1, 0, Edge::End, u32::MAX));
222 assert_eq!(
223 session.clip_at(0).expect("still one clip").out_frame,
224 whole.out_frame,
225 "as far as the file goes and not one frame further"
226 );
227 session.save_project(&path).expect("save");
228 PlaybackSession::open_project(&path).expect("and it still opens");
229 std::fs::remove_dir_all(&dir).expect("cleanup");
230}
231
232/// Two files, four clips, one of them deleted, playhead at 3 s -- a timeline
233/// with something to lose in every field.
234fn edited(dir: &Path) -> PlaybackSession {
235 let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open");
236 // Imported into the library, then dragged onto the end -- the two acts a
237 // second file on the timeline takes.
238 let second = copy_in(dir, "test_av2.mp4");
239 session.import(&second).expect("import the second file");
240 let end = session.timeline_duration();
241 assert!(
242 session
243 .place_stream_at(end, &second, 0, None)
244 .expect("a file just imported is on this timeline"),
245 "drag it onto the end"
246 );
247 assert!(session.cut_at(2.0), "cut inside the first file");
248 assert!(session.cut_at(6.5), "cut inside the second file");
249 assert!(
250 session.delete_clip(Lane::V1, 1),
251 "drop the second half of the first"
252 );
253 session.seek(3.0);
254 assert_eq!(session.timeline_duration(), 6.0);
255 session
256}
257
258/// What has to survive a round trip, all of it public API.
259fn shape(session: &PlaybackSession) -> (Vec<(f64, f64, usize)>, Vec<Source>, f64) {
260 (
261 session.clip_spans_by_source(),
262 session.sources().to_vec(),
263 session.timeline_duration(),
264 )
265}
266
267/// The version-1 promise: a file written before the lanes existed still opens,
268/// as one grouped video+audio pair per clip laid end to end, and saving it
269/// again writes version 7 -- which reopens as the same timeline.
270#[test]
271fn a_version_1_project_loads_fully_grouped_and_saves_as_version_7() {
272 let dir = scratch("v1");
273 copy_in(&dir, "test_av.mp4");
274 let path = dir.join("old.edith");
275 // 60 frames of picture from two clips: [0,30) then [60,90) of the source.
276 std::fs::write(
277 &path,
278 "edith 1\nplayhead 45\nsource test_av.mp4\nclip 0 30 0\nclip 60 90 0\n",
279 )
280 .expect("write a v1 file");
281
282 let loaded = PlaybackSession::open_project(&path).expect("a v1 file still opens");
283 let fps = loaded.meta().frame_rate;
284 assert_eq!(
285 loaded.clip_spans_by_source(),
286 vec![(0.0, 30.0 / fps, 0), (30.0 / fps, 30.0 / fps, 0)],
287 "v1 clips queue up, the second starting where the first ended"
288 );
289 assert_eq!(
290 loaded.lane_spans_by_source(engine::project::Lane::A1),
291 loaded.clip_spans_by_source(),
292 "both lanes, fully grouped: a v1 timeline had no holes"
293 );
294 assert!(
295 (loaded.now() - 45.0 / fps).abs() < 1.0 / fps,
296 "playhead kept"
297 );
298
299 // Saved again it is v7, and v7 round-trips to the same timeline.
300 let v2 = dir.join("new.edith");
301 loaded.save_project(&v2).expect("save");
302 let text = std::fs::read_to_string(&v2).expect("read back");
303 assert!(text.starts_with("edith 30\n"), "{text}");
304 assert!(
305 text.contains("\nresolution 1280 720\n"),
306 "a project with no resolution of its own is saved at source 0's: {text}"
307 );
308 assert_eq!(
309 text.lines().filter(|l| l.starts_with("video ")).count(),
310 2,
311 "{text}"
312 );
313 assert_eq!(
314 text.lines().filter(|l| l.starts_with("audio ")).count(),
315 2,
316 "{text}"
317 );
318 let again = PlaybackSession::open_project(&v2).expect("open the v2 file");
319 assert_eq!(shape(&again), shape(&loaded));
320 // ...and re-saving it is byte-identical, so a round trip is stable.
321 let third = dir.join("third.edith");
322 again.save_project(&third).expect("save");
323 assert_eq!(std::fs::read(&third).expect("read"), text.as_bytes());
324
325 // And the same promise one version on: a v2 file names no audio stream and
326 // opens on stream 0, which is the only one that dialect could play.
327 let v2 = dir.join("v2.edith");
328 std::fs::write(
329 &v2,
330 "edith 2\nplayhead 0\nsource test_av.mp4\nvideo 0 0 30 0 0\naudio 0 0 30 0 0\n",
331 )
332 .expect("write a v2 file");
333 let loaded = PlaybackSession::open_project(&v2).expect("a v2 file still opens");
334 assert_eq!(loaded.sources().len(), 1);
335 assert_eq!(loaded.sources()[0].audio_stream, 0);
336 loaded.save_project(&v2).expect("save");
337 assert!(
338 std::fs::read_to_string(&v2)
339 .expect("read back")
340 .contains("source 0 test_av.mp4"),
341 "a re-saved v2 project says the stream it always meant"
342 );
343}
344
345/// What version 4 is for: a project file holds any number of lanes, in the
346/// order they are displayed in, and one holding nothing is still a lane. The
347/// timeline is built by hand because the format is what this checks -- no UI
348/// adds a lane yet -- and the file it writes back has to be the one it read.
349#[test]
350fn a_version_4_project_holds_more_than_two_lanes() {
351 use engine::project::{Lane, LaneKind};
352
353 let dir = scratch("v4_lanes");
354 copy_in(&dir, "test_av.mp4");
355 let path = dir.join("many.edith");
356 // V1, A1, V2, A2 in display order: a take grouped across V1 and A2 (link 3,
357 // one span on two lanes that are not a pair), a lone clip on V2, and an
358 // empty A1 that has only its own line to say it is there.
359 let text = "edith 4\nplayhead 0\nsource 0 test_av.mp4\n\
360 video 1 0 0 30 0 3\naudio 1\nvideo 2 40 0 20 0 -\naudio 2 0 0 30 0 3\n";
361 std::fs::write(&path, text).expect("write a v4 file");
362
363 let loaded = PlaybackSession::open_project(&path).expect("a four-lane project opens");
364 let v2 = Lane::new(LaneKind::Video, 1);
365 let a2 = Lane::new(LaneKind::Audio, 1);
366 assert_eq!(loaded.lane_clips(Lane::V1).len(), 1);
367 assert!(loaded.lane_clips(Lane::A1).is_empty(), "A1 holds nothing");
368 assert_eq!(
369 loaded
370 .lane_clips(v2)
371 .iter()
372 .map(|c| (c.start, c.end()))
373 .collect::<Vec<_>>(),
374 vec![(40, 60)],
375 "V2 is a lane of its own, gap and all"
376 );
377 assert_eq!(
378 loaded.lane_clips(a2)[0].link,
379 loaded.lane_clips(Lane::V1)[0].link,
380 "the group spans V1 and A2, which are not a pair"
381 );
382 assert_eq!(loaded.timeline_duration(), 60.0 / loaded.meta().frame_rate);
383
384 // ...and saving it writes the v7 twin of what it read: the lane list, its
385 // order and the empty lane all survive, each clip now saying it plays flat,
386 // ungraded and fitted, under a project resolution taken from the media.
387 let again = dir.join("again.edith");
388 loaded.save_project(&again).expect("save");
389 assert_eq!(
390 std::fs::read_to_string(&again).expect("read back"),
391 "edith 30\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
392 video 1 0 0 30 0 3 - - fit 1000 0 0 0 - 0 0\naudio 1\n\
393 video 2 40 0 20 0 - - - fit 1000 0 0 0 - 0 0\naudio 2 0 0 30 0 3 - - fit 1000 0 0 0 - 0 0\n",
394 "a four-lane project is written as it was read, three versions on"
395 );
396}
397
398/// What version 5 is for: a clip carries equalizer settings, the file names
399/// them once for however many clips share them, and the whole thing survives
400/// the engine's own door -- open, save, every curve and index of it intact one
401/// version on.
402#[test]
403fn a_version_5_project_carries_per_clip_equalizers() {
404 let dir = scratch("v5_eq");
405 copy_in(&dir, "test_av.mp4");
406 let path = dir.join("eq.edith");
407 // Two clips on one curve, one on another, one flat.
408 let text = "edith 5\nplayhead 0\nsource 0 test_av.mp4\n\
409 eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\
410 eq 12000.0:6.25:0.5:hs\n\
411 video 1 0 0 30 0 - 0\nvideo 1 30 30 60 0 - 1\n\
412 audio 1 0 0 30 0 - 0\naudio 1 30 30 60 0 - -\n";
413 std::fs::write(&path, text).expect("write a v5 file");
414
415 let loaded = PlaybackSession::open_project(&path).expect("a v5 project opens");
416 assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2);
417 let again = dir.join("again.edith");
418 loaded.save_project(&again).expect("save");
419 assert_eq!(
420 std::fs::read_to_string(&again).expect("read back"),
421 "edith 30\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
422 eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\
423 eq 12000.0:6.25:0.5:hs\n\
424 video 1 0 0 30 0 - 0 - fit 1000 0 0 0 - 0 0\nvideo 1 30 30 60 0 - 1 - fit 1000 0 0 0 - 0 0\n\
425 audio 1 0 0 30 0 - 0 - fit 1000 0 0 0 - 0 0\naudio 1 30 30 60 0 - - - fit 1000 0 0 0 - 0 0\n",
426 "the equalizer table and every clip's index survive a round trip"
427 );
428}
429
430/// What version 6 is for, the same claim one version on: a clip carries a
431/// colour grade beside its equalizer and the file names each once for however
432/// many clips share it. A format bump necessarily rewrites the file, so what is
433/// asserted here is the v7 twin of what was read; the byte-identity claim lives
434/// in `a_version_7_project_carries_a_resolution_and_fit_policies` below.
435#[test]
436fn a_version_6_project_carries_per_clip_colours() {
437 let dir = scratch("v6_color");
438 copy_in(&dir, "test_av.mp4");
439 let path = dir.join("color.edith");
440 // Two clips on one grade, one on another, one ungraded -- and an equalizer
441 // on a clip that is graded too, so the two tables are seen not to collide.
442 let text = "edith 6\nplayhead 0\nsource 0 test_av.mp4\n\
443 eq 80.0:-3.0:0.707:ls\n\
444 color 0.1:1.2:0.9:-0.3\n\
445 color -0.25:1.0:0.0:0.5\n\
446 video 1 0 0 30 0 - 0 0\nvideo 1 30 30 60 0 - - 1\n\
447 audio 1 0 0 30 0 - - 0\naudio 1 30 30 60 0 - - -\n";
448 std::fs::write(&path, text).expect("write a v6 file");
449
450 let loaded = PlaybackSession::open_project(&path).expect("a v6 project opens");
451 assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2);
452 let again = dir.join("again.edith");
453 loaded.save_project(&again).expect("save");
454 assert_eq!(
455 std::fs::read_to_string(&again).expect("read back"),
456 "edith 30\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
457 eq 80.0:-3.0:0.707:ls\n\
458 color 0.1:1.2:0.9:-0.3\n\
459 color -0.25:1.0:0.0:0.5\n\
460 video 1 0 0 30 0 - 0 0 fit 1000 0 0 0 - 0 0\nvideo 1 30 30 60 0 - - 1 fit 1000 0 0 0 - 0 0\n\
461 audio 1 0 0 30 0 - - 0 fit 1000 0 0 0 - 0 0\naudio 1 30 30 60 0 - - - fit 1000 0 0 0 - 0 0\n",
462 "the colour table and every clip's index survive a round trip"
463 );
464 // A dialect that could not say either one means the defaults, and those are
465 // what such a project always was: the media's own size, nothing letterboxed.
466 assert_eq!(loaded.resolution(), (1280, 720));
467 assert_eq!(
468 loaded.fit_of(engine::project::Lane::V1, 0),
469 engine::scale::FitPolicy::Fit
470 );
471}
472
473/// What version 7 is for: the project's picture size is its own, not source 0's,
474/// and each clip says how it meets it. Both survive the engine's door byte for
475/// byte -- and a project whose resolution is nobody's media size is exactly the
476/// case that could not be written down before.
477#[test]
478fn a_version_7_project_carries_a_resolution_and_fit_policies() {
479 let dir = scratch("v7_resolution");
480 copy_in(&dir, "test_av.mp4");
481 let path = dir.join("mixed.edith");
482 let text = "edith 7\nplayhead 0\nresolution 960 720\nsource 0 test_av.mp4\n\
483 video 1 0 0 30 0 - - - fill\nvideo 1 30 30 60 0 - - - center\n\
484 audio 1 0 0 60 0 - - - fit\n";
485 std::fs::write(&path, text).expect("write a v7 file");
486
487 let loaded = PlaybackSession::open_project(&path).expect("a v7 project opens");
488 assert_eq!(
489 loaded.resolution(),
490 (960, 720),
491 "the project's own size, which is not the media's 1280x720"
492 );
493 assert_eq!(
494 loaded.native_resolution(),
495 (1280, 720),
496 "...and the media's own size is still known"
497 );
498 let v1 = engine::project::Lane::V1;
499 assert_eq!(loaded.fit_of(v1, 0), engine::scale::FitPolicy::Fill);
500 assert_eq!(loaded.fit_of(v1, 1), engine::scale::FitPolicy::Center);
501
502 let again = dir.join("again.edith");
503 loaded.save_project(&again).expect("save");
504 assert_eq!(
505 std::fs::read_to_string(&again).expect("read back"),
506 "edith 30\nplayhead 0\nresolution 960 720\nfps 30.0\nsource 0 test_av.mp4\n\
507 video 1 0 0 30 0 - - - fill 1000 0 0 0 - 0 0\nvideo 1 30 30 60 0 - - - center 1000 0 0 0 - 0 0\n\
508 audio 1 0 0 60 0 - - - fit 1000 0 0 0 - 0 0\n",
509 "a v7 project is written back as the v8 it now is: the same clips, each \
510 saying it plays at real time, which is what it always played at"
511 );
512
513 // The refusals the new grammar adds, each naming its line.
514 for (bad, want) in [
515 (
516 "edith 7\nresolution 0 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n",
517 "0x720 is not a picture",
518 ),
519 (
520 "edith 7\nresolution 960 720\nresolution 960 720\nsource 0 test_av.mp4\n\
521 video 1 0 0 30 0 - - - fit\n",
522 "resolution belongs once",
523 ),
524 // Past 8K is refused by the same bound the keystroke has: unbounded,
525 // this line reached `open_black` and panicked on a capacity overflow.
526 (
527 "edith 7\nresolution 4294967295 4294967295\nsource 0 test_av.mp4\n\
528 video 1 0 0 30 0 - - - fit\n",
529 "4294967295x4294967295 is not a picture",
530 ),
531 (
532 "edith 7\nresolution 7681 1080\nsource 0 test_av.mp4\n\
533 video 1 0 0 30 0 - - - fit\n",
534 "7681x1080 is not a picture",
535 ),
536 (
537 "edith 7\nresolution 960 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - squish\n",
538 "not a fit policy",
539 ),
540 ] {
541 let path = dir.join("bad.edith");
542 std::fs::write(&path, bad).expect("write");
543 let err = PlaybackSession::open_project(&path)
544 .err()
545 .expect("a malformed v7 file must be refused")
546 .to_string();
547 assert!(err.contains(want), "wanted {want:?}, got {err:?}");
548 }
549}
550
551#[test]
552fn a_saved_project_reopens_as_the_same_timeline() {
553 let dir = scratch("round_trip");
554 let saved = edited(&dir);
555 let path = dir.join("edit.edith");
556 saved.save_project(&path).expect("save");
557
558 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
559 assert_eq!(shape(&loaded), shape(&saved), "clips, sources and duration");
560 assert!(
561 (loaded.now() - saved.now()).abs() < 1.0 / loaded.meta().frame_rate,
562 "playhead landed at {:.3}s, saved at {:.3}s",
563 loaded.now(),
564 saved.now()
565 );
566 assert!(!loaded.is_playing(), "a loaded project starts paused");
567 assert!(!loaded.undo(), "history is not saved");
568
569 // Saving what was loaded reproduces the file byte for byte -- the format
570 // has no state the round trip drops.
571 let again = dir.join("again.edith");
572 loaded
573 .save_project(&again)
574 .expect("save the loaded project");
575 assert_eq!(
576 std::fs::read(&path).unwrap(),
577 std::fs::read(&again).unwrap(),
578 "save -> load -> save is not a fixed point"
579 );
580 assert!(!path.with_extension("edith.part").exists(), "left a .part");
581}
582
583#[test]
584fn a_folder_of_media_and_its_project_can_be_moved() {
585 let from = scratch("relocate_from");
586 let saved = edited(&from);
587 saved.save_project(&from.join("edit.edith")).expect("save");
588 drop(saved);
589
590 let to = scratch("relocate_to");
591 for name in ["test_av.mp4", "test_av2.mp4", "edit.edith"] {
592 std::fs::copy(from.join(name), to.join(name)).expect("copy");
593 }
594 // The originals go away entirely: nothing absolute can be resolving here.
595 std::fs::remove_dir_all(&from).expect("remove the original folder");
596
597 let moved = PlaybackSession::open_project(&to.join("edit.edith")).expect("open the copy");
598 assert_eq!(moved.timeline_duration(), 6.0);
599 assert!(
600 moved.sources().iter().all(|s| s.path.starts_with(&to)),
601 "sources did not follow the folder: {:?}",
602 moved.sources()
603 );
604}
605
606#[test]
607fn imported_sources_are_written_even_when_no_clip_plays_them() {
608 let dir = scratch("orphans");
609 let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open");
610 // An import that was never dragged onto a lane *is* the orphan: a library
611 // row with no clip naming it. (So is one whose clip was undone -- the same
612 // entry, reached the other way.)
613 session
614 .import(&copy_in(&dir, "test_av2.mp4"))
615 .expect("import");
616 assert_eq!(
617 session.sources().len(),
618 2,
619 "the orphan entry stays in-session"
620 );
621
622 let path = dir.join("orphan.edith");
623 session.save_project(&path).expect("save");
624 let text = std::fs::read_to_string(&path).expect("read back");
625 assert_eq!(
626 text.lines().filter(|l| l.starts_with("source ")).count(),
627 2,
628 "the imported source did not reach the file:\n{text}"
629 );
630 let loaded = PlaybackSession::open_project(&path).expect("open the project");
631 assert_eq!(
632 loaded.sources(),
633 session.sources(),
634 "same indices after reopen"
635 );
636 assert_eq!(loaded.timeline_duration(), 5.0);
637}
638
639/// A vanished source no longer refuses the project: it opens with that source
640/// offline, its clips kept, and the session names the file. Export refusal by
641/// name is the front end's job (the MCP export tool refuses while any source is
642/// offline); the engine only reports which sources are offline.
643#[test]
644fn a_source_that_vanished_opens_offline_and_is_named() {
645 let dir = scratch("missing");
646 let session = edited(&dir);
647 let path = dir.join("edit.edith");
648 session.save_project(&path).expect("save");
649 drop(session);
650
651 let gone = dir.join("test_av2.mp4");
652 std::fs::remove_file(&gone).expect("unlink");
653 let opened = PlaybackSession::open_project(&path)
654 .expect("a project with one source gone opens with it offline");
655 let offline = opened.offline_sources();
656 assert_eq!(offline.len(), 1, "{offline:?}");
657 assert!(
658 offline[0].1.ends_with("test_av2.mp4"),
659 "the offline source must be named by file: {offline:?}"
660 );
661 assert!(!opened.lanes().is_empty(), "the clips stay on the timeline");
662 drop(opened);
663
664 // The first source is no different -- it is offline too, and both are named.
665 std::fs::remove_file(dir.join("test_av.mp4")).expect("unlink");
666 let opened = PlaybackSession::open_project(&path).expect("every source gone still opens");
667 let names: Vec<String> = opened
668 .offline_sources()
669 .iter()
670 .map(|(_, p)| p.file_name().unwrap().to_string_lossy().into_owned())
671 .collect();
672 assert_eq!(names.len(), 2, "{names:?}");
673 assert!(names.iter().any(|n| n == "test_av.mp4"), "{names:?}");
674 assert!(names.iter().any(|n| n == "test_av2.mp4"), "{names:?}");
675}
676
677#[test]
678fn a_source_that_shrank_is_refused_by_clip() {
679 let dir = scratch("shrunk");
680 copy_in(&dir, "test_av.mp4");
681 let path = dir.join("shrunk.edith");
682 // Hand-written: a clip that ran to frame 10000 of a 150-frame file, which
683 // is what re-encoding a source shorter leaves behind. It opens: the clip is
684 // clamped to the file's end and the load says so, rather than refusing the
685 // whole project.
686 std::fs::write(
687 &path,
688 "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\nclip 30 10000 0\n",
689 )
690 .expect("write");
691 let clamped = PlaybackSession::open_project(&path).expect("an overrun clip still opens");
692 assert_eq!(clamped.timeline_duration(), 5.0, "clip 1 is cut to the 120 frames left after clip 0");
693 let warnings = clamped.load_warnings();
694 // The picture and its sound are one take, so each lane's clip is clamped and reported.
695 assert_eq!(warnings.len(), 2, "{warnings:?}");
696 assert!(
697 warnings[0].contains("clip 1") && warnings[0].contains("test_av.mp4") && warnings[0].contains("150"),
698 "the warning names the clip, the file and its length: {warnings:?}"
699 );
700
701 // One frame short of the end is still inside the file, and says nothing.
702 std::fs::write(
703 &path,
704 "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 150 0\n",
705 )
706 .expect("write");
707 let whole = PlaybackSession::open_project(&path).expect("the exact length is legal");
708 assert_eq!(whole.timeline_duration(), 5.0);
709 assert!(whole.load_warnings().is_empty());
710}
711
712/// A refusal must *return*. Opening a project starts decoding source 0 before
713/// it has validated anything, so by the time a later source is refused the
714/// worker is several frames in -- and with a two-frame channel that nobody is
715/// draining, it is parked in `send`. Tearing that down in the wrong order joins
716/// a thread that is waiting for a receiver the same scope still holds, and the
717/// app hangs on a bad project file instead of showing the error.
718///
719/// The many sources are what make the park a certainty rather than a race: each
720/// one is opened and probed before the clip check that refuses, which is far
721/// longer than the few frames it takes to fill the channel.
722#[test]
723fn a_refused_project_does_not_hang_on_its_own_decoder() {
724 let dir = scratch("refusal_hang");
725 copy_in(&dir, "test_av.mp4");
726 let path = dir.join("refused.edith");
727 let mut text = String::from("edith 1\nplayhead 0\n");
728 for _ in 0..1024 {
729 text.push_str("source test_av.mp4\n");
730 }
731 // The last source is not media at all: it is refused after the 1024 real ones
732 // have each been opened and probed, which is the same shape of refusal.
733 std::fs::write(dir.join("notes.txt"), "not a video\n").expect("write");
734 text.push_str("source notes.txt\n");
735 text.push_str("clip 0 150 0\n");
736 std::fs::write(&path, &text).expect("write");
737
738 let (tx, rx) = std::sync::mpsc::sync_channel(1);
739 let probe = path.clone();
740 std::thread::spawn(move || {
741 let err = PlaybackSession::open_project(&probe)
742 .err()
743 .map(|e| e.to_string());
744 let _ = tx.send(err);
745 });
746 match rx.recv_timeout(std::time::Duration::from_secs(60)) {
747 Ok(Some(err)) => assert!(err.contains("notes.txt"), "refused, but not by name: {err}"),
748 Ok(None) => panic!("a project with a non-media source must not open"),
749 Err(_) => panic!(
750 "open_project hung on a refusal: its own decode worker is parked in \
751 send and the teardown is waiting for it"
752 ),
753 }
754}
755
756#[test]
757fn a_project_that_names_itself_is_refused() {
758 let dir = scratch("self_reference");
759 let path = dir.join("ouroboros.edith");
760 std::fs::write(
761 &path,
762 "edith 1\nplayhead 0\nsource ouroboros.edith\nclip 0 30 0\n",
763 )
764 .expect("write");
765 let err = PlaybackSession::open_project(&path)
766 .err()
767 .expect("a project is not a video")
768 .to_string();
769 assert!(err.contains("ouroboros.edith"), "{err}");
770}
771
772#[test]
773fn malformed_files_are_numbered_errors_and_never_panics() {
774 let dir = scratch("malformed");
775 copy_in(&dir, "test_av.mp4");
776 let path = dir.join("bad.edith");
777 let good = "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\n";
778
779 for (text, want) in [
780 (
781 "edith 17\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n",
782 "line 3",
783 ),
784 // An eq index the table does not hold, and a band shape there is none.
785 (
786 "edith 5\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - 0\n",
787 "line 3",
788 ),
789 (
790 "edith 5\nsource 0 test_av.mp4\neq 80.0:0.0:0.707:band\nvideo 1 0 0 30 0 - 0\n",
791 "line 3",
792 ),
793 // A lane number that skips one of its kind: V2 was never declared.
794 (
795 "edith 4\nsource 0 test_av.mp4\nvideo 2 0 0 30 0 -\n",
796 "line 3",
797 ),
798 // A v2 source line in a v3 file: the stream field is not optional.
799 ("edith 3\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 2"),
800 ("not a project at all\n", "line 1"),
801 // Dialects do not mix: lane lines are v2's, `clip` is v1's.
802 ("edith 2\nsource test_av.mp4\nclip 0 30 0\n", "line 3"),
803 ("edith 1\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 3"),
804 ("edith 2\nsource test_av.mp4\nvideo 0 0 30 0\n", "line 3"),
805 (
806 "edith 2\nsource test_av.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n",
807 "line 4",
808 ),
809 ("edith 1\nsource test_av.mp4\nclip 0 30\n", "line 3"),
810 ("edith 1\nsource test_av.mp4\nclip 0 30 4\n", "line 3"),
811 ("edith 1\nsource test_av.mp4\nclip 30 30 0\n", "line 3"),
812 ("edith 1\nsource test_av.mp4\n\nclip 0 30 0\n", "line 3"),
813 ("edith 1\nsource test_av.mp4\nwhat 1\n", "line 3"),
814 // Crafted numbers: `start + len` used to wrap past the top of `u32`,
815 // and a wrapped end walks straight through the overlap check.
816 (
817 "edith 2\nsource test_av.mp4\nvideo 4294967290 0 30 0 -\n",
818 "line 3",
819 ),
820 ] {
821 std::fs::write(&path, text).expect("write");
822 let err = PlaybackSession::open_project(&path)
823 .err()
824 .unwrap_or_else(|| panic!("accepted {text:?}"))
825 .to_string();
826 assert!(err.starts_with(want), "{text:?} -> {err}");
827 }
828
829 // Every truncation of a good file: some are valid shorter projects, none
830 // may panic or be reported as anything but an error.
831 for cut in 0..good.len() {
832 std::fs::write(&path, &good[..cut]).expect("write");
833 let _ = PlaybackSession::open_project(&path);
834 }
835 // ...and a file that is not there at all.
836 std::fs::remove_file(&path).expect("unlink");
837 assert!(PlaybackSession::open_project(&path).is_err());
838}
839
840#[test]
841fn a_stereo_source_that_joins_a_mono_timeline_reopens() {
842 // `test_av.mp4` -- source 0, which sets the timeline's norm -- is substituted
843 // with the mono fixture, so the norm this reload settles on is mono;
844 // `test_av2.mp4` is the real, stereo file. It used to be refused here ("audio
845 // 2 ch does not match the timeline's 1 ch") while `import` let it in, which
846 // is how a project could be saved and then never opened again. Now the pair
847 // is averaged to the timeline's one channel, so the reload accepts it.
848 let dir = scratch("mismatch");
849 std::fs::copy(asset("test_ac3.mp4"), dir.join("test_av.mp4")).expect("substitute");
850 copy_in(&dir, "test_av2.mp4");
851 let path = dir.join("swapped.edith");
852 std::fs::write(
853 &path,
854 "edith 1\nplayhead 0\nsource test_av.mp4\nsource test_av2.mp4\nclip 0 30 0\nclip 0 30 1\n",
855 )
856 .expect("write");
857 let loaded =
858 PlaybackSession::open_project(&path).expect("a stereo source on a mono timeline opens");
859 assert_eq!(loaded.sources().len(), 2);
860 std::fs::remove_dir_all(&dir).expect("cleanup");
861}
862
863/// Import and open agree on a stereo file joining a mono timeline: the import is
864/// accepted, the save writes it, and the reload opens it -- with the stereo
865/// pair folded to one channel, so the sound is the same length as before.
866#[test]
867fn a_stereo_wav_imported_into_a_mono_project_saves_and_reopens() {
868 let dir = scratch("stereo_on_mono");
869 let mono = copy_in(&dir, "test_audio_only.mp4"); // 1 ch AAC, 44100 Hz
870 let wav = copy_in(&dir, "test_tone.wav"); // stereo
871 let mut session = PlaybackSession::open(&mono).expect("open the mono fixture");
872 session.set_gain(0.0);
873 let end = session.timeline_duration();
874 session
875 .import(&wav)
876 .expect("a stereo file imports into a mono timeline");
877 assert!(
878 session
879 .place_stream_at(end, &wav, 0, None)
880 .expect("a stereo file joins a mono timeline"),
881 "the stereo wav landed on the timeline"
882 );
883 let length = session.timeline_duration();
884 let path = dir.join("stereo_on_mono.edith");
885 session.save_project(&path).expect("save");
886 let loaded = PlaybackSession::open_project(&path).expect("the saved project reopens");
887 assert_eq!(loaded.sources(), session.sources());
888 assert!((loaded.timeline_duration() - length).abs() < 1e-6);
889 std::fs::remove_dir_all(&dir).expect("cleanup");
890}
891
892/// Sources nothing plays are written too, in session order: a file imported
893/// into the library and never placed keeps its index across a save and a reopen.
894#[test]
895fn unused_sources_keep_their_index_across_save_and_open() {
896 let dir = scratch("stable_indices");
897 let media = copy_in(&dir, "test_multilang.mp4");
898 let other = copy_in(&dir, "test_multiaudio.mp4");
899 let mut session = PlaybackSession::open(&media).expect("open the fixture");
900 session.set_gain(0.0);
901 session.import(&other).expect("import, unplaced");
902 let placed = session.sources().to_vec();
903 assert_eq!(placed.len(), 2);
904 let path = dir.join("library.edith");
905 session.save_project(&path).expect("save");
906 let loaded = PlaybackSession::open_project(&path).expect("reopen");
907 assert_eq!(
908 loaded.sources(),
909 &placed[..],
910 "every imported source comes back, in order"
911 );
912 std::fs::remove_dir_all(&dir).expect("cleanup");
913}
914
915/// The mix a project was left at comes back with it: a track's fader, the
916/// master limiter and the rate the timeline was cut at. A fader that vanished
917/// on a reload would be a setting nobody could keep.
918#[test]
919fn the_mix_survives_a_save_and_an_open() {
920 use engine::project::{Lane, LaneKind};
921 let dir = scratch("mix");
922 let source = copy_in(&dir, "test_av.mp4");
923 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
924 let a2 = saved.add_lane(LaneKind::Audio);
925 assert!(saved.set_lane_gain_db(Lane::A1, -6.0));
926 assert!(saved.set_lane_gain_db(a2, 3.0));
927 let limiter = engine::limiter::Limiter {
928 ceiling_db: -2.0,
929 on: true,
930 };
931 assert!(saved.set_limiter(limiter));
932 let path = dir.join("mix.edith");
933 saved.save_project(&path).expect("save");
934
935 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
936 assert_eq!(loaded.lane_gain_db(Lane::A1), -6.0, "A1's fader");
937 assert_eq!(loaded.lane_gain_db(a2), 3.0, "A2's own, not A1's");
938 assert_eq!(loaded.limiter(), limiter);
939 assert!(!loaded.undo(), "a loaded mix is not an undo step");
940 // ...and saving it again is the same bytes: the format holds all of it.
941 let again = dir.join("again.edith");
942 loaded
943 .save_project(&again)
944 .expect("save the loaded project");
945 assert_eq!(
946 std::fs::read(&path).unwrap(),
947 std::fs::read(&again).unwrap(),
948 "save -> load -> save is not a fixed point"
949 );
950}
951
952/// The duck is a mix setting like a volume: it is written, comes back as the
953/// same numbers, and saving the loaded project again is the same bytes.
954#[test]
955fn a_duck_survives_a_save_and_an_open() {
956 use engine::duck::Duck;
957 use engine::project::{Lane, LaneKind};
958 let dir = scratch("duck");
959 let source = copy_in(&dir, "test_av.mp4");
960 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
961 let a2 = saved.add_lane(LaneKind::Audio);
962 let duck = Duck {
963 under: 0,
964 depth_db: -15.0,
965 threshold_db: -35.0,
966 attack_ms: 50.0,
967 release_ms: 600.0,
968 };
969 // A2 is empty, which a volume does not mind and a duck does not need: the
970 // key it names (A1) is the lane with the sound in it.
971 assert!(saved.set_lane_duck(a2, Some(duck)), "ducked under A1");
972 let path = dir.join("duck.edith");
973 saved.save_project(&path).expect("save");
974
975 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
976 assert_eq!(
977 loaded.lane_duck(a2),
978 Some(duck),
979 "the same numbers come back"
980 );
981 assert_eq!(loaded.lane_duck(Lane::A1), None, "and A1 is not ducked");
982 assert!(!loaded.undo(), "a loaded duck is not an undo step");
983 let again = dir.join("again.edith");
984 loaded
985 .save_project(&again)
986 .expect("save the loaded project");
987 assert_eq!(
988 std::fs::read(&path).unwrap(),
989 std::fs::read(&again).unwrap(),
990 "save -> load -> save is not a fixed point"
991 );
992}
993
994/// The v11 bump's own round trip: the HDR rendition a project was left in comes
995/// back with it, and a file written before there were any is the reference one.
996/// A picked look that vanished on a reload would be a setting nobody could keep
997/// -- the mix test's claim, for the picture.
998#[test]
999fn the_tone_map_preset_survives_a_save_and_an_open() {
1000 use engine::tonemap::Preset;
1001 let dir = scratch("tone");
1002 let source = copy_in(&dir, "test_av.mp4");
1003 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
1004 assert_eq!(saved.tone(), Preset::Reference, "nobody has picked yet");
1005 assert!(saved.set_tone(Preset::Vivid));
1006 let path = dir.join("tone.edith");
1007 saved.save_project(&path).expect("save");
1008
1009 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
1010 assert_eq!(loaded.tone(), Preset::Vivid);
1011 assert!(!loaded.undo(), "a loaded rendition is not an undo step");
1012 // ...and saving it again is the same bytes, as the mix is.
1013 let again = dir.join("again.edith");
1014 loaded
1015 .save_project(&again)
1016 .expect("save the loaded project");
1017 assert_eq!(
1018 std::fs::read(&path).unwrap(),
1019 std::fs::read(&again).unwrap(),
1020 "save -> load -> save is not a fixed point"
1021 );
1022
1023 // A project written before v11 says nothing about a rendition, and means the
1024 // published one -- the picture it was always shown in.
1025 let old = dir.join("old.edith");
1026 std::fs::write(
1027 &old,
1028 "edith 10\nplayhead 0\nresolution 1920 1080\nsource 0 test_av.mp4\n\
1029 video 1 0 0 24 0 - - - fit 1000\naudio 1 0 0 24 0 - - - fit 1000\n",
1030 )
1031 .expect("write a v10 project");
1032 let older = PlaybackSession::open_project(&old).expect("open the v10 project");
1033 assert_eq!(older.tone(), Preset::Reference);
1034}
1035
1036/// The other half of the v9 bump, and the debt it pays: a timeline holding
1037/// nothing but a song has no file to take a frame rate from, so it used to come
1038/// back at the canvas rate whatever it was cut at. The written `fps` is now
1039/// preferred over that inference -- and a project whose scaffold *does* have a
1040/// picture still takes the rate from the file, which is what every clip on it
1041/// was conformed to.
1042#[test]
1043fn a_song_only_project_comes_back_at_the_rate_it_names() {
1044 let dir = scratch("song_fps");
1045 copy_in(&dir, "test_tone.mp3");
1046 let path = dir.join("song.edith");
1047 std::fs::write(
1048 &path,
1049 "edith 10\nplayhead 0\nresolution 1920 1080\nfps 24.0\nsource 0 test_tone.mp3\n\
1050 video 1\naudio 1 0 0 24 0 - - - fit 1000\n",
1051 )
1052 .expect("write a v9 song project");
1053 let loaded = PlaybackSession::open_project(&path).expect("a song-only project opens");
1054 assert_eq!(loaded.meta().frame_rate, 24.0, "the rate the file names");
1055 assert_eq!(loaded.timeline_duration(), 1.0, "one second at 24 fps");
1056
1057 // The same file without the line -- which is every dialect before v9 --
1058 // still comes back at the canvas rate, so an old project is unchanged.
1059 let old = dir.join("song8.edith");
1060 std::fs::write(
1061 &old,
1062 "edith 8\nplayhead 0\nresolution 1920 1080\nsource 0 test_tone.mp3\n\
1063 video 1\naudio 1 0 0 24 0 - - - fit 1000\n",
1064 )
1065 .expect("write a v8 song project");
1066 let loaded = PlaybackSession::open_project(&old).expect("a v8 song project opens");
1067 assert_eq!(loaded.meta().frame_rate, 30.0, "the canvas rate, as before");
1068}
1069
1070/// A hand-edited `.edith` whose caption names a microsecond window no project
1071/// can hold: the whole load, the same edit a 30-second window survives, and the
1072/// refusal the loader owes for it.
1073///
1074/// A caption's window is the one unbounded number this format takes. The
1075/// never-empty check at `edith.rs` asks whether `out_us > in_us` and never how
1076/// large either is, so `out_us = i64::MAX` was a legal line -- and every split,
1077/// ripple and group trim since cuts the window *by proportion*
1078/// (`SubClip::window_at`), a plain `window_us() * into` that overflows on the
1079/// first frame inside the caption. That is a panic on the UI thread the first
1080/// time a hand ripple-deletes across the caption, and in a release build the
1081/// same line wraps the caption's clock to a negative number instead. So the
1082/// window is bounded at the door, by name, and the arithmetic behind the cut
1083/// is made total as well.
1084#[test]
1085fn a_caption_window_no_project_can_hold_is_refused_by_name() {
1086 let dir = scratch("caption_window");
1087 let media = copy_in(&dir, "test_av.mp4");
1088 let subs = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
1089 .join("tests/data/test_subs.srt")
1090 .canonicalize()
1091 .expect("the subtitle fixture");
1092
1093 // The control first: a 30-second window over 30 frames, the shape every
1094 // save writes, and the shape this file's whole edit path is proved on.
1095 let ok = dir.join("window_ok.edith");
1096 std::fs::write(
1097 &ok,
1098 format!(
1099 "edith 24\nplayhead 0\nresolution 1280 720\nsource 0 {}\nsubtitle - {}\n\
1100 video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\
1101 sub 1 0 30 0 0 30000000\n",
1102 media.display(),
1103 subs.display(),
1104 ),
1105 )
1106 .expect("write");
1107 let mut control = PlaybackSession::open_project(&ok).expect("a 30s caption window opens");
1108 assert_eq!(control.sub_lane(Lane::S1)[0].out_us, 30_000_000);
1109 // The edits the unbounded window used to survive: a split inside the
1110 // caption and a ripple across it. Both cut the window by proportion.
1111 assert!(
1112 control.cut_at(0.5),
1113 "the playhead splits the caption under it"
1114 );
1115 assert_eq!(
1116 control.sub_lane(Lane::S1).len(),
1117 2,
1118 "the split cut the caption in two"
1119 );
1120 control.undo();
1121 control
1122 .cut_regions(&[(0, 10)], &[Lane::S1])
1123 .expect("a ripple across the caption, scoped to its own lane");
1124 let after = control.sub_lane(Lane::S1);
1125 assert_eq!(after.len(), 1, "the caption's tail is still there");
1126 assert_eq!(
1127 (after[0].start, after[0].frames),
1128 (0, 20),
1129 "and it closed the hole the cut left"
1130 );
1131
1132 // The same file, the same lane, the same edit -- with a window no project
1133 // can hold. This is the line that used to load and then panic.
1134 let huge = dir.join("window_huge.edith");
1135 std::fs::write(
1136 &huge,
1137 format!(
1138 "edith 24\nplayhead 0\nresolution 1280 720\nsource 0 {}\nsubtitle - {}\n\
1139 video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\
1140 sub 1 0 30 0 0 9223372036854775807\n",
1141 media.display(),
1142 subs.display(),
1143 ),
1144 )
1145 .expect("write");
1146 let err = PlaybackSession::open_project(&huge)
1147 .err()
1148 .unwrap_or_else(|| {
1149 panic!(
1150 "a caption window of {}us loaded, and the first split across it \
1151 panics in SubClip::window_at",
1152 i64::MAX
1153 )
1154 })
1155 .to_string();
1156 assert!(
1157 err.contains("window") && err.contains("9223372036854775807"),
1158 "the refusal names the window it is refusing, and the value: {err}"
1159 );
1160 assert!(err.starts_with("line 7"), "and which line said it: {err}");
1161
1162 // The same bound at the other two doors a caption comes through, because a
1163 // caller is not more trustworthy than a file: the placement a hand drags
1164 // out of the palette is a `SubClip` somebody built, and it goes through
1165 // `place_sub` rather than the parse.
1166 let mut session = PlaybackSession::open(&media).expect("open the fixture");
1167 session
1168 .import_subtitles(&subs)
1169 .expect("the fixture's one track");
1170 let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1171 let err = session
1172 .place_sub(
1173 s1,
1174 0,
1175 engine::project::SubClip {
1176 start: 0,
1177 frames: 30,
1178 track: 0,
1179 in_us: 0,
1180 out_us: i64::MAX,
1181 link: None,
1182 },
1183 )
1184 .expect_err("a window no project can hold is refused by name, not clamped")
1185 .to_string();
1186 assert!(
1187 err.contains("window") && err.contains(&i64::MAX.to_string()),
1188 "the placement refusal names the window and the value: {err}"
1189 );
1190 assert!(
1191 session.sub_lane(s1).is_empty(),
1192 "and a refusal places nothing: {:?}",
1193 session.sub_lane(s1)
1194 );
1195}
1196
1197/// A subtitle track whose last cue ends `hours` in, written into `dir`.
1198///
1199/// A caption's window is a window into SOURCE media, so its width is however
1200/// long the recording was. Nothing in the format bounds that, and a lecture
1201/// archive or a conference stream is tens of hours -- which is what a ceiling
1202/// under a day refuses back at the user. The hour field is parsed as a plain
1203/// `u32` with no digit limit, so 960 is a legal 40 days.
1204fn long_srt(dir: &Path, hours: u32) -> PathBuf {
1205 let path = dir.join(format!("subs_{hours}h.srt"));
1206 std::fs::write(
1207 &path,
1208 format!(
1209 "1\n00:00:00,500 --> 00:00:01,500\nfirst line\n\n\
1210 2\n00:00:01,600 --> {hours}:00:00,000\nlast line of a long recording\n"
1211 ),
1212 )
1213 .expect("write a long subtitle track");
1214 path
1215}
1216
1217/// The round trip a trim has to survive: grow a caption's window to the room
1218/// the engine itself offers, save, and open the file that was just written.
1219///
1220/// This is the hole a ceiling on its own leaves. Every door -- the parse,
1221/// `with_subs`, `place_sub` -- only ever sees a window on the way *in*, so a
1222/// project could load legally and then be trimmed past the ceiling: `save`
1223/// wrote the line and the loader refused it again, which is the user's own
1224/// saved work stopping, with no in-app way back (the trim is committed and the
1225/// undo does not survive a restart). So growth clamps (`grow_window`) and the
1226/// room the front-end drags against is walled by the same number, and the two
1227/// agree by construction rather than by luck.
1228#[test]
1229fn a_trim_grown_to_the_offered_room_saves_and_reopens() {
1230 use engine::project::{Edge, MAX_WINDOW_US, SubClip};
1231
1232 let dir = scratch("caption_growth");
1233 let media = copy_in(&dir, "test_av.mp4");
1234 // 40 days of track, comfortably past the 30-day ceiling: only the growth
1235 // clamp can keep this inside it, never the ceiling itself.
1236 let subs = long_srt(&dir, 960);
1237
1238 let mut session = PlaybackSession::open(&media).expect("open the fixture");
1239 session
1240 .import_subtitles(&subs)
1241 .expect("a 40-day subtitle track imports");
1242 let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1243 session
1244 .place_sub(
1245 s1,
1246 60,
1247 SubClip {
1248 start: 0,
1249 frames: 90,
1250 track: 0,
1251 in_us: 0,
1252 out_us: 30_000_000,
1253 link: None,
1254 },
1255 )
1256 .expect("a 30s window places");
1257
1258 // The room the drag is drawn against. It must itself stop at the ceiling:
1259 // an engine that offers a trim its own `trim_sub` would clamp is a drag
1260 // that snaps back at the end of the gesture.
1261 let (_, last) = session
1262 .trim_sub_room(s1, 0, Edge::End)
1263 .expect("a room for a caption that is there");
1264 session
1265 .trim_sub(s1, 0, Edge::End, last)
1266 .expect("the trim to its own offered room");
1267
1268 let grown = session.sub_lane(s1)[0];
1269 assert!(
1270 (grown.out_us - grown.in_us) <= MAX_WINDOW_US,
1271 "trimming to the offered room gave a {}us window, past the {}us ceiling",
1272 grown.out_us - grown.in_us,
1273 MAX_WINDOW_US
1274 );
1275
1276 let saved = dir.join("grown.edith");
1277 session.save_project(&saved).expect("save");
1278 let reopened = PlaybackSession::open_project(&saved)
1279 .expect("a project the editor wrote must open again -- that is the whole claim");
1280 let back = reopened.sub_lane(s1)[0];
1281 assert_eq!(
1282 (back.in_us, back.out_us),
1283 (grown.in_us, grown.out_us),
1284 "and the window came back as it was saved"
1285 );
1286}
1287
1288/// The ceiling itself, against the case it was measured on: a 25-hour
1289/// recording. `place_sub` is the door a palette row's drag comes through
1290/// (`sub_of_track` builds the placement from the track's own last cue), so a
1291/// ceiling under the length of real media refuses the user's own file back at
1292/// them, with nothing placed.
1293#[test]
1294fn a_caption_over_a_day_of_track_places_and_round_trips() {
1295 use engine::project::MAX_WINDOW_US;
1296
1297 let dir = scratch("caption_long_track");
1298 let media = copy_in(&dir, "test_av.mp4");
1299 let subs = long_srt(&dir, 25);
1300 let whole_track_us = 25i64 * 60 * 60 * 1_000_000;
1301 assert!(
1302 whole_track_us > 86_400_000_000,
1303 "the fixture must really be over a day, or this test proves nothing"
1304 );
1305 assert!(
1306 whole_track_us < MAX_WINDOW_US,
1307 "and comfortably under the ceiling, or the test is not about the ceiling"
1308 );
1309
1310 let mut session = PlaybackSession::open(&media).expect("open the fixture");
1311 assert_eq!(
1312 session
1313 .import_subtitles(&subs)
1314 .expect("a 25-hour track imports"),
1315 1,
1316 "one track"
1317 );
1318 let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1319 // Exactly what `Player::sub_of_track` hands `place_sub` for palette row 0.
1320 let fps = session.meta().frame_rate;
1321 session
1322 .place_sub(
1323 s1,
1324 0,
1325 engine::project::SubClip {
1326 start: 0,
1327 frames: ((whole_track_us as f64) * fps / 1e6).round() as u32,
1328 track: 0,
1329 in_us: 0,
1330 out_us: whole_track_us,
1331 link: None,
1332 },
1333 )
1334 .expect("a caption over a 25-hour recording places -- that is the claim");
1335
1336 let saved = dir.join("long.edith");
1337 session.save_project(&saved).expect("save");
1338 let reopened = PlaybackSession::open_project(&saved).expect("and reopens");
1339 assert_eq!(
1340 reopened.sub_lane(s1)[0].out_us,
1341 whole_track_us,
1342 "the whole window survived the round trip"
1343 );
1344}
1345
1346/// The second writer, which is the one a primary-trim-only fix would miss.
1347///
1348/// [`write_sub_edge`] carries a *sibling* in a group by the same delta as the
1349/// member that was dragged, and reaches the same `out_us`. `follow_group`
1350/// skips the dragged member, so the route here is a second caption in the
1351/// group on another lane: it is not the one whose `trim_sub` arm is fixed, so
1352/// nothing about the dragged caption's own clamp bounds it.
1353///
1354/// The sibling starts deep into the track -- 20 days in -- so the same delta
1355/// that stops the dragged caption exactly at the ceiling carries the sibling
1356/// past it. Without the clamp on this writer the sibling is 20 days wider than
1357/// the ceiling when `save_project` runs, and the file it writes will not open.
1358#[test]
1359fn a_group_trim_carries_its_sibling_no_further_than_the_ceiling() {
1360 use engine::project::{Edge, LaneKind, MAX_WINDOW_US, SubClip};
1361
1362 let dir = scratch("caption_group_growth");
1363 let media = copy_in(&dir, "test_av.mp4");
1364 let subs = long_srt(&dir, 960);
1365 let day = 24 * 60 * 60 * 1_000_000i64;
1366
1367 let mut session = PlaybackSession::open(&media).expect("open the fixture");
1368 session
1369 .import_subtitles(&subs)
1370 .expect("a 40-day track imports");
1371 let s1 = session.add_lane(LaneKind::Subtitle);
1372 let s2 = session.add_lane(LaneKind::Subtitle);
1373 let caption = |in_us| SubClip {
1374 start: 0,
1375 frames: 90,
1376 track: 0,
1377 in_us,
1378 out_us: in_us + 30_000_000,
1379 link: None,
1380 };
1381 session
1382 .place_sub(s1, 60, caption(0))
1383 .expect("the dragged caption places");
1384 session
1385 .place_sub(s2, 60, caption(20 * day))
1386 .expect("its sibling, 20 days into the track");
1387 session
1388 .group_all(&[(s1, 0), (s2, 0)])
1389 .expect("the two captions into one group");
1390
1391 // Drag the first caption's tail out to the room the engine offers it, which
1392 // is a 40-day track so the room is the ceiling, not the track.
1393 let (_, last) = session
1394 .trim_sub_room(s1, 0, Edge::End)
1395 .expect("a room for the dragged caption");
1396 session
1397 .trim_sub(s1, 0, Edge::End, last)
1398 .expect("the drag commits");
1399
1400 let dragged = session.sub_lane(s1)[0];
1401 assert_eq!(
1402 dragged.out_us - dragged.in_us,
1403 MAX_WINDOW_US,
1404 "the dragged caption stops exactly at the ceiling, which is the room it was offered"
1405 );
1406 let sibling = session.sub_lane(s2)[0];
1407 assert!(
1408 (sibling.out_us - sibling.in_us) <= MAX_WINDOW_US,
1409 "the group carried the sibling to a {}us window, past the {}us ceiling -- \
1410 the dragged caption's own clamp does not reach here",
1411 sibling.out_us - sibling.in_us,
1412 MAX_WINDOW_US
1413 );
1414
1415 let saved = dir.join("group.edith");
1416 session.save_project(&saved).expect("save");
1417 PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");
1418}
1419
1420/// The composer that no per-arm check finds: a **rejoin** takes the left
1421/// half's `in_us` and the right half's `out_us`, and `sub_joinable` has just
1422/// established that the two halves are consecutive in the *source* -- so the
1423/// joined width is the SUM of two widths that each passed their own door.
1424///
1425/// Neither half is illegal and the whole is. Two 20-day windows, one ordinary
1426/// undo-a-split keystroke, a 40-day window, and a file the editor writes and
1427/// cannot reopen.
1428#[test]
1429fn a_rejoin_of_two_legal_windows_stays_inside_the_ceiling() {
1430 use engine::project::{MAX_WINDOW_US, SubClip};
1431
1432 let dir = scratch("caption_rejoin");
1433 let media = copy_in(&dir, "test_av.mp4");
1434 let subs = long_srt(&dir, 960);
1435 let twenty_days = 20 * 24 * 60 * 60 * 1_000_000i64;
1436
1437 let mut session = PlaybackSession::open(&media).expect("open the fixture");
1438 session
1439 .import_subtitles(&subs)
1440 .expect("a 40-day track imports");
1441 let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1442 // Two adjacent halves, each a legal 20-day window, the second starting
1443 // exactly where the first's window ends -- which is what `sub_joinable`
1444 // requires, and what makes the join a sum.
1445 for (at, frames, in_us) in [(0u32, 10_000u32, 0i64), (10_000, 10_000, twenty_days)] {
1446 session
1447 .place_sub(
1448 s1,
1449 at,
1450 SubClip {
1451 start: 0,
1452 frames,
1453 track: 0,
1454 in_us,
1455 out_us: in_us + twenty_days,
1456 link: None,
1457 },
1458 )
1459 .unwrap_or_else(|e| panic!("a 20-day half at {at} places: {e}"));
1460 }
1461 let halves: Vec<i64> = session
1462 .sub_lane(s1)
1463 .iter()
1464 .map(|s| s.out_us - s.in_us)
1465 .collect();
1466 assert!(
1467 halves.iter().all(|&w| w <= MAX_WINDOW_US),
1468 "both halves must be legal for this to be about the join: {halves:?}"
1469 );
1470
1471 // `regroup_at` is in seconds, like every other front-end door; the
1472 // boundary is the frame the second half starts on.
1473 let at_secs = 10_000.0 / session.meta().frame_rate;
1474 assert!(
1475 session.regroup_at(at_secs),
1476 "the two halves rejoin -- the inverse of the split that made them"
1477 );
1478 let joined = &session.sub_lane(s1)[0];
1479 assert!(
1480 (joined.out_us - joined.in_us) <= MAX_WINDOW_US,
1481 "the rejoin summed two {}us windows into {}us, past the {}us ceiling",
1482 MAX_WINDOW_US / 2,
1483 joined.out_us - joined.in_us,
1484 MAX_WINDOW_US
1485 );
1486
1487 let saved = dir.join("rejoined.edith");
1488 session.save_project(&saved).expect("save");
1489 PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");
1490}
1491
1492/// The other half of the ceiling: it bounds a **difference**, so a head trim
1493/// grows the width as surely as a tail trim does, and from the other side.
1494///
1495/// A caption placed a year into its source with a 30-second window is legal.
1496/// One head drag pulling its start back by 200 days of frames makes the
1497/// window 200 days wide while `out_us` never moves -- so bounding `in_us`
1498/// from above, which is all the head arm's own clamp does, says nothing at all
1499/// about the ceiling.
1500#[test]
1501fn a_head_drag_no_further_than_the_ceiling() {
1502 use engine::project::{Edge, MAX_WINDOW_US, SubClip};
1503
1504 let dir = scratch("caption_head_drag");
1505 let media = copy_in(&dir, "test_av.mp4");
1506 let subs = long_srt(&dir, 960);
1507 let day = 24 * 60 * 60 * 1_000_000i64;
1508
1509 let mut session = PlaybackSession::open(&media).expect("open the fixture");
1510 session
1511 .import_subtitles(&subs)
1512 .expect("a 40-day track imports");
1513 let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1514 // A year into the source, 30 seconds of window: legal, and the only kind
1515 // of caption a head drag can make enormous.
1516 session
1517 .place_sub(
1518 s1,
1519 90_000_000,
1520 SubClip {
1521 start: 0,
1522 frames: 900,
1523 track: 0,
1524 in_us: 365 * day,
1525 out_us: 365 * day + 30_000_000,
1526 link: None,
1527 },
1528 )
1529 .expect("a year-deep caption places");
1530
1531 // The head room the engine offers, then the drag to its far end: 200 days
1532 // of source at 30 fps, which the Start arm's own `in_us <= out_us - 1`
1533 // clamp is perfectly happy with.
1534 let (lo, _) = session
1535 .trim_sub_room(s1, 0, Edge::Start)
1536 .expect("a head room");
1537 session
1538 .trim_sub(s1, 0, Edge::Start, lo)
1539 .expect("the head drag commits");
1540
1541 let dragged = session.sub_lane(s1)[0];
1542 assert!(
1543 (dragged.out_us - dragged.in_us) <= MAX_WINDOW_US,
1544 "a head drag onto a year-deep caption gave a {}us window, past the {}us ceiling",
1545 dragged.out_us - dragged.in_us,
1546 MAX_WINDOW_US
1547 );
1548
1549 let saved = dir.join("head.edith");
1550 session.save_project(&saved).expect("save");
1551 PlaybackSession::open_project(&saved).expect("and the file the editor wrote reopens");
1552}
1553
1554/// The lane switches -- hidden, muted, locked -- are the project's and not the
1555/// machine's: each one a person set comes back set on a reload, the file says
1556/// which lane carries which, and saving the reloaded project is the same bytes.
1557#[test]
1558fn the_lane_switches_survive_a_save_and_an_open() {
1559 use engine::project::LaneKind;
1560 let dir = scratch("switches");
1561 let source = copy_in(&dir, "test_av.mp4");
1562 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
1563 let v2 = saved.add_lane(LaneKind::Video);
1564 assert!(saved.set_lane_hidden(v2, true), "a video lane hides");
1565 assert!(saved.set_lane_muted(Lane::A1, true), "an audio lane mutes");
1566 assert!(saved.set_lane_locked(Lane::A1, true), "any lane locks");
1567 let path = dir.join("switches.edith");
1568 saved.save_project(&path).expect("save");
1569 let text = std::fs::read_to_string(&path).expect("read back");
1570 assert!(text.starts_with("edith 30\n"), "{text}");
1571 assert!(text.contains("\nflags video 2 hidden\n"), "{text}");
1572 assert!(text.contains("\nflags audio 1 muted locked\n"), "{text}");
1573
1574 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
1575 assert!(loaded.lane_hidden(v2), "V2 is still hidden");
1576 assert!(!loaded.lane_hidden(Lane::V1), "and V1 is not");
1577 assert!(loaded.lane_muted(Lane::A1), "A1 is still muted");
1578 assert!(loaded.lane_locked(Lane::A1), "and still locked");
1579 assert!(!loaded.undo(), "a loaded switch is not an undo step");
1580
1581 let again = dir.join("again.edith");
1582 loaded
1583 .save_project(&again)
1584 .expect("save the loaded project");
1585 assert_eq!(
1586 std::fs::read(&path).unwrap(),
1587 std::fs::read(&again).unwrap(),
1588 "save -> load -> save is not a fixed point"
1589 );
1590}
1591
1592/// A version 23 project has no switches, so every lane opens showing and audible
1593/// and unlocked, and saving it writes the same lines at version 24 with no
1594/// `flags` line in them.
1595#[test]
1596fn a_version_23_project_opens_with_every_lane_showing_and_audible() {
1597 let dir = scratch("v23_switches");
1598 copy_in(&dir, "test_av.mp4");
1599 let path = dir.join("v23.edith");
1600 let text = "edith 23\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
1601 video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\
1602 audio 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n";
1603 std::fs::write(&path, text).expect("write a v23 file");
1604
1605 let loaded = PlaybackSession::open_project(&path).expect("a v23 project opens");
1606 assert!(!loaded.lane_hidden(Lane::V1));
1607 assert!(!loaded.lane_muted(Lane::A1));
1608 assert!(!loaded.lane_locked(Lane::A1));
1609 let again = dir.join("again.edith");
1610 loaded.save_project(&again).expect("save");
1611 assert_eq!(
1612 std::fs::read_to_string(&again).expect("read back"),
1613 text.replace("edith 23", "edith 30"),
1614 "the same project, at the current version, with no switch line"
1615 );
1616}
1617
1618/// A retyped cue is the file's words no longer: it saves as a `cue` line that
1619/// names its track and cue, reopens with the typed words over the file's own,
1620/// survives a re-save of the reopened project, and one undo takes it back.
1621#[test]
1622fn a_retyped_cue_saves_reopens_and_undoes_in_one_step() {
1623 let dir = scratch("cue_text");
1624 let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open");
1625 session.set_gain(0.0);
1626 let srt = dir.join("subs.srt");
1627 std::fs::write(
1628 &srt,
1629 "1\n00:00:01,000 --> 00:00:02,000\nFirst\n\n2\n00:00:03,000 --> 00:00:04,000\nSecond\n\n",
1630 )
1631 .expect("write the subtitle file");
1632 assert_eq!(session.import_subtitles(&srt).expect("import"), 1);
1633 assert_eq!(session.subtitles()[0].cues.len(), 2, "two cues in the file");
1634
1635 let typed = "Ça va?\nback\\slash";
1636 assert!(session.set_cue_text(0, 1, typed));
1637 assert_eq!(session.subtitles()[0].cues[1].text, typed);
1638 assert!(
1639 !session.set_cue_text(0, 1, typed),
1640 "the same words are no edit"
1641 );
1642 assert!(!session.set_cue_text(0, 9, "x"), "a cue that is not there");
1643
1644 let path = dir.join("cues.edith");
1645 session.save_project(&path).expect("save");
1646 let text = std::fs::read_to_string(&path).expect("read back");
1647 assert!(
1648 text.contains("cue 0 1 Ça va?\\nback\\\\slash\n"),
1649 "the override reached the file:\n{text}"
1650 );
1651
1652 let loaded = PlaybackSession::open_project(&path).expect("reopen");
1653 assert_eq!(
1654 loaded.subtitles()[0].cues[1].text,
1655 typed,
1656 "the typed words come back"
1657 );
1658 assert_eq!(
1659 loaded.subtitles()[0].cues[0].text,
1660 "First",
1661 "the rest is the file's"
1662 );
1663
1664 loaded.save_project(&path).expect("re-save");
1665 let again = PlaybackSession::open_project(&path).expect("reopen the re-save");
1666 assert_eq!(
1667 again.subtitles()[0].cues[1].text,
1668 typed,
1669 "a re-save keeps the override"
1670 );
1671
1672 assert!(session.undo(), "one step back takes the retype");
1673 assert_eq!(session.subtitles()[0].cues[1].text, "Second");
1674 std::fs::remove_dir_all(&dir).expect("cleanup");
1675}
1676
1677/// A cue added to a track is no file's cue: it saves as a `cueadd` line, reopens
1678/// in its place among the file's cues, and one undo takes the add back. A cue
1679/// that overlaps one already there is refused, changing nothing, unless it is
1680/// `overwrite`, which replaces the overlapped cues in the same step.
1681#[test]
1682fn an_added_cue_saves_reopens_in_place_and_undoes_in_one_step() {
1683 let dir = scratch("cue_add");
1684 let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open");
1685 session.set_gain(0.0);
1686 let srt = dir.join("subs.srt");
1687 std::fs::write(
1688 &srt,
1689 "1\n00:00:01,000 --> 00:00:02,000\nFirst\n\n2\n00:00:03,000 --> 00:00:04,000\nSecond\n\n",
1690 )
1691 .expect("write the subtitle file");
1692 assert_eq!(session.import_subtitles(&srt).expect("import"), 1);
1693
1694 let at = session
1695 .add_cue(0, 2_200_000, 2_800_000, "Added\nline two", false)
1696 .expect("a gap between the two cues takes a cue");
1697 assert_eq!(at, 1, "sorted by start, so it lands between the two");
1698 assert!(
1699 session.add_cue(0, 1_500_000, 2_500_000, "Clash", false).is_err(),
1700 "an overlap is refused without overwrite"
1701 );
1702 assert_eq!(session.subtitles()[0].cues.len(), 3, "the refusal changed nothing");
1703
1704 let path = dir.join("cues_add.edith");
1705 session.save_project(&path).expect("save");
1706 let text = std::fs::read_to_string(&path).expect("read back");
1707 assert!(
1708 text.contains("cueadd 0 2200000 2800000 Added\\nline two\n"),
1709 "the added cue reached the file:\n{text}"
1710 );
1711
1712 let loaded = PlaybackSession::open_project(&path).expect("reopen");
1713 let cues = loaded.subtitles()[0].cues.clone();
1714 assert_eq!(cues.len(), 3, "the added cue is back among the file's two");
1715 assert_eq!(
1716 (cues[0].text.as_str(), cues[1].text.as_str(), cues[2].text.as_str()),
1717 ("First", "Added\nline two", "Second")
1718 );
1719 assert_eq!((cues[1].start_us, cues[1].end_us), (2_200_000, 2_800_000));
1720
1721 // An overwrite replaces the cue it overlaps, and the replacement survives a re-save.
1722 loaded.save_project(&path).expect("re-save");
1723 let mut again = PlaybackSession::open_project(&path).expect("reopen the re-save");
1724 assert_eq!(again.subtitles()[0].cues.len(), 3, "the re-save keeps the added cue");
1725 again.add_cue(0, 2_500_000, 2_900_000, "Over", true).expect("overwrite replaces the cue it overlaps");
1726 let texts: Vec<String> = again.subtitles()[0].cues.iter().map(|c| c.text.clone()).collect();
1727 assert_eq!(texts, ["First", "Over", "Second"], "the added cue it overlapped is gone");
1728 assert!(again.undo(), "one step back takes the overwrite");
1729 assert_eq!(
1730 again.subtitles()[0].cues.iter().map(|c| c.text.as_str()).collect::<Vec<_>>(),
1731 ["First", "Added\nline two", "Second"],
1732 "and brings the replaced cue back"
1733 );
1734
1735 assert!(session.undo(), "one step back takes the add");
1736 assert_eq!(session.subtitles()[0].cues.len(), 2, "the file's two cues again");
1737 std::fs::remove_dir_all(&dir).expect("cleanup");
1738}
1739
1740#[test]
1741fn an_empty_project_takes_a_clip_and_comes_back_the_same() {
1742 let dir = scratch("empty");
1743 let media = copy_in(&dir, "test_av.mp4");
1744 let mut session = PlaybackSession::empty(640, 360, 25.0).expect("an empty timeline");
1745 assert!(session.is_empty(), "nothing on any lane yet");
1746 assert_eq!(session.timeline_duration(), 0.0);
1747 session.set_gain(0.0);
1748 session.import(&media).expect("import");
1749 assert!(
1750 session
1751 .place_stream_at(0.0, &media, 0, None)
1752 .expect("place")
1753 );
1754 let file = dir.join("empty.edith");
1755 session.save_project(&file).expect("save");
1756 let back = PlaybackSession::open_project(&file).expect("reopen");
1757 assert_eq!(back.resolution(), (640, 360));
1758 assert_eq!(back.sources().len(), 1);
1759 assert_eq!(back.clip_spans(), session.clip_spans());
1760 assert!(
1761 back.timeline_duration() > 4.5,
1762 "{}",
1763 back.timeline_duration()
1764 );
1765}
1766
1767/// Titles survive a save and an open in list order, with their text and style
1768/// exactly as typed -- a newline, a backslash and a percent sign included -- and
1769/// a save of the reopened project is the same bytes.
1770#[test]
1771fn titles_survive_a_save_and_an_open() {
1772 use engine::project::Title;
1773 use engine::text::{Outline, Place, TextStyle};
1774 let dir = scratch("titles");
1775 let source = copy_in(&dir, "test_av.mp4");
1776 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
1777 let card = Title {
1778 start: 150,
1779 frames: 90,
1780 text: "Episode 17\n100% \\ done".into(),
1781 style: TextStyle {
1782 size: 80,
1783 outline: Outline::Thick,
1784 boxed: false,
1785 place: Place::Centre,
1786 ..TextStyle::default()
1787 },
1788 fade_in: 10,
1789 fade_out: 5,
1790 in_anim: Default::default(),
1791 out_anim: Default::default(),
1792 anim_frames: 0,
1793 keys: Vec::new(),
1794 };
1795 let lower = Title {
1796 start: 60,
1797 frames: 120,
1798 text: "Jane Doe \u{2014} Engineer".into(),
1799 style: TextStyle::default(),
1800 fade_in: 0,
1801 fade_out: 0,
1802 in_anim: Default::default(),
1803 out_anim: Default::default(),
1804 anim_frames: 0,
1805 keys: Vec::new(),
1806 };
1807 saved.add_title(card.clone()).expect("the card is added");
1808 saved
1809 .add_title(lower.clone())
1810 .expect("the lower third is added");
1811 assert_eq!(
1812 saved.titles(),
1813 [lower.clone(), card.clone()],
1814 "sorted by start"
1815 );
1816 let path = dir.join("titles.edith");
1817 saved.save_project(&path).expect("save");
1818
1819 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
1820 assert_eq!(loaded.titles(), [lower, card], "the same titles come back");
1821 assert!(!loaded.undo(), "a loaded title is not an undo step");
1822 let again = dir.join("again.edith");
1823 loaded
1824 .save_project(&again)
1825 .expect("save the loaded project");
1826 assert_eq!(
1827 std::fs::read(&path).unwrap(),
1828 std::fs::read(&again).unwrap(),
1829 "save -> load -> save is not a fixed point"
1830 );
1831}
1832
1833/// A title's colour, weight, alignment, side and box colour survive a save and an open as format
1834/// v49; a project whose titles keep the default look is still a v25-era file bar its header.
1835#[test]
1836fn a_title_look_survives_a_save_and_an_open_as_v49() {
1837 use engine::project::Title;
1838 use engine::text::{Align, HPlace, Outline, Place, TextStyle, Weight};
1839 let dir = scratch("title_look");
1840 let source = copy_in(&dir, "test_av.mp4");
1841 let mut plain = PlaybackSession::open(&source).expect("open the fixture");
1842 let mk = |style: TextStyle| Title { start: 30, frames: 60, text: "Jane\nDirector".into(), style, fade_in: 0, fade_out: 0, in_anim: Default::default(), out_anim: Default::default(), anim_frames: 0, keys: Vec::new() };
1843 plain.add_title(mk(TextStyle::default())).expect("added");
1844 let p0 = dir.join("plain.edith");
1845 plain.save_project(&p0).expect("save");
1846 let text = std::fs::read_to_string(&p0).unwrap();
1847 assert!(!text.starts_with("edith 49") && !text.contains("titlestyle"), "{}", text.lines().next().unwrap());
1848
1849 let look = TextStyle {
1850 outline: Outline::None,
1851 place: Place::Bottom,
1852 hplace: HPlace::Left,
1853 align: Align::Left,
1854 weight: Weight::Bold,
1855 ink: [255, 204, 0],
1856 box_color: [17, 34, 51, 200],
1857 ..TextStyle::default()
1858 };
1859 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
1860 saved.add_title(mk(look.clone())).expect("added");
1861 let path = dir.join("look.edith");
1862 saved.save_project(&path).expect("save");
1863 let text = std::fs::read_to_string(&path).unwrap();
1864 assert!(text.starts_with("edith 49\n"), "{}", text.lines().next().unwrap());
1865 let loaded = PlaybackSession::open_project(&path).expect("open the project");
1866 assert_eq!(loaded.titles()[0].style, look);
1867 // Older edith refuses the newer line by its header, and this one refuses it under an old header.
1868 let old = text.replacen("edith 49", "edith 48", 1);
1869 let old_path = dir.join("old.edith");
1870 std::fs::write(&old_path, old).unwrap();
1871 assert!(PlaybackSession::open_project(&old_path).is_err());
1872 let again = dir.join("again.edith");
1873 loaded.save_project(&again).expect("save the loaded project");
1874 assert_eq!(std::fs::read(&path).unwrap(), std::fs::read(&again).unwrap());
1875}
1876
1877/// A clip's own gain is a per-clip setting like an equalizer: it is written on
1878/// its own `clipgain` line only where it is not unity, comes back on the same
1879/// clip, and saving the loaded project again is the same bytes. Version 27 is
1880/// the dialect that says so.
1881#[test]
1882fn a_clip_gain_survives_a_save_and_an_open() {
1883 use engine::project::{Lane, LaneKind};
1884 let dir = scratch("clip_gain");
1885 let source = copy_in(&dir, "test_av.mp4");
1886 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
1887 assert!(
1888 saved.cut_at(2.0),
1889 "two clips on A1, so the index means something"
1890 );
1891 let a1 = Lane::new(LaneKind::Audio, 0);
1892 assert!(saved.set_clip_gain(a1, 0, -6.0), "the first clip down 6 dB");
1893 assert!(saved.set_clip_gain(a1, 1, 3.0), "the second up 3 dB");
1894 assert!(
1895 !saved.set_clip_gain(Lane::V1, 0, -6.0),
1896 "a picture has no gain"
1897 );
1898 let path = dir.join("clip_gain.edith");
1899 saved.save_project(&path).expect("save");
1900
1901 let text = std::fs::read_to_string(&path).expect("read back");
1902 assert!(text.starts_with("edith 30\n"), "written as version 29");
1903 assert!(text.contains("clipgain audio 1 0 -6\n"), "{text}");
1904 assert!(text.contains("clipgain audio 1 1 3\n"), "{text}");
1905 assert_eq!(
1906 text.matches("clipgain ").count(),
1907 2,
1908 "unity is never written"
1909 );
1910
1911 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
1912 assert_eq!(loaded.clip_gain_of(a1, 0), Some(-6.0));
1913 assert_eq!(loaded.clip_gain_of(a1, 1), Some(3.0));
1914 assert_eq!(
1915 loaded.clip_gain_of(Lane::V1, 0),
1916 Some(0.0),
1917 "a picture is unity"
1918 );
1919 assert!(!loaded.undo(), "a loaded gain is not an undo step");
1920 let again = dir.join("again.edith");
1921 loaded
1922 .save_project(&again)
1923 .expect("save the loaded project");
1924 assert_eq!(
1925 std::fs::read(&path).unwrap(),
1926 std::fs::read(&again).unwrap(),
1927 "save -> load -> save is not a fixed point"
1928 );
1929}
1930
1931/// A volume envelope saves as one `gainkeys` line, reopens with its points in
1932/// order, and one undo takes a point back out. A flat clip writes no such line.
1933#[test]
1934fn a_gain_envelope_saves_reopens_and_undoes_in_one_step() {
1935 let dir = scratch("gain_keys");
1936 let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open");
1937 let a1 = Lane::new(LaneKind::Audio, 0);
1938 let flat = dir.join("flat.edith");
1939 session.save_project(&flat).expect("save a flat project");
1940 assert!(
1941 !std::fs::read_to_string(&flat)
1942 .expect("read")
1943 .contains("gainkeys"),
1944 "a clip with no points writes no gainkeys line"
1945 );
1946
1947 assert!(session.set_gain_keys(
1948 a1,
1949 0,
1950 vec![GainKey::new(0, 0.0), GainKey::new(30, -20.0),],
1951 ));
1952 assert!(session.add_gain_key(a1, 0, GainKey::new(60, -6.5)));
1953 let path = dir.join("keys.edith");
1954 session.save_project(&path).expect("save");
1955 let text = std::fs::read_to_string(&path).expect("read back");
1956 assert!(text.starts_with("edith 30\n"), "the current version");
1957 assert!(
1958 text.contains("gainkeys audio 1 0 0:0 30:-20 60:-6.5\n"),
1959 "one line for the clip's points: {text}"
1960 );
1961
1962 let reopened = PlaybackSession::open_project(&path).expect("reopen");
1963 assert_eq!(
1964 reopened.gain_keys_of(a1, 0),
1965 &[
1966 GainKey::new(0, 0.0),
1967 GainKey::new(30, -20.0),
1968 GainKey::new(60, -6.5),
1969 ]
1970 );
1971
1972 assert!(session.remove_gain_key(a1, 0, 60));
1973 assert!(session.undo(), "the removal is one undo");
1974 assert_eq!(session.gain_keys_of(a1, 0).len(), 3, "the point is back");
1975}
1976
1977/// A version 27 project has no envelopes: it loads at flat, and a `gainkeys`
1978/// line in it is refused as a line that version does not have.
1979#[test]
1980fn a_version_27_project_loads_flat_and_refuses_a_gainkeys_line() {
1981 let dir = scratch("v27_gain_keys");
1982 copy_in(&dir, "test_av.mp4");
1983 let path = dir.join("old.edith");
1984 let text = "edith 27\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\
1985 video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\
1986 audio 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\
1987 clipgain audio 1 0 -6\n";
1988 std::fs::write(&path, text).expect("write a v27 file");
1989 let loaded = PlaybackSession::open_project(&path).expect("a v27 project opens");
1990 assert!(
1991 loaded
1992 .gain_keys_of(Lane::new(LaneKind::Audio, 0), 0)
1993 .is_empty()
1994 );
1995 assert_eq!(
1996 loaded.clip_gain_of(Lane::new(LaneKind::Audio, 0), 0),
1997 Some(-6.0)
1998 );
1999
2000 let refused = dir.join("refused.edith");
2001 std::fs::write(
2002 &refused,
2003 text.replace("clipgain audio 1 0 -6\n", "gainkeys audio 1 0 0:-6\n"),
2004 )
2005 .expect("write");
2006 let err = PlaybackSession::open_project(&refused)
2007 .err()
2008 .expect("refused");
2009 assert!(err.to_string().contains("gainkeys"), "{err}");
2010}
2011
2012/// The v52 bump's own round trip: the notes and the still length a project was left with come back
2013/// with it, a save of the loaded project is the same bytes, neither is an undo step, and an edit to
2014/// either is one. A file written before v52 has no notes and means the five-second still.
2015#[test]
2016fn the_notes_and_the_still_length_survive_a_save_and_an_open() {
2017 let dir = scratch("notes");
2018 let source = copy_in(&dir, "test_av.mp4");
2019 let mut saved = PlaybackSession::open(&source).expect("open the fixture");
2020 assert_eq!((saved.notes(), saved.still_secs()), ("", 5.0), "nothing picked yet");
2021 let notes = "Cut the intro.\nTitle card: \"Day 2\" \\ tbd\r\n";
2022 assert!(saved.set_notes(notes));
2023 assert!(!saved.set_notes(notes), "the same text changes nothing");
2024 assert!(saved.set_still_secs(2.5));
2025 let path = dir.join("notes.edith");
2026 saved.save_project(&path).expect("save");
2027
2028 let mut loaded = PlaybackSession::open_project(&path).expect("open the project");
2029 assert_eq!(loaded.notes(), notes);
2030 assert_eq!(loaded.still_secs(), 2.5);
2031 assert!(!loaded.undo(), "loaded notes and a loaded still length are not undo steps");
2032 let again = dir.join("again.edith");
2033 loaded.save_project(&again).expect("save the loaded project");
2034 assert_eq!(
2035 std::fs::read(&path).unwrap(),
2036 std::fs::read(&again).unwrap(),
2037 "save -> load -> save is not a fixed point"
2038 );
2039
2040 // Each edit is one undo step, and undo puts the old value back.
2041 assert!(loaded.set_notes("changed"));
2042 assert!(loaded.undo());
2043 assert_eq!(loaded.notes(), notes);
2044 assert!(loaded.set_still_secs(9.0));
2045 assert!(loaded.undo());
2046 assert_eq!(loaded.still_secs(), 2.5);
2047
2048 // The still length is clamped to what a still may be.
2049 assert!(loaded.set_still_secs(10_000.0));
2050 assert_eq!(loaded.still_secs(), 600.0);
2051
2052 // A v51 project says nothing about notes or a still length: empty, and five seconds. A plain
2053 // project saved today has no v52 line, so its header is relabelled as the v51 one it still is.
2054 let plain = dir.join("plain.edith");
2055 PlaybackSession::open(&source)
2056 .expect("open the fixture")
2057 .save_project(&plain)
2058 .expect("save a plain project");
2059 let text = std::fs::read_to_string(&plain).expect("read it back");
2060 let (_, body) = text.split_once('\n').expect("a header line");
2061 let old = dir.join("old.edith");
2062 std::fs::write(&old, format!("edith 51\n{body}")).expect("write a v51 project");
2063 let older = PlaybackSession::open_project(&old).expect("open the v51 project");
2064 assert_eq!((older.notes(), older.still_secs()), ("", 5.0));
2065}