| 1 | use super::*; |
| 2 | use std::{ |
| 3 | collections::BTreeSet, |
| 4 | fs, |
| 5 | sync::atomic::{AtomicU64, Ordering}, |
| 6 | }; |
| 7 | use zipelo_ui_memory_app::archive_present::*; |
| 8 | |
| 9 | fn terminal() -> Terminal { |
| 10 | Terminal { |
| 11 | outcome: Outcome::Failed, |
| 12 | error: Some(ErrorCode::Io), |
| 13 | publication: Publication::Partial, |
| 14 | created_count: 1, |
| 15 | retained_partial_count: 0, |
| 16 | durability: Durability::Synced, |
| 17 | cleanup: Cleanup::Complete, |
| 18 | } |
| 19 | } |
| 20 | fn targets() -> BTreeSet<String> { |
| 21 | ["one.txt".into(), "two.txt".into()].into_iter().collect() |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn partial_extraction_requires_matching_file_reports() { |
| 26 | let t = terminal(); |
| 27 | assert_eq!( |
| 28 | warnings( |
| 29 | Some(&t), |
| 30 | true, |
| 31 | Some(&targets()), |
| 32 | &[("one.txt".into(), Disposition::Created)] |
| 33 | ), |
| 34 | vec![PARTIAL] |
| 35 | ); |
| 36 | } |
| 37 | #[test] |
| 38 | fn all_saved_with_temporary_leftover_is_not_partial_extraction() { |
| 39 | let mut t = terminal(); |
| 40 | t.retained_partial_count = 1; |
| 41 | t.cleanup = Cleanup::Failed; |
| 42 | let files = ["one.txt".into()].into_iter().collect(); |
| 43 | assert_eq!( |
| 44 | warnings( |
| 45 | Some(&t), |
| 46 | true, |
| 47 | Some(&files), |
| 48 | &[ |
| 49 | ("one.txt".into(), Disposition::Created), |
| 50 | ("temp".into(), Disposition::PartialRetained) |
| 51 | ] |
| 52 | ), |
| 53 | vec![LEFTOVERS] |
| 54 | ); |
| 55 | } |
| 56 | #[test] |
| 57 | fn leftover_without_file_reports_and_commit_uncertainty_are_unknown() { |
| 58 | let mut t = terminal(); |
| 59 | t.created_count = 0; |
| 60 | t.retained_partial_count = 1; |
| 61 | for outputs in [ |
| 62 | vec![("temp".into(), Disposition::PartialRetained)], |
| 63 | vec![("one.txt".into(), Disposition::CommitUncertain)], |
| 64 | ] { |
| 65 | assert!(warnings(Some(&t), true, Some(&targets()), &outputs).contains(&UNCERTAIN)); |
| 66 | assert!(!warnings(Some(&t), true, Some(&targets()), &outputs).contains(&PARTIAL)); |
| 67 | } |
| 68 | } |
| 69 | #[test] |
| 70 | fn synchronization_and_cleanup_warnings_are_both_visible() { |
| 71 | let mut t = terminal(); |
| 72 | t.created_count = 0; |
| 73 | t.durability = Durability::Unconfirmed; |
| 74 | t.cleanup = Cleanup::Failed; |
| 75 | let result = warnings(Some(&t), true, Some(&targets()), &[]); |
| 76 | assert!(result.contains(&SYNC_WARNING)); |
| 77 | assert!(result.contains(&CLEANUP_WARNING)); |
| 78 | } |
| 79 | #[test] |
| 80 | fn missing_terminal_exit_or_contradictory_reports_never_claim_unsaved() { |
| 81 | let mut t = terminal(); |
| 82 | t.publication = Publication::None; |
| 83 | t.created_count = 0; |
| 84 | assert!(definitely_unsaved(Some(&t), true, &[])); |
| 85 | assert!(!definitely_unsaved(None, true, &[])); |
| 86 | assert!(!definitely_unsaved(Some(&t), false, &[])); |
| 87 | let reports = [("one.txt".into(), Disposition::Created)]; |
| 88 | assert!(!definitely_unsaved(Some(&t), true, &reports)); |
| 89 | assert!(warnings(Some(&t), true, Some(&targets()), &reports).contains(&UNCERTAIN)); |
| 90 | } |
| 91 | #[test] |
| 92 | fn unsafe_path_and_conflict_preserve_result_classification() { |
| 93 | for code in [ErrorCode::UnsafePath, ErrorCode::Conflict] { |
| 94 | let mut t = terminal(); |
| 95 | t.error = Some(code); |
| 96 | assert_eq!( |
| 97 | warnings( |
| 98 | Some(&t), |
| 99 | true, |
| 100 | Some(&targets()), |
| 101 | &[("one.txt".into(), Disposition::Created)] |
| 102 | ), |
| 103 | vec![PARTIAL] |
| 104 | ); |
| 105 | t.publication = Publication::Uncertain; |
| 106 | assert!(warnings(Some(&t), true, Some(&targets()), &[]).contains(&UNCERTAIN)); |
| 107 | t.publication = Publication::None; |
| 108 | t.created_count = 0; |
| 109 | assert!(definitely_unsaved(Some(&t), true, &[])); |
| 110 | } |
| 111 | } |
| 112 | #[test] |
| 113 | fn every_error_and_stage_has_user_facing_text() { |
| 114 | for code in [ |
| 115 | ErrorCode::Integrity, |
| 116 | ErrorCode::UnsupportedFormat, |
| 117 | ErrorCode::UnsupportedFeature, |
| 118 | ErrorCode::WrongPassword, |
| 119 | ErrorCode::UnsafePath, |
| 120 | ErrorCode::Conflict, |
| 121 | ErrorCode::Limit, |
| 122 | ErrorCode::Io, |
| 123 | ErrorCode::DiskFull, |
| 124 | ErrorCode::Protocol, |
| 125 | ErrorCode::Internal, |
| 126 | ErrorCode::SourceChanged, |
| 127 | ErrorCode::InvalidRequest, |
| 128 | ] { |
| 129 | assert!(!error_text(code).is_empty()); |
| 130 | assert!(!error_text(code).contains("작업자")); |
| 131 | } |
| 132 | assert_eq!(error_text(ErrorCode::Protocol), INTERNAL); |
| 133 | assert_eq!(error_text(ErrorCode::Internal), INTERNAL); |
| 134 | assert_eq!(progress_text(Stage::Decode, true), "압축하는 중"); |
| 135 | assert_eq!(progress_text(Stage::Decode, false), "압축을 푸는 중"); |
| 136 | } |
| 137 | #[test] |
| 138 | fn folder_selection_expands_only_matching_descendants() { |
| 139 | let entries = vec![ |
| 140 | Entry { |
| 141 | id: 0, |
| 142 | name: "docs".into(), |
| 143 | kind: Kind::Directory, |
| 144 | bytes: None, |
| 145 | packed: None, |
| 146 | encrypted: false, |
| 147 | }, |
| 148 | Entry { |
| 149 | id: 1, |
| 150 | name: "docs/one.txt".into(), |
| 151 | kind: Kind::File, |
| 152 | bytes: None, |
| 153 | packed: None, |
| 154 | encrypted: false, |
| 155 | }, |
| 156 | Entry { |
| 157 | id: 2, |
| 158 | name: "docs-old/two.txt".into(), |
| 159 | kind: Kind::File, |
| 160 | bytes: None, |
| 161 | packed: None, |
| 162 | encrypted: false, |
| 163 | }, |
| 164 | ]; |
| 165 | assert_eq!( |
| 166 | target_files(&entries, &[0].into_iter().collect(), true, None), |
| 167 | Some(["docs/one.txt".into()].into_iter().collect()) |
| 168 | ); |
| 169 | } |
| 170 | #[test] |
| 171 | fn duplicate_names_prevent_file_completion_assumptions() { |
| 172 | let entry = Entry { |
| 173 | id: 0, |
| 174 | name: "same.txt".into(), |
| 175 | kind: Kind::File, |
| 176 | bytes: None, |
| 177 | packed: None, |
| 178 | encrypted: false, |
| 179 | }; |
| 180 | assert!(target_files(&[entry.clone(), entry], &BTreeSet::new(), false, None).is_none()); |
| 181 | assert_eq!( |
| 182 | warnings(Some(&terminal()), true, None, &[]), |
| 183 | vec![UNCERTAIN] |
| 184 | ); |
| 185 | } |
| 186 | #[test] |
| 187 | fn default_names_and_middle_truncation_preserve_unicode() { |
| 188 | assert_eq!(default_destination(&path("자료.tar.gz")), path("자료")); |
| 189 | assert_eq!(default_destination(&path("자료.zip")), path("자료")); |
| 190 | assert_eq!(middle_text("가나다라마바사아자차", 7), "가나다…아자차"); |
| 191 | assert_eq!(bytes_text(21), "21 B"); |
| 192 | } |
| 193 | |
| 194 | static SCRATCH_ID: AtomicU64 = AtomicU64::new(0); |
| 195 | pub(super) struct Folder(pub(super) PathBuf); |
| 196 | impl Folder { |
| 197 | pub(super) fn new() -> Self { |
| 198 | let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/rust-app-ui/runs"); |
| 199 | fs::create_dir_all(&root).unwrap(); |
| 200 | let path = root.join(format!( |
| 201 | "presentation-{}-{}", |
| 202 | std::process::id(), |
| 203 | SCRATCH_ID.fetch_add(1, Ordering::Relaxed) |
| 204 | )); |
| 205 | fs::create_dir(&path).unwrap(); |
| 206 | Self(path.canonicalize().unwrap()) |
| 207 | } |
| 208 | } |
| 209 | impl Drop for Folder { |
| 210 | fn drop(&mut self) { |
| 211 | fs::remove_dir_all(&self.0).unwrap(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn automatic_destination_creation_and_failure_are_distinct() { |
| 217 | let scratch = Folder::new(); |
| 218 | let output = scratch.0.join("output"); |
| 219 | assert!(prepare_destination(&output, true).unwrap()); |
| 220 | assert!(!prepare_destination(&output, true).unwrap()); |
| 221 | assert!(prepare_destination(&scratch.0.join("missing/child"), true).is_err()); |
| 222 | assert!(prepare_destination(&scratch.0.join("chosen-missing"), false).is_err()); |
| 223 | let file = scratch.0.join("file"); |
| 224 | fs::write(&file, "existing").unwrap(); |
| 225 | assert!(prepare_destination(&file, true).is_err()); |
| 226 | assert_eq!(fs::read_to_string(file).unwrap(), "existing"); |
| 227 | } |
| 228 | #[test] |
| 229 | fn only_new_empty_output_is_removed_and_nonempty_is_preserved() { |
| 230 | let scratch = Folder::new(); |
| 231 | let empty = scratch.0.join("empty"); |
| 232 | assert!(prepare_destination(&empty, true).unwrap()); |
| 233 | remove_owned_empty(&empty); |
| 234 | assert!(!empty.exists()); |
| 235 | let nonempty = scratch.0.join("nonempty"); |
| 236 | assert!(prepare_destination(&nonempty, true).unwrap()); |
| 237 | fs::write(nonempty.join("existing"), "keep").unwrap(); |
| 238 | remove_owned_empty(&nonempty); |
| 239 | assert_eq!( |
| 240 | fs::read_to_string(nonempty.join("existing")).unwrap(), |
| 241 | "keep" |
| 242 | ); |
| 243 | assert!(!prepare_destination(&scratch.0, false).unwrap()); |
| 244 | assert!(scratch.0.is_dir()); |
| 245 | } |
| 246 | #[cfg(unix)] |
| 247 | #[test] |
| 248 | fn browser_and_output_do_not_follow_symbolic_links() { |
| 249 | let scratch = Folder::new(); |
| 250 | let link = scratch.0.join("link"); |
| 251 | std::os::unix::fs::symlink(&scratch.0, &link).unwrap(); |
| 252 | let rows = directory_entries(&scratch.0).unwrap(); |
| 253 | assert!(rows.iter().any(|entry| entry.link && entry.path == link)); |
| 254 | assert!(directory_entries(&link).is_err()); |
| 255 | assert!(prepare_destination(&link, true).is_err()); |
| 256 | } |
| 257 | |
| 258 | #[cfg(feature = "ui")] |
| 259 | mod widgets { |
| 260 | use super::*; |
| 261 | use eframe::egui::{self, epaint::Shape}; |
| 262 | use zipelo_ui_memory_app::{ |
| 263 | archive_picker::{Choice, Picker, Purpose}, |
| 264 | archive_screen::ArchiveApp, |
| 265 | }; |
| 266 | |
| 267 | fn labels(shapes: &[egui::epaint::ClippedShape]) -> Vec<(String, egui::Pos2)> { |
| 268 | fn walk(shape: &Shape, result: &mut Vec<(String, egui::Pos2)>) { |
| 269 | match shape { |
| 270 | Shape::Text(text) => result.push(( |
| 271 | text.galley.job.text.clone(), |
| 272 | text.pos + text.galley.rect.size() / 2.0, |
| 273 | )), |
| 274 | Shape::Vec(shapes) => { |
| 275 | for shape in shapes { |
| 276 | walk(shape, result); |
| 277 | } |
| 278 | } |
| 279 | _ => {} |
| 280 | } |
| 281 | } |
| 282 | let mut result = Vec::new(); |
| 283 | for shape in shapes { |
| 284 | walk(&shape.shape, &mut result); |
| 285 | } |
| 286 | result |
| 287 | } |
| 288 | fn render( |
| 289 | ctx: &egui::Context, |
| 290 | app: &mut ArchiveApp, |
| 291 | events: Vec<egui::Event>, |
| 292 | ) -> Vec<(String, egui::Pos2)> { |
| 293 | let mut output = ctx.run_ui( |
| 294 | egui::RawInput { |
| 295 | screen_rect: Some(egui::Rect::from_min_size( |
| 296 | egui::Pos2::ZERO, |
| 297 | egui::vec2(760.0, 540.0), |
| 298 | )), |
| 299 | events, |
| 300 | ..Default::default() |
| 301 | }, |
| 302 | |ui| app.show(ui), |
| 303 | ); |
| 304 | output.textures_delta.clear(); |
| 305 | labels(&output.shapes) |
| 306 | } |
| 307 | fn click(ctx: &egui::Context, app: &mut ArchiveApp, label: &str) { |
| 308 | render(ctx, app, vec![]); |
| 309 | let point = render(ctx, app, vec![]) |
| 310 | .into_iter() |
| 311 | .filter(|(text, _)| text == label) |
| 312 | .max_by(|a, b| a.1.y.total_cmp(&b.1.y)) |
| 313 | .unwrap_or_else(|| panic!("항목 없음: {label}")) |
| 314 | .1; |
| 315 | for pressed in [true, false] { |
| 316 | render( |
| 317 | ctx, |
| 318 | app, |
| 319 | vec![ |
| 320 | egui::Event::PointerMoved(point), |
| 321 | egui::Event::PointerButton { |
| 322 | pos: point, |
| 323 | button: egui::PointerButton::Primary, |
| 324 | pressed, |
| 325 | modifiers: egui::Modifiers::NONE, |
| 326 | }, |
| 327 | ], |
| 328 | ); |
| 329 | } |
| 330 | } |
| 331 | fn key(key: egui::Key) -> egui::Event { |
| 332 | egui::Event::Key { |
| 333 | key, |
| 334 | physical_key: None, |
| 335 | pressed: true, |
| 336 | repeat: false, |
| 337 | modifiers: egui::Modifiers::NONE, |
| 338 | } |
| 339 | } |
| 340 | fn text(ctx: &egui::Context, app: &mut ArchiveApp, id: &str, value: &str) { |
| 341 | ctx.memory_mut(|memory| memory.request_focus(egui::Id::unique(id))); |
| 342 | render( |
| 343 | ctx, |
| 344 | app, |
| 345 | vec![ |
| 346 | egui::Event::Key { |
| 347 | key: egui::Key::A, |
| 348 | physical_key: None, |
| 349 | pressed: true, |
| 350 | repeat: false, |
| 351 | modifiers: egui::Modifiers::COMMAND, |
| 352 | }, |
| 353 | egui::Event::Text(value.into()), |
| 354 | ], |
| 355 | ); |
| 356 | } |
| 357 | fn loaded(scratch: &Folder, factory: &FakeFactory) -> ArchiveApp { |
| 358 | let mut app = ArchiveApp::new( |
| 359 | Box::new(factory.clone()), |
| 360 | Some(scratch.0.join("sample.zip")), |
| 361 | false, |
| 362 | ); |
| 363 | pump(&mut app.model); |
| 364 | app |
| 365 | } |
| 366 | #[test] |
| 367 | fn empty_view_has_no_internal_status_and_keyboard_focus_is_reachable() { |
| 368 | let ctx = egui::Context::default(); |
| 369 | let mut app = ArchiveApp::new(Box::new(FakeFactory::default()), None, false); |
| 370 | render(&ctx, &mut app, vec![]); |
| 371 | let shown = render(&ctx, &mut app, vec![key(egui::Key::Tab)]); |
| 372 | assert!( |
| 373 | shown |
| 374 | .iter() |
| 375 | .any(|(text, _)| text == "압축파일을 여기에 끌어 놓으세요") |
| 376 | ); |
| 377 | assert!(!shown.iter().any(|(text, _)| text.contains("게시") |
| 378 | || text.contains("동기화") |
| 379 | || text.contains("시험 모드"))); |
| 380 | assert!(ctx.memory(|memory| memory.focused().is_some())); |
| 381 | app.model.request_close(); |
| 382 | let closing = render(&ctx, &mut app, vec![]); |
| 383 | assert!(!closing.iter().any(|(text, _)| text == UNCERTAIN)); |
| 384 | } |
| 385 | #[test] |
| 386 | fn empty_selection_and_empty_zip_sources_do_not_start_work() { |
| 387 | let scratch = Folder::new(); |
| 388 | let ctx = egui::Context::default(); |
| 389 | let factory = FakeFactory::default(); |
| 390 | let mut app = loaded(&scratch, &factory); |
| 391 | render(&ctx, &mut app, vec![]); |
| 392 | click(&ctx, &mut app, "선택한 항목만 풀기"); |
| 393 | click(&ctx, &mut app, "풀기"); |
| 394 | assert_eq!(factory.state.borrow().starts.len(), 1); |
| 395 | click(&ctx, &mut app, "ZIP 만들기"); |
| 396 | click(&ctx, &mut app, "만들기"); |
| 397 | assert_eq!(factory.state.borrow().starts.len(), 1); |
| 398 | assert!(!app.model.busy()); |
| 399 | } |
| 400 | #[test] |
| 401 | fn destination_failure_sends_no_request_and_explains_folder_change() { |
| 402 | let scratch = Folder::new(); |
| 403 | let factory = FakeFactory::default(); |
| 404 | let mut app = loaded(&scratch, &factory); |
| 405 | let file = scratch.0.join("existing.txt"); |
| 406 | fs::write(&file, "preserve").unwrap(); |
| 407 | app.choose_destination(file.clone()); |
| 408 | assert!(!app.begin_extract()); |
| 409 | assert_eq!(factory.state.borrow().starts.len(), 1); |
| 410 | let ctx = egui::Context::default(); |
| 411 | render(&ctx, &mut app, vec![]); |
| 412 | assert!( |
| 413 | render(&ctx, &mut app, vec![]) |
| 414 | .iter() |
| 415 | .any(|(text, _)| text == FOLDER_ERROR) |
| 416 | ); |
| 417 | assert_eq!(fs::read_to_string(file).unwrap(), "preserve"); |
| 418 | } |
| 419 | #[test] |
| 420 | fn cancelling_removes_only_automatically_created_empty_folder() { |
| 421 | for case in ["automatic-empty", "automatic-nonempty", "chosen-empty"] { |
| 422 | let scratch = Folder::new(); |
| 423 | let factory = FakeFactory::default(); |
| 424 | let mut app = loaded(&scratch, &factory); |
| 425 | factory.state.borrow_mut().manual = true; |
| 426 | let destination = if case == "chosen-empty" { |
| 427 | let path = scratch.0.join("chosen"); |
| 428 | fs::create_dir(&path).unwrap(); |
| 429 | app.choose_destination(path.clone()); |
| 430 | path |
| 431 | } else { |
| 432 | scratch.0.join("sample") |
| 433 | }; |
| 434 | assert!(app.begin_extract()); |
| 435 | if case == "automatic-nonempty" { |
| 436 | fs::write(destination.join("preserve"), "keep").unwrap(); |
| 437 | } |
| 438 | { |
| 439 | let mut state = factory.state.borrow_mut(); |
| 440 | state.frame(Event::Accepted { encryption: true }); |
| 441 | state.finish(Outcome::Cancelled, Publication::None); |
| 442 | } |
| 443 | let ctx = egui::Context::default(); |
| 444 | for _ in 0..30 { |
| 445 | render(&ctx, &mut app, vec![]); |
| 446 | } |
| 447 | assert!(!app.model.busy()); |
| 448 | assert_eq!(destination.exists(), case != "automatic-empty"); |
| 449 | } |
| 450 | } |
| 451 | #[test] |
| 452 | fn password_confirmation_requires_match_and_enter_sends_once() { |
| 453 | let scratch = Folder::new(); |
| 454 | let factory = FakeFactory::manual(); |
| 455 | let mut app = ArchiveApp::new(Box::new(factory.clone()), None, false); |
| 456 | assert!(app.model.begin( |
| 457 | Start::CreateZip { |
| 458 | sources: vec![(scratch.0.join("source"), "source".into())], |
| 459 | output_file: scratch.0.join("new.zip"), |
| 460 | method: Method::Deflate, |
| 461 | aes256: true |
| 462 | }, |
| 463 | &mut factory.clone() |
| 464 | )); |
| 465 | { |
| 466 | let mut state = factory.state.borrow_mut(); |
| 467 | state.frame(Event::Accepted { encryption: true }); |
| 468 | state.frame(Event::PasswordRequired { |
| 469 | challenge_id: 17, |
| 470 | attempt: 1, |
| 471 | encrypt: true, |
| 472 | }); |
| 473 | } |
| 474 | let ctx = egui::Context::default(); |
| 475 | for _ in 0..12 { |
| 476 | render(&ctx, &mut app, vec![]); |
| 477 | } |
| 478 | assert_eq!( |
| 479 | ctx.memory(|memory| memory.focused()), |
| 480 | Some(egui::Id::unique("archive-password-input")) |
| 481 | ); |
| 482 | text(&ctx, &mut app, "archive-password-input", "fixture-secret"); |
| 483 | text(&ctx, &mut app, "archive-password-confirm", "different"); |
| 484 | click(&ctx, &mut app, "확인"); |
| 485 | assert!(!factory.state.borrow().password_seen); |
| 486 | text(&ctx, &mut app, "archive-password-confirm", "fixture-secret"); |
| 487 | let shown = render(&ctx, &mut app, vec![key(egui::Key::Enter)]); |
| 488 | assert!( |
| 489 | !shown |
| 490 | .iter() |
| 491 | .any(|(text, _)| text.contains("fixture-secret")) |
| 492 | ); |
| 493 | assert!(factory.state.borrow().password_seen); |
| 494 | assert!(app.model.challenge.is_none()); |
| 495 | } |
| 496 | #[test] |
| 497 | fn picker_fits_minimum_window_and_opens_selected_file_with_enter() { |
| 498 | let scratch = Folder::new(); |
| 499 | let file = scratch.0.join("one.zip"); |
| 500 | fs::write(&file, "fixture").unwrap(); |
| 501 | let ctx = egui::Context::default(); |
| 502 | let mut picker = Picker::new(Purpose::Open, &scratch.0); |
| 503 | let mut result = None; |
| 504 | let mut draw = |events| { |
| 505 | let mut output = ctx.run_ui( |
| 506 | egui::RawInput { |
| 507 | screen_rect: Some(egui::Rect::from_min_size( |
| 508 | egui::Pos2::ZERO, |
| 509 | egui::vec2(760.0, 540.0), |
| 510 | )), |
| 511 | events, |
| 512 | ..Default::default() |
| 513 | }, |
| 514 | |_ui| { |
| 515 | result = picker.show(&ctx); |
| 516 | }, |
| 517 | ); |
| 518 | output.textures_delta.clear(); |
| 519 | labels(&output.shapes) |
| 520 | }; |
| 521 | draw(vec![]); |
| 522 | let shown = draw(vec![]); |
| 523 | for label in ["파일 열기", "열기", "홈"] { |
| 524 | let (_, point) = shown.iter().find(|(text, _)| text == label).unwrap(); |
| 525 | assert!((0.0..540.0).contains(&point.y), "{label}: {point:?}"); |
| 526 | } |
| 527 | let cancel = shown.iter().find(|(text, _)| text == "취소").unwrap().1; |
| 528 | let confirm = shown.iter().find(|(text, _)| text == "열기").unwrap().1; |
| 529 | assert!(cancel.x < confirm.x); |
| 530 | let point = shown.iter().find(|(text, _)| text == "one.zip").unwrap().1; |
| 531 | for pressed in [true, false] { |
| 532 | draw(vec![ |
| 533 | egui::Event::PointerMoved(point), |
| 534 | egui::Event::PointerButton { |
| 535 | pos: point, |
| 536 | button: egui::PointerButton::Primary, |
| 537 | pressed, |
| 538 | modifiers: egui::Modifiers::NONE, |
| 539 | }, |
| 540 | ]); |
| 541 | } |
| 542 | draw(vec![key(egui::Key::Enter)]); |
| 543 | assert!(matches!(result, Some(Choice::Paths(ref paths)) if paths == &vec![file])); |
| 544 | } |
| 545 | |
| 546 | fn focus_zip_action_with_tab(ctx: &egui::Context, app: &mut ArchiveApp, label: &str) { |
| 547 | render(ctx, app, vec![]); |
| 548 | let shown = render(ctx, app, vec![]); |
| 549 | let point = shown.iter().find(|(text, _)| text == label).unwrap().1; |
| 550 | for _ in 0..80 { |
| 551 | if ctx |
| 552 | .memory(|memory| memory.focused()) |
| 553 | .and_then(|id| ctx.read_response(id)) |
| 554 | .is_some_and(|response| response.rect.contains(point)) |
| 555 | { |
| 556 | return; |
| 557 | } |
| 558 | render(ctx, app, vec![key(egui::Key::Tab)]); |
| 559 | let mut released = key(egui::Key::Tab); |
| 560 | if let egui::Event::Key { pressed, .. } = &mut released { |
| 561 | *pressed = false; |
| 562 | } |
| 563 | render(ctx, app, vec![released]); |
| 564 | } |
| 565 | panic!("키보드 초점 없음: {label}"); |
| 566 | } |
| 567 | |
| 568 | #[test] |
| 569 | fn keyboard_cancel_and_escape_never_start_zip_creation() { |
| 570 | for escape in [false, true] { |
| 571 | let scratch = Folder::new(); |
| 572 | let factory = FakeFactory::manual(); |
| 573 | let mut app = ArchiveApp::new(Box::new(factory.clone()), None, false); |
| 574 | let source = scratch.0.join("source.txt"); |
| 575 | fs::write(&source, "fixture").unwrap(); |
| 576 | app.add_sources(vec![source]); |
| 577 | let ctx = egui::Context::default(); |
| 578 | focus_zip_action_with_tab(&ctx, &mut app, if escape { "만들기" } else { "취소" }); |
| 579 | let mut events = vec![key(egui::Key::Enter)]; |
| 580 | if escape { |
| 581 | events.push(key(egui::Key::Escape)); |
| 582 | } |
| 583 | render(&ctx, &mut app, events); |
| 584 | let shown = render(&ctx, &mut app, vec![]); |
| 585 | assert!(factory.state.borrow().starts.is_empty()); |
| 586 | assert!(!app.model.busy()); |
| 587 | assert!(!scratch.0.join("source.zip").exists()); |
| 588 | assert_eq!( |
| 589 | shown |
| 590 | .iter() |
| 591 | .filter(|(text, _)| text == "ZIP 만들기") |
| 592 | .count(), |
| 593 | 1 |
| 594 | ); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | #[test] |
| 599 | fn zip_creation_enter_only_activates_focused_make_button() { |
| 600 | for make_button in [false, true] { |
| 601 | let scratch = Folder::new(); |
| 602 | let factory = FakeFactory::manual(); |
| 603 | let mut app = ArchiveApp::new(Box::new(factory.clone()), None, false); |
| 604 | let source = scratch.0.join("source.txt"); |
| 605 | fs::write(&source, "fixture").unwrap(); |
| 606 | app.add_sources(vec![source.clone()]); |
| 607 | let ctx = egui::Context::default(); |
| 608 | render(&ctx, &mut app, vec![]); |
| 609 | if make_button { |
| 610 | focus_zip_action_with_tab(&ctx, &mut app, "만들기"); |
| 611 | } else { |
| 612 | ctx.memory_mut(|memory| memory.request_focus(egui::Id::unique("zip-name"))); |
| 613 | render(&ctx, &mut app, vec![]); |
| 614 | } |
| 615 | render(&ctx, &mut app, vec![key(egui::Key::Enter)]); |
| 616 | render(&ctx, &mut app, vec![]); |
| 617 | let state = factory.state.borrow(); |
| 618 | assert_eq!(state.starts.len(), usize::from(make_button)); |
| 619 | if make_button { |
| 620 | assert!( |
| 621 | matches!(state.starts.last(), Some(Start::CreateZip {sources, output_file, ..}) |
| 622 | if sources == &vec![(source, "source.txt".into())] |
| 623 | && output_file == &scratch.0.join("source.zip")) |
| 624 | ); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | fn picker_frame( |
| 630 | ctx: &egui::Context, |
| 631 | picker: &mut Picker, |
| 632 | events: Vec<egui::Event>, |
| 633 | ) -> (Vec<(String, egui::Pos2)>, Option<Choice>) { |
| 634 | let mut choice = None; |
| 635 | let mut output = ctx.run_ui( |
| 636 | egui::RawInput { |
| 637 | screen_rect: Some(egui::Rect::from_min_size( |
| 638 | egui::Pos2::ZERO, |
| 639 | egui::vec2(760.0, 540.0), |
| 640 | )), |
| 641 | events, |
| 642 | ..Default::default() |
| 643 | }, |
| 644 | |_ui| choice = picker.show(ctx), |
| 645 | ); |
| 646 | output.textures_delta.clear(); |
| 647 | (labels(&output.shapes), choice) |
| 648 | } |
| 649 | |
| 650 | fn keyboard_activate_picker( |
| 651 | ctx: &egui::Context, |
| 652 | picker: &mut Picker, |
| 653 | label: &str, |
| 654 | ) -> Option<Choice> { |
| 655 | picker_frame(ctx, picker, vec![]); |
| 656 | let shown = picker_frame(ctx, picker, vec![]).0; |
| 657 | let point = shown.iter().find(|(text, _)| text == label).unwrap().1; |
| 658 | for _ in 0..80 { |
| 659 | let focused = ctx.memory(|memory| memory.focused()); |
| 660 | if focused |
| 661 | .and_then(|id| ctx.read_response(id)) |
| 662 | .is_some_and(|response| response.rect.contains(point)) |
| 663 | { |
| 664 | let choice = picker_frame(ctx, picker, vec![key(egui::Key::Enter)]).1; |
| 665 | let mut released = key(egui::Key::Enter); |
| 666 | if let egui::Event::Key { pressed, .. } = &mut released { |
| 667 | *pressed = false; |
| 668 | } |
| 669 | picker_frame(ctx, picker, vec![released]); |
| 670 | return choice; |
| 671 | } |
| 672 | picker_frame(ctx, picker, vec![key(egui::Key::Tab)]); |
| 673 | let mut released = key(egui::Key::Tab); |
| 674 | if let egui::Event::Key { pressed, .. } = &mut released { |
| 675 | *pressed = false; |
| 676 | } |
| 677 | picker_frame(ctx, picker, vec![released]); |
| 678 | } |
| 679 | panic!("키보드 초점 없음: {label}"); |
| 680 | } |
| 681 | |
| 682 | #[test] |
| 683 | fn keyboard_file_pickers_enter_child_before_confirming_file() { |
| 684 | for purpose in [Purpose::Open, Purpose::Files] { |
| 685 | let scratch = Folder::new(); |
| 686 | let child = scratch.0.join("child"); |
| 687 | fs::create_dir(&child).unwrap(); |
| 688 | let file = child.join("inside.zip"); |
| 689 | fs::write(&file, "fixture").unwrap(); |
| 690 | let ctx = egui::Context::default(); |
| 691 | let mut picker = Picker::new(purpose, &scratch.0); |
| 692 | assert!(keyboard_activate_picker(&ctx, &mut picker, "child").is_none()); |
| 693 | assert!( |
| 694 | picker_frame(&ctx, &mut picker, vec![]) |
| 695 | .0 |
| 696 | .iter() |
| 697 | .any(|(text, _)| text == "inside.zip") |
| 698 | ); |
| 699 | let choice = keyboard_activate_picker(&ctx, &mut picker, "inside.zip"); |
| 700 | assert!(matches!(choice, Some(Choice::Paths(paths)) if paths == vec![file])); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | #[test] |
| 705 | fn keyboard_folder_pickers_navigate_before_explicit_folder_confirmation() { |
| 706 | for purpose in [ |
| 707 | Purpose::Destination, |
| 708 | Purpose::Retry, |
| 709 | Purpose::Folder, |
| 710 | Purpose::ZipFolder, |
| 711 | ] { |
| 712 | let scratch = Folder::new(); |
| 713 | let child = scratch.0.join("child"); |
| 714 | fs::create_dir(&child).unwrap(); |
| 715 | fs::write(child.join("inside.zip"), "fixture").unwrap(); |
| 716 | let ctx = egui::Context::default(); |
| 717 | let mut picker = Picker::new(purpose, &scratch.0); |
| 718 | assert!(keyboard_activate_picker(&ctx, &mut picker, "child").is_none()); |
| 719 | assert!( |
| 720 | picker_frame(&ctx, &mut picker, vec![]) |
| 721 | .0 |
| 722 | .iter() |
| 723 | .any(|(text, _)| text == "inside.zip") |
| 724 | ); |
| 725 | let choice = keyboard_activate_picker(&ctx, &mut picker, "이 폴더 선택"); |
| 726 | assert!(matches!(choice, Some(Choice::Paths(paths)) if paths == vec![child])); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | #[test] |
| 731 | fn changed_zip_sources_retry_creation_with_or_without_open_archive() { |
| 732 | for opened in [false, true] { |
| 733 | let scratch = Folder::new(); |
| 734 | let factory = FakeFactory::default(); |
| 735 | let mut app = ArchiveApp::new( |
| 736 | Box::new(factory.clone()), |
| 737 | opened.then(|| scratch.0.join("unrelated.zip")), |
| 738 | false, |
| 739 | ); |
| 740 | if opened { |
| 741 | pump(&mut app.model); |
| 742 | } |
| 743 | factory.state.borrow_mut().manual = true; |
| 744 | let source = scratch.0.join("source.txt"); |
| 745 | fs::write(&source, "fixture").unwrap(); |
| 746 | app.add_sources(vec![source]); |
| 747 | let ctx = egui::Context::default(); |
| 748 | click(&ctx, &mut app, "만들기"); |
| 749 | assert!(app.model.busy()); |
| 750 | let request = factory.state.borrow().starts.last().unwrap().clone(); |
| 751 | assert!(matches!(request, Start::CreateZip { .. })); |
| 752 | { |
| 753 | let mut state = factory.state.borrow_mut(); |
| 754 | state.frame(Event::Accepted { encryption: false }); |
| 755 | state.frame(Event::Terminal(Terminal { |
| 756 | outcome: Outcome::Failed, |
| 757 | error: Some(ErrorCode::SourceChanged), |
| 758 | publication: Publication::None, |
| 759 | created_count: 0, |
| 760 | retained_partial_count: 0, |
| 761 | durability: Durability::NotApplicable, |
| 762 | cleanup: Cleanup::Complete, |
| 763 | })); |
| 764 | state.queue.extend([ |
| 765 | HostEvent::StdoutEof, |
| 766 | HostEvent::StderrEof, |
| 767 | HostEvent::Exited { success: true }, |
| 768 | ]); |
| 769 | } |
| 770 | for _ in 0..30 { |
| 771 | render(&ctx, &mut app, vec![]); |
| 772 | } |
| 773 | assert!(!app.model.busy()); |
| 774 | let shown = render(&ctx, &mut app, vec![]); |
| 775 | assert!( |
| 776 | shown |
| 777 | .iter() |
| 778 | .any(|(text, _)| text.contains("묶을 파일이 바뀌었습니다")) |
| 779 | ); |
| 780 | assert!(!shown.iter().any(|(text, _)| text == "압축파일 다시 열기")); |
| 781 | let count = factory.state.borrow().starts.len(); |
| 782 | click(&ctx, &mut app, "다시 만들기"); |
| 783 | assert!(app.model.busy()); |
| 784 | let state = factory.state.borrow(); |
| 785 | assert_eq!(state.starts.len(), count + 1); |
| 786 | assert_eq!(state.starts.last(), Some(&request)); |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | #[cfg(feature = "ui")] |
| 792 | #[test] |
| 793 | fn conflict_retry_waits_for_folder_choice_and_preserves_selection() { |
| 794 | use eframe::egui; |
| 795 | use zipelo_ui_memory_app::archive_screen::ArchiveApp; |
| 796 | let scratch = Folder::new(); |
| 797 | let factory = FakeFactory::default(); |
| 798 | let mut app = ArchiveApp::new( |
| 799 | Box::new(factory.clone()), |
| 800 | Some(scratch.0.join("sample.zip")), |
| 801 | false, |
| 802 | ); |
| 803 | pump(&mut app.model); |
| 804 | app.model.selected.insert(0); |
| 805 | factory.state.borrow_mut().manual = true; |
| 806 | let ctx = egui::Context::default(); |
| 807 | let render = |app: &mut ArchiveApp, events| { |
| 808 | let mut output = ctx.run_ui( |
| 809 | egui::RawInput { |
| 810 | screen_rect: Some(egui::Rect::from_min_size( |
| 811 | egui::Pos2::ZERO, |
| 812 | egui::vec2(760.0, 540.0), |
| 813 | )), |
| 814 | events, |
| 815 | ..Default::default() |
| 816 | }, |
| 817 | |ui| app.show(ui), |
| 818 | ); |
| 819 | output.textures_delta.clear(); |
| 820 | fn walk(shape: &egui::epaint::Shape, labels: &mut Vec<(String, egui::Pos2)>) { |
| 821 | match shape { |
| 822 | egui::epaint::Shape::Text(text) => labels.push(( |
| 823 | text.galley.job.text.clone(), |
| 824 | text.pos + text.galley.rect.size() / 2.0, |
| 825 | )), |
| 826 | egui::epaint::Shape::Vec(shapes) => { |
| 827 | for shape in shapes { |
| 828 | walk(shape, labels); |
| 829 | } |
| 830 | } |
| 831 | _ => {} |
| 832 | } |
| 833 | } |
| 834 | let mut labels = Vec::new(); |
| 835 | for shape in &output.shapes { |
| 836 | walk(&shape.shape, &mut labels); |
| 837 | } |
| 838 | labels |
| 839 | }; |
| 840 | let click = |app: &mut ArchiveApp, text: &str| { |
| 841 | render(app, vec![]); |
| 842 | let point = render(app, vec![]) |
| 843 | .into_iter() |
| 844 | .filter(|(value, _)| value == text) |
| 845 | .max_by(|a, b| a.1.y.total_cmp(&b.1.y)) |
| 846 | .unwrap_or_else(|| panic!("항목 없음: {text}")) |
| 847 | .1; |
| 848 | for pressed in [true, false] { |
| 849 | render( |
| 850 | app, |
| 851 | vec![ |
| 852 | egui::Event::PointerMoved(point), |
| 853 | egui::Event::PointerButton { |
| 854 | pos: point, |
| 855 | button: egui::PointerButton::Primary, |
| 856 | pressed, |
| 857 | modifiers: egui::Modifiers::NONE, |
| 858 | }, |
| 859 | ], |
| 860 | ); |
| 861 | } |
| 862 | }; |
| 863 | render(&mut app, vec![]); |
| 864 | click(&mut app, "선택한 항목만 풀기"); |
| 865 | assert!(app.begin_extract()); |
| 866 | let old = scratch.0.join("sample"); |
| 867 | fs::write(old.join("existing"), "preserve").unwrap(); |
| 868 | let chosen = scratch.0.join("chosen"); |
| 869 | fs::create_dir(&chosen).unwrap(); |
| 870 | { |
| 871 | let mut state = factory.state.borrow_mut(); |
| 872 | state.frame(Event::Accepted { encryption: true }); |
| 873 | state.frame(Event::Terminal(Terminal { |
| 874 | outcome: Outcome::Failed, |
| 875 | error: Some(ErrorCode::Conflict), |
| 876 | publication: Publication::None, |
| 877 | created_count: 0, |
| 878 | retained_partial_count: 0, |
| 879 | durability: Durability::NotApplicable, |
| 880 | cleanup: Cleanup::Complete, |
| 881 | })); |
| 882 | state.queue.extend([ |
| 883 | HostEvent::StdoutEof, |
| 884 | HostEvent::StderrEof, |
| 885 | HostEvent::Exited { success: true }, |
| 886 | ]); |
| 887 | } |
| 888 | for _ in 0..30 { |
| 889 | render(&mut app, vec![]); |
| 890 | } |
| 891 | assert_eq!(factory.state.borrow().starts.len(), 2); |
| 892 | click(&mut app, "다른 폴더에 풀기"); |
| 893 | assert_eq!(factory.state.borrow().starts.len(), 2); |
| 894 | click(&mut app, "상위 폴더"); |
| 895 | click(&mut app, "chosen"); |
| 896 | click(&mut app, "이 폴더 선택"); |
| 897 | assert!(matches!(factory.state.borrow().starts.last(), |
| 898 | Some(Start::Extract { destination, selection: Selection::EntryIds(ids), .. }) |
| 899 | if destination == &chosen && ids == &vec![0])); |
| 900 | assert_eq!( |
| 901 | fs::read_to_string(old.join("existing")).unwrap(), |
| 902 | "preserve" |
| 903 | ); |
| 904 | } |
| 905 | |
| 906 | #[cfg(feature = "ui")] |
| 907 | #[test] |
| 908 | fn create_dialog_keeps_title_and_actions_visible_after_resize() { |
| 909 | use eframe::egui::{self, epaint::Shape}; |
| 910 | use zipelo_ui_memory_app::archive_screen::ArchiveApp; |
| 911 | let scratch = Folder::new(); |
| 912 | let mut app = ArchiveApp::new(Box::new(FakeFactory::default()), None, false); |
| 913 | app.add_sources(vec![scratch.0.join("one.txt"), scratch.0.join("photos")]); |
| 914 | let ctx = egui::Context::default(); |
| 915 | for size in [egui::vec2(1060.0, 720.0), egui::vec2(760.0, 540.0)] { |
| 916 | let mut texts = Vec::new(); |
| 917 | for _ in 0..8 { |
| 918 | let mut output = ctx.run_ui( |
| 919 | egui::RawInput { |
| 920 | screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, size)), |
| 921 | ..Default::default() |
| 922 | }, |
| 923 | |ui| app.show(ui), |
| 924 | ); |
| 925 | output.textures_delta.clear(); |
| 926 | fn walk(shape: &Shape, output: &mut Vec<(String, egui::Pos2)>) { |
| 927 | match shape { |
| 928 | Shape::Text(text) => output.push(( |
| 929 | text.galley.job.text.clone(), |
| 930 | text.pos + text.galley.rect.size() / 2.0, |
| 931 | )), |
| 932 | Shape::Vec(shapes) => { |
| 933 | for shape in shapes { |
| 934 | walk(shape, output); |
| 935 | } |
| 936 | } |
| 937 | _ => {} |
| 938 | } |
| 939 | } |
| 940 | texts.clear(); |
| 941 | for shape in &output.shapes { |
| 942 | walk(&shape.shape, &mut texts); |
| 943 | } |
| 944 | } |
| 945 | for label in ["ZIP 만들기", "만들기", "취소"] { |
| 946 | let positions = texts |
| 947 | .iter() |
| 948 | .filter(|(text, _)| text == label) |
| 949 | .map(|(_, p)| p) |
| 950 | .collect::<Vec<_>>(); |
| 951 | assert!(!positions.is_empty(), "{label}"); |
| 952 | assert!( |
| 953 | positions.iter().all(|p| p.y >= 0.0 && p.y <= size.y), |
| 954 | "{label}: {positions:?}" |
| 955 | ); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | #[test] |
| 960 | fn folder_open_command_preserves_shell_characters_as_one_argument() { |
| 961 | let path = path("한글 폴더 ; $(touch unexpected)"); |
| 962 | let command = folder_command(&path); |
| 963 | assert_eq!( |
| 964 | command.get_program(), |
| 965 | if cfg!(windows) { |
| 966 | "explorer" |
| 967 | } else { |
| 968 | "xdg-open" |
| 969 | } |
| 970 | ); |
| 971 | assert_eq!( |
| 972 | command.get_args().collect::<Vec<_>>(), |
| 973 | vec![path.as_os_str()] |
| 974 | ); |
| 975 | } |
| 976 | |
| 977 | #[test] |
| 978 | fn late_cancel_and_terminal_wait_use_finishing_text() { |
| 979 | let mut model = Model::default(); |
| 980 | model.progress = Some((Stage::Decode, 1, Some(2), 0)); |
| 981 | assert_eq!(activity_text(&model, false), "압축을 푸는 중"); |
| 982 | model.cancel = Some(CancelStatus::Requested); |
| 983 | assert_eq!(activity_text(&model, false), "취소하는 중..."); |
| 984 | for state in [CancelStatus::TooLate, CancelStatus::Finished] { |
| 985 | model.cancel = Some(state); |
| 986 | assert_eq!(activity_text(&model, false), "마무리하는 중"); |
| 987 | } |
| 988 | model.cancel = Some(CancelStatus::Requested); |
| 989 | model.terminal = Some(terminal()); |
| 990 | assert_eq!(activity_text(&model, false), "마무리하는 중"); |
| 991 | } |
| 992 | #[test] |
| 993 | fn client_protocol_errors_hide_internal_terms_but_preserve_exit_uncertainty() { |
| 994 | for error in [ |
| 995 | "작업 ID 한도에 도달했습니다", |
| 996 | "작업자 응답 순서나 작업 ID가 일치하지 않습니다", |
| 997 | "작업 승인 응답이 없습니다", |
| 998 | "출력 보고 한도 초과", |
| 999 | ] { |
| 1000 | assert_eq!(client_error_text(error), INTERNAL); |
| 1001 | } |
| 1002 | assert_eq!( |
| 1003 | client_error_text("작업자 종료 결과를 확인할 수 없습니다. 출력 상태를 확인하세요"), |
| 1004 | UNCONFIRMED_FINISH |
| 1005 | ); |
| 1006 | } |