# SPDX-License-Identifier: Apache-2.0 # Copyright 2026 Scienting # # Source Code: https://git.scient.ing/papers/paperspec # See the LICENSE.md file for full license terms. """Corrections: the gate, every operation and refusal, and the state they leave behind.""" import copy import json import re import pytest from hypothesis import HealthCheck, assume, given, settings, strategies as st from conftest import vector_cases from helpers_cli import HALE, store from paperspec import corrections, hashing, package from paperspec.tree import Refusal STORE = store() VALUES = ("text_hash", "match_hash", "align_key", "own_hash", "node_hash") CROP = [{"kind": "crop", "sha256": "0" * 64}] BASE = { "format": "scienting-correction/1", "evidence": [{"kind": "person", "by": "ana"}], "author": {"key": "ana"}, "reason": "Test.", "at": "2026-10-14T09:00:00Z", "status": "proposed", } PROFILE = settings( derandomize=True, max_examples=60, database=None, deadline=None, suppress_health_check=[HealthCheck.too_slow], ) # Nodes of the main example the tests correct. INTRO = "kea5026" # a sentence with a cite run EQUATION_SENTENCE = "rr20tev" # text, an equation run, math runs WATER = "hca6dc5" # a math run and a cite run TABLE_SENTENCE = "a4jrz2t" # starts with an xref run SCAN = "xjns57z" # a sentence with digits FIGURE_SENTENCE = "qwgfg51" # an xref run and a footnote mark ABSTRACT_SENTENCE = "mhp0at8" # a single text run without digits ABSTRACT_PARAGRAPH = "vcmfvx9" TITLE_SENTENCE = "mhkp90t" EQUATION = "tkxsmts" SECTION = "p36k8t6" SECOND_SECTION = "d400rz3" FIRST_PARAGRAPH = "tgp1qey" SECOND_PARAGRAPH = "v7q07ne" METHODS_PARAGRAPH = "gwzbj3s" CELL = "c20cna6" TABLE = "hj4s7pb" REFERENCE = "tb6syqq" OTHER_REFERENCE = "vxsdvzq" def proposal(**fields): """A proposed correction with the fields every proposal carries.""" return dict(BASE, **fields) def gate(state, **fields): return corrections.gate(state, proposal(**fields), STORE) def refusal(state, **fields): """The refusal the gate raises for a proposal.""" with pytest.raises(Refusal) as raised: gate(state, **fields) return raised.value def fresh(state): return corrections.State( state.key, state.nodes, state.ids, state.corrections, references=state.references, ) @pytest.fixture(scope="module") def hale(): return package.read_state(HALE) def node(state, nid): return next(n for n in state.nodes if n["id"] == nid) def plain(state, nid): return hashing.plain(state.doc(), nid) def assert_values_recompute(state): """The values kept with a state equal a full computation from its nodes.""" assert ( state.index().values == hashing.Index.build(state.doc(), hashing.PLAIN).values ) def gate_state(case): """The state a corrections vector starts from: its nodes without hash values.""" return corrections.State( case["doc"], [ {k: v for k, v in n.items() if k not in VALUES} for n in case["before"]["nodes"] ], references=case["before"].get("references"), ) @pytest.mark.parametrize("case", vector_cases("corrections"), ids=lambda c: c["id"]) def test_vector_case(case): from paperspec.cli.build import correction_expect state = gate_state(case) if "refusal" in case["expect"]: with pytest.raises(Refusal) as raised: corrections.apply(state, case["correction"], store=STORE) assert raised.value.rule == case["expect"]["refusal"] return after, record = corrections.apply(state, case["correction"], store=STORE) assert_values_recompute(after) got = correction_expect(after, record, "references" in case["before"]) assert got == case["expect"] class TestState: def test_copies_what_it_is_given(self, hale): nodes = copy.deepcopy(hale.nodes) state = corrections.State(hale.key, nodes, hale.ids, hale.corrections) nodes[0]["kind"] = "changed" assert state.nodes[0]["kind"] == "document" def test_rev_counts_corrections(self, hale): assert hale.rev == 12 assert corrections.State("k", hale.nodes).rev == 0 def test_used_ids_are_live_nodes_and_the_id_table(self, hale): used = hale.used_ids() assert {n["id"] for n in hale.nodes} <= used assert {e["id"] for e in hale.ids} <= used assert len(used) == len(hale.nodes) + len(hale.ids) def test_the_index_is_kept_per_hasher(self, hale): state = fresh(hale) first = state.index() assert state.index() is first keyed = hashing.Hasher(b"\x01" * 32) assert state.index(keyed) is not first assert state.index(keyed) is state.index(keyed) def test_doc_is_built_from_the_nodes(self, hale): doc = hale.doc() assert doc.root == "xxqgpzr" assert doc.nodes[EQUATION]["kind"] == "equation" @pytest.mark.parametrize( ("entries", "ordered"), [ ( [ {"id": "b", "kind": "retired", "rev": 1}, {"id": "a", "kind": "alias"}, ], ["a", "b"], ), ( [ {"id": "z", "kind": "retired", "rev": 1}, {"id": "a", "kind": "retired", "rev": 2}, {"id": "b", "kind": "retired", "rev": 1}, ], ["b", "z", "a"], ), ], ) def test_id_order_puts_aliases_first_then_revisions(self, entries, ordered): assert [e["id"] for e in sorted(entries, key=corrections.id_order)] == ordered class TestGate: def test_returns_the_next_state_and_leaves_the_given_one(self, hale): before = json.dumps([hale.nodes, hale.ids, hale.corrections], sort_keys=True) after, record = gate( hale, op="replace", target=ABSTRACT_SENTENCE, find="harmonic", replace="quadratic", ) assert ( json.dumps([hale.nodes, hale.ids, hale.corrections], sort_keys=True) == before ) assert hale.rev == 12 assert after.rev == 13 assert record["index"] == 13 assert after.corrections[-1] == record assert "quadratic bond constants" in plain(after, ABSTRACT_SENTENCE) assert_values_recompute(after) def test_the_record_validates_against_its_schema(self, hale): from paperspec.schema import validate _, record = gate( hale, op="replace", target=ABSTRACT_SENTENCE, find="harmonic", replace="quadratic", ) assert validate(STORE, "correction.schema.json", record) == [] def test_a_proposal_that_breaks_the_schema_is_refused(self, hale): error = refusal(hale, op="replace", target=ABSTRACT_SENTENCE) assert error.rule == "correction.schema" def test_an_unknown_operation_is_refused_by_the_schema(self, hale): error = refusal(hale, op="explode", target=ABSTRACT_SENTENCE) assert error.rule == "correction.schema" def test_apply_refuses_an_unknown_operation(self, hale): with pytest.raises(Refusal, match="unknown operation explode") as raised: corrections.apply(hale, proposal(op="explode", target=ABSTRACT_SENTENCE)) assert raised.value.rule == "correction.op" def test_the_target_must_be_live(self, hale): error = refusal(hale, op="replace", target="zzzzzzz", find="a", replace="b") assert error.rule == "correction.target" assert "no live node zzzzzzz" in error.message @pytest.mark.parametrize( ("fields", "verb"), [ ({"op": "delete"}, "deleted"), ({"op": "reclassify", "kind": "front"}, "reclassifyd"), ({"op": "merge", "with": SECTION}, "merged"), ({"op": "move", "parent": SECTION}, "moved"), ], ) def test_the_root_stays(self, hale, fields, verb): error = refusal(hale, target="xxqgpzr", **fields) assert error.rule == "correction.target" assert verb in error.message class TestReplace: def test_changes_one_occurrence_of_a_word(self, hale): after, record = gate( hale, op="replace", target=INTRO, find="most common", replace="usual" ) assert plain(after, INTRO).startswith( "Harmonic bond terms remain the usual description" ) assert record["touched"] == [INTRO] assert record["before"] != record["after"] assert record["path"] == ["xxqgpzr", SECTION, FIRST_PARAGRAPH] def test_find_must_occur_exactly_once(self, hale): error = refusal( hale, op="replace", target=EQUATION_SENTENCE, find="bond", replace="x" ) assert error.rule == "correction.find" assert "3 times" in error.message error = refusal( hale, op="replace", target=INTRO, find="absent text", replace="x" ) assert "0 times" in error.message def test_run_narrows_the_search_to_one_run(self, hale): after, _ = gate( hale, op="replace", target=EQUATION_SENTENCE, find="bond", replace="link", run=0, ) assert plain(after, EQUATION_SENTENCE).startswith("The link energy") assert "bond force constant" in plain(after, EQUATION_SENTENCE) def test_an_empty_find_is_refused(self, hale): with pytest.raises(Refusal, match="find must not be empty") as raised: corrections.apply( hale, proposal(op="replace", target=INTRO, find="", replace="x") ) assert raised.value.rule == "correction.find" def test_the_source_of_an_equation_is_replaced_not_emended(self, hale): after, record = gate( hale, op="replace", target=EQUATION, find="K_r", replace="k_r" ) assert "k_r" in node(after, EQUATION)["content"]["source"] assert record["touched"] == [EQUATION] def test_a_math_run_source_is_replaced(self, hale): after, _ = gate( hale, op="replace", target=EQUATION_SENTENCE, find="K_r", replace="k_r" ) runs = node(after, EQUATION_SENTENCE)["content"] assert next(r["source"] for r in runs if r["kind"] == "math") == "k_r" def test_code_text_is_replaced(self, hale): inserted, record = gate( hale, op="insert", parent=SECOND_SECTION, place_after=METHODS_PARAGRAPH, evidence=CROP, node={"kind": "code", "content": {"text": "run fit"}}, ) code = record["created"][0] after, _ = corrections.gate( inserted, proposal(op="replace", target=code, find="fit", replace="scan"), STORE, ) assert node(after, code)["content"]["text"] == "run scan" def test_runs_replace_text_inside_a_text_run_keeping_its_style(self, hale): after, _ = gate( hale, op="replace", target=ABSTRACT_SENTENCE, find="harmonic", replace=[{"kind": "text", "text": "harmonic", "style": ["em"]}], ) runs = node(after, ABSTRACT_SENTENCE)["content"] assert [r["text"] for r in runs] == [ "We fit ", "harmonic", " bond constants for twelve bonds in two steps, first to a scan and then to vibrational frequencies.", ] assert runs[1]["style"] == ["em"] assert_values_recompute(after) def test_runs_cannot_replace_inside_math(self, hale): error = refusal( hale, op="replace", target=EQUATION_SENTENCE, find="K_r", replace=[{"kind": "text", "text": "K"}], ) assert error.rule == "correction.replace" def test_equation_runs_are_added_by_insert(self, hale): error = refusal( hale, op="replace", target=ABSTRACT_SENTENCE, find="harmonic", replace=[{"kind": "equation", "node": EQUATION}], ) assert error.rule == "correction.replace" def test_only_an_emend_records_what_was_printed(self, hale): error = refusal( hale, op="replace", target=ABSTRACT_SENTENCE, find="harmonic", replace=[{"kind": "text", "text": "harmonic", "printed": "harmonik"}], ) assert error.rule == "correction.emend" def test_a_changed_digit_needs_crop_evidence(self, hale): error = refusal(hale, op="replace", target=SCAN, find="0.01", replace="0.02") assert error.rule == "correction.digits" after, _ = gate( hale, op="replace", target=SCAN, find="0.01", replace="0.02", evidence=CROP ) assert "0.02" in plain(after, SCAN) def test_a_node_without_text_has_nothing_to_find(self, hale): error = refusal( hale, op="replace", target=FIRST_PARAGRAPH, find="Harmonic", replace="x" ) assert error.rule == "correction.find" assert "0 times" in error.message def test_without_a_schema_store_node_shapes_are_not_checked(self, hale): change = proposal(op="set", target=CELL, attr="role", value="boss") after, _ = corrections.apply(hale, change) assert node(after, CELL)["attrs"]["role"] == "boss" with pytest.raises(Refusal) as raised: corrections.apply(hale, change, store=STORE) assert raised.value.rule == "doc.node.attrs" class TestInsert: def test_text_goes_into_a_run_at_an_offset(self, hale): after, record = gate( hale, op="insert", target=ABSTRACT_SENTENCE, run=0, offset=3, text="really ", evidence=CROP, ) assert plain(after, ABSTRACT_SENTENCE).startswith("We really fit") assert record["touched"] == [ABSTRACT_SENTENCE] def test_text_can_go_into_a_math_run_source(self, hale): after, _ = gate( hale, op="insert", target=EQUATION_SENTENCE, run=3, offset=1, text="x", evidence=CROP, ) assert node(after, EQUATION_SENTENCE)["content"][3]["source"] == "Kx_r" def test_inserting_needs_crop_evidence(self, hale): error = refusal( hale, op="insert", target=ABSTRACT_SENTENCE, run=0, offset=0, text="x" ) assert error.rule == "correction.evidence" @pytest.mark.parametrize( "fields", [ {"run": 9, "offset": 0, "text": "x"}, {"run": 0, "offset": 999, "text": "x"}, {"run": -1, "offset": 0, "text": "x"}, {"run": 0, "offset": 0, "text": 7}, ], ) def test_the_run_and_offset_must_exist(self, hale, fields): error = corrections_error( hale, op="insert", target=ABSTRACT_SENTENCE, evidence=CROP, **fields ) assert error.rule == "correction.insert" assert error.message == "bad run or offset" def test_a_node_without_runs_takes_no_text(self, hale): error = refusal( hale, op="insert", target=SECTION, run=0, offset=0, text="x", evidence=CROP ) assert error.rule == "correction.insert" assert "text is inserted into runs" in error.message def test_a_run_that_takes_no_text_is_refused(self, hale): error = refusal( hale, op="insert", target=EQUATION_SENTENCE, run=1, offset=0, text="x", evidence=CROP, ) assert error.rule == "correction.insert" def test_text_goes_into_a_footnote_mark(self, hale): after, _ = gate( hale, op="insert", target=FIGURE_SENTENCE, run=2, offset=1, text="b", evidence=CROP, ) assert node(after, FIGURE_SENTENCE)["content"][2]["text"] == "ab" def test_a_new_node_goes_under_a_parent_after_a_sibling(self, hale): after, record = gate( hale, op="insert", parent=ABSTRACT_PARAGRAPH, place_after=ABSTRACT_SENTENCE, evidence=CROP, node={"kind": "sentence", "content": [{"kind": "text", "text": "Added."}]}, ) created = record["created"][0] kids = after.doc().kids(ABSTRACT_PARAGRAPH) assert kids == [ABSTRACT_SENTENCE, created, "v3063qv"] assert record["target"] == created assert record["touched"] == [created] assert created not in hale.used_ids() def test_a_new_node_goes_first_without_a_sibling(self, hale): after, record = gate( hale, op="insert", parent=ABSTRACT_PARAGRAPH, evidence=CROP, node={"kind": "sentence", "content": [{"kind": "text", "text": "Added."}]}, ) assert after.doc().kids(ABSTRACT_PARAGRAPH)[0] == record["created"][0] def test_a_new_subtree_is_minted_with_fresh_ids(self, hale): _, record = gate( hale, op="insert", parent=SECTION, place_after=SECOND_PARAGRAPH, evidence=CROP, node={ "kind": "paragraph", "children": [ {"kind": "sentence", "content": [{"kind": "text", "text": "One."}]}, {"kind": "sentence", "content": [{"kind": "text", "text": "Two."}]}, ], }, ) assert len(record["created"]) == 3 assert len(set(record["created"]) | hale.used_ids()) == len(hale.used_ids()) + 3 assert len(record["touched"]) == 2 def test_a_new_node_needs_crop_evidence(self, hale): error = refusal( hale, op="insert", parent=ABSTRACT_PARAGRAPH, node={"kind": "sentence", "content": [{"kind": "text", "text": "Added."}]}, ) assert error.rule == "correction.evidence" def test_a_new_node_needs_a_live_parent(self, hale): error = refusal( hale, op="insert", parent="zzzzzzz", evidence=CROP, node={"kind": "sentence", "content": [{"kind": "text", "text": "Added."}]}, ) assert error.rule == "correction.target" def test_the_sibling_must_belong_to_the_parent(self, hale): error = refusal( hale, op="insert", parent=ABSTRACT_PARAGRAPH, place_after=INTRO, evidence=CROP, node={"kind": "sentence", "content": [{"kind": "text", "text": "Added."}]}, ) assert error.rule == "correction.target" assert "not a child of the parent" in error.message def test_only_an_emend_records_what_was_printed(self, hale): error = refusal( hale, op="insert", parent=ABSTRACT_PARAGRAPH, evidence=CROP, node={ "kind": "sentence", "content": [{"kind": "text", "text": "Added.", "printed": "Aded."}], }, ) assert error.rule == "correction.emend" def test_a_new_node_in_the_wrong_place_breaks_the_tree(self, hale): error = refusal( hale, op="insert", parent=ABSTRACT_PARAGRAPH, evidence=CROP, node={ "kind": "paragraph", "children": [ { "kind": "sentence", "content": [{"kind": "text", "text": "Added."}], } ], }, ) assert error.rule == "doc.tree.child" def test_text_cannot_go_into_an_emended_run(self, hale): emended, _ = gate( hale, op="emend", kind="wording", target=ABSTRACT_SENTENCE, find="harmonic", replace="Harmonic", ) index = next( i for i, r in enumerate(node(emended, ABSTRACT_SENTENCE)["content"]) if "printed" in r ) error = corrections_error( emended, op="insert", target=ABSTRACT_SENTENCE, run=index, offset=0, text="x", evidence=CROP, ) assert error.rule == "correction.emend" def sentence_spec(text): return {"kind": "sentence", "content": [{"kind": "text", "text": text}]} def insert_theorem(state): """The state with a lemma added after the second paragraph, and its ID.""" after, record = corrections.gate( state, proposal( op="insert", parent=SECTION, place_after=SECOND_PARAGRAPH, evidence=CROP, node={ "kind": "theorem", "label": "Lemma 1", "attrs": {"variant": "lemma"}, "children": [ {"kind": "paragraph", "children": [sentence_spec("It holds.")]} ], }, ), STORE, ) return after, record["target"] def corrections_error(state, **fields): """The refusal `corrections.apply` raises, without the schema step of the gate.""" with pytest.raises(Refusal) as raised: corrections.apply(state, proposal(**fields), store=STORE) return raised.value class TestDelete: def test_removes_a_node_and_retires_its_id(self, hale): after, record = gate(hale, op="delete", target=ABSTRACT_SENTENCE) assert ABSTRACT_SENTENCE not in {n["id"] for n in after.nodes} assert record["retired"] == [ABSTRACT_SENTENCE] assert record["touched"] == [ABSTRACT_SENTENCE] entry = next(e for e in after.ids if e["id"] == ABSTRACT_SENTENCE) assert entry == { "id": ABSTRACT_SENTENCE, "kind": "retired", "successor": None, "node_kind": "sentence", "rev": 13, } def test_a_subtree_retires_every_node(self, hale): after, record = gate( hale, op="delete", target=ABSTRACT_PARAGRAPH, evidence=CROP ) assert record["retired"] == [ABSTRACT_PARAGRAPH, ABSTRACT_SENTENCE, "v3063qv"] assert record["touched"] == sorted([ABSTRACT_SENTENCE, "v3063qv"]) assert {e["id"] for e in after.ids} >= set(record["retired"]) def test_the_id_table_stays_in_order(self, hale): after, _ = gate(hale, op="delete", target=ABSTRACT_SENTENCE) assert after.ids == sorted(after.ids, key=corrections.id_order) def test_a_node_the_tree_needs_cannot_go(self, hale): error = refusal(hale, op="delete", target="wrxng89") assert error.rule == "doc.tree.empty" class TestSplit: def runs_length(self, state, nid, count): return len(hashing.plain_runs(node(state, nid)["content"][:count], state.doc())) def test_cuts_a_sentence_inside_a_text_run(self, hale): after, record = gate(hale, op="split", target=SCAN, offset=30) second = record["created"][0] assert plain(after, SCAN) == "Each bond was scanned in steps" assert plain(after, second).startswith("of 0.01 Å around its minimum") assert record["touched"] == sorted([SCAN, second]) assert node(after, second)["fidelity"] == node(hale, SCAN)["fidelity"] siblings = after.doc().kids(node(hale, SCAN)["parent"]) assert siblings.index(second) == siblings.index(SCAN) + 1 assert_values_recompute(after) def test_cuts_between_runs(self, hale): offset = self.runs_length(hale, "ec5jc93", 2) after, record = gate(hale, op="split", target="ec5jc93", offset=offset) second = record["created"][0] assert plain(after, "ec5jc93") == "Values below 10\N{MINUS SIGN}3" assert plain(after, second) == "kcal mol⁻¹ were treated as zero." def test_carries_an_equation_run_to_the_new_sentence(self, hale): offset = self.runs_length(hale, EQUATION_SENTENCE, 1) after, record = gate(hale, op="split", target=EQUATION_SENTENCE, offset=offset) second = record["created"][0] assert after.doc().kids(second) == [EQUATION] assert after.doc().kids(EQUATION_SENTENCE) == [] assert_values_recompute(after) def test_only_sentences_are_split(self, hale): error = refusal(hale, op="split", target=FIRST_PARAGRAPH, offset=1) assert error.rule == "correction.split" assert "only sentences are split" in error.message @pytest.mark.parametrize("offset", [0, 1000]) def test_the_offset_must_fall_inside(self, hale, offset): error = corrections_error(hale, op="split", target=SCAN, offset=offset) assert error.rule == "correction.split" assert "inside the sentence" in error.message def test_a_cut_inside_a_cross_reference_is_refused(self, hale): error = refusal(hale, op="split", target=TABLE_SENTENCE, offset=3) assert error.rule == "correction.split" assert "xref run" in error.message def test_a_cut_inside_an_emended_run_is_refused(self, hale): emended, _ = gate( hale, op="emend", kind="wording", target=ABSTRACT_SENTENCE, find="harmonic", replace="Harmonic", ) start = len("We fit ") error = corrections_error( emended, op="split", target=ABSTRACT_SENTENCE, offset=start + 3 ) assert error.rule == "correction.split" assert "emended run" in error.message def test_whitespace_at_the_cut_is_dropped(self, hale): after, record = gate( hale, op="split", target=SCAN, offset=len("Each bond was scanned") ) second = record["created"][0] assert not plain(after, SCAN).endswith(" ") assert not plain(after, second).startswith(" ") def test_whitespace_an_emend_printed_at_the_cut_is_dropped_too(self, hale): emended, _ = gate( hale, op="emend", kind="wording", target=ABSTRACT_SENTENCE, find=" harmonic ", replace=" Harmonic ", ) offset = len("We fit") + len(" Harmonic ") after, _ = corrections.gate( emended, proposal(op="split", target=ABSTRACT_SENTENCE, offset=offset), STORE, ) last = node(after, ABSTRACT_SENTENCE)["content"][-1] assert (last["text"], last["printed"]) == (" Harmonic", " harmonic") def test_a_sentence_without_fidelity_gives_none_to_its_second_part(self, hale): nodes = [ {k: v for k, v in n.items() if not (n["id"] == SCAN and k == "fidelity")} for n in hale.nodes ] bare = corrections.State(hale.key, nodes, hale.ids, hale.corrections) after, record = corrections.apply( bare, proposal(op="split", target=SCAN, offset=30) ) assert "fidelity" not in node(after, record["created"][0]) def test_a_part_with_only_whitespace_is_refused(self, hale): padded, _ = gate( hale, op="insert", target=ABSTRACT_SENTENCE, run=0, offset=len(plain(hale, ABSTRACT_SENTENCE)), text=" ", evidence=CROP, ) error = corrections_error( padded, op="split", target=ABSTRACT_SENTENCE, offset=len(plain(padded, ABSTRACT_SENTENCE)) - 1, ) assert error.rule == "correction.split" assert "both parts must keep text" in error.message class TestMerge: def test_joins_two_sentences_with_a_space(self, hale): after, record = gate(hale, op="merge", target=SCAN, **{"with": FIGURE_SENTENCE}) assert plain(after, SCAN).endswith(" " + plain(hale, FIGURE_SENTENCE)) assert record["retired"] == [FIGURE_SENTENCE] assert record["touched"] == [FIGURE_SENTENCE, SCAN] entry = next(e for e in after.ids if e["id"] == FIGURE_SENTENCE) assert entry["successor"] == SCAN def test_joins_with_a_chosen_joiner_or_none(self, hale): glued, _ = gate( hale, op="merge", target=SCAN, joiner="", **{"with": FIGURE_SENTENCE} ) assert plain(glued, SCAN) == plain(hale, SCAN) + plain(hale, FIGURE_SENTENCE) dashed, _ = gate( hale, op="merge", target=SCAN, joiner=" - ", **{"with": FIGURE_SENTENCE} ) assert " - " in plain(dashed, SCAN) def test_joins_two_paragraphs_and_moves_the_children(self, hale): after, record = gate( hale, op="merge", target=FIRST_PARAGRAPH, **{"with": SECOND_PARAGRAPH} ) assert after.doc().kids(FIRST_PARAGRAPH) == [ *hale.doc().kids(FIRST_PARAGRAPH), *hale.doc().kids(SECOND_PARAGRAPH), ] assert record["retired"] == [SECOND_PARAGRAPH] assert SECOND_PARAGRAPH not in {n["id"] for n in after.nodes} def test_the_other_node_must_exist(self, hale): error = refusal(hale, op="merge", target=SCAN, **{"with": "zzzzzzz"}) assert error.rule == "correction.target" def test_the_kinds_must_match(self, hale): error = refusal( hale, op="merge", target=FIRST_PARAGRAPH, **{"with": ABSTRACT_SENTENCE} ) assert error.rule == "correction.merge" def test_a_kind_that_does_not_merge_is_refused(self, hale): error = refusal(hale, op="merge", target=SECTION, **{"with": SECOND_SECTION}) assert error.rule == "correction.merge" def test_the_other_must_be_the_next_sibling(self, hale): error = refusal( hale, op="merge", target=FIRST_PARAGRAPH, **{"with": METHODS_PARAGRAPH} ) assert error.rule == "correction.merge" class TestMove: def test_moves_a_node_under_another_parent(self, hale): after, record = gate( hale, op="move", target=SECOND_PARAGRAPH, parent=SECOND_SECTION, place_after="m27nkzr", ) assert node(after, SECOND_PARAGRAPH)["parent"] == SECOND_SECTION assert after.doc().kids(SECOND_SECTION)[:2] == ["m27nkzr", SECOND_PARAGRAPH] assert record["to_path"] == ["xxqgpzr", SECOND_SECTION] assert record["touched"] == sorted(["ec5jc93", TABLE_SENTENCE]) def test_moves_after_a_sibling(self, hale): after, _ = gate( hale, op="move", target=FIRST_PARAGRAPH, parent=SECTION, place_after=SECOND_PARAGRAPH, ) assert after.doc().kids(SECTION) == [ "j8rdzk6", SECOND_PARAGRAPH, FIRST_PARAGRAPH, ] @pytest.mark.parametrize( "fields", [ {"parent": "zzzzzzz"}, {"parent": FIRST_PARAGRAPH}, {"parent": SECTION, "place_after": FIRST_PARAGRAPH}, ], ) def test_the_destination_must_be_elsewhere(self, hale, fields): error = refusal(hale, op="move", target=FIRST_PARAGRAPH, **fields) assert error.rule == "correction.move" def test_the_sibling_must_belong_to_the_destination(self, hale): error = refusal( hale, op="move", target=FIRST_PARAGRAPH, parent=SECOND_SECTION, place_after=SECOND_PARAGRAPH, ) assert error.rule == "correction.target" class TestReclassify: def test_changes_the_kind_and_drops_what_it_does_not_carry(self, hale): after, record = gate( hale, op="reclassify", target=SECOND_PARAGRAPH, kind="footnote" ) assert node(after, SECOND_PARAGRAPH)["kind"] == "footnote" assert record["from"] == "paragraph" def test_a_label_goes_when_the_new_kind_has_none(self, hale): after, record = gate( hale, op="reclassify", target=FIRST_PARAGRAPH, kind="footnote" ) relabelled, _ = corrections.gate( after, proposal(op="relabel", target=FIRST_PARAGRAPH, label="x"), STORE, ) assert node(relabelled, FIRST_PARAGRAPH)["label"] == "x" back, _ = corrections.gate( relabelled, proposal(op="reclassify", target=FIRST_PARAGRAPH, kind="paragraph"), STORE, ) assert "label" not in node(back, FIRST_PARAGRAPH) assert record["from"] == "paragraph" def test_attributes_and_label_of_the_old_kind_are_dropped(self, hale): with_theorem, theorem = insert_theorem(hale) assert node(with_theorem, theorem)["attrs"] == {"variant": "lemma"} after, record = corrections.gate( with_theorem, proposal(op="reclassify", target=theorem, kind="quote"), STORE, ) assert node(after, theorem)["kind"] == "quote" assert "attrs" not in node(after, theorem) assert "label" not in node(after, theorem) assert record["from"] == "theorem" @pytest.mark.parametrize( ("target", "kind"), [ (ABSTRACT_SENTENCE, "figure"), (ABSTRACT_SENTENCE, "equation"), (EQUATION, "sentence"), ], ) def test_a_kind_of_another_shape_is_refused(self, hale, target, kind): error = refusal(hale, op="reclassify", target=target, kind=kind) assert error.rule == "correction.reclassify" assert "cannot become" in error.message def test_an_unknown_kind_is_refused(self, hale): error = corrections_error( hale, op="reclassify", target=SECOND_PARAGRAPH, kind="widget" ) assert error.rule == "correction.reclassify" class TestRelabel: def test_sets_a_label(self, hale): after, record = gate( hale, op="relabel", target=SECOND_SECTION, label="3", evidence=CROP ) assert node(after, SECOND_SECTION)["label"] == "3" assert record["from"] == "2" assert record["touched"] == [] def test_a_null_label_removes_it(self, hale): after, record = gate( hale, op="relabel", target=SECOND_SECTION, label=None, evidence=CROP ) assert "label" not in node(after, SECOND_SECTION) assert record["label"] is None def test_a_leaf_with_a_label_is_touched(self, hale): after, record = gate( hale, op="relabel", target=EQUATION, label="(2)", evidence=CROP ) assert node(after, EQUATION)["label"] == "(2)" assert record["touched"] == [EQUATION] def test_a_kind_without_labels_is_refused(self, hale): error = refusal(hale, op="relabel", target=ABSTRACT_SENTENCE, label="a") assert error.rule == "correction.relabel" def test_a_changed_digit_needs_crop_evidence(self, hale): error = refusal(hale, op="relabel", target=SECOND_SECTION, label="3") assert error.rule == "correction.digits" class TestSet: def test_sets_an_attribute_and_remembers_the_old_value(self, hale): after, record = gate(hale, op="set", target=SECTION, attr="level", value=2) assert node(after, SECTION)["attrs"]["level"] == 2 assert record["from"] == 1 def test_a_leaf_is_touched(self, hale): after, record = gate(hale, op="set", target=CELL, attr="colspan", value=2) assert node(after, CELL)["attrs"]["colspan"] == 2 assert record["touched"] == [CELL] def test_an_attribute_a_kind_does_not_take_is_refused(self, hale): error = refusal(hale, op="set", target=SECTION, attr="colspan", value=2) assert error.rule == "correction.set" def test_a_value_of_the_wrong_shape_is_refused(self, hale): error = refusal(hale, op="set", target=SECTION, attr="level", value="high") assert error.rule == "doc.node.attrs" def test_an_attribute_that_was_unset_has_no_old_value(self, hale): inserted, record = gate( hale, op="insert", parent=SECTION, place_after=SECOND_PARAGRAPH, evidence=CROP, node={ "kind": "list", "children": [ { "kind": "item", "children": [ { "kind": "paragraph", "children": [sentence_spec("Entry.")], } ], } ], }, ) after, change = corrections.gate( inserted, proposal(op="set", target=record["target"], attr="ordered", value=True), STORE, ) assert change["from"] is None assert node(after, record["target"])["attrs"] == {"ordered": True} class TestEmend: def test_keeps_what_was_printed(self, hale): after, record = gate( hale, op="emend", kind="wording", target=ABSTRACT_SENTENCE, find="harmonic", replace="Harmonic", ) runs = node(after, ABSTRACT_SENTENCE)["content"] assert [r["text"] for r in runs][1] == "Harmonic" assert runs[1]["printed"] == "harmonic" assert record["touched"] == [ABSTRACT_SENTENCE] assert hashing.printed(after.doc(), ABSTRACT_SENTENCE) == plain( hale, ABSTRACT_SENTENCE ) def test_an_emend_that_changes_nothing_leaves_the_node_as_it_was(self, hale): after, record = gate( hale, op="emend", kind="wording", target=ABSTRACT_SENTENCE, find="harmonic", replace="harmonic", ) assert node(after, ABSTRACT_SENTENCE) == node(hale, ABSTRACT_SENTENCE) assert record["before"] == record["after"] assert after.rev == hale.rev + 1 def test_a_second_emend_keeps_the_first_printed_text(self, hale): once, _ = gate( hale, op="emend", kind="wording", target=ABSTRACT_SENTENCE, find="harmonic", replace="Harmonic", ) twice, _ = corrections.gate( once, proposal( op="emend", kind="wording", target=ABSTRACT_SENTENCE, find="Harmonic", replace="Quadratic", ), STORE, ) runs = node(twice, ABSTRACT_SENTENCE)["content"] emended = [r for r in runs if "printed" in r] assert [(r["text"], r["printed"]) for r in emended] == [ ("Quadratic", "harmonic") ] def test_run_narrows_the_search(self, hale): after, _ = gate( hale, op="emend", kind="wording", target=EQUATION_SENTENCE, find="bond", replace="link", run=0, ) assert plain(after, EQUATION_SENTENCE).startswith("The link energy") def test_a_digit_is_never_emended(self, hale): error = refusal( hale, op="emend", kind="wording", target=SCAN, find="0.01", replace="0.02" ) assert error.rule == "correction.digits" def test_the_text_must_occur_once(self, hale): error = refusal( hale, op="emend", kind="wording", target=EQUATION_SENTENCE, find="bond", replace="link", ) assert error.rule == "correction.find" error = refusal( hale, op="emend", kind="wording", target=INTRO, find="absent", replace="x" ) assert error.rule == "correction.find" def test_the_text_may_not_be_empty(self, hale): error = corrections_error( hale, op="emend", kind="wording", target=INTRO, find="", replace="x" ) assert error.rule == "correction.find" def test_math_is_corrected_by_replace(self, hale): error = refusal( hale, op="emend", kind="wording", target=EQUATION_SENTENCE, find="K_r", replace="k_r", ) assert error.rule == "correction.emend" assert "not math" in error.message def test_a_run_of_math_named_by_index_is_refused(self, hale): error = refusal( hale, op="emend", kind="wording", target=EQUATION_SENTENCE, find="zzz", replace="x", run=3, ) assert error.rule == "correction.emend" def test_text_across_runs_is_refused(self, hale): error = refusal( hale, op="emend", kind="wording", target="ec5jc93", find="10\N{MINUS SIGN}3", replace="x", ) assert error.rule == "correction.emend" assert "crosses a run boundary" in error.message def test_a_node_without_runs_is_refused(self, hale): error = refusal( hale, op="emend", kind="wording", target=FIRST_PARAGRAPH, find="a", replace="b", ) assert error.rule == "correction.emend" assert "corrected by replace" in error.message def test_a_cite_run_keeps_its_references(self, hale): after, _ = gate( hale, op="emend", kind="wording", target=INTRO, find="remain", replace="remains", ) assert any(r["kind"] == "cite" for r in node(after, INTRO)["content"]) assert_values_recompute(after) class TestRelink: def test_links_a_text_run_to_references(self, hale): after, record = gate(hale, op="relink", target=INTRO, run=0, refs=[REFERENCE]) run = node(after, INTRO)["content"][0] assert run["kind"] == "cite" assert run["refs"] == [REFERENCE] assert record["from"] is None def test_changes_the_references_of_a_cite_run(self, hale): after, record = gate( hale, op="relink", target=INTRO, run=1, refs=[OTHER_REFERENCE] ) assert node(after, INTRO)["content"][1]["refs"] == [OTHER_REFERENCE] assert record["from"] == [REFERENCE] def test_unlinks_a_cite_run(self, hale): after, record = gate(hale, op="relink", target=INTRO, run=1, refs=None) assert node(after, INTRO)["content"][-1]["kind"] == "text" assert record["from"] == [REFERENCE] def test_an_emended_run_keeps_what_was_printed(self, hale): emended, _ = gate( hale, op="emend", kind="wording", target=INTRO, find="remain", replace="remains", ) index = next( i for i, r in enumerate(node(emended, INTRO)["content"]) if "printed" in r ) after, _ = corrections.gate( emended, proposal(op="relink", target=INTRO, run=index, refs=[REFERENCE]), STORE, ) assert node(after, INTRO)["content"][index]["printed"] == "remain" def test_a_text_run_cannot_be_unlinked(self, hale): error = refusal(hale, op="relink", target=INTRO, run=0, refs=None) assert error.rule == "correction.relink" def test_only_text_and_cite_runs_relink(self, hale): error = refusal( hale, op="relink", target=TABLE_SENTENCE, run=0, refs=[REFERENCE] ) assert error.rule == "correction.relink" @pytest.mark.parametrize( "fields", [{"run": 9, "refs": ["tb6syqq"]}, {"run": -1, "refs": ["tb6syqq"]}] ) def test_the_run_must_exist(self, hale, fields): error = corrections_error(hale, op="relink", target=INTRO, **fields) assert error.rule == "correction.relink" def test_a_node_without_runs_is_refused(self, hale): error = refusal( hale, op="relink", target=FIRST_PARAGRAPH, run=0, refs=[REFERENCE] ) assert error.rule == "correction.relink" @pytest.mark.parametrize("refs", [[], "tb6syqq"]) def test_the_references_are_a_non_empty_list(self, hale, refs): error = corrections_error(hale, op="relink", target=INTRO, run=1, refs=refs) assert error.rule == "correction.relink" assert "non-empty list" in error.message class TestResolve: def test_changes_the_record_of_a_resolved_reference(self, hale): after, record = gate( hale, op="resolve", target=REFERENCE, resolution={"type": "none"} ) held = next(r for r in after.references if r["node"] == REFERENCE) assert held["resolution"] == {"type": "none"} assert record["from"] == {"doi": "10.5555/example.ref1", "type": "doi"} assert ( next(r for r in hale.references if r["node"] == REFERENCE)["resolution"][ "type" ] == "doi" ) def test_adds_a_record_for_a_reference_without_one_in_document_order(self, hale): bare = corrections.State(hale.key, hale.nodes, hale.ids, hale.corrections) after, record = corrections.gate( bare, proposal(op="resolve", target=OTHER_REFERENCE, resolution={"type": "none"}), STORE, ) assert record["from"] == {"type": "none"} assert [r["node"] for r in after.references] == [OTHER_REFERENCE] both, _ = corrections.gate( after, proposal(op="resolve", target=REFERENCE, resolution={"type": "none"}), STORE, ) assert [r["node"] for r in both.references] == [REFERENCE, OTHER_REFERENCE] assert both.references[0]["label"] == "1" def test_only_a_reference_resolves(self, hale): error = refusal(hale, op="resolve", target=INTRO, resolution={"type": "none"}) assert error.rule == "correction.resolve" def test_the_resolution_must_be_a_known_type(self, hale): error = corrections_error( hale, op="resolve", target=REFERENCE, resolution={"type": "isbn"} ) assert error.rule == "correction.resolve" error = corrections_error( hale, op="resolve", target=REFERENCE, resolution="none" ) assert error.rule == "correction.resolve" class TestInvariants: @PROFILE @given(st.data()) def test_a_wording_change_keeps_the_incremental_values_exact(self, hale, data): sentences = [ n["id"] for n in hale.nodes if n["kind"] == "sentence" and len(n["content"]) == 1 and n["content"][0]["kind"] == "text" and not re.search(r"\d", n["content"][0]["text"]) ] target = data.draw(st.sampled_from(sentences)) text = node(hale, target)["content"][0]["text"] word = data.draw(st.sampled_from(re.findall(r"[A-Za-z]+", text))) assume(text.count(word) == 1) replacement = data.draw(st.from_regex(r"[a-z]{1,8}", fullmatch=True)) after, record = gate( hale, op="replace", target=target, find=word, replace=replacement ) assert plain(after, target) == text.replace(word, replacement) assert_values_recompute(after) assert record["touched"] == [target] assert after.rev == hale.rev + 1 @PROFILE @given(st.data()) def test_deleting_a_leaf_without_digits_keeps_the_rest_exact(self, hale, data): doc = hale.doc() deletable = [ n["id"] for n in hale.nodes if n["kind"] == "sentence" and len(doc.kids(n["parent"])) > 1 and not re.search(r"\d", plain(hale, n["id"])) ] target = data.draw(st.sampled_from(deletable)) after, record = gate(hale, op="delete", target=target) assert target not in {n["id"] for n in after.nodes} assert record["retired"] == [target] assert_values_recompute(after)