forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

autosave.rs270 lines, 12.4 KiBHistoryRawDownload
1//! The autosave's writer and the crash path: an autosave lands whole or not at
2//! all, a failed one leaves the saved file alone, a recovery is one undo step
3//! over the saved project, and an autosave that outlives a crash is offered
4//! again on the next open.
5
6use super::*;
7use crate::Player;
8use crate::player::library::{recovered_session, recovery_replaces_words};
9use engine::project::{LaneKind, Marker, MarkerColor};
10use engine::scratch::Scratch;
11
12/// Makes `sidecar` the younger of the two files, as it is after a save and
13/// then an autosave of edits made since. The mtimes are set rather than left
14/// to the clock, so the offer does not depend on how fast the test ran.
15fn autosave_is_newer(project: &Path, sidecar: &Path) {
16 let now = std::time::SystemTime::now();
17 std::fs::File::open(project)
18 .unwrap()
19 .set_modified(now - Duration::from_secs(3600))
20 .unwrap();
21 std::fs::File::open(sidecar)
22 .unwrap()
23 .set_modified(now)
24 .unwrap();
25}
26
27/// A successful autosave is renamed into place: no temp file is left beside
28/// it, and what is there opens as a project.
29#[test]
30fn an_autosave_leaves_no_temp_file_behind() {
31 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
32 let dir = Scratch::dir("ve_autosave_temp");
33 let file = dir.join("p.autosave.edith");
34
35 session.save_autosave(&file).expect("write the autosave");
36
37 assert!(file.is_file(), "the autosave is on disk");
38 assert!(
39 !dir.join("p.autosave.edith.part").exists(),
40 "the temp file was renamed over the name, not left beside it"
41 );
42 assert!(
43 PlaybackSession::open_project(&file).is_ok(),
44 "it opens as a project"
45 );
46}
47
48/// An autosave that cannot start its temp file fails, and the autosave that
49/// was already on disk is exactly as it was: the next good write replaces
50/// it whole, and a crash in between still has something to recover.
51#[test]
52fn a_failed_autosave_leaves_the_one_on_disk_intact() {
53 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
54 let dir = Scratch::dir("ve_autosave_intact");
55 let file = dir.join("p.autosave.edith");
56 session.save_autosave(&file).expect("the first write");
57 let before = std::fs::read(&file).expect("read the first write");
58
59 // A directory where the temp file has to be created: the write cannot
60 // start, so nothing reaches the real name.
61 std::fs::create_dir(dir.join("p.autosave.edith.part")).expect("block the temp file");
62 assert!(
63 session.save_autosave(&file).is_err(),
64 "the blocked write reports itself"
65 );
66
67 assert_eq!(std::fs::read(&file).expect("read it again"), before);
68 assert!(PlaybackSession::open_project(&file).is_ok(), "still opens");
69}
70
71/// Recovering takes the autosave's edits on the saved project as one undo
72/// step: the edits are there, and one undo takes them all back to the saved
73/// timeline.
74#[test]
75fn a_recovery_is_one_undo_step_over_the_saved_project() {
76 let dir = Scratch::dir("ve_recover_step");
77 let saved = dir.join("p.edith");
78 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
79 session.save_project(&saved).expect("the manual save");
80 let saved_lanes = PlaybackSession::open_project(&saved)
81 .expect("reopen")
82 .lanes();
83
84 let mut edited = PlaybackSession::open_project(&saved).expect("reopen");
85 let v2 = edited.add_lane(LaneKind::Video);
86 let auto = dir.join("p.autosave.edith");
87 edited.save_autosave(&auto).expect("autosave the edit");
88
89 let mut recovered = recovered_session(Some(&saved), &auto).expect("recovers");
90 assert!(recovered.lanes().contains(&v2), "the unsaved lane is back");
91 assert_eq!(recovered.undo_depth(), 1, "the recovery is one step");
92 assert_eq!(recovered.undo_label(), Some("Recover unsaved edits"), "and it is named");
93
94 assert!(recovered.undo());
95 assert_eq!(
96 recovered.lanes(),
97 saved_lanes,
98 "one undo gives the saved timeline"
99 );
100 assert!(!recovered.lanes().contains(&v2));
101}
102
103/// Recover keeps the edits made since the file was opened: the window's own
104/// timeline becomes one undo step back, and undo brings it back to the exact
105/// state (lanes and revision) it had before the recovery.
106#[test]
107fn recovering_over_edits_made_since_the_open_is_one_undo_step_back_to_them() {
108 let dir = Scratch::dir("ve_recover_keeps");
109 let saved = dir.join("p.edith");
110 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
111 session.save_project(&saved).expect("the manual save");
112 let saved_revision = PlaybackSession::open_project(&saved).expect("reopen").revision();
113
114 // The autosave holds one edit (a lane the file does not have).
115 let mut autosaved = PlaybackSession::open_project(&saved).expect("reopen");
116 let recovered_lane = autosaved.add_lane(LaneKind::Video);
117 let auto = dir.join("p.autosave.edith");
118 autosaved.save_autosave(&auto).expect("autosave the edit");
119
120 // The window kept working after the open: its own edit, never autosaved.
121 let mut current = PlaybackSession::open_project(&saved).expect("reopen");
122 let own_lane = current.add_lane(LaneKind::Audio);
123 let before = current.lanes();
124 let before_revision = current.revision();
125 assert_ne!(before_revision, saved_revision, "the window is off the saved timeline");
126
127 let recovered = recovered_session(Some(&saved), &auto).expect("recovers");
128 assert!(current.adopt_as_step(&recovered), "the recovery goes on the window's edits");
129 assert!(current.lanes().contains(&recovered_lane), "the recovered edit is in");
130 assert!(!current.lanes().contains(&own_lane), "and the window's own edit is replaced");
131
132 assert!(current.undo(), "the recovery is one step");
133 assert_eq!(current.lanes(), before, "undo gives the window's edits back");
134 assert_eq!(current.revision(), before_revision, "by the same revision they had");
135 assert!(current.redo());
136 assert!(current.lanes().contains(&recovered_lane));
137}
138
139/// The recovery toast says what recovering costs once the window has edits.
140#[test]
141fn the_recovery_toast_says_recovering_replaces_the_current_edits() {
142 assert!(recovery_replaces_words().contains("Recovering will replace the current edits"));
143}
144
145/// An untitled session has no saved file to stand on: the autosave is the
146/// project, and there is nothing earlier for undo to return to.
147#[test]
148fn an_untitled_recovery_is_the_autosave_with_nothing_to_undo_to() {
149 let dir = Scratch::dir("ve_recover_untitled");
150 let auto = dir.join("untitled.autosave.edith");
151 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
152 let v2 = session.add_lane(LaneKind::Video);
153 session.save_autosave(&auto).expect("autosave");
154
155 let recovered = recovered_session(None, &auto).expect("recovers");
156
157 assert!(recovered.lanes().contains(&v2));
158 assert_eq!(recovered.undo_depth(), 0);
159}
160
161/// The kill test: a session saves, edits, autosaves, and then goes away with
162/// nothing saved since (the crash). Reopening the project offers the
163/// autosave, recovering it brings the edit back, and declining it leaves the
164/// offer gone for good.
165#[test]
166fn unsaved_edits_survive_a_crash_and_are_offered_on_reopen() {
167 let dir = Scratch::dir("ve_autosave_crash");
168 let project = dir.join("p.edith");
169 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
170 session.save_project(&project).expect("the manual save");
171 let v2 = session.add_lane(LaneKind::Video);
172 let sidecar = Player::autosave_target(&project, true).expect("a place for the autosave");
173 session.save_autosave(&sidecar).expect("the autosave");
174
175 // The crash: no save, no close, the session is simply gone.
176 drop(session);
177 autosave_is_newer(&project, &sidecar);
178
179 // The reopen: the offer names the autosave beside the project.
180 assert_eq!(
181 Player::recovery_offer(&project, true),
182 Some(sidecar.clone())
183 );
184 let recovered = recovered_session(Some(&project), &sidecar).expect("recovers");
185 assert!(
186 recovered.lanes().contains(&v2),
187 "the edit made before the crash is back"
188 );
189
190 // Declined instead: the autosave is deleted, and nothing is offered again.
191 Player::discard_sidecar(&sidecar);
192 assert!(!sidecar.exists());
193 assert_eq!(Player::recovery_offer(&project, true), None);
194}
195
196/// The crash with several sequences: a second sequence, a copy of it, a compound
197/// clip over a clip of the copy and a marker all live only in the autosave. The
198/// recovery brings every sequence back, the compound clip and the marker with them.
199#[test]
200fn a_crash_with_several_sequences_recovers_every_one() {
201 let dir = Scratch::dir("ve_autosave_sequences");
202 let project = dir.join("p.edith");
203 let media = asset("test_av.mp4");
204 let mut session = PlaybackSession::open(&media).expect("open the fixture");
205 session.save_project(&project).expect("the manual save");
206 let b = session.new_sequence(Some("Cut B")).expect("a second sequence");
207 session.switch_sequence(b).expect("switch to it");
208 assert!(session.place_stream_at(0.0, &media, 0, None).expect("placeable"));
209 let copy = session.duplicate_sequence(b).expect("duplicate it");
210 session.switch_sequence(copy).expect("switch to the copy");
211 session.nest(&[(Lane::V1, 0)], Some("Inner")).expect("nest a clip");
212 session.add_marker(Marker { frame: 3, length: 0, name: "beat".into(), note: String::new(), color: MarkerColor::Red })
213 .expect("a marker");
214 assert_eq!(session.markers().len(), 1, "the marker is on the sequence being edited");
215 let sidecar = Player::autosave_target(&project, true).expect("a place for the autosave");
216 session.save_autosave(&sidecar).expect("the autosave");
217
218 // The crash: nothing saved since the manual save.
219 let want = session.sequences();
220 drop(session);
221 autosave_is_newer(&project, &sidecar);
222
223 let recovered = recovered_session(Some(&project), &sidecar).expect("every sequence recovers");
224 assert_eq!(recovered.sequences(), want, "the same sequences, in the same order");
225 assert_eq!(recovered.active_sequence(), copy, "the copy is still the one being edited");
226 let markers = recovered.markers();
227 assert_eq!(markers.len(), 1, "the marker comes back");
228 assert_eq!((markers[0].frame, markers[0].name.as_str()), (3, "beat"));
229}
230
231/// A recovery that cannot read its autosave is refused as a whole: the saved file
232/// is untouched, the autosave is still on disk, and the reason comes back as text.
233#[test]
234fn a_refused_recovery_keeps_the_saved_file_and_the_autosave() {
235 let dir = Scratch::dir("ve_autosave_refused");
236 let project = dir.join("p.edith");
237 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
238 session.save_project(&project).expect("the manual save");
239 let sidecar = dir.join("p.autosave.edith");
240 std::fs::write(&sidecar, b"not a project at all").expect("a damaged autosave");
241 autosave_is_newer(&project, &sidecar);
242 let before = std::fs::read(&project).expect("the saved file");
243
244 let refused = recovered_session(Some(&project), &sidecar);
245 assert!(refused.is_err(), "a damaged autosave is not recovered");
246 assert!(sidecar.exists(), "the autosave is kept");
247 assert_eq!(std::fs::read(&project).expect("the saved file"), before, "the saved file is untouched");
248}
249
250/// Opening a project is not an edit: the window comes up with nothing to undo, so Undo is
251/// off until the person has changed something. The load's own fixes (a relink, a clamped
252/// clip, the settings it carries) must not leave a step behind.
253#[test]
254fn opening_a_project_leaves_nothing_to_undo() {
255 let dir = Scratch::dir("ve_open_no_undo");
256 let saved = dir.join("p.edith");
257 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
258 session.save_project(&saved).expect("the manual save");
259
260 let mut opened = PlaybackSession::open_project(&saved).expect("reopen");
261 assert_eq!(opened.undo_depth(), 0, "a load pushes no undo step");
262 assert_eq!(opened.undo_label(), None, "so there is no step to name");
263
264 // The window's own default for a project with no seat of its own is applied on
265 // load too, and it is no step either.
266 assert_eq!(opened.encoder_seat(), engine::export::EncoderSeat::Auto, "the fixture carries no seat");
267 crate::export_ui::gui_default_seat(&mut opened);
268 assert_eq!(opened.encoder_seat(), engine::export::EncoderSeat::Software);
269 assert_eq!(opened.undo_depth(), 0, "the default seat is not a pick");
270}