Integration test against Supabase staging environment
epic-bufdir-data-aggregation-data-layer-task-009 — Write integration tests that execute the Aggregation Query Builder and Bufdir Metrics Repository methods against a real Supabase staging instance. Tests must verify: RPC functions return correctly typed responses for known seed data, org-isolation filters actually prevent cross-org data leakage, snapshot persistence survives a fresh repository instantiation, and multi-org query contexts work independently. Document any Supabase RPC function signature requirements discovered during testing.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 7 - 84 tasks
Can start after Tier 6 completes
Implementation Notes
Create a StagingTestHelper class with static methods insertSeedOrg(), insertSeedActivities(orgId, count), insertSeedContacts(), and cleanupSeedData(orgIds) to keep test setup readable. For multi-org JWT testing, use Supabase's signInWithPassword for two different test user accounts (one per org) and run queries with each client in parallel using Future.wait(). If the staging RPC functions are not yet deployed when this task starts, mock the RPC layer in a subset of tests and document which assertions are blocked on RPC deployment. Use expect(..., completion(...)) for async assertions.
Never use sleep() — use await and proper async patterns.
Testing Requirements
Integration tests live in integration_test/data/bufdir_metrics_integration_test.dart. Use flutter_test's TestWidgetsFlutterBinding.ensureInitialized() for async setup. Tag tests with group('Integration - BufdirMetricsRepository', ...) and individual tests with tags: ['integration', 'supabase', 'rls']. CI pipeline must have a separate job that runs these tests only on staging environment with the SUPABASE_STAGING_URL env var set — unit test job must not require this variable.
Document pass/fail for each RPC function in INTEGRATION_TEST_NOTES.md alongside the function signature (name, parameters, return shape).
Supabase RPC functions return JSON with PostgreSQL numeric types (bigint, numeric) that do not map cleanly to Dart int/double. Silent truncation or JSON parsing errors could corrupt participant counts in the final Bufdir submission without any runtime exception.
Mitigation & Contingency
Mitigation: Define explicit Dart fromJson factories for all RPC result models with type-safe parsing and assertion checks. Add a contract test that compares raw RPC JSON output against expected Dart model values using a known seed dataset.
Contingency: If type mismatches are found in production metrics, expose a validation endpoint in BufdirMetricsRepository that re-fetches and compares raw RPC output against the persisted snapshot, flagging any discrepancies before export proceeds.
Persisted metric snapshots can become stale if additional activities are registered after the snapshot is saved but before the export is finalized. Coordinators might unknowingly export data that does not reflect the latest activity registrations.
Mitigation & Contingency
Mitigation: Store a snapshot_generated_at timestamp and a record_count_at_generation field in the snapshot. When the coordinator views cached results, compare the current activity count for the period against the snapshot value and display a 'Data updated since last aggregation — re-run?' warning if counts differ.
Contingency: Add a mandatory staleness check before the export confirmation dialog can proceed: if the snapshot is more than 24 hours old or the record count has changed, require the coordinator to re-run aggregation before the export button is enabled.