End-to-End Export Pipeline Integration Test
epic-accounting-system-export-engine-task-012 — Write an end-to-end integration test for the full export pipeline: seed approved claims in the test database, trigger an export run for Xledger (org: Blindeforbundet) and Dynamics (org: HLF), assert the Double-Export Guard filters previously exported claims, assert correct payload structure for both exporters, assert export run records are created in the repository, and verify that re-triggering the export for the same period produces an empty batch due to the guard. Validates the complete epic's business logic.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 5 - 253 tasks
Can start after Tier 4 completes
Implementation Notes
Structure the test file in three phases: (1) Seed phase — helper functions that insert test organisations, test users, and test approved claims using the Supabase admin client; (2) Execute phase — invoke the RunExportPipelineUseCase for each exporter type using a real (not mocked) pipeline with real guard and real repository connected to the local Supabase; (3) Assert phase — query the database directly to verify claim state, export run records, and payload content. The concurrency/idempotency test (re-triggering the same period) is the highest-value test in this suite — it validates the core business invariant of the entire epic. Use a TestSupabaseClient helper that wraps the Supabase client with automatic organisation-scoped JWTs for test users, so each database call in the assertion phase is authenticated as the correct test user. Avoid sharing state between test cases via class-level variables — use local variables in each test() block to keep tests independent and debuggable.
Testing Requirements
Integration test suite using flutter_test with a real local Supabase instance (supabase CLI). Tests organised in a single E2E test file: export_pipeline_e2e_test.dart. Test lifecycle: setUpAll seeds test organisations, users, and approved claims; each test case is independent and uses its own claim IDs; tearDownAll removes all seeded data. Use Supabase's admin client (service role) only in setup/teardown helpers, never in test assertions.
Test assertions query the database directly via the test client to verify state — do not rely solely on return values from the pipeline. Include a dedicated RLS test case that attempts a cross-org query with a scoped JWT and asserts zero results returned. Run this suite in CI with supabase start as a prerequisite step.
The Xledger CSV/JSON import specification may not be available in full detail at implementation time. If the field format, column ordering, encoding requirements, or required fields differ from assumptions, the generated file will be rejected by Xledger on first production use.
Mitigation & Contingency
Mitigation: Obtain the official Xledger import specification document from Blindeforbundet before starting XledgerExporter implementation. Build a dedicated acceptance test that validates a sample export file against all documented constraints.
Contingency: If the spec arrives late, implement a configurable column-mapping layer so that field order and names can be adjusted via configuration without code changes. Ship a file-based export that coordinators can manually verify before connecting to Xledger import.
The atomic claim-marking transaction in Double-Export Guard could fail under high concurrency if two coordinators trigger an export for overlapping date ranges simultaneously, potentially allowing duplicate exports to proceed past the guard.
Mitigation & Contingency
Mitigation: Use a database-level advisory lock or a SELECT FOR UPDATE on the relevant claim rows within the export transaction to serialize concurrent exports per organization. Add an integration test that simulates concurrent export triggers.
Contingency: If locking proves problematic at the database level, implement an application-level distributed lock using a Supabase row in a dedicated export_locks table with an expiry timestamp and automatic cleanup on failure.
HLF's Dynamics portal API endpoint may not be available or documented in time for Phase 1, leaving DynamicsExporter unable to be validated against a real system and potentially shipping with an incorrect field schema.
Mitigation & Contingency
Mitigation: Design DynamicsExporter for file-based export first (CSV/JSON download), with the API push implemented behind a feature flag. Request a Dynamics test environment or sandbox from HLF as early as possible.
Contingency: Ship DynamicsExporter as a file export only for Phase 1. Phase the API push integration into a follow-on task once the Dynamics sandbox is available, using the same AccountingExporter interface with no breaking changes.