Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Round file size bounds and trim duplicate filter tests

Manav Rathif722645ec5c3
diff --git a/rust/apps/cli/src/admin.rs b/rust/apps/cli/src/admin.rs
index c97600e827..64e8180090 100644
--- a/rust/apps/cli/src/admin.rs
+++ b/rust/apps/cli/src/admin.rs
@@ -177,7 +177,7 @@ async fn update_subscription(
if !args.no_limit {
args.storage = prompt_change(
"Storage (MB/GB/TB; leave empty to keep current)",
- parse_size,
+ parse_storage,
)?;
args.expires = prompt_change(
"Expiry (date or RFC3339; leave empty to keep current)",
@@ -263,8 +263,8 @@ fn prompt_change(label: &str, parse: fn(&str) -> Result<i64, String>) -> Result<
}
}
-pub fn parse_size(input: &str) -> Result<i64, String> {
- parse_scaled_size(
+pub fn parse_storage(input: &str) -> Result<i64, String> {
+ parse_size(
input,
&[
("MB", 1024_i64.pow(2)),
@@ -273,10 +273,11 @@ pub fn parse_size(input: &str) -> Result<i64, String> {
],
"use a size such as 500GB or 1.5TB (MB/GB/TB use powers of 1024)",
)
+ .map(|bytes| bytes as i64)
}
-pub fn parse_file_size(input: &str) -> Result<u64, String> {
- let bytes = parse_scaled_size(
+pub fn parse_file_size(input: &str) -> Result<f64, String> {
+ parse_size(
input,
&[
("KB", 1024_i64),
@@ -284,11 +285,10 @@ pub fn parse_file_size(input: &str) -> Result<u64, String> {
("GB", 1024_i64.pow(3)),
],
"use a size such as 500MB or 1.5GB (KB/MB/GB use powers of 1024)",
- )?;
- u64::try_from(bytes).map_err(|error| error.to_string())
+ )
}
-fn parse_scaled_size(input: &str, units: &[(&str, i64)], hint: &str) -> Result<i64, String> {
+fn parse_size(input: &str, units: &[(&str, i64)], hint: &str) -> Result<f64, String> {
let input = input.to_ascii_uppercase();
let (number, multiplier) = units
.iter()
@@ -303,7 +303,7 @@ fn parse_scaled_size(input: &str, units: &[(&str, i64)], hint: &str) -> Result<i
{
return Err("size must be positive and fit in a signed 64-bit byte count".into());
}
- Ok(bytes as i64)
+ Ok(bytes)
}
pub fn parse_date(input: &str) -> Result<i64, String> {
@@ -319,7 +319,7 @@ pub fn parse_date(input: &str) -> Result<i64, String> {
#[cfg(test)]
mod tests {
- use super::{parse_date, parse_file_size, parse_size};
+ use super::{parse_date, parse_file_size, parse_storage};
#[test]
fn storage_uses_go_units_and_dates_use_utc_or_explicit_offsets() {
@@ -328,7 +328,7 @@ mod tests {
("1.5gB", 1610612736),
("2Tb", 2199023255552),
] {
- assert_eq!(parse_size(input).unwrap(), bytes);
+ assert_eq!(parse_storage(input).unwrap(), bytes);
}
for input in [
"0GB",
@@ -339,7 +339,7 @@ mod tests {
"999999999999TB",
"500KB",
] {
- assert!(parse_size(input).is_err(), "{input}");
+ assert!(parse_storage(input).is_err(), "{input}");
}
for input in [
"2030-01-01",
@@ -356,10 +356,11 @@ mod tests {
#[test]
fn file_sizes_use_kb_mb_and_gb() {
for (input, bytes) in [
- ("3KB", 3072),
- ("1.5kb", 1536),
- ("5MB", 5242880),
- ("1.5gB", 1610612736),
+ ("3KB", 3072.0),
+ ("1.5kb", 1536.0),
+ ("1.1KB", 1126.4),
+ ("5MB", 5242880.0),
+ ("1.5gB", 1610612736.0),
] {
assert_eq!(parse_file_size(input).unwrap(), bytes);
}
diff --git a/rust/apps/cli/src/args.rs b/rust/apps/cli/src/args.rs
index 233bd6cd55..7f0b28608e 100644
--- a/rust/apps/cli/src/args.rs
+++ b/rust/apps/cli/src/args.rs
@@ -619,7 +619,7 @@ pub struct FileQuery {
#[arg(
long,
value_name = "SIZE",
- value_parser = crate::admin::parse_file_size,
+ value_parser = min_file_size,
help = "Include files of at least this size, e.g. 100MB",
long_help = "Include files whose sizeBytes is at least SIZE. Accepts KB, MB and GB, case-insensitive, using powers of 1024; fractions are allowed, such as 1.5GB. Files with an unknown size are left out."
)]
@@ -627,7 +627,7 @@ pub struct FileQuery {
#[arg(
long,
value_name = "SIZE",
- value_parser = crate::admin::parse_file_size,
+ value_parser = max_file_size,
help = "Include files of at most this size, e.g. 1.5GB",
long_help = "Include files whose sizeBytes is at most SIZE. Accepts KB, MB and GB, case-insensitive, using powers of 1024; fractions are allowed, such as 1.5GB. Files with an unknown size are left out."
)]
@@ -666,6 +666,14 @@ fn nonempty_search(text: &str) -> Result<String, String> {
}
}
+fn min_file_size(input: &str) -> Result<u64, String> {
+ crate::admin::parse_file_size(input).map(|bytes| bytes.ceil() as u64)
+}
+
+fn max_file_size(input: &str) -> Result<u64, String> {
+ crate::admin::parse_file_size(input).map(|bytes| bytes as u64)
+}
+
const ACCOUNT_HELP: &str = "Reads accounts stored on this device without network access.
With --json, list returns an array and view returns one object.
Fields: id, name, email, host (strings); apps (string array); selected (boolean).";
@@ -995,7 +1003,7 @@ pub struct SubscriptionArgs {
pub target: AdminTarget,
#[arg(long, conflicts_with_all = ["storage", "expires"], help = "Set 100 TB of storage and another 100 years")]
pub no_limit: bool,
- #[arg(long, value_name = "SIZE", value_parser = crate::admin::parse_size, help = "Storage limit, e.g. 500GB or 1.5TB")]
+ #[arg(long, value_name = "SIZE", value_parser = crate::admin::parse_storage, help = "Storage limit, e.g. 500GB or 1.5TB")]
pub storage: Option<i64>,
#[arg(long, value_name = "WHEN", value_parser = crate::admin::parse_date, help = "Expiry date or timestamp")]
pub expires: Option<i64>,
diff --git a/rust/apps/cli/tests/cli/files.rs b/rust/apps/cli/tests/cli/files.rs
index 1d1893539b..18feff226d 100644
--- a/rust/apps/cli/tests/cli/files.rs
+++ b/rust/apps/cli/tests/cli/files.rs
@@ -600,7 +600,7 @@ fn photos_lists_limit_output_and_stream_complete_results() {
}
#[test]
-fn file_lists_filter_and_sort_synced_metadata() {
+fn file_lists_handle_size_and_date_boundaries() {
let mut server = mockito::Server::new();
let home = TestHome::new();
home.seed(&server.url());
@@ -624,10 +624,9 @@ fn file_lists_filter_and_sort_synced_metadata() {
json!([
file(
1,
- json!({"title": "Beach.jpg", "fileType": 0, "creationTime": 1_700_000_000_000_000i64,
- "latitude": 1.0, "longitude": 2.0}),
- json!({"caption": "Sunset at the beach"}),
- Some(1200),
+ json!({"title": "Beach.jpg", "fileType": 0, "creationTime": 1_700_000_000_000_000i64}),
+ json!({}),
+ Some(1126),
),
file(
2,
@@ -638,15 +637,20 @@ fn file_lists_filter_and_sort_synced_metadata() {
file(
3,
json!({"title": "Live.heic", "fileType": 2, "creationTime": 1_735_689_600_000_000i64}),
- json!({"caption": "Family dinner", "lat": 3.0, "long": 4.0}),
+ json!({}),
None,
),
file(
4,
- json!({"title": "Holiday.JPG", "fileType": 0, "creationTime": 1_262_304_000_000_000i64,
- "latitude": 0.0, "longitude": 0.0}),
+ json!({"title": "Holiday.JPG", "fileType": 0, "creationTime": 1_262_304_000_000_000i64}),
json!({"editedTime": 1_710_028_800_000_000i64}),
- Some(1200),
+ Some(1126),
+ ),
+ file(
+ 5,
+ json!({"title": "Larger.jpg", "fileType": 0, "creationTime": 1_262_304_000_000_000i64}),
+ json!({}),
+ Some(1127),
),
]),
false,
@@ -665,50 +669,22 @@ fn file_lists_filter_and_sort_synced_metadata() {
};
let listed = home.json(&["photos", "file", "list"]);
- assert_eq!(listed[0]["sizeBytes"], 1200);
+ assert_eq!(listed[0]["sizeBytes"], 1126);
assert_eq!(listed[2]["sizeBytes"], Value::Null);
albums.assert();
files.assert();
drop(server);
for (args, expected) in [
- (vec![], vec![1, 2, 3, 4]),
- (vec!["--type", "image"], vec![1, 4]),
- (vec!["--type", "video", "--type", "livephoto"], vec![2, 3]),
(vec!["--since", "2024-06-01"], vec![2, 3]),
- (vec!["--before", "2024-06-01"], vec![1, 4]),
- (
- vec!["--since", "2024-01-01", "--before", "2025-01-01"],
- vec![2, 4],
- ),
+ (vec!["--before", "2024-06-01"], vec![1, 4, 5]),
(vec!["--since", "2024-03-10T01:00:00+01:00"], vec![2, 3, 4]),
- (vec!["--search", "BEACH sunset"], vec![1]),
- (vec!["--search", "family dinner"], vec![3]),
- (vec!["--search", "holiday jpg"], vec![4]),
- (vec!["--search", "beach dinner"], vec![]),
- (vec!["--search", "jpg sunset"], vec![1]),
- (vec!["--no-location"], vec![2, 4]),
- (vec!["--type", "image", "--no-location"], vec![4]),
(vec!["--min-size", "5MB"], vec![2]),
- (vec!["--min-size", "0.5mb"], vec![2]),
- (vec!["--max-size", "5MB"], vec![1, 2, 4]),
- (vec!["--max-size", "1MB"], vec![1, 4]),
- (vec!["--min-size", "1kb"], vec![1, 2, 4]),
- (vec!["--max-size", "2KB"], vec![1, 4]),
- (vec!["--min-size", "1MB", "--max-size", "4MB"], vec![]),
- (vec!["--max-size", "1MB", "--sort", "created"], vec![4, 1]),
- (vec!["--sort", "created"], vec![3, 2, 4, 1]),
- (vec!["--sort", "created", "--reverse"], vec![1, 4, 2, 3]),
- (vec!["--sort", "size"], vec![2, 1, 4, 3]),
- (vec!["--sort", "size", "--reverse"], vec![3, 1, 4, 2]),
- (
- vec!["--type", "image", "--sort", "size", "--all"],
- vec![1, 4],
- ),
- (
- vec!["--sort", "size", "--album", "1", "--limit", "2"],
- vec![2, 1],
- ),
+ (vec!["--max-size", "5MB"], vec![1, 2, 4, 5]),
+ (vec!["--min-size", "1.1KB"], vec![2, 5]),
+ (vec!["--max-size", "1.1KB"], vec![1, 4]),
+ (vec!["--sort", "size"], vec![2, 5, 1, 4, 3]),
+ (vec!["--sort", "size", "--reverse"], vec![3, 1, 4, 5, 2]),
] {
assert_eq!(ids(&args), expected, "{args:?}");
}
@@ -725,27 +701,12 @@ fn file_lists_filter_and_sort_synced_metadata() {
]));
let stdout = String::from_utf8_lossy(&output.stdout);
let mut lines = stdout.lines();
- assert!(lines.next().unwrap().contains("SIZE"));
- assert!(lines.next().unwrap().starts_with("2 "));
+ assert!(lines.nth(1).unwrap().starts_with("2 "));
assert!(String::from_utf8_lossy(&output.stderr).contains("Showing first 1"));
let output = success(home.run(&["--offline", "photos", "file", "view", "1"]));
- assert!(String::from_utf8_lossy(&output.stdout).contains("1.17 KB (1200 bytes)"));
-
- for args in [
- vec!["--since", "yesterday"],
- vec!["--before", "2024-06-01T00:00:00"],
- vec!["--search", " "],
- vec!["--type", "gif"],
- vec!["--sort", "name"],
- vec!["--reverse"],
- vec!["--min-size", "100B"],
- vec!["--max-size", "1TB"],
- vec!["--max-size", "0MB"],
- ] {
- let mut command = vec!["--offline", "photos", "file", "list"];
- command.extend(&args);
- failure(&home.run(&command));
- }
+ assert!(String::from_utf8_lossy(&output.stdout).contains("1.1 KB (1126 bytes)"));
+ let output = home.run(&["--offline", "photos", "file", "list", "--search", " "]);
+ assert!(failure(&output).contains("supply search text"));
}
pub(super) fn collection(id: i64, name: &str, key: &Key) -> Value {
diff --git a/rust/apps/cli/tests/cli/museum.rs b/rust/apps/cli/tests/cli/museum.rs
index f3d18dee39..5e2d20c33a 100644
--- a/rust/apps/cli/tests/cli/museum.rs
+++ b/rust/apps/cli/tests/cli/museum.rs
@@ -358,8 +358,9 @@ fn file_lists_filter_and_sort_server_metadata() -> TestResult {
vec!["--since", "2024-01-01", "--before", "2025-01-01"],
by_id(vec![clip, holiday]),
),
- (vec!["--search", "BEACH sunset"], vec![beach]),
+ (vec!["--search", "JPG sunset"], vec![beach]),
(vec!["--search", "family dinner"], vec![live]),
+ (vec!["--search", "beach dinner"], vec![]),
(vec!["--no-location"], by_id(vec![clip, holiday])),
(vec!["--album", "Family", "--no-location"], vec![holiday]),
(vec!["--min-size", "1.5KB"], by_id(vec![clip, holiday])),