| 1 | //! Moving and copying a section into another notebook, as OneNote 2010's Move or Copy does. |
| 2 | |
| 3 | use notebook::session::{Notebook, stored_pages}; |
| 4 | use onestore::{ |
| 5 | PageCreation, |
| 6 | op::{Edit, Op, SectionOp}, |
| 7 | }; |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | |
| 10 | struct Notebooks { |
| 11 | _temporary: tempfile::TempDir, |
| 12 | cache: PathBuf, |
| 13 | source: PathBuf, |
| 14 | target: PathBuf, |
| 15 | } |
| 16 | |
| 17 | /// A notebook holding Alpha and Beta beside "New Section 1", and another holding Gamma. |
| 18 | fn notebooks() -> Notebooks { |
| 19 | let temporary = tempfile::tempdir().unwrap(); |
| 20 | let cache = temporary.path().join("cache"); |
| 21 | let page = PageCreation::new(None, Some("First page"), "Author").unwrap(); |
| 22 | let [source, target] = ["Source", "Target"].map(|name| temporary.path().join(name)); |
| 23 | let mut notebook = Notebook::create(&source, &cache, 0x00d7ff, &page).unwrap(); |
| 24 | for name in ["Alpha", "Beta"] { |
| 25 | notebook.create_section("", name, &page).unwrap(); |
| 26 | } |
| 27 | let mut notebook = Notebook::create(&target, &cache, 0x91baae, &page).unwrap(); |
| 28 | notebook.create_section("", "Gamma", &page).unwrap(); |
| 29 | Notebooks { |
| 30 | _temporary: temporary, |
| 31 | cache, |
| 32 | source, |
| 33 | target, |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn sections(notebook: &Notebook) -> Vec<String> { |
| 38 | (notebook.catalog().sections.iter()) |
| 39 | .map(|section| section.path.clone()) |
| 40 | .collect() |
| 41 | } |
| 42 | |
| 43 | fn titles(file: &Path) -> Vec<String> { |
| 44 | stored_pages(&std::fs::read(file).unwrap()) |
| 45 | .unwrap() |
| 46 | .into_iter() |
| 47 | .map(|stored| stored.page.title) |
| 48 | .collect() |
| 49 | } |
| 50 | |
| 51 | /// The file identity, root object space and colour a section file holds. |
| 52 | fn identities(file: &Path) -> ([u8; 16], onestore::ExGuid, Option<u32>) { |
| 53 | let image = std::fs::read(file).unwrap(); |
| 54 | let store = onestore::Store::parse(&image).unwrap(); |
| 55 | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 56 | let document = onestore::document::Document::parse(&index).unwrap(); |
| 57 | let revision = document.active(document.root).unwrap(); |
| 58 | let onestore::document::Kind::SectionMetadata { color, .. } = |
| 59 | &revision.nodes[&revision.roots[&2]].kind |
| 60 | else { |
| 61 | panic!("no section metadata") |
| 62 | }; |
| 63 | (store.header.file_id, index.root, *color) |
| 64 | } |
| 65 | |
| 66 | fn queue_page(section: &notebook::session::Section, title: &str) { |
| 67 | let creation = PageCreation::new(None, Some(title), "Author").unwrap(); |
| 68 | section |
| 69 | .apply( |
| 70 | "Author", |
| 71 | Edit { |
| 72 | at: 133_000_000_000_000_000, |
| 73 | ops: vec![Op::Section(SectionOp::Create(creation))], |
| 74 | }, |
| 75 | ) |
| 76 | .unwrap(); |
| 77 | section.written().unwrap(); |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn a_moved_section_carries_its_queued_edits_and_its_original_goes_to_the_bin() { |
| 82 | let place = notebooks(); |
| 83 | let mut source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 84 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 85 | let before = identities(&place.source.join("Alpha.one")); |
| 86 | // Offline, so the edit waits in the replica, published nowhere. |
| 87 | let section = source.section("Alpha.one", true, || {}).unwrap(); |
| 88 | queue_page(&section, "Queued page"); |
| 89 | assert!(!section.pending().unwrap().is_empty()); |
| 90 | let moved = source |
| 91 | .move_section( |
| 92 | "Alpha.one", |
| 93 | section, |
| 94 | &mut target, |
| 95 | "", |
| 96 | Some("New Section 1.one"), |
| 97 | ) |
| 98 | .unwrap(); |
| 99 | assert_eq!(moved.path, "Alpha.one"); |
| 100 | assert!(moved.kept.is_none(), "{:?}", moved.kept); |
| 101 | |
| 102 | assert_eq!( |
| 103 | sections(&target), |
| 104 | ["New Section 1.one", "Alpha.one", "Gamma.one"] |
| 105 | ); |
| 106 | let copy = place.target.join("Alpha.one"); |
| 107 | let after = identities(&copy); |
| 108 | assert_ne!(after.0, before.0, "a file identity of its own"); |
| 109 | assert_ne!(after.1, before.1, "object spaces of its own"); |
| 110 | assert_eq!(after.2, before.2, "its colour"); |
| 111 | assert_eq!(titles(&copy), ["First page", "Queued page"]); |
| 112 | let header = onestore::Store::parse(&std::fs::read(&copy).unwrap()) |
| 113 | .unwrap() |
| 114 | .header; |
| 115 | let toc = target.catalog().toc.as_ref().unwrap().file_id; |
| 116 | assert_eq!(header.ancestor, toc, "placed under its TOC"); |
| 117 | |
| 118 | assert_eq!(sections(&source), ["New Section 1.one", "Beta.one"]); |
| 119 | assert!(source.catalog().toc.as_ref().unwrap().unresolved.is_empty()); |
| 120 | let bin = &source.catalog().groups[0]; |
| 121 | assert_eq!(bin.path, "OneNote_RecycleBin"); |
| 122 | assert_eq!(bin.sections[0].path, "OneNote_RecycleBin/Alpha.one"); |
| 123 | assert_eq!( |
| 124 | identities(&place.source.join("OneNote_RecycleBin/Alpha.one")), |
| 125 | before |
| 126 | ); |
| 127 | |
| 128 | // Each notebook read cold lists the section where it went. |
| 129 | let source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 130 | let target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 131 | assert_eq!(sections(&source), ["New Section 1.one", "Beta.one"]); |
| 132 | assert_eq!( |
| 133 | sections(&target), |
| 134 | ["New Section 1.one", "Alpha.one", "Gamma.one"] |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn a_copy_takes_the_next_free_name_and_leaves_the_section_where_it_is() { |
| 140 | let place = notebooks(); |
| 141 | let source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 142 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 143 | let beta = source.section("Beta.one", false, || {}).unwrap(); |
| 144 | for expected in ["Beta.one", "Beta 2.one"] { |
| 145 | let copy = source |
| 146 | .copy_section("Beta.one", beta.replica(), &mut target, "", None) |
| 147 | .unwrap(); |
| 148 | assert_eq!(copy, expected); |
| 149 | } |
| 150 | assert_eq!( |
| 151 | sections(&target), |
| 152 | ["New Section 1.one", "Gamma.one", "Beta.one", "Beta 2.one"] |
| 153 | ); |
| 154 | // Into its own notebook, beside itself, with an identity of its own. |
| 155 | let mut again = Notebook::open(&place.source, &place.cache).unwrap(); |
| 156 | let copy = source |
| 157 | .copy_section("Beta.one", beta.replica(), &mut again, "", Some("Beta.one")) |
| 158 | .unwrap(); |
| 159 | assert_eq!(copy, "Beta 2.one"); |
| 160 | assert_eq!( |
| 161 | sections(&again), |
| 162 | ["New Section 1.one", "Alpha.one", "Beta.one", "Beta 2.one"] |
| 163 | ); |
| 164 | let [original, copied] = |
| 165 | ["Beta.one", "Beta 2.one"].map(|name| identities(&place.source.join(name))); |
| 166 | assert_ne!(original.1, copied.1); |
| 167 | assert_ne!( |
| 168 | again.replica_path("Beta.one").unwrap(), |
| 169 | again.replica_path("Beta 2.one").unwrap(), |
| 170 | "a replica of its own" |
| 171 | ); |
| 172 | assert!(place.source.join("Beta.one").exists()); |
| 173 | } |
| 174 | |
| 175 | #[test] |
| 176 | fn a_section_without_waiting_edits_moves_and_its_replica_goes() { |
| 177 | let place = notebooks(); |
| 178 | let mut source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 179 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 180 | let section = source.section("Beta.one", false, || {}).unwrap(); |
| 181 | let replica = source.replica_path("Beta.one").unwrap(); |
| 182 | assert!(replica.exists()); |
| 183 | let moved = source |
| 184 | .move_section("Beta.one", section, &mut target, "", None) |
| 185 | .unwrap(); |
| 186 | assert!(moved.kept.is_none(), "{:?}", moved.kept); |
| 187 | assert_eq!(titles(&place.target.join(&moved.path)), ["First page"]); |
| 188 | assert!(place.source.join("OneNote_RecycleBin/Beta.one").exists()); |
| 189 | assert!(!replica.exists()); |
| 190 | } |
| 191 | |
| 192 | /// A name iCloud Drive holds for a section it keeps elsewhere is taken, and a copy cut short |
| 193 | /// leaves nothing a notebook lists. |
| 194 | #[test] |
| 195 | fn a_copy_passes_over_a_name_icloud_holds() { |
| 196 | let place = notebooks(); |
| 197 | let source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 198 | std::fs::write(place.target.join(".Beta.one.icloud"), b"").unwrap(); |
| 199 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 200 | let beta = source.section("Beta.one", false, || {}).unwrap(); |
| 201 | let copy = source |
| 202 | .copy_section("Beta.one", beta.replica(), &mut target, "", None) |
| 203 | .unwrap(); |
| 204 | assert_eq!(copy, "Beta 2.one"); |
| 205 | let hidden: Vec<_> = (std::fs::read_dir(&place.target).unwrap()) |
| 206 | .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()) |
| 207 | .filter(|name| name.ends_with(".snowbound")) |
| 208 | .collect(); |
| 209 | assert!(hidden.is_empty(), "{hidden:?}"); |
| 210 | } |
| 211 | |
| 212 | /// A section whose file can't be reached is not copied from its replica, which may be behind. |
| 213 | #[test] |
| 214 | fn a_copy_of_a_section_out_of_reach_is_refused() { |
| 215 | let place = notebooks(); |
| 216 | let source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 217 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 218 | let beta = source.section("Beta.one", true, || {}).unwrap(); |
| 219 | std::fs::rename(place.source.join("Beta.one"), place.source.join("Away.bak")).unwrap(); |
| 220 | assert!( |
| 221 | source |
| 222 | .copy_section("Beta.one", beta.replica(), &mut target, "", None) |
| 223 | .is_err() |
| 224 | ); |
| 225 | assert!(!place.target.join("Beta.one").exists()); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn a_move_that_cannot_reach_the_bin_keeps_the_section_in_both_notebooks() { |
| 230 | let place = notebooks(); |
| 231 | let mut source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 232 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 233 | // The bin can't be made: a file stands where its folder would. |
| 234 | std::fs::write(place.source.join("OneNote_RecycleBin"), b"").unwrap(); |
| 235 | let original = std::fs::read(place.source.join("Alpha.one")).unwrap(); |
| 236 | let section = source.section("Alpha.one", true, || {}).unwrap(); |
| 237 | queue_page(&section, "Queued page"); |
| 238 | let replica = source.replica_path("Alpha.one").unwrap(); |
| 239 | let moved = source |
| 240 | .move_section("Alpha.one", section, &mut target, "", None) |
| 241 | .unwrap(); |
| 242 | assert!(moved.kept.is_some()); |
| 243 | assert_eq!( |
| 244 | titles(&place.target.join(&moved.path)), |
| 245 | ["First page", "Queued page"] |
| 246 | ); |
| 247 | assert_eq!( |
| 248 | std::fs::read(place.source.join("Alpha.one")).unwrap(), |
| 249 | original |
| 250 | ); |
| 251 | assert_eq!( |
| 252 | sections(&Notebook::open(&place.source, &place.cache).unwrap()), |
| 253 | ["New Section 1.one", "Alpha.one", "Beta.one"] |
| 254 | ); |
| 255 | // The edit still waits in the original's replica, to publish there too. |
| 256 | let replica = notebook::Replica::open(replica).unwrap(); |
| 257 | assert_eq!(replica.pending().unwrap().len(), 1); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn a_copy_the_notebook_cannot_list_leaves_nothing_behind() { |
| 262 | let place = notebooks(); |
| 263 | let source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 264 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 265 | let toc = place.target.join("Open Notebook.onetoc2"); |
| 266 | let mut permissions = std::fs::metadata(&toc).unwrap().permissions(); |
| 267 | permissions.set_readonly(true); |
| 268 | std::fs::set_permissions(&toc, permissions).unwrap(); |
| 269 | let alpha = source.section("Alpha.one", false, || {}).unwrap(); |
| 270 | assert!( |
| 271 | source |
| 272 | .copy_section("Alpha.one", alpha.replica(), &mut target, "", None) |
| 273 | .is_err() |
| 274 | ); |
| 275 | assert!(!place.target.join("Alpha.one").exists()); |
| 276 | assert!(place.source.join("Alpha.one").exists()); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn a_move_waits_while_queued_edits_have_a_changed_file_to_rebase_onto() { |
| 281 | let place = notebooks(); |
| 282 | let mut source = Notebook::open(&place.source, &place.cache).unwrap(); |
| 283 | let mut target = Notebook::open(&place.target, &place.cache).unwrap(); |
| 284 | let section = source.section("Alpha.one", true, || {}).unwrap(); |
| 285 | queue_page(&section, "Queued page"); |
| 286 | // Another client's change, which the replica has not read. |
| 287 | source |
| 288 | .set_section_color("Alpha.one", Some(0x5ed7ff)) |
| 289 | .unwrap(); |
| 290 | let error = source |
| 291 | .move_section("Alpha.one", section, &mut target, "", None) |
| 292 | .unwrap_err(); |
| 293 | assert!( |
| 294 | matches!(&error, notebook::Error::Io(error) if error.kind() == std::io::ErrorKind::WouldBlock), |
| 295 | "{error}" |
| 296 | ); |
| 297 | assert!(!place.target.join("Alpha.one").exists()); |
| 298 | assert!(place.source.join("Alpha.one").exists()); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn 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. |
| 348 | fn 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] |
| 368 | fn 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 | } |