Unit Test Role State Manager and Permission Checker
epic-role-based-access-control-state-and-services-task-009 — Write comprehensive unit tests for RoleStateManager (initial state, setActiveRole, switchRole, resetOnLogout, multi-role scenarios) and PermissionCheckerService (canAccess for all route/role combinations, pre-action validation with revoked permissions mid-session, globalAdmin blocked on mobile). Use flutter_test and mocktail to mock RoleRepository and Supabase dependencies. Target 90%+ coverage of the permission matrix.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 5 - 253 tasks
Can start after Tier 4 completes
Implementation Notes
Use ProviderContainer directly in tests (not WidgetTester) since these are pure service tests with no UI. Pattern: final container = ProviderContainer(overrides: [roleRepositoryProvider.overrideWithValue(mockRepo)]); addTearDown(container.dispose). For stream-based tests use expectLater(stream, emitsInOrder([...])) to assert ordered state emissions. The permission matrix test is best written as a data-driven test using a table of (route, role, expectedResult) tuples iterated in a single test case โ this makes the matrix exhaustive and readable.
Include a comment in the test file explaining which workshop requirement each permission rule implements (e.g., bulkRegister restricted to coordinator โ NHF/HLF workshop ยง2.4).
Testing Requirements
Use flutter_test for all tests. Use mocktail to create MockRoleRepository, MockSupabaseClient, and MockGoTrueClient. Organise tests into three test files: role_state_manager_test.dart, permission_checker_service_test.dart, pre_action_validation_test.dart. Each file should have a group() per method under test.
Use setUp() to construct a fresh ProviderContainer with overrideWithValue() for all mocked dependencies. After all tests, run flutter test --coverage and assert coverage report shows 90%+ for the two service files. Add a CI step that fails the build if coverage drops below threshold.
A coordinator's permissions could be revoked by an admin while they are actively using the app. If the permission checker relies solely on the cached role state from login, the coordinator could continue performing actions they are no longer authorized for until the next login.
Mitigation & Contingency
Mitigation: The Permission Checker Service must re-validate against the Role Repository (not just in-memory state) before high-impact actions. Implement a configurable staleness window (e.g., 15 minutes) after which role data is refreshed from Supabase in the background.
Contingency: If a revoked permission is detected during a pre-action check, immediately clear the cached role state, force a re-resolution from Supabase, and display an inline error explaining the permission change rather than crashing or silently failing.
Using both BLoC and Riverpod in the same state management layer for roles risks state synchronization bugs where one system updates before the other, causing widgets to render with stale role data during the switch transition.
Mitigation & Contingency
Mitigation: Choose a single primary state management approach (Riverpod StateNotifier is recommended) for role state and wrap the BLoC pattern within it if legacy code requires BLoC interfaces. Establish a single source-of-truth provider that all consumers read from.
Contingency: If synchronization bugs appear during integration testing, introduce a RoleStateReady gate widget that delays rendering of role-dependent UI until the state notifier emits a confirmed resolved state, preventing partial renders.
Hardcoded permission constants per role can become a maintenance burden as new features are added across 61 total features, leading to permission definitions that are scattered, stale, or inconsistent.
Mitigation & Contingency
Mitigation: Centralize all role-permission mappings in a single RolePermissions constants file with named action keys. Enforce that no widget or service directly checks role type strings; all checks must go through the Permission Checker Service.
Contingency: If permission definitions drift out of sync, introduce a validation test suite that cross-references all registered permission constants against their usage sites and fails the CI build if an undefined permission key is referenced.