//! Restore an earlier day: what the core says of the server's answers. Against the real Worker: `restore_e2e.rs`. use serde_json::json; use super::*; /// RULE: every line this module says is in the kit's restore set (a missing one would say nothing on a screen). #[test] fn every_line_the_restore_methods_say_is_in_the_kit() { let source = include_str!("restore.rs"); let mut said = 0; for piece in source.split("say(\"").skip(1) { let line = piece.split('"').next().unwrap_or_default(); if line.contains('.') { let fills: &[(&str, &str)] = if line.ends_with("older.line") || line.ends_with("after.kept") { &[("n", "2")] } else { &[] }; assert!(cotton_core::rules::shell_lines::resolve("restore", line, &[], fills).is_some_and(|w| !w.is_empty()), "restore.{line} is not in kits/shell/lines/restore.json"); said += 1; } } assert!(said > 20, "the lines were read ({said})"); } /// RULE: where a restore stands is said in its order, with the day in the person's zone, and only a restore still /// checking may be stopped. #[test] fn progress_is_said_in_its_order_with_the_day_in_the_persons_zone() { let checking = json!({"stage": "checking", "may_cancel": true, "restore": {"to": "2026-11-20T18:30:00.000Z", "parts": 4, "checked": 1, "laid": 0, "done": 1, "total": 8}}); let said = progress_of(&checking, "Asia/Kolkata", true); assert_eq!(said["line"], json!("Checking the copy of Saturday 21 November… 1 of 4"), "{said}"); assert_eq!((said["mayCancel"].clone(), said["stop"].clone(), said["underWay"].clone()), (json!(true), json!("Stop"), json!(true))); assert_eq!(said["fraction"], json!(0.125)); let laying = json!({"stage": "laying", "may_cancel": false, "restore": {"to": "2026-11-20T18:30:00.000Z", "parts": 4, "checked": 4, "laid": 2, "done": 6, "total": 8}}); let said = progress_of(&laying, "Asia/Kolkata", true); assert_eq!(said["line"], json!("Putting your Cotton back to Saturday 21 November… 2 of 4")); assert_eq!((said["mayCancel"].clone(), said["stop"].clone()), (json!(false), json!(null))); let finished = json!({"stage": "none", "finished": true, "restore": null, "restored_to": "2026-11-20T18:30:00.000Z"}); let said = progress_of(&finished, "Asia/Kolkata", true); assert_eq!((said["stage"].clone(), said["fraction"].clone(), said["underWay"].clone()), (json!("done"), json!(1.0), json!(false))); assert_eq!(said["line"], json!("Your Cotton is back to how it was on Saturday 21 November. Bringing it to this phone…")); let idle = progress_of(&json!({"stage": "none", "restore": null}), "Asia/Kolkata", false); assert_eq!((idle["stage"].clone(), idle["line"].clone()), (json!("none"), json!(null))); let held = progress_of(&json!({"stage": "held", "restore": null}), "Asia/Kolkata", false); assert_eq!(held["line"], json!(say("progress.held", &[]))); } /// RULE: the server's refusals reach the shell as the set's words under a stable code; the off switch (404) is /// `unavailable`, an unanswered call `unreached`, never a fault. #[test] fn refusals_are_said_in_the_sets_words_under_a_stable_code() { let refusal = |status: u16, code: &str| ObjectRefusal { status, code: code.into(), message: "the server's own words".into() }; for (status, code, want, line) in [ (404, "not_found", "unavailable", "page.unavailable"), (503, "account_frozen", "unreached", "refused.unreached"), (0, "offline", "unreached", "refused.unreached"), (403, "owner_device_only", "owner_device_only", "refused.owner_only"), (409, "account_deleting", "account_deleting", "refused.deleting"), (401, "credential_revoked", "account_deleting", "refused.deleting"), (409, "restore_under_way", "restore_under_way", "refused.under_way"), (404, "no_such_point", "no_such_point", "refused.gone"), (409, "restore_laying", "restore_laying", "progress.too_far"), (409, "manifest_refused", "not_now", "refused.not_now"), ] { let said = said(refusal(status, code)); assert_eq!((said.code.as_str(), said.message.as_str()), (want, say(line, &[]).as_str()), "{status} {code}"); } }