ClaimReceiptRepository unit tests
epic-receipt-capture-and-attachment-foundation-task-003 — Write flutter_test unit tests for all ClaimReceiptRepository methods using an in-memory SQLite database. Cover insert, query by claim_id, delete, and update sync status. Assert correct row counts, returned model fields, and error handling for missing claim references.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Use sqflite_common_ffi's inMemoryDatabaseFactory.openDatabase(':memory:') in setUp to get a real SQLite in-process. Call the same migration function used in production (e.g., DatabaseMigrations.applyV1(db)) so the schema is always in sync with real migrations. Build a small test factory function (makeTestAttachment({String claimId = 'test-claim-1', ...})) at the top of the test file to reduce boilerplate. For the ordering assertion in getByClaimId, insert records with explicit created_at values 1 second apart to make the DESC ordering deterministic.
Avoid using DateTime.now() directly in test data — use fixed ISO-8601 strings instead.
Testing Requirements
Unit tests only — no Flutter widget rendering, no Supabase calls. Use sqflite_common_ffi with inMemoryDatabaseFactory to obtain a real SQLite engine in-memory without file I/O. Run the project's database migration in setUp so the schema is identical to production. Use descriptive test names following the pattern 'methodName — scenario — expected outcome'.
Group tests by method using group() blocks. Avoid test interdependency: each test must be runnable in isolation.
Supabase Storage RLS policies using org/user/claim path scoping may not enforce correctly if claim ownership is not present in the JWT or if path segments are constructed differently at upload vs. read time, leading to data leakage or access denial for legitimate users.
Mitigation & Contingency
Mitigation: Define and test RLS policies in isolation before wiring to app code. Write integration tests that assert cross-org and cross-user access is denied. Use service-role key only in edge functions, never in client code.
Contingency: If client-side RLS proves insufficient, route all storage reads through a Supabase Edge Function that validates ownership before generating signed URLs, adding a controlled server-side enforcement layer.
Aggressive image compression may reduce receipt legibility below the threshold required for financial auditing, causing claim rejections or compliance failures despite technically successful uploads.
Mitigation & Contingency
Mitigation: Define minimum legibility requirements with HLF finance team before implementation. Set compression targets conservatively (e.g., max 1MB, min 80% JPEG quality) and validate with sample receipt images. Provide compression statistics in verbose/debug mode.
Contingency: If post-compression quality is disputed by auditors, increase the quality floor at the cost of larger file sizes, and add a manual override allowing users to skip compression for PDFs and high-quality scans.
The Flutter image_picker package behaves differently on iOS 17+ (PHPicker) vs older Android (Intent-based), particularly for file types, permission flows, and PDF selection, which may cause platform-specific failures not caught in development.
Mitigation & Contingency
Mitigation: Test image picker integration on physical devices for both platforms early in the sprint. Pin the image_picker package version and review changelogs before updates. Write widget tests using mock file results for each platform branch.
Contingency: If PHPicker or Android Intent differences cause blocking issues, implement separate platform-specific picker delegates behind the unified interface, allowing platform-specific fixes without breaking the shared API.