mod common;
use common::{bare_remote, clone, commit_all, dt, git, press, type_str};
use crossterm::event::KeyCode;
use tasu::app::{Model, Runtime, update};
use tasu::command;
use tasu::config::Config;
use tasu::domain::Board;
use tasu::store::Store;
use tasu::sync;
#[test]
fn cli_add_syncs_to_a_remote_and_back() {
let dir = tempfile::tempdir().unwrap();
let first = dir.path().join("first");
std::fs::create_dir_all(&first).unwrap();
let remote = bare_remote(dir.path());
let config = Config {
data_dir: first,
remote: Some(remote.to_string_lossy().into_owned()),
};
command::add(&config, &["Nand2Tetris".to_string()]).unwrap();
let second = dir.path().join("second");
clone(&remote, &second);
let board = Store::new(second.join("todos.json")).load().board;
assert!(
board.tasks().iter().any(|task| task.title == "Nand2Tetris"),
"remote board missing the task"
);
}
#[test]
fn cli_add_converges_across_two_machines() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let url = remote.to_string_lossy().into_owned();
let machine_a = dir.path().join("a");
let machine_b = dir.path().join("b");
let config_a = Config {
data_dir: machine_a.clone(),
remote: Some(url.clone()),
};
let config_b = Config {
data_dir: machine_b.clone(),
remote: Some(url.clone()),
};
command::add(&config_a, &["from A".to_string()]).unwrap();
command::add(&config_b, &["from B".to_string()]).unwrap();
command::add(&config_a, &["back on A".to_string()]).unwrap();
let board = Store::new(machine_a.join("todos.json")).load().board;
let titles: Vec<&str> = board
.tasks()
.iter()
.map(|task| task.title.as_str())
.collect();
assert!(titles.contains(&"from A"));
assert!(titles.contains(&"from B"), "A did not pull B's task");
assert!(titles.contains(&"back on A"));
}
#[test]
fn a_pull_in_flight_does_not_lose_a_task_added_meanwhile() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let url = remote.to_string_lossy().into_owned();
let seed = dir.path().join("seed");
std::fs::create_dir_all(&seed).unwrap();
let mut base = Board::new();
base.add("base", dt(5));
Store::new(seed.join("todos.json")).save(&base).unwrap();
git(&seed, &["init", "-q", "-b", "main"]);
commit_all(&seed, "base");
git(&seed, &["remote", "add", "origin", &url]);
git(&seed, &["push", "-q", "origin", "main"]);
let a = dir.path().join("a");
clone(&remote, &a);
let mut board_a = Store::new(a.join("todos.json")).load().board;
board_a.add("local only", dt(6));
Store::new(a.join("todos.json")).save(&board_a).unwrap();
commit_all(&a, "a");
let b = dir.path().join("b");
clone(&remote, &b);
let mut board_b = Store::new(b.join("todos.json")).load().board;
board_b.add("from B", dt(7));
Store::new(b.join("todos.json")).save(&board_b).unwrap();
commit_all(&b, "b");
git(&b, &["push", "-q", "origin", "main"]);
let config = Config {
data_dir: a.clone(),
remote: Some(url),
};
let mut runtime = Runtime::new(&config);
let now = dt(8);
let mut model = Model::new(runtime.load_board(now).board, now);
runtime.start_sync();
update(&mut model, press(KeyCode::Char('a')), now);
type_str(&mut model, "urgent", now);
let effects = update(&mut model, press(KeyCode::Enter), now);
runtime.apply(&mut model, effects);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
while !model
.board
.tasks()
.iter()
.any(|task| task.title == "from B")
{
for action in runtime.poll_external() {
let effects = update(&mut model, action, now);
runtime.apply(&mut model, effects);
}
assert!(
std::time::Instant::now() < deadline,
"the pull never landed"
);
std::thread::sleep(std::time::Duration::from_millis(20));
}
let titles: Vec<String> = model
.board
.tasks()
.iter()
.map(|task| task.title.clone())
.collect();
for wanted in ["base", "local only", "urgent", "from B"] {
assert!(
titles.iter().any(|title| title == wanted),
"lost {wanted:?}: {titles:?}"
);
}
let disk: Vec<String> = Store::new(a.join("todos.json"))
.load()
.board
.tasks()
.iter()
.map(|task| task.title.clone())
.collect();
for wanted in ["local only", "urgent", "from B"] {
assert!(
disk.iter().any(|title| title == wanted),
"not on disk {wanted:?}: {disk:?}"
);
}
}
#[test]
fn a_later_rename_survives_a_diverged_sync() {
let dir = tempfile::tempdir().unwrap();
let remote = bare_remote(dir.path());
let url = remote.to_string_lossy().into_owned();
let a = dir.path().join("a");
let b = dir.path().join("b");
std::fs::create_dir_all(&a).unwrap();
let mut board_a = Board::new();
board_a.add("shared", dt(5));
Store::new(a.join("todos.json")).save(&board_a).unwrap();
sync::commit_now(&a, Some(&url)).unwrap();
let _ = sync::pull_now(&b, Some(&url)).unwrap();
let mut board_b = Store::new(b.join("todos.json")).load().board;
assert_eq!(board_b.tasks().len(), 1, "B did not receive the task");
board_a.rename(0, "named on A", dt(6));
Store::new(a.join("todos.json")).save(&board_a).unwrap();
sync::commit_now(&a, Some(&url)).unwrap();
board_b.rename(0, "named on B", dt(7));
Store::new(b.join("todos.json")).save(&board_b).unwrap();
let _ = sync::pull_now(&b, Some(&url)).unwrap();
sync::commit_now(&b, Some(&url)).unwrap();
let merged = Store::new(b.join("todos.json")).load().board;
assert_eq!(
merged.task(0).unwrap().title,
"named on B",
"the later rename must survive the diverged merge"
);
}