diff --git a/packages/flow/benchmark/form_tx_heldout/MANIFEST.json b/packages/flow/benchmark/form_tx_heldout/MANIFEST.json index 27dc3de00e..30cb99c097 100644 --- a/packages/flow/benchmark/form_tx_heldout/MANIFEST.json +++ b/packages/flow/benchmark/form_tx_heldout/MANIFEST.json @@ -49,7 +49,7 @@ "drivers/__init__.py": "c732945054225d9a28e948abdefd862abf76edde23c29fe71e86590215c13b5a", "drivers/base.py": "6128d83baee6c8709609764af0540a3ddb788d4cd680cece7d0b95d6e5c2d9e2", "drivers/cobalt.py": "d5528f2dd6c207312b7f213168933b3fea49b4c1d217317faf950447c60acd80", - "drivers/harborline.py": "d78ec672760d874ddc99934a2b7a05a99d1587ef327739ba5b5c4cebe63d2993", + "drivers/harborline.py": "f59d386f4ed18c1a989c311cb75b0bff8b3b13a1dd0b1a8c784a624aa8629b77", "drivers/pinecrest.py": "ec7f388bab0170b9f23952ff57b2de1139018a07b87837137896c335b42d1799", "drivers/tidewater.py": "f3046691cb5670df27191925d9b61fdd4b2a16a3688a0dfc3cc135d2727f001c", "estimators.py": "795b85575e6caa2a80efab49e4d511e4e9a30d27f36283b341ae3306b9830d3c", diff --git a/packages/flow/benchmark/form_tx_heldout/drivers/harborline.py b/packages/flow/benchmark/form_tx_heldout/drivers/harborline.py index 3c7dbe12e4..e3f5cd0cc1 100644 --- a/packages/flow/benchmark/form_tx_heldout/drivers/harborline.py +++ b/packages/flow/benchmark/form_tx_heldout/drivers/harborline.py @@ -5,7 +5,7 @@ from typing import Any from urllib.parse import urlsplit -from .base import DriverError, Source, fmt_date, option_matches +from .base import DriverError, Source, digits, fmt_date, option_matches NAIVE_LABELS: dict[str, Any] = { "First Name": "applicant.first_name", @@ -112,6 +112,17 @@ def fill_rows(page: Any, src: Source) -> None: cell.locator("input[type=text]").fill(want) +# The portal hands figures of $500,000 or more back rounded to the nearest +# $10,000, so a value that really arrived is at most $5,000 off the display. +HANDOFF_TOLERANCE = 5000 + + +def handed_back(displayed: int, current: str) -> bool: + """Whether the parent box holds the value the estimator handed back.""" + got = digits(current) + return bool(got) and abs(int(got) - displayed) <= HANDOFF_TOLERANCE + + def run_estimator(page: Any, cell: Any, label: str, src: Source) -> None: with page.expect_popup() as info: cell.locator("input[value='Estimate RC']").click() @@ -123,9 +134,22 @@ def run_estimator(page: Any, cell: Any, label: str, src: Source) -> None: shown = pop.locator("#rce_value").inner_text() displayed = int(shown.replace("$", "").replace(",", "")) with pop.expect_event("close"): - pop.locator("input[value='Use This Value']").click() + # The button closes its own window; a click that loses that race + # raises after the click went through, so a closed popup is not an + # error by itself. Whether the value arrived is checked below. + try: + pop.locator("input[value='Use This Value']").click(no_wait_after=True) + except Exception: + if not pop.is_closed(): + raise box = cell.locator("input[type=text]") - final = src.estimator(label, displayed, box.input_value()) + current = box.input_value() + if src.kind == "reference" and not handed_back(displayed, current): + raise DriverError( + f"{label}: the estimator closed without handing ${displayed:,} " + f"to the form (the box holds {current!r})" + ) + final = src.estimator(label, displayed, current) if final is not None: box.fill(final) diff --git a/packages/flow/tests/test_form_tx_heldout_harborline_handoff.py b/packages/flow/tests/test_form_tx_heldout_harborline_handoff.py new file mode 100644 index 0000000000..3e18e2459d --- /dev/null +++ b/packages/flow/tests/test_form_tx_heldout_harborline_handoff.py @@ -0,0 +1,170 @@ +"""The reference human must see the estimator's value arrive in the form. + +Harborline's "Use This Value" button writes the estimate into the parent form +and closes its own window. Playwright's click can raise after that window +closes, so the driver accepts a closed popup. It must still fail when the +popup closed without writing a value to the parent box: otherwise the +reference human would type the answer-key value itself and the selfcheck +would pass on a case no real filler could finish. These tests drive +``run_estimator`` with stand-in page objects; no browser is started. +""" + +from __future__ import annotations + +from contextlib import contextmanager +from typing import Any, Iterator + +import pytest + +from benchmark.form_tx_heldout.drivers import harborline +from benchmark.form_tx_heldout.drivers.base import DriverError +from benchmark.form_tx_heldout.portals import PORTALS +from benchmark.form_tx_heldout.reference_human import make_source +from benchmark.form_tx_heldout.scoring import load_answer_key + +LABEL = "Dwelling Coverage (A)" + + +class Box: + def __init__(self, value: str = "") -> None: + self.value = value + + def input_value(self) -> str: + return self.value + + def fill(self, value: str) -> None: + self.value = value + + +class Button: + def __init__(self, on_click: Any) -> None: + self.on_click = on_click + + def click(self, **_kw: Any) -> None: + self.on_click() + + +class Text: + def __init__(self, text: str) -> None: + self.text = text + + def inner_text(self) -> str: + return self.text + + +class Popup: + """The estimator window: shows ``displayed`` and, on use, hands ``handed``.""" + + def __init__( + self, box: Box, displayed: int, handed: str | None, raise_on_close: bool + ) -> None: + self.box = box + self.displayed = displayed + self.handed = handed + self.raise_on_close = raise_on_close + self.closed = False + + def wait_for_load_state(self) -> None: + pass + + def is_closed(self) -> bool: + return self.closed + + @contextmanager + def expect_event(self, _name: str) -> Iterator[None]: + yield + + def use_it(self) -> None: + if self.handed is not None: + self.box.fill(self.handed) + self.closed = True + if self.raise_on_close: + raise RuntimeError("Target page, context or browser has been closed") + + def locator(self, sel: str) -> Any: + if sel == "#rce_value": + return Text(f"${self.displayed:,}") + if sel == "input[value='Use This Value']": + return Button(self.use_it) + return Button(lambda: None) # Calculate + + +class Cell: + def __init__(self, box: Box) -> None: + self.box = box + + def locator(self, sel: str) -> Any: + return self.box if sel == "input[type=text]" else Button(lambda: None) + + +class Page: + def __init__(self, pop: Popup) -> None: + self.pop = pop + + @contextmanager + def expect_popup(self) -> Iterator[Any]: + yield type("Info", (), {"value": self.pop})() + + +def harborline_case() -> tuple[Any, int]: + """A harborline case and its expected dwelling coverage, largest first.""" + cases = load_answer_key()["cases"] + cid = max( + PORTALS["harborline"].cases, + key=lambda c: int(cases[c]["expected_record"]["cov_a"]), + ) + return make_source("harborline", cid), int(cases[cid]["expected_record"]["cov_a"]) + + +def rounded(value: int) -> str: + handed = int(round(value / 10000.0)) * 10000 if value >= 500000 else value + return f"{handed:,}" + + +def run( + monkeypatch: pytest.MonkeyPatch, handed: str | None, raise_on_close: bool +) -> tuple[Box, Any, int]: + monkeypatch.setattr(harborline, "fill_rows", lambda _page, _src: None) + src, want = harborline_case() + box = Box() + pop = Popup(box, want, handed(want) if callable(handed) else handed, raise_on_close) + harborline.run_estimator(Page(pop), Cell(box), LABEL, src) + return box, src, want + + +@pytest.mark.parametrize("raise_on_close", [False, True]) +def test_rounded_handoff_is_corrected_to_the_answer( + monkeypatch: pytest.MonkeyPatch, raise_on_close: bool +) -> None: + box, src, want = run(monkeypatch, rounded, raise_on_close) + assert box.value == str(want) + assert src.problems == [] + + +@pytest.mark.parametrize("raise_on_close", [False, True]) +def test_popup_that_closes_without_handing_a_value_fails( + monkeypatch: pytest.MonkeyPatch, raise_on_close: bool +) -> None: + with pytest.raises(DriverError, match="closed without handing"): + run(monkeypatch, None, raise_on_close) + + +def test_popup_that_hands_a_wrong_value_fails(monkeypatch: pytest.MonkeyPatch) -> None: + with pytest.raises(DriverError, match="closed without handing"): + run(monkeypatch, lambda want: f"{want + 50000:,}", True) + + +def test_popup_that_raises_while_open_still_raises( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(harborline, "fill_rows", lambda _page, _src: None) + src, want = harborline_case() + box = Box() + pop = Popup(box, want, rounded(want), False) + + def broken() -> None: + raise RuntimeError("element is not attached") + + pop.use_it = broken # type: ignore[method-assign] + with pytest.raises(RuntimeError, match="not attached"): + harborline.run_estimator(Page(pop), Cell(box), LABEL, src)