Datasets:
Download macos/masscodeio-masscode/tests/test_ax_menus.py from Qwen/RecreationBench: direct link, hf CLI and curl.
- Browser
- Download file 17.7 kB
-
https://huggingface.co/datasets/Qwen/RecreationBench/resolve/main/macos/masscodeio-masscode/tests/test_ax_menus.py
- Command line
-
hf download hf://datasets/Qwen/RecreationBench/macos/masscodeio-masscode/tests/test_ax_menus.py
-
curl -L -o test_ax_menus.py https://huggingface.co/datasets/Qwen/RecreationBench/resolve/main/macos/masscodeio-masscode/tests/test_ax_menus.py
17.7 kB
| """AX menu tests for massCode (Electron/Vue snippet manager).""" | |
| 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 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_bar_item_names(ax): | |
| return [m.name for m in ax.find(role="menu_bar_item", max_results=15, max_depth=2) if m.name] | |
| def submenu_item_names(ax, path): | |
| el = menu_el(ax, path) | |
| if el is None: | |
| return [] | |
| return [i.name for i in ax.find(role="menu_item", root_id=el.id, max_results=40, max_depth=3)] | |
| 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 mark_of(ax, path): | |
| """'✓' when a checkbox/radio menu item is checked, else None.""" | |
| el = menu_el(ax, path) | |
| if el is None: | |
| return None | |
| return ax.get(el.id, ["AXMenuItemMarkChar"]).get("AXMenuItemMarkChar") | |
| 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_context_menu(ax, row_label, settle=1.2): | |
| """NAVIGATION: AXShowMenu on the snippet-list row whose name label == row_label.""" | |
| for g in ax.find(role="group", max_results=600, max_depth=26): | |
| if "AXShowMenu" not in (g.actions or []): | |
| continue | |
| labs = ax.find(role="label", root_id=g.id, max_results=4, max_depth=3) | |
| if any((l.value or "") == row_label for l in labs): | |
| ok = ax.action(g.id, "AXShowMenu") | |
| time.sleep(settle) | |
| return ok | |
| 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 | |
| def rb_enter_new_snippet(ax): | |
| """NAVIGATION: create + select a fresh snippet (the editor surface).""" | |
| ok = rb_enter_menu(ax, ["File", "New Snippet"], settle=1.6) | |
| return ok | |
| # ── local, test-file-only utilities ─────────────────────────────────────────── | |
| APP_MENUS = ("massCode", "File", "View", "Edit", "Editor", "Markdown", "History", "Help") | |
| SORT_ITEMS = ("Date Modified", "Date Created", "Name") | |
| def named_items(names): | |
| """Drop separators (AXMenuItem rows with an empty title).""" | |
| return {n for n in names if n and n.strip()} | |
| def sheets(ax): | |
| return ax.find(role="sheet", max_results=3, max_depth=4) | |
| def sheet_ok_button(ax): | |
| """The alert's OK button, located by visible label + role (never by index).""" | |
| roots = [s.id for s in sheets(ax)] or [None] | |
| for root in roots: | |
| for b in ax.find(role="button", root_id=root, max_results=60, max_depth=10): | |
| if (b.name or "").strip() == "OK": | |
| return b | |
| return None | |
| def open_context_menu_item_names(ax, anchor): | |
| """Names of the visible pop-up menu that contains `anchor` (semantic lookup).""" | |
| for m in ax.find(role="menu", max_results=30, max_depth=4): | |
| kids = [i.name for i in ax.find(role="menu_item", root_id=m.id, | |
| max_results=40, max_depth=2)] | |
| if anchor in named_items(kids): | |
| return named_items(kids) | |
| # fallback: flat scan of shallow menu items (context menus hang near the root) | |
| flat = named_items(i.name for i in ax.find(role="menu_item", max_results=300, | |
| max_depth=6)) | |
| return flat if anchor in flat else set() | |
| def sort_marks(ax): | |
| return tuple(mark_of(ax, ["View", "Sort Snippets By", n]) for n in SORT_ITEMS) | |
| # ── 1-3 EXISTENCE / STRUCTURE (jump_kind="none", depth 0) ──────────────────── | |
| class TestMenuStructure: | |
| """Menu-bar shape: every menu node exists, is named, and has >= 1 item.""" | |
| def test_menu_bar_items(self, ax): | |
| app_ready(ax) | |
| names = menu_bar_item_names(ax) | |
| assert names, "no AXMenuBarItem exposed by the application" | |
| assert all(n and n.strip() for n in names), names | |
| missing = set(APP_MENUS) - set(names) | |
| assert not missing, f"missing menu-bar items {sorted(missing)}; got {names}" | |
| # total accessible menu-bar items >= the 8 app menus (+ the system Apple item) | |
| assert len(names) >= len(APP_MENUS), names | |
| screenshot_assert( | |
| "menus_menu_bar", | |
| "macOS menu bar while massCode is frontmost: the Apple item followed by the " | |
| "app menus massCode, File, View, Edit, Editor, Markdown, History and Help, " | |
| "above the massCode window's three-column layout.", | |
| ) | |
| def test_file_and_editor_submenu_items(self, ax): | |
| app_ready(ax) | |
| file_items = named_items(submenu_item_names(ax, ["File"])) | |
| for want in ("New Snippet", "New Fragment", "Add Description", "New Folder", "Find"): | |
| assert want in file_items, f"File menu missing {want!r}: {sorted(file_items)}" | |
| editor_items = named_items(submenu_item_names(ax, ["Editor"])) | |
| for want in ("Copy Snippet to Clipboard", "Format", "Preview Code", | |
| "Font Size Increase", "Font Size Decrease", "Font Size Reset"): | |
| assert want in editor_items, f"Editor menu missing {want!r}: {sorted(editor_items)}" | |
| # every app menu node must be reachable and hold at least one child row | |
| for menu in APP_MENUS: | |
| el = menu_el(ax, [menu]) | |
| assert el is not None, f"menu {menu!r} not reachable" | |
| assert (el.name or "").strip() == menu | |
| rows = submenu_item_names(ax, [menu]) | |
| assert len(rows) >= 1, f"menu {menu!r} exposes no menu items" | |
| assert len(named_items(rows)) >= 1, f"menu {menu!r} has no NAMED menu items" | |
| # ── 4-8 REACH (jump_kind="reach") ──────────────────────────────────────────── | |
| class TestMenuReach: | |
| """Safe menu items that cross into a new surface (SPA route, alert sheet, context menu).""" | |
| def test_settings_menu_reaches_preferences(self, ax): | |
| app_ready(ax) | |
| go_home(ax) | |
| before = kit.rb_showing(ax) | |
| assert rb_enter_menu(ax, ["massCode", "Settings…"], settle=1.4), \ | |
| "massCode > Settings… not reachable" | |
| after = kit.rb_showing(ax) | |
| assert kit.rb_reach(before, after, target="Preferences", source="All Snippets") | |
| screenshot_assert( | |
| "menus_preferences_open", | |
| "massCode Preferences page opened from the massCode > Settings... menu: title " | |
| "'Preferences', left rail listing Storage / Editor / Markdown / Appearance / " | |
| "Language, and the Storage pane with the storage path field; the sidebar's " | |
| "Inbox / Favorites / All Snippets / Trash rows are gone.", | |
| ) | |
| rb_enter_close_surface(ax) | |
| def test_devtools_menu_reaches_developer_tools(self, ax): | |
| app_ready(ax) | |
| go_home(ax) | |
| before = kit.rb_showing(ax) | |
| assert rb_enter_menu(ax, ["massCode", "Developer Tools…"], settle=1.4), \ | |
| "massCode > Developer Tools… not reachable" | |
| after = kit.rb_showing(ax) | |
| assert kit.rb_reach(before, after, target="Developer Tools", source="All Snippets") | |
| ls = labels(ax) | |
| assert "Case Converter" in ls, ls[:40] | |
| assert "Hash Generator" in ls, ls[:40] | |
| screenshot_assert( | |
| "menus_devtools_open", | |
| "massCode Developer Tools page opened from the massCode > Developer Tools... " | |
| "menu: heading 'Developer Tools' with a left rail grouped into Text Tools " | |
| "(Case Converter, Slug Generator, Sort Lines, URL Parser), Cryptography / " | |
| "Security (Hash Generator, HMAC Generator, Password Generator, UUID Generator) " | |
| "and Encoders / Decoders, plus the selected tool's Input/Output panes.", | |
| ) | |
| rb_enter_close_surface(ax) | |
| def test_about_menu_reaches_alert_sheet(self, ax): | |
| app_ready(ax) | |
| go_home(ax) | |
| before = kit.rb_showing(ax) | |
| assert rb_enter_menu(ax, ["massCode", "About massCode"], settle=1.8), \ | |
| "massCode > About massCode not reachable" | |
| after = kit.rb_showing(ax) | |
| assert kit.rb_reach(before, after, target="OK") | |
| assert len(sheets(ax)) == 1, "expected exactly one modal alert sheet" | |
| screenshot_assert( | |
| "menus_about_dialog", | |
| "About massCode modal alert sheet dropped over the main window: the massCode " | |
| "app icon, the bold title 'massCode', a detail block listing Version / " | |
| "Electron / Chrome / Node.js / V8 / OS, and a single default OK button.", | |
| ) | |
| ok = sheet_ok_button(ax) | |
| assert ok is not None, "no OK button inside the About alert sheet" | |
| assert ax.action(ok.id, "AXPress") | |
| time.sleep(1.0) | |
| assert len(sheets(ax)) == 0, "About sheet did not close after pressing OK" | |
| def test_add_description_menu_reveals_description_area(self, ax): | |
| app_ready(ax) | |
| go_home(ax) | |
| # surface 1: the snippet editor for a brand-new snippet | |
| assert rb_enter_new_snippet(ax), "File > New Snippet not reachable" | |
| before = kit.rb_showing(ax) | |
| # surface 2: the description area inside the snippet editor | |
| assert rb_enter_menu(ax, ["File", "Add Description"], settle=1.5), \ | |
| "File > Add Description not reachable" | |
| after = kit.rb_showing(ax) | |
| assert kit.rb_reach(before, after, target="Add description") | |
| # ── 9-11 TRANSITION (jump_kind="transition") ───────────────────────────────── | |
| class TestMenuToggles: | |
| """Checkbox / radio menu items — read the mark BEFORE, flip, compare, restore.""" | |
| def test_compact_mode_toggles_check_mark(self, ax): | |
| app_ready(ax) | |
| path = ["View", "Compact Mode"] | |
| assert menu_el(ax, path) is not None, "View > Compact Mode not reachable" | |
| before = mark_of(ax, path) | |
| assert rb_enter_menu(ax, path, settle=1.2) | |
| after = mark_of(ax, path) | |
| assert kit.rb_flip(before, after, expect="Compact Mode check mark toggles"), \ | |
| f"mark did not change: {before!r} -> {after!r}" | |
| assert {before, after} == {None, "✓"}, (before, after) | |
| # restore the persisted state | |
| rb_enter_menu(ax, path, settle=1.2) | |
| assert mark_of(ax, path) == before | |
| def test_hide_subfolder_snippets_toggles_check_mark(self, ax): | |
| app_ready(ax) | |
| path = ["View", "Hide Subfolder Snippets"] | |
| assert menu_el(ax, path) is not None, "View > Hide Subfolder Snippets not reachable" | |
| before = mark_of(ax, path) | |
| assert rb_enter_menu(ax, path, settle=1.2) | |
| after = mark_of(ax, path) | |
| assert kit.rb_flip(before, after, | |
| expect="Hide Subfolder Snippets check mark toggles"), \ | |
| f"mark did not change: {before!r} -> {after!r}" | |
| assert {before, after} == {None, "✓"}, (before, after) | |
| rb_enter_menu(ax, path, settle=1.2) | |
| assert mark_of(ax, path) == before | |
| def test_sort_by_radio_is_exclusive(self, ax): | |
| app_ready(ax) | |
| original = sort_marks(ax) | |
| assert len(original) == 3 | |
| assert rb_enter_menu(ax, ["View", "Sort Snippets By", "Name"], settle=1.2), \ | |
| "View > Sort Snippets By > Name not reachable" | |
| after_name = sort_marks(ax) | |
| assert list(after_name).count("✓") == 1, after_name | |
| assert after_name[SORT_ITEMS.index("Name")] == "✓", after_name | |
| assert rb_enter_menu(ax, ["View", "Sort Snippets By", "Date Created"], settle=1.2), \ | |
| "View > Sort Snippets By > Date Created not reachable" | |
| after_created = sort_marks(ax) | |
| assert kit.rb_flip(after_name, after_created, | |
| expect="sort radio check moves to Date Created"), \ | |
| f"marks unchanged: {after_name} -> {after_created}" | |
| assert list(after_created).count("✓") == 1, after_created | |
| assert after_created[SORT_ITEMS.index("Date Created")] == "✓", after_created | |
| # restore whatever sort order the app persisted before this test | |
| if "✓" in original: | |
| rb_enter_menu(ax, ["View", "Sort Snippets By", | |
| SORT_ITEMS[list(original).index("✓")]], settle=1.0) | |
| # ── 12 VALUE (jump_kind="value") ───────────────────────────────────────────── | |