Unit test AuthService error mapping and session logic
epic-email-password-login-auth-logic-task-010 — Write unit tests for the GoTrue error mapper covering all mapped error categories (invalid credentials, network, rate limit, server error, and unknown fallback). Test AuthService.signIn() with mocked Supabase client responses. Test session persistence and restoration paths. Target 100% branch coverage on the error mapper.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 5 - 253 tasks
Can start after Tier 4 completes
Implementation Notes
Mock the Supabase GoTrue client by creating a fake implementation or using mocktail's mock. The error mapper is a pure function (GoTrueException input → domain exception output) — test it in isolation first before testing AuthService as a whole. For session storage tests, inject a mock SecureStorage interface into AuthService rather than using the real flutter_secure_storage package in tests. Group test files by component: `test/auth/gotrue_error_mapper_test.dart` and `test/auth/auth_service_test.dart` — keep them separate for clarity.
Use `throwsA(isA
Testing Requirements
Use flutter_test with mocktail for mocking. Structure test file as: (1) 'GoTrue error mapper' group with one test per error branch; (2) 'AuthService.signIn' group with success and each exception path; (3) 'AuthService session persistence' group covering persistSession and restoreSession scenarios. Use setUp() to create fresh mocks per test. Run coverage with `flutter test --coverage` and verify 100% on the mapper.
All tests must be hermetic — no shared mutable state between tests.
Supabase GoTrue returns HTTP error codes and string messages that may change between SDK versions. Incorrect or incomplete mapping could cause the wrong user-facing message to be shown (e.g., showing a generic error instead of a specific credential error), violating the plain-language feedback acceptance criteria and potentially exposing security-sensitive information.
Mitigation & Contingency
Mitigation: Pin the supabase_flutter SDK to a specific minor version in pubspec.yaml. Write integration tests that mock the Supabase HTTP layer and assert each error code maps to the correct domain exception. Document the mapping table as a constant in AuthService.
Contingency: If an unrecognized error code is received at runtime, catch it as an UnknownAuthException and display a generic safe message. Alert via crash reporting for triage and SDK update.
If the user taps the sign-in button multiple times rapidly, concurrent authentication requests could result in race conditions: duplicate network calls, out-of-order state emissions, or multiple session tokens being written to secure storage.
Mitigation & Contingency
Mitigation: Use bloc concurrency transformer (droppable or restartable) to ensure only one authentication event is processed at a time. The BLoC should guard against submission while in LoginLoading state.
Contingency: Add a UI-level disable on the submit button when loading state is active as a secondary guard independent of BLoC concurrency control.