attachHostDebugArtifacts function

Future<void> attachHostDebugArtifacts({
  1. required WidgetTester tester,
  2. required EnsembleTestContext context,
  3. required TestStatus status,
  4. required int durationMs,
  5. int? failedStepIndex,
  6. String? failedStepLabel,
  7. String? failureMessage,
})

Writes screenshot frames, API, storage, and app console logs.

Call while the widget tree is still mounted so dump-tree and emergency captures can read it. Screenshot encoding runs inside tester.runAsync so fake-async widget tests do not deadlock on GPU/image work.

Implementation

Future<void> attachHostDebugArtifacts({
  required WidgetTester tester,
  required EnsembleTestContext context,
  required TestStatus status,
  required int durationMs,
  int? failedStepIndex,
  String? failedStepLabel,
  String? failureMessage,
}) async {
  try {
    final artifacts = context.runtime.takeScreenshotArtifacts();
    final frames = artifacts.frames;
    final stepObservers = artifacts.stepObservers;
    if (context.config.screenshots.enabled &&
        (frames.isNotEmpty ||
            stepObservers.isNotEmpty ||
            context.config.devices.isNotEmpty)) {
      final aggregator = ScreenshotSheetAggregator(
        screenshots: context.config.screenshots,
        devices: context.config.devices,
      );
      final path = usesDeviceArtifactTransport
          // Integration captures are already rasterized simulator frames.
          // Their PNG encoding and artifact transport do not need Flutter's
          // runAsync lane, which may still be occupied by a live plugin call.
          ? await aggregator.completeRun(
              testCase: context.testCase,
              frames: frames,
              status: status,
              durationMs: durationMs,
              failedStepIndex: failedStepIndex,
              failedStepLabel: failedStepLabel,
              failureMessage: failureMessage,
              stepObservers: stepObservers,
            )
          : await LiveAsyncCallSupport.runPriorityUntracked(
              () => aggregator.completeRun(
                testCase: context.testCase,
                frames: frames,
                status: status,
                durationMs: durationMs,
                failedStepIndex: failedStepIndex,
                failedStepLabel: failedStepLabel,
                failureMessage: failureMessage,
                stepObservers: stepObservers,
              ),
            );
      if (path != null) {
        context.logger.log('screenshotFrames: $path');
      }
    } else {
      for (final observer in stepObservers) {
        observer.dispose();
      }
      for (final frame in frames) {
        try {
          frame.image.dispose();
        } catch (_) {}
      }
    }
  } catch (error) {
    context.logger.log('screenshotsError: $error');
  }

  // Snapshot API calls at attempt cleanup. In-flight calls remain explicitly
  // pending in the report; artifact capture does not wait for their responses.
  await const TestArtifactCoordinator().attachLogArtifactsOnce(context);
  try {
    Map<String, dynamic> keys = const {};
    Map<String, dynamic> secureStorageKeys = const {};
    Map<String, dynamic> keychainKeys = const {};
    if (StorageManager().initialized) {
      try {
        keys = capturePublicStorage();
      } catch (_) {}
      try {
        secureStorageKeys = captureSecureStorage();
      } catch (_) {}
    }
    // The plugin keychain is independent of GetStorage initialization. Capture
    // it even when the app never initialized its public-storage backend.
    try {
      keychainKeys = await captureKeychainStorage();
    } catch (error) {
      context.logger.log('keychainStorageCaptureError: $error');
    }
    TestArtifactCoordinator.replaceArtifactLog(
      context.logger,
      'storage',
      await writeStorageLogFile(
        logger: context.logger,
        filePrefix: context.testCase.id,
        keys: keys,
        secureStorageKeys: secureStorageKeys,
        keychainKeys: keychainKeys,
        stepDiffs: context.runtime.storageStepDiffs,
        secureStepDiffs: context.runtime.secureStorageStepDiffs,
        keychainStepDiffs: context.runtime.keychainStepDiffs,
      ),
    );
  } catch (error) {
    context.logger.log('storageError: $error');
  }
}