| ... | ... | @@ -4486,3 +4486,379 @@ fn a_page_restored_offline_on_a_share_publishes_once_online() { |
| 4486 | 4486 | .is_empty() |
| 4487 | 4487 | ); |
| 4488 | 4488 | } |
| 4489 | |
| 4490 | /// iCloud Drive's conflict versions as the iOS host lists them (`NSFileVersion`): the file |
| 4491 | /// each is of, where its contents are, and the device that saved it. |
| 4492 | static CLOUD_VERSIONS: Mutex<Vec<(PathBuf, PathBuf, String)>> = Mutex::new(Vec::new()); |
| 4493 | |
| 4494 | extern "C" fn cloud_versions( |
| 4495 | path: *const c_char, |
| 4496 | found: extern "C" fn(*mut c_void, *const c_char, *const c_char), |
| 4497 | context: *mut c_void, |
| 4498 | ) { |
| 4499 | let Ok(path) = std::fs::canonicalize(string(path)) else { |
| 4500 | return; |
| 4501 | }; |
| 4502 | for (file, version, device) in CLOUD_VERSIONS.lock().unwrap().iter() { |
| 4503 | if *file == path { |
| 4504 | let id = std::ffi::CString::new(version.as_os_str().as_encoded_bytes()).unwrap(); |
| 4505 | let device = std::ffi::CString::new(device.as_str()).unwrap(); |
| 4506 | found(context, id.as_ptr(), device.as_ptr()); |
| 4507 | } |
| 4508 | } |
| 4509 | } |
| 4510 | |
| 4511 | /// Retires a version as ICloud.swift does, first keeping it beside the file when asked. |
| 4512 | extern "C" fn cloud_retire(path: *const c_char, id: *const c_char, keep: bool) -> bool { |
| 4513 | let (path, id) = (PathBuf::from(string(path)), PathBuf::from(string(id))); |
| 4514 | let mut held = CLOUD_VERSIONS.lock().unwrap(); |
| 4515 | let Some(at) = held.iter().position(|(_, version, _)| *version == id) else { |
| 4516 | return true; |
| 4517 | }; |
| 4518 | let (_, version, device) = held.remove(at); |
| 4519 | if keep { |
| 4520 | let stem = path.file_stem().unwrap().to_string_lossy(); |
| 4521 | std::fs::copy( |
| 4522 | version, |
| 4523 | path.with_file_name(format!("{stem} ({device}).one")), |
| 4524 | ) |
| 4525 | .unwrap(); |
| 4526 | } |
| 4527 | true |
| 4528 | } |
| 4529 | |
| 4530 | /// A notebook opened as `Notebook.open` opens one in iCloud Drive. |
| 4531 | fn cloud_library(root: &Path, cache: &Path) -> Arc<Library> { |
| 4532 | let text = |path: &Path| std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap(); |
| 4533 | let mut error = std::ptr::null_mut(); |
| 4534 | let raw = unsafe { |
| 4535 | library::sb_library_open(text(root).as_ptr(), text(cache).as_ptr(), false, &mut error) |
| 4536 | }; |
| 4537 | assert!(error.is_null() && !raw.is_null()); |
| 4538 | // SAFETY: `sb_library_open` hands over one reference. |
| 4539 | unsafe { Arc::from_raw(raw) } |
| 4540 | } |
| 4541 | |
| 4542 | /// The section at `path`, opened as `Section.open` opens it. |
| 4543 | fn cloud_section(library: &Library, path: &str) -> Box<Section> { |
| 4544 | extern "C" fn wake(_: usize) {} |
| 4545 | let path = std::ffi::CString::new(path).unwrap(); |
| 4546 | let mut error = std::ptr::null_mut(); |
| 4547 | let raw = unsafe { |
| 4548 | library::sb_section_open( |
| 4549 | library, |
| 4550 | path.as_ptr(), |
| 4551 | c"Clover".as_ptr(), |
| 4552 | wake, |
| 4553 | 0, |
| 4554 | &mut error, |
| 4555 | ) |
| 4556 | }; |
| 4557 | assert!(error.is_null() && !raw.is_null()); |
| 4558 | unsafe { Box::from_raw(raw) } |
| 4559 | } |
| 4560 | |
| 4561 | /// The page ids `sb_section_pages` lists, conflict pages after their page. |
| 4562 | fn listed_ids(section: &Section) -> Vec<ExGuid> { |
| 4563 | let json = library::sb_section_pages(section); |
| 4564 | let rows: Vec<serde_json::Value> = |
| 4565 | serde_json::from_str(unsafe { std::ffi::CStr::from_ptr(json) }.to_str().unwrap()).unwrap(); |
| 4566 | unsafe { sb_string_free(json) }; |
| 4567 | rows.iter() |
| 4568 | .flat_map(|row| { |
| 4569 | std::iter::once(&row["id"]) |
| 4570 | .chain(row["versions"].as_array().unwrap().iter().map(|v| &v["id"])) |
| 4571 | }) |
| 4572 | .map(|id| id.as_str().unwrap().parse().unwrap()) |
| 4573 | .collect() |
| 4574 | } |
| 4575 | |
| 4576 | /// A view on page `space`, as `sb_view_new` makes one, in the Reading View. |
| 4577 | fn reading_view(section: &Section, space: ExGuid) -> Option<View> { |
| 4578 | let (page, read_only) = section.shared.page(space).ok()?; |
| 4579 | let mut view = View { |
| 4580 | canvas: ManuallyDrop::new(Canvas::new(space, page, [1206, 2622], 3.0).unwrap()), |
| 4581 | presentation: None, |
| 4582 | frame: Arc::default(), |
| 4583 | section: Arc::clone(&section.shared), |
| 4584 | read_only, |
| 4585 | reload_pending: false, |
| 4586 | local_edits: Arc::new(Mutex::new(LocalEdits::default())), |
| 4587 | }; |
| 4588 | let _ = view.canvas.focus_title(); |
| 4589 | sb_view_set_reading(&mut view, 402.0, &mut [0.0; 2]); |
| 4590 | Some(view) |
| 4591 | } |
| 4592 | |
| 4593 | /// Types `text` on page `space`: below its title in a new outline, or in its first line. |
| 4594 | fn type_on(section: &Section, space: ExGuid, text: &str) { |
| 4595 | let mut canvas = Canvas::new( |
| 4596 | space, |
| 4597 | section.shared.page(space).unwrap().0, |
| 4598 | [1206, 2622], |
| 4599 | 3.0, |
| 4600 | ) |
| 4601 | .unwrap(); |
| 4602 | canvas.press([300.0, 600.0]).unwrap(); |
| 4603 | canvas.release().unwrap(); |
| 4604 | if let Some(edit) = canvas.edit().unwrap() { |
| 4605 | section.shared.apply(edit).unwrap(); |
| 4606 | } |
| 4607 | canvas.insert(text.into()).unwrap(); |
| 4608 | section |
| 4609 | .shared |
| 4610 | .apply(canvas.edit().unwrap().unwrap()) |
| 4611 | .unwrap(); |
| 4612 | } |
| 4613 | |
| 4614 | fn titled(section: &Section, title: &str) -> ExGuid { |
| 4615 | let space = section |
| 4616 | .new_page(None, "Friday, October 16, 2026", "1:45 AM") |
| 4617 | .unwrap(); |
| 4618 | let mut canvas = Canvas::new( |
| 4619 | space, |
| 4620 | section.shared.page(space).unwrap().0, |
| 4621 | [1206, 2622], |
| 4622 | 3.0, |
| 4623 | ) |
| 4624 | .unwrap(); |
| 4625 | canvas.focus_title().unwrap(); |
| 4626 | canvas.insert(title.into()).unwrap(); |
| 4627 | section |
| 4628 | .shared |
| 4629 | .apply(canvas.edit().unwrap().unwrap()) |
| 4630 | .unwrap(); |
| 4631 | space |
| 4632 | } |
| 4633 | |
| 4634 | fn close_section(section: Box<Section>) { |
| 4635 | assert!(section.flush(Duration::from_secs(20))); |
| 4636 | let shared = Arc::clone(&section.shared); |
| 4637 | drop(section); |
| 4638 | Arc::into_inner(shared).unwrap().section.close().unwrap(); |
| 4639 | } |
| 4640 | |
| 4641 | /// Every page and conflict page of every section of the notebook at `root`, as text, |
| 4642 | /// each opened in the page view and the Reading View on the way. |
| 4643 | fn every_page(root: &Path, cache: &Path) -> String { |
| 4644 | let library = cloud_library(root, cache); |
| 4645 | let mut shown = String::new(); |
| 4646 | for tab in library.tabs().unwrap() { |
| 4647 | let section = cloud_section(&library, &tab.path); |
| 4648 | for space in listed_ids(&section) { |
| 4649 | let (page, _) = section.shared.page(space).unwrap(); |
| 4650 | shown += &format!("{}\n{}\n", page.title, canvas::search::page_text(&page)); |
| 4651 | drop(reading_view(&section, space).unwrap()); |
| 4652 | } |
| 4653 | close_section(section); |
| 4654 | } |
| 4655 | shown |
| 4656 | } |
| 4657 | |
| 4658 | /// The phone and the Mac edit one iCloud Drive notebook Snowbound made, each in its own copy: |
| 4659 | /// the same page, a page each adds and moves to the top, a page the Mac deletes as the phone |
| 4660 | /// types on it, and a section the phone adds. The Mac's upload wins, so the phone's next |
| 4661 | /// launch finds its own file a conflict version, and opens, lists and merges it through the C |
| 4662 | /// surface as iOS does, its pages open and typed on in the Reading View all the while. |
| 4663 | /// Nothing either device typed goes missing, merged or set aside whole. |
| 4664 | #[test] |
| 4665 | fn an_icloud_notebook_merges_the_other_devices_version_at_launch() { |
| 4666 | library::sb_set_versions(cloud_versions, cloud_retire); |
| 4667 | // Whether the devices' copies already share an edit of the page both type on. |
| 4668 | for shared in [false, true] { |
| 4669 | let directory = tempfile::tempdir().unwrap(); |
| 4670 | let folder = |name: &str| directory.path().join(name); |
| 4671 | let (phone, mac) = (folder("phone"), folder("mac")); |
| 4672 | let creation = onestore::PageCreation::new(None, Some("Shared page"), "Clover").unwrap(); |
| 4673 | let notebook = notebook::session::Notebook::create( |
| 4674 | &phone, |
| 4675 | folder("phone-cache"), |
| 4676 | 0x00f0_a0c0, |
| 4677 | &creation, |
| 4678 | ) |
| 4679 | .unwrap(); |
| 4680 | let path = notebook.catalog().sections[0].path.clone(); |
| 4681 | drop(notebook); |
| 4682 | let library = cloud_library(&phone, &folder("phone-cache")); |
| 4683 | let section = cloud_section(&library, &path); |
| 4684 | let first = listed_ids(&section)[0]; |
| 4685 | let doomed = titled(&section, "Doomed page"); |
| 4686 | if shared { |
| 4687 | type_on(&section, first, "Typed on both "); |
| 4688 | } |
| 4689 | close_section(section); |
| 4690 | drop(library); |
| 4691 | let copied = std::process::Command::new("cp") |
| 4692 | .arg("-R") |
| 4693 | .arg(&phone) |
| 4694 | .arg(&mac) |
| 4695 | .status(); |
| 4696 | assert!(copied.unwrap().success()); |
| 4697 | |
| 4698 | for device in ["Phone", "Mac"] { |
| 4699 | let (root, cache) = ( |
| 4700 | folder(&device.to_lowercase()), |
| 4701 | folder(&format!("{}-cache", device.to_lowercase())), |
| 4702 | ); |
| 4703 | let library = cloud_library(&root, &cache); |
| 4704 | let section = cloud_section(&library, &path); |
| 4705 | type_on(&section, first, &format!("{device} first words ")); |
| 4706 | type_on(&section, doomed, &format!("{device} on the doomed page ")); |
| 4707 | let added = titled(&section, &format!("{device} page")); |
| 4708 | let moved = onestore::PageEdit::move_to(added, Some(first), 1).unwrap(); |
| 4709 | section |
| 4710 | .shared |
| 4711 | .apply(Edit { |
| 4712 | at: library::filetime(), |
| 4713 | ops: vec![Op::Section(onestore::op::SectionOp::Pages(vec![moved]))], |
| 4714 | }) |
| 4715 | .unwrap(); |
| 4716 | if device == "Mac" { |
| 4717 | section |
| 4718 | .delete_page(doomed, "Friday, October 16, 2026", "8:00 AM") |
| 4719 | .unwrap(); |
| 4720 | } else { |
| 4721 | let made = unsafe { |
| 4722 | library::sb_library_new_section( |
| 4723 | &library, |
| 4724 | c"".as_ptr(), |
| 4725 | c"Phone section".as_ptr(), |
| 4726 | c"Clover".as_ptr(), |
| 4727 | c"Friday, October 16, 2026".as_ptr(), |
| 4728 | c"1:46 AM".as_ptr(), |
| 4729 | ) |
| 4730 | }; |
| 4731 | assert!(!made.is_null()); |
| 4732 | unsafe { sb_string_free(made) }; |
| 4733 | } |
| 4734 | close_section(section); |
| 4735 | library.flush(Duration::from_secs(20)).unwrap(); |
| 4736 | } |
| 4737 | |
| 4738 | let file = phone.join(&path); |
| 4739 | let version = folder("version.one"); |
| 4740 | std::fs::rename(&file, &version).unwrap(); |
| 4741 | std::fs::copy(mac.join(&path), &file).unwrap(); |
| 4742 | let device = "Clover’s iPhone".to_owned(); |
| 4743 | CLOUD_VERSIONS.lock().unwrap().push(( |
| 4744 | std::fs::canonicalize(&file).unwrap(), |
| 4745 | version, |
| 4746 | device, |
| 4747 | )); |
| 4748 | |
| 4749 | // The launch: the notebook opens and lists, the page shown last opens, and the |
| 4750 | // background merges the version meanwhile. |
| 4751 | let library = cloud_library(&phone, &folder("phone-cache")); |
| 4752 | sb_library_set_offline(&library, false); |
| 4753 | let tabs = library::sb_library_sections(&library); |
| 4754 | assert!(!tabs.is_null()); |
| 4755 | unsafe { sb_string_free(tabs) }; |
| 4756 | library::sb_library_color(&library); |
| 4757 | let section = cloud_section(&library, &path); |
| 4758 | let mut views: Vec<View> = listed_ids(&section) |
| 4759 | .into_iter() |
| 4760 | .filter_map(|space| reading_view(&section, space)) |
| 4761 | .collect(); |
| 4762 | sb_library_sync_now(&library); |
| 4763 | let deadline = Instant::now() + Duration::from_secs(10); |
| 4764 | while !CLOUD_VERSIONS.lock().unwrap().is_empty() { |
| 4765 | assert!(Instant::now() < deadline, "the version never merged"); |
| 4766 | library::sb_section_poll(&section, &mut 0); |
| 4767 | for view in &mut views { |
| 4768 | unsafe { sb_insert(view, c"Launch ".as_ptr()) }; |
| 4769 | sb_view_reload(view, false, Some(&mut [0.0; 2])); |
| 4770 | } |
| 4771 | std::thread::sleep(Duration::from_millis(20)); |
| 4772 | } |
| 4773 | library::sb_section_poll(&section, &mut 0); |
| 4774 | for mut view in views { |
| 4775 | sb_view_reload(&mut view, false, Some(&mut [0.0; 2])); |
| 4776 | sb_view_reload(&mut view, true, Some(&mut [0.0; 2])); |
| 4777 | sb_view_set_reading(&mut view, 402.0, &mut [0.0; 2]); |
| 4778 | unsafe { sb_insert(&mut view, c"After ".as_ptr()) }; |
| 4779 | sb_view_keep(&view); |
| 4780 | } |
| 4781 | close_section(section); |
| 4782 | library.flush(Duration::from_secs(20)).unwrap(); |
| 4783 | drop(library); |
| 4784 | |
| 4785 | let shown = every_page(&phone, &folder("phone-cache")); |
| 4786 | for typed in [ |
| 4787 | "Phone first words", |
| 4788 | "Mac first words", |
| 4789 | "Phone page", |
| 4790 | "Mac page", |
| 4791 | "Phone on the doomed page", |
| 4792 | ] { |
| 4793 | assert!(shown.contains(typed), "{typed} is gone:\n{shown}"); |
| 4794 | } |
| 4795 | } |
| 4796 | } |
| 4797 | |
| 4798 | /// What the iOS host calls on the main thread as a launch restores a page returns while the |
| 4799 | /// background waits on a coordinated read of the section, as iCloud Drive holds one until the |
| 4800 | /// file downloads: a main thread that waited would have the system end the app. |
| 4801 | #[test] |
| 4802 | fn main_thread_calls_return_while_icloud_holds_the_section_file() { |
| 4803 | library::sb_set_coordinator(coordinator); |
| 4804 | let directory = tempfile::tempdir().unwrap(); |
| 4805 | let (root, cache) = ( |
| 4806 | directory.path().join("notebook"), |
| 4807 | directory.path().join("cache"), |
| 4808 | ); |
| 4809 | let creation = onestore::PageCreation::new(None, Some("Waiting"), "Clover").unwrap(); |
| 4810 | let notebook = |
| 4811 | notebook::session::Notebook::create(&root, &cache, 0x00f0_a0c0, &creation).unwrap(); |
| 4812 | let path = notebook.catalog().sections[0].path.clone(); |
| 4813 | drop(notebook); |
| 4814 | let library = cloud_library(&root, &cache); |
| 4815 | let section: Arc<Section> = Arc::from(cloud_section(&library, &path)); |
| 4816 | let space = listed_ids(&section)[0]; |
| 4817 | let file = root.canonicalize().unwrap().join(&path); |
| 4818 | let (entered, held) = std::sync::mpsc::channel(); |
| 4819 | let (release, released) = std::sync::mpsc::channel(); |
| 4820 | *HELD.lock().unwrap() = Some((file.to_string_lossy().into_owned(), entered, released)); |
| 4821 | type_on(&section, space, "Typed while iCloud downloads "); |
| 4822 | sb_library_sync_now(&library); |
| 4823 | held.recv_timeout(Duration::from_secs(10)).unwrap(); |
| 4824 | type Call = Box<dyn FnOnce(&Library, Arc<Section>) + Send>; |
| 4825 | let calls: Vec<(&str, Call)> = vec![ |
| 4826 | ( |
| 4827 | "sb_section_poll", |
| 4828 | Box::new(|_, section| _ = library::sb_section_poll(&section, &mut 0)), |
| 4829 | ), |
| 4830 | ( |
| 4831 | "sb_view_new", |
| 4832 | Box::new(move |_, section| _ = section.shared.page(space).unwrap()), |
| 4833 | ), |
| 4834 | ( |
| 4835 | "sb_insert", |
| 4836 | Box::new(move |_, section| type_on(&section, space, "More ")), |
| 4837 | ), |
| 4838 | ( |
| 4839 | "sb_library_set_offline", |
| 4840 | Box::new(|library, _| sb_library_set_offline(library, false)), |
| 4841 | ), |
| 4842 | ("sb_section_free", Box::new(|_, section| drop(section))), |
| 4843 | ]; |
| 4844 | let mut section = Some(section); |
| 4845 | for (name, call) in calls { |
| 4846 | let (done, finished) = std::sync::mpsc::channel(); |
| 4847 | let (library, section) = ( |
| 4848 | Arc::clone(&library), |
| 4849 | match name { |
| 4850 | "sb_section_free" => section.take().unwrap(), |
| 4851 | _ => Arc::clone(section.as_ref().unwrap()), |
| 4852 | }, |
| 4853 | ); |
| 4854 | std::thread::spawn(move || { |
| 4855 | call(&library, section); |
| 4856 | let _ = done.send(()); |
| 4857 | }); |
| 4858 | assert!( |
| 4859 | finished.recv_timeout(Duration::from_secs(4)).is_ok(), |
| 4860 | "{name} waited on iCloud" |
| 4861 | ); |
| 4862 | } |
| 4863 | release.send(()).unwrap(); |
| 4864 | } |