Write integration tests for StatsRepository
epic-coordinator-statistics-dashboard-foundation-task-011 — Create integration tests for StatsRepository against a Supabase test instance with seeded activity data. Test period filtering (monthly, quarterly, annual), activity-type filtering, coordinator-scope queries, org-admin-scope queries, cache hit and miss paths, and access denial for out-of-scope queries. Verify response times are under 100ms for indexed lookups. Use flutter_test.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Use supabase_flutter's SupabaseClient initialised with test credentials injected at runtime via --dart-define or a .env.test file read by flutter_dotenv. Create a TestAuthHelper that mints coordinator and org-admin JWT tokens by calling the test Supabase Auth API with pre-seeded test user credentials. Wrap each test group in a setUpAll / tearDownAll pair: setUpAll runs the seed SQL via the Supabase management API or a direct postgres connection; tearDownAll deletes seeded rows. For performance assertions, wrap the repository call in a Stopwatch, run it 5 times after a warm-up call, and assert that the median is under 100 ms.
For cache behaviour, inject a request-counting HttpClient adapter into the Supabase client so you can assert exactly how many network calls were made. For access-denial tests, catch the thrown exception and verify its type — do not rely on an empty result set as a proxy for denial because that could mask data leaks.
Testing Requirements
Integration tests using flutter_test against a dedicated Supabase test project (separate from staging and production). Maintain a SQL seed script (test/fixtures/stats_seed.sql) that inserts a deterministic dataset including: 3 coordinators across 2 organisations, 5 peer mentors per coordinator, activities spanning three calendar years with all supported types, and at least two boundary-date activities per period type. Write test groups: period_filtering (monthly, quarterly, annual, boundary dates), activity_type_filtering, scope_isolation (coordinator, org_admin, cross-coordinator denial), cache_behaviour (hit, miss, invalidation), and performance (timing assertions with Stopwatch). Teardown must DELETE only rows inserted by the seed script using a known seed_run_id column.
Run with flutter test integration_test/ --dart-define=SUPABASE_TEST_URL=... --dart-define=SUPABASE_TEST_ANON_KEY=...
Pre-aggregated Supabase views may still be slow for orgs with very large activity datasets (NHF with 1,400 chapters). If the view query plan performs sequential scans, dashboard load times could exceed acceptable thresholds and degrade the perceived value of the feature.
Mitigation & Contingency
Mitigation: Design views with composite indexes on (org_id, coordinator_id, month) from the start. Run EXPLAIN ANALYZE during development against a seeded dataset of realistic scale. Add materialized view refresh strategy if needed.
Contingency: If live view performance is insufficient, convert to materialized views refreshed on a schedule or on activity-write triggers. Expose the refresh delay transparently in the UI with a 'last updated' timestamp.
Supabase RLS policies for the stats views may not be configured correctly during initial migration, potentially allowing cross-coordinator data leakage before the RoleAccessValidator layer is reached. This is a security and compliance risk.
Mitigation & Contingency
Mitigation: Write RLS integration tests as part of this epic that explicitly verify a coordinator JWT cannot read another coordinator's stats rows. Apply RLS policies in the migration script itself, not as a manual step.
Contingency: If an RLS gap is discovered post-deployment, immediately disable the stats screen via a feature flag, apply the corrected RLS migration, and re-enable after verification. Log and audit all queries that ran during the gap window.
Cache invalidation logic may not be triggered correctly when a new activity is registered by a peer mentor or when an expense approval is granted. Stale data could cause coordinators to make decisions based on outdated KPIs, undermining trust in the dashboard.
Mitigation & Contingency
Mitigation: Define explicit invalidation event contracts with the activity registration and expense approval pipelines. Implement an event bus subscription within StatsCacheManager. Document the invalidation contract in code.
Contingency: If event-driven invalidation proves unreliable, add a manual 'Refresh' pull-to-refresh gesture on the dashboard and reduce TTL to 5 minutes as a fallback degradation strategy.