- A vault has a kind on the hub: Obsidian (as before) or GBrain. Set it with
`lunesync vault kind`, the canvas panel or the window; Lune suggests it when
it finds gbrain.yml / .gbrain-source. Every change is in the access log.
- GBrain vaults: devices that have `gbrain` (looked for beyond PATH) are asked
once whether to refresh it after sync. Lune then runs
`gbrain sync --source <name> --no-pull` in the background after notes arrive
or are saved; it never holds up syncing. A failed refresh turns the vault
amber ("GBrain not refreshed") with Retry, also on the canvas.
- Clashes in GBrain vaults: lines both sides added are kept in the one note,
incoming first. Same lines changed: the incoming version stays, this
device's waits under Notes to tidy, stored in .git/lune/tidy (never a copy
file GBrain would index). Applies to syncing, undo, held notes and
handovers. Held notes moved aside stay red until decided.
- .gbrain-source is kept per device. Obsidian vaults are unchanged.
- SPEC: M8 section and decisions recorded.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
589 lines
21 KiB
Rust
589 lines
21 KiB
Rust
//! 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(), "Added 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 device_names_with_brackets_keep_both_and_tidy_cleanly() {
|
|
let w = World::new();
|
|
let a = w.device("Sen setup (CLI)");
|
|
write(&a, "welcome.md", "base\n");
|
|
save_and_sync(&a);
|
|
let b = w.device("Mac");
|
|
write(&a, "welcome.md", "tuesday\n");
|
|
write(&b, "welcome.md", "friday\n");
|
|
save_and_sync(&b);
|
|
save_and_sync(&a);
|
|
let kept = tidy::list(&a).unwrap();
|
|
assert_eq!(kept.len(), 1);
|
|
assert_eq!((kept[0].note.as_str(), kept[0].label.as_str()), ("welcome.md", "Sen setup (CLI)"));
|
|
tidy::keep(&a, &kept[0].copy, Keep::Mine).unwrap();
|
|
assert_eq!(read(&a, "welcome.md"), b"tuesday\n");
|
|
assert_eq!(snapshot(&a.root).len(), 1, "no stray notes");
|
|
}
|
|
|
|
#[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");
|
|
}
|
|
|
|
// ---- Milestone 2: secrets and the runaway guard ----
|
|
|
|
use lune_core::secrets;
|
|
|
|
const FAKE_KEY: &str = "aws_access_key_id = AKIAIOSFODNN7EXAMPLE\n";
|
|
|
|
#[test]
|
|
fn a_note_with_a_secret_stays_home_while_the_rest_is_shared() {
|
|
let w = World::new();
|
|
let a = w.device("Codex");
|
|
let b = w.device("Hermes");
|
|
write(&a, "aws.md", format!("# AWS\n{FAKE_KEY}"));
|
|
write(&a, "other.md", "fine\n");
|
|
save_and_sync(&a);
|
|
b.sync().unwrap();
|
|
assert_eq!(read(&b, "other.md"), b"fine\n");
|
|
assert!(!b.root.join("aws.md").exists());
|
|
|
|
let held = a.secret_holds().unwrap();
|
|
assert_eq!(held.len(), 1);
|
|
assert_eq!((held[0].path.as_str(), held[0].hits[0].rule, held[0].hits[0].line), ("aws.md", "AWS access key", 2));
|
|
|
|
let text = String::from_utf8(read(&a, "aws.md")).unwrap();
|
|
write(&a, "aws.md", secrets::replace_with_pointer(&text, &held[0].hits, None, "(search prod-deploy in Secrets Manager)"));
|
|
save_and_sync(&a);
|
|
b.sync().unwrap();
|
|
assert_eq!(read(&b, "aws.md"), b"# AWS\naws_access_key_id = (search prod-deploy in Secrets Manager)\n");
|
|
}
|
|
|
|
#[test]
|
|
fn not_a_secret_is_remembered_for_that_line() {
|
|
let w = World::new();
|
|
let a = w.device("Codex");
|
|
let b = w.device("Hermes");
|
|
write(&a, "example.md", "The docs use AKIAIOSFODNN7EXAMPLE as a sample.\n");
|
|
save_and_sync(&a);
|
|
let held = a.secret_holds().unwrap();
|
|
secrets::allow(&a.git_dir, &held[0].hits[0], "example.md").unwrap();
|
|
assert!(a.secret_holds().unwrap().is_empty());
|
|
save_and_sync(&a);
|
|
b.sync().unwrap();
|
|
assert!(b.root.join("example.md").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn incoming_changes_to_a_held_note_land_and_the_held_version_moves_beside() {
|
|
let w = World::new();
|
|
let a = w.device("Codex");
|
|
write(&a, "ops.md", "ops\n");
|
|
save_and_sync(&a);
|
|
let b = w.device("Hermes");
|
|
|
|
write(&a, "ops.md", format!("ops\n{FAKE_KEY}"));
|
|
save_and_sync(&a);
|
|
write(&b, "ops.md", "ops\nnew runbook link\n");
|
|
save_and_sync(&b);
|
|
|
|
let report = a.sync().unwrap();
|
|
assert!(!report.waiting_for_local_edits);
|
|
assert_eq!(report.set_aside, vec![("ops.md".into(), "ops (Codex's version).md".into())]);
|
|
assert_eq!(read(&a, "ops.md"), b"ops\nnew runbook link\n");
|
|
assert_eq!(read(&a, "ops (Codex's version).md"), format!("ops\n{FAKE_KEY}").as_bytes());
|
|
let held: Vec<String> = a.secret_holds().unwrap().into_iter().map(|n| n.path).collect();
|
|
assert_eq!(held, ["ops (Codex's version).md"]);
|
|
}
|
|
|
|
fn vault_of_20(w: &World) -> (Vault, Vault) {
|
|
let a = w.device("Codex");
|
|
for i in 0..20 {
|
|
write(&a, &format!("notes/{i:02}.md"), format!("note {i}\nline two\n"));
|
|
}
|
|
save_and_sync(&a);
|
|
let b = w.device("Hermes");
|
|
(a, b)
|
|
}
|
|
|
|
#[test]
|
|
fn deleting_half_the_vault_is_held_and_undo_brings_back_every_byte() {
|
|
let w = World::new();
|
|
let (a, b) = vault_of_20(&w);
|
|
let before = snapshot(&a.root);
|
|
for i in 0..10 {
|
|
fs::remove_file(a.root.join(format!("notes/{i:02}.md"))).unwrap();
|
|
}
|
|
write(&a, "notes/03-new.md", "unrelated new note\n");
|
|
let saved = a.save_changes(None, &HashSet::new()).unwrap().unwrap();
|
|
let d = saved.held.expect("should be held");
|
|
assert_eq!((d.removed, d.total), (10, 20));
|
|
|
|
let report = a.sync().unwrap();
|
|
assert!(report.holding);
|
|
b.sync().unwrap();
|
|
assert_eq!(snapshot(&b.root), before, "nothing reached the other device");
|
|
|
|
a.undo_held().unwrap();
|
|
let mut expected = before.clone();
|
|
expected.insert("notes/03-new.md".into(), b"unrelated new note\n".to_vec());
|
|
assert_eq!(snapshot(&a.root), expected);
|
|
a.sync().unwrap();
|
|
b.sync().unwrap();
|
|
assert_eq!(snapshot(&b.root), expected);
|
|
}
|
|
|
|
#[test]
|
|
fn share_anyway_sends_the_big_change() {
|
|
let w = World::new();
|
|
let (a, b) = vault_of_20(&w);
|
|
for i in 0..10 {
|
|
fs::remove_file(a.root.join(format!("notes/{i:02}.md"))).unwrap();
|
|
}
|
|
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_some());
|
|
a.sync().unwrap();
|
|
a.share_held().unwrap();
|
|
let report = a.sync().unwrap();
|
|
assert!(!report.holding);
|
|
b.sync().unwrap();
|
|
assert_eq!(snapshot(&b.root).len(), 10);
|
|
}
|
|
|
|
#[test]
|
|
fn undo_keeps_both_when_a_removed_note_was_edited_elsewhere() {
|
|
let w = World::new();
|
|
let (a, b) = vault_of_20(&w);
|
|
for i in 0..10 {
|
|
fs::remove_file(a.root.join(format!("notes/{i:02}.md"))).unwrap();
|
|
}
|
|
a.save_changes(None, &HashSet::new()).unwrap();
|
|
write(&b, "notes/03.md", "note 3\nline two\nedited on hermes\n");
|
|
save_and_sync(&b);
|
|
a.sync().unwrap();
|
|
a.undo_held().unwrap();
|
|
let all: Vec<Vec<u8>> = snapshot(&a.root).into_values().collect();
|
|
assert!(all.contains(&b"note 3\nline two\n".to_vec()));
|
|
assert!(all.contains(&b"note 3\nline two\nedited on hermes\n".to_vec()));
|
|
}
|
|
|
|
#[test]
|
|
fn moves_and_appends_are_not_runaway() {
|
|
let w = World::new();
|
|
let (a, _b) = vault_of_20(&w);
|
|
for i in 0..20 {
|
|
fs::rename(a.root.join(format!("notes/{i:02}.md")), a.root.join(format!("{i:02}.md"))).unwrap();
|
|
}
|
|
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_none());
|
|
for i in 0..20 {
|
|
write(&a, &format!("{i:02}.md"), format!("note {i}\nline two\nappended\n"));
|
|
}
|
|
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_none());
|
|
for i in 0..20 {
|
|
write(&a, &format!("{i:02}.md"), "wiped\n");
|
|
}
|
|
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn uninstall_deletes_only_folders_it_made_and_never_unshared_work() {
|
|
let w = World::new();
|
|
// Uninstall rewrites the config; keep it inside this test's folder.
|
|
unsafe { std::env::set_var("LUNE_HOME", w.folder("home")) };
|
|
let other = w.device("Hermes");
|
|
let made = w.device_at("Codex", &w.folder("made"));
|
|
write(&made, "note.md", "shared\n");
|
|
save_and_sync(&made);
|
|
// A folder the person already had: Lune adopted it, so it never deletes it.
|
|
let own = w.folder("own");
|
|
fs::create_dir_all(&own).unwrap();
|
|
fs::write(own.join("mine.md"), "mine\n").unwrap();
|
|
let remote2 = w.folder("remote2.git");
|
|
Repository::init_bare(&remote2).unwrap();
|
|
let (own_cfg, _, _) = vault::add("Own", &own, remote2.to_str().unwrap(), "Codex").unwrap();
|
|
// A folder Lune made that holds a note with a secret: it can't be shared, so it must stay.
|
|
let remote3 = w.folder("remote3.git");
|
|
Repository::init_bare(&remote3).unwrap();
|
|
let (held_cfg, _, _) = vault::add("Held", &w.folder("held"), remote3.to_str().unwrap(), "Codex").unwrap();
|
|
fs::write(w.folder("held").join("aws.md"), format!("# AWS\n{FAKE_KEY}")).unwrap();
|
|
|
|
let mut config = lune_core::config::Config {
|
|
device_name: Some("Codex".into()),
|
|
vaults: vec![made_cfg(&w), own_cfg, held_cfg],
|
|
..Default::default()
|
|
};
|
|
let done = lune_core::membership::uninstall(&mut config).unwrap();
|
|
|
|
assert!(!w.folder("made").exists(), "{done:?}");
|
|
assert_eq!(fs::read(own.join("mine.md")).unwrap(), b"mine\n");
|
|
assert!(w.folder("held").join("aws.md").exists());
|
|
assert_eq!(done.removed.len(), 1);
|
|
assert_eq!(done.kept.len(), 2, "{:?}", done.kept);
|
|
assert!(config.vaults.is_empty());
|
|
// What was shared before uninstalling is still with everyone else.
|
|
other.sync().unwrap();
|
|
assert_eq!(read(&other, "note.md"), b"shared\n");
|
|
}
|
|
|
|
fn made_cfg(w: &World) -> lune_core::config::VaultConfig {
|
|
lune_core::config::VaultConfig {
|
|
name: "Test".into(),
|
|
path: w.folder("made"),
|
|
remote: w.remote.to_str().unwrap().into(),
|
|
branch: "main".into(),
|
|
hub_vault: None,
|
|
hub_url: None,
|
|
..Default::default()
|
|
}
|
|
}
|