| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use super::*; |
| 7 | use crate::Player; |
| 8 | use crate::player::library::{recovered_session, recovery_replaces_words}; |
| 9 | use engine::project::{LaneKind, Marker, MarkerColor}; |
| 10 | use engine::scratch::Scratch; |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | fn 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 | |
| 28 | |
| 29 | #[test] |
| 30 | fn 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 | |
| 49 | |
| 50 | |
| 51 | #[test] |
| 52 | fn 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 | |
| 60 | |
| 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 | |
| 72 | |
| 73 | |
| 74 | #[test] |
| 75 | fn 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 | |
| 104 | |
| 105 | |
| 106 | #[test] |
| 107 | fn 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 | |
| 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 | |
| 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 | |
| 140 | #[test] |
| 141 | fn the_recovery_toast_says_recovering_replaces_the_current_edits() { |
| 142 | assert!(recovery_replaces_words().contains("Recovering will replace the current edits")); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | |
| 147 | #[test] |
| 148 | fn 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 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | #[test] |
| 166 | fn 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 | |
| 176 | drop(session); |
| 177 | autosave_is_newer(&project, &sidecar); |
| 178 | |
| 179 | |
| 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 | |
| 191 | Player::discard_sidecar(&sidecar); |
| 192 | assert!(!sidecar.exists()); |
| 193 | assert_eq!(Player::recovery_offer(&project, true), None); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | #[test] |
| 200 | fn 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 | |
| 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 | |
| 232 | |
| 233 | #[test] |
| 234 | fn 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 | |
| 251 | |
| 252 | |
| 253 | #[test] |
| 254 | fn 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 | |
| 265 | |
| 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 | } |