| 1 | #!/usr/bin/env python3 |
| 2 | """Verify native replica tests reject representative defects, restoring sources.""" |
| 3 | from pathlib import Path |
| 4 | import os |
| 5 | import subprocess |
| 6 | root = Path(__file__).resolve().parents[1] |
| 7 | cargo = os.environ.get('CARGO', 'cargo') |
| 8 | log_dir = Path(os.environ.get('DOCS_MUTATION_LOGS', str(root / 'build' / 'mutation-logs'))) |
| 9 | log_dir.mkdir(parents=True, exist_ok=True) |
| 10 | cases = [ |
| 11 | ('crdt/src/paste.rs', '(!prefix_empty || e.tag.as_ref() == "paragraph")', 'true', 'rich_paste_at_empty_block_keeps_heading_and_list_structure'), |
| 12 | ('crdt/src/input_rules.rs', 'structure::execute(doc, &command)?;', 'let _ = &command;', 'input_rules_transform_and_backspace_restore_typed_prefix'), |
| 13 | ('crdt/src/lib.rs', 'if local { "native-edit" } else { "remote" }', 'if local { "remote" } else { "remote" }', 'distinct_display_generations_preserve_peer_and_authoritative_undo'), |
| 14 | ('crdt/src/display.rs', 'cursor += 1;', 'cursor += 0;', 'optimistic_newline_typeahead_resolves_new_paragraph_in_source'), |
| 15 | ('crdt/src/structure.rs', 'start_text.remove_range(&mut txn, start_at, tail);', 'let _ = tail;', 'cross_paragraph_replacement_keeps_prefix_id_and_formatted_suffix'), |
| 16 | ('crdt/src/display.rs', 'found = Some(start + local.min(length))', 'found = Some(start)', 'selection_anchor_tracks_peer_insertion_in_utf16'), |
| 17 | ('crdt/src/operations.rs', 'text.format(&mut txn, at, length, marks);', 'let _ = marks;', 'utf16_edits_preserve_identity_and_marks'), |
| 18 | ('crdt/src/lib.rs', 'HashSet::from(["native-edit".into()])', 'HashSet::from(["native-edit".into(), "remote".into()])', 'concurrent_insertions_and_own_undo_preserve_peer'), |
| 19 | ('crdt/src/lib.rs', 'remaining = &remaining[remaining.len() - trailing..];', 'remaining = &[];', 'concatenated_log_applies_every_update'), |
| 20 | ('crdt/src/lib.rs', 'self.doc.transact().encode_state_as_update_v1(&StateVector::default())', 'vec![0, 0]', 'reads_existing_yjs_fragment_without_rewriting'), |
| 21 | ('crdt/src/structure.rs', 'Any::from(suffix)), diff.attributes', 'Any::from(suffix)), None', 'paragraph_split_keeps_prefix_identity_and_suffix_marks'), |
| 22 | ('crdt/src/structure.rs', 'copy.attributes.insert("level".into(), number(&request["level"])?.clamp(1,6).to_string());', 'copy.attributes.insert("level".into(), "1".into());', 'changing_block_preserves_existing_inline_formatting'), |
| 23 | ('host/src/lib.rs', 'if self.failed || request.len() > MAX_BYTES', 'if request.len() > MAX_BYTES', 'failed_replica_cannot_be_reused'), |
| 24 | ('host/src/lib.rs', 'request.len()).min(self.ceiling)', 'request.len())', 'running_out_of_fuel_is_a_clean_failure_that_discards_the_instance'), |
| 25 | ('host/src/lib.rs', 'BASE_FUEL + FUEL_PER_MEMORY_BYTE * memory + FUEL_PER_REQUEST_BYTE * request', 'BASE_FUEL + 2 * FUEL_PER_MEMORY_BYTE * memory + FUEL_PER_REQUEST_BYTE * request', 'fuel_budget_grows_with_the_instance_but_stays_bounded'), |
| 26 | # The vendored yrs patches (vendor/yrs-0.23.5/PATCHES.md), each reverted; |
| 27 | # their tests live in crdt. Patch 1 panics rather than failing an assert. |
| 28 | ('vendor/yrs-0.23.5/src/block.rs', ' if self.is_deleted() {\n self.content.gc(collector);', ' if self.is_deleted() && !self.info.is_keep() {\n self.content.gc(collector);', 'a_snapshot_after_a_peer_deletes_a_kept_items_paragraph_holds_what_the_editor_shows', 'crdt', 'Could not get parent branch info'), |
| 29 | ('vendor/yrs-0.23.5/src/block.rs', ' txn.store_mut().blocks.push_block(redone_item);\n if should_delete {', ' txn.store_mut().blocks.push_block(redone_item);\n if false && should_delete {', 'an_item_undone_into_a_deleted_restored_parent_is_deleted_as_everywhere_else', 'crdt'), |
| 30 | ('vendor/yrs-0.23.5/src/transaction.rs', ' if should_delete {\n self.delete(block_ptr);', ' if false && should_delete {\n self.delete(block_ptr);', 'local_inserts_into_a_type_a_peer_deleted_are_deleted_as_everywhere_else', 'crdt'), |
| 31 | ('vendor/yrs-0.23.5/src/block_iter.rs', ' if should_delete {\n txn.delete(block_ptr);', ' if false && should_delete {\n txn.delete(block_ptr);', 'local_inserts_into_a_type_a_peer_deleted_are_deleted_as_everywhere_else', 'crdt'), |
| 32 | ('vendor/yrs-0.23.5/src/block.rs', ' right = l.right;', ' let _ = l.right;', 'an_undo_whose_neighbours_trace_to_one_item_places_it_as_every_peer_does', 'crdt'), |
| 33 | ] |
| 34 | for relative, old, new, test, *rest in cases: |
| 35 | path = root / relative |
| 36 | original = path.read_text() |
| 37 | assert original.count(old) >= 1, (relative, old) |
| 38 | try: |
| 39 | path.write_text(original.replace(old, new)) |
| 40 | crate = rest[0] if rest else relative.split('/')[0] |
| 41 | result = subprocess.run([cargo, 'test', '--locked', '--offline', '--manifest-path', str(root / crate / 'Cargo.toml'), test, '--', '--exact'], capture_output=True, text=True) |
| 42 | output = result.stdout + result.stderr |
| 43 | (log_dir / ('docs-mutation-' + test + '.log')).write_text(output) |
| 44 | expected = rest[1:] or ['assertion', 'assert_eq'] |
| 45 | assert result.returncode != 0 and any(marker in output for marker in expected), (test, output) |
| 46 | print(test + ': caught') |
| 47 | finally: |
| 48 | path.write_text(original) |
| 49 | |