"""AX state tests for massCode — menu check marks, popup then AXPress one of the options it exposes.""" tgt = None for p in popups(ax): if (p.value or "") == popup_value: tgt = p break if tgt is None: return False ax.action(tgt.id, "AXPress") time.sleep(0.9) for p in popups(ax): for o in ax.find(role="menu_item", root_id=p.id, max_results=300, max_depth=4): if (o.value or "") == option_label: ok = ax.action(o.id, "AXPress") time.sleep(settle) return ok esc() return False def popup_value_now(ax, candidates): """Current value of whichever popup currently holds one of `candidates`.""" for p in popups(ax): if (p.value or "") in candidates: return p.value return None def checkbox_children(ax, label_text): """massCode renders checkboxes as a div whose tick only exists when CHECKED: the form-item group holding exactly this one label has 3 direct group children when CHECKED and 2 when UNCHECKED. Returns that count (or None).""" for g in ax.find(role="group", max_results=600, max_depth=26): labs = ax.find(role="label", root_id=g.id, max_results=4, max_depth=3) if len(labs) == 1 and (labs[0].value or "").strip() == label_text: return len(ax.find(role="group", root_id=g.id, max_results=6, max_depth=1)) return None def click_label(ax, label_text): """Toggle a custom checkbox by CG-clicking its visible label.""" for l in ax.find(role="label", max_results=300, max_depth=26): if (l.value or "").strip() == label_text: return ax.click_element_cg(l.id) return False def esc(): subprocess.run(["osascript", "-e", 'tell application "System Events" to key code 53'], capture_output=True, timeout=5) time.sleep(0.4) def go_home(ax): for _ in range(3): ls = labels(ax) if "Preferences" in ls or "Developer Tools" in ls or "1 / 0" in ls: if rb_enter_close_surface(ax): continue break if "Inbox" not in labels(ax): press_labelled_group(ax, "LIBRARY") time.sleep(0.8) return ax # ── local additions ───────────────────────────────────────────────────────── CHECK = "✓" # AXMenuItemMarkChar for a checked item SORT_OPTS = ("Date Modified", "Date Created", "Name") def _mk(mark): """Normalise AXMenuItemMarkChar: an UNCHECKED item reports None (some toolkits report ''), a CHECKED item reports its mark character. Keeps the CHECKED/UNCHECKED distinction while tolerating the empty-string spelling of 'no mark'.""" return mark if mark else None def sort_marks(ax): """(Date Modified, Date Created, Name) mark chars — the radio group's state.""" return tuple(_mk(mark_of(ax, ["View", "Sort Snippets By", o])) for o in SORT_OPTS) def is_enabled(ax, el): return bool(ax.get(el.id, ["AXEnabled"]).get("AXEnabled")) # ── menu CHECKED / radio state ────────────────────────────────────────────── class TestMenuCheckState: """Native menu-item CHECKED state (AXMenuItemMarkChar). massCode persists these in electron-store, so every test reads the mark BEFORE, flips it, and restores it.""" @pytest.mark.rb(depth=0, jump_kind="transition", expect="View ▸ Compact Mode AXMenuItemMarkChar flips None <-> '✓' " "and is restored to its entry value") def test_compact_mode_check_state_flips_and_restores(self, ax): app_ready(ax) path = ["View", "Compact Mode"] assert menu_el(ax, path) is not None, "View ▸ Compact Mode menu item missing" before = _mk(mark_of(ax, path)) assert rb_enter_menu(ax, path), "could not press View ▸ Compact Mode" after = _mk(mark_of(ax, path)) # restore BEFORE asserting — the setting persists across app relaunches rb_enter_menu(ax, path) restored = _mk(mark_of(ax, path)) assert kit.rb_flip(before, after, expect="Compact Mode ✓ toggles"), \ f"Compact Mode mark did not change: {before!r} -> {after!r}" assert {before, after} == {None, CHECK}, \ f"expected the mark to move between None and '✓', got {before!r}/{after!r}" assert restored == before, \ f"Compact Mode not restored: {restored!r} != {before!r}" @pytest.mark.rb(depth=0, jump_kind="transition", expect="View ▸ Hide Subfolder Snippets AXMenuItemMarkChar flips " "None <-> '✓' and is restored to its entry value") def test_hide_subfolder_snippets_check_state_flips_and_restores(self, ax): app_ready(ax) path = ["View", "Hide Subfolder Snippets"] assert menu_el(ax, path) is not None, "View ▸ Hide Subfolder Snippets missing" before = _mk(mark_of(ax, path)) assert rb_enter_menu(ax, path), "could not press View ▸ Hide Subfolder Snippets" after = _mk(mark_of(ax, path)) rb_enter_menu(ax, path) restored = _mk(mark_of(ax, path)) assert kit.rb_flip(before, after, expect="Hide Subfolder Snippets ✓ toggles"), \ f"Hide Subfolder Snippets mark did not change: {before!r} -> {after!r}" assert {before, after} == {None, CHECK}, \ f"expected the mark to move between None and '✓', got {before!r}/{after!r}" assert restored == before, \ f"Hide Subfolder Snippets not restored: {restored!r} != {before!r}" @pytest.mark.rb(depth=1, jump_kind="transition", expect="View ▸ Sort Snippets By submenu: exactly one of " "Date Modified / Date Created / Name is ✓, and the ✓ MOVES " "to whichever item is pressed (radio exclusivity)") def test_sort_radio_exclusive_and_moves(self, ax): app_ready(ax) for opt in SORT_OPTS: assert menu_el(ax, ["View", "Sort Snippets By", opt]) is not None, \ f"Sort Snippets By ▸ {opt} missing" original = sort_marks(ax) assert rb_enter_menu(ax, ["View", "Sort Snippets By", "Name"]), \ "could not press Sort Snippets By ▸ Name" m1 = sort_marks(ax) assert rb_enter_menu(ax, ["View", "Sort Snippets By", "Date Created"]), \ "could not press Sort Snippets By ▸ Date Created" m2 = sort_marks(ax) # restore the originally-checked option (this setting persists) orig_idx = [i for i, m in enumerate(original) if m == CHECK] if orig_idx: rb_enter_menu(ax, ["View", "Sort Snippets By", SORT_OPTS[orig_idx[0]]]) restored = sort_marks(ax) assert original.count(CHECK) == 1, \ f"exactly one sort option must be ✓ on entry, got {original!r}" assert m1 == (None, None, CHECK), \ f"after pressing Name expected (None, None, '✓'), got {m1!r}" assert m2 == (None, CHECK, None), \ f"after pressing Date Created expected (None, '✓', None), got {m2!r}" assert kit.rb_flip(m1, m2, expect="sort radio ✓ moves from Name to Date Created"), \ f"sort radio mark did not move: {m1!r} -> {m2!r}" assert restored == original, \ f"sort radio not restored: {restored!r} != {original!r}" # ── HTML