Write unit tests for NotificationRepository queries
epic-in-app-notification-centre-foundation-task-010 — Write unit tests for NotificationRepository using a mocked Supabase client. Cover: getNotifications returns correctly typed Notification list, getUnreadCount returns integer, markAsRead and markAllAsRead call correct RPC or update endpoints, deleteNotification issues correct delete, role-scoped query expansion applies correct filters for coordinator vs peer_mentor vs org_admin roles, and error cases (network failure) propagate as typed exceptions.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
The most complex part is mocking the Supabase query builder chain (`.from().select().eq().order()` etc.). Use mocktail's `when(...).thenAnswer(...)` to stub the terminal call (`.execute()` or equivalent) rather than each intermediate builder step — or use a fake implementation of the SupabaseClient interface if the builder chain is hard to mock. For role-scoped filter verification, capture the query parameters passed to the mock and assert their values rather than asserting on intermediate builder calls, which are brittle. Define a `NotificationRepositoryException` hierarchy in the domain layer so error propagation tests have a concrete type to assert against.
Keep fixture data minimal — one or two notification objects per test is sufficient.
Testing Requirements
Unit tests using flutter_test with a mock Supabase client (use mocktail or mockito — align with project convention). Place tests in `test/features/notifications/data/notification_repository_test.dart`. Structure test groups by method: getNotifications, getUnreadCount, markAsRead, markAllAsRead, deleteNotification, role-scoped filtering, error propagation. For role-scoped tests, instantiate the repository with each of the three role values and capture the query builder calls to verify correct filter application.
For error tests, configure the mock to throw a `PostgrestException` or similar and verify the repository wraps it in a domain-level exception. Do not test Supabase internals — test only the repository's public contract.
Supabase Realtime channels on mobile networks can drop silently. If reconnection logic is flawed, users miss notifications without knowing it, undermining the audit-trail guarantee.
Mitigation & Contingency
Mitigation: Implement exponential-backoff reconnection with a maximum of 5 retries; expose a channel-status stream to the BLoC so it can trigger a full-fetch fallback when the channel reconnects after a gap.
Contingency: If Realtime reliability proves insufficient in production, fall back to polling the repository every 60 seconds as a background supplement to the Realtime channel.
Coordinator and org-admin RLS expansions require joining user_roles and org_memberships tables. An incorrect policy could expose notifications to wrong users or block legitimate access entirely.
Mitigation & Contingency
Mitigation: Write dedicated RLS integration tests for each role (peer mentor, coordinator, org admin) using separate Supabase test projects. Review policies with the security checklist before merging.
Contingency: If an RLS defect is discovered post-deployment, disable the expanded-scope policy and revert to user-scoped-only access while a corrected migration is prepared and tested.
JSONB payload structure may vary across notification types created by different Edge Functions (reminder, expiry, scenario, pause). Missing or renamed fields will cause runtime parse failures.
Mitigation & Contingency
Mitigation: Define a canonical NotificationPayload union type in a shared schema document. Each Edge Function must validate its payload against this schema before inserting. Add fallback parsing with default values in the domain model.
Contingency: Wrap all payload parsing in try/catch and log malformed payloads to a monitoring channel; render a generic notification item rather than crashing when the payload cannot be parsed.