0.1.6: GBrain brains — refreshed after sync, clashes never make a second page
- 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>
This commit is contained in:
237
core/tests/brain.rs
Normal file
237
core/tests/brain.rs
Normal file
@@ -0,0 +1,237 @@
|
||||
//! GBrain vaults (M8): the refresh after sync, with a fake `gbrain` that records what it was asked and can fail or hang.
|
||||
|
||||
use lune_core::brain;
|
||||
use lune_core::config::{Config, VaultConfig, VaultKind};
|
||||
use lune_core::runner::{self, Timing};
|
||||
use lune_core::vault::{self, Vault};
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant};
|
||||
use tempfile::TempDir;
|
||||
|
||||
/// A `gbrain` that logs "<folder> <args>" and fails if `fail` exists beside it, or hangs if `hang` does.
|
||||
fn fake_gbrain(dir: &Path) -> (PathBuf, PathBuf) {
|
||||
fs::create_dir_all(dir).unwrap();
|
||||
let (bin, log) = (dir.join("gbrain"), dir.join("calls.log"));
|
||||
fs::write(
|
||||
&bin,
|
||||
format!(
|
||||
"#!/bin/sh\necho \"$PWD $*\" >> '{log}'\n[ -e '{d}/hang' ] && exec sleep 30\n[ -e '{d}/fail' ] && {{ echo 'database is locked' >&2; exit 3; }}\nexit 0\n",
|
||||
log = log.display(),
|
||||
d = dir.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::set_permissions(&bin, fs::Permissions::from_mode(0o755)).unwrap();
|
||||
(bin, log)
|
||||
}
|
||||
|
||||
fn calls(log: &Path) -> Vec<String> {
|
||||
fs::read_to_string(log).unwrap_or_default().lines().map(String::from).collect()
|
||||
}
|
||||
|
||||
fn wait_for(what: &str, secs: u64, mut ok: impl FnMut() -> bool) {
|
||||
let until = Instant::now() + Duration::from_secs(secs);
|
||||
while !ok() {
|
||||
assert!(Instant::now() < until, "timed out waiting for {what}");
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
|
||||
fn add(remote: &Path, folder: &Path, label: &str, kind: VaultKind) -> VaultConfig {
|
||||
let (mut cfg, _, err) = vault::add("Brain", folder, remote.to_str().unwrap(), label).unwrap();
|
||||
assert_eq!(err, None);
|
||||
cfg.kind = kind;
|
||||
vault::apply_kind_ignores(&git2::Repository::open(&cfg.path).unwrap(), kind).unwrap();
|
||||
cfg
|
||||
}
|
||||
|
||||
fn bare(dir: &Path, name: &str) -> PathBuf {
|
||||
let p = dir.join(name);
|
||||
git2::Repository::init_bare(&p).unwrap();
|
||||
p
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pin_file_stays_home_and_the_shared_file_travels() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let remote = bare(tmp.path(), "r.git");
|
||||
let gb = add(&remote, &tmp.path().join("gb"), "Codex", VaultKind::Gbrain);
|
||||
let v = Vault::open(&gb, "Codex").unwrap();
|
||||
fs::write(v.root.join(".gbrain-source"), "brain\n").unwrap();
|
||||
fs::write(v.root.join("gbrain.yml"), "database: x\n").unwrap();
|
||||
assert!(brain::looks_like_gbrain(&v.root));
|
||||
v.save_changes(None, &HashSet::new()).unwrap();
|
||||
assert_eq!(v.tracked_files().unwrap(), vec!["gbrain.yml".to_string()]);
|
||||
|
||||
// An Obsidian vault is untouched: the file is shared like any other, and Lune suggests marking it.
|
||||
let remote2 = bare(tmp.path(), "r2.git");
|
||||
let ob = add(&remote2, &tmp.path().join("ob"), "Codex", VaultKind::Obsidian);
|
||||
let o = Vault::open(&ob, "Codex").unwrap();
|
||||
fs::write(o.root.join(".gbrain-source"), "brain\n").unwrap();
|
||||
o.save_changes(None, &HashSet::new()).unwrap();
|
||||
assert_eq!(o.tracked_files().unwrap(), vec![".gbrain-source".to_string()]);
|
||||
assert!(brain::should_suggest(&ob, &o.root, &o.git_dir));
|
||||
brain::dismiss_suggestion(&o.git_dir).unwrap();
|
||||
assert!(!brain::should_suggest(&ob, &o.root, &o.git_dir));
|
||||
assert!(!brain::should_suggest(&gb, &v.root, &v.git_dir), "already marked");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gbrain_is_found_beyond_the_shell_path() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let (bin, _) = fake_gbrain(&tmp.path().join("bun/bin"));
|
||||
let dirs = vec![tmp.path().join("nothing"), tmp.path().join("bun/bin")];
|
||||
assert_eq!(brain::find_in(&dirs), Some(bin));
|
||||
assert_eq!(brain::find_in(&dirs[..1]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kind_and_device_settings_live_in_the_config_file() {
|
||||
let c: Config = toml::from_str("[[vault]]\nname = \"Old\"\npath = \"/x\"\nremote = \"\"\nbranch = \"main\"\n").unwrap();
|
||||
assert_eq!(c.vaults[0].kind, VaultKind::Obsidian, "vaults from before stay Obsidian");
|
||||
let mut v = c.vaults[0].clone();
|
||||
v.kind = VaultKind::Gbrain;
|
||||
v.gbrain_refresh = Some(true);
|
||||
v.gbrain_source = Some("brain-vps".into());
|
||||
let text = toml::to_string_pretty(&Config { vaults: vec![v], ..Default::default() }).unwrap();
|
||||
let back: Config = toml::from_str(&text).unwrap();
|
||||
assert_eq!(back.vaults[0].kind, VaultKind::Gbrain);
|
||||
assert_eq!(back.vaults[0].gbrain_refresh, Some(true));
|
||||
assert_eq!(back.vaults[0].gbrain_source_name(), "brain-vps");
|
||||
assert_eq!(c.vaults[0].gbrain_source_name(), "Old");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refresh_uses_the_source_and_no_pull_fails_plainly_and_retry_clears_it() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let remote = bare(tmp.path(), "r.git");
|
||||
let mut cfg = add(&remote, &tmp.path().join("brain"), "Hermes", VaultKind::Gbrain);
|
||||
cfg.gbrain_refresh = Some(true);
|
||||
cfg.gbrain_source = Some("hermes-brain".into());
|
||||
let (bin, log) = fake_gbrain(&tmp.path().join("bin"));
|
||||
let secs = Duration::from_secs(20);
|
||||
|
||||
assert!(brain::refresh_now(&cfg, Some(&bin), secs, true).unwrap());
|
||||
let said = calls(&log);
|
||||
assert_eq!(said.len(), 1);
|
||||
let (folder, args) = said[0].split_once(' ').unwrap();
|
||||
assert_eq!(fs::canonicalize(folder).unwrap(), fs::canonicalize(&cfg.path).unwrap(), "runs in the vault folder");
|
||||
assert_eq!(args, "sync --source hermes-brain --no-pull");
|
||||
let st = lune_core::status::of(&cfg, "Hermes");
|
||||
assert!(!st.gbrain.unwrap().failed);
|
||||
|
||||
// It breaks: amber with the plain words, and the reason.
|
||||
fs::write(tmp.path().join("bin/fail"), "").unwrap();
|
||||
assert!(brain::refresh_now(&cfg, Some(&bin), secs, true).is_err());
|
||||
let st = lune_core::status::of(&cfg, "Hermes");
|
||||
assert_eq!(st.level, "behind");
|
||||
assert!(st.headline.contains("GBrain not refreshed"), "{}", st.headline);
|
||||
let g = st.gbrain.unwrap();
|
||||
assert!(g.failed && g.error.unwrap().contains("database is locked"));
|
||||
|
||||
// Retry once it's fixed clears it.
|
||||
fs::remove_file(tmp.path().join("bin/fail")).unwrap();
|
||||
assert!(brain::refresh_now(&cfg, Some(&bin), secs, true).unwrap());
|
||||
let st = lune_core::status::of(&cfg, "Hermes");
|
||||
assert!(!st.gbrain.unwrap().failed && !st.headline.contains("GBrain not refreshed"));
|
||||
|
||||
// A hang is stopped at the timeout and counts as a failure.
|
||||
fs::write(tmp.path().join("bin/hang"), "").unwrap();
|
||||
let began = Instant::now();
|
||||
let err = brain::refresh_now(&cfg, Some(&bin), Duration::from_secs(1), true).unwrap_err();
|
||||
assert!(format!("{err:#}").contains("too long") && began.elapsed() < Duration::from_secs(10));
|
||||
}
|
||||
|
||||
/// The whole path through the background runner: which syncs refresh, and that a bad gbrain never holds one up.
|
||||
#[test]
|
||||
fn the_runner_refreshes_only_gbrain_vaults_after_changes_and_never_waits_for_it() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let (bin, log) = fake_gbrain(&tmp.path().join("bin"));
|
||||
unsafe {
|
||||
std::env::set_var("LUNE_HOME", tmp.path().join("home"));
|
||||
std::env::set_var("LUNE_GBRAIN", &bin);
|
||||
}
|
||||
|
||||
// Brain (GBrain, refresh on) and Plain (Obsidian) each have another device, A, writing to the same remote.
|
||||
let (r_brain, r_plain) = (bare(tmp.path(), "brain.git"), bare(tmp.path(), "plain.git"));
|
||||
let (a_brain, a_plain) = (add(&r_brain, &tmp.path().join("a-brain"), "A", VaultKind::Gbrain), add(&r_plain, &tmp.path().join("a-plain"), "A", VaultKind::Obsidian));
|
||||
let mut mine = add(&r_brain, &tmp.path().join("my-brain"), "Mine", VaultKind::Gbrain);
|
||||
mine.name = "Brain".into();
|
||||
mine.gbrain_refresh = Some(true);
|
||||
let (mut plain, _, _) = vault::add("Plain", &tmp.path().join("my-plain"), r_plain.to_str().unwrap(), "Mine").unwrap();
|
||||
plain.gbrain_refresh = Some(true); // even if it were set, an Obsidian vault never runs it
|
||||
let cfg = Config { device_name: Some("Mine".into()), vaults_dir: Some(tmp.path().join("vaults")), vaults: vec![mine.clone(), plain.clone()], ..Default::default() };
|
||||
cfg.save().unwrap();
|
||||
|
||||
runner::STOP.store(false, Ordering::Relaxed);
|
||||
let timing = Timing { quiet: Duration::from_millis(300), cap: Duration::from_secs(2), settle: Duration::from_millis(100) };
|
||||
let quiet_log: lune_core::runner::Log<'static> = &|_, _| {};
|
||||
let threads: Vec<_> = [mine.clone(), plain.clone()]
|
||||
.into_iter()
|
||||
.map(|c| std::thread::spawn(move || runner::run(&c, "Mine", Duration::from_millis(200), timing, quiet_log)))
|
||||
.collect();
|
||||
|
||||
let a = Vault::open(&a_brain, "A").unwrap();
|
||||
let ap = Vault::open(&a_plain, "A").unwrap();
|
||||
let write_sync = |v: &Vault, f: &str, t: &str| {
|
||||
fs::write(v.root.join(f), t).unwrap();
|
||||
v.save_changes(None, &HashSet::new()).unwrap();
|
||||
v.sync().unwrap();
|
||||
};
|
||||
|
||||
// Nothing has changed anywhere: nothing runs.
|
||||
std::thread::sleep(Duration::from_millis(1500));
|
||||
assert!(calls(&log).is_empty(), "{:?}", calls(&log));
|
||||
|
||||
// Notes arrive in both vaults: only the GBrain one refreshes, with its source and --no-pull.
|
||||
write_sync(&a, "one.md", "one\n");
|
||||
write_sync(&ap, "one.md", "one\n");
|
||||
wait_for("the first refresh", 10, || !calls(&log).is_empty());
|
||||
std::thread::sleep(Duration::from_millis(1200));
|
||||
let said = calls(&log);
|
||||
assert_eq!(said.len(), 1, "{said:?}");
|
||||
assert!(said[0].ends_with("sync --source Brain --no-pull") && said[0].contains("my-brain"), "{said:?}");
|
||||
assert!(fs::read(tmp.path().join("my-plain/one.md")).is_ok(), "the Obsidian vault synced too");
|
||||
|
||||
// Idle: no more runs. This device's own saved edit refreshes once it's committed.
|
||||
std::thread::sleep(Duration::from_millis(1000));
|
||||
assert_eq!(calls(&log).len(), 1);
|
||||
fs::write(tmp.path().join("my-brain/mine.md"), "mine\n").unwrap();
|
||||
wait_for("a refresh after this device's own edit", 10, || calls(&log).len() == 2);
|
||||
|
||||
// GBrain breaks: the vault turns amber, and notes keep arriving.
|
||||
fs::write(tmp.path().join("bin/fail"), "").unwrap();
|
||||
write_sync(&a, "two.md", "two\n");
|
||||
wait_for("the failed refresh", 10, || lune_core::status::of(&mine, "Mine").headline.contains("GBrain not refreshed"));
|
||||
assert_eq!(lune_core::status::of(&mine, "Mine").level, "behind");
|
||||
assert_eq!(fs::read(tmp.path().join("my-brain/two.md")).unwrap(), b"two\n");
|
||||
|
||||
// Retry (what the window's button and `lunesync gbrain --retry` do) once it's fixed.
|
||||
fs::remove_file(tmp.path().join("bin/fail")).unwrap();
|
||||
assert!(brain::refresh_now(&mine, None, Duration::from_secs(20), false).unwrap());
|
||||
assert!(!lune_core::status::of(&mine, "Mine").headline.contains("GBrain not refreshed"));
|
||||
|
||||
// GBrain hangs: the next notes still arrive, and several changes in a row queue just one more run.
|
||||
fs::write(tmp.path().join("bin/hang"), "").unwrap();
|
||||
let before = calls(&log).len();
|
||||
write_sync(&a, "three.md", "three\n");
|
||||
wait_for("a refresh to start", 10, || calls(&log).len() == before + 1);
|
||||
for i in 4..7 {
|
||||
write_sync(&a, &format!("n{i}.md"), "later\n");
|
||||
}
|
||||
wait_for("notes to arrive while gbrain hangs", 10, || tmp.path().join("my-brain/n6.md").exists());
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
assert_eq!(calls(&log).len(), before + 1, "no second copy running at once");
|
||||
assert!(brain::is_refreshing(&tmp.path().join("my-brain/.git")));
|
||||
|
||||
runner::STOP.store(true, Ordering::Relaxed);
|
||||
for t in threads {
|
||||
t.join().unwrap().unwrap();
|
||||
}
|
||||
assert!(!calls(&log).iter().any(|c| c.contains("my-plain")), "Obsidian vaults never run gbrain");
|
||||
}
|
||||
|
||||
use std::sync::atomic::Ordering;
|
||||
359
core/tests/clash.rs
Normal file
359
core/tests/clash.rs
Normal file
@@ -0,0 +1,359 @@
|
||||
//! M8 step 2: in a GBrain vault a clash never makes a second page. Lines both sides added are all kept (incoming
|
||||
//! first); a rewrite of the same lines keeps the incoming one in place and this device's version waits in
|
||||
//! `.git/lune/tidy/`. Obsidian vaults keep making `(Label's version).md` copies, as before.
|
||||
|
||||
use git2::{ObjectType, Repository, TreeWalkMode, TreeWalkResult};
|
||||
use lune_core::config::VaultKind;
|
||||
use lune_core::names;
|
||||
use lune_core::tidy::{self, Keep};
|
||||
use lune_core::vault::{self, Vault};
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tempfile::TempDir;
|
||||
|
||||
struct World {
|
||||
tmp: TempDir,
|
||||
remote: PathBuf,
|
||||
kind: VaultKind,
|
||||
}
|
||||
|
||||
impl World {
|
||||
fn new(kind: VaultKind) -> World {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let remote = tmp.path().join("remote.git");
|
||||
Repository::init_bare(&remote).unwrap();
|
||||
World { tmp, remote, kind }
|
||||
}
|
||||
|
||||
fn device(&self, label: &str) -> Vault {
|
||||
let (mut cfg, _, err) = vault::add("Brain", &self.tmp.path().join(label), self.remote.to_str().unwrap(), label).unwrap();
|
||||
assert_eq!(err, None);
|
||||
cfg.kind = self.kind;
|
||||
vault::apply_kind_ignores(&Repository::open(&cfg.path).unwrap(), self.kind).unwrap();
|
||||
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()
|
||||
}
|
||||
|
||||
fn files(root: &Path) -> Vec<String> {
|
||||
fn walk(root: &Path, dir: &Path, out: &mut Vec<String>) {
|
||||
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.push(rel);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = vec![];
|
||||
walk(root, root, &mut out);
|
||||
out.sort();
|
||||
out
|
||||
}
|
||||
|
||||
/// True if any commit reachable from any ref in `repo` has a tree holding a kept-both copy path.
|
||||
fn copy_anywhere_in_history(repo: &Repository) -> bool {
|
||||
let mut walk = repo.revwalk().unwrap();
|
||||
for r in repo.references().unwrap() {
|
||||
if let Ok(oid) = r.unwrap().peel_to_commit().map(|c| c.id()) {
|
||||
walk.push(oid).unwrap();
|
||||
}
|
||||
}
|
||||
walk.any(|id| {
|
||||
let tree = repo.find_commit(id.unwrap()).unwrap().tree().unwrap();
|
||||
let mut found = false;
|
||||
tree.walk(TreeWalkMode::PreOrder, |dir, e| {
|
||||
if e.kind() == Some(ObjectType::Blob) {
|
||||
found |= names::parse_copy(&format!("{dir}{}", e.name().unwrap())).is_some();
|
||||
}
|
||||
TreeWalkResult::Ok
|
||||
})
|
||||
.unwrap();
|
||||
found
|
||||
})
|
||||
}
|
||||
|
||||
const PAGE: &str = "# Ada\n\nShe builds engines.\n\n## Timeline\n- 2020: joined\n";
|
||||
|
||||
fn two(w: &World) -> (Vault, Vault) {
|
||||
let a = w.device("Codex");
|
||||
write(&a, "people/ada.md", PAGE);
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
(a, b)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_append_to_the_same_page_and_both_entries_stay_incoming_first() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
let (a, b) = two(&w);
|
||||
write(&a, "people/ada.md", format!("{PAGE}- 2021: Codex met her\n"));
|
||||
write(&b, "people/ada.md", format!("{PAGE}- 2021: Hermes read her paper\n"));
|
||||
save_and_sync(&a);
|
||||
let report = save_and_sync(&b);
|
||||
|
||||
assert_eq!(report.joined, vec!["people/ada.md".to_string()]);
|
||||
assert!(report.kept_both.is_empty() && report.to_tidy.is_empty());
|
||||
let want = format!("{PAGE}- 2021: Codex met her\n- 2021: Hermes read her paper\n");
|
||||
assert_eq!(read(&b, "people/ada.md"), want.as_bytes(), "incoming (Codex) first, then this device's");
|
||||
a.sync().unwrap();
|
||||
assert_eq!(read(&a, "people/ada.md"), want.as_bytes());
|
||||
assert_eq!(files(&a.root), ["people/ada.md"]);
|
||||
assert_eq!(files(&b.root), ["people/ada.md"]);
|
||||
assert!(tidy::list(&a).unwrap().is_empty() && tidy::list(&b).unwrap().is_empty());
|
||||
assert!(!copy_anywhere_in_history(a.repo()) && !copy_anywhere_in_history(b.repo()));
|
||||
assert!(!copy_anywhere_in_history(&Repository::open(&w.remote).unwrap()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn appending_to_a_page_without_a_final_newline_still_joins() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
let a = w.device("Codex");
|
||||
write(&a, "p.md", "title\nlast");
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
write(&a, "p.md", "title\nlast\ncodex");
|
||||
write(&b, "p.md", "title\nlast\nhermes");
|
||||
save_and_sync(&a);
|
||||
let r = save_and_sync(&b);
|
||||
assert_eq!(r.joined.len(), 1, "{r:?}");
|
||||
assert_eq!(read(&b, "p.md"), b"title\nlast\ncodex\nhermes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewriting_the_same_paragraph_keeps_the_incoming_one_and_tidies_ours() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
let (a, b) = two(&w);
|
||||
let codex = PAGE.replace("She builds engines.", "She builds steam engines.");
|
||||
let hermes = PAGE.replace("She builds engines.", "She designs analytical engines.");
|
||||
write(&a, "people/ada.md", &codex);
|
||||
write(&b, "people/ada.md", &hermes);
|
||||
save_and_sync(&a);
|
||||
let report = save_and_sync(&b);
|
||||
|
||||
assert_eq!(report.to_tidy, vec!["people/ada.md".to_string()]);
|
||||
assert_eq!(read(&b, "people/ada.md"), codex.as_bytes(), "incoming stays in place");
|
||||
assert_eq!(files(&b.root), ["people/ada.md"], "no copy in the folder");
|
||||
a.sync().unwrap();
|
||||
assert_eq!(files(&a.root), ["people/ada.md"]);
|
||||
assert_eq!(read(&a, "people/ada.md"), codex.as_bytes());
|
||||
assert!(!copy_anywhere_in_history(a.repo()) && !copy_anywhere_in_history(b.repo()));
|
||||
assert!(!copy_anywhere_in_history(&Repository::open(&w.remote).unwrap()));
|
||||
|
||||
// Hermes's version is kept byte-exact on its device only, inside .git/lune/tidy.
|
||||
let entries = tidy::entries(&b.git_dir);
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!((entries[0].note.as_str(), entries[0].label.as_str(), entries[0].source.as_str()), ("people/ada.md", "Hermes", "sync"));
|
||||
assert_eq!(tidy::mine_bytes(&b.git_dir, &entries[0].id).unwrap(), hermes.as_bytes());
|
||||
assert!(tidy::entries(&a.git_dir).is_empty());
|
||||
// This device's own entries stay in history, as for Obsidian; only the folder has to stay clean.
|
||||
// And it shows under Notes to tidy, not as a file.
|
||||
let listed = tidy::list(&b).unwrap();
|
||||
assert_eq!(listed.len(), 1);
|
||||
assert!(listed[0].local && listed[0].copy.starts_with(".git/"));
|
||||
// Sync keeps working with it waiting.
|
||||
write(&a, "other.md", "still syncing\n");
|
||||
save_and_sync(&a);
|
||||
b.sync().unwrap();
|
||||
assert_eq!(read(&b, "other.md"), b"still syncing\n");
|
||||
assert_eq!(tidy::list(&b).unwrap().len(), 1);
|
||||
}
|
||||
|
||||
fn clash(w: &World) -> (Vault, Vault, String, String) {
|
||||
let (a, b) = two(w);
|
||||
let codex = PAGE.replace("She builds engines.", "She builds steam engines.");
|
||||
let hermes = PAGE.replace("She builds engines.", "She designs analytical engines.");
|
||||
write(&a, "people/ada.md", &codex);
|
||||
write(&b, "people/ada.md", &hermes);
|
||||
save_and_sync(&a);
|
||||
save_and_sync(&b);
|
||||
a.sync().unwrap();
|
||||
(a, b, codex, hermes)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tidy_keep_mine_reaches_the_other_device() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
let (a, b, _, hermes) = clash(&w);
|
||||
tidy::keep(&b, "people/ada.md", Keep::Mine).unwrap().unwrap();
|
||||
assert!(tidy::entries(&b.git_dir).is_empty() && tidy::list(&b).unwrap().is_empty());
|
||||
b.sync().unwrap();
|
||||
a.sync().unwrap();
|
||||
assert_eq!(read(&a, "people/ada.md"), hermes.as_bytes());
|
||||
assert_eq!(files(&a.root), ["people/ada.md"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tidy_keep_theirs_drops_ours_and_changes_nothing_elsewhere() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
let (a, b, codex, _) = clash(&w);
|
||||
let saved = tidy::keep(&b, "people/ada.md", Keep::Theirs).unwrap();
|
||||
assert!(saved.is_none());
|
||||
assert!(tidy::list(&b).unwrap().is_empty());
|
||||
assert_eq!(read(&b, "people/ada.md"), codex.as_bytes());
|
||||
b.sync().unwrap();
|
||||
a.sync().unwrap();
|
||||
assert_eq!(read(&a, "people/ada.md"), codex.as_bytes());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tidy_keep_both_writes_both_into_the_one_note() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
let (a, b, codex, hermes) = clash(&w);
|
||||
tidy::keep(&b, "people/ada.md", Keep::Both).unwrap().unwrap();
|
||||
assert_eq!(files(&b.root), ["people/ada.md"]);
|
||||
let text = String::from_utf8(read(&b, "people/ada.md")).unwrap();
|
||||
assert!(text.starts_with(&codex), "the one in place comes first");
|
||||
assert!(text.ends_with(&hermes), "this device's version follows, exact");
|
||||
assert!(text.contains("Kept from Hermes's version"), "{text}");
|
||||
assert!(tidy::entries(&b.git_dir).is_empty());
|
||||
b.sync().unwrap();
|
||||
a.sync().unwrap();
|
||||
assert_eq!(read(&a, "people/ada.md"), text.as_bytes());
|
||||
assert_eq!(files(&a.root), ["people/ada.md"]);
|
||||
}
|
||||
|
||||
const FAKE_KEY: &str = "AWS_KEY=AKIAIOSFODNN7EXAMPLE\n";
|
||||
|
||||
#[test]
|
||||
fn a_held_note_in_a_gbrain_vault_is_kept_aside_not_copied_and_stays_held() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
let a = w.device("Codex");
|
||||
write(&a, "ops.md", "ops\n");
|
||||
save_and_sync(&a);
|
||||
let b = w.device("Hermes");
|
||||
let held = format!("ops\n{FAKE_KEY}");
|
||||
write(&a, "ops.md", &held);
|
||||
a.save_changes(None, &HashSet::new()).unwrap();
|
||||
assert_eq!(a.secret_holds().unwrap().len(), 1);
|
||||
write(&b, "ops.md", "ops\nnew runbook link\n");
|
||||
save_and_sync(&b);
|
||||
|
||||
let report = a.sync().unwrap();
|
||||
assert_eq!(report.held_to_tidy, vec!["ops.md".to_string()]);
|
||||
assert!(report.set_aside.is_empty());
|
||||
assert_eq!(read(&a, "ops.md"), b"ops\nnew runbook link\n");
|
||||
assert_eq!(files(&a.root), ["ops.md"], "no copy in the folder");
|
||||
assert!(a.secret_holds().unwrap().is_empty(), "the working note is the plain incoming one");
|
||||
// Still held (P2, X6): counts as a held secret everywhere a held note does, so the vault is red, not just amber.
|
||||
let held_notes: Vec<String> = a.all_secret_holds().unwrap().into_iter().map(|n| n.path).collect();
|
||||
assert_eq!(held_notes, ["ops.md"]);
|
||||
let cfg = lune_core::config::VaultConfig {
|
||||
name: "Brain".into(),
|
||||
path: a.root.clone(),
|
||||
remote: w.remote.to_str().unwrap().into(),
|
||||
branch: "main".into(),
|
||||
kind: VaultKind::Gbrain,
|
||||
..Default::default()
|
||||
};
|
||||
let st = lune_core::status::of(&cfg, "Codex");
|
||||
assert_eq!(st.level, "secret");
|
||||
assert_eq!(st.secrets.len(), 1);
|
||||
assert_eq!(st.secrets[0].path, "ops.md");
|
||||
let entries = tidy::entries(&a.git_dir);
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert!(entries[0].held);
|
||||
assert_eq!(tidy::mine_bytes(&a.git_dir, &entries[0].id).unwrap(), held.as_bytes());
|
||||
|
||||
// "Not a secret" / "Replace with a pointer" work on the kept-aside version too.
|
||||
let (entry, hits) = tidy::held_aside_hits(&a, "ops.md", None).unwrap().unwrap();
|
||||
tidy::replace_held_aside(&a, &entry, &hits, None, "search prod in Secrets Manager").unwrap();
|
||||
assert!(a.all_secret_holds().unwrap().is_empty(), "no longer held once the secret is replaced");
|
||||
assert!(String::from_utf8(tidy::mine_bytes(&a.git_dir, &entry.id).unwrap()).unwrap().contains("Secrets Manager"));
|
||||
// Put the original back for the next check.
|
||||
tidy::put(&a.git_dir, &entry, held.as_bytes()).unwrap();
|
||||
assert_eq!(a.all_secret_holds().unwrap().len(), 1);
|
||||
|
||||
// Putting it back brings the secret check back with it: held, not shared.
|
||||
tidy::keep(&a, "ops.md", Keep::Mine).unwrap();
|
||||
assert_eq!(read(&a, "ops.md"), held.as_bytes());
|
||||
let holds: Vec<String> = a.secret_holds().unwrap().into_iter().map(|n| n.path).collect();
|
||||
assert_eq!(holds, ["ops.md"]);
|
||||
a.sync().unwrap();
|
||||
b.sync().unwrap();
|
||||
assert!(!String::from_utf8(read(&b, "ops.md")).unwrap().contains("AKIA"), "the key never travelled");
|
||||
assert!(!copy_anywhere_in_history(a.repo()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undo_in_a_gbrain_vault_keeps_the_brought_back_version_and_tidies_the_other() {
|
||||
let w = World::new(VaultKind::Gbrain);
|
||||
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");
|
||||
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();
|
||||
let clashes = a.undo_held().unwrap();
|
||||
assert_eq!(clashes.to_tidy, vec!["notes/03.md".to_string()]);
|
||||
assert!(clashes.copies.is_empty());
|
||||
assert_eq!(files(&a.root).len(), 20, "no extra note");
|
||||
assert_eq!(read(&a, "notes/03.md"), b"note 3\nline two\n", "what the undo brought back is in place");
|
||||
let entries = tidy::entries(&a.git_dir);
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!(tidy::mine_bytes(&a.git_dir, &entries[0].id).unwrap(), b"note 3\nline two\nedited on hermes\n");
|
||||
assert!(!copy_anywhere_in_history(a.repo()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn obsidian_vaults_still_make_version_copies() {
|
||||
let w = World::new(VaultKind::Obsidian);
|
||||
let (a, b, codex, hermes) = clash(&w);
|
||||
assert_eq!(read(&b, "people/ada.md"), codex.as_bytes());
|
||||
assert_eq!(read(&b, "people/ada (Hermes's version).md"), hermes.as_bytes());
|
||||
assert!(tidy::entries(&b.git_dir).is_empty());
|
||||
assert_eq!(tidy::list(&b).unwrap().len(), 1);
|
||||
assert!(!tidy::list(&b).unwrap()[0].local);
|
||||
assert_eq!(read(&a, "people/ada (Hermes's version).md"), hermes.as_bytes());
|
||||
|
||||
// Both appended: still a copy for Obsidian (as today).
|
||||
let w = World::new(VaultKind::Obsidian);
|
||||
let (a, b) = two(&w);
|
||||
write(&a, "people/ada.md", format!("{PAGE}- codex\n"));
|
||||
write(&b, "people/ada.md", format!("{PAGE}- hermes\n"));
|
||||
save_and_sync(&a);
|
||||
let r = save_and_sync(&b);
|
||||
assert_eq!(r.kept_both.len(), 1);
|
||||
assert!(r.joined.is_empty() && r.to_tidy.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_kind_is_read_when_the_clash_happens() {
|
||||
// A vault opened as Obsidian, then marked GBrain while it runs: the next clash makes no copy.
|
||||
let w = World::new(VaultKind::Obsidian);
|
||||
let (a, b) = two(&w);
|
||||
vault::apply_kind_ignores(b.repo(), VaultKind::Gbrain).unwrap();
|
||||
write(&a, "people/ada.md", PAGE.replace("engines", "steam engines"));
|
||||
write(&b, "people/ada.md", PAGE.replace("engines", "analytical engines"));
|
||||
save_and_sync(&a);
|
||||
let r = save_and_sync(&b);
|
||||
assert_eq!(r.to_tidy.len(), 1);
|
||||
assert_eq!(files(&b.root), ["people/ada.md"]);
|
||||
}
|
||||
@@ -583,5 +583,6 @@ fn made_cfg(w: &World) -> lune_core::config::VaultConfig {
|
||||
branch: "main".into(),
|
||||
hub_vault: None,
|
||||
hub_url: None,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user