ensureLiveCloudFunctionsForTest function

void ensureLiveCloudFunctionsForTest()

Bridges Firebase Cloud Functions pigeon calls to real HTTPS callable endpoints during flutter test. Native macOS/iOS/Android plugins are unavailable in the VM test binding, so without this handler httpsCallable fails immediately.

Implementation

void ensureLiveCloudFunctionsForTest() {
  if (_cloudFunctionsBridgeInstalled) return;
  TestWidgetsFlutterBinding.ensureInitialized();

  final messenger =
      TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;

  messenger.setMockDecodedMessageHandler<Object?>(
    BasicMessageChannel<Object?>(
      'dev.flutter.pigeon.cloud_functions_platform_interface.CloudFunctionsHostApi.call',
      CloudFunctionsHostApi.pigeonChannelCodec,
    ),
    (Object? message) async {
      final args = (message! as List<Object?>)[0]! as Map<Object?, Object?>;
      final arguments = args.cast<String, Object?>();
      try {
        final result = await _invokeCallableOverHttp(arguments);
        return <Object?>[result];
      } on PlatformException catch (error) {
        return <Object?>[error.code, error.message, error.details];
      } catch (error) {
        return <Object?>['error', error.toString(), null];
      }
    },
  );

  messenger.setMockDecodedMessageHandler<Object?>(
    BasicMessageChannel<Object?>(
      'dev.flutter.pigeon.cloud_functions_platform_interface.CloudFunctionsHostApi.registerEventChannel',
      CloudFunctionsHostApi.pigeonChannelCodec,
    ),
    (_) async => <Object?>[],
  );

  _cloudFunctionsBridgeInstalled = true;
}