diff --git a/crates/notebook/src/smb/tests.rs b/crates/notebook/src/smb/tests.rs index 3c00cccb08ce7bdafe4337ac5e99d4fafe983c30..a68889f9e015e3ac4de022f5ed5883447f7d7880 100644 --- a/crates/notebook/src/smb/tests.rs +++ b/crates/notebook/src/smb/tests.rs @@ -1078,3 +1078,124 @@ fn unreachable_server_is_not_connected() { }; assert_eq!(io_error(denied).kind(), io::ErrorKind::Other); } + +/// A section moves from a notebook on the share to one on this computer and back, with an edit +/// queued offline coming along, each original going to its notebook's recycle bin. With +/// `ONESTORE_SMB_MOVE_EXPORT`, the share's notebook is copied there for a cold reopen. +#[test] +#[ignore = "requires an owned Samba share at ONESTORE_SMB_LAB"] +fn live_move_section() { + use crate::session::{Notebook, Section, stored_pages}; + use onestore::op::{Edit, Op, SectionOp}; + let client = std::sync::Arc::new(client()); + let root = format!("move-{}", std::process::id()); + client.create_directory(&root).unwrap(); + let toc = onestore::create_table_of_contents("Open Notebook.onetoc2", &[]).unwrap(); + client + .create(&format!("{root}/Open Notebook.onetoc2"), &toc) + .unwrap(); + let cache = tempfile::tempdir().unwrap(); + let page = |title| onestore::PageCreation::new(None, Some(title), "Author").unwrap(); + let mut share = + Notebook::open_smb(std::sync::Arc::clone(&client), &root, cache.path()).unwrap(); + for name in ["Alpha", "Beta"] { + share.create_section("", name, &page(name)).unwrap(); + } + let mut local = Notebook::create( + cache.path().join("Local"), + cache.path(), + 0x91baae, + &page("Local"), + ) + .unwrap(); + + let replica = share.replica_path("Alpha.one").unwrap(); + std::fs::create_dir_all(replica.parent().unwrap()).unwrap(); + let replica = + crate::Replica::open_or_create(replica, None, || share.read_section("Alpha.one")).unwrap(); + let section = Section::resume_smb( + format!("{root}/Alpha.one"), + replica, + 1 << 24, + || Ok(super::tests::client()), + true, + || {}, + ) + .unwrap(); + let edit = Edit { + at: 134_040_000_000_000_000, + ops: vec![Op::Section(SectionOp::Create(page("Queued offline")))], + }; + section.apply("Author", edit).unwrap(); + section.written().unwrap(); + let moved = share + .move_section("Alpha.one", section, &mut local, "", None) + .unwrap(); + assert!(moved.kept.is_none(), "{:?}", moved.kept); + let titles = |image: Vec| -> Vec { + (stored_pages(&image).unwrap().into_iter()) + .map(|stored| stored.page.title) + .collect() + }; + assert_eq!( + titles(local.read_section(&moved.path).unwrap()), + ["Alpha", "Queued offline"] + ); + let sections = |notebook: &Notebook| -> Vec { + (notebook.catalog().sections()) + .map(|section| section.path.clone()) + .collect() + }; + let fresh = Notebook::open_smb(std::sync::Arc::clone(&client), &root, cache.path()).unwrap(); + assert_eq!( + sections(&fresh), + ["Beta.one", "OneNote_RecycleBin/Alpha.one"] + ); + assert!(fresh.catalog().toc.as_ref().unwrap().unresolved.is_empty()); + + // And back onto the share, after Beta. + let section = local.section(&moved.path, false, || {}).unwrap(); + let back = local + .move_section(&moved.path, section, &mut share, "", Some("Beta.one")) + .unwrap(); + assert!(back.kept.is_none(), "{:?}", back.kept); + assert_eq!(back.path, "Alpha.one"); + let fresh = Notebook::open_smb(std::sync::Arc::clone(&client), &root, cache.path()).unwrap(); + assert_eq!( + sections(&fresh), + ["Beta.one", "Alpha.one", "OneNote_RecycleBin/Alpha.one"] + ); + assert_eq!( + titles(fresh.read_section("Alpha.one").unwrap()), + ["Alpha", "Queued offline"] + ); + let header = onestore::Store::parse(&fresh.read_section("Alpha.one").unwrap()) + .unwrap() + .header; + assert_eq!( + header.ancestor, + fresh.catalog().toc.as_ref().unwrap().file_id + ); + if let Ok(export) = std::env::var("ONESTORE_SMB_MOVE_EXPORT") { + let export = std::path::Path::new(&export); + for folder in fresh.catalog().folders() { + std::fs::create_dir_all(export.join(&folder.path)).unwrap(); + let files = (folder.toc.iter().map(|toc| toc.filename.clone())).chain( + folder + .sections + .iter() + .map(|section| section.path.rsplit('/').next().unwrap().to_owned()), + ); + for name in files { + let path = match folder.path.as_str() { + "" => name.clone(), + folder => format!("{folder}/{name}"), + }; + let bytes = client + .read_storage(&format!("{root}/{path}"), 1 << 24) + .unwrap(); + std::fs::write(export.join(&path), bytes).unwrap(); + } + } + } +} diff --git a/crates/notebook/tests/move_section.rs b/crates/notebook/tests/move_section.rs index b5967d88d4eb3170b1477dd6b95dbcc576ee6d6e..2c9676e5dd3f7bdd3a1ba85cc2c864346b797f59 100644 --- a/crates/notebook/tests/move_section.rs +++ b/crates/notebook/tests/move_section.rs @@ -297,3 +297,114 @@ fn a_move_waits_while_queued_edits_have_a_changed_file_to_rebase_onto() { assert!(!place.target.join("Alpha.one").exists()); assert!(place.source.join("Alpha.one").exists()); } + +#[test] +fn a_protected_section_moves_and_copies_under_its_password() { + let place = notebooks(); + let mut source = Notebook::open(&place.source, &place.cache).unwrap(); + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + for path in ["Alpha.one", "Beta.one"] { + source.set_password(path, None, Some("secret")).unwrap(); + } + let key = source.unlock("Alpha.one", "secret").unwrap(); + let section = source + .section_unlocked("Alpha.one", &key, true, || {}) + .unwrap(); + queue_page(§ion, "Queued page"); + let moved = source + .move_section("Alpha.one", section, &mut target, "", None) + .unwrap(); + assert!(moved.kept.is_none(), "{:?}", moved.kept); + let key = source.unlock("Beta.one", "secret").unwrap(); + let beta = source + .section_unlocked("Beta.one", &key, false, || {}) + .unwrap(); + let copy = source + .copy_section("Beta.one", beta.replica(), &mut target, "", None) + .unwrap(); + for (path, titles) in [ + (&moved.path, ["First page", "Queued page"].as_slice()), + (©, ["First page"].as_slice()), + ] { + let listed = (target.catalog().sections.iter()).find(|section| section.path == *path); + assert!(matches!( + listed.map(|section| §ion.state), + Some(notebook::discover::SectionState::Locked) + )); + assert!(target.unlock(path, "wrong").is_err()); + let key = target.unlock(path, "secret").unwrap(); + let image = std::fs::read(place.target.join(path)).unwrap(); + let stored: Vec = notebook::session::stored_pages_unlocked(&image, &key) + .unwrap() + .into_iter() + .map(|stored| stored.page.title) + .collect(); + assert_eq!(stored, titles); + } +} + +/// What a reader sees of the section file at `file`: each page's title with its versions' +/// authors and its conflict pages' titles. +fn history(file: &Path) -> Vec<(String, Vec>, Vec)> { + let arena = onestore::Arena::default(); + let mut section = onestore::Section::open(&arena, std::fs::read(file).unwrap()).unwrap(); + let (versions, conflicts) = (section.versions().unwrap(), section.conflicts().unwrap()); + let pages = section.pages().unwrap(); + (pages.into_iter()) + .map(|(space, title, _)| { + let of = |listed: &onestore::ExGuid| *listed == space; + let versions = (versions.iter().filter(|(listed, _)| of(listed))) + .flat_map(|(_, versions)| versions.iter().map(|version| version.author.clone())) + .collect(); + let conflicts = (conflicts.iter().filter(|(listed, _)| of(listed))) + .flat_map(|(_, pages)| pages.iter().map(|page| page.title.clone())) + .collect(); + (title, versions, conflicts) + }) + .collect() +} + +#[test] +fn versions_and_conflict_pages_come_along_in_a_move_and_a_copy() { + let place = notebooks(); + let corpus = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus"); + let fixtures = [ + corpus.join("page-versions/native/step-02/notebook/History.one"), + corpus.join("conflict-page/native-three/notebook/synthetic.one"), + ]; + let mut source = Notebook::open(&place.source, &place.cache).unwrap(); + source.create_group("", "Group").unwrap(); + for fixture in &fixtures { + let name = fixture.file_name().unwrap(); + std::fs::copy(fixture, place.source.join(name)).unwrap(); + source.refresh().unwrap(); + source.move_entry(name.to_str().unwrap(), "Group").unwrap(); + } + let mut target = Notebook::open(&place.target, &place.cache).unwrap(); + for fixture in &fixtures { + let name = fixture.file_name().unwrap().to_str().unwrap(); + let path = format!("Group/{name}"); + let expected = history(fixture); + assert!( + expected + .iter() + .any(|(_, versions, conflicts)| { !versions.is_empty() || !conflicts.is_empty() }) + ); + let section = source.section(&path, false, || {}).unwrap(); + let copy = source + .copy_section(&path, section.replica(), &mut target, "", None) + .unwrap(); + assert_eq!(history(&place.target.join(©)), expected, "{copy}"); + let moved = source + .move_section(&path, section, &mut target, "", None) + .unwrap(); + assert!(moved.kept.is_none(), "{:?}", moved.kept); + assert_ne!(moved.path, copy); + assert_eq!( + history(&place.target.join(&moved.path)), + expected, + "{}", + moved.path + ); + } +}