r""" conditions/dummy_codes.py — the dummy codes DCONS and Dscprit get unchecked + zero-fee excluded, unconditionally, the same treatment j2930_j2001_update.py gives its own target codes. BHT3/BHT4 are NOT dummy codes (operator, 2026-10-06) and must be left alone. python tests est_conditions_dummy_codes.py Uses hcfa_claim.html, same as the j2930_j2001_update tests: no dummy code has a row of its own in that fixture, so `_inject_cpt_row()` adds one for whichever code a case needs, rather than growing the shared fixture. """ import sys import pathlib import inspect ROOT = pathlib.Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "src")) FIXTURE = pathlib.Path(__file__).resolve().parent / "fixtures" / "hcfa_claim.html" from ecw_bot.conditions import dummy_codes # noqa: E402 from ecw_bot.conditions.base import ConditionResult, run_all # noqa: E402 from ecw_bot.pages import claim_form # noqa: E402 from ecw_bot.selectors import claim_form as sel # noqa: E402 CASES = [] def case(fn): CASES.append(fn) return fn def _inject_cpt_row(page, code, row_id="CPTROW_3", checked=True, fee="45.00"): page.evaluate( """(args) => { const tbody = document.querySelector( '#claimCPTCodesTable8888888888888 tbody'); const addRowTr = tbody.querySelector('tr:last-child'); const tr = document.createElement('tr'); tr.id = args.rowId; tr.innerHTML = '' + '' + ''; tbody.insertBefore(tr, addRowTr); }""", {"code": code, "rowId": row_id, "checked": checked, "fee": fee}) @case def detect_true_when_dcons_is_on_the_claim(): data = {"cpt": [{"code": "99213"}, {"code": "DCONS"}]} assert dummy_codes.detect("C1", data) is True @case def detect_true_when_dscprit_is_on_the_claim(): data = {"cpt": [{"code": "Dscprit"}]} assert dummy_codes.detect("C1", data) is True @case def detect_ignores_case_and_spaces_of_the_code(): assert dummy_codes.detect("C1", {"cpt": [{"code": " DSCPRIT "}]}) is True assert dummy_codes.detect("C1", {"cpt": [{"code": "dcons"}]}) is True @case def detect_false_for_bht3_and_bht4_which_are_not_dummy_codes(): data = {"cpt": [{"code": "BHT3"}, {"code": "BHT4"}]} assert dummy_codes.detect("C1", data) is False @case def detect_false_when_no_dummy_code_is_present(): data = {"cpt": [{"code": "99213"}, {"code": "J7298"}]} assert dummy_codes.detect("C1", data) is False @case def detect_false_with_no_cpt_key_at_all(): assert dummy_codes.detect("C1", {}) is False @case def apply_unchecks_and_zeroes_dcons_only(page): page.goto(FIXTURE.as_uri()) _inject_cpt_row(page, "DCONS", checked=True, fee="45.00") data = {"cpt": [{"code": "DCONS"}]} result = dummy_codes.apply(page, "C1", data) assert isinstance(result, ConditionResult) assert result.condition_name == "dummy_codes" assert result.changed is True assert len(result.changes) == 1, result.changes c = result.changes[0] assert c["code"] == "DCONS", c assert c["unchecked"] is True, c assert c["fee"] == {"before": "45.00", "after": "0.00"}, c dcons_row = page.locator(sel.SEL_CPT_ROWS).nth(3) assert dcons_row.locator(sel.SEL_CPT_ASSIGN_TO_PATIENT).first.is_checked() is False assert dcons_row.locator(sel.SEL_CPT_BILLED_FEE).first.input_value() == "0.00" row0_fee = page.locator(sel.SEL_CPT_ROWS).nth(0).locator( sel.SEL_CPT_BILLED_FEE).first assert row0_fee.input_value() == "210.00", "99213 must be left alone — only DCONS is targeted" @case def apply_handles_both_dummy_codes_on_the_same_claim(page): page.goto(FIXTURE.as_uri()) _inject_cpt_row(page, "DCONS", row_id="CPTROW_3", checked=True, fee="75.00") _inject_cpt_row(page, "Dscprit", row_id="CPTROW_4", checked=True, fee="30.00") data = {"cpt": [{"code": "DCONS"}, {"code": "Dscprit"}]} result = dummy_codes.apply(page, "C1", data) assert result.changed is True by_code = {c["code"]: c for c in result.changes} assert set(by_code) == {"DCONS", "Dscprit"}, result.changes assert by_code["DCONS"]["unchecked"] is True, by_code assert by_code["DCONS"]["fee"] == {"before": "75.00", "after": "0.00"} assert by_code["Dscprit"]["unchecked"] is True, by_code assert by_code["Dscprit"]["fee"] == {"before": "30.00", "after": "0.00"} assert claim_form.cpt_code_present(page, "Dscprit"), "Dscprit's row must still exist — unchecked/zeroed, never removed" @case def apply_matches_a_dummy_code_whatever_its_case(page): page.goto(FIXTURE.as_uri()) _inject_cpt_row(page, "DSCPRIT", checked=True, fee="30.00") result = dummy_codes.apply(page, "C1", {"cpt": [{"code": "DSCPRIT"}]}) assert result.changed is True assert [c["code"] for c in result.changes] == ["DSCPRIT"], result.changes @case def apply_leaves_bht3_and_bht4_alone(page): page.goto(FIXTURE.as_uri()) _inject_cpt_row(page, "BHT3", row_id="CPTROW_3", checked=True, fee="45.00") _inject_cpt_row(page, "BHT4", row_id="CPTROW_4", checked=True, fee="120.00") result = dummy_codes.apply(page, "C1", {"cpt": [{"code": "BHT3"}, {"code": "BHT4"}]}) assert result.changed is False assert result.changes == [] for i, fee in ((3, "45.00"), (4, "120.00")): row = page.locator(sel.SEL_CPT_ROWS).nth(i) assert row.locator(sel.SEL_CPT_ASSIGN_TO_PATIENT).first.is_checked() is True assert row.locator(sel.SEL_CPT_BILLED_FEE).first.input_value() == fee @case def apply_is_a_no_op_once_dcons_is_already_excluded(page): page.goto(FIXTURE.as_uri()) _inject_cpt_row(page, "DCONS", checked=False, fee="0.00") data = {"cpt": [{"code": "DCONS"}]} result = dummy_codes.apply(page, "C1", data) assert result.changed is False assert result.changes == [] @case def apply_is_a_no_op_when_no_dummy_code_is_on_the_claim(page): page.goto(FIXTURE.as_uri()) data = {"cpt": [{"code": "99213"}]} result = dummy_codes.apply(page, "C1", data) assert result.changed is False assert result.changes == [] @case def describe_names_dcons_and_the_fee_change(page): page.goto(FIXTURE.as_uri()) _inject_cpt_row(page, "DCONS", checked=True, fee="45.00") data = {"cpt": [{"code": "DCONS"}]} result = dummy_codes.apply(page, "C1", data) text = dummy_codes.describe(result) assert "DCONS" in text, text assert "45.00" in text and "0.00" in text, text @case def run_all_fires_dummy_codes_alongside_others_independently(page): """ Against the REAL registry, not fakes — proves this condition is wired into conditions.CONDITIONS and does not collide with the depression-DX or telehealth conditions, which key off icd/progress_notes, not cpt. """ page.goto(FIXTURE.as_uri()) _inject_cpt_row(page, "DCONS", checked=True, fee="45.00") data = {"cpt": [{"code": "DCONS"}]} results = run_all(page, "C1", data) assert len(results) == 1, results assert results[0].condition_name == "dummy_codes", results def main() -> int: if not FIXTURE.exists(): print(f" FAIL fixture missing: {FIXTURE}") return 1 try: from playwright.sync_api import sync_playwright except ImportError: print(" SKIP playwright is not installed") return 0 failures = 0 with sync_playwright() as pw: browser = pw.chromium.launch(headless=True) page = browser.new_page() for fn in CASES: name = fn.__name__.replace("_", " ") try: if len(inspect.signature(fn).parameters) == 0: fn() else: fn(page) print(f" ok {name}") except AssertionError as exc: print(f" FAIL {name}: {exc}") failures += 1 except Exception as exc: print(f" ERROR {name}: {type(exc).__name__}: {exc}") failures += 1 browser.close() print(f"\n{len(CASES) - failures}/{len(CASES)} passed") return 1 if failures else 0 if __name__ == "__main__": sys.exit(main())