planShardRunIdsForTest function

Future<List<List<String>>> planShardRunIdsForTest({
  1. required String appDir,
  2. List<String> arguments = const [],
  3. int? jobs,
})

Returns the expanded run ids assigned to each parallel worker.

This is intentionally a small test hook for the CLI sharding planner. The production runner uses the same private _testFilesForSharding and _balancedShards path before launching Flutter worker processes.

Implementation

Future<List<List<String>>> planShardRunIdsForTest({
  required String appDir,
  List<String> arguments = const [],
  int? jobs,
}) async {
  final testFiles = await _testFilesForSharding(
    appDir,
    YamlTestAppPatcher(appDir),
    arguments: arguments,
  );
  final allRuns = [
    ...testFiles.parallel.map((file) => file.id),
    ...testFiles.serial,
  ];
  if (allRuns.isEmpty) return const [];
  if (allRuns.length < 2) return [allRuns];

  final requestedJobs = jobs ?? _autoWorkerCount(allRuns.length);
  final totalJobCount = requestedJobs.clamp(1, allRuns.length);
  final serialLaneCount = testFiles.serial.isNotEmpty ? 1 : 0;
  final parallelLaneBudget = totalJobCount - serialLaneCount;
  final parallelWorkerCount = testFiles.parallel.isNotEmpty
      ? parallelLaneBudget.clamp(1, testFiles.parallel.length)
      : 0;

  final result = <List<String>>[
    for (final shard in _balancedShards(
      testFiles.parallel,
      parallelWorkerCount,
    ))
      [for (final file in shard) file.id],
  ];
  if (testFiles.serial.isNotEmpty) {
    result.add(testFiles.serial);
  }
  return result.where((shard) => shard.isNotEmpty).toList();
}