| 1 | mod fake; |
| 2 | use fake::*; |
| 3 | use std::path::PathBuf; |
| 4 | use zipelo_ui_memory_app::archive_ui::*; |
| 5 | |
| 6 | fn path(name: &str) -> PathBuf { |
| 7 | std::env::current_dir().unwrap().join(name) |
| 8 | } |
| 9 | fn pump(m: &mut Model) { |
| 10 | for _ in 0..100 { |
| 11 | m.poll(); |
| 12 | if !m.busy() { |
| 13 | return; |
| 14 | } |
| 15 | } |
| 16 | panic!("가짜 작업자 종료 누락: {}", m.status()); |
| 17 | } |
| 18 | fn list(m: &mut Model, f: &mut FakeFactory) { |
| 19 | assert!(m.begin( |
| 20 | Start::List { |
| 21 | archive: path("example.zip") |
| 22 | }, |
| 23 | f |
| 24 | )); |
| 25 | pump(m); |
| 26 | assert!(m.list_id.is_some()); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn open_lists_only_after_terminal_eofs_and_wait() { |
| 31 | let mut f = FakeFactory::default(); |
| 32 | let mut m = Model::default(); |
| 33 | assert!(m.begin( |
| 34 | Start::List { |
| 35 | archive: path("example.zip") |
| 36 | }, |
| 37 | &mut f |
| 38 | )); |
| 39 | while m.terminal.is_none() { |
| 40 | m.poll(); |
| 41 | assert!(m.list_id.is_none()); |
| 42 | assert!(!m.can_extract()); |
| 43 | } |
| 44 | assert!(m.busy()); |
| 45 | assert!(!m.begin( |
| 46 | Start::List { |
| 47 | archive: path("replacement.zip") |
| 48 | }, |
| 49 | &mut f |
| 50 | )); |
| 51 | pump(&mut m); |
| 52 | assert_eq!(m.entries[0].name, "읽어주세요.txt"); |
| 53 | assert!(m.can_extract()); |
| 54 | assert!(m.confirmed); |
| 55 | } |
| 56 | #[test] |
| 57 | fn extract_uses_list_token_selection_and_destination() { |
| 58 | let mut f = FakeFactory::default(); |
| 59 | let mut m = Model::default(); |
| 60 | list(&mut m, &mut f); |
| 61 | let token = m.list_id.clone().unwrap(); |
| 62 | m.selected.insert(0); |
| 63 | assert!(m.extract(path("output"), true, None, &mut f)); |
| 64 | pump(&mut m); |
| 65 | assert!( |
| 66 | matches!(f.state.borrow().starts.last(),Some(Start::Extract {selection:Selection::EntryIds(ids),list_id,..}) if ids==&vec![0]&&*list_id==token) |
| 67 | ); |
| 68 | assert_eq!(m.terminal.unwrap().publication, Publication::Complete); |
| 69 | } |
| 70 | #[test] |
| 71 | fn gzip_requires_explicit_leaf() { |
| 72 | let mut f = FakeFactory::default(); |
| 73 | let mut m = Model::default(); |
| 74 | assert!(m.begin( |
| 75 | Start::List { |
| 76 | archive: path("sample.gz") |
| 77 | }, |
| 78 | &mut f |
| 79 | )); |
| 80 | pump(&mut m); |
| 81 | for name in [None, Some("../escape".into()), Some("sub/file".into())] { |
| 82 | assert!(!m.extract(path("output"), false, name, &mut f)); |
| 83 | } |
| 84 | assert!(m.extract(path("output"), false, Some("plain.txt".into()), &mut f)); |
| 85 | pump(&mut m); |
| 86 | } |
| 87 | #[test] |
| 88 | fn creates_zip_and_reports_progress_outputs() { |
| 89 | let mut f = FakeFactory::default(); |
| 90 | let mut m = Model::default(); |
| 91 | assert!(m.begin( |
| 92 | Start::CreateZip { |
| 93 | sources: vec![(path("one.txt"), "one.txt".into())], |
| 94 | output_file: path("new.zip"), |
| 95 | method: Method::Deflate, |
| 96 | aes256: false |
| 97 | }, |
| 98 | &mut f |
| 99 | )); |
| 100 | pump(&mut m); |
| 101 | assert_eq!(m.progress.unwrap().1, 31); |
| 102 | assert_eq!(m.outputs.len(), 1); |
| 103 | assert_eq!(m.status(), "작업 완료"); |
| 104 | } |
| 105 | #[test] |
| 106 | fn password_challenge_is_answered_without_retaining_secret() { |
| 107 | let mut f = FakeFactory::default(); |
| 108 | let mut m = Model::default(); |
| 109 | assert!(m.begin( |
| 110 | Start::List { |
| 111 | archive: path("locked.zip") |
| 112 | }, |
| 113 | &mut f |
| 114 | )); |
| 115 | for _ in 0..10 { |
| 116 | m.poll(); |
| 117 | if m.challenge.is_some() { |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | assert_eq!(m.challenge, Some((17, 1, false))); |
| 122 | assert!(!m.password(String::new())); |
| 123 | assert!(m.password("synthetic password".into())); |
| 124 | pump(&mut m); |
| 125 | assert!(f.state.borrow().password_seen); |
| 126 | assert!(m.challenge.is_none()); |
| 127 | assert!(m.list_id.is_some()); |
| 128 | } |
| 129 | #[test] |
| 130 | fn cancel_close_retract_preserves_request_and_waits_for_exit() { |
| 131 | let mut f = FakeFactory::default(); |
| 132 | let mut m = Model::default(); |
| 133 | assert!(m.begin( |
| 134 | Start::List { |
| 135 | archive: path("one.zip") |
| 136 | }, |
| 137 | &mut f |
| 138 | )); |
| 139 | m.poll(); |
| 140 | m.request_close(); |
| 141 | assert_eq!(m.close, Close::Confirm); |
| 142 | m.confirm_close(); |
| 143 | m.keep_open(); |
| 144 | m.cancel(); |
| 145 | pump(&mut m); |
| 146 | assert_eq!(m.close, Close::Open); |
| 147 | assert_eq!(m.cancel, Some(CancelStatus::Requested)); |
| 148 | assert_eq!(m.terminal.unwrap().outcome, Outcome::Cancelled); |
| 149 | assert_eq!( |
| 150 | f.state |
| 151 | .borrow() |
| 152 | .requests |
| 153 | .iter() |
| 154 | .filter(|(_, k)| *k == "cancel") |
| 155 | .count(), |
| 156 | 1 |
| 157 | ); |
| 158 | } |
| 159 | #[test] |
| 160 | fn terminal_race_needs_no_cancel_ack() { |
| 161 | let mut f = FakeFactory::manual(); |
| 162 | let mut m = Model::default(); |
| 163 | assert!(m.begin( |
| 164 | Start::List { |
| 165 | archive: path("one.zip") |
| 166 | }, |
| 167 | &mut f |
| 168 | )); |
| 169 | { |
| 170 | let mut s = f.state.borrow_mut(); |
| 171 | s.frame(Event::Accepted { encryption: false }); |
| 172 | s.finish(Outcome::Cancelled, Publication::None); |
| 173 | } |
| 174 | m.cancel(); |
| 175 | pump(&mut m); |
| 176 | assert!(m.cancel.is_none()); |
| 177 | assert_eq!(m.status(), "작업 취소됨"); |
| 178 | } |
| 179 | #[test] |
| 180 | fn eof_exit_without_terminal_never_means_success() { |
| 181 | let mut f = FakeFactory::manual(); |
| 182 | let mut m = Model::default(); |
| 183 | assert!(m.begin( |
| 184 | Start::List { |
| 185 | archive: path("one.zip") |
| 186 | }, |
| 187 | &mut f |
| 188 | )); |
| 189 | { |
| 190 | let mut s = f.state.borrow_mut(); |
| 191 | s.queue.extend([ |
| 192 | HostEvent::StdoutEof, |
| 193 | HostEvent::StderrEof, |
| 194 | HostEvent::Exited { success: true }, |
| 195 | ]); |
| 196 | } |
| 197 | pump(&mut m); |
| 198 | assert!(!m.confirmed); |
| 199 | assert!(m.list_id.is_none()); |
| 200 | assert!(m.error.is_some()); |
| 201 | } |
| 202 | #[test] |
| 203 | fn stale_sequence_and_job_are_protocol_errors() { |
| 204 | for bad_job in [true, false] { |
| 205 | let mut f = FakeFactory::manual(); |
| 206 | let mut m = Model::default(); |
| 207 | assert!(m.begin( |
| 208 | Start::List { |
| 209 | archive: path("one.zip") |
| 210 | }, |
| 211 | &mut f |
| 212 | )); |
| 213 | let id = m.job_id().unwrap(); |
| 214 | f.state.borrow_mut().queue.push_back(HostEvent::Frame { |
| 215 | job_id: if bad_job { id + 1 } else { id }, |
| 216 | sequence: if bad_job { 0 } else { 1 }, |
| 217 | event: Event::Accepted { encryption: false }, |
| 218 | }); |
| 219 | m.poll(); |
| 220 | assert!(m.error.is_some()); |
| 221 | assert!(m.busy()); |
| 222 | } |
| 223 | } |
| 224 | #[test] |
| 225 | fn source_changed_invalidates_selection_without_retry() { |
| 226 | let mut f = FakeFactory::default(); |
| 227 | let mut m = Model::default(); |
| 228 | list(&mut m, &mut f); |
| 229 | m.selected.insert(0); |
| 230 | f.state.borrow_mut().manual = true; |
| 231 | assert!(m.extract(path("out"), true, None, &mut f)); |
| 232 | { |
| 233 | let mut s = f.state.borrow_mut(); |
| 234 | s.frame(Event::Accepted { encryption: false }); |
| 235 | s.frame(Event::Terminal(Terminal { |
| 236 | outcome: Outcome::Failed, |
| 237 | error: Some(ErrorCode::SourceChanged), |
| 238 | publication: Publication::None, |
| 239 | created_count: 0, |
| 240 | retained_partial_count: 0, |
| 241 | durability: Durability::NotApplicable, |
| 242 | cleanup: Cleanup::Complete, |
| 243 | })); |
| 244 | s.queue.extend([ |
| 245 | HostEvent::StdoutEof, |
| 246 | HostEvent::StderrEof, |
| 247 | HostEvent::Exited { success: true }, |
| 248 | ]); |
| 249 | } |
| 250 | pump(&mut m); |
| 251 | assert!(m.list_id.is_none()); |
| 252 | assert!(m.selected.is_empty()); |
| 253 | assert_eq!(f.state.borrow().starts.len(), 2); |
| 254 | } |
| 255 | #[test] |
| 256 | fn partial_and_uncertain_results_are_preserved() { |
| 257 | let mut f = FakeFactory::manual(); |
| 258 | let mut m = Model::default(); |
| 259 | assert!(m.begin( |
| 260 | Start::CreateZip { |
| 261 | sources: vec![(path("one"), "one".into())], |
| 262 | output_file: path("out.zip"), |
| 263 | method: Method::Store, |
| 264 | aes256: false |
| 265 | }, |
| 266 | &mut f |
| 267 | )); |
| 268 | { |
| 269 | let mut s = f.state.borrow_mut(); |
| 270 | s.frame(Event::Accepted { encryption: false }); |
| 271 | s.frame(Event::Output { |
| 272 | entry_id: None, |
| 273 | relative_name: "out.zip".into(), |
| 274 | disposition: Disposition::CommitUncertain, |
| 275 | }); |
| 276 | s.frame(Event::Terminal(Terminal { |
| 277 | outcome: Outcome::Failed, |
| 278 | error: Some(ErrorCode::Io), |
| 279 | publication: Publication::Uncertain, |
| 280 | created_count: 0, |
| 281 | retained_partial_count: 1, |
| 282 | durability: Durability::Failed, |
| 283 | cleanup: Cleanup::Unconfirmed, |
| 284 | })); |
| 285 | s.queue.extend([ |
| 286 | HostEvent::StdoutEof, |
| 287 | HostEvent::StderrEof, |
| 288 | HostEvent::Exited { success: true }, |
| 289 | ]); |
| 290 | } |
| 291 | pump(&mut m); |
| 292 | let t = m.terminal.as_ref().unwrap(); |
| 293 | assert_eq!(t.publication, Publication::Uncertain); |
| 294 | assert_eq!(t.retained_partial_count, 1); |
| 295 | assert_eq!(t.durability, Durability::Failed); |
| 296 | assert_eq!(m.outputs[0].1, Disposition::CommitUncertain); |
| 297 | } |
| 298 | #[test] |
| 299 | fn close_requires_result_acknowledgement() { |
| 300 | let mut f = FakeFactory::default(); |
| 301 | let mut m = Model::default(); |
| 302 | assert!(m.begin( |
| 303 | Start::List { |
| 304 | archive: path("one.zip") |
| 305 | }, |
| 306 | &mut f |
| 307 | )); |
| 308 | m.request_close(); |
| 309 | m.confirm_close(); |
| 310 | m.acknowledge_close(); |
| 311 | assert_eq!(m.close, Close::Waiting); |
| 312 | pump(&mut m); |
| 313 | assert_eq!(m.close, Close::Review); |
| 314 | m.acknowledge_close(); |
| 315 | assert_eq!(m.close, Close::Allowed); |
| 316 | } |
| 317 | #[test] |
| 318 | fn unsupported_worker_and_unsafe_inputs_do_not_start() { |
| 319 | let mut m = Model::default(); |
| 320 | assert!(!m.begin( |
| 321 | Start::List { |
| 322 | archive: path("one.zip") |
| 323 | }, |
| 324 | &mut Unavailable |
| 325 | )); |
| 326 | assert!(m.error.as_ref().unwrap().contains("준비")); |
| 327 | let mut f = FakeFactory::default(); |
| 328 | assert!(!m.begin( |
| 329 | Start::List { |
| 330 | archive: "relative.zip".into() |
| 331 | }, |
| 332 | &mut f |
| 333 | )); |
| 334 | assert!(!m.begin( |
| 335 | Start::CreateZip { |
| 336 | sources: vec![(path("one"), "../one".into())], |
| 337 | output_file: path("out.zip"), |
| 338 | method: Method::Store, |
| 339 | aes256: false |
| 340 | }, |
| 341 | &mut f |
| 342 | )); |
| 343 | assert!(f.state.borrow().starts.is_empty()); |
| 344 | } |
| 345 | #[cfg(feature = "ui")] |
| 346 | #[test] |
| 347 | fn renders_every_flow_with_actual_egui_widgets() { |
| 348 | use eframe::egui::{self, epaint::Shape}; |
| 349 | use zipelo_ui_memory_app::archive_screen::ArchiveApp; |
| 350 | fn text(shapes: &[egui::epaint::ClippedShape]) -> Vec<(String, egui::Pos2)> { |
| 351 | fn walk(shape: &Shape, result: &mut Vec<(String, egui::Pos2)>) { |
| 352 | match shape { |
| 353 | Shape::Text(t) => result.push(( |
| 354 | t.galley.job.text.clone(), |
| 355 | t.pos + t.galley.rect.size() / 2.0, |
| 356 | )), |
| 357 | Shape::Vec(v) => { |
| 358 | for shape in v { |
| 359 | walk(shape, result); |
| 360 | } |
| 361 | } |
| 362 | _ => {} |
| 363 | } |
| 364 | } |
| 365 | let mut result = Vec::new(); |
| 366 | for shape in shapes { |
| 367 | walk(&shape.shape, &mut result); |
| 368 | } |
| 369 | result |
| 370 | } |
| 371 | let ctx = egui::Context::default(); |
| 372 | let f = FakeFactory::default(); |
| 373 | let mut app = ArchiveApp::new(Box::new(f.clone()), None, true); |
| 374 | let render = |app: &mut ArchiveApp, events: Vec<egui::Event>, width: f32| { |
| 375 | let mut output = ctx.run_ui( |
| 376 | egui::RawInput { |
| 377 | screen_rect: Some(egui::Rect::from_min_size( |
| 378 | egui::Pos2::ZERO, |
| 379 | egui::vec2(width, 720.0), |
| 380 | )), |
| 381 | events, |
| 382 | ..Default::default() |
| 383 | }, |
| 384 | |ui| app.show(ui), |
| 385 | ); |
| 386 | output.textures_delta.clear(); |
| 387 | assert!(!output.shapes.is_empty()); |
| 388 | text(&output.shapes) |
| 389 | }; |
| 390 | let initial = render(&mut app, vec![], 1060.0); |
| 391 | let point = initial.iter().find(|(t, _)| t == "ZIP 만들기").unwrap().1; |
| 392 | render( |
| 393 | &mut app, |
| 394 | vec![ |
| 395 | egui::Event::PointerMoved(point), |
| 396 | egui::Event::PointerButton { |
| 397 | pos: point, |
| 398 | button: egui::PointerButton::Primary, |
| 399 | pressed: true, |
| 400 | modifiers: egui::Modifiers::default(), |
| 401 | }, |
| 402 | ], |
| 403 | 1060.0, |
| 404 | ); |
| 405 | render( |
| 406 | &mut app, |
| 407 | vec![egui::Event::PointerButton { |
| 408 | pos: point, |
| 409 | button: egui::PointerButton::Primary, |
| 410 | pressed: false, |
| 411 | modifiers: egui::Modifiers::default(), |
| 412 | }], |
| 413 | 1060.0, |
| 414 | ); |
| 415 | let create = render(&mut app, vec![], 1060.0); |
| 416 | assert!(create.iter().any(|(t, _)| t == "저장할 ZIP 절대 경로")); |
| 417 | let mut app = ArchiveApp::new(Box::new(f.clone()), Some(path("locked.zip")), true); |
| 418 | for _ in 0..8 { |
| 419 | app.model.poll(); |
| 420 | } |
| 421 | for width in [760.0, 1060.0] { |
| 422 | render(&mut app, vec![], width); |
| 423 | render(&mut app, vec![], width); |
| 424 | let password = render(&mut app, vec![], width); |
| 425 | assert!(password.iter().any(|(t, _)| t.contains("암호 전달"))); |
| 426 | } |
| 427 | assert!(app.model.password("synthetic secret".into())); |
| 428 | pump(&mut app.model); |
| 429 | let listed = render(&mut app, vec![], 1060.0); |
| 430 | assert!(listed.iter().any(|(t, _)| t == "읽어주세요.txt")); |
| 431 | assert!(!listed.iter().any(|(t, _)| t.contains("synthetic secret"))); |
| 432 | assert!(app.model.extract(path("out"), false, None, &mut f.clone())); |
| 433 | render(&mut app, vec![], 1060.0); |
| 434 | app.model.request_close(); |
| 435 | render(&mut app, vec![], 1060.0); |
| 436 | let confirm = render(&mut app, vec![], 1060.0); |
| 437 | assert!(confirm.iter().any(|(t, _)| t == "작업 중 닫기")); |
| 438 | app.model.confirm_close(); |
| 439 | pump(&mut app.model); |
| 440 | render(&mut app, vec![], 1060.0); |
| 441 | let result = render(&mut app, vec![], 1060.0); |
| 442 | assert!(result.iter().any(|(t, _)| t == "결과 확인 후 닫기")); |
| 443 | } |
| 444 | |
| 445 | #[test] |
| 446 | fn file_association_arguments_preserve_native_paths() { |
| 447 | let cwd = std::env::current_dir().unwrap(); |
| 448 | let (fixture, archive) = |
| 449 | launch_args(vec!["--ui-fixture".into(), "한글.zip".into()], cwd.clone()).unwrap(); |
| 450 | assert!(fixture); |
| 451 | assert_eq!(archive, Some(cwd.join("한글.zip"))); |
| 452 | assert!(launch_args(vec!["a".into(), "b".into()], cwd.clone()).is_err()); |
| 453 | #[cfg(unix)] |
| 454 | { |
| 455 | use std::os::unix::ffi::OsStringExt; |
| 456 | let name = std::ffi::OsString::from_vec(vec![b'/', 0xff, b'a']); |
| 457 | assert_eq!( |
| 458 | launch_args(vec![name.clone()], cwd).unwrap().1, |
| 459 | Some(PathBuf::from(name)) |
| 460 | ); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | #[cfg(feature = "ui")] |
| 465 | #[test] |
| 466 | fn dropped_native_file_opens_without_ui_reading_contents() { |
| 467 | use eframe::egui; |
| 468 | use zipelo_ui_memory_app::archive_screen::ArchiveApp; |
| 469 | #[derive(Debug)] |
| 470 | struct DropFile(PathBuf); |
| 471 | impl egui::DroppedFile for DropFile { |
| 472 | fn path(&self) -> &std::path::Path { |
| 473 | &self.0 |
| 474 | } |
| 475 | fn bytes(&self) -> Result<Vec<u8>, String> { |
| 476 | panic!("화면이 압축파일을 직접 읽으면 안 됨") |
| 477 | } |
| 478 | } |
| 479 | let ctx = egui::Context::default(); |
| 480 | let f = FakeFactory::default(); |
| 481 | let mut app = ArchiveApp::new(Box::new(f.clone()), None, true); |
| 482 | let file = path("dropped.zip"); |
| 483 | let mut output = ctx.run_ui( |
| 484 | egui::RawInput { |
| 485 | dropped_files: vec![std::sync::Arc::new(DropFile(file.clone()))], |
| 486 | ..Default::default() |
| 487 | }, |
| 488 | |ui| app.show(ui), |
| 489 | ); |
| 490 | output.textures_delta.clear(); |
| 491 | assert_eq!(app.model.archive, Some(file)); |
| 492 | assert_eq!(f.state.borrow().starts.len(), 1); |
| 493 | pump(&mut app.model); |
| 494 | assert!(app.model.can_extract()); |
| 495 | } |