use super::*;
use crate::types::Relations;
use crate::types::media::{FileEpisode, MediaItemReference};
fn reference(id: &str, conf: Option<u16>) -> MediaItemReference {
MediaItemReference {
id: id.into(),
conf,
}
}
fn tag(id: &str, name: &str, path: &str, generated: bool) -> Tag {
serde_json::from_value(serde_json::json!({
"id": id, "name": name, "parent": null, "alt": null, "modified": 0, "added": 0,
"generated": generated, "path": path
}))
.unwrap()
}
fn person(id: &str, name: &str) -> Person {
Person {
id: id.into(),
name: name.into(),
..Default::default()
}
}
fn names() -> Names {
Names::new(
vec![
person("p2", "Élodie"),
person("p1", "Alice"),
person("p3", "Bob Alison"),
],
vec![
tag("t1", "beach", "/", false),
tag("t2", "cat", "/animals", false),
tag("t3", "sunset", "/", true),
tag("t4", "summer", "/", false),
tag("t5", "alpine", "/", true),
],
vec![Serie {
id: "s1".into(),
name: "Alps trip".into(),
..Default::default()
}],
vec![Movie {
id: "f1".into(),
name: "Alien".into(),
..Default::default()
}],
vec![Book {
id: "b1".into(),
name: "Alice in Wonderland".into(),
..Default::default()
}],
)
}
fn photo() -> MediaItem {
MediaItem {
item: Media {
id: "m".into(),
name: "IMG_0001.jpg".into(),
description: Some(" ".into()),
kind: FileType::Photo,
mimetype: "image/jpeg".into(),
size: Some(3_500_000),
created: Some(1_700_000_000_000),
added: Some(1_700_000_100_000),
rating: Some(8.),
width: Some(4032),
height: Some(3024),
model: Some("iPhone 15".into()),
focal: Some(26),
f_number: Some(1.8),
sspeed: Some("1/120".into()),
iso: Some(100),
icc: Some("Display P3".into()),
uploader: Some("u1".into()),
lat: Some(48.8566),
long: Some(2.3522),
original_id: Some("orig".into()),
origin: Some(RsLink {
platform: "link".into(),
id: "https://www.example.com/p/1".into(),
..Default::default()
}),
..Default::default()
},
relations: Some(Relations {
people: Some(vec![reference("p1", None), reference("gone", None)]),
tags: Some(vec![
reference("t1", None),
reference("t2", Some(85)),
reference("t4", Some(100)),
reference("t3", Some(60)),
reference("t5", Some(90)),
]),
series: Some(vec![
FileEpisode {
id: "s1".into(),
season: Some(1),
episode: Some(2),
episode_to: None,
},
FileEpisode {
id: "s1".into(),
season: Some(3),
episode: None,
episode_to: None,
},
]),
movies: Some(vec!["f1".into()]),
books: Some(vec!["unknown-book".into()]),
..Default::default()
}),
}
}
#[test]
fn a_photo_shows_its_rows_dates_place_and_origin() {
let details = MediaDetails::new(&photo(), &names());
assert_eq!(details.name, "IMG_0001.jpg");
assert_eq!(details.description, None, "blank");
assert_eq!(details.rating, Some(8));
let dates: Vec<(&str, i64)> = details.dates.iter().map(|d| (d.label, d.ms)).collect();
assert_eq!(
dates,
[("Taken", 1_700_000_000_000), ("Added", 1_700_000_100_000)]
);
assert_eq!(details.dates[0].kind, DateKind::Taken);
let rows: Vec<(&str, &str)> = (details.info.iter())
.map(|d| (d.label, d.value.as_str()))
.collect();
assert_eq!(
rows,
[
("Type", "image/jpeg"),
("Size", "3.3 MB"),
("Dimensions", "4032 × 3024 (12 MP)"),
("Camera", "iPhone 15"),
("Exposure", "26 mm · ƒ/1.8 · 1/120 s · ISO 100"),
("Color profile", "Display P3"),
("Uploader", "u1"),
]
);
let place = details.place.unwrap();
assert_eq!(place.label, "48.85660, 2.35220");
assert_eq!(
place.map_url,
"https://www.openstreetmap.org/?mlat=48.856600&mlon=2.352200#map=15/48.856600/2.352200"
);
assert_eq!(
details.origin,
Some(Origin {
label: "example.com".into(),
url: Some("https://www.example.com/p/1".into())
})
);
assert_eq!(details.original.as_deref(), Some("orig"));
assert_eq!(details.pages, None);
}
#[test]
fn relations_show_as_chips_with_their_names() {
let details = MediaDetails::new(&photo(), &names());
let people: Vec<&str> = details.people.iter().map(|c| c.name.as_str()).collect();
assert_eq!(people, ["Alice", "gone"], "an unknown name shows the id");
let series: Vec<(Option<&str>, Option<u32>)> = (details.series.iter())
.map(|c| (c.detail.as_deref(), c.season))
.collect();
assert_eq!(
series,
[(Some("S1 · E2"), Some(1)), (Some("Season 3"), Some(3))]
);
assert_eq!(details.series[0].name, "Alps trip");
assert_eq!(details.movie.as_ref().unwrap().name, "Alien");
assert_eq!(details.book.as_ref().unwrap().name, "unknown-book");
let tags: Vec<(&str, Option<&str>, bool)> = (details.tags.iter())
.map(|t| {
(
t.chip.name.as_str(),
t.chip.detail.as_deref(),
t.to_validate,
)
})
.collect();
assert_eq!(
tags,
[
("beach", None, false),
("summer", Some("100%"), false),
("cat", Some("85% · /animals"), true),
]
);
let generated: Vec<&str> = (details.generated_tags.iter())
.map(|t| t.chip.name.as_str())
.collect();
assert_eq!(generated, ["alpine", "sunset"]);
assert!(details.generated_tags.iter().all(|t| t.generated));
}
#[test]
fn episode_labels_say_what_they_have() {
assert_eq!(episode_label(Some(1), Some(2), None).unwrap(), "S1 · E2");
assert_eq!(
episode_label(Some(1), Some(2), Some(4)).unwrap(),
"S1 · E2–E4"
);
assert_eq!(episode_label(Some(1), None, None).unwrap(), "Season 1");
assert_eq!(episode_label(None, Some(2), None).unwrap(), "Episode 2");
assert_eq!(episode_label(None, None, None), None);
}
#[test]
fn a_video_shows_its_duration_and_stream() {
let video = MediaItem {
item: Media {
name: "clip.mp4".into(),
kind: FileType::Video,
mimetype: "video/mp4".into(),
duration: Some(65),
fps: Some(29.97002997),
vcodecs: Some(vec!["h264".into()]),
acodecs: Some(vec!["aac".into()]),
bitrate: Some(12_400_000),
rating: Some(0.),
..Default::default()
},
relations: None,
};
let details = MediaDetails::new(&video, &Names::default());
let rows: Vec<(&str, &str)> = (details.info.iter())
.map(|d| (d.label, d.value.as_str()))
.collect();
assert_eq!(
rows,
[
("Type", "video/mp4"),
("Duration", "1:05"),
("Video", "29.97 fps · H.264, AAC · 12.4 Mb/s"),
]
);
assert_eq!(details.rating, None);
assert!(details.dates.is_empty() && details.place.is_none());
assert!(is_video(&video.item));
assert!(!has_relations(&video));
assert!(has_relations(&photo()));
}
#[test]
fn an_origin_names_its_platform_or_host() {
let link = |platform: &str, id: &str| RsLink {
platform: platform.into(),
id: id.into(),
..Default::default()
};
assert_eq!(
origin(&link("twitter", "123")),
Origin {
label: "twitter".into(),
url: None
}
);
assert_eq!(origin(&link("link", "not a url")).label, "not a url");
assert_eq!(origin(&link("link", " ")).label, "link");
}
fn choices(typed: &str, media: &MediaItem, generated: bool) -> Vec<(RelationKind, String)> {
(add_choices(typed, &names(), media, generated, 2).into_iter())
.map(|c| (c.kind, c.name))
.collect()
}
#[test]
fn the_add_field_offers_what_the_media_lacks() {
let bare = MediaItem::default();
let found = |kind: RelationKind, name: &str| (kind, name.to_owned());
assert_eq!(
choices("al", &bare, false),
[
found(RelationKind::Person, "Alice"),
found(RelationKind::Person, "Bob Alison"),
found(RelationKind::Tag, "cat"),
found(RelationKind::Serie, "Alps trip"),
found(RelationKind::Movie, "Alien"),
found(RelationKind::Book, "Alice in Wonderland"),
],
"the name's start first, then a word's, then a path; generated tags left out"
);
for typed in ["animals ca", "ca animals", "anim cat"] {
assert_eq!(
choices(typed, &bare, false),
[found(RelationKind::Tag, "cat")],
"{typed}"
);
}
assert!(choices("cat dog", &bare, false).is_empty());
assert_eq!(
choices("al", &bare, true)[2],
found(RelationKind::Tag, "alpine")
);
assert!(choices(" ", &bare, true).is_empty());
assert_eq!(
choices("elo", &bare, false),
[found(RelationKind::Person, "Élodie")]
);
let media = photo();
let offered = choices("a", &media, true);
assert!(!offered.contains(&found(RelationKind::Person, "Alice")));
assert!(!offered.contains(&found(RelationKind::Movie, "Alien")));
assert!(
offered.contains(&found(RelationKind::Tag, "alpine")),
"90 %"
);
assert!(!choices("beach", &media, false).contains(&found(RelationKind::Tag, "beach")));
assert!(choices("cat", &media, false).contains(&found(RelationKind::Tag, "cat")));
let cat = add_choices("cat", &names(), &bare, false, 5);
assert_eq!(cat[0].detail.as_deref(), Some("/animals"));
}
#[test]
fn offers_to_create_what_has_no_exact_name() {
let names = names();
assert!(!can_create("alice", &names, RelationKind::Person));
assert!(!can_create(" ELODIE ", &names, RelationKind::Person));
assert!(can_create("Ali", &names, RelationKind::Person));
assert!(can_create("alice", &names, RelationKind::Tag));
assert!(!can_create(" ", &names, RelationKind::Tag));
}
#[test]
fn a_person_picker_lists_people_by_name() {
let names = names();
let all: Vec<&str> = names.people().map(|p| p.name.as_str()).collect();
assert_eq!(all, ["Alice", "Bob Alison", "Élodie"]);
let first: Vec<String> = (names.person_choices("", 2).into_iter())
.map(|c| c.name)
.collect();
assert_eq!(first, ["Alice", "Bob Alison"]);
let found: Vec<String> = (names.person_choices("ali", 5).into_iter())
.map(|c| c.name)
.collect();
assert_eq!(found, ["Alice", "Bob Alison"]);
assert_eq!(names.person_item("p2").unwrap().name, "Élodie");
assert_eq!(names.serie_item("s1").unwrap().name, "Alps trip");
assert_eq!(names.serie("nope"), None);
}
#[cfg(feature = "mirror")]
#[test]
fn chips_add_and_remove_through_the_queue() {
let added = add_action("m", RelationKind::Tag, "t2");
assert_eq!(
added,
Action::MediaAddTags {
media: "m".into(),
tags: vec![reference("t2", None)]
}
);
assert_eq!(
add_action("m", RelationKind::Movie, "f1"),
Action::MediaMovie {
media: "m".into(),
movie: "f1".into()
}
);
let details = MediaDetails::new(&photo(), &names());
assert_eq!(
remove_action("m", &details.series[1]),
Action::MediaRemoveSeries {
media: "m".into(),
series: vec![FileEpisode {
id: "s1".into(),
season: Some(3),
episode: None,
episode_to: None,
}]
}
);
assert_eq!(
remove_action("m", details.movie.as_ref().unwrap()),
Action::MediaRemoveMovie {
media: "m".into(),
movie: details.movie.as_ref().unwrap().id.clone()
}
);
let pasted = paste_tags_action("m", &[reference("t1", Some(80))]);
assert_eq!(pasted.describe(), "Add 1 tag to media");
}
#[test]
fn a_typed_place_reads_with_a_comma_or_spaces() {
assert_eq!(parse_place("48.8566, 2.3522"), Some((48.8566, 2.3522)));
assert_eq!(parse_place("48.8566,2.3522"), Some((48.8566, 2.3522)));
assert_eq!(parse_place(" -33.9 151.2 "), Some((-33.9, 151.2)));
assert_eq!(parse_place("48.8566"), None);
assert_eq!(parse_place("91, 2"), None);
assert_eq!(parse_place("1, 2, 3"), None);
assert_eq!(parse_place("north, east"), None);
}
#[test]
fn faces_split_by_whether_they_have_a_person() {
let face = |id: &str, person: Option<&str>, confidence: Option<f32>| FaceEmbedding {
id: id.into(),
embedding: Vec::new(),
media_ref: None,
bbox: None,
confidence,
pose: None,
person_id: person.map(str::to_owned),
};
let rows = faces(
vec![
face("f1", Some("p1"), Some(0.916)),
face("f2", None, None),
face("f3", Some("gone"), Some(0.5)),
],
&names(),
);
let assigned: Vec<(&str, &str, Option<&str>)> = (rows.assigned.iter())
.map(|f| {
let person = f.person.as_ref().unwrap();
(f.id.as_str(), person.name.as_str(), f.confidence.as_deref())
})
.collect();
assert_eq!(
assigned,
[("f1", "Alice", Some("92%")), ("f3", "gone", Some("50%"))]
);
assert_eq!(rows.unassigned[0].id, "f2");
assert_eq!(rows.unassigned[0].confidence, None);
}
#[test]
fn access_follows_roles_and_the_uploader() {
let library = |roles: serde_json::Value| -> ServerLibraryForRead {
serde_json::from_value(serde_json::json!({
"id": "l", "name": "L", "type": "photos", "roles": roles
}))
.unwrap()
};
let media = photo().item;
let read = library(serde_json::json!(["read"]));
assert_eq!(access(&read, &media, Some("u2")), Access::default());
assert_eq!(
access(&read, &media, Some("u1")),
Access {
edit: true,
delete: true,
admin: false
},
"the uploader"
);
assert_eq!(
access(&library(serde_json::json!(["write"])), &media, None),
Access {
edit: true,
delete: false,
admin: false
}
);
let all = Access {
edit: true,
delete: true,
admin: true,
};
assert_eq!(
access(&library(serde_json::json!(["admin"])), &media, None),
all
);
assert_eq!(access(&library(serde_json::Value::Null), &media, None), all);
}
#[test]
fn a_turned_photo_shows_its_sides_swapped() {
let turned = Media {
width: Some(4000),
height: Some(3000),
orientation: Some(6),
..Default::default()
};
assert_eq!(shown_size(&turned), (3000, 4000));
let upright = Media {
orientation: Some(1),
..turned
};
assert_eq!(shown_size(&upright), (4000, 3000));
}