Write integration tests for cron trigger lifecycle
epic-assignment-follow-up-reminders-cron-infrastructure-task-006 — Write integration tests covering the full cron trigger lifecycle: successful run with logging, advisory lock preventing a concurrent second run, graceful error handling and log capture on ReminderSchedulerService failure, and correct reset of last_contact_date when an activity is logged. Use Supabase test utilities and Flutter integration test harness. Validate that consecutive failure counter increments and resets correctly.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 21 - 1 tasks
Can start after Tier 20 completes
Implementation Notes
Organize tests in a file named `cron_trigger_integration_test.dart` under the `integration_test/` directory. Use a `CronTestHelpers` utility class to encapsulate common setup patterns (seeding an assignment, inserting an activity, reading log rows). For the advisory lock concurrency test, insert a deliberate 2-second delay into the Edge Function via a test-mode flag to make the race condition reliably observable — remove this flag from production builds. Use Supabase's `supabase_flutter` client with the test project's URL and service role key, loaded from environment variables in test setup.
Consider using Supabase's branching feature (if available) to create a clean database branch per test run for maximum isolation.
Testing Requirements
This task IS the testing deliverable. Each of the 8 acceptance criteria test cases must be implemented as a named test in flutter_test or a Deno test file (depending on where the Edge Function logic is tested). Use `setUp` and `tearDown` hooks to isolate test state. For the concurrency test, use Dart's `Future.wait` to invoke the cron function twice simultaneously.
For the activity reset test, use the Supabase test client to insert an activity row directly and then assert the assignments table state. Mock the alerting webhook using a local HTTP server spun up in the test (e.g., using Dart's `HttpServer`). Assert both the HTTP call was received AND the payload matches the expected structure.
If the daily cron job takes longer than 24 hours to complete (due to a large dataset or a slow query), a second instance will start while the first is still running, causing duplicate reminder dispatch for assignments processed twice.
Mitigation & Contingency
Mitigation: Implement an advisory lock that prevents a second run from starting if the first is still active. Monitor run duration via the execution log table and alert if any run exceeds 30 minutes. The 10,000-assignment load test should verify the run completes in under 5 minutes.
Contingency: If a double-run occurs, the idempotency guard in ReminderDispatchService prevents duplicate notifications from being sent. The execution log identifies the overlap and allows the ops team to investigate the root cause.
If the activity registration hook that resets last_contact_date is implemented incorrectly or not triggered for all activity types (e.g., proxy registrations, bulk registrations), peer mentors will continue receiving reminders even after logging contact, damaging user trust.
Mitigation & Contingency
Mitigation: Audit all code paths that create activity records (direct registration, proxy registration, bulk registration, coordinator proxy) and ensure each path calls the assignment contact update. Write integration tests for each registration path asserting that last_contact_date is updated.
Contingency: Provide an authenticated admin endpoint that allows manual correction of last_contact_date for a specific assignment, enabling ops to resolve individual cases while the bug is fixed and deployed.