| ... | ... | @@ -962,6 +962,68 @@ fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op { |
| 962 | 962 | } |
| 963 | 963 | } |
| 964 | 964 | |
| 965 | /// The `n`th of a run of edits that type "x" at the start of a page's title and take it out. |
| 966 | fn toggling(page: &(ExGuid, Page), n: usize) -> Vec<Op> { |
| 967 | let mut op = typing_title(page, "x"); |
| 968 | if let Op::Page { |
| 969 | op: PageOp::Text { range, with, .. }, |
| 970 | .. |
| 971 | } = &mut op |
| 972 | && n % 2 == 1 |
| 973 | { |
| 974 | *range = 0..1; |
| 975 | with.clear(); |
| 976 | } |
| 977 | vec![op] |
| 978 | } |
| 979 | |
| 980 | /// The ops adding a body of `text` to a page. |
| 981 | fn body((space, page): &(ExGuid, Page), text: &str) -> Vec<Op> { |
| 982 | lower_page(page, &with_body(page, text)) |
| 983 | .unwrap() |
| 984 | .into_iter() |
| 985 | .map(|op| Op::Page { space: *space, op }) |
| 986 | .collect() |
| 987 | } |
| 988 | |
| 989 | /// `image` with each of `edits` sealed as a revision of its own. |
| 990 | fn sealed(image: Vec<u8>, author: &str, edits: impl IntoIterator<Item = Vec<Op>>) -> Vec<u8> { |
| 991 | let arena = onestore::Arena::default(); |
| 992 | let mut section = Section::open(&arena, image).unwrap(); |
| 993 | for ops in edits { |
| 994 | edit(&mut section, author, ops); |
| 995 | section.seal().unwrap(); |
| 996 | } |
| 997 | section.image() |
| 998 | } |
| 999 | |
| 1000 | /// Revisions in the dependency chain of `space`'s newest revision in `image`. |
| 1001 | fn depth(image: &[u8], space: ExGuid) -> usize { |
| 1002 | let store = onestore::Store::parse(image).unwrap(); |
| 1003 | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 1004 | let revisions = &index.spaces[&space].revisions; |
| 1005 | std::iter::successors(index.active(space).ok(), |rid| revisions[rid].dependency).count() |
| 1006 | } |
| 1007 | |
| 1008 | /// `pages` as `image` holds them. |
| 1009 | fn reread(image: &[u8], pages: &[(ExGuid, Page)]) -> Vec<(ExGuid, Page)> { |
| 1010 | let arena = onestore::Arena::default(); |
| 1011 | let section = Section::open(&arena, image.to_vec()).unwrap(); |
| 1012 | pages |
| 1013 | .iter() |
| 1014 | .map(|(space, _)| (*space, section.page(*space).unwrap())) |
| 1015 | .collect() |
| 1016 | } |
| 1017 | |
| 1018 | /// `untouched(count)` with its first page's chain `deep` revisions long. |
| 1019 | fn deep(count: usize, deep: usize) -> (Vec<u8>, Vec<(ExGuid, Page)>) { |
| 1020 | let (source, pages) = untouched(count); |
| 1021 | let runs = deep - depth(&source, pages[0].0); |
| 1022 | let source = sealed(source, "Fixture", (0..runs).map(|n| toggling(&pages[0], n))); |
| 1023 | let pages = reread(&source, &pages); |
| 1024 | (source, pages) |
| 1025 | } |
| 1026 | |
| 965 | 1027 | /// Issue #152: the first edits two devices make to a page merge as edits, not as a version |
| 966 | 1028 | /// set aside. |
| 967 | 1029 | #[test] |
| ... | ... | @@ -1036,16 +1098,8 @@ fn page_moves_and_deletions_survive_a_first_edit_elsewhere() { |
| 1036 | 1098 | /// What the Mac and the iPhone do to four pages: the Mac types in the first page's title; the |
| 1037 | 1099 | /// iPhone writes on the first and second pages, moves the fourth first and deletes the third. |
| 1038 | 1100 | fn apart(pages: &[(ExGuid, Page)]) -> [Vec<Op>; 2] { |
| 1039 | | let body = |(space, page): &(ExGuid, Page), text| { |
| 1040 | | let space = *space; |
| 1041 | | lower_page(page, &with_body(page, text)) |
| 1042 | | .unwrap() |
| 1043 | | .into_iter() |
| 1044 | | .map(move |op| Op::Page { space, op }) |
| 1045 | | }; |
| 1046 | | let mut iphone: Vec<Op> = body(&pages[0], "Typed on the iPhone.") |
| 1047 | | .chain(body(&pages[1], "Page 1 from the iPhone.")) |
| 1048 | | .collect(); |
| 1101 | let mut iphone = body(&pages[0], "Typed on the iPhone."); |
| 1102 | iphone.extend(body(&pages[1], "Page 1 from the iPhone.")); |
| 1049 | 1103 | iphone.push(Op::Section(SectionOp::Pages(vec![ |
| 1050 | 1104 | PageEdit::move_to(pages[3].0, Some(pages[0].0), 1).unwrap(), |
| 1051 | 1105 | ]))); |
| ... | ... | @@ -1056,7 +1110,7 @@ fn apart(pages: &[(ExGuid, Page)]) -> [Vec<Op>; 2] { |
| 1056 | 1110 | /// `apart`'s copies merged either way round, where the first page's two copies share no |
| 1057 | 1111 | /// revision: that page stays the file's with the other kept beside it as a conflict page, and |
| 1058 | 1112 | /// every other change merges. |
| 1059 | | fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)]) { |
| 1113 | fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)], first: &str) { |
| 1060 | 1114 | let space = pages[0].0; |
| 1061 | 1115 | for copy in copies { |
| 1062 | 1116 | let store = onestore::Store::parse(copy).unwrap(); |
| ... | ... | @@ -1083,8 +1137,8 @@ fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)]) { |
| 1083 | 1137 | .into_iter() |
| 1084 | 1138 | .map(|(_, title, _)| title) |
| 1085 | 1139 | .collect(); |
| 1086 | | let first = ["Mac: Page 0", "Page 0"][winner]; |
| 1087 | | assert_eq!(titles, ["Page 3", first, "Page 1"], "{winner}"); |
| 1140 | let first = [format!("Mac: {first}"), first.to_owned()][winner].clone(); |
| 1141 | assert_eq!(titles, ["Page 3", &first, "Page 1"], "{winner}"); |
| 1088 | 1142 | assert_eq!( |
| 1089 | 1143 | bodies(&section.page(pages[1].0).unwrap())[0].1, |
| 1090 | 1144 | "Page 1 from the iPhone." |
| ... | ... | @@ -1105,38 +1159,9 @@ fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)]) { |
| 1105 | 1159 | /// conflict page; the rest of the version still merges. |
| 1106 | 1160 | #[test] |
| 1107 | 1161 | fn pages_checkpointed_on_both_devices_merge_as_conflict_pages() { |
| 1108 | | let (source, pages) = untouched(4); |
| 1109 | | let arena = onestore::Arena::default(); |
| 1110 | | let mut section = Section::open(&arena, source).unwrap(); |
| 1111 | | let (space, page) = &pages[0]; |
| 1112 | | let body = lower_page(page, &with_body(page, "Body")) |
| 1113 | | .unwrap() |
| 1114 | | .into_iter() |
| 1115 | | .map(|op| Op::Page { space: *space, op }) |
| 1116 | | .collect(); |
| 1117 | | edit(&mut section, "Fixture", body); |
| 1118 | | section.seal().unwrap(); |
| 1119 | | let (text, _) = bodies(&section.page(*space).unwrap())[0].clone(); |
| 1120 | | for n in 0..510 { |
| 1121 | | let (range, with) = if n % 2 == 0 { (0..0, "x") } else { (0..1, "") }; |
| 1122 | | let op = PageOp::Text { |
| 1123 | | text, |
| 1124 | | range, |
| 1125 | | with: with.into(), |
| 1126 | | }; |
| 1127 | | edit( |
| 1128 | | &mut section, |
| 1129 | | "Fixture", |
| 1130 | | vec![Op::Page { space: *space, op }], |
| 1131 | | ); |
| 1132 | | section.seal().unwrap(); |
| 1133 | | } |
| 1134 | | let source = section.image(); |
| 1135 | | let pages: Vec<(ExGuid, Page)> = pages |
| 1136 | | .iter() |
| 1137 | | .map(|(space, _)| (*space, section.page(*space).unwrap())) |
| 1138 | | .collect(); |
| 1139 | | assert_merged_beside(&diverged(&source, &apart(&pages)), &pages); |
| 1162 | let (source, pages) = deep(4, 512); |
| 1163 | let first = title(&pages[0].1).text.text().to_owned(); |
| 1164 | assert_merged_beside(&diverged(&source, &apart(&pages)), &pages, &first); |
| 1140 | 1165 | } |
| 1141 | 1166 | |
| 1142 | 1167 | /// The same for sections written before #152, whose first edit to a page checkpointed: |
| ... | ... | @@ -1156,7 +1181,7 @@ fn pages_first_edited_before_152_merge_as_conflict_pages() { |
| 1156 | 1181 | .into_iter() |
| 1157 | 1182 | .map(|(space, ..)| (space, section.page(space).unwrap())) |
| 1158 | 1183 | .collect(); |
| 1159 | | assert_merged_beside(&copies, &pages); |
| 1184 | assert_merged_beside(&copies, &pages, "Page 0"); |
| 1160 | 1185 | } |
| 1161 | 1186 | |
| 1162 | 1187 | /// A page one device takes past the chain cap merges as edits with the other device's edits |
| ... | ... | @@ -1164,38 +1189,17 @@ fn pages_first_edited_before_152_merge_as_conflict_pages() { |
| 1164 | 1189 | #[test] |
| 1165 | 1190 | fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() { |
| 1166 | 1191 | let (source, pages) = untouched(2); |
| 1167 | | let arena = onestore::Arena::default(); |
| 1168 | | let mut section = Section::open(&arena, source.clone()).unwrap(); |
| 1169 | | for n in 0..521 { |
| 1170 | | let with = if n == 520 { "Mac: " } else { "x" }; |
| 1171 | | let mut op = typing_title(&pages[0], with); |
| 1172 | | if let Op::Page { |
| 1173 | | op: PageOp::Text { range, with, .. }, |
| 1174 | | .. |
| 1175 | | } = &mut op |
| 1176 | | && n % 2 == 1 |
| 1177 | | { |
| 1178 | | *range = 0..1; |
| 1179 | | with.clear(); |
| 1180 | | } |
| 1181 | | edit(&mut section, "Mac", vec![op]); |
| 1182 | | section.seal().unwrap(); |
| 1183 | | } |
| 1184 | | let mac = section.image(); |
| 1185 | | let store = onestore::Store::parse(&mac).unwrap(); |
| 1186 | | let index = onestore::RevisionIndex::parse(&store).unwrap(); |
| 1187 | | let space = &index.spaces[&pages[0].0]; |
| 1188 | | let chain = std::iter::successors(index.active(pages[0].0).ok(), |rid| { |
| 1189 | | space.revisions[rid].dependency |
| 1190 | | }); |
| 1191 | | assert!(chain.count() < 512); |
| 1192 | | let (space, page) = &pages[1]; |
| 1193 | | let body = lower_page(page, &with_body(page, "Page 1 from the iPhone.")) |
| 1194 | | .unwrap() |
| 1195 | | .into_iter() |
| 1196 | | .map(|op| Op::Page { space: *space, op }) |
| 1197 | | .collect(); |
| 1198 | | let [_, iphone] = diverged(&source, &[Vec::new(), body]); |
| 1192 | let runs = (0..520).map(|n| toggling(&pages[0], n)); |
| 1193 | let mac = sealed( |
| 1194 | source.clone(), |
| 1195 | "Mac", |
| 1196 | runs.chain([vec![typing_title(&pages[0], "Mac: ")]]), |
| 1197 | ); |
| 1198 | assert!(depth(&mac, pages[0].0) < 512); |
| 1199 | let [_, iphone] = diverged( |
| 1200 | &source, |
| 1201 | &[Vec::new(), body(&pages[1], "Page 1 from the iPhone.")], |
| 1202 | ); |
| 1199 | 1203 | for winner in 0..2 { |
| 1200 | 1204 | let image = merged(&[mac.clone(), iphone.clone()], winner); |
| 1201 | 1205 | let (listed, conflicts) = texts_of(&image); |
| ... | ... | @@ -1207,6 +1211,165 @@ fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() { |
| 1207 | 1211 | } |
| 1208 | 1212 | } |
| 1209 | 1213 | |
| 1214 | /// A page one device takes past the chain cap merges with the other device's edit to it. |
| 1215 | #[test] |
| 1216 | fn a_page_past_the_cap_on_one_device_merges_with_the_others_edit_to_it() { |
| 1217 | let (source, pages) = deep(2, 500); |
| 1218 | let first = title(&pages[0].1).text.text().to_owned(); |
| 1219 | let runs = (0..20).map(|n| toggling(&pages[0], n)); |
| 1220 | let mac = sealed( |
| 1221 | source.clone(), |
| 1222 | "Mac", |
| 1223 | runs.chain([vec![typing_title(&pages[0], "Mac: ")]]), |
| 1224 | ); |
| 1225 | assert!(depth(&mac, pages[0].0) < 20); |
| 1226 | let [_, iphone] = diverged( |
| 1227 | &source, |
| 1228 | &[Vec::new(), body(&pages[0], "Typed on the iPhone.")], |
| 1229 | ); |
| 1230 | for winner in 0..2 { |
| 1231 | let image = merged(&[mac.clone(), iphone.clone()], winner); |
| 1232 | let (listed, conflicts) = texts_of(&image); |
| 1233 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); |
| 1234 | assert_eq!( |
| 1235 | listed, |
| 1236 | format!("Mac: {first}\nTyped on the iPhone.\nPage 1\n"), |
| 1237 | "{winner}" |
| 1238 | ); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | /// A version whose page the file holds merged, then taken past the chain cap, merges from |
| 1243 | /// that merge: what the file already merged is not replayed. |
| 1244 | #[test] |
| 1245 | fn a_page_merged_then_past_the_cap_merges_as_edits() { |
| 1246 | let (source, pages) = deep(2, 490); |
| 1247 | let first = title(&pages[0].1).text.text().to_owned(); |
| 1248 | let iphone = sealed( |
| 1249 | source.clone(), |
| 1250 | "iPhone", |
| 1251 | [body(&pages[0], "A from the iPhone.")], |
| 1252 | ); |
| 1253 | let mac = sealed(source, "Mac", [vec![typing_title(&pages[1], "Mac: ")]]); |
| 1254 | let mac = merged(&[mac, iphone.clone()], 0); |
| 1255 | let page = reread(&iphone, &pages[..1]).remove(0); |
| 1256 | let runs = (0..30).map(|n| toggling(&page, n)); |
| 1257 | let iphone = sealed( |
| 1258 | iphone, |
| 1259 | "iPhone", |
| 1260 | runs.chain([vec![typing_title(&page, "iPhone: ")]]), |
| 1261 | ); |
| 1262 | assert!(depth(&iphone, page.0) < 30); |
| 1263 | for winner in 0..2 { |
| 1264 | let image = merged(&[mac.clone(), iphone.clone()], winner); |
| 1265 | let (listed, conflicts) = texts_of(&image); |
| 1266 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); |
| 1267 | assert_eq!( |
| 1268 | listed, |
| 1269 | format!("iPhone: {first}\nA from the iPhone.\nMac: Page 1\n"), |
| 1270 | "{winner}" |
| 1271 | ); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /// A page the file deleted, which the version edited with no revision in common, comes back |
| 1276 | /// as a copy, as a page deleted on one side and edited on the other does. |
| 1277 | #[test] |
| 1278 | fn a_page_the_file_deleted_with_no_common_revision_comes_back() { |
| 1279 | let (source, pages) = deep(2, 512); |
| 1280 | let space = pages[0].0; |
| 1281 | let deleting = Op::Section(SectionOp::Delete(vec![space])); |
| 1282 | let mac = sealed( |
| 1283 | source.clone(), |
| 1284 | "Mac", |
| 1285 | [vec![typing_title(&pages[0], "Mac: ")], vec![deleting]], |
| 1286 | ); |
| 1287 | let iphone = sealed(source, "iPhone", [body(&pages[0], "Typed on the iPhone.")]); |
| 1288 | for copy in [&mac, &iphone] { |
| 1289 | assert!(depth(copy, space) < 3); |
| 1290 | } |
| 1291 | let (listed, conflicts) = texts_of(&merged(&[mac, iphone], 0)); |
| 1292 | assert_eq!( |
| 1293 | listed.matches("Typed on the iPhone.").count(), |
| 1294 | 1, |
| 1295 | "{listed}" |
| 1296 | ); |
| 1297 | assert!(!listed.contains("Mac: "), "{listed}"); |
| 1298 | assert!(conflicts.is_empty(), "{conflicts}"); |
| 1299 | } |
| 1300 | |
| 1301 | /// A page list with no revision in common counts as the file's: the version's new page, with |
| 1302 | /// its conflict page, and page edits merge, its deletion does not. |
| 1303 | #[test] |
| 1304 | fn a_page_list_with_no_common_revision_counts_as_unchanged() { |
| 1305 | let (source, pages) = untouched(3); |
| 1306 | let root = { |
| 1307 | let store = onestore::Store::parse(&source).unwrap(); |
| 1308 | onestore::RevisionIndex::parse(&store).unwrap().root |
| 1309 | }; |
| 1310 | let moving = |n: usize| { |
| 1311 | let (space, before) = if n.is_multiple_of(2) { (1, 0) } else { (0, 1) }; |
| 1312 | let edit = PageEdit::move_to(pages[space].0, Some(pages[before].0), 1).unwrap(); |
| 1313 | vec![Op::Section(SectionOp::Pages(vec![edit]))] |
| 1314 | }; |
| 1315 | let runs = 512 - depth(&source, root); |
| 1316 | let source = sealed(source, "Fixture", (0..runs).map(moving)); |
| 1317 | let pages = reread(&source, &pages); |
| 1318 | let first = PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap(); |
| 1319 | let mac = sealed( |
| 1320 | source.clone(), |
| 1321 | "Mac", |
| 1322 | [ |
| 1323 | vec![Op::Section(SectionOp::Pages(vec![first]))], |
| 1324 | vec![typing_title(&pages[0], "Mac: ")], |
| 1325 | ], |
| 1326 | ); |
| 1327 | let creation = PageCreation::new(None, Some("From the iPhone"), "iPhone").unwrap(); |
| 1328 | let made = creation.space(); |
| 1329 | let iphone = sealed( |
| 1330 | source, |
| 1331 | "iPhone", |
| 1332 | [ |
| 1333 | vec![Op::Section(SectionOp::Delete(vec![pages[1].0]))], |
| 1334 | vec![Op::Section(SectionOp::Create(creation))], |
| 1335 | body(&pages[2], "Page 2 from the iPhone."), |
| 1336 | ], |
| 1337 | ); |
| 1338 | let page = reread(&iphone, &[(made, pages[0].1.clone())]).remove(0).1; |
| 1339 | let conflict = Op::Section(SectionOp::Conflict { |
| 1340 | of: made, |
| 1341 | creation: PageCreation::new(None, Some("From the iPhone"), "Someone") |
| 1342 | .unwrap() |
| 1343 | .in_space(new_id().unwrap().guid) |
| 1344 | .unwrap(), |
| 1345 | page: with_body(&page, "Kept beside it."), |
| 1346 | objects: Vec::new(), |
| 1347 | }); |
| 1348 | let iphone = sealed(iphone, "iPhone", [vec![conflict]]); |
| 1349 | for copy in [&mac, &iphone] { |
| 1350 | assert!(depth(copy, root) < 4); |
| 1351 | } |
| 1352 | let image = merged(&[mac, iphone], 0); |
| 1353 | let arena = onestore::Arena::default(); |
| 1354 | let mut section = Section::open(&arena, image.clone()).unwrap(); |
| 1355 | let conflicts = section.conflicts().unwrap(); |
| 1356 | let [(of, beside)] = &conflicts[..] else { |
| 1357 | panic!("{conflicts:?}") |
| 1358 | }; |
| 1359 | assert_eq!((*of, beside.len()), (made, 1)); |
| 1360 | let beside = bodies(&section.page(beside[0].space).unwrap()); |
| 1361 | assert!(beside.iter().any(|(_, text)| text == "Kept beside it.")); |
| 1362 | let (listed, _) = texts_of(&image); |
| 1363 | for text in [ |
| 1364 | "Mac: Page 0", |
| 1365 | "Page 1", |
| 1366 | "Page 2 from the iPhone.", |
| 1367 | "From the iPhone", |
| 1368 | ] { |
| 1369 | assert_eq!(listed.matches(text).count(), 1, "{text}:\n{listed}"); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1210 | 1373 | /// Two devices merge one version onto different states of a page, then their results meet: |
| 1211 | 1374 | /// each merge is its own revision, so neither device's typing is taken as held by the other |
| 1212 | 1375 | /// (sweep seed 600). The version's own typing, merged on both sides, may show twice: the limit |