"""Automated Test Suite for UI Role-Based Navigation Matrix & Default Tab Resolution. Fulfills: - TC-UI-NAV-001: Persona-to-Default Tab Resolution Verification. - TC-UI-NAV-002: Non-Empty Operational Sidebar Invariant for all 11 PRD Roles. - TC-UI-DEADEND-001: Dead-end prevention invariant (no persona lands on a hidden/unauthorized tab). Standard: UP-AI v1.5 Phase IV Transition Quality Standards. """ import re import unittest from pathlib import Path class TestUINavigationMatrix(unittest.TestCase): """Parses and validates App.tsx navigation definitions to guarantee zero dead-end user journeys.""" @classmethod def setUpClass(cls): app_file = Path(__file__).resolve().parents[2] / "frontend" / "src" / "App.tsx" assert app_file.exists(), f"App.tsx not found at {app_file}" cls.app_code = app_file.read_text(encoding="utf-8") # 11 Canonical PRD Roles cls.ALL_PRD_ROLES = [ "Tenant Administrator", "Mandi Buyer", "Purchase Manager", "Yard Supervisor", "Quality Officer", "Sales Executive", "Sales Manager", "Logistics Coordinator", "Accounts Executive", "Partner / MD", "System Administrator" ] def _extract_nav_items(self): """Extracts ALL_NAV_ITEMS mapping tab_id -> list of allowed roles.""" nav_match = re.search(r"const ALL_NAV_ITEMS.*?=\s*\[(.*?)\];", self.app_code, re.DOTALL) self.assertIsNotNone(nav_match, "Could not extract ALL_NAV_ITEMS from App.tsx") block = nav_match.group(1) items = {} for item_match in re.finditer(r"id:\s*'([^']+)'(?:(?!id:).)*?allowedRoles:\s*\[(.*?)\]", block, re.DOTALL): tab_id = item_match.group(1) roles_str = item_match.group(2) roles = [r.strip().strip("'\"") for r in roles_str.split(",") if r.strip()] items[tab_id] = roles return items def _simulate_resolve_default_tab(self, roles): """Matches resolveDefaultTab implementation from App.tsx.""" if any(r in roles for r in ['Partner / MD', 'Tenant Administrator']): return 'dashboard' if any(r in roles for r in ['Mandi Buyer', 'Purchase Manager']): return 'market' if any(r in roles for r in ['Sales Executive', 'Sales Manager']): return 'sales' if any(r in roles for r in ['Accounts Executive']): return 'finance' if any(r in roles for r in ['Quality Officer', 'Lab Chemist']): return 'quality' if any(r in roles for r in ['Yard Supervisor', 'Warehouse Clerk']): return 'receiving' if any(r in roles for r in ['Logistics Coordinator']): return 'logistics' if any(r in roles for r in ['System Administrator']): return 'tenants' return 'market' def test_01_all_nav_items_extracted(self): """Asserts that all core tabs exist in ALL_NAV_ITEMS.""" nav_items = self._extract_nav_items() expected_tabs = [ 'market', 'procurement', 'receiving', 'quality', 'inventory', 'sales', 'logistics', 'finance', 'dashboard', 'admin', 'members', 'tenants', 'audit', 'sessions' ] for t in expected_tabs: self.assertIn(t, nav_items, f"Tab '{t}' is missing from ALL_NAV_ITEMS in App.tsx") def test_02_yard_supervisor_has_receiving_and_inventory(self): """TC-UI-NAV-001: Asserts Yard Supervisor is explicitly allowed on receiving and inventory.""" nav_items = self._extract_nav_items() self.assertIn("Yard Supervisor", nav_items["receiving"]) self.assertIn("Yard Supervisor", nav_items["inventory"]) def test_03_logistics_coordinator_has_logistics_and_inventory(self): """TC-UI-NAV-001: Asserts Logistics Coordinator is explicitly allowed on logistics and inventory.""" nav_items = self._extract_nav_items() self.assertIn("Logistics Coordinator", nav_items["logistics"]) self.assertIn("Logistics Coordinator", nav_items["inventory"]) def test_04_every_prd_role_has_valid_landing_tab(self): """TC-UI-DEADEND-001: Invariant — resolveDefaultTab MUST map to a tab permitted for that role.""" nav_items = self._extract_nav_items() for role in self.ALL_PRD_ROLES: default_tab = self._simulate_resolve_default_tab([role]) allowed_roles = nav_items.get(default_tab, []) # Check if role is allowed or if tab allows '*' is_allowed = ('*' in allowed_roles) or (role in allowed_roles) self.assertTrue( is_allowed, f"Role '{role}' defaults to tab '{default_tab}', but is NOT in its allowed roles: {allowed_roles}" ) def test_05_non_empty_operational_sidebar_for_all_roles(self): """TC-UI-NAV-002: Invariant — Every PRD role must see at least 2 navigation tabs (not just sessions).""" nav_items = self._extract_nav_items() for role in self.ALL_PRD_ROLES: visible_tabs = [ tab_id for tab_id, allowed_roles in nav_items.items() if '*' in allowed_roles or role in allowed_roles ] self.assertGreaterEqual( len(visible_tabs), 2, f"Role '{role}' only has {len(visible_tabs)} visible tabs ({visible_tabs}). Must have >= 2!" ) if __name__ == "__main__": unittest.main()