"""AX computation tests for massCode — Developer Tools calculators + editor/status computations.""" import json import os import re import subprocess import time import pytest import rb_testgen_kit as kit RESULTS_DIR = os.environ.get("RESULTS_DIR", os.path.dirname(os.path.abspath(__file__))) def screenshot_assert(test_name, description, results_dir=None): results_dir = results_dir or RESULTS_DIR ss_dir = os.path.join(results_dir, "screenshots") os.makedirs(ss_dir, exist_ok=True) ss_path = os.path.join(ss_dir, f"{test_name}.png") try: subprocess.run(["screencapture", "-x", ss_path], timeout=5) except Exception: pass assertions_file = os.path.join(ss_dir, "assertions.json") assertions = {} if os.path.exists(assertions_file): try: with open(assertions_file) as f: assertions = json.load(f) except Exception: assertions = {} assertions[f"{test_name}.png"] = description with open(assertions_file, "w") as f: json.dump(assertions, f, indent=2) def _enable_web_ax(ax, timeout=10.0): """Chromium/Electron builds its web-content AX tree only once an AT announces itself.""" from ApplicationServices import AXUIElementSetAttributeValue try: AXUIElementSetAttributeValue(ax._app_ref, "AXEnhancedUserInterface", True) except Exception: pass deadline = time.time() + timeout while time.time() < deadline: if ax.find(role="label", max_results=1, max_depth=16): return True time.sleep(0.25) return False def app_ready(ax): """MANDATORY bring-up: build the web AX tree and foreground the app.""" _enable_web_ax(ax) try: from AppKit import NSRunningApplication a = NSRunningApplication.runningApplicationWithProcessIdentifier_(int(ax.get_pid())) if a is not None: a.activateWithOptions_(1 << 1) except Exception: pass time.sleep(0.4) return ax def labels(ax): return [l.value for l in ax.find(role="label", max_results=250, max_depth=26) if l.value] def field(ax, placeholder): for t in ax.find(role="text_field", max_results=15, max_depth=26): if (t.name or "") == placeholder: return t return None def fields(ax): return ax.find(role="text_field", max_results=12, max_depth=26) def text_areas(ax): return ax.find(role="text_area", max_results=8, max_depth=26) def popups(ax): return ax.find(role="popup", max_results=12, max_depth=26) def named_button(ax, name): for b in ax.find(role="button", max_results=40, max_depth=26): if (b.name or "").strip() == name.strip(): return b return None def press_labelled_group(ax, text): for g in ax.find(role="group", max_results=600, max_depth=26): if "AXPress" not in (g.actions or []): continue labs = ax.find(role="label", root_id=g.id, max_results=3, max_depth=2) if any((l.value or "").strip() == text for l in labs): return ax.action(g.id, "AXPress") return False def webarea(ax): w = ax.find(role="webarea", max_results=1, max_depth=6) return w[0] if w else None def menu_el(ax, path): items = ax.find(role="menu_bar_item", name=path[0], max_results=3, max_depth=3) if not items: return None cur = items[0] for lbl in path[1:]: kids = ax.find(role="menu_item", name=lbl, root_id=cur.id, max_results=3, max_depth=3) if not kids: return None cur = kids[0] return cur def rb_enter_menu(ax, path, settle=1.2): """NAVIGATION: fire a menu item, crossing into whatever surface it opens.""" el = menu_el(ax, path) if el is None: return False ok = ax.action(el.id, "AXPress") time.sleep(settle) return ok def rb_enter_close_surface(ax, settle=1.3): """NAVIGATION: leave Preferences / Developer Tools / Presentation.""" wa = webarea(ax) if wa is None: return False btns = ax.find(role="button", root_id=wa.id, max_results=5, max_depth=24) if not btns: return False ok = ax.action(btns[0].id, "AXPress") time.sleep(settle) return ok def rb_enter_devtool(ax, tool_name, settle=1.3): """NAVIGATION: open the Developer Tools surface (if needed) and select a tool.""" if "Developer Tools" not in labels(ax): rb_enter_menu(ax, ["massCode", "Developer Tools…"], settle=1.6) ok = press_labelled_group(ax, tool_name) time.sleep(settle) return ok def select_option(ax, popup_value, option_label, settle=1.0): """AXPress a .""" v = read_output(ax, 1) if v is not None and str(v).strip(): return v return read_result_any(ax, exclude=exclude) def case_convert(ax, text, button_name, in_settle=0.7, out_settle=0.8): """Set the Case Converter input, press one case button, return the Output String.""" set_input(ax, text, settle=in_settle) if not press_button(ax, button_name, settle=out_settle): return None return read_output_robust(ax) def set_url(ax, url, settle=1.1): """URL Parser input: a plain (text_field), with a text_area fallback.""" flds = fields(ax) if flds: ok = ax.set_value(flds[0].id, url) else: ta = text_areas(ax) ok = ax.set_value(ta[0].id, url) if ta else False time.sleep(settle) return ok def parsed_rows(ax): """{Component: Value} built from the Parsed URL table(s): every row has 2 cells, each holding one label.""" out = {} for r in ax.find(role="row", max_results=25, max_depth=28): cells = ax.find(role="cell", root_id=r.id, max_results=3, max_depth=2) vals = [] for c in cells: ls = ax.find(role="label", root_id=c.id, max_results=2, max_depth=2) vals.append(ls[0].value if ls else "") if len(vals) >= 2 and vals[0]: out[vals[0]] = vals[1] return out def select_option_any(ax, current_candidates, option_label): """Drive a