//! UPGRADE PROOF, the HEAD side: the copy an old build left (`upgrade_writer.rs`, run at the old sha) opened by this //! core, then passes. Nothing lost, nothing erased, the queue intact through the migration, then delivered once, the //! refused change still parked and said, and the copy's schema at this build's. The object's own count of versions //! (nothing sent twice) is checked by the driver, `apps/business/scripts/__tests__/upgrade-proof.ts`. use serde_json::{json, Value}; use super::upgrade_writer::{ask, copy_state, given, level, open_words}; fn strings(value: &Value) -> Vec { value.as_array().map(|items| items.iter().filter_map(Value::as_str).map(str::to_string).collect()).unwrap_or_default() } #[test] #[ignore = "run by apps/business/scripts/__tests__/upgrade-proof.ts after the old side"] fn upgrade_head_side_opens_the_old_copy() { let manifest: Value = serde_json::from_slice(&std::fs::read(given("UPGRADE_MANIFEST")).expect("the old side's manifest")).expect("JSON"); let before = &manifest["state"]; let handle = open_words(&given("UPGRADE_CONTAINER")); let connect = |handle: &crate::handle::CottonHandle| { ask(handle, "personal.connect", json!({ "server": given("COTTON_E2E_BASE"), "databaseId": given("COTTON_E2E_DATABASE"), "credential": given("COTTON_E2E_CREDENTIAL") })); }; // A copy another schema stamped is never opened before the launch connects (`shell::client::ANOTHER_BUILDS_COPY`): // `personal.connect` moves it in place when the move only adds, else sends its unsent writes and makes it again. // So for such a build the copy is held to the same things after the connect, before any pass. let refused_at_open = handle.client().with_writer(|_| Ok(())).is_err(); if refused_at_open { println!("UPGRADE-OPENED the copy (schema {}) is refused at open, as designed; checked after personal.connect", before["user_version"]); connect(&handle); } // Opened, before any pass: the migration kept every note and the whole queue and every park. let opened = copy_state(&handle); println!("UPGRADE-OPENED before {} now {}", before["op_tables"], opened["op_tables"]); assert_eq!(opened["notes"], before["notes"], "every note as the old build left it"); assert_eq!(opened["outbox_all"], before["outbox_all"], "the queue intact through the migration"); assert_eq!(opened["parked_push"], before["parked_push"], "the refused change still parked"); assert_eq!(opened["parked_pull"], before["parked_pull"], "the pull's parks kept"); assert_eq!(opened["held"], before["held"], "the writes held for the server's time kept"); for (table, count) in before["tables"].as_object().expect("the old tables") { let now = opened["tables"][table].as_i64(); assert!(now.is_some_and(|now| now >= count.as_i64().unwrap_or(0)), "{table}: {count} before, {now:?} now: nothing erased"); } let schema = opened["user_version"].as_i64().unwrap_or(-1); assert!(schema >= before["user_version"].as_i64().unwrap_or(-1), "the schema moved only forward"); assert_eq!(schema, i64::from(cotton_store::migrate::SCHEMA_VERSION), "the copy migrated to this build's schema at open (or at the connect)"); // The launch connects its copy again, as the app does at every launch (BusinessClient.swift `personal.connect`). if !refused_at_open { connect(&handle); } ask(&handle, "seal.useKey", json!({ "key": manifest["key"] })); level(&handle); level(&handle); let after = copy_state(&handle); assert_eq!(after["notes"], before["notes"], "every note as the person left it, after passes"); let queued = strings(&before["outbox"]); let still: Vec = strings(&after["outbox_all"]).into_iter().filter(|op| queued.iter().any(|queued| op.starts_with(queued.as_str()))).collect(); assert!(still.is_empty(), "the queued changes went: {still:?} still wait"); assert_eq!(after["held"].as_i64(), Some(0), "the held writes were released and went"); let notes_waiting: Vec = strings(&after["outbox_all"]).into_iter().filter(|op| op.contains(" cotton_notes ")).collect(); assert!(notes_waiting.is_empty(), "no note change waits: {notes_waiting:?}"); assert_eq!(after["parked_push"], before["parked_push"], "the refused change stays parked, never sent nor dropped"); let stuck = ask(&handle, "sync.stuck", json!({})); assert_eq!(stuck["stuck"]["things"].as_u64(), Some(1), "the refused note is said, once: {stuck}"); // A second phone at HEAD reads what the object holds: the synced notes, the edit, the offline note; never the refused one. let fresh = tempfile::tempdir().expect("a second phone"); let other = open_words(&fresh.path().to_string_lossy()); ask(&other, "personal.connect", json!({ "server": given("COTTON_E2E_BASE"), "databaseId": given("COTTON_E2E_DATABASE"), "credential": given("COTTON_E2E_CREDENTIAL") })); ask(&other, "seal.useKey", json!({ "key": manifest["key"] })); level(&other); level(&other); let held = copy_state(&other)["notes"].clone(); let parked = manifest["parked"].as_str().expect("the parked note"); for (id, text) in before["notes"].as_object().expect("the notes") { if id == parked { assert_eq!(held.get(id), Some(&manifest["parked_on_object"]), "the refused edit never reached the object"); } else { assert_eq!(held.get(id), Some(text), "the object holds {id} as the phone left it"); } } println!("UPGRADE-HEAD {}", json!({ "schema_before": before["user_version"], "schema_after": schema, "refused_at_open": refused_at_open, "notes": before["notes"].as_object().map(|n| n.len()), "queued": queued.len() })); }