Unit tests for activity attribution service
epic-proxy-activity-registration-core-services-task-009 — Write unit tests for activity-attribution-service covering: single record attribution success, missing registered_by raises AttributionValidationError, missing attributed_to raises AttributionValidationError, batch with mixed valid/invalid records reports per-record errors, audit query helpers return only proxy records when filtering by coordinator, and audit query helpers return only self-registered records when filtering by mentor. Ensures legal defensibility of all proxy entries.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
AttributionValidationError should carry a field name property so tests can assert which field caused the failure — this is critical for building meaningful error messages in the UI layer. For the audit query tests, the service method should accept a filter parameter (e.g., AuditFilter.proxyOnly, AuditFilter.selfOnly) rather than raw booleans to keep the interface extensible. Verify that batch tests use a list of exactly 3 records to keep fixtures minimal while covering all paths. Use fake UUIDs that are visually distinguishable (e.g., mentor-uuid-1111, coordinator-uuid-2222) to make test assertions readable.
Testing Requirements
Pure unit tests using flutter_test. Mock both ActivityRepository and DuplicateDetectionService. In tests that focus on attribution validation, configure DuplicateDetectionService mock to return no conflicts so it does not interfere. In integration-path tests (task-007 integration), configure it to return a conflict to verify error propagation.
Use expect(() => service.register(record), throwsA(isA
Overly strict duplicate matching (exact date + type) may flag legitimate back-to-back sessions of the same activity type on the same day as duplicates, frustrating coordinators and undermining trust in the feature.
Mitigation & Contingency
Mitigation: Confirm with product owners whether the matching key should be (mentor_id, date, activity_type_id) only or should also consider duration and time-of-day. Document the chosen threshold in the service and surface it in the duplicate warning dialog for transparency.
Contingency: If false-positive rates are high in user testing, add a duration-window tolerance parameter to the detection query that can be tuned without code changes.
If the current session token is invalidated between the coordinator starting the proxy form and submitting it, the activity-attribution-service may fail to resolve the coordinator's user ID, causing a silent attribution error.
Mitigation & Contingency
Mitigation: Read the coordinator's user ID from the session at service call time rather than at form-open time. Validate the session is still active before committing the insert, and surface a clear re-authentication prompt if it has expired.
Contingency: If a mis-attributed record is detected post-submission, the audit log retains the original session metadata, allowing a corrective record to be created with accurate attribution.