diff --git a/src-tauri/src/cloud.rs b/src-tauri/src/cloud.rs index 98260755..8043d600 100644 --- a/src-tauri/src/cloud.rs +++ b/src-tauri/src/cloud.rs @@ -549,8 +549,67 @@ fn first_time_paused(id: &str) -> bool { true } +/// What a game's saves are doing right now, for its page: "check", "up" or "down", then "idle". +/// The page asks saves_status for how it ended. +fn phase(app: &AppHandle, id: &str, what: &str) { + let _ = app.emit("save-sync", json!({"id": id, "phase": what})); +} + +/// Says "idle" when the work it covers ends, on every way out of it (errors included). +struct Working { + app: AppHandle, + id: String, +} + +impl Drop for Working { + fn drop(&mut self) { + phase(&self.app, &self.id, "idle"); + } +} + +fn working(app: &AppHandle, id: &str, what: &str) -> Working { + phase(app, id, what); + Working { app: app.clone(), id: id.to_string() } +} + +/// The kind of trouble behind a failed upload, as saves_status reports it. +fn problem_kind(e: &Error) -> &'static str { + if paused() { + return "paused"; + } + if is_full(e) { + return "full"; + } + if e.to_string().starts_with("This save is too big") { + return "too-big-copy"; + } + if matches!(e, Error::Net(_)) { + return "offline"; + } + "error" +} + +/// Remembers why this game's saves didn't go up, so its page can still say so after the toast is +/// gone. A conflict is kept on its own (upload sets it), not as a problem. +fn note_failure(dir: &Path, id: &str, e: &Error) { + if e.to_string() == SYNC_CONFLICT { + return; + } + let mut s = saves::state(dir, id); + s.problem = Some(saves::Problem { kind: problem_kind(e).into(), text: e.to_string(), at: crate::library::now() }); + saves::set_state(dir, id, &s); +} + /// Puts one local copy up if the cloud hasn't changed since the last sync. async fn upload(app: &AppHandle, id: &str, name: &str) -> Res<()> { + let r = upload_one(app, id, name).await; + if let Err(e) = &r { + note_failure(&app.state::().dir, id, e); + } + r +} + +async fn upload_one(app: &AppHandle, id: &str, name: &str) -> Res<()> { let st = app.state::(); let mut state = saves::state(&st.dir, id); let copy = saves::manifest(&saves::dir_of(&st.dir, id).join(name)) @@ -558,7 +617,7 @@ async fn upload(app: &AppHandle, id: &str, name: &str) -> Res<()> { let (list, _) = cloud_list(app, id).await?; let Some(top) = list.first() else { return upload_copy(app, id, name, 0).await }; if top.hash == copy.hash { - state.synced_hash = copy.hash; + state.mark_synced(©.hash, top.made, &top.device); if state.pending.as_deref() == Some(name) { state.pending = None; } @@ -566,6 +625,8 @@ async fn upload(app: &AppHandle, id: &str, name: &str) -> Res<()> { return Ok(()); } if top.hash != state.synced_hash { + state.conflict = Some(top.v.clone()); + saves::set_state(&st.dir, id, &state); return Err(Error::Msg(SYNC_CONFLICT.into())); } upload_copy(app, id, name, top.made.saturating_add(1)).await @@ -590,16 +651,21 @@ async fn upload_copy(app: &AppHandle, id: &str, name: &str, earliest: u64) -> Re let body = tokio::fs::read(&path).await?; // Too big to go up: it stays on this PC, and isn't tried again (the next copy is). if body.len() > MAX_UPLOAD { + let why = "This save is too big for cloud saves (over 95 MB). It's kept on this PC."; let mut s = saves::state(&st.dir, id); if s.pending.as_deref() == Some(name) { s.pending = None; - saves::set_state(&st.dir, id, &s); } + // Kept here, not only said in a toast: with nothing queued, the game's page would + // otherwise show this copy as waiting to go up for good. + s.problem = Some(saves::Problem { kind: "too-big-copy".into(), text: why.into(), at: crate::library::now() }); + saves::set_state(&st.dir, id, &s); crate::trail::note(format!("Cloud saves: {id}'s copy is {} MB, too big to send", body.len() / (1024 * 1024))); - return Err(Error::Msg("This save is too big for cloud saves (over 95 MB). It's kept on this PC.".into())); + return Err(Error::Msg(why.into())); } let a = load(&st.dir).ok_or_else(|| Error::Msg("Sign in first.".into()))?; - let (made, played) = (man.made.max(earliest).to_string(), man.played.to_string()); + let made_at = man.made.max(earliest); + let (made, played) = (made_at.to_string(), man.played.to_string()); let size = body.len() as u64; let url = api(); let req = st @@ -614,6 +680,7 @@ async fn upload_copy(app: &AppHandle, id: &str, name: &str, earliest: u64) -> Re if !crate::net::forced() && crate::net::resting(&save_url("save-put"), true) { return Err(Error::Net("Can't reach recomplauncher.com right now.".into())); } + let _working = working(app, id, "up"); let r = match crate::net::send(req, &save_url("save-put"), true).await { Ok(r) => r, Err(e) => { @@ -637,7 +704,7 @@ async fn upload_copy(app: &AppHandle, id: &str, name: &str, earliest: u64) -> Re } copy_went_up(id, name); let mut s = saves::state(&st.dir, id); - s.synced_hash = man.hash; + s.mark_synced(&man.hash, made_at, &man.device); // The copy waiting is this one or an older one: what's in the cloud now is newer, so it // doesn't go up after it. if s.pending.as_deref().map(|p| p == name || copy_time(p) <= copy_time(name)).unwrap_or(false) { @@ -800,6 +867,7 @@ pub async fn after_session(app: &AppHandle, id: &str) -> Res async fn take_cloud(app: &AppHandle, id: &str, c: &CloudCopy) -> Res { let st = app.state::(); let a = load(&st.dir).ok_or_else(|| Error::Msg("Sign in first.".into()))?; + let _working = working(app, id, "down"); let url = api(); let req = st .http @@ -824,7 +892,7 @@ async fn take_cloud(app: &AppHandle, id: &str, c: &CloudCopy) -> Res { let _ = tokio::fs::remove_file(&tmp).await; let n = n??; let mut s = saves::state(&st.dir, id); - s.synced_hash = c.hash.clone(); + s.mark_synced(&c.hash, c.made, &c.device); s.last_hash = c.hash.clone(); saves::set_state(&st.dir, id, &s); Ok(n) @@ -863,7 +931,24 @@ async fn upload_current(app: &AppHandle, id: &str, g: Installed, spots: Vec().dir, id, e); + } + r +} + +/// A save place too big to look through stops this game's sync: kept so its page says so on +/// every launch, not only in the once-per-launch toast. Cleared by the first check that finds +/// every place readable again. +fn note_folder_too_big(dir: &Path, id: &str, why: Option) { + let mut s = saves::state(dir, id); + let was = s.problem.as_ref().is_some_and(|p| p.kind == "too-big-folder"); + if why.is_none() && !was { + return; + } + s.problem = why.map(|text| saves::Problem { kind: "too-big-folder".into(), text, at: crate::library::now() }); + saves::set_state(dir, id, &s); } /// Before Play: what to do about the cloud. @@ -888,6 +973,7 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res { } let _busy = busy().await?; let _reserved = reserve_sync(&st, &id)?; + let _working = working(&app, &id, "check"); let (list, _) = match cloud_list(&app, &id).await { Ok(x) => x, Err(Error::Net(_)) => return Ok(json!({"action": "offline"})), @@ -907,6 +993,7 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res { // A cloud copy put in place now could be older than the saves there and replace them. That // holds even when other places had saves: the copy writes into every place it holds. if !too_big.is_empty() { + note_folder_too_big(&st.dir, &id, Some(SYNC_PAUSED.into())); if manual { return Err(saves::too_big_error(&g, &spots, &too_big)); } @@ -915,6 +1002,7 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res { } return Ok(json!({"action": "play"})); } + note_folder_too_big(&st.dir, &id, None); let s = saves::state(&st.dir, &id); let Some(top) = top else { if local.is_empty() { @@ -924,9 +1012,11 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res { return Ok(json!({"action": "uploaded"})); }; if top.hash == local { - if s.synced_hash != local || s.pending.is_some() { + // Already the same: a conflict or a problem noted before is over too. + let stale = s.synced_hash != local || s.pending.is_some() || s.conflict.is_some() || s.problem.is_some(); + if stale { let mut s = s; - s.synced_hash = local; + s.mark_synced(&local, top.made, &top.device); s.pending = None; saves::set_state(&st.dir, &id, &s); } @@ -944,6 +1034,10 @@ async fn check_saves(app: AppHandle, id: String, manual: bool) -> Res { // After Play, the local changes go up at the end of the session. return Ok(json!({"action": "play"})); } + // Both changed: kept until the player picks, so the game's page can ask after this prompt. + let mut s = s; + s.conflict = Some(top.v.clone()); + saves::set_state(&st.dir, &id, &s); let newest = entries.iter().filter_map(|e| std::fs::metadata(&e.abs).ok()?.modified().ok()).max(); let when = newest.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_secs()).unwrap_or(0); Ok(json!({"action": "conflict", "local": {"when": when, "device": saves::device_name(), "played": g.played_secs, "hash": local}, "cloud": top})) @@ -1052,11 +1146,7 @@ pub async fn saves_info(app: AppHandle, id: String) -> Res { Err(_) => {} } } - // Copies on this PC that haven't gone up yet: the newest ones, back to the one that's in the cloud. - let waiting = match state.pending.as_deref() { - Some(p) => local.iter().take_while(|c| c.hash != state.synced_hash).filter(|c| c.when >= copy_time(p)).count().max(1), - None => 0, - }; + let waiting = waiting_copies(&local, &state); Ok(json!({ "spots": spots, "local": local, "cloud": cloud, "space": space, "offline": offline, "cloudOn": g.cloud, "signedIn": signed_in(&st.dir), "synced": state.synced_hash, "pending": state.pending.is_some(), @@ -1070,6 +1160,38 @@ pub async fn saves_info(app: AppHandle, id: String) -> Res { /// Where players see whether the site is having trouble on our side. const STATUS_PAGE: &str = "https://recomplauncher.com/status"; +/// Copies on this PC that haven't gone up yet: the newest ones, back to the one that's in the cloud. +fn waiting_copies(local: &[saves::Copy], s: &saves::State) -> usize { + let Some(p) = s.pending.as_deref() else { return 0 }; + local.iter().take_while(|c| c.hash != s.synced_hash).filter(|c| c.when >= copy_time(p)).count().max(1) +} + +/// Where a game's cloud saves stand, for the badge on its page. Only what this PC knows: it +/// never asks the website, so the page can ask on every draw. The save-sync event says when it +/// changes. `state` is one word (saves::State::sync_state); `problem` says why when it's stuck. +#[tauri::command] +pub async fn saves_status(app: AppHandle, id: String) -> Res { + blocking(move || sync_status(&app, &id)).await? +} + +fn sync_status(app: &AppHandle, id: &str) -> Res { + let st = app.state::(); + let g = game(&st, id)?; + let s = saves::state(&st.dir, id); + let signed = signed_in(&st.dir); + let local = saves::list(&st.dir, id); + let retry = s.pending.as_deref().and_then(|p| copy_wait_left(id, p)); + Ok(json!({ + "state": s.sync_state(g.cloud == Some(true), signed), + "cloudOn": g.cloud, "signedIn": signed, + "syncedAt": s.synced_at, "syncedMade": s.synced_made, "syncedDevice": s.synced_device, + "waiting": waiting_copies(&local, &s), "retryIn": retry, "full": full_now(), "paused": paused(), + "problem": s.problem, "conflict": s.conflict.is_some(), "newest": local.first(), + // Where the saves go. Only RecompStore today; named so another cloud can say so later. + "provider": "recompstore", + })) +} + fn upload_candidate(dir: &Path, id: &str, running: bool) -> Res> { let s = saves::state(dir, id); let latest = saves::list(dir, id).into_iter().next(); @@ -1180,6 +1302,7 @@ fn overview(app: &AppHandle) -> Value { out.push(json!({ "id": id, "copies": local.len(), "size": size, "newest": local.first(), "cloudOn": g.cloud, "synced": state.synced_hash, "pending": state.pending.is_some(), "found": found, + "state": state.sync_state(g.cloud == Some(true), signed), })); } let chosen = saves::chosen_root(); @@ -1769,3 +1892,80 @@ mod sync_tests { std::fs::remove_dir_all(dir).unwrap(); } } + +#[cfg(test)] +mod status_tests { + use super::*; + + fn tmp(n: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("rl-cloud-status-{n}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d + } + + #[test] + fn a_full_cloud_is_its_own_problem() { + assert_eq!(problem_kind(&Error::Msg(FULL_TEXT.into())), "full"); + } + + #[test] + fn a_copy_over_the_limit_is_too_big() { + let e = Error::Msg("This save is too big for cloud saves (over 95 MB). It's kept on this PC.".into()); + assert_eq!(problem_kind(&e), "too-big-copy"); + } + + #[test] + fn no_connection_is_offline() { + assert_eq!(problem_kind(&Error::Net("Can't reach recomplauncher.com right now.".into())), "offline"); + } + + #[test] + fn any_other_refusal_is_an_error() { + assert_eq!(problem_kind(&Error::Msg("That copy is gone.".into())), "error"); + } + + #[test] + fn a_failed_upload_is_remembered() { + let d = tmp("failed"); + note_failure(&d, "g", &Error::Net("Can't reach recomplauncher.com right now.".into())); + let p = saves::state(&d, "g").problem.unwrap(); + assert_eq!(p.kind, "offline"); + assert_eq!(p.text, "Can't reach recomplauncher.com right now."); + std::fs::remove_dir_all(d).unwrap(); + } + + #[test] + fn a_conflict_is_not_recorded_as_a_problem() { + let d = tmp("conflict"); + note_failure(&d, "g", &Error::Msg(SYNC_CONFLICT.into())); + assert!(saves::state(&d, "g").problem.is_none()); + std::fs::remove_dir_all(d).unwrap(); + } + + #[test] + fn a_readable_folder_again_clears_the_folder_problem() { + let d = tmp("folder"); + note_folder_too_big(&d, "g", Some(SYNC_PAUSED.into())); + assert_eq!(saves::state(&d, "g").problem.unwrap().kind, "too-big-folder"); + note_folder_too_big(&d, "g", None); + assert!(saves::state(&d, "g").problem.is_none()); + std::fs::remove_dir_all(d).unwrap(); + } + + #[test] + fn a_readable_folder_keeps_other_problems() { + let d = tmp("folder-other"); + note_failure(&d, "g", &Error::Msg(FULL_TEXT.into())); + note_folder_too_big(&d, "g", None); + assert_eq!(saves::state(&d, "g").problem.unwrap().kind, "full"); + std::fs::remove_dir_all(d).unwrap(); + } + + #[test] + fn nothing_waits_without_a_queued_copy() { + let local = vec![saves::Copy { name: "200.zip".into(), when: 200, size: 1, files: 1, hash: "b".into(), device: String::new(), played: 0 }]; + let s = saves::State { synced_hash: "a".into(), ..Default::default() }; + assert_eq!(waiting_copies(&local, &s), 0); + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index bc2d1a81..1e6939af 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -9867,7 +9867,7 @@ pub fn run() { convert_copy, convert_cancel, looks_info, set_look, look_picture, ui_data, reset_settings, tray::tray_words, tray::quit_launcher, ach::ach_access, ach::ach_status, ach::ach_apply, ach::ach_profile, ach::ach_games, ach::ach_board, ach::ach_game_web, ach::ach_report, ach::ach_tracker_get, ach::ach_game, ach::ach_set_disc, ach::ach_set_folder, ach::ach_sound, ach::ach_log, ach::ach_peek, ach::ach_rule, ach::ach_reset, ach::ach_test_toast, ach::ach_toast_take, ach::ach_toast_done, cloud::account_start, cloud::account_poll, cloud::account_cancel, cloud::account_state, cloud::account_signout, - cloud::saves_check, cloud::saves_pick, cloud::saves_cloud, download_history, report_counts, play_reports, cloud::saves_info, cloud::saves_upload_now, cloud::cloud_retry, cloud::saves_overview, cloud::saves_backup, cloud::saves_restore, cloud::saves_share, cloud::saves_forget_found, cloud::saves_set_place, cloud::saves_add_place, cloud::saves_remove_place, cloud::saves_open, saves_set_root, saves_open_root, missing_files, missing_made, dll_check, dll_aside, gpu_choice, prefer_fast_gpu, copy_check, wine_tools, deck_scores, latest_counts, team_notes, trending, ig_info, ig_view, usage::notifies, usage::notify, usage::pc_specs, usage::recomp_request, + cloud::saves_check, cloud::saves_pick, cloud::saves_cloud, download_history, report_counts, play_reports, cloud::saves_info, cloud::saves_status, cloud::saves_upload_now, cloud::cloud_retry, cloud::saves_overview, cloud::saves_backup, cloud::saves_restore, cloud::saves_share, cloud::saves_forget_found, cloud::saves_set_place, cloud::saves_add_place, cloud::saves_remove_place, cloud::saves_open, saves_set_root, saves_open_root, missing_files, missing_made, dll_check, dll_aside, gpu_choice, prefer_fast_gpu, copy_check, wine_tools, deck_scores, latest_counts, team_notes, trending, ig_info, ig_view, usage::notifies, usage::notify, usage::pc_specs, usage::recomp_request, adopt::adopt_scan, adopt::migrate_scan, adopt::migrate_pick, adopt::adopt_pick, adopt::adopt_add, adopt::adopt_check, adopt::set_project, usage::usage_hello, usage::usage_event, usage::data_ids, usage::reset_ids, ping::usage_open, ping::usage_mode, usage::run_report, cloud::inbox, cloud::inbox_done, cloud::betas, cloud::beta_join, cloud::beta_leave, launcher_notes, scan_info, devtools::dev_add, devtools::add_any, devtools::dev_check, devtools::dev_starter, fs_places, fs_list, fs_extract, gb_mods, gb_mod, gb_install ]) .build(tauri::generate_context!()) diff --git a/src-tauri/src/saves.rs b/src-tauri/src/saves.rs index c9795948..1c4910b5 100644 --- a/src-tauri/src/saves.rs +++ b/src-tauri/src/saves.rs @@ -622,6 +622,76 @@ pub struct State { /// A copy that still has to go up (the upload failed or the PC was offline). #[serde(default)] pub pending: Option, + /// When this PC and the cloud last agreed, and when and where that save was made. Kept here + /// so the game page can say "in the cloud since 14:02" without asking the website. + #[serde(default)] + pub synced_at: u64, + #[serde(default)] + pub synced_made: u64, + #[serde(default, skip_serializing_if = "String::is_empty")] + pub synced_device: String, + /// The cloud copy that changed while this PC's save changed too. Kept until the player + /// picks one, because the toast that says so is gone after a few seconds. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub conflict: Option, + /// Why the last sync didn't go through. Kept until one does. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub problem: Option, +} + +/// Why a game's saves didn't reach the cloud. `kind` is one of: offline, full, paused, +/// too-big-copy, too-big-folder, error. +#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)] +pub struct Problem { + pub kind: String, + pub text: String, + pub at: u64, +} + +impl State { + /// This PC and the cloud hold the same save now: whatever was in the way is over. + pub fn mark_synced(&mut self, hash: &str, made: u64, device: &str) { + self.synced_hash = hash.to_string(); + self.synced_at = crate::library::now(); + self.synced_made = made; + self.synced_device = device.to_string(); + self.conflict = None; + self.problem = None; + } + + /// The newest copy on this PC isn't the one in the cloud: it's queued, or it goes up after + /// the next session. A copy put back from the cloud sets both hashes, so it isn't behind. + pub fn behind(&self) -> bool { + self.pending.is_some() || (!self.last_hash.is_empty() && self.last_hash != self.synced_hash) + } + + /// One word for where this game's saves stand, from what this PC knows (no website asked): + /// signed-out, off, conflict, problem, waiting, none or up-to-date. Live work (checking, + /// uploading, downloading) comes from the save-sync event instead. + pub fn sync_state(&self, cloud_on: bool, signed_in: bool) -> &'static str { + if !signed_in { + return "signed-out"; + } + if !cloud_on { + return "off"; + } + if self.conflict.is_some() { + return "conflict"; + } + let kind = self.problem.as_ref().map(|p| p.kind.as_str()).unwrap_or(""); + // A folder too big to look through stops every sync, whether or not a copy waits. + if kind == "too-big-folder" { + return "problem"; + } + if !self.behind() { + return if self.synced_hash.is_empty() && self.last_hash.is_empty() { "none" } else { "up-to-date" }; + } + // No connection is only waiting: it goes up by itself once the website answers. + if kind.is_empty() || kind == "offline" { + return "waiting"; + } + "problem" + } } /// This PC's notes about a game's saves stay in the launcher's own folder, even with the copies @@ -1670,7 +1740,7 @@ mod tests { assert!(queued.when > chosen.when); let queued_path = dir_of(&app, "g").join(&queued.name); let queued_bytes = fs::read(&queued_path).unwrap(); - set_state(&app, "g", &State { last_hash: queued.hash.clone(), synced_hash: "previous cloud".into(), pending: Some(queued.name.clone()) }); + set_state(&app, "g", &State { last_hash: queued.hash.clone(), synced_hash: "previous cloud".into(), pending: Some(queued.name.clone()), ..Default::default() }); set_state(&app, "other", &State { pending: Some("123.zip".into()), ..Default::default() }); restore(&app, "g", &dir_of(&app, "g").join(&chosen.name), &spots, &g).unwrap(); @@ -2047,3 +2117,101 @@ mod tests { } } } + +#[cfg(test)] +mod sync_state_tests { + use super::{Problem, State}; + + fn problem(kind: &str) -> Option { + Some(Problem { kind: kind.into(), text: String::new(), at: 1 }) + } + + #[test] + fn signed_out_comes_first() { + let s = State { last_hash: "a".into(), synced_hash: "a".into(), ..Default::default() }; + assert_eq!(s.sync_state(true, false), "signed-out"); + } + + #[test] + fn cloud_saves_off_is_off() { + let s = State { last_hash: "a".into(), ..Default::default() }; + assert_eq!(s.sync_state(false, true), "off"); + } + + #[test] + fn nothing_backed_up_or_synced_is_none() { + assert_eq!(State::default().sync_state(true, true), "none"); + } + + #[test] + fn the_same_save_here_and_in_the_cloud_is_up_to_date() { + let s = State { last_hash: "a".into(), synced_hash: "a".into(), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "up-to-date"); + } + + #[test] + fn a_newer_copy_here_is_waiting() { + let s = State { last_hash: "b".into(), synced_hash: "a".into(), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "waiting"); + } + + #[test] + fn a_queued_copy_is_waiting() { + let s = State { last_hash: "a".into(), synced_hash: "a".into(), pending: Some("1.zip".into()), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "waiting"); + } + + #[test] + fn no_connection_is_still_only_waiting() { + let s = State { last_hash: "b".into(), synced_hash: "a".into(), problem: problem("offline"), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "waiting"); + } + + #[test] + fn a_copy_too_big_to_send_is_a_problem_not_waiting() { + // Its upload clears pending without moving synced_hash; without the stored problem the + // page would say it's waiting to go up for good. + let s = State { last_hash: "big".into(), synced_hash: "a".into(), problem: problem("too-big-copy"), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "problem"); + } + + #[test] + fn a_folder_too_big_to_read_is_a_problem_even_when_nothing_waits() { + let s = State { last_hash: "a".into(), synced_hash: "a".into(), problem: problem("too-big-folder"), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "problem"); + } + + #[test] + fn a_problem_left_from_before_an_upload_does_not_show_once_synced() { + let s = State { last_hash: "a".into(), synced_hash: "a".into(), problem: problem("full"), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "up-to-date"); + } + + #[test] + fn a_conflict_shows_over_everything_but_off_and_signed_out() { + let s = State { last_hash: "b".into(), synced_hash: "a".into(), conflict: Some("v9".into()), problem: problem("full"), ..Default::default() }; + assert_eq!(s.sync_state(true, true), "conflict"); + } + + #[test] + fn syncing_clears_a_conflict_and_a_problem() { + let mut s = State { conflict: Some("v9".into()), problem: problem("full"), ..Default::default() }; + s.mark_synced("a", 100, "Steam Deck"); + assert!(s.conflict.is_none()); + assert!(s.problem.is_none()); + } + + #[test] + fn syncing_remembers_when_and_where_the_save_was_made() { + let mut s = State::default(); + s.mark_synced("a", 100, "Steam Deck"); + assert_eq!((s.synced_hash.as_str(), s.synced_made, s.synced_device.as_str()), ("a", 100, "Steam Deck")); + assert!(s.synced_at > 0); + } + + #[test] + fn a_state_file_from_before_reads_with_the_new_fields_empty() { + let s: State = serde_json::from_str(r#"{"last_hash":"a","synced_hash":"a","pending":null}"#).unwrap(); + assert_eq!((s.synced_at, s.conflict.is_none(), s.problem.is_none()), (0, true, true)); + } +} diff --git a/ui/games.js b/ui/games.js index 2ebade1e..83a7f67b 100644 --- a/ui/games.js +++ b/ui/games.js @@ -176,7 +176,7 @@ function playtimeRows(a, total) { // folder, settings): those get the recomp's id. Playing it, its setup, shortcuts and achievements // go by the game's own id, and so do its saves when it keeps its own. const GAME_CMDS = new Set(['play', 'game_info', 'game_setup', 'setup_inputs_run', 'desktop_shortcut', 'add_to_steam', 'ach_game', 'ach_set_disc', 'ach_set_folder']); -const SAVE_CMDS = new Set(['saves_check', 'saves_pick', 'saves_info', 'saves_upload_now', 'saves_backup', 'saves_restore', 'saves_open', 'saves_open_backups']); +const SAVE_CMDS = new Set(['saves_check', 'saves_pick', 'saves_info', 'saves_status', 'saves_upload_now', 'saves_backup', 'saves_restore', 'saves_open', 'saves_open_backups']); function gameArgs(cmd, args) { if (!args || typeof args.id !== 'string') return args; const s = subOf(args.id);