|
| 1 | +"""Read a field back after typing and confirm it holds the intended value. |
| 2 | +
|
| 3 | +``field_entry`` types into a control and *hopes* it landed: a slow IME, a focus |
| 4 | +steal, an input mask or an auto-format can silently mangle or drop characters, |
| 5 | +and nothing reads the field back to notice. This is distinct from |
| 6 | +``action_effect`` (did *anything* change near the target?) and |
| 7 | +``postcondition.text_present`` (does the text appear *anywhere* on screen?) — |
| 8 | +neither confirms *this* field now equals *this* value. |
| 9 | +
|
| 10 | +* :func:`compare_field_value` — pure: compare an expected and actual value under |
| 11 | + a match ``mode`` (``exact`` / ``trim`` / ``ci`` / ``normalized`` / |
| 12 | + ``contains``). |
| 13 | +* :func:`verify_field_value` — read the field through an injectable ``reader`` |
| 14 | + and compare. |
| 15 | +* :func:`fill_and_verify` — type through an injectable ``filler``, read back, and |
| 16 | + retry (optionally clearing first) until it matches or attempts run out. |
| 17 | +
|
| 18 | +The reader / filler seams default to the native accessibility value in the |
| 19 | +executor, but every comparison and retry decision is pure and testable without a |
| 20 | +real control. Imports no ``PySide6``. |
| 21 | +""" |
| 22 | +from typing import Any, Callable, Dict, Optional |
| 23 | + |
| 24 | +# Match modes. |
| 25 | +MATCH_EXACT = "exact" |
| 26 | +MATCH_TRIM = "trim" |
| 27 | +MATCH_CI = "ci" |
| 28 | +MATCH_NORMALIZED = "normalized" |
| 29 | +MATCH_CONTAINS = "contains" |
| 30 | + |
| 31 | +# A reader returns the field's current value; a filler types a value into it. |
| 32 | +FieldReader = Callable[[], Optional[str]] |
| 33 | +FieldFiller = Callable[[str], None] |
| 34 | + |
| 35 | + |
| 36 | +def _canonical(text: str, mode: str) -> str: |
| 37 | + """Canonicalize ``text`` for comparison under ``mode`` (pure).""" |
| 38 | + if mode in (MATCH_TRIM, MATCH_CI, MATCH_CONTAINS): |
| 39 | + text = text.strip() |
| 40 | + if mode in (MATCH_CI, MATCH_CONTAINS): |
| 41 | + text = text.casefold() |
| 42 | + if mode == MATCH_NORMALIZED: |
| 43 | + from je_auto_control.utils.text_normalize import normalize_text |
| 44 | + return normalize_text(text) |
| 45 | + return text |
| 46 | + |
| 47 | + |
| 48 | +def _as_text(value: Any) -> str: |
| 49 | + """Coerce a value to a string, treating ``None`` as empty.""" |
| 50 | + return "" if value is None else str(value) |
| 51 | + |
| 52 | + |
| 53 | +def compare_field_value(expected: Any, actual: Any, *, |
| 54 | + mode: str = MATCH_EXACT) -> Dict[str, Any]: |
| 55 | + """Compare ``expected`` against ``actual`` under ``mode`` (pure). |
| 56 | +
|
| 57 | + Returns ``{match, mode, expected, actual}``. ``contains`` is a (trimmed, |
| 58 | + case-insensitive) substring test; the others compare canonical equality. |
| 59 | + """ |
| 60 | + expected_text = _as_text(expected) |
| 61 | + actual_text = _as_text(actual) |
| 62 | + if mode == MATCH_CONTAINS: |
| 63 | + match = _canonical(expected_text, mode) in _canonical(actual_text, mode) |
| 64 | + else: |
| 65 | + match = _canonical(expected_text, mode) == _canonical(actual_text, mode) |
| 66 | + return {"match": bool(match), "mode": mode, |
| 67 | + "expected": expected_text, "actual": actual_text} |
| 68 | + |
| 69 | + |
| 70 | +def verify_field_value(expected: Any, *, reader: FieldReader, |
| 71 | + mode: str = MATCH_EXACT) -> Dict[str, Any]: |
| 72 | + """Read the field via ``reader`` and compare it to ``expected``. |
| 73 | +
|
| 74 | + Returns the :func:`compare_field_value` result for the value read back. |
| 75 | + """ |
| 76 | + return compare_field_value(expected, reader(), mode=mode) |
| 77 | + |
| 78 | + |
| 79 | +def fill_and_verify(value: Any, *, filler: FieldFiller, reader: FieldReader, |
| 80 | + attempts: int = 2, mode: str = MATCH_EXACT, |
| 81 | + clear: Optional[Callable[[], None]] = None |
| 82 | + ) -> Dict[str, Any]: |
| 83 | + """Type ``value`` via ``filler``, read it back, and retry until it matches. |
| 84 | +
|
| 85 | + Up to ``attempts`` tries; before each retry (not the first) ``clear`` is |
| 86 | + called if supplied. Returns the final :func:`compare_field_value` result |
| 87 | + with an added ``attempts`` count. |
| 88 | + """ |
| 89 | + total = max(1, int(attempts)) |
| 90 | + result: Dict[str, Any] = compare_field_value(value, None, mode=mode) |
| 91 | + used = 0 |
| 92 | + for used in range(1, total + 1): |
| 93 | + if clear is not None and used > 1: |
| 94 | + clear() |
| 95 | + filler(value) |
| 96 | + result = compare_field_value(value, reader(), mode=mode) |
| 97 | + if result["match"]: |
| 98 | + break |
| 99 | + result = dict(result) |
| 100 | + result["attempts"] = used |
| 101 | + return result |
0 commit comments