Milestone 1: sync engine and lunesync CLI
Rust workspace with core (watch, settle, save, sync over a plain Git remote, keep-both on same-line edits, offline queueing, tidy) and the lunesync CLI (device, add, run, service, status, checkpoint, tidy, history). Tests cover byte-exact round trips, keep-both, offline catch-up and adopting an existing vault. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
363
core/tests/sync.rs
Normal file
363
core/tests/sync.rs
Normal file
@@ -0,0 +1,363 @@
|
||||
//! Sync correctness: nothing lost, clashes keep both, bytes round-trip exactly.
|
||||
|
||||
use git2::Repository;
|
||||
use lune_core::tidy::{self, Keep};
|
||||
use lune_core::vault::{self, Added, Vault};
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tempfile::TempDir;
|
||||
|
||||
struct World {
|
||||
tmp: TempDir,
|
||||
remote: PathBuf,
|
||||
}
|
||||
|
||||
impl World {
|
||||
fn new() -> World {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let remote = tmp.path().join("remote.git");
|
||||
Repository::init_bare(&remote).unwrap();
|
||||
World { tmp, remote }
|
||||
}
|
||||
|
||||
fn folder(&self, name: &str) -> PathBuf {
|
||||
self.tmp.path().join(name)
|
||||
}
|
||||
|
||||
fn device(&self, label: &str) -> Vault {
|
||||
self.device_at(label, &self.folder(label))
|
||||
}
|
||||
|
||||
fn device_at(&self, label: &str, folder: &Path) -> Vault {
|
||||
let (cfg, _, err) = vault::add("Test", folder, self.remote.to_str().unwrap(), label).unwrap();
|
||||
assert_eq!(err, None);
|
||||
Vault::open(&cfg, label).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn write(v: &Vault, rel: &str, bytes: impl AsRef<[u8]>) {
|
||||
let p = v.root.join(rel);
|
||||
fs::create_dir_all(p.parent().unwrap()).unwrap();
|
||||
fs::write(p, bytes).unwrap();
|
||||
}
|
||||
|
||||
fn read(v: &Vault, rel: &str) -> Vec<u8> {
|
||||
fs::read(v.root.join(rel)).unwrap_or_else(|e| panic!("{rel}: {e}"))
|
||||
}
|
||||
|
||||
fn save_and_sync(v: &Vault) -> vault::SyncReport {
|
||||
v.save_changes(None, &HashSet::new()).unwrap();
|
||||
v.sync().unwrap()
|
||||
}
|
||||
|
||||
/// Every file under `root` (outside .git) with its bytes.
|
||||
fn snapshot(root: &Path) -> BTreeMap<String, Vec<u8>> {
|
||||
fn walk(root: &Path, dir: &Path, out: &mut BTreeMap<String, Vec<u8>>) {
|
||||
for e in fs::read_dir(dir).unwrap() {
|
||||
let p = e.unwrap().path();
|
||||
let rel = p.strip_prefix(root).unwrap().to_string_lossy().into_owned();
|
||||
if rel == ".git" {
|
||||
continue;
|
||||
}
|
||||
if p.is_dir() {
|
||||
walk(root, &p, out);
|
||||
} else {
|
||||
out.insert(rel, fs::read(&p).unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = BTreeMap::new();
|
||||
walk(root, root, &mut out);
|
||||
out
|
||||
}
|
||||
|
||||
fn ten_lines() -> String {
|
||||
(1..=10).map(|i| format!("line {i}\n")).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_round_trip_exactly_and_ignored_files_stay_home() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
let binary: Vec<u8> = (0..=255u8).cycle().take(5000).collect();
|
||||
write(&a, "vm-list.md", "web-1\r\nweb-2\r\n");
|
||||
write(&a, "no-newline.md", "end");
|
||||
write(&a, "attachments/diagram.png", &binary);
|
||||
write(&a, "Ünïcode folder/note.md", "héllo\n");
|
||||
write(&a, ".obsidian/app.json", "{}");
|
||||
write(&a, ".obsidian/workspace.json", "{\"open\":1}");
|
||||
write(&a, ".DS_Store", "junk");
|
||||
write(&a, ".trash/old.md", "gone");
|
||||
save_and_sync(&a);
|
||||
|
||||
let b = w.device("Hermes");
|
||||
assert_eq!(read(&b, "vm-list.md"), b"web-1\r\nweb-2\r\n");
|
||||
assert_eq!(read(&b, "no-newline.md"), b"end");
|
||||
assert_eq!(read(&b, "attachments/diagram.png"), binary);
|
||||
assert_eq!(read(&b, "Ünïcode folder/note.md"), "héllo\n".as_bytes());
|
||||
assert_eq!(read(&b, ".obsidian/app.json"), b"{}");
|
||||
for ignored in [".obsidian/workspace.json", ".DS_Store", ".trash/old.md"] {
|
||||
assert!(!b.root.join(ignored).exists(), "{ignored} should not sync");
|
||||
}
|
||||
|
||||
// And back again, with an edit from the other side.
|
||||
write(&b, "vm-list.md", "web-1\r\nweb-2\r\nweb-3\r\n");
|
||||
let report = save_and_sync(&b);
|
||||
assert_eq!(report.shared, 1);
|
||||
let report = a.sync().unwrap();
|
||||
assert_eq!(report.received, vec!["vm-list.md"]);
|
||||
assert!(report.from.contains("Hermes"));
|
||||
assert_eq!(read(&a, "vm-list.md"), b"web-1\r\nweb-2\r\nweb-3\r\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_burst_is_one_history_entry_labeled_with_the_device() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
let b = w.device("Hermes");
|
||||
for i in 0..14 {
|
||||
write(&b, &format!("notes/{i}.md"), format!("note {i}\n"));
|
||||
}
|
||||
let saved = b.save_changes(None, &HashSet::new()).unwrap().unwrap();
|
||||
assert_eq!(saved.files.len(), 14);
|
||||
b.sync().unwrap();
|
||||
a.sync().unwrap();
|
||||
let head = a.head_commit().unwrap().unwrap();
|
||||
assert_eq!(head.author().name().unwrap(), "Hermes");
|
||||
assert_eq!(head.summary().unwrap().unwrap(), "Updated 14 notes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsettled_files_wait_for_the_next_burst() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "done.md", "done\n");
|
||||
write(&a, "still-writing.md", "half");
|
||||
let skip = HashSet::from(["still-writing.md".to_string()]);
|
||||
let saved = a.save_changes(None, &skip).unwrap().unwrap();
|
||||
assert_eq!(saved.files, vec!["done.md"]);
|
||||
let saved = a.save_changes(None, &HashSet::new()).unwrap().unwrap();
|
||||
assert_eq!(saved.files, vec!["still-writing.md"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edits_to_different_lines_combine() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "runbook.md", ten_lines());
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
|
||||
write(&a, "runbook.md", ten_lines().replace("line 2\n", "line 2 from Codex\n"));
|
||||
write(&b, "runbook.md", ten_lines().replace("line 9\n", "line 9 from Hermes\n"));
|
||||
save_and_sync(&a);
|
||||
let report = save_and_sync(&b);
|
||||
assert!(report.kept_both.is_empty());
|
||||
a.sync().unwrap();
|
||||
|
||||
let expected = ten_lines().replace("line 2\n", "line 2 from Codex\n").replace("line 9\n", "line 9 from Hermes\n");
|
||||
assert_eq!(read(&a, "runbook.md"), expected.as_bytes());
|
||||
assert_eq!(read(&b, "runbook.md"), expected.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_lines_keep_both_and_tidy_cleans_up() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "runbook.md", ten_lines());
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
|
||||
let codex = ten_lines().replace("line 2\n", "restart with systemctl\n");
|
||||
let hermes = ten_lines().replace("line 2\n", "restart with docker\n");
|
||||
write(&a, "runbook.md", &codex);
|
||||
write(&b, "runbook.md", &hermes);
|
||||
save_and_sync(&a);
|
||||
let report = save_and_sync(&b);
|
||||
|
||||
// Incoming stays in place, the local version is saved beside it. No markers anywhere.
|
||||
assert_eq!(report.kept_both, vec![("runbook.md".into(), "runbook (Hermes's version).md".into())]);
|
||||
assert_eq!(read(&b, "runbook.md"), codex.as_bytes());
|
||||
assert_eq!(read(&b, "runbook (Hermes's version).md"), hermes.as_bytes());
|
||||
a.sync().unwrap();
|
||||
assert_eq!(snapshot(&a.root), snapshot(&b.root));
|
||||
assert!(!String::from_utf8(read(&a, "runbook.md")).unwrap().contains("<<<<<<<"));
|
||||
|
||||
// Sync keeps running: further edits still flow.
|
||||
write(&a, "other.md", "still syncing\n");
|
||||
save_and_sync(&a);
|
||||
b.sync().unwrap();
|
||||
assert_eq!(read(&b, "other.md"), b"still syncing\n");
|
||||
|
||||
// Hermes keeps its own version.
|
||||
let listed = tidy::list(&b).unwrap();
|
||||
assert_eq!(listed.len(), 1);
|
||||
tidy::keep(&b, "runbook.md", Keep::Mine).unwrap().unwrap();
|
||||
b.sync().unwrap();
|
||||
a.sync().unwrap();
|
||||
assert_eq!(read(&a, "runbook.md"), hermes.as_bytes());
|
||||
assert!(!a.root.join("runbook (Hermes's version).md").exists());
|
||||
assert!(tidy::list(&a).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tidy_theirs_and_both() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "x.md", "base\n");
|
||||
write(&a, "y.md", "base\n");
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
write(&a, "x.md", "codex\n");
|
||||
write(&a, "y.md", "codex\n");
|
||||
write(&b, "x.md", "hermes\n");
|
||||
write(&b, "y.md", "hermes\n");
|
||||
save_and_sync(&a);
|
||||
save_and_sync(&b);
|
||||
|
||||
// From Codex's side, "theirs" is Hermes's version (the copy).
|
||||
a.sync().unwrap();
|
||||
tidy::keep(&a, "x.md", Keep::Theirs).unwrap();
|
||||
assert_eq!(read(&a, "x.md"), b"hermes\n");
|
||||
tidy::keep(&a, "y (Hermes's version).md", Keep::Both).unwrap();
|
||||
assert_eq!(read(&a, "y.md"), b"codex\n");
|
||||
assert_eq!(read(&a, "y (from Hermes).md"), b"hermes\n");
|
||||
assert!(tidy::list(&a).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleted_on_one_side_edited_on_the_other_keeps_the_edit() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "note.md", ten_lines());
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
fs::remove_file(a.root.join("note.md")).unwrap();
|
||||
write(&b, "note.md", ten_lines().replace("line 5", "important"));
|
||||
save_and_sync(&a);
|
||||
save_and_sync(&b);
|
||||
a.sync().unwrap();
|
||||
for v in [&a, &b] {
|
||||
assert!(String::from_utf8(read(v, "note.md")).unwrap().contains("important"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offline_changes_queue_and_catch_up() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "seed.md", "seed\n");
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
|
||||
let hidden = w.folder("remote-away.git");
|
||||
fs::rename(&w.remote, &hidden).unwrap();
|
||||
for n in ["one", "two", "three"] {
|
||||
write(&a, &format!("{n}.md"), format!("{n}\n"));
|
||||
a.save_changes(None, &HashSet::new()).unwrap();
|
||||
assert!(a.sync().is_err());
|
||||
}
|
||||
assert_eq!(a.waiting_files().unwrap(), 3);
|
||||
|
||||
fs::rename(&hidden, &w.remote).unwrap();
|
||||
let report = a.sync().unwrap();
|
||||
assert_eq!(report.shared, 3);
|
||||
assert_eq!(a.waiting_files().unwrap(), 0);
|
||||
b.sync().unwrap();
|
||||
for n in ["one", "two", "three"] {
|
||||
assert_eq!(read(&b, &format!("{n}.md")), format!("{n}\n").as_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incoming_changes_never_overwrite_unsaved_local_edits() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "note.md", "v1\n");
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
write(&a, "note.md", "v2 from codex\n");
|
||||
save_and_sync(&a);
|
||||
|
||||
write(&b, "note.md", "being typed on hermes");
|
||||
let report = b.sync().unwrap();
|
||||
assert!(report.waiting_for_local_edits);
|
||||
assert_eq!(read(&b, "note.md"), b"being typed on hermes");
|
||||
|
||||
// Once the edit is saved, both versions are kept.
|
||||
let report = save_and_sync(&b);
|
||||
assert_eq!(report.kept_both.len(), 1);
|
||||
assert_eq!(read(&b, "note.md"), b"v2 from codex\n");
|
||||
assert_eq!(read(&b, "note (Hermes's version).md"), b"being typed on hermes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adopting_an_existing_vault_preserves_every_byte() {
|
||||
let w = World::new();
|
||||
let folder = w.folder("existing");
|
||||
fs::create_dir_all(folder.join(".obsidian/plugins/x")).unwrap();
|
||||
fs::create_dir_all(folder.join("Daily/2026")).unwrap();
|
||||
fs::create_dir_all(folder.join("empty-folder-is-fine")).unwrap();
|
||||
fs::write(folder.join("Home.md"), "# Home\r\n[[Daily]]").unwrap();
|
||||
fs::write(folder.join("Daily/2026/10-28.md"), "today\n").unwrap();
|
||||
fs::write(folder.join(".obsidian/app.json"), "{\"x\":1}").unwrap();
|
||||
fs::write(folder.join(".obsidian/plugins/x/main.js"), "console.log(1)\n").unwrap();
|
||||
fs::write(folder.join("photo.jpg"), [0xFFu8, 0xD8, 0x00, 0x0D, 0x0A, 0x1A]).unwrap();
|
||||
let before = snapshot(&folder);
|
||||
|
||||
let (cfg, added, err) = vault::add("DevOps", &folder, w.remote.to_str().unwrap(), "Codex").unwrap();
|
||||
assert!(matches!(added, Added::Adopted(5)));
|
||||
assert_eq!(err, None);
|
||||
assert_eq!(snapshot(&folder), before);
|
||||
assert!(lune_core::state::read_marker(&folder.join(".git")).is_some_and(|m| !m.created_folder));
|
||||
|
||||
// A second device with its own notes adopts into the same remote: everything from both survives.
|
||||
let other = w.folder("other");
|
||||
fs::create_dir_all(&other).unwrap();
|
||||
fs::write(other.join("Home.md"), "# Other home\n").unwrap();
|
||||
fs::write(other.join("vps.md"), "hermes notes\n").unwrap();
|
||||
let b = w.device_at("Hermes", &other);
|
||||
let a = Vault::open(&cfg, "Codex").unwrap();
|
||||
a.sync().unwrap();
|
||||
assert_eq!(read(&b, "Home.md"), b"# Home\r\n[[Daily]]");
|
||||
assert_eq!(read(&b, "Home (Hermes's version).md"), b"# Other home\n");
|
||||
assert_eq!(read(&a, "vps.md"), b"hermes notes\n");
|
||||
assert_eq!(read(&a, "photo.jpg"), [0xFFu8, 0xD8, 0x00, 0x0D, 0x0A, 0x1A]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_records_a_name_even_with_nothing_new() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "a.md", "a\n");
|
||||
a.checkpoint("first pass").unwrap();
|
||||
a.checkpoint("reviewed").unwrap();
|
||||
let head = a.head_commit().unwrap().unwrap();
|
||||
assert_eq!(head.summary().unwrap().unwrap(), "reviewed");
|
||||
assert_eq!(head.parent(0).unwrap().summary().unwrap().unwrap(), "first pass");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn names_differing_only_by_case_are_renamed_not_overwritten() {
|
||||
let w = World::new();
|
||||
let a = w.device("Codex");
|
||||
write(&a, "seed.md", "seed\n");
|
||||
save_and_sync(&a);
|
||||
|
||||
// Someone on a case-sensitive system adds Notes.md and notes.md.
|
||||
let remote = Repository::open_bare(&w.remote).unwrap();
|
||||
let parent = remote.find_reference("refs/heads/main").unwrap().peel_to_commit().unwrap();
|
||||
let mut tb = remote.treebuilder(Some(&parent.tree().unwrap())).unwrap();
|
||||
tb.insert("Notes.md", remote.blob(b"upper\n").unwrap(), 0o100644).unwrap();
|
||||
tb.insert("notes.md", remote.blob(b"lower\n").unwrap(), 0o100644).unwrap();
|
||||
let tree = remote.find_tree(tb.write().unwrap()).unwrap();
|
||||
let sig = git2::Signature::now("Linux box", "l@x").unwrap();
|
||||
remote.commit(Some("refs/heads/main"), &sig, &sig, "case", &tree, &[&parent]).unwrap();
|
||||
|
||||
a.repo().config().unwrap().open_level(git2::ConfigLevel::Local).unwrap().set_bool("core.ignorecase", true).unwrap();
|
||||
let report = a.sync().unwrap();
|
||||
assert_eq!(report.case_renamed, vec![("notes.md".into(), "notes (name clash).md".into())]);
|
||||
assert_eq!(read(&a, "Notes.md"), b"upper\n");
|
||||
assert_eq!(read(&a, "notes (name clash).md"), b"lower\n");
|
||||
}
|
||||
Reference in New Issue
Block a user