beginWith method

  1. @override
TaskChainBuilder beginWith(
  1. TaskRequest task
)
override

Records the chain start and intercepts TaskChainBuilder.enqueue so the full chain structure (all steps from all .then() / .thenAll() calls) is captured in chains.

Note: This overwrites TaskChainBuilder.enqueueCallback. The original value is restored when dispose is called.

Implementation

@override
TaskChainBuilder beginWith(TaskRequest task) {
  final builder = TaskChainBuilder.internal([task]);

  // Intercept enqueue() on the builder to capture the complete chain.
  // C-02 fix: we saved the original callback in the constructor; it is
  // restored in dispose() so tests can't contaminate each other.
  TaskChainBuilder.enqueueCallback = (b) async {
    final record = FakeChainRecord(firstTask: task, steps: b.steps);
    chains.add(record);

    // Also record each task in enqueued for easy assertion.
    for (final step in b.steps) {
      for (final t in step) {
        enqueued.add(EnqueueCall(
          taskId: t.id,
          trigger: const TaskTrigger.oneTime(),
          worker: t.worker,
          constraints: t.constraints,
          existingPolicy: ExistingTaskPolicy.replace,
          tag: null,
        ));
      }
    }

    return enqueueResultByTaskId[task.id] ?? enqueueResult;
  };

  return builder;
}