|
1 | 1 | """Deadline regression for mixed-case embedded recovery.""" |
2 | 2 |
|
| 3 | +from dataclasses import replace |
3 | 4 | from time import monotonic |
4 | 5 |
|
| 6 | +import pytest |
| 7 | + |
5 | 8 | from codex32 import Profile |
6 | | -from codex32._cli_input import _case_interpretation, _scheduled_candidates |
| 9 | +from codex32._cli_input import _case_interpretation, _correction_candidates, _scheduled_candidates |
7 | 10 |
|
| 11 | +SOURCE = "ms10testsxxxxxxxxxxxxxxxxxxxxxxxxxx4nzvca9cmczlw" |
8 | 12 |
|
9 | | -def test_embedded_mixed_case_completes_both_interpretations_within_deadline() -> None: |
10 | | - source = "ms10testsxxxxxxxxxxxxxxxxxxxxxxxxxx4nzvca9cmczlw" |
11 | | - damaged = "ms10testsxPxxxxxPxxxxxPxxxxxPxxxxxP4nzvca9cmczlw" |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + ("damaged", "erasure_fixes", "normalized_fixes"), |
| 16 | + [ |
| 17 | + # Five minority-case P: five erasures are correctable, five substitutions are not. |
| 18 | + ("ms10testsxPxxxxxPxxxxxPxxxxxPxxxxxP4nzvca9cmczlw", True, False), |
| 19 | + # Fifteen minority-case X, one mistyped: fifteen erasures are not correctable, |
| 20 | + # one substitution after case normalization is. |
| 21 | + ("ms10testsXXXXXXXPXXXXXXXxxxxxxxxxxx4nzvca9cmczlw", False, True), |
| 22 | + ], |
| 23 | +) |
| 24 | +def test_embedded_mixed_case_recovers_either_sole_interpretation_within_deadline( |
| 25 | + damaged: str, erasure_fixes: bool, normalized_fixes: bool |
| 26 | +) -> None: |
12 | 27 | interpretation = _case_interpretation(damaged, "", (Profile.MS,), None) |
13 | 28 |
|
14 | 29 | assert interpretation is not None |
15 | 30 | direct, normalized, erased, _prefix = interpretation |
16 | 31 | assert direct is None |
17 | | - separator = normalized.rfind("1") |
18 | | - deadline = monotonic() + 10 |
| 32 | + immutable = normalized[: normalized.rfind("1") + 1] |
| 33 | + for value, fixes in ((erased, erasure_fixes), (normalized, normalized_fixes)): |
| 34 | + alone, alone_complete, _deadline = _correction_candidates( |
| 35 | + value, Profile.MS, None, immutable, deadline=monotonic() + 10, required_only=True |
| 36 | + ) |
| 37 | + assert alone_complete |
| 38 | + assert [candidate.artifact.text for candidate in alone] == ([SOURCE] if fixes else []) |
19 | 39 |
|
20 | 40 | candidates, complete = _scheduled_candidates( |
21 | 41 | normalized, |
22 | 42 | erased, |
23 | 43 | Profile.MS, |
24 | 44 | None, |
25 | | - normalized[: separator + 1], |
26 | | - deadline=deadline, |
| 45 | + immutable, |
| 46 | + deadline=monotonic() + 10, |
27 | 47 | ) |
28 | 48 |
|
29 | | - assert complete |
30 | | - assert len(candidates) == 1 |
31 | | - assert candidates[0].artifact.text == source |
| 49 | + # The exhaustive optional search may truncate at the deadline; the |
| 50 | + # candidate must still be found and must report that truncation. |
| 51 | + assert [candidate.artifact.text for candidate in candidates] == [SOURCE] |
| 52 | + assert candidates[0].search_complete is complete |
| 53 | + |
| 54 | + |
| 55 | +def test_scheduled_truncation_survives_a_complete_later_pass(monkeypatch: pytest.MonkeyPatch) -> None: |
| 56 | + damaged = "ms10testsxPxxxxxPxxxxxPxxxxxPxxxxxP4nzvca9cmczlw" |
| 57 | + interpretation = _case_interpretation(damaged, "", (Profile.MS,), None) |
| 58 | + assert interpretation is not None |
| 59 | + _direct, normalized, erased, _prefix = interpretation |
| 60 | + immutable = normalized[: normalized.rfind("1") + 1] |
| 61 | + found, _complete, _deadline = _correction_candidates( |
| 62 | + erased, Profile.MS, None, immutable, deadline=monotonic() + 10, required_only=True |
| 63 | + ) |
| 64 | + full_searches: list[str] = [] |
| 65 | + |
| 66 | + def truncated_first(value: str, *_args: object, **kwargs: object) -> tuple[object, bool, float]: |
| 67 | + if kwargs.get("required_only"): |
| 68 | + return (), True, 0.0 |
| 69 | + full_searches.append(value) |
| 70 | + if len(full_searches) == 1: |
| 71 | + return (replace(found[0], search_complete=False),), False, 0.0 |
| 72 | + return (), True, 0.0 |
| 73 | + |
| 74 | + monkeypatch.setattr("codex32._cli_input._correction_candidates", truncated_first) |
| 75 | + |
| 76 | + candidates, complete = _scheduled_candidates(normalized, erased, Profile.MS, None, immutable) |
| 77 | + |
| 78 | + assert full_searches == [erased, normalized] |
| 79 | + assert not complete |
| 80 | + assert [(candidate.artifact.text, candidate.search_complete) for candidate in candidates] == [ |
| 81 | + (SOURCE, False) |
| 82 | + ] |
0 commit comments