diff --git a/.github/workflows/packages.yml b/.github/workflows/packages.yml index 0c51135cef..b7142e7ffb 100644 --- a/.github/workflows/packages.yml +++ b/.github/workflows/packages.yml @@ -475,7 +475,7 @@ jobs: python - <<'PY' import xml.etree.ElementTree as ET cases = ET.parse("tests.xml").getroot().findall(".//testcase") - assert len(cases) == 142, f"expected 142 independent-store, surgical-heal, portal-readback, and mcpify controls, got {len(cases)}" + assert len(cases) == 161, f"expected 161 independent-store, surgical-heal, portal-readback, and mcpify controls, got {len(cases)}" # The PostgreSQL and MySQL quickstart controls need the database # services of verify-clean-install, which runs them; nothing else skips. skipped = {c.get("name") for c in cases if c.find("skipped") is not None} diff --git a/packages/verify/src/openadapt_verify/portal_readback.py b/packages/verify/src/openadapt_verify/portal_readback.py index ec4a33bb0c..f215de2d05 100644 --- a/packages/verify/src/openadapt_verify/portal_readback.py +++ b/packages/verify/src/openadapt_verify/portal_readback.py @@ -8,8 +8,11 @@ Requires an independent producer (``separate_session`` or ``system_of_record``), valid same-origin ``submit_url`` / ``summary_url``, ISO-8601 ``reloaded_at_utc``, a 64-hex ``summary_dom_sha256`` digest, and distinct non-empty DOM tokens - proving a fresh navigation/reload distinct from the submit DOM, then diffs - extracted summary fields against the expected intake record. + proving a fresh navigation/reload distinct from the submit DOM. It then + reads each field from the reloaded page itself: the visible value bound to + the field's label (or locator), compared as a whole normalized value. A + repeated label with different values is UNCERTAIN. Inline DOM bytes with a + declared producer cannot reach VERIFIED; a DOM file in a graded store can. 2. ``ConfirmationPdfOracle``: Enforces artifact provenance via @@ -56,8 +59,10 @@ "€": "€", "£": "£", } +# Groups: a sign before the symbol, the symbol, then the amount (which may carry +# its own sign). ``-$500`` and ``$-500`` both normalize to ``$:-500.00``. _CURRENCY_RE = re.compile( - r"^\s*([$€£])?\s*([+-]?\d{1,3}(?:,\d{3})*(?:\.\d+)?|[+-]?\d+(?:\.\d+)?)\s*$" + r"^\s*([+-])?\s*([$€£])?\s*([+-]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?)\s*$" ) _DATE_FORMATS = ( "%Y-%m-%d", @@ -232,13 +237,20 @@ def normalize_field_value(value: Any, *, field_type: str | None = None) -> str: continue m_cur = _CURRENCY_RE.match(text) - if m_cur and ( - field_type == "currency" or m_cur.group(1) is not None or "," in m_cur.group(2) + lead_sign = m_cur.group(1) if m_cur else None + if ( + m_cur + and not (lead_sign and m_cur.group(3)[0] in "+-") + and ( + field_type == "currency" + or m_cur.group(2) is not None + or "," in m_cur.group(3) + ) ): - raw_num = m_cur.group(2).replace(",", "") + raw_num = (lead_sign or "") + m_cur.group(3).replace(",", "") try: dec = Decimal(raw_num).quantize(Decimal("0.01")) - sym = m_cur.group(1) + sym = m_cur.group(2) if sym is not None: code = _CURRENCY_SYMBOL_TO_CODE.get(sym, sym) return f"{code}:{dec}" @@ -367,31 +379,225 @@ def _compare_fields( return tuple(checks), confirmed, refuted, abstained -class _VisibleTextExtractor(HTMLParser): - """Extract ordered visible text nodes from HTML, ignoring attributes and non-rendered elements.""" +_VOID_TAGS = frozenset( + { + "area", + "base", + "br", + "col", + "embed", + "hr", + "img", + "input", + "link", + "meta", + "param", + "source", + "track", + "wbr", + } +) +# Elements whose text is never rendered page text. +_NEVER_RENDERED_TAGS = frozenset( + {"script", "style", "noscript", "template", "head", "title"} +) +# Form controls: their text is editable state, not a persisted summary. It counts +# only when the control is the field being checked (see ``_bound_values``). +_FORM_CONTROL_TEXT_TAGS = frozenset({"textarea", "select", "option", "datalist"}) +_HIDDEN_STYLE_DECLS = frozenset( + {"display:none", "visibility:hidden", "visibility:collapse"} +) +# Input types whose ``value`` is a field value a person reads on the page. +_VALUE_INPUT_TYPES = frozenset( + {"", "text", "number", "email", "tel", "url", "search", "date", "month", "week"} +) + + +# Inline elements: text inside them joins the surrounding text into one node, so +# ``$500,000`` reads as ``$500,000``, never as ``$500``. +_INLINE_TAGS = frozenset( + { + "a", + "abbr", + "b", + "bdi", + "bdo", + "cite", + "code", + "data", + "del", + "dfn", + "em", + "font", + "i", + "ins", + "kbd", + "mark", + "nobr", + "q", + "s", + "samp", + "small", + "span", + "strike", + "strong", + "sub", + "sup", + "time", + "tt", + "u", + "var", + "wbr", + } +) + + +def _attrs_hide_element(tag: str, attrs: Mapping[str, str | None]) -> bool: + """Whether inline markup alone hides this element and its subtree.""" + if "hidden" in attrs: + return True + if (attrs.get("aria-hidden") or "").strip().lower() == "true": + return True + if tag == "input" and (attrs.get("type") or "").strip().lower() == "hidden": + return True + style = re.sub(r"\s+", "", (attrs.get("style") or "").lower()) + for decl in style.split(";"): + if decl.replace("!important", "") in _HIDDEN_STYLE_DECLS: + return True + return False + + +@dataclass(frozen=True) +class _TextNode: + """One run of visible text, and the element its block sits in. + + ``scope`` is the parent of the nearest non-inline element around the text. + A label in one node binds the next node only when both share a scope: two + cells of one table row, a ``dt``/``dd`` pair in one ``dl``, or two blocks in + one container. A header row never binds the next row's cells. + """ + + text: str + scope: int + + +class _VisibleDomExtractor(HTMLParser): + """Collect what a reloaded page shows, using inline markup only. - _SKIP_TAGS = frozenset({"script", "style", "noscript", "template", "head", "title"}) + - ``nodes``: visible text runs in document order. Text split only by inline + elements (``span``, ``b``, ``sup``, ``del`` ...) is one run. Comments, + attributes, ``script``/``style``/``template``/``head``, form-control text, + and subtrees hidden by ``hidden``, ``aria-hidden="true"`` or an inline + ``display:none`` / ``visibility:hidden`` style are left out. + - ``controls_by_id`` / ``controls_by_name``: values of visible, text-like + ``input`` and ``textarea`` controls. They are read only when a check binds + a field to that control by ``