Implement validation orchestration and issue prioritization
epic-bufdir-report-preview-core-logic-task-005 — Wire all three rule evaluators (completeness, threshold, anomaly) into the main BufdirFieldValidationService.validate() method. The method accepts a BufdirReportModel and an optional prior-period BufdirReportModel. Run all rule evaluators, aggregate their issues, and return a ValidationResult sorted by severity (errors first, then warnings, then info). Ensure the service is stateless and testable without Flutter dependencies.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Inject evaluators via constructor to enable testing with fakes: BufdirFieldValidationService({ required CompletenessRuleEvaluator completenessEvaluator, required ThresholdComplianceRuleEvaluator thresholdEvaluator, required AnomalyDetectionRuleEvaluator anomalyEvaluator }). The validate() method should iterate report.sections, call each evaluator per section (passing prior section looked up by sectionId from the prior-period model), collect results into a flat list, then sort by severity ordinal. Define severity ordinals in the ValidationIssueSeverity enum: error = 0, warning = 1, info = 2. Use List.sort() with a comparator on severity ordinal.
Wrap each evaluator call in a try/catch and emit a synthetic ValidationIssue(severity: error, ruleClass: 'system', message: 'Validation error in section $sectionId: $e') on failure — this ensures one bad section never silently swallows other sections.
Testing Requirements
Unit tests using flutter_test. Test class: BufdirFieldValidationServiceTest. Required scenarios: (1) all fields valid, no prior → ValidationResult with empty issues and isValid true, (2) one completeness error → isValid false, error appears first in sorted result, (3) one threshold warning only → isValid true, (4) one anomaly warning only → isValid true, (5) mixed errors and warnings → errors sorted before warnings, (6) null prior-period model → anomaly evaluator runs without crash, (7) multiple sections with multiple issues → all issues aggregated, (8) empty BufdirReportModel (no sections) → empty ValidationResult, isValid true. Use test doubles (fake implementations) for each evaluator rather than real evaluators — this isolates the orchestration logic.
Minimum 90% line coverage.
The exact minimum threshold values required by Bufdir guidelines (e.g., minimum participant counts per section) may not be formally documented in machine-readable form. If thresholds must be researched or negotiated during implementation, the validation service will be delayed and may launch with incomplete rules, reducing its effectiveness.
Mitigation & Contingency
Mitigation: Compile threshold rules from the Bufdir reporting guidelines PDF before sprint start. Store rules in a separate configuration file (not hardcoded in the service class) so they can be updated without a service rewrite. Treat unknown thresholds as 'no minimum' to avoid false errors.
Contingency: Launch with completeness and anomaly validation only, shipping threshold compliance rules as a follow-on config update once rules are confirmed with Bufdir. Flag this as a known limitation in the coordinator help text.
BufdirPreviewService coordinates three async operations (fetch aggregated data, map structure, run validation). Race conditions or partial failures in this chain could produce an inconsistent preview model — e.g., a model with field values but no validation results — which would silently mislead coordinators into thinking the report is clean.
Mitigation & Contingency
Mitigation: Model the orchestration as a single BLoC/Cubit state machine with explicit states (Loading, Loaded, Error) and ensure validation is always run atomically after mapping, never in parallel. Write integration tests that simulate network failure at each step of the chain.
Contingency: If a partial failure state reaches production, detect it via the missing validation summary field in the preview model and show a full-screen error state rather than an incomplete preview, prompting the coordinator to retry.