Write Unit Tests for Way Forward Task Service
epic-structured-post-session-report-services-task-008 — Write unit tests for way-forward-task-service covering successful task creation, duplicate prevention via idempotency keys, repository failure rollback, and task queue signal emission. Mock way-forward-item-repository. Validate coordinator assignment logic and due-date calculation with flutter_test.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Before writing tests, confirm WayForwardTaskService exposes its dependencies (repository, task queue, clock) via constructor injection — if not, refactor before writing tests. For the rollback test, if Dart lacks built-in transaction scope, simulate rollback via a collected list: service accumulates created tasks in a local list and only calls repository.insertBatch() after all items are validated — test this by verifying insertBatch is called once with all items or not at all. The idempotency test should assert the exact WayForwardTaskCreationResult.alreadyExisted variant is returned (use expect with isA
coordinatorId, List
Testing Requirements
Pure unit tests with flutter_test and mockito. Mock IWayForwardItemRepository and ITaskQueue using @GenerateMocks. For rollback test, use when().thenThrow() on the repository mock for the second invocation using mockito's answer sequencing (thenAnswer chained calls). Verify task queue signal count with verify(mockQueue.signal(any)).called(N).
Due date calculation tests should be extracted into a separate group() block that tests the pure calculation function directly if it is extracted. Use fake DateTime via a Clock abstraction — do not rely on DateTime.now() in tests. Run flutter test --coverage to confirm ≥ 90% line coverage.
Flutter's speech_to_text package behaviour differs meaningfully between iOS and Android — microphone permission flows, locale availability, background audio session interference, and partial-result timing all vary. Inconsistent behaviour could make voice input unreliable for the primary audience (visually impaired peer mentors on iOS VoiceOver).
Mitigation & Contingency
Mitigation: Test speech-to-text-adapter on physical iOS and Android devices from the start, not just simulators. Write platform-specific test cases for permission flows and locale detection. Design the adapter's public interface to be platform-agnostic so that a native bridge could replace the package if needed.
Contingency: If speech_to_text proves unreliable on a platform, implement a native-speech-api-bridge (already identified in the component catalogue) as a drop-in replacement within the adapter, keeping the external interface unchanged so no UI code needs to change.
The coordinator task queue notification mechanism is not fully specified. If the queue system is owned by another team or uses an external service, way-forward-task-service may block on an undefined integration contract, delaying this epic.
Mitigation & Contingency
Mitigation: Define the task queue notification interface as an abstract Dart interface early in the epic. Implement a stub that writes a flag to the database so coordinator list queries can detect new tasks, deferring the real notification integration to a later epic.
Contingency: If the queue integration remains undefined at implementation time, ship way-forward-task-service with database persistence only and add a TODO-flagged notification hook. Coordinators will still see items on next page load; push notification delivery is deferred.