Unit test WayForwardItemRepository and ReportSchemaCache
epic-structured-post-session-report-foundation-task-009 — Write comprehensive unit tests for WayForwardItemRepository (mocking Supabase client) covering create, read, update, delete, error handling, and RLS rejection scenarios. Write unit tests for ReportSchemaCache covering TTL expiry, invalidation, concurrent access, and cache miss behaviour. Target 90%+ branch coverage for both components.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
Inject the Supabase client (and for the cache, a clock abstraction) via constructor parameters — do not use global singletons or `Supabase.instance` directly in repository classes, as this makes mocking impossible. If the repository currently uses `Supabase.instance`, refactor it to accept `SupabaseClient` as a constructor arg before writing tests. For the cache concurrency test, the simplest approach is storing an in-flight `Future` per key so a second caller awaits the same Future — check if this pattern is already implemented; if not, add it as part of this task. Use `throwsA(isA
Keep test fixtures in a shared `test/fixtures/` folder so they can be reused by tasks 010 and 011.
Testing Requirements
These ARE the tests. Use `@GenerateMocks([SupabaseClient, ...])` with mockito's build_runner code generation. Group tests with `group()` blocks per method. Use `setUp()` to construct the repository with injected mock.
For cache concurrency test, use `Future.wait([cache.get(id), cache.get(id)])` inside `fakeAsync` and verify the underlying loader was called exactly once via `verify(...).called(1)`. Run coverage with `flutter test --coverage` and add a CI step to fail if line coverage drops below 90%.
Supabase RLS policies for multi-org report access may be more complex than anticipated — coordinators need cross-peer-mentor access within their org but not across orgs, and draft reports should be invisible to coordinators until submitted. Misconfigured RLS could expose sensitive health data or block legitimate access.
Mitigation & Contingency
Mitigation: Define and test RLS policies in isolation before writing repository code. Create a dedicated SQL migration file with policy definitions and an automated integration test suite that verifies each role's access boundaries using real Supabase auth tokens.
Contingency: If RLS proves too complex to express declaratively, implement application-level access control in the repository layer with explicit org and role checks, and add a security audit task before the feature goes to production.
The org field config JSON stored in Supabase may lack a stable, versioned schema contract. If different organisations have drifted to different field-definition formats, org-field-config-loader will fail silently or crash, breaking form rendering for those orgs.
Mitigation & Contingency
Mitigation: Define a canonical JSON Schema for field config and validate all existing org configs against it before implementation begins. Store a schema version field in every config record and handle version migrations explicitly in the loader.
Contingency: If existing configs are too heterogeneous, implement a config normalisation pass in org-field-config-loader that coerces known variants to the canonical format, logging warnings for fields that cannot be normalised so operations can fix them in the admin console.
TTL-based schema cache invalidation may cause peer mentors to use stale field definitions for up to the TTL window after an admin updates the org config, potentially collecting data against outdated field structures.
Mitigation & Contingency
Mitigation: Set a conservative TTL (e.g. 15 minutes) and expose a manual cache-bust mechanism triggered on app foreground-resume. Document the maximum staleness window in the admin console so org admins know to plan config changes outside active reporting windows.
Contingency: If stale schema causes a data quality incident, add a Supabase Realtime subscription to the org config table that invalidates the cache immediately on any config update.