Write unit and integration tests for adapter
epic-multi-chapter-membership-handling-foundation-task-006 — Write flutter_test unit tests for the ContactChapter domain mapper covering all field mappings and edge cases. Write integration tests for the SupabaseContactChapterAdapter methods against a local Supabase instance or mocked PostgREST client, verifying that CRUD operations return correctly mapped domain objects, RLS filter clauses are appended as expected, and error paths throw typed exceptions.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Prefer mocktail over mockito if the project already uses it (no code-gen required). For integration tests, store the local Supabase URL and anon key as constants in a `test/helpers/supabase_test_client.dart` file — never hardcode in individual test files. Use a `test/fixtures/contact_chapters.sql` seed file for consistent test data; run it in `setUpAll` and clean up in `tearDownAll`. For the mapper, test JSON round-trip: `mapper.fromJson(mapper.toJson(entity)) == entity`.
Tag integration tests with `@Tags(['integration'])` so they can be excluded from fast unit-test runs in CI. Structure test file as: mapper tests first (pure, no I/O), then adapter unit tests (mocked I/O), then integration tests (real I/O) — this ordering helps diagnose failures at the lowest layer first.
Testing Requirements
Unit tests (flutter_test with mocktail/mockito): test ContactChapterMapper.fromJson with 6+ scenarios (happy path, nulls, extra keys, missing required keys). Test each public adapter method with a mocked SupabaseClient; use ArgumentMatchers to assert filter clauses. Integration tests: use Supabase CLI (`supabase start`) to spin up a local instance seeded via SQL fixture files. Run adapter CRUD operations and assert domain object correctness.
Include a cross-org isolation test and a 5-chapter constraint test. Group tests with `group()` blocks matching class names. Use `setUp`/`tearDown` for database cleanup in integration tests.
Supabase RLS policies for a junction table that spans organisations are non-trivial. An incorrectly scoped policy could expose chapter affiliations from other organisations to coordinators, constituting a data breach.
Mitigation & Contingency
Mitigation: Draft RLS policies in a staging environment and run an explicit cross-organisation isolation test suite before merging. Use Supabase policy testing tools and peer review all policy definitions.
Contingency: If a policy error reaches review, roll back the migration and apply a corrective patch. Ensure no production data has been exposed by auditing Supabase logs for cross-organisation query results.
The contact_chapters table migration may conflict with existing foreign key structures or require a backfill for contacts already assigned to a single chapter, causing migration failures in production.
Mitigation & Contingency
Mitigation: Write the migration as an additive, non-destructive operation. Backfill existing single-chapter assignments by deriving them from the existing contact records. Test the full migration on a production-sized dataset clone before release.
Contingency: Provide a rollback migration script that removes the new table without touching existing contact records. Coordinate with operations for a maintenance window if a re-run is needed.