Warn before watch history is left behind, and re-apply it later (#2)
CI / test (push) Successful in 1m16s
CI / test (push) Successful in 1m16s
This commit is contained in:
1 parent
2bd4489b1a
commit
8cfa0f05eb
15 files changed
+611
-73
No files matched your search
@@ -726,6 +726,95 @@ async fn refuses_a_season_folder_holding_other_seasons() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn warns_about_missing_users_and_reapplies_their_watch_state_later() {
|
||||
let (cloud, home) = pair(seed_movie).await;
|
||||
// Cloud has a second user, Bob, whom home doesn't know yet.
|
||||
cloud.fake.add_user("u2", "Bob");
|
||||
let movie = cloud.fake.find("Movie", "Alien").unwrap();
|
||||
cloud.fake.set_user_data(&movie, json!({"Played": true}));
|
||||
cloud
|
||||
.fake
|
||||
.set_user_data_for("u2", &movie, json!({"PlaybackPositionTicks": 777}));
|
||||
|
||||
// Before moving, the UI can see whose watch history won't carry over,
|
||||
// whichever side it asks from.
|
||||
let check = get(
|
||||
&cloud.url,
|
||||
&format!("/api/moves/check?from=local&item_id={movie}"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(check["watchers"], json!(["Alice", "Bob"]));
|
||||
assert_eq!(check["missing_users"], json!(["Bob"]));
|
||||
let check = get(
|
||||
&home.url,
|
||||
&format!("/api/moves/check?from=peer&item_id={movie}"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(check["missing_users"], json!(["Bob"]));
|
||||
|
||||
// A dry run says so too.
|
||||
let dry: Value = post(
|
||||
&cloud.url,
|
||||
"/api/moves",
|
||||
json!({"from": "local", "item_id": movie, "name": "Alien", "dry_run": true}),
|
||||
)
|
||||
.await
|
||||
.json()
|
||||
.await
|
||||
.unwrap();
|
||||
let dry = wait_job(&cloud.url, dry["id"].as_str().unwrap()).await;
|
||||
assert_eq!(dry["status"]["kind"], "Planned");
|
||||
assert_eq!(dry["missing_users"], json!(["Bob"]));
|
||||
assert!(
|
||||
dry["log"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|l| l.as_str().unwrap().contains("won't carry over for Bob")),
|
||||
"{dry:#}"
|
||||
);
|
||||
|
||||
// The move goes ahead; Alice's watch state follows, Bob's is kept.
|
||||
let job = move_item(&cloud.url, "local", &movie, "Alien").await;
|
||||
assert_eq!(job["missing_users"], json!(["Bob"]));
|
||||
let new_id = home.fake.find("Movie", "Alien").unwrap();
|
||||
assert_eq!(home.fake.get_user_data(&new_id).unwrap()["Played"], true);
|
||||
let id = job["id"].as_str().unwrap();
|
||||
|
||||
// Re-applying before Bob has an account explains what to do.
|
||||
let r = post(&cloud.url, &format!("/api/jobs/{id}/reapply"), json!({})).await;
|
||||
assert!(!r.status().is_success());
|
||||
let text = r.text().await.unwrap();
|
||||
assert!(
|
||||
text.contains("no user called Bob on home yet; create them in Jellyfin on home first"),
|
||||
"{text}"
|
||||
);
|
||||
assert!(!text.contains("HTTP 502"), "{text}");
|
||||
|
||||
// Alice carries on watching on home in the meantime.
|
||||
home.fake
|
||||
.set_user_data(&new_id, json!({"Played": true, "PlayCount": 5}));
|
||||
|
||||
// Once home has a "bob" (names match ignoring case), re-apply from the
|
||||
// cloud UI: the job runs on home, so the request is passed on.
|
||||
home.fake.add_user("u9", "bob");
|
||||
let r = post(&cloud.url, &format!("/api/jobs/{id}/reapply"), json!({})).await;
|
||||
assert!(r.status().is_success(), "{}", r.text().await.unwrap());
|
||||
let job: Value = r.json().await.unwrap();
|
||||
assert_eq!(job["missing_users"], json!([]));
|
||||
assert_eq!(
|
||||
home.fake.get_user_data_for("u9", &new_id).unwrap()["PlaybackPositionTicks"],
|
||||
777
|
||||
);
|
||||
// Alice's newer watch state wasn't overwritten by the old snapshot.
|
||||
assert_eq!(home.fake.get_user_data(&new_id).unwrap()["PlayCount"], 5);
|
||||
|
||||
// Nothing left to re-apply.
|
||||
let r = post(&home.url, &format!("/api/jobs/{id}/reapply"), json!({})).await;
|
||||
assert!(r.text().await.unwrap().contains("no skipped watch history"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn peer_api_rejects_bad_token_and_ui_requires_csrf_header() {
|
||||
let (cloud, _home) = pair(|_| {}).await;
|
||||
|
||||
@@ -47,6 +47,8 @@ pub struct Fake {
|
||||
items: Arc<Mutex<Vec<FakeItem>>>,
|
||||
/// (user id, item id) → UserData JSON
|
||||
pub user_data: Arc<Mutex<HashMap<(String, String), Value>>>,
|
||||
/// (user id, name)
|
||||
users: Arc<Mutex<Vec<(String, String)>>>,
|
||||
}
|
||||
|
||||
pub fn id_for(path: &Path) -> String {
|
||||
@@ -103,6 +105,7 @@ impl Fake {
|
||||
libs: Arc::new(libs),
|
||||
items: Default::default(),
|
||||
user_data: Default::default(),
|
||||
users: Arc::new(Mutex::new(vec![("u1".into(), "Alice".into())])),
|
||||
};
|
||||
f.scan();
|
||||
f
|
||||
@@ -258,6 +261,25 @@ impl Fake {
|
||||
.map(|i| i.id.clone())
|
||||
}
|
||||
|
||||
pub fn add_user(&self, id: &str, name: &str) {
|
||||
self.users.lock().unwrap().push((id.into(), name.into()));
|
||||
}
|
||||
|
||||
pub fn set_user_data_for(&self, user: &str, item: &str, data: Value) {
|
||||
self.user_data
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((user.into(), item.into()), data);
|
||||
}
|
||||
|
||||
pub fn get_user_data_for(&self, user: &str, item: &str) -> Option<Value> {
|
||||
self.user_data
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get(&(user.to_string(), item.to_string()))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn set_user_data(&self, item: &str, data: Value) {
|
||||
self.user_data
|
||||
.lock()
|
||||
@@ -281,10 +303,7 @@ impl Fake {
|
||||
Json(json!({"ServerName": "fake", "Version": "10.11.0", "Id": "x"}))
|
||||
}),
|
||||
)
|
||||
.route(
|
||||
"/Users",
|
||||
get(|| async { Json(json!([{"Id": "u1", "Name": "Alice"}])) }),
|
||||
)
|
||||
.route("/Users", get(users))
|
||||
.route("/Library/VirtualFolders", get(virtual_folders))
|
||||
.route("/Items", get(items))
|
||||
.route("/Library/Media/Updated", post(media_updated))
|
||||
@@ -297,6 +316,17 @@ impl Fake {
|
||||
}
|
||||
}
|
||||
|
||||
async fn users(State(f): State<Fake>) -> Json<Value> {
|
||||
Json(Value::Array(
|
||||
f.users
|
||||
.lock()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|(id, name)| json!({"Id": id, "Name": name}))
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
async fn virtual_folders(State(f): State<Fake>) -> Json<Value> {
|
||||
Json(Value::Array(
|
||||
f.libs
|
||||
|
||||
Reference in new issue
Block a user