| 1 | //! Devices sharing a section through a cloud drive that, like iCloud Drive, syncs whole files |
| 2 | //! with no lock and no compare-and-swap: an upload made from a copy the server has moved past |
| 3 | //! makes one of the two the file and keeps the other as a conflict version, which every device |
| 4 | //! sees. Each device commits to its own copy through its replica and merges the conflict |
| 5 | //! versions it sees (`Remote::versions`). Random schedules of typing, new paragraphs, new, |
| 6 | //! deleted and moved pages, offline spans and late deliveries must end with every device on |
| 7 | //! the same file, holding each typed string once. |
| 8 | |
| 9 | #[path = "../../onestore/tests/support/sweep.rs"] |
| 10 | mod sweep; |
| 11 | |
| 12 | use notebook::{Remote, Replica, Version}; |
| 13 | use onestore::{ |
| 14 | CommitError, CommitState, ExGuid, PageCreation, PageEdit, Section, Stamp, Transaction, |
| 15 | document::{Format, Layout}, |
| 16 | op::{Edit, Op, PageOp, SectionOp, lower_page}, |
| 17 | page::{ |
| 18 | Outline, Page, PageObject, PageParagraph, Paragraph, ParagraphContent, TextObject, |
| 19 | text::new_id, |
| 20 | }, |
| 21 | }; |
| 22 | use std::{collections::BTreeSet, io}; |
| 23 | |
| 24 | /// The file as the server holds it, and the versions it keeps beside it. |
| 25 | struct Server { |
| 26 | current: Vec<u8>, |
| 27 | /// Names what `current` is, so a device knows whether its copy is. |
| 28 | id: u64, |
| 29 | next: u64, |
| 30 | conflicts: Vec<(u64, Vec<u8>, String)>, |
| 31 | /// Versions a device kept beside the file as files of their own. |
| 32 | kept: usize, |
| 33 | } |
| 34 | |
| 35 | impl Server { |
| 36 | fn next(&mut self) -> u64 { |
| 37 | self.next += 1; |
| 38 | self.next |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// A device's copy of the file and what it has heard of the server. |
| 43 | struct Copy { |
| 44 | bytes: Vec<u8>, |
| 45 | /// The server's file this copy was, before any local commit. |
| 46 | base: u64, |
| 47 | dirty: bool, |
| 48 | /// The conflict versions delivered to this device. |
| 49 | versions: Vec<(u64, Vec<u8>, String)>, |
| 50 | online: bool, |
| 51 | } |
| 52 | |
| 53 | struct Device { |
| 54 | name: String, |
| 55 | copy: Copy, |
| 56 | replica: Replica, |
| 57 | } |
| 58 | |
| 59 | /// A device's copy of the file as its replica publishes to it. |
| 60 | struct Local<'a> { |
| 61 | copy: &'a mut Copy, |
| 62 | server: &'a mut Server, |
| 63 | } |
| 64 | |
| 65 | fn refused() -> CommitError { |
| 66 | CommitError { |
| 67 | state: CommitState::NotCommitted, |
| 68 | error: io::Error::from(io::ErrorKind::WouldBlock), |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl Remote for Local<'_> { |
| 73 | fn read(&mut self) -> io::Result<Vec<u8>> { |
| 74 | Ok(self.copy.bytes.clone()) |
| 75 | } |
| 76 | |
| 77 | fn stamp(&mut self) -> io::Result<Stamp> { |
| 78 | Stamp::of(&self.copy.bytes).map_err(io::Error::other) |
| 79 | } |
| 80 | |
| 81 | fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> { |
| 82 | transaction |
| 83 | .apply(&mut self.copy.bytes) |
| 84 | .map_err(|_| refused())?; |
| 85 | self.copy.dirty = true; |
| 86 | Ok(()) |
| 87 | } |
| 88 | |
| 89 | fn confirm(&mut self, base: &Stamp) -> Result<(), CommitError> { |
| 90 | match Stamp::of(&self.copy.bytes) { |
| 91 | Ok(stamp) if stamp == *base => Ok(()), |
| 92 | _ => Err(refused()), |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn versions(&mut self) -> io::Result<Vec<Version>> { |
| 97 | if self.copy.base != self.server.id { |
| 98 | return Ok(Vec::new()); |
| 99 | } |
| 100 | Ok(self |
| 101 | .copy |
| 102 | .versions |
| 103 | .iter() |
| 104 | .map(|(id, _, device)| Version { |
| 105 | id: id.to_string(), |
| 106 | device: Some(device.clone()), |
| 107 | }) |
| 108 | .collect()) |
| 109 | } |
| 110 | |
| 111 | fn version(&mut self, id: &str) -> io::Result<Vec<u8>> { |
| 112 | self.copy |
| 113 | .versions |
| 114 | .iter() |
| 115 | .find(|(held, ..)| held.to_string() == id) |
| 116 | .map(|(_, bytes, _)| bytes.clone()) |
| 117 | .ok_or_else(|| io::Error::from(io::ErrorKind::NotFound)) |
| 118 | } |
| 119 | |
| 120 | fn retire(&mut self, id: &str, keep: bool) -> io::Result<()> { |
| 121 | self.copy |
| 122 | .versions |
| 123 | .retain(|(held, ..)| held.to_string() != id); |
| 124 | let before = self.server.conflicts.len(); |
| 125 | self.server |
| 126 | .conflicts |
| 127 | .retain(|(held, ..)| held.to_string() != id); |
| 128 | if keep && before != self.server.conflicts.len() { |
| 129 | self.server.kept += 1; |
| 130 | } |
| 131 | Ok(()) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// A small xorshift generator; schedules replay from their seed. |
| 136 | struct Random(u64); |
| 137 | |
| 138 | impl Random { |
| 139 | fn next(&mut self) -> u64 { |
| 140 | self.0 ^= self.0 << 13; |
| 141 | self.0 ^= self.0 >> 7; |
| 142 | self.0 ^= self.0 << 17; |
| 143 | self.0 |
| 144 | } |
| 145 | |
| 146 | fn below(&mut self, n: usize) -> usize { |
| 147 | (self.next() % n.max(1) as u64) as usize |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | fn format() -> Format { |
| 152 | Format { |
| 153 | font: Some("Calibri".to_owned()), |
| 154 | font_size: Some(11.0), |
| 155 | language: Some(0x409), |
| 156 | ..Default::default() |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | fn paragraph(text: &str) -> PageParagraph { |
| 161 | PageParagraph { |
| 162 | id: new_id().unwrap(), |
| 163 | parent: None, |
| 164 | level: 1, |
| 165 | style: None, |
| 166 | format: Default::default(), |
| 167 | content: ParagraphContent::Text(TextObject { |
| 168 | id: new_id().unwrap(), |
| 169 | date_field: None, |
| 170 | text: Paragraph::new(text.into(), format()), |
| 171 | tags: Vec::new(), |
| 172 | }), |
| 173 | lists: Vec::new(), |
| 174 | tags: Vec::new(), |
| 175 | media: Default::default(), |
| 176 | collapsed: false, |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// `page` with a body outline holding one paragraph of `text`. |
| 181 | fn with_body(page: &Page, text: &str) -> Page { |
| 182 | let mut page = page.clone(); |
| 183 | let outline = Outline { |
| 184 | id: new_id().unwrap(), |
| 185 | title: false, |
| 186 | min_width: None, |
| 187 | layout: Layout { |
| 188 | x: Some(36.0), |
| 189 | y: Some(86.0), |
| 190 | ..Default::default() |
| 191 | }, |
| 192 | indents: Vec::new(), |
| 193 | paragraphs: vec![paragraph(text)], |
| 194 | unsupported: Vec::new(), |
| 195 | }; |
| 196 | page.objects.push(PageObject::Outline(outline)); |
| 197 | page |
| 198 | } |
| 199 | |
| 200 | /// A section of three pages, each with a body paragraph. |
| 201 | fn fixture() -> Vec<u8> { |
| 202 | let source = onestore::create_empty_section("cloud.one", None).unwrap(); |
| 203 | let arena = onestore::Arena::default(); |
| 204 | let mut section = Section::open(&arena, source).unwrap(); |
| 205 | for n in 0..3 { |
| 206 | let creation = PageCreation::new(None, Some(&format!("Page {n}")), "Fixture").unwrap(); |
| 207 | let space = creation.space(); |
| 208 | edit( |
| 209 | &mut section, |
| 210 | "Fixture", |
| 211 | vec![Op::Section(SectionOp::Create(creation))], |
| 212 | ); |
| 213 | let page = section.page(space).unwrap(); |
| 214 | let body = with_body(&page, &format!("Body {n} with words")); |
| 215 | let ops = lower_page(&page, &body) |
| 216 | .unwrap() |
| 217 | .into_iter() |
| 218 | .map(|op| Op::Page { space, op }) |
| 219 | .collect(); |
| 220 | edit(&mut section, "Fixture", ops); |
| 221 | } |
| 222 | section.seal().unwrap(); |
| 223 | section.image() |
| 224 | } |
| 225 | |
| 226 | fn edit(section: &mut Section<'_>, author: &str, ops: Vec<Op>) { |
| 227 | section |
| 228 | .apply( |
| 229 | author, |
| 230 | &Edit { |
| 231 | at: 133_000_000_000_000_000, |
| 232 | ops, |
| 233 | }, |
| 234 | ) |
| 235 | .unwrap(); |
| 236 | } |
| 237 | |
| 238 | /// Every body text of `page`: its identity and characters. |
| 239 | fn bodies(page: &Page) -> Vec<(ExGuid, String)> { |
| 240 | page.objects |
| 241 | .iter() |
| 242 | .filter_map(|object| match object { |
| 243 | PageObject::Outline(outline) => Some(outline), |
| 244 | _ => None, |
| 245 | }) |
| 246 | .flat_map(|outline| &outline.paragraphs) |
| 247 | .filter_map(|paragraph| match &paragraph.content { |
| 248 | ParagraphContent::Text(text) => Some((text.id, text.text.text().to_owned())), |
| 249 | _ => None, |
| 250 | }) |
| 251 | .collect() |
| 252 | } |
| 253 | |
| 254 | /// What a schedule typed, and where. |
| 255 | #[derive(Default)] |
| 256 | struct Typed { |
| 257 | /// Each typed string with the page it went into. |
| 258 | strings: Vec<(String, ExGuid)>, |
| 259 | /// Pages some device deleted, whose strings may be gone. |
| 260 | deleted: BTreeSet<ExGuid>, |
| 261 | } |
| 262 | |
| 263 | impl Device { |
| 264 | fn remote<'a>(&'a mut self, server: &'a mut Server) -> (Local<'a>, &'a Replica) { |
| 265 | ( |
| 266 | Local { |
| 267 | copy: &mut self.copy, |
| 268 | server, |
| 269 | }, |
| 270 | &self.replica, |
| 271 | ) |
| 272 | } |
| 273 | |
| 274 | /// Commits what the replica holds and merges what it sees, as the sync thread does. |
| 275 | fn sync(&mut self, server: &mut Server) { |
| 276 | let (mut remote, replica) = self.remote(server); |
| 277 | for _ in 0..8 { |
| 278 | match replica.sync_once(&mut remote) { |
| 279 | Ok(synced) |
| 280 | if matches!( |
| 281 | synced.edit, |
| 282 | Some((_, notebook::EditStatus::Published { .. })) |
| 283 | ) => {} |
| 284 | Ok(_) => return, |
| 285 | Err(notebook::Error::Remote(error)) if error.state == CommitState::NotCommitted => { |
| 286 | } |
| 287 | Err(error) => panic!("{}: {error}", self.name), |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /// Sends a changed copy to the server; one made from a file the server has moved past |
| 293 | /// becomes the file or a conflict version, either way round. |
| 294 | fn upload(&mut self, server: &mut Server, random: &mut Random) { |
| 295 | if !self.copy.online || !self.copy.dirty { |
| 296 | return; |
| 297 | } |
| 298 | self.copy.dirty = false; |
| 299 | if self.copy.base == server.id { |
| 300 | server.current = self.copy.bytes.clone(); |
| 301 | server.id = server.next(); |
| 302 | self.copy.base = server.id; |
| 303 | return; |
| 304 | } |
| 305 | let id = server.next(); |
| 306 | if random.below(2) == 0 { |
| 307 | // This copy becomes the file; the server's goes beside it. |
| 308 | let lost = std::mem::replace(&mut server.current, self.copy.bytes.clone()); |
| 309 | server.conflicts.push((id, lost, "The server".into())); |
| 310 | server.id = server.next(); |
| 311 | self.copy.base = server.id; |
| 312 | } else { |
| 313 | server |
| 314 | .conflicts |
| 315 | .push((id, self.copy.bytes.clone(), self.name.clone())); |
| 316 | self.copy.bytes = server.current.clone(); |
| 317 | self.copy.base = server.id; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /// Takes the server's file, or conflicts with it while this copy has changes. |
| 322 | fn download(&mut self, server: &mut Server, random: &mut Random) { |
| 323 | if !self.copy.online || self.copy.base == server.id { |
| 324 | return; |
| 325 | } |
| 326 | if self.copy.dirty { |
| 327 | return self.upload(server, random); |
| 328 | } |
| 329 | self.copy.bytes = server.current.clone(); |
| 330 | self.copy.base = server.id; |
| 331 | } |
| 332 | |
| 333 | /// Learns which conflict versions the server keeps. |
| 334 | fn deliver(&mut self, server: &Server) { |
| 335 | if self.copy.online { |
| 336 | self.copy.versions = server.conflicts.clone(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /// Makes a random edit, as someone using the device would. |
| 341 | fn edit(&mut self, random: &mut Random, typed: &mut Typed, serial: &mut u32) { |
| 342 | let pages = self.replica.pages().unwrap(); |
| 343 | let (space, ..) = pages[random.below(pages.len())].clone(); |
| 344 | let page = self.replica.page(space).unwrap(); |
| 345 | *serial += 1; |
| 346 | let token = format!("⟨{}-{serial}⟩", self.name); |
| 347 | let at = crate_now(); |
| 348 | let ops = match random.below(20) { |
| 349 | 0..=12 => { |
| 350 | let texts = bodies(&page); |
| 351 | if texts.is_empty() { |
| 352 | return; |
| 353 | } |
| 354 | let (text, characters) = &texts[random.below(texts.len())]; |
| 355 | // Anywhere but inside another typed string, so each stays whole to count. |
| 356 | let mut inside = false; |
| 357 | let mut boundaries = Vec::new(); |
| 358 | for (at, character) in characters.char_indices() { |
| 359 | if !inside { |
| 360 | boundaries.push(at); |
| 361 | } |
| 362 | inside = (inside || character == '⟨') && character != '⟩'; |
| 363 | } |
| 364 | boundaries.push(characters.len()); |
| 365 | let byte = boundaries[random.below(boundaries.len())]; |
| 366 | let offset = characters[..byte].encode_utf16().count() as u32; |
| 367 | vec![Op::Page { |
| 368 | space, |
| 369 | op: PageOp::Text { |
| 370 | text: *text, |
| 371 | range: offset..offset, |
| 372 | with: token.clone(), |
| 373 | }, |
| 374 | }] |
| 375 | } |
| 376 | 13..=15 => { |
| 377 | let mut after = page.clone(); |
| 378 | let Some(outline) = after.objects.iter_mut().find_map(|object| match object { |
| 379 | PageObject::Outline(outline) => Some(outline), |
| 380 | _ => None, |
| 381 | }) else { |
| 382 | return; |
| 383 | }; |
| 384 | let top: Vec<usize> = (0..outline.paragraphs.len()) |
| 385 | .filter(|at| outline.paragraphs[*at].parent.is_none()) |
| 386 | .collect(); |
| 387 | let at = top[random.below(top.len())]; |
| 388 | outline.paragraphs.insert(at + 1, paragraph(&token)); |
| 389 | lower_page(&page, &after) |
| 390 | .unwrap() |
| 391 | .into_iter() |
| 392 | .map(|op| Op::Page { space, op }) |
| 393 | .collect() |
| 394 | } |
| 395 | 16 => { |
| 396 | // First or last, as a series starts at the first page. |
| 397 | let before = (random.below(2) == 0).then_some(pages[0].0); |
| 398 | let creation = PageCreation::new(before, Some(&token), &self.name).unwrap(); |
| 399 | let space = creation.space(); |
| 400 | return self.apply( |
| 401 | at, |
| 402 | vec![Op::Section(SectionOp::Create(creation))], |
| 403 | typed, |
| 404 | (token, space), |
| 405 | ); |
| 406 | } |
| 407 | 17 if pages.len() > 1 => { |
| 408 | typed.deleted.insert(space); |
| 409 | vec![Op::Section(SectionOp::Delete(vec![space]))] |
| 410 | } |
| 411 | _ => { |
| 412 | let before = pages |
| 413 | .get(random.below(pages.len() + 1)) |
| 414 | .map(|page| page.0) |
| 415 | .filter(|before| *before != space); |
| 416 | let first = pages[0].0; |
| 417 | // A page going first stays at the top level. |
| 418 | let level = if before == Some(first) { |
| 419 | 1 |
| 420 | } else { |
| 421 | 1 + random.below(2) as u32 |
| 422 | }; |
| 423 | vec![Op::Section(SectionOp::Pages(vec![ |
| 424 | PageEdit::move_to(space, before, level).unwrap(), |
| 425 | ]))] |
| 426 | } |
| 427 | }; |
| 428 | self.apply(at, ops, typed, (token, space)); |
| 429 | } |
| 430 | |
| 431 | /// Applies an edit, noting the string it typed once the section takes it. |
| 432 | fn apply(&self, at: u64, ops: Vec<Op>, typed: &mut Typed, token: (String, ExGuid)) { |
| 433 | let text = matches!( |
| 434 | ops.first(), |
| 435 | Some(Op::Page { .. } | Op::Section(SectionOp::Create(_))) |
| 436 | ); |
| 437 | match self.replica.apply(&self.name, Edit { at, ops }) { |
| 438 | Ok(_) if text => typed.strings.push(token), |
| 439 | Ok(_) => {} |
| 440 | Err(notebook::Error::Rejected(error)) if !text => { |
| 441 | let _ = error; |
| 442 | } |
| 443 | Err(error) => panic!("{}: {error}", self.name), |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | fn crate_now() -> u64 { |
| 449 | let unix = std::time::SystemTime::now() |
| 450 | .duration_since(std::time::UNIX_EPOCH) |
| 451 | .unwrap(); |
| 452 | (unix.as_secs() + 11_644_473_600) * 10_000_000 |
| 453 | } |
| 454 | |
| 455 | /// Every text on the section's pages, and on its conflict pages. |
| 456 | fn texts_of(image: &[u8]) -> (String, String) { |
| 457 | let arena = onestore::Arena::default(); |
| 458 | let mut section = Section::open(&arena, image.to_vec()).unwrap(); |
| 459 | let mut listed = String::new(); |
| 460 | for (space, title, _) in section.pages().unwrap() { |
| 461 | listed += &title; |
| 462 | listed += "\n"; |
| 463 | for (_, text) in bodies(&section.page(space).unwrap()) { |
| 464 | listed += &text; |
| 465 | listed += "\n"; |
| 466 | } |
| 467 | } |
| 468 | let mut conflicts = String::new(); |
| 469 | for (_, pages) in section.conflicts().unwrap() { |
| 470 | for conflict in pages { |
| 471 | let page = section.page(conflict.space).unwrap(); |
| 472 | conflicts += &page.title; |
| 473 | for (_, text) in bodies(&page) { |
| 474 | conflicts += &text; |
| 475 | conflicts += "\n"; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | (listed, conflicts) |
| 480 | } |
| 481 | |
| 482 | /// Runs a schedule of `events` among `count` devices from `seed`, then lets every device |
| 483 | /// come online until nothing changes, and checks where they ended. |
| 484 | fn run(seed: u64, count: usize, events: usize) { |
| 485 | let source = fixture(); |
| 486 | let directory = tempfile::tempdir().unwrap(); |
| 487 | let mut server = Server { |
| 488 | current: source.clone(), |
| 489 | id: 0, |
| 490 | next: 0, |
| 491 | conflicts: Vec::new(), |
| 492 | kept: 0, |
| 493 | }; |
| 494 | let mut devices: Vec<Device> = (0..count) |
| 495 | .map(|n| Device { |
| 496 | name: format!("D{n}"), |
| 497 | copy: Copy { |
| 498 | bytes: source.clone(), |
| 499 | base: 0, |
| 500 | dirty: false, |
| 501 | versions: Vec::new(), |
| 502 | online: true, |
| 503 | }, |
| 504 | replica: Replica::create(directory.path().join(format!("{n}.sqlite")), &source) |
| 505 | .unwrap(), |
| 506 | }) |
| 507 | .collect(); |
| 508 | let mut random = Random(seed.wrapping_mul(0x9e37_79b9_7f4a_7c15) | 1); |
| 509 | let mut typed = Typed::default(); |
| 510 | let mut serial = 0; |
| 511 | let trace = std::env::var("CLOUD_TRACE").ok(); |
| 512 | for event in 0..events { |
| 513 | let index = random.below(count); |
| 514 | let device = &mut devices[index]; |
| 515 | let kind = random.below(12); |
| 516 | match kind { |
| 517 | 0..=3 => device.edit(&mut random, &mut typed, &mut serial), |
| 518 | 4..=5 => device.sync(&mut server), |
| 519 | 6..=7 => device.upload(&mut server, &mut random), |
| 520 | 8 => device.download(&mut server, &mut random), |
| 521 | 9 => device.deliver(&server), |
| 522 | _ => device.copy.online = !device.copy.online, |
| 523 | } |
| 524 | if let Some(token) = &trace { |
| 525 | report( |
| 526 | token, |
| 527 | &format!("{event} D{index} {kind}"), |
| 528 | &server, |
| 529 | &devices, |
| 530 | ); |
| 531 | } |
| 532 | } |
| 533 | // Everyone online until a whole round changes nothing. |
| 534 | for device in &mut devices { |
| 535 | device.copy.online = true; |
| 536 | } |
| 537 | let mut rounds = 0; |
| 538 | loop { |
| 539 | rounds += 1; |
| 540 | assert!(rounds < 64, "seed {seed}: no quiescence"); |
| 541 | let before = (server.id, server.conflicts.len()); |
| 542 | for device in 0..devices.len() { |
| 543 | devices[device].download(&mut server, &mut random); |
| 544 | devices[device].deliver(&server); |
| 545 | devices[device].sync(&mut server); |
| 546 | devices[device].upload(&mut server, &mut random); |
| 547 | if let Some(token) = &trace { |
| 548 | report( |
| 549 | token, |
| 550 | &format!("round {rounds} D{device}"), |
| 551 | &server, |
| 552 | &devices, |
| 553 | ); |
| 554 | } |
| 555 | } |
| 556 | let settled = server.conflicts.is_empty() |
| 557 | && (server.id, server.conflicts.len()) == before |
| 558 | && devices.iter().all(|device| { |
| 559 | !device.copy.dirty |
| 560 | && device.copy.base == server.id |
| 561 | && device.replica.pending().unwrap().is_empty() |
| 562 | }); |
| 563 | if settled { |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | for device in &devices { |
| 568 | assert_eq!( |
| 569 | device.copy.bytes, server.current, |
| 570 | "seed {seed}: {}", |
| 571 | device.name |
| 572 | ); |
| 573 | } |
| 574 | assert_eq!(server.kept, 0, "seed {seed}: a version could not merge"); |
| 575 | let (listed, conflicts) = texts_of(&server.current); |
| 576 | for (string, space) in &typed.strings { |
| 577 | // A page deleted on one device while another typed into it comes back as a copy, one |
| 578 | // for the version merged and one for the edits queued since, if both hold edits. |
| 579 | if typed.deleted.contains(space) { |
| 580 | continue; |
| 581 | } |
| 582 | let shown = listed.matches(string.as_str()).count(); |
| 583 | assert!( |
| 584 | shown <= 1, |
| 585 | "seed {seed}: {string} shows {shown} times:\n{listed}" |
| 586 | ); |
| 587 | assert!( |
| 588 | shown == 1 || conflicts.contains(string.as_str()), |
| 589 | "seed {seed}: {string} is gone:\n{listed}\n--\n{conflicts}" |
| 590 | ); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | #[test] |
| 595 | fn devices_on_a_cloud_drive_converge_with_every_edit_once() { |
| 596 | for seed in sweep::seeds(0..200, 60) { |
| 597 | run(seed, 3, 60); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | #[test] |
| 602 | fn two_devices_merging_one_version_at_once_add_it_once() { |
| 603 | let source = fixture(); |
| 604 | let directory = tempfile::tempdir().unwrap(); |
| 605 | let mut server = Server { |
| 606 | current: source.clone(), |
| 607 | id: 0, |
| 608 | next: 0, |
| 609 | conflicts: Vec::new(), |
| 610 | kept: 0, |
| 611 | }; |
| 612 | let mut devices: Vec<Device> = (0..3) |
| 613 | .map(|n| Device { |
| 614 | name: format!("D{n}"), |
| 615 | copy: Copy { |
| 616 | bytes: source.clone(), |
| 617 | base: 0, |
| 618 | dirty: false, |
| 619 | versions: Vec::new(), |
| 620 | online: true, |
| 621 | }, |
| 622 | replica: Replica::create(directory.path().join(format!("{n}.sqlite")), &source) |
| 623 | .unwrap(), |
| 624 | }) |
| 625 | .collect(); |
| 626 | let space = devices[0].replica.pages().unwrap()[0].0; |
| 627 | let type_at = |device: &Device, token: &str| { |
| 628 | let page = device.replica.page(space).unwrap(); |
| 629 | let (text, _) = bodies(&page)[0].clone(); |
| 630 | device |
| 631 | .replica |
| 632 | .apply( |
| 633 | &device.name, |
| 634 | Edit { |
| 635 | at: crate_now(), |
| 636 | ops: vec![Op::Page { |
| 637 | space, |
| 638 | op: PageOp::Text { |
| 639 | text, |
| 640 | range: 0..0, |
| 641 | with: token.into(), |
| 642 | }, |
| 643 | }], |
| 644 | }, |
| 645 | ) |
| 646 | .unwrap(); |
| 647 | }; |
| 648 | // D0 and D1 edit the same page; D1's upload loses and becomes a conflict version. |
| 649 | type_at(&devices[0], "⟨zero⟩"); |
| 650 | type_at(&devices[1], "⟨one⟩"); |
| 651 | let mut random = Random(1); |
| 652 | for device in &mut devices[..2] { |
| 653 | device.sync(&mut server); |
| 654 | } |
| 655 | devices[0].upload(&mut server, &mut random); |
| 656 | devices[1].upload(&mut server, &mut random); |
| 657 | assert_eq!(server.conflicts.len(), 1); |
| 658 | // D0 and D2 both see the version and merge it before either hears the other did. |
| 659 | for device in [0, 2] { |
| 660 | devices[device].download(&mut server, &mut random); |
| 661 | devices[device].deliver(&server); |
| 662 | } |
| 663 | for device in [0, 2] { |
| 664 | devices[device].sync(&mut server); |
| 665 | } |
| 666 | assert!(server.conflicts.is_empty()); |
| 667 | devices[0].upload(&mut server, &mut random); |
| 668 | // D2's merge lands on the file D0's already changed: a conflict version again. |
| 669 | devices[2].upload(&mut server, &mut random); |
| 670 | assert_eq!(server.conflicts.len(), 1); |
| 671 | for _ in 0..8 { |
| 672 | for device in &mut devices { |
| 673 | device.download(&mut server, &mut random); |
| 674 | device.deliver(&server); |
| 675 | device.sync(&mut server); |
| 676 | device.upload(&mut server, &mut random); |
| 677 | } |
| 678 | } |
| 679 | assert!(server.conflicts.is_empty()); |
| 680 | let (listed, conflicts) = texts_of(&server.current); |
| 681 | for token in ["⟨zero⟩", "⟨one⟩"] { |
| 682 | let shown = listed.matches(token).count() + conflicts.matches(token).count(); |
| 683 | assert!( |
| 684 | (1..=2).contains(&shown) && listed.matches(token).count() <= 1, |
| 685 | "{token} shows {shown} times:\n{listed}\n--\n{conflicts}" |
| 686 | ); |
| 687 | } |
| 688 | let arena = onestore::Arena::default(); |
| 689 | let mut section = Section::open(&arena, server.current.clone()).unwrap(); |
| 690 | let conflict_pages: usize = section |
| 691 | .conflicts() |
| 692 | .unwrap() |
| 693 | .iter() |
| 694 | .map(|(_, pages)| pages.len()) |
| 695 | .sum(); |
| 696 | assert!(conflict_pages <= 1, "{conflict_pages} conflict pages"); |
| 697 | } |
| 698 | |
| 699 | /// A version of a OneNote 2010 file merges into it: an edit to another paragraph replays, a |
| 700 | /// page the version made comes along, and an edit both made at one place becomes a conflict |
| 701 | /// page. `ONESTORE_ICLOUD_EXPORT` names a folder to write the merged notebook to, for a cold |
| 702 | /// read in OneNote 2010. |
| 703 | #[test] |
| 704 | fn a_version_of_a_onenote_file_merges_into_it() { |
| 705 | let notebook = format!( |
| 706 | "{}/../../corpus/conflict-page/native/initial/notebook", |
| 707 | env!("CARGO_MANIFEST_DIR") |
| 708 | ); |
| 709 | let source = std::fs::read(format!("{notebook}/synthetic.one")).unwrap(); |
| 710 | let directory = tempfile::tempdir().unwrap(); |
| 711 | let mut server = Server { |
| 712 | current: source.clone(), |
| 713 | id: 0, |
| 714 | next: 0, |
| 715 | conflicts: Vec::new(), |
| 716 | kept: 0, |
| 717 | }; |
| 718 | let mut devices: Vec<Device> = ["Mac", "iPhone"] |
| 719 | .into_iter() |
| 720 | .enumerate() |
| 721 | .map(|(n, name)| Device { |
| 722 | name: name.into(), |
| 723 | copy: Copy { |
| 724 | bytes: source.clone(), |
| 725 | base: 0, |
| 726 | dirty: false, |
| 727 | versions: Vec::new(), |
| 728 | online: true, |
| 729 | }, |
| 730 | replica: Replica::create(directory.path().join(format!("{n}.sqlite")), &source) |
| 731 | .unwrap(), |
| 732 | }) |
| 733 | .collect(); |
| 734 | let space = devices[0].replica.pages().unwrap()[0].0; |
| 735 | let texts = bodies(&devices[0].replica.page(space).unwrap()); |
| 736 | let (first, second) = (texts[0].clone(), texts[1].clone()); |
| 737 | let typing = |text: ExGuid, at: u32, with: &str| Op::Page { |
| 738 | space, |
| 739 | op: PageOp::Text { |
| 740 | text, |
| 741 | range: at..at, |
| 742 | with: with.into(), |
| 743 | }, |
| 744 | }; |
| 745 | let apply = |device: &Device, ops: Vec<Op>| { |
| 746 | device |
| 747 | .replica |
| 748 | .apply( |
| 749 | &device.name, |
| 750 | Edit { |
| 751 | at: crate_now(), |
| 752 | ops, |
| 753 | }, |
| 754 | ) |
| 755 | .unwrap(); |
| 756 | }; |
| 757 | apply(&devices[0], vec![typing(first.0, 0, "Mac: ")]); |
| 758 | let end = second.1.encode_utf16().count() as u32; |
| 759 | apply( |
| 760 | &devices[1], |
| 761 | vec![ |
| 762 | typing(first.0, 0, "iPhone: "), |
| 763 | typing(second.0, end, " (typed on the iPhone)"), |
| 764 | ], |
| 765 | ); |
| 766 | let creation = PageCreation::new(None, Some("From the iPhone"), "iPhone").unwrap(); |
| 767 | let made = creation.space(); |
| 768 | apply(&devices[1], vec![Op::Section(SectionOp::Create(creation))]); |
| 769 | let page = devices[1].replica.page(made).unwrap(); |
| 770 | let body = with_body(&page, "Written offline on the iPhone."); |
| 771 | apply( |
| 772 | &devices[1], |
| 773 | lower_page(&page, &body) |
| 774 | .unwrap() |
| 775 | .into_iter() |
| 776 | .map(|op| Op::Page { space: made, op }) |
| 777 | .collect(), |
| 778 | ); |
| 779 | let mut random = Random(3); |
| 780 | for device in &mut devices { |
| 781 | device.sync(&mut server); |
| 782 | device.upload(&mut server, &mut random); |
| 783 | } |
| 784 | assert_eq!(server.conflicts.len(), 1); |
| 785 | let version = server.conflicts[0].clone(); |
| 786 | for _ in 0..6 { |
| 787 | for device in &mut devices { |
| 788 | device.download(&mut server, &mut random); |
| 789 | device.deliver(&server); |
| 790 | device.sync(&mut server); |
| 791 | device.upload(&mut server, &mut random); |
| 792 | } |
| 793 | } |
| 794 | assert!(server.conflicts.is_empty()); |
| 795 | assert!( |
| 796 | devices |
| 797 | .iter() |
| 798 | .all(|device| device.copy.bytes == server.current) |
| 799 | ); |
| 800 | // The version once more, as a device that has not heard it was resolved sees it: merged |
| 801 | // already, it adds nothing. |
| 802 | devices[1].copy.versions = vec![version]; |
| 803 | devices[1].sync(&mut server); |
| 804 | assert!(!devices[1].copy.dirty && devices[1].copy.versions.is_empty()); |
| 805 | let (listed, conflicts) = texts_of(&server.current); |
| 806 | assert!(listed.contains("(typed on the iPhone)"), "{listed}"); |
| 807 | assert!( |
| 808 | listed.contains("Written offline on the iPhone."), |
| 809 | "{listed}" |
| 810 | ); |
| 811 | let arena = onestore::Arena::default(); |
| 812 | let merged = Section::open(&arena, server.current.clone()).unwrap(); |
| 813 | let shown: String = bodies(&merged.page(space).unwrap()) |
| 814 | .into_iter() |
| 815 | .map(|(_, text)| text + "\n") |
| 816 | .collect(); |
| 817 | assert_eq!( |
| 818 | shown.matches("Mac: ").count() + shown.matches("iPhone: ").count(), |
| 819 | 1, |
| 820 | "{shown}" |
| 821 | ); |
| 822 | assert!( |
| 823 | conflicts.contains("Mac: ") || conflicts.contains("iPhone: "), |
| 824 | "{conflicts}" |
| 825 | ); |
| 826 | if let Some(output) = std::env::var_os("ONESTORE_ICLOUD_EXPORT") { |
| 827 | let output = std::path::Path::new(&output); |
| 828 | std::fs::create_dir_all(output).unwrap(); |
| 829 | std::fs::write(output.join("synthetic.one"), &server.current).unwrap(); |
| 830 | std::fs::copy( |
| 831 | format!("{notebook}/Open Notebook.onetoc2"), |
| 832 | output.join("Open Notebook.onetoc2"), |
| 833 | ) |
| 834 | .unwrap(); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /// Where `token` stands after `label`, for tracing a schedule (`CLOUD_TRACE`). |
| 839 | fn report(token: &str, label: &str, server: &Server, devices: &[Device]) { |
| 840 | let has = |image: &[u8]| { |
| 841 | let (listed, conflicts) = texts_of(image); |
| 842 | format!( |
| 843 | "{}{}", |
| 844 | listed.matches(token).count(), |
| 845 | conflicts.matches(token).count() |
| 846 | ) |
| 847 | }; |
| 848 | let mut line = format!("{label}: server {}", has(&server.current)); |
| 849 | for (id, bytes, _) in &server.conflicts { |
| 850 | line += &format!(" v{id}:{}", has(bytes)); |
| 851 | } |
| 852 | for device in devices { |
| 853 | let working: String = device |
| 854 | .replica |
| 855 | .pages() |
| 856 | .unwrap() |
| 857 | .iter() |
| 858 | .map(|(space, title, _)| { |
| 859 | title.clone() |
| 860 | + &bodies(&device.replica.page(*space).unwrap()) |
| 861 | .into_iter() |
| 862 | .map(|(_, text)| text) |
| 863 | .collect::<String>() |
| 864 | }) |
| 865 | .collect(); |
| 866 | line += &format!( |
| 867 | " | {} copy {} base {} dirty {} replica {} pending {}", |
| 868 | device.name, |
| 869 | has(&device.copy.bytes), |
| 870 | device.copy.base, |
| 871 | u8::from(device.copy.dirty), |
| 872 | working.matches(token).count(), |
| 873 | device.replica.pending().unwrap().len() |
| 874 | ); |
| 875 | } |
| 876 | eprintln!("{line}"); |
| 877 | } |
| 878 | |
| 879 | /// A section as Snowbound makes it, `count` titled pages no one has edited yet, and its pages. |
| 880 | fn untouched(count: usize) -> (Vec<u8>, Vec<(ExGuid, Page)>) { |
| 881 | let source = onestore::create_empty_section("first.one", None).unwrap(); |
| 882 | let arena = onestore::Arena::default(); |
| 883 | let mut section = Section::open(&arena, source).unwrap(); |
| 884 | for n in 0..count { |
| 885 | let creation = PageCreation::new(None, Some(&format!("Page {n}")), "Fixture").unwrap(); |
| 886 | edit( |
| 887 | &mut section, |
| 888 | "Fixture", |
| 889 | vec![Op::Section(SectionOp::Create(creation))], |
| 890 | ); |
| 891 | } |
| 892 | section.seal().unwrap(); |
| 893 | let pages = section |
| 894 | .pages() |
| 895 | .unwrap() |
| 896 | .into_iter() |
| 897 | .map(|(space, ..)| (space, section.page(space).unwrap())) |
| 898 | .collect(); |
| 899 | (section.image(), pages) |
| 900 | } |
| 901 | |
| 902 | const DEVICES: [&str; 2] = ["Mac", "iPhone"]; |
| 903 | |
| 904 | /// The copies two devices make of `source`, each publishing its edit to its own. |
| 905 | fn diverged(source: &[u8], edits: &[Vec<Op>; 2]) -> [Vec<u8>; 2] { |
| 906 | [0, 1].map(|n| { |
| 907 | let arena = onestore::Arena::default(); |
| 908 | let mut section = Section::open(&arena, source.to_vec()).unwrap(); |
| 909 | edit(&mut section, DEVICES[n], edits[n].clone()); |
| 910 | section.seal().unwrap(); |
| 911 | section.image() |
| 912 | }) |
| 913 | } |
| 914 | |
| 915 | /// The file once the device whose copy it is has merged the other's, a conflict version. |
| 916 | fn merged(copies: &[Vec<u8>; 2], winner: usize) -> Vec<u8> { |
| 917 | let directory = tempfile::tempdir().unwrap(); |
| 918 | let file = &copies[winner]; |
| 919 | let mut server = Server { |
| 920 | current: file.clone(), |
| 921 | id: 1, |
| 922 | next: 1, |
| 923 | conflicts: vec![(2, copies[1 - winner].clone(), DEVICES[1 - winner].into())], |
| 924 | kept: 0, |
| 925 | }; |
| 926 | let mut device = Device { |
| 927 | name: DEVICES[winner].into(), |
| 928 | copy: Copy { |
| 929 | bytes: file.clone(), |
| 930 | base: 1, |
| 931 | dirty: false, |
| 932 | versions: Vec::new(), |
| 933 | online: true, |
| 934 | }, |
| 935 | replica: Replica::create(directory.path().join("replica.sqlite"), file).unwrap(), |
| 936 | }; |
| 937 | device.deliver(&server); |
| 938 | device.sync(&mut server); |
| 939 | assert!(server.conflicts.is_empty()); |
| 940 | assert_eq!(server.kept, 0, "the version was set aside whole"); |
| 941 | device.copy.bytes |
| 942 | } |
| 943 | |
| 944 | fn title(page: &Page) -> &TextObject { |
| 945 | page.objects |
| 946 | .iter() |
| 947 | .find_map(|object| match object { |
| 948 | PageObject::Title(title) => match &title.outlines[0].paragraphs[0].content { |
| 949 | ParagraphContent::Text(text) => Some(text), |
| 950 | _ => None, |
| 951 | }, |
| 952 | _ => None, |
| 953 | }) |
| 954 | .unwrap() |
| 955 | } |
| 956 | |
| 957 | fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op { |
| 958 | Op::Page { |
| 959 | space: *space, |
| 960 | op: PageOp::Text { |
| 961 | text: title(page).id, |
| 962 | range: 0..0, |
| 963 | with: with.into(), |
| 964 | }, |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /// The `n`th of a run of edits that type "x" at the start of a page's title and take it out. |
| 969 | fn toggling(page: &(ExGuid, Page), n: usize) -> Vec<Op> { |
| 970 | let mut op = typing_title(page, "x"); |
| 971 | if let Op::Page { |
| 972 | op: PageOp::Text { range, with, .. }, |
| 973 | .. |
| 974 | } = &mut op |
| 975 | && n % 2 == 1 |
| 976 | { |
| 977 | *range = 0..1; |
| 978 | with.clear(); |
| 979 | } |
| 980 | vec![op] |
| 981 | } |
| 982 | |
| 983 | /// The ops adding a body of `text` to a page. |
| 984 | fn body((space, page): &(ExGuid, Page), text: &str) -> Vec<Op> { |
| 985 | lower_page(page, &with_body(page, text)) |
| 986 | .unwrap() |
| 987 | .into_iter() |
| 988 | .map(|op| Op::Page { space: *space, op }) |
| 989 | .collect() |
| 990 | } |
| 991 | |
| 992 | /// `image` with each of `edits` sealed as a revision of its own. |
| 993 | fn sealed(image: Vec<u8>, author: &str, edits: impl IntoIterator<Item = Vec<Op>>) -> Vec<u8> { |
| 994 | let arena = onestore::Arena::default(); |
| 995 | let mut section = Section::open(&arena, image).unwrap(); |
| 996 | for ops in edits { |
| 997 | edit(&mut section, author, ops); |
| 998 | section.seal().unwrap(); |
| 999 | } |
| 1000 | section.image() |
| 1001 | } |
| 1002 | |
| 1003 | /// Revisions in the dependency chain of `space`'s newest revision in `image`. |
| 1004 | fn depth(image: &[u8], space: ExGuid) -> usize { |
| 1005 | let store = onestore::Store::parse(image).unwrap(); |
| 1006 | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 1007 | let revisions = &index.spaces[&space].revisions; |
| 1008 | std::iter::successors(index.active(space).ok(), |rid| revisions[rid].dependency).count() |
| 1009 | } |
| 1010 | |
| 1011 | /// `pages` as `image` holds them. |
| 1012 | fn reread(image: &[u8], pages: &[(ExGuid, Page)]) -> Vec<(ExGuid, Page)> { |
| 1013 | let arena = onestore::Arena::default(); |
| 1014 | let section = Section::open(&arena, image.to_vec()).unwrap(); |
| 1015 | pages |
| 1016 | .iter() |
| 1017 | .map(|(space, _)| (*space, section.page(*space).unwrap())) |
| 1018 | .collect() |
| 1019 | } |
| 1020 | |
| 1021 | /// `untouched(count)` with its first page's chain `deep` revisions long. |
| 1022 | fn deep(count: usize, deep: usize) -> (Vec<u8>, Vec<(ExGuid, Page)>) { |
| 1023 | let (source, pages) = untouched(count); |
| 1024 | let runs = deep - depth(&source, pages[0].0); |
| 1025 | let source = sealed(source, "Fixture", (0..runs).map(|n| toggling(&pages[0], n))); |
| 1026 | let pages = reread(&source, &pages); |
| 1027 | (source, pages) |
| 1028 | } |
| 1029 | |
| 1030 | /// Issue #152: the first edits two devices make to a page merge as edits, not as a version |
| 1031 | /// set aside. |
| 1032 | #[test] |
| 1033 | fn first_edits_on_two_devices_merge_as_edits() { |
| 1034 | let (source, pages) = untouched(3); |
| 1035 | let (space, page) = &pages[0]; |
| 1036 | let body = lower_page(page, &with_body(page, "Typed on the iPhone.")) |
| 1037 | .unwrap() |
| 1038 | .into_iter() |
| 1039 | .map(|op| Op::Page { space: *space, op }) |
| 1040 | .collect(); |
| 1041 | let edits = [vec![typing_title(&pages[0], "Mac: ")], body]; |
| 1042 | for winner in 0..2 { |
| 1043 | let image = merged(&diverged(&source, &edits), winner); |
| 1044 | let (listed, conflicts) = texts_of(&image); |
| 1045 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); |
| 1046 | assert!( |
| 1047 | listed.starts_with("Mac: Page 0\nTyped on the iPhone.\n"), |
| 1048 | "{winner}: {listed}" |
| 1049 | ); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /// First edits that clash still leave OneNote's conflict page. |
| 1054 | #[test] |
| 1055 | fn clashing_first_edits_leave_a_conflict_page() { |
| 1056 | let (source, pages) = untouched(3); |
| 1057 | let typed = ["Mac: ", "iPhone: "]; |
| 1058 | let edits = typed.map(|with| vec![typing_title(&pages[0], with)]); |
| 1059 | for winner in 0..2 { |
| 1060 | let image = merged(&diverged(&source, &edits), winner); |
| 1061 | let arena = onestore::Arena::default(); |
| 1062 | let mut section = Section::open(&arena, image).unwrap(); |
| 1063 | let conflicts = section.conflicts().unwrap(); |
| 1064 | let [(of, conflict)] = &conflicts[..] else { |
| 1065 | panic!("{winner}: {conflicts:?}") |
| 1066 | }; |
| 1067 | assert_eq!((*of, conflict.len()), (pages[0].0, 1)); |
| 1068 | for (space, with) in [(*of, typed[winner]), (conflict[0].space, typed[1 - winner])] { |
| 1069 | let page = section.page(space).unwrap(); |
| 1070 | assert_eq!(title(&page).text.text(), format!("{with}Page 0")); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /// Pages a version moved and deleted stay so beside another device's first edit. |
| 1076 | #[test] |
| 1077 | fn page_moves_and_deletions_survive_a_first_edit_elsewhere() { |
| 1078 | let (source, pages) = untouched(3); |
| 1079 | let reordering = vec![ |
| 1080 | Op::Section(SectionOp::Pages(vec![ |
| 1081 | PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap(), |
| 1082 | ])), |
| 1083 | Op::Section(SectionOp::Delete(vec![pages[1].0])), |
| 1084 | ]; |
| 1085 | let edits = [vec![typing_title(&pages[0], "Mac: ")], reordering]; |
| 1086 | for winner in 0..2 { |
| 1087 | let image = merged(&diverged(&source, &edits), winner); |
| 1088 | let arena = onestore::Arena::default(); |
| 1089 | let mut section = Section::open(&arena, image).unwrap(); |
| 1090 | let titles: Vec<String> = section |
| 1091 | .pages() |
| 1092 | .unwrap() |
| 1093 | .into_iter() |
| 1094 | .map(|(_, title, _)| title) |
| 1095 | .collect(); |
| 1096 | assert_eq!(titles, ["Page 2", "Mac: Page 0"], "{winner}"); |
| 1097 | assert!(section.conflicts().unwrap().is_empty(), "{winner}"); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | /// What the Mac and the iPhone do to four pages: the Mac types in the first page's title; the |
| 1102 | /// iPhone writes on the first and second pages, moves the fourth first and deletes the third. |
| 1103 | fn apart(pages: &[(ExGuid, Page)]) -> [Vec<Op>; 2] { |
| 1104 | let mut iphone = body(&pages[0], "Typed on the iPhone."); |
| 1105 | iphone.extend(body(&pages[1], "Page 1 from the iPhone.")); |
| 1106 | iphone.push(Op::Section(SectionOp::Pages(vec![ |
| 1107 | PageEdit::move_to(pages[3].0, Some(pages[0].0), 1).unwrap(), |
| 1108 | ]))); |
| 1109 | iphone.push(Op::Section(SectionOp::Delete(vec![pages[2].0]))); |
| 1110 | [vec![typing_title(&pages[0], "Mac: ")], iphone] |
| 1111 | } |
| 1112 | |
| 1113 | /// `apart`'s copies merged either way round, where the first page's two copies share no |
| 1114 | /// revision: that page stays the file's with the other kept beside it as a conflict page, and |
| 1115 | /// every other change merges. |
| 1116 | fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)], first: &str) { |
| 1117 | let space = pages[0].0; |
| 1118 | for copy in copies { |
| 1119 | let store = onestore::Store::parse(copy).unwrap(); |
| 1120 | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 1121 | let head = index.active(space).unwrap(); |
| 1122 | assert_eq!(index.spaces[&space].revisions[&head].dependency, None); |
| 1123 | } |
| 1124 | // Whether a page holds the Mac's edit and the iPhone's. |
| 1125 | let marks = |page: &Page| { |
| 1126 | ( |
| 1127 | title(page).text.text().starts_with("Mac: "), |
| 1128 | bodies(page) |
| 1129 | .iter() |
| 1130 | .any(|(_, text)| text == "Typed on the iPhone."), |
| 1131 | ) |
| 1132 | }; |
| 1133 | for winner in 0..2 { |
| 1134 | let image = merged(copies, winner); |
| 1135 | let arena = onestore::Arena::default(); |
| 1136 | let mut section = Section::open(&arena, image).unwrap(); |
| 1137 | let titles: Vec<String> = section |
| 1138 | .pages() |
| 1139 | .unwrap() |
| 1140 | .into_iter() |
| 1141 | .map(|(_, title, _)| title) |
| 1142 | .collect(); |
| 1143 | let first = [format!("Mac: {first}"), first.to_owned()][winner].clone(); |
| 1144 | assert_eq!(titles, ["Page 3", &first, "Page 1"], "{winner}"); |
| 1145 | assert_eq!( |
| 1146 | bodies(&section.page(pages[1].0).unwrap())[0].1, |
| 1147 | "Page 1 from the iPhone." |
| 1148 | ); |
| 1149 | let conflicts = section.conflicts().unwrap(); |
| 1150 | let [(of, conflict)] = &conflicts[..] else { |
| 1151 | panic!("{winner}: {conflicts:?}") |
| 1152 | }; |
| 1153 | assert_eq!((*of, conflict.len()), (space, 1)); |
| 1154 | let page = section.page(space).unwrap(); |
| 1155 | assert_eq!(marks(&page), (winner == 0, winner == 1)); |
| 1156 | let beside = section.page(conflict[0].space).unwrap(); |
| 1157 | assert_eq!(marks(&beside), (winner == 1, winner == 0)); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | /// A page both devices edited past the chain cap, each with a checkpoint, merges as a |
| 1162 | /// conflict page; the rest of the version still merges. |
| 1163 | #[test] |
| 1164 | fn pages_checkpointed_on_both_devices_merge_as_conflict_pages() { |
| 1165 | let (source, pages) = deep(4, 512); |
| 1166 | let first = title(&pages[0].1).text.text().to_owned(); |
| 1167 | assert_merged_beside(&diverged(&source, &apart(&pages)), &pages, &first); |
| 1168 | } |
| 1169 | |
| 1170 | /// The same for sections written before #152, whose first edit to a page checkpointed: |
| 1171 | /// `corpus/icloud-merge/first-edit-checkpoints`. |
| 1172 | #[test] |
| 1173 | fn pages_first_edited_before_152_merge_as_conflict_pages() { |
| 1174 | let row = format!( |
| 1175 | "{}/../../corpus/icloud-merge/first-edit-checkpoints", |
| 1176 | env!("CARGO_MANIFEST_DIR") |
| 1177 | ); |
| 1178 | let copies = DEVICES.map(|device| std::fs::read(format!("{row}/{device}.one")).unwrap()); |
| 1179 | let arena = onestore::Arena::default(); |
| 1180 | let mut section = Section::open(&arena, copies[0].clone()).unwrap(); |
| 1181 | let pages: Vec<(ExGuid, Page)> = section |
| 1182 | .pages() |
| 1183 | .unwrap() |
| 1184 | .into_iter() |
| 1185 | .map(|(space, ..)| (space, section.page(space).unwrap())) |
| 1186 | .collect(); |
| 1187 | assert_merged_beside(&copies, &pages, "Page 0"); |
| 1188 | } |
| 1189 | |
| 1190 | /// A page one device takes past the chain cap merges as edits with the other device's edits |
| 1191 | /// elsewhere: the file's state of the page is one the version passed through. |
| 1192 | #[test] |
| 1193 | fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() { |
| 1194 | let (source, pages) = untouched(2); |
| 1195 | let runs = (0..520).map(|n| toggling(&pages[0], n)); |
| 1196 | let mac = sealed( |
| 1197 | source.clone(), |
| 1198 | "Mac", |
| 1199 | runs.chain([vec![typing_title(&pages[0], "Mac: ")]]), |
| 1200 | ); |
| 1201 | assert!(depth(&mac, pages[0].0) < 512); |
| 1202 | let [_, iphone] = diverged( |
| 1203 | &source, |
| 1204 | &[Vec::new(), body(&pages[1], "Page 1 from the iPhone.")], |
| 1205 | ); |
| 1206 | for winner in 0..2 { |
| 1207 | let image = merged(&[mac.clone(), iphone.clone()], winner); |
| 1208 | let (listed, conflicts) = texts_of(&image); |
| 1209 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); |
| 1210 | assert_eq!( |
| 1211 | listed, "Mac: Page 0\nPage 1\nPage 1 from the iPhone.\n", |
| 1212 | "{winner}" |
| 1213 | ); |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | /// A page one device takes past the chain cap merges with the other device's edit to it. |
| 1218 | #[test] |
| 1219 | fn a_page_past_the_cap_on_one_device_merges_with_the_others_edit_to_it() { |
| 1220 | let (source, pages) = deep(2, 500); |
| 1221 | let first = title(&pages[0].1).text.text().to_owned(); |
| 1222 | let runs = (0..20).map(|n| toggling(&pages[0], n)); |
| 1223 | let mac = sealed( |
| 1224 | source.clone(), |
| 1225 | "Mac", |
| 1226 | runs.chain([vec![typing_title(&pages[0], "Mac: ")]]), |
| 1227 | ); |
| 1228 | assert!(depth(&mac, pages[0].0) < 20); |
| 1229 | let [_, iphone] = diverged( |
| 1230 | &source, |
| 1231 | &[Vec::new(), body(&pages[0], "Typed on the iPhone.")], |
| 1232 | ); |
| 1233 | for winner in 0..2 { |
| 1234 | let image = merged(&[mac.clone(), iphone.clone()], winner); |
| 1235 | let (listed, conflicts) = texts_of(&image); |
| 1236 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); |
| 1237 | assert_eq!( |
| 1238 | listed, |
| 1239 | format!("Mac: {first}\nTyped on the iPhone.\nPage 1\n"), |
| 1240 | "{winner}" |
| 1241 | ); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | /// A version whose page the file holds merged, then taken past the chain cap, merges from |
| 1246 | /// that merge: what the file already merged is not replayed. |
| 1247 | #[test] |
| 1248 | fn a_page_merged_then_past_the_cap_merges_as_edits() { |
| 1249 | let (source, pages) = deep(2, 490); |
| 1250 | let first = title(&pages[0].1).text.text().to_owned(); |
| 1251 | let iphone = sealed( |
| 1252 | source.clone(), |
| 1253 | "iPhone", |
| 1254 | [body(&pages[0], "A from the iPhone.")], |
| 1255 | ); |
| 1256 | let mac = sealed(source, "Mac", [vec![typing_title(&pages[1], "Mac: ")]]); |
| 1257 | let mac = merged(&[mac, iphone.clone()], 0); |
| 1258 | let page = reread(&iphone, &pages[..1]).remove(0); |
| 1259 | let runs = (0..30).map(|n| toggling(&page, n)); |
| 1260 | let iphone = sealed( |
| 1261 | iphone, |
| 1262 | "iPhone", |
| 1263 | runs.chain([vec![typing_title(&page, "iPhone: ")]]), |
| 1264 | ); |
| 1265 | assert!(depth(&iphone, page.0) < 30); |
| 1266 | for winner in 0..2 { |
| 1267 | let image = merged(&[mac.clone(), iphone.clone()], winner); |
| 1268 | let (listed, conflicts) = texts_of(&image); |
| 1269 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); |
| 1270 | assert_eq!( |
| 1271 | listed, |
| 1272 | format!("iPhone: {first}\nA from the iPhone.\nMac: Page 1\n"), |
| 1273 | "{winner}" |
| 1274 | ); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /// A page the file deleted, which the version edited with no revision in common, comes back |
| 1279 | /// as a copy, as a page deleted on one side and edited on the other does. |
| 1280 | #[test] |
| 1281 | fn a_page_the_file_deleted_with_no_common_revision_comes_back() { |
| 1282 | let (source, pages) = deep(2, 512); |
| 1283 | let space = pages[0].0; |
| 1284 | let deleting = Op::Section(SectionOp::Delete(vec![space])); |
| 1285 | let mac = sealed( |
| 1286 | source.clone(), |
| 1287 | "Mac", |
| 1288 | [vec![typing_title(&pages[0], "Mac: ")], vec![deleting]], |
| 1289 | ); |
| 1290 | let iphone = sealed(source, "iPhone", [body(&pages[0], "Typed on the iPhone.")]); |
| 1291 | for copy in [&mac, &iphone] { |
| 1292 | assert!(depth(copy, space) < 3); |
| 1293 | } |
| 1294 | let (listed, conflicts) = texts_of(&merged(&[mac, iphone], 0)); |
| 1295 | assert_eq!( |
| 1296 | listed.matches("Typed on the iPhone.").count(), |
| 1297 | 1, |
| 1298 | "{listed}" |
| 1299 | ); |
| 1300 | assert!(!listed.contains("Mac: "), "{listed}"); |
| 1301 | assert!(conflicts.is_empty(), "{conflicts}"); |
| 1302 | } |
| 1303 | |
| 1304 | /// A page list with no revision in common counts as the file's: the version's new page, with |
| 1305 | /// its conflict page, and page edits merge, its deletion does not. |
| 1306 | #[test] |
| 1307 | fn a_page_list_with_no_common_revision_counts_as_unchanged() { |
| 1308 | let (source, pages) = untouched(3); |
| 1309 | let root = { |
| 1310 | let store = onestore::Store::parse(&source).unwrap(); |
| 1311 | onestore::RevisionIndex::parse(&store).unwrap().root |
| 1312 | }; |
| 1313 | let moving = |n: usize| { |
| 1314 | let (space, before) = if n.is_multiple_of(2) { (1, 0) } else { (0, 1) }; |
| 1315 | let edit = PageEdit::move_to(pages[space].0, Some(pages[before].0), 1).unwrap(); |
| 1316 | vec![Op::Section(SectionOp::Pages(vec![edit]))] |
| 1317 | }; |
| 1318 | let runs = 512 - depth(&source, root); |
| 1319 | let source = sealed(source, "Fixture", (0..runs).map(moving)); |
| 1320 | let pages = reread(&source, &pages); |
| 1321 | let first = PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap(); |
| 1322 | let mac = sealed( |
| 1323 | source.clone(), |
| 1324 | "Mac", |
| 1325 | [ |
| 1326 | vec![Op::Section(SectionOp::Pages(vec![first]))], |
| 1327 | vec![typing_title(&pages[0], "Mac: ")], |
| 1328 | ], |
| 1329 | ); |
| 1330 | let creation = PageCreation::new(None, Some("From the iPhone"), "iPhone").unwrap(); |
| 1331 | let made = creation.space(); |
| 1332 | let iphone = sealed( |
| 1333 | source, |
| 1334 | "iPhone", |
| 1335 | [ |
| 1336 | vec![Op::Section(SectionOp::Delete(vec![pages[1].0]))], |
| 1337 | vec![Op::Section(SectionOp::Create(creation))], |
| 1338 | body(&pages[2], "Page 2 from the iPhone."), |
| 1339 | ], |
| 1340 | ); |
| 1341 | let page = reread(&iphone, &[(made, pages[0].1.clone())]).remove(0).1; |
| 1342 | let conflict = Op::Section(SectionOp::Conflict { |
| 1343 | of: made, |
| 1344 | creation: PageCreation::new(None, Some("From the iPhone"), "Someone") |
| 1345 | .unwrap() |
| 1346 | .in_space(new_id().unwrap().guid) |
| 1347 | .unwrap(), |
| 1348 | page: with_body(&page, "Kept beside it."), |
| 1349 | objects: Vec::new(), |
| 1350 | }); |
| 1351 | let iphone = sealed(iphone, "iPhone", [vec![conflict]]); |
| 1352 | for copy in [&mac, &iphone] { |
| 1353 | assert!(depth(copy, root) < 4); |
| 1354 | } |
| 1355 | let image = merged(&[mac, iphone], 0); |
| 1356 | let arena = onestore::Arena::default(); |
| 1357 | let mut section = Section::open(&arena, image.clone()).unwrap(); |
| 1358 | let conflicts = section.conflicts().unwrap(); |
| 1359 | let [(of, beside)] = &conflicts[..] else { |
| 1360 | panic!("{conflicts:?}") |
| 1361 | }; |
| 1362 | assert_eq!((*of, beside.len()), (made, 1)); |
| 1363 | let beside = bodies(&section.page(beside[0].space).unwrap()); |
| 1364 | assert!(beside.iter().any(|(_, text)| text == "Kept beside it.")); |
| 1365 | let (listed, _) = texts_of(&image); |
| 1366 | for text in [ |
| 1367 | "Mac: Page 0", |
| 1368 | "Page 1", |
| 1369 | "Page 2 from the iPhone.", |
| 1370 | "From the iPhone", |
| 1371 | ] { |
| 1372 | assert_eq!(listed.matches(text).count(), 1, "{text}:\n{listed}"); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | /// Two devices merge one version onto different states of a page, then their results meet: |
| 1377 | /// each merge is its own revision, so neither device's typing is taken as held by the other |
| 1378 | /// (sweep seed 600). The version's own typing, merged on both sides, may show twice: the limit |
| 1379 | /// on integrations from opposite bases in `arc/sync.md`. |
| 1380 | #[test] |
| 1381 | fn merges_of_one_version_onto_different_states_keep_both() { |
| 1382 | let source = fixture(); |
| 1383 | let arena = onestore::Arena::default(); |
| 1384 | let mut section = Section::open(&arena, source.clone()).unwrap(); |
| 1385 | let space = section.pages().unwrap()[0].0; |
| 1386 | let (text, body) = bodies(&section.page(space).unwrap())[0].clone(); |
| 1387 | let end = body.encode_utf16().count() as u32; |
| 1388 | let typed = |at: u32, with: &str| { |
| 1389 | let arena = onestore::Arena::default(); |
| 1390 | let mut section = Section::open(&arena, source.clone()).unwrap(); |
| 1391 | let op = PageOp::Text { |
| 1392 | text, |
| 1393 | range: at..at, |
| 1394 | with: with.into(), |
| 1395 | }; |
| 1396 | edit(&mut section, "Device", vec![Op::Page { space, op }]); |
| 1397 | section.seal().unwrap(); |
| 1398 | section.image() |
| 1399 | }; |
| 1400 | let (a, b, c) = (typed(0, "⟨A⟩"), typed(end, "⟨B⟩"), typed(4, "⟨C⟩")); |
| 1401 | let onto_a = merged(&[a, c.clone()], 0); |
| 1402 | let onto_b = merged(&[b, c], 0); |
| 1403 | for winner in 0..2 { |
| 1404 | let image = merged(&[onto_a.clone(), onto_b.clone()], winner); |
| 1405 | let (listed, conflicts) = texts_of(&image); |
| 1406 | for (token, shown) in [("⟨A⟩", 1..=1), ("⟨B⟩", 1..=1), ("⟨C⟩", 1..=2)] { |
| 1407 | let count = listed.matches(token).count(); |
| 1408 | assert!(shown.contains(&count), "{winner} {token}:\n{listed}"); |
| 1409 | } |
| 1410 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); |
| 1411 | } |
| 1412 | } |
| 1413 | /// A version an older build merged, under the names it gave merges, merges as nothing again: |
| 1414 | /// neither its clean merge nor its conflict page comes back |
| 1415 | /// (`corpus/icloud-merge/older-build-merges`). |
| 1416 | #[test] |
| 1417 | fn a_version_an_older_build_merged_merges_as_nothing() { |
| 1418 | let row = format!( |
| 1419 | "{}/../../corpus/icloud-merge/older-build-merges", |
| 1420 | env!("CARGO_MANIFEST_DIR") |
| 1421 | ); |
| 1422 | for name in ["clean", "clash"] { |
| 1423 | let read = |kind: &str| std::fs::read(format!("{row}/{name}-{kind}.one")).unwrap(); |
| 1424 | let file = read("merged"); |
| 1425 | assert!( |
| 1426 | merged(&[file.clone(), read("version")], 0) == file, |
| 1427 | "{name}" |
| 1428 | ); |
| 1429 | } |
| 1430 | } |
| 1431 | /// A merge of a merge onto the same state is a revision of its own: Z's version merged into X's |
| 1432 | /// file, that file merged into W's on X's state, and W's file merged back into Z's copy keeps |
| 1433 | /// X's typing. Z's own, merged on both sides, may show twice, as in the sweep's duplicates. |
| 1434 | #[test] |
| 1435 | fn a_merge_merged_again_onto_the_same_state_is_its_own_revision() { |
| 1436 | let source = fixture(); |
| 1437 | let arena = onestore::Arena::default(); |
| 1438 | let mut section = Section::open(&arena, source.clone()).unwrap(); |
| 1439 | let pages = section.pages().unwrap(); |
| 1440 | let space = pages[0].0; |
| 1441 | let (text, words) = bodies(&section.page(space).unwrap())[0].clone(); |
| 1442 | let end = words.encode_utf16().count() as u32; |
| 1443 | let typed = |image: Vec<u8>, space, text, at: u32, with: &str| { |
| 1444 | let op = PageOp::Text { |
| 1445 | text, |
| 1446 | range: at..at, |
| 1447 | with: with.into(), |
| 1448 | }; |
| 1449 | let arena = onestore::Arena::default(); |
| 1450 | let mut section = Section::open(&arena, image).unwrap(); |
| 1451 | edit(&mut section, "Device", vec![Op::Page { space, op }]); |
| 1452 | section.seal().unwrap(); |
| 1453 | section.image() |
| 1454 | }; |
| 1455 | let x = typed(source.clone(), space, text, end, "⟨X⟩"); |
| 1456 | let z = typed(source.clone(), space, text, 0, "⟨Z⟩"); |
| 1457 | let onto_x = merged(&[x.clone(), z.clone()], 0); |
| 1458 | let other = pages[1].0; |
| 1459 | let (text, _) = bodies(&section.page(other).unwrap())[0].clone(); |
| 1460 | let w = typed(x, other, text, 0, "⟨W⟩"); |
| 1461 | let onto_w = merged(&[w, onto_x], 0); |
| 1462 | let image = merged(&[z, onto_w], 0); |
| 1463 | let (listed, conflicts) = texts_of(&image); |
| 1464 | for (token, shown) in [("⟨X⟩", 1..=1), ("⟨W⟩", 1..=1), ("⟨Z⟩", 1..=2)] { |
| 1465 | let count = listed.matches(token).count(); |
| 1466 | assert!( |
| 1467 | shown.contains(&count), |
| 1468 | "{token}:\n{listed}\n--\n{conflicts}" |
| 1469 | ); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /// A device that hears of a version before the file that replaced its own copy merges it only |
| 1474 | /// into that file: found held in the stale copy, it would be retired everywhere and the |
| 1475 | /// download that follows would drop what it held (sweep seed 728). |
| 1476 | #[test] |
| 1477 | fn a_version_waits_for_the_file_that_replaced_a_stale_copy() { |
| 1478 | let source = fixture(); |
| 1479 | let directory = tempfile::tempdir().unwrap(); |
| 1480 | let mut server = Server { |
| 1481 | current: source.clone(), |
| 1482 | id: 0, |
| 1483 | next: 0, |
| 1484 | conflicts: Vec::new(), |
| 1485 | kept: 0, |
| 1486 | }; |
| 1487 | let mut devices: Vec<Device> = DEVICES |
| 1488 | .into_iter() |
| 1489 | .enumerate() |
| 1490 | .map(|(n, name)| Device { |
| 1491 | name: name.into(), |
| 1492 | copy: Copy { |
| 1493 | bytes: source.clone(), |
| 1494 | base: 0, |
| 1495 | dirty: false, |
| 1496 | versions: Vec::new(), |
| 1497 | online: true, |
| 1498 | }, |
| 1499 | replica: Replica::create(directory.path().join(format!("{n}.sqlite")), &source) |
| 1500 | .unwrap(), |
| 1501 | }) |
| 1502 | .collect(); |
| 1503 | let pages = devices[0].replica.pages().unwrap(); |
| 1504 | let typing = |device: &Device, page: usize, with: &str| { |
| 1505 | let space = pages[page].0; |
| 1506 | let (text, _) = bodies(&device.replica.page(space).unwrap())[0].clone(); |
| 1507 | let op = PageOp::Text { |
| 1508 | text, |
| 1509 | range: 0..0, |
| 1510 | with: with.into(), |
| 1511 | }; |
| 1512 | let edit = Edit { |
| 1513 | at: crate_now(), |
| 1514 | ops: vec![Op::Page { space, op }], |
| 1515 | }; |
| 1516 | device.replica.apply(&device.name, edit).unwrap(); |
| 1517 | }; |
| 1518 | let mut random = Random(1); |
| 1519 | // The Mac's typing becomes the file. |
| 1520 | typing(&devices[0], 0, "⟨Mac⟩"); |
| 1521 | devices[0].sync(&mut server); |
| 1522 | devices[0].upload(&mut server, &mut random); |
| 1523 | assert_eq!(server.id, devices[0].copy.base); |
| 1524 | // The iPhone, still on the first file, uploads too and wins: the Mac's file becomes a |
| 1525 | // version, which the Mac hears of before the iPhone's file reaches it. |
| 1526 | typing(&devices[1], 1, "⟨iPhone⟩"); |
| 1527 | devices[1].sync(&mut server); |
| 1528 | let lost = std::mem::replace(&mut server.current, devices[1].copy.bytes.clone()); |
| 1529 | let id = server.next(); |
| 1530 | server.conflicts.push((id, lost, "Mac".into())); |
| 1531 | server.id = server.next(); |
| 1532 | devices[1].copy.base = server.id; |
| 1533 | devices[1].copy.dirty = false; |
| 1534 | devices[0].deliver(&server); |
| 1535 | devices[0].sync(&mut server); |
| 1536 | for _ in 0..4 { |
| 1537 | for device in &mut devices { |
| 1538 | device.download(&mut server, &mut random); |
| 1539 | device.deliver(&server); |
| 1540 | device.sync(&mut server); |
| 1541 | device.upload(&mut server, &mut random); |
| 1542 | } |
| 1543 | } |
| 1544 | assert!(server.conflicts.is_empty()); |
| 1545 | let (listed, _) = texts_of(&server.current); |
| 1546 | for token in ["⟨Mac⟩", "⟨iPhone⟩"] { |
| 1547 | assert_eq!(listed.matches(token).count(), 1, "{token}:\n{listed}"); |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | /// A version identical to the file is that file's stale copy whatever the provider reports: |
| 1552 | /// it waits for the file that replaces the copy. |
| 1553 | #[test] |
| 1554 | fn a_version_identical_to_the_file_is_kept() { |
| 1555 | let source = fixture(); |
| 1556 | let directory = tempfile::tempdir().unwrap(); |
| 1557 | let mut server = Server { |
| 1558 | current: source.clone(), |
| 1559 | id: 1, |
| 1560 | next: 1, |
| 1561 | conflicts: vec![(2, source.clone(), "iPhone".into())], |
| 1562 | kept: 0, |
| 1563 | }; |
| 1564 | let mut device = Device { |
| 1565 | name: "Mac".into(), |
| 1566 | copy: Copy { |
| 1567 | bytes: source.clone(), |
| 1568 | base: 1, |
| 1569 | dirty: false, |
| 1570 | versions: Vec::new(), |
| 1571 | online: true, |
| 1572 | }, |
| 1573 | replica: Replica::create(directory.path().join("replica.sqlite"), &source).unwrap(), |
| 1574 | }; |
| 1575 | device.deliver(&server); |
| 1576 | device.sync(&mut server); |
| 1577 | assert_eq!(server.conflicts.len(), 1); |
| 1578 | assert_eq!(server.kept, 0); |
| 1579 | } |