| 1 | use std::{collections::BTreeSet, path::Path}; |
| 2 | use zipelo_ui_memory_app::{ |
| 3 | archive_dialog::archive_filters, |
| 4 | archive_present::{gzip_output_name, valid_output_leaf}, |
| 5 | }; |
| 6 | |
| 7 | #[test] |
| 8 | fn archive_filter_contains_every_case_variant_and_all_files_option() { |
| 9 | let filters = archive_filters(); |
| 10 | assert_eq!(filters.len(), 2); |
| 11 | assert_eq!(filters[0].0, "압축파일"); |
| 12 | let expected = [ |
| 13 | "zip", "ziP", "zIp", "zIP", "Zip", "ZiP", "ZIp", "ZIP", "7z", "7Z", "tar", "taR", "tAr", |
| 14 | "tAR", "Tar", "TaR", "TAr", "TAR", "gz", "gZ", "Gz", "GZ", "tgz", "tgZ", "tGz", "tGZ", |
| 15 | "Tgz", "TgZ", "TGz", "TGZ", |
| 16 | ] |
| 17 | .into_iter() |
| 18 | .map(str::to_owned) |
| 19 | .collect::<BTreeSet<_>>(); |
| 20 | assert_eq!( |
| 21 | filters[0].1.iter().cloned().collect::<BTreeSet<_>>(), |
| 22 | expected |
| 23 | ); |
| 24 | assert_eq!(filters[0].1.len(), expected.len()); |
| 25 | assert_eq!(filters[1], ("모든 파일", vec!["*".to_owned()])); |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn gzip_suggested_names_are_portable_and_preserve_valid_stems() { |
| 30 | for name in [ |
| 31 | ".gz", |
| 32 | ".GZ", |
| 33 | "report:2026.gz", |
| 34 | "report?2026.gz", |
| 35 | "CON.gz", |
| 36 | "COM1.gz", |
| 37 | "name..gz", |
| 38 | "name .gz", |
| 39 | ] { |
| 40 | assert_eq!( |
| 41 | gzip_output_name(Path::new(name)), |
| 42 | "압축 해제 파일", |
| 43 | "{name}" |
| 44 | ); |
| 45 | } |
| 46 | for (name, expected) in [ |
| 47 | ("report.gz", "report"), |
| 48 | ("report.GZ", "report"), |
| 49 | ("report.Gz", "report"), |
| 50 | ("한글.gz", "한글"), |
| 51 | ("input.archive", "input.archive"), |
| 52 | ] { |
| 53 | assert_eq!(gzip_output_name(Path::new(name)), expected); |
| 54 | } |
| 55 | for name in [ |
| 56 | "", |
| 57 | ".", |
| 58 | "..", |
| 59 | "../file", |
| 60 | "dir/file", |
| 61 | "dir\\file", |
| 62 | "a:b", |
| 63 | "a*", |
| 64 | "a?", |
| 65 | "a|b", |
| 66 | "a<b", |
| 67 | "a>b", |
| 68 | "a\"b", |
| 69 | "NUL.txt", |
| 70 | "name.", |
| 71 | "name ", |
| 72 | "control\n", |
| 73 | ] { |
| 74 | assert!(!valid_output_leaf(name), "{name:?}"); |
| 75 | } |
| 76 | assert!(valid_output_leaf("사용자 이름.txt")); |
| 77 | } |
| 78 | |
| 79 | #[cfg(target_os = "linux")] |
| 80 | #[test] |
| 81 | fn portal_response_distinguishes_cancellation_from_backend_failure() { |
| 82 | use ashpd::{Error, PortalError, desktop::ResponseError}; |
| 83 | use zipelo_ui_memory_app::archive_dialog::{DIALOG_UNAVAILABLE, portal_response}; |
| 84 | for error in [ |
| 85 | Error::Response(ResponseError::Cancelled), |
| 86 | Error::Portal(PortalError::Cancelled("cancelled".into())), |
| 87 | ] { |
| 88 | assert_eq!(portal_response(Err(error)), Ok(None)); |
| 89 | } |
| 90 | for error in [ |
| 91 | Error::Response(ResponseError::Other), |
| 92 | Error::Portal(PortalError::Failed("failed".into())), |
| 93 | Error::Portal(PortalError::WindowDestroyed("destroyed".into())), |
| 94 | Error::NoResponse, |
| 95 | Error::PortalNotFound("org.freedesktop.portal.FileChooser".try_into().unwrap()), |
| 96 | Error::IO(std::io::Error::other("backend unavailable")), |
| 97 | Error::ParseError("invalid URI"), |
| 98 | ] { |
| 99 | assert_eq!(portal_response(Err(error)), Err(DIALOG_UNAVAILABLE.into())); |
| 100 | } |
| 101 | assert_eq!(portal_response(Ok(vec![])), Err(DIALOG_UNAVAILABLE.into())); |
| 102 | let path = Path::new("/selected/archive.zip").to_path_buf(); |
| 103 | assert_eq!( |
| 104 | portal_response(Ok(vec![path.clone()])), |
| 105 | Ok(Some(vec![path])) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | #[cfg(target_os = "linux")] |
| 110 | #[test] |
| 111 | fn missing_portal_returns_errors_for_every_dialog() { |
| 112 | use std::{ |
| 113 | process::{Command, Stdio}, |
| 114 | time::{Duration, Instant}, |
| 115 | }; |
| 116 | use zipelo_ui_memory_app::archive_dialog::{ |
| 117 | DIALOG_UNAVAILABLE, DialogRequest, Dialogs, NativeDialogs, Purpose, |
| 118 | }; |
| 119 | if std::env::var_os("ZIPELO_NO_PORTAL_CHILD").is_some() { |
| 120 | let mut dialogs = NativeDialogs; |
| 121 | for purpose in [ |
| 122 | Purpose::Open, |
| 123 | Purpose::Files, |
| 124 | Purpose::Folder, |
| 125 | Purpose::Destination, |
| 126 | Purpose::Retry, |
| 127 | Purpose::SaveZip, |
| 128 | Purpose::SaveGzip, |
| 129 | Purpose::RetryGzip, |
| 130 | ] { |
| 131 | let result = dialogs.choose(DialogRequest { |
| 132 | purpose, |
| 133 | directory: std::env::current_dir().unwrap(), |
| 134 | file_name: "output.zip".into(), |
| 135 | }); |
| 136 | assert_eq!(result, Err(DIALOG_UNAVAILABLE.into()), "{purpose:?}"); |
| 137 | } |
| 138 | return; |
| 139 | } |
| 140 | let endpoint = Path::new(env!("CARGO_MANIFEST_DIR")).join(format!( |
| 141 | "../../tests/rust-app-ui/runs/no-bus-{}", |
| 142 | std::process::id() |
| 143 | )); |
| 144 | assert!(!endpoint.exists()); |
| 145 | let mut child = Command::new(std::env::current_exe().unwrap()) |
| 146 | .args([ |
| 147 | "--exact", |
| 148 | "native_dialogs::missing_portal_returns_errors_for_every_dialog", |
| 149 | "--nocapture", |
| 150 | ]) |
| 151 | .env("ZIPELO_NO_PORTAL_CHILD", "1") |
| 152 | .env( |
| 153 | "DBUS_SESSION_BUS_ADDRESS", |
| 154 | format!("unix:path={}", endpoint.display()), |
| 155 | ) |
| 156 | .env_remove("DBUS_STARTER_ADDRESS") |
| 157 | .stdout(Stdio::piped()) |
| 158 | .stderr(Stdio::piped()) |
| 159 | .spawn() |
| 160 | .unwrap(); |
| 161 | let deadline = Instant::now() + Duration::from_secs(15); |
| 162 | loop { |
| 163 | if child.try_wait().unwrap().is_some() { |
| 164 | break; |
| 165 | } |
| 166 | if Instant::now() >= deadline { |
| 167 | child.kill().unwrap(); |
| 168 | child.wait().unwrap(); |
| 169 | panic!("portal 실패 응답 시간 초과"); |
| 170 | } |
| 171 | std::thread::sleep(Duration::from_millis(10)); |
| 172 | } |
| 173 | let output = child.wait_with_output().unwrap(); |
| 174 | assert!( |
| 175 | output.status.success(), |
| 176 | "{}{}", |
| 177 | String::from_utf8_lossy(&output.stdout), |
| 178 | String::from_utf8_lossy(&output.stderr) |
| 179 | ); |
| 180 | } |