| 1 | fn conflict_fixture(scratch: &Scratch) -> (PathBuf, PathBuf) { |
| 2 | use worker_protocol::{archive, protocol}; |
| 3 | let members = ["기록.txt", "사진.txt"] |
| 4 | .into_iter() |
| 5 | .enumerate() |
| 6 | .map(|(id, name)| archive::Member { |
| 7 | entry: protocol::Entry { |
| 8 | entry_id: id as u32, |
| 9 | name: name.into(), |
| 10 | kind: protocol::EntryKind::File, |
| 11 | size: 3, |
| 12 | compressed_size: 3, |
| 13 | encrypted: false, |
| 14 | }, |
| 15 | data: b"new".to_vec(), |
| 16 | }) |
| 17 | .collect::<Vec<_>>(); |
| 18 | let archive = scratch.path("충돌.zip"); |
| 19 | fs::write( |
| 20 | &archive, |
| 21 | archive::create_zip( |
| 22 | &members, |
| 23 | protocol::Method::Store, |
| 24 | protocol::Limits::default(), |
| 25 | &mut || Ok(()), |
| 26 | ) |
| 27 | .unwrap(), |
| 28 | ) |
| 29 | .unwrap(); |
| 30 | let output = make_directory(&scratch.path("output")); |
| 31 | for name in ["기록.txt", "사진.txt"] { |
| 32 | fs::write(output.join(name), b"original").unwrap(); |
| 33 | } |
| 34 | (archive, output) |
| 35 | } |
| 36 | |
| 37 | impl Screen { |
| 38 | fn start_conflicting_extract(&mut self, output: &Path) { |
| 39 | self.app.choose_destination(output.to_path_buf()); |
| 40 | self.dialogs |
| 41 | .0 |
| 42 | .borrow_mut() |
| 43 | .answers |
| 44 | .push_back(Some(vec![output.to_path_buf()])); |
| 45 | self.click("압축해제"); |
| 46 | assert!(self.app.model.busy()); |
| 47 | } |
| 48 | |
| 49 | fn await_conflict(&mut self) -> Conflict { |
| 50 | let deadline = Instant::now() + Duration::from_secs(10); |
| 51 | loop { |
| 52 | self.render(vec![]); |
| 53 | if let Some(conflict) = &self.app.model.conflict { |
| 54 | return conflict.clone(); |
| 55 | } |
| 56 | assert!( |
| 57 | self.app.model.busy(), |
| 58 | "충돌 질문 전에 종료됨: {:?}", |
| 59 | self.app.model.terminal |
| 60 | ); |
| 61 | assert!(Instant::now() < deadline, "충돌 질문 시간 초과"); |
| 62 | std::thread::sleep(Duration::from_millis(2)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn settle_conflict_result(&mut self) { |
| 67 | let deadline = Instant::now() + Duration::from_secs(10); |
| 68 | while self.app.model.busy() { |
| 69 | self.render(vec![]); |
| 70 | assert!(Instant::now() < deadline, "충돌 결과 시간 초과"); |
| 71 | std::thread::sleep(Duration::from_millis(2)); |
| 72 | } |
| 73 | assert!(self.app.model.confirmed, "{}", self.app.model.status()); |
| 74 | assert!(self.app.model.conflict.is_none()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn real_conflicts_follow_widget_choices_and_apply_remaining() { |
| 80 | for (label, choice) in [ |
| 81 | ("덮어쓰기", ConflictChoice::Overwrite), |
| 82 | ("건너뛰기", ConflictChoice::Skip), |
| 83 | ("이름 바꿔 저장", ConflictChoice::Rename), |
| 84 | ] { |
| 85 | for apply in [false, true] { |
| 86 | let scratch = Scratch::new(); |
| 87 | let (archive, output) = conflict_fixture(&scratch); |
| 88 | let (factory, observed) = observed_factory(); |
| 89 | let mut screen = Screen::new(factory); |
| 90 | screen.open(&archive); |
| 91 | screen.start_conflicting_extract(&output); |
| 92 | let first = screen.await_conflict(); |
| 93 | assert_eq!(first.incoming_size, 3); |
| 94 | assert_eq!(first.existing_size, 8); |
| 95 | if apply { |
| 96 | screen.click("나머지 파일에도 같게 적용"); |
| 97 | } |
| 98 | screen.click(label); |
| 99 | if !apply { |
| 100 | let second = screen.await_conflict(); |
| 101 | assert_ne!(first.challenge_id, second.challenge_id); |
| 102 | assert_ne!(first.entry_id, second.entry_id); |
| 103 | screen.click(label); |
| 104 | } |
| 105 | screen.settle_conflict_result(); |
| 106 | completed(&screen.app.model); |
| 107 | assert_eq!(screen.app.model.resolutions.len(), 2); |
| 108 | assert!( |
| 109 | screen |
| 110 | .app |
| 111 | .model |
| 112 | .resolutions |
| 113 | .iter() |
| 114 | .all(|r| r.choice == choice) |
| 115 | ); |
| 116 | let terminal = screen.app.model.terminal.as_ref().unwrap(); |
| 117 | assert_eq!( |
| 118 | terminal.created_count, |
| 119 | if choice == ConflictChoice::Skip { 0 } else { 2 } |
| 120 | ); |
| 121 | assert_eq!( |
| 122 | terminal.publication, |
| 123 | if choice == ConflictChoice::Skip { |
| 124 | Publication::None |
| 125 | } else { |
| 126 | Publication::Complete |
| 127 | } |
| 128 | ); |
| 129 | for name in ["기록.txt", "사진.txt"] { |
| 130 | assert_eq!( |
| 131 | fs::read(output.join(name)).unwrap(), |
| 132 | if choice == ConflictChoice::Overwrite { |
| 133 | b"new".as_slice() |
| 134 | } else { |
| 135 | b"original".as_slice() |
| 136 | } |
| 137 | ); |
| 138 | } |
| 139 | for resolution in &screen.app.model.resolutions { |
| 140 | if choice == ConflictChoice::Rename { |
| 141 | assert_eq!( |
| 142 | fs::read(output.join(&resolution.relative_name)).unwrap(), |
| 143 | b"new" |
| 144 | ); |
| 145 | assert!(resolution.relative_name.contains(" (1)")); |
| 146 | } |
| 147 | } |
| 148 | let shown = screen |
| 149 | .render(vec![]) |
| 150 | .into_iter() |
| 151 | .map(|(t, _)| t) |
| 152 | .collect::<Vec<_>>() |
| 153 | .join("\n"); |
| 154 | assert!( |
| 155 | shown.contains(if choice == ConflictChoice::Skip { |
| 156 | "모든 파일을 건너뛰었습니다" |
| 157 | } else { |
| 158 | "압축을 풀었습니다" |
| 159 | }), |
| 160 | "{shown}" |
| 161 | ); |
| 162 | assert!(!shown.contains("저장이 끝났는지"), "{shown}"); |
| 163 | assert_reaped(&observed); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn real_conflict_cancel_button_and_escape_preserve_originals() { |
| 170 | for escape in [false, true] { |
| 171 | let scratch = Scratch::new(); |
| 172 | let (archive, output) = conflict_fixture(&scratch); |
| 173 | let (factory, observed) = observed_factory(); |
| 174 | let mut screen = Screen::new(factory); |
| 175 | screen.open(&archive); |
| 176 | screen.start_conflicting_extract(&output); |
| 177 | screen.await_conflict(); |
| 178 | if escape { |
| 179 | screen.escape(); |
| 180 | } else { |
| 181 | screen.click("작업 취소"); |
| 182 | } |
| 183 | screen.settle_conflict_result(); |
| 184 | assert_eq!( |
| 185 | screen.app.model.terminal.as_ref().unwrap().outcome, |
| 186 | Outcome::Cancelled |
| 187 | ); |
| 188 | assert!(screen.app.model.outputs.is_empty()); |
| 189 | for name in ["기록.txt", "사진.txt"] { |
| 190 | assert_eq!(fs::read(output.join(name)).unwrap(), b"original"); |
| 191 | } |
| 192 | assert_reaped(&observed); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn target_changed_during_real_question_fails_without_saved_name_notice() { |
| 198 | let scratch = Scratch::new(); |
| 199 | let (archive, output) = conflict_fixture(&scratch); |
| 200 | let (factory, observed) = observed_factory(); |
| 201 | let mut screen = Screen::new(factory); |
| 202 | screen.open(&archive); |
| 203 | screen.start_conflicting_extract(&output); |
| 204 | let conflict = screen.await_conflict(); |
| 205 | fs::write( |
| 206 | output.join(&conflict.relative_name), |
| 207 | b"changed after question", |
| 208 | ) |
| 209 | .unwrap(); |
| 210 | screen.click("덮어쓰기"); |
| 211 | screen.settle_conflict_result(); |
| 212 | assert_eq!( |
| 213 | screen.app.model.terminal.as_ref().unwrap().outcome, |
| 214 | Outcome::Failed |
| 215 | ); |
| 216 | assert_eq!(screen.app.model.terminal.as_ref().unwrap().created_count, 0); |
| 217 | assert!(screen.app.model.outputs.is_empty()); |
| 218 | assert_eq!( |
| 219 | fs::read(output.join(&conflict.relative_name)).unwrap(), |
| 220 | b"changed after question" |
| 221 | ); |
| 222 | let shown = screen |
| 223 | .render(vec![]) |
| 224 | .into_iter() |
| 225 | .map(|(t, _)| t) |
| 226 | .collect::<Vec<_>>() |
| 227 | .join("\n"); |
| 228 | assert!(shown.contains("압축을 풀지 못했습니다"), "{shown}"); |
| 229 | assert!(!shown.contains("이름 바꿔 저장:"), "{shown}"); |
| 230 | assert_reaped(&observed); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn approved_extract_continues_to_conflict_choices() { |
| 235 | let scratch = Scratch::new(); |
| 236 | let (archive, output) = conflict_fixture(&scratch); |
| 237 | let (factory, observed) = observed_factory(); |
| 238 | let mut screen = Screen::new(factory); |
| 239 | screen.open(&archive); |
| 240 | screen.app.model.entries[0].bytes = Some(17 * 1024 * 1024 * 1024); |
| 241 | screen.start_conflicting_extract(&output); |
| 242 | let deadline = Instant::now() + Duration::from_secs(10); |
| 243 | while screen.app.model.resource.is_none() { |
| 244 | screen.render(vec![]); |
| 245 | assert!(screen.app.model.busy(), "{:?}", screen.app.model.error); |
| 246 | assert!(Instant::now() < deadline, "승인 질문 시간 초과"); |
| 247 | std::thread::sleep(Duration::from_millis(2)); |
| 248 | } |
| 249 | let approval = screen.app.model.resource.clone().unwrap(); |
| 250 | assert!(screen.app.model.conflict.is_none()); |
| 251 | screen.click("이 작업 허용"); |
| 252 | let conflict = screen.await_conflict(); |
| 253 | assert!(conflict.challenge_id > approval.id); |
| 254 | assert!(screen.app.model.resource.is_none()); |
| 255 | screen.click("나머지 파일에도 같게 적용"); |
| 256 | screen.click("덮어쓰기"); |
| 257 | screen.settle_conflict_result(); |
| 258 | completed(&screen.app.model); |
| 259 | assert_eq!(screen.app.model.resolutions.len(), 2); |
| 260 | for name in ["기록.txt", "사진.txt"] { |
| 261 | assert_eq!(fs::read(output.join(name)).unwrap(), b"new"); |
| 262 | } |
| 263 | assert_reaped(&observed); |
| 264 | } |