#![cfg(feature = "server-local")]
use chrono::Utc;
use pretty_assertions::assert_eq;
use taskchampion::storage::inmemory::InMemoryStorage;
use taskchampion::{Operations, Replica, ServerConfig, Status, Uuid};
use tempfile::TempDir;
#[tokio::test]
async fn cross_sync() -> anyhow::Result<()> {
let mut rep1 = Replica::new(InMemoryStorage::new());
let mut rep2 = Replica::new(InMemoryStorage::new());
let tmp_dir = TempDir::new().expect("TempDir failed");
let server_config = ServerConfig::Local {
server_dir: tmp_dir.path().to_path_buf(),
};
let mut server = server_config.into_server().await?;
let (uuid1, uuid2) = (Uuid::new_v4(), Uuid::new_v4());
let mut ops = Operations::new();
let mut t1 = rep1.create_task(uuid1, &mut ops).await?;
t1.set_description("test 1".into(), &mut ops)?;
t1.set_status(Status::Pending, &mut ops)?;
t1.set_entry(Some(Utc::now()), &mut ops)?;
let mut t2 = rep1.create_task(uuid2, &mut ops).await?;
t2.set_description("test 2".into(), &mut ops)?;
t2.set_status(Status::Pending, &mut ops)?;
t2.set_entry(Some(Utc::now()), &mut ops)?;
t1.start(&mut ops)?;
rep1.commit_operations(ops).await?;
rep1.sync(&mut server, false).await?;
rep2.sync(&mut server, false).await?;
let mut t12 = rep2
.get_task(uuid1)
.await?
.expect("expected task 1 on rep2");
let t22 = rep2
.get_task(uuid2)
.await?
.expect("expected task 2 on rep2");
assert_eq!(t12.get_description(), "test 1");
assert_eq!(t12.is_active(), true);
assert_eq!(t22.get_description(), "test 2");
assert_eq!(t22.is_active(), false);
let mut ops = Operations::new();
t2.set_status(Status::Completed, &mut ops)?;
rep1.commit_operations(ops).await?;
let mut ops = Operations::new();
t12.set_status(Status::Completed, &mut ops)?;
rep2.commit_operations(ops).await?;
rep1.sync(&mut server, false).await?; rep2.sync(&mut server, false).await?; rep1.sync(&mut server, false).await?;
let t1 = rep1
.get_task(uuid1)
.await?
.expect("expected task 1 on rep1");
assert_eq!(t1.get_status(), Status::Completed);
let mut t22 = rep2
.get_task(uuid2)
.await?
.expect("expected task 2 on rep2");
assert_eq!(t22.get_status(), Status::Completed);
rep1.rebuild_working_set(true).await?;
rep2.rebuild_working_set(true).await?;
let mut ops = Operations::new();
t22.set_status(Status::Pending, &mut ops)?;
rep2.commit_operations(ops).await?;
let ws = rep2.working_set().await?;
assert_eq!(ws.by_index(1), Some(uuid2));
let ws = rep1.working_set().await?;
assert_eq!(ws.by_index(1), None);
rep2.sync(&mut server, false).await?;
rep1.sync(&mut server, false).await?;
let ws = rep1.working_set().await?;
assert_eq!(ws.by_index(1), Some(uuid2));
Ok(())
}