authoravatarclover caruso<git@paperclover.net>2026-10-19 12:44:17-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 22:58:56-07:00
log73e6c5a36317d186c098ef23929b70a878cc09d3
treed8fe76c9ce397f1b1477da3ccadbac2eab3f8f1b
parent548ff67a10b8310d3ef2841ea5c508df8ab4df0f
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

test(notebook): protected, versioned and conflicted sections move and copy whole, on a share too

Notebook tests move and copy a password-protected section, with an edit queued under its key, and OneNote 2010's own sections holding page versions and conflict pages, and find each whole in the other notebook. live_move_section, against a Samba lab share, moves a section with an edit queued offline from the share to this computer and back after another section, each original going to its notebook's recycle bin. Refs #105 Assisted-by: claude-opus-5.5

2 files changed, 232 insertions(+), 0 deletions(-)

crates/notebook/src/smb/tests.rs+121
...@@ -1078,3 +1078,124 @@ fn unreachable_server_is_not_connected() {...@@ -1078,3 +1078,124 @@ fn unreachable_server_is_not_connected() {
1078 };1078 };
1079 assert_eq!(io_error(denied).kind(), io::ErrorKind::Other);1079 assert_eq!(io_error(denied).kind(), io::ErrorKind::Other);
1080}1080}
1081
1082/// A section moves from a notebook on the share to one on this computer and back, with an edit
1083/// queued offline coming along, each original going to its notebook's recycle bin. With
1084/// `ONESTORE_SMB_MOVE_EXPORT`, the share's notebook is copied there for a cold reopen.
1085#[test]
1086#[ignore = "requires an owned Samba share at ONESTORE_SMB_LAB"]
1087fn live_move_section() {
1088 use crate::session::{Notebook, Section, stored_pages};
1089 use onestore::op::{Edit, Op, SectionOp};
1090 let client = std::sync::Arc::new(client());
1091 let root = format!("move-{}", std::process::id());
1092 client.create_directory(&root).unwrap();
1093 let toc = onestore::create_table_of_contents("Open Notebook.onetoc2", &[]).unwrap();
1094 client
1095 .create(&format!("{root}/Open Notebook.onetoc2"), &toc)
1096 .unwrap();
1097 let cache = tempfile::tempdir().unwrap();
1098 let page = |title| onestore::PageCreation::new(None, Some(title), "Author").unwrap();
1099 let mut share =
1100 Notebook::open_smb(std::sync::Arc::clone(&client), &root, cache.path()).unwrap();
1101 for name in ["Alpha", "Beta"] {
1102 share.create_section("", name, &page(name)).unwrap();
1103 }
1104 let mut local = Notebook::create(
1105 cache.path().join("Local"),
1106 cache.path(),
1107 0x91baae,
1108 &page("Local"),
1109 )
1110 .unwrap();
1111
1112 let replica = share.replica_path("Alpha.one").unwrap();
1113 std::fs::create_dir_all(replica.parent().unwrap()).unwrap();
1114 let replica =
1115 crate::Replica::open_or_create(replica, None, || share.read_section("Alpha.one")).unwrap();
1116 let section = Section::resume_smb(
1117 format!("{root}/Alpha.one"),
1118 replica,
1119 1 << 24,
1120 || Ok(super::tests::client()),
1121 true,
1122 || {},
1123 )
1124 .unwrap();
1125 let edit = Edit {
1126 at: 134_040_000_000_000_000,
1127 ops: vec![Op::Section(SectionOp::Create(page("Queued offline")))],
1128 };
1129 section.apply("Author", edit).unwrap();
1130 section.written().unwrap();
1131 let moved = share
1132 .move_section("Alpha.one", section, &mut local, "", None)
1133 .unwrap();
1134 assert!(moved.kept.is_none(), "{:?}", moved.kept);
1135 let titles = |image: Vec<u8>| -> Vec<String> {
1136 (stored_pages(&image).unwrap().into_iter())
1137 .map(|stored| stored.page.title)
1138 .collect()
1139 };
1140 assert_eq!(
1141 titles(local.read_section(&moved.path).unwrap()),
1142 ["Alpha", "Queued offline"]
1143 );
1144 let sections = |notebook: &Notebook| -> Vec<String> {
1145 (notebook.catalog().sections())
1146 .map(|section| section.path.clone())
1147 .collect()
1148 };
1149 let fresh = Notebook::open_smb(std::sync::Arc::clone(&client), &root, cache.path()).unwrap();
1150 assert_eq!(
1151 sections(&fresh),
1152 ["Beta.one", "OneNote_RecycleBin/Alpha.one"]
1153 );
1154 assert!(fresh.catalog().toc.as_ref().unwrap().unresolved.is_empty());
1155
1156 // And back onto the share, after Beta.
1157 let section = local.section(&moved.path, false, || {}).unwrap();
1158 let back = local
1159 .move_section(&moved.path, section, &mut share, "", Some("Beta.one"))
1160 .unwrap();
1161 assert!(back.kept.is_none(), "{:?}", back.kept);
1162 assert_eq!(back.path, "Alpha.one");
1163 let fresh = Notebook::open_smb(std::sync::Arc::clone(&client), &root, cache.path()).unwrap();
1164 assert_eq!(
1165 sections(&fresh),
1166 ["Beta.one", "Alpha.one", "OneNote_RecycleBin/Alpha.one"]
1167 );
1168 assert_eq!(
1169 titles(fresh.read_section("Alpha.one").unwrap()),
1170 ["Alpha", "Queued offline"]
1171 );
1172 let header = onestore::Store::parse(&fresh.read_section("Alpha.one").unwrap())
1173 .unwrap()
1174 .header;
1175 assert_eq!(
1176 header.ancestor,
1177 fresh.catalog().toc.as_ref().unwrap().file_id
1178 );
1179 if let Ok(export) = std::env::var("ONESTORE_SMB_MOVE_EXPORT") {
1180 let export = std::path::Path::new(&export);
1181 for folder in fresh.catalog().folders() {
1182 std::fs::create_dir_all(export.join(&folder.path)).unwrap();
1183 let files = (folder.toc.iter().map(|toc| toc.filename.clone())).chain(
1184 folder
1185 .sections
1186 .iter()
1187 .map(|section| section.path.rsplit('/').next().unwrap().to_owned()),
1188 );
1189 for name in files {
1190 let path = match folder.path.as_str() {
1191 "" => name.clone(),
1192 folder => format!("{folder}/{name}"),
1193 };
1194 let bytes = client
1195 .read_storage(&format!("{root}/{path}"), 1 << 24)
1196 .unwrap();
1197 std::fs::write(export.join(&path), bytes).unwrap();
1198 }
1199 }
1200 }
1201}
crates/notebook/tests/move_section.rs+111
...@@ -297,3 +297,114 @@ fn a_move_waits_while_queued_edits_have_a_changed_file_to_rebase_onto() {...@@ -297,3 +297,114 @@ fn a_move_waits_while_queued_edits_have_a_changed_file_to_rebase_onto() {
297 assert!(!place.target.join("Alpha.one").exists());297 assert!(!place.target.join("Alpha.one").exists());
298 assert!(place.source.join("Alpha.one").exists());298 assert!(place.source.join("Alpha.one").exists());
299}299}
300
301#[test]
302fn a_protected_section_moves_and_copies_under_its_password() {
303 let place = notebooks();
304 let mut source = Notebook::open(&place.source, &place.cache).unwrap();
305 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
306 for path in ["Alpha.one", "Beta.one"] {
307 source.set_password(path, None, Some("secret")).unwrap();
308 }
309 let key = source.unlock("Alpha.one", "secret").unwrap();
310 let section = source
311 .section_unlocked("Alpha.one", &key, true, || {})
312 .unwrap();
313 queue_page(&section, "Queued page");
314 let moved = source
315 .move_section("Alpha.one", section, &mut target, "", None)
316 .unwrap();
317 assert!(moved.kept.is_none(), "{:?}", moved.kept);
318 let key = source.unlock("Beta.one", "secret").unwrap();
319 let beta = source
320 .section_unlocked("Beta.one", &key, false, || {})
321 .unwrap();
322 let copy = source
323 .copy_section("Beta.one", beta.replica(), &mut target, "", None)
324 .unwrap();
325 for (path, titles) in [
326 (&moved.path, ["First page", "Queued page"].as_slice()),
327 (&copy, ["First page"].as_slice()),
328 ] {
329 let listed = (target.catalog().sections.iter()).find(|section| section.path == *path);
330 assert!(matches!(
331 listed.map(|section| &section.state),
332 Some(notebook::discover::SectionState::Locked)
333 ));
334 assert!(target.unlock(path, "wrong").is_err());
335 let key = target.unlock(path, "secret").unwrap();
336 let image = std::fs::read(place.target.join(path)).unwrap();
337 let stored: Vec<String> = notebook::session::stored_pages_unlocked(&image, &key)
338 .unwrap()
339 .into_iter()
340 .map(|stored| stored.page.title)
341 .collect();
342 assert_eq!(stored, titles);
343 }
344}
345
346/// What a reader sees of the section file at `file`: each page's title with its versions'
347/// authors and its conflict pages' titles.
348fn history(file: &Path) -> Vec<(String, Vec<Option<String>>, Vec<String>)> {
349 let arena = onestore::Arena::default();
350 let mut section = onestore::Section::open(&arena, std::fs::read(file).unwrap()).unwrap();
351 let (versions, conflicts) = (section.versions().unwrap(), section.conflicts().unwrap());
352 let pages = section.pages().unwrap();
353 (pages.into_iter())
354 .map(|(space, title, _)| {
355 let of = |listed: &onestore::ExGuid| *listed == space;
356 let versions = (versions.iter().filter(|(listed, _)| of(listed)))
357 .flat_map(|(_, versions)| versions.iter().map(|version| version.author.clone()))
358 .collect();
359 let conflicts = (conflicts.iter().filter(|(listed, _)| of(listed)))
360 .flat_map(|(_, pages)| pages.iter().map(|page| page.title.clone()))
361 .collect();
362 (title, versions, conflicts)
363 })
364 .collect()
365}
366
367#[test]
368fn versions_and_conflict_pages_come_along_in_a_move_and_a_copy() {
369 let place = notebooks();
370 let corpus = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus");
371 let fixtures = [
372 corpus.join("page-versions/native/step-02/notebook/History.one"),
373 corpus.join("conflict-page/native-three/notebook/synthetic.one"),
374 ];
375 let mut source = Notebook::open(&place.source, &place.cache).unwrap();
376 source.create_group("", "Group").unwrap();
377 for fixture in &fixtures {
378 let name = fixture.file_name().unwrap();
379 std::fs::copy(fixture, place.source.join(name)).unwrap();
380 source.refresh().unwrap();
381 source.move_entry(name.to_str().unwrap(), "Group").unwrap();
382 }
383 let mut target = Notebook::open(&place.target, &place.cache).unwrap();
384 for fixture in &fixtures {
385 let name = fixture.file_name().unwrap().to_str().unwrap();
386 let path = format!("Group/{name}");
387 let expected = history(fixture);
388 assert!(
389 expected
390 .iter()
391 .any(|(_, versions, conflicts)| { !versions.is_empty() || !conflicts.is_empty() })
392 );
393 let section = source.section(&path, false, || {}).unwrap();
394 let copy = source
395 .copy_section(&path, section.replica(), &mut target, "", None)
396 .unwrap();
397 assert_eq!(history(&place.target.join(&copy)), expected, "{copy}");
398 let moved = source
399 .move_section(&path, section, &mut target, "", None)
400 .unwrap();
401 assert!(moved.kept.is_none(), "{:?}", moved.kept);
402 assert_ne!(moved.path, copy);
403 assert_eq!(
404 history(&place.target.join(&moved.path)),
405 expected,
406 "{}",
407 moved.path
408 );
409 }
410}