Skip to content

Commit

Permalink
chore: add mock attribute to bugsee manager initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
koukibadr committed Nov 22, 2024
1 parent ac885a0 commit f6a6938
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
9 changes: 9 additions & 0 deletions src/app/integration_test/bugsee_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:flutter_test/flutter_test.dart';

/// Test all Bugsee setup features
Future<void> bugseeSetupTest() async {
testWidgets(
'Test Bugsee configuration',
(tester) async {},
);
}
7 changes: 7 additions & 0 deletions src/app/integration_test/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:integration_test/integration_test.dart';

import 'bugsee_test.dart';
import 'dad_jokes_page_test.dart';
import 'forced_update_test.dart';
import 'kill_switch_test.dart';
Expand All @@ -12,6 +13,11 @@ import 'kill_switch_test.dart';
Future<void> main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
await initializeComponents(isMocked: true);
await registerBugseeManager(
isMock: true,
//A mock hexadecimal-based Bugsee token
bugseeToken: '01234567-0123-0123-0123-0123456789AB',
);

tearDownAll(
() async => await GetIt.I.get<MockingRepository>().setMocking(false),
Expand All @@ -20,4 +26,5 @@ Future<void> main() async {
await dadJokeTest();
await killSwitchTest();
await forcedUpdateTest();
await bugseeSetupTest();
}
31 changes: 20 additions & 11 deletions src/app/lib/business/bugsee/bugsee_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract interface class BugseeManager {
/// [BUGSEE_TOKEN] in the env using `--dart-define` or `launch.json` on vscode
Future<void> initialize({
String? bugseeToken,
bool isMock,
required Logger logger,
required LoggerManager loggerManager,
required BugseeRepository bugseeRepository,
Expand Down Expand Up @@ -118,6 +119,7 @@ final class _BugseeManager implements BugseeManager {
@override
Future<void> initialize({
String? bugseeToken,
bool isMock = false,
required Logger logger,
required LoggerManager loggerManager,
required BugseeRepository bugseeRepository,
Expand Down Expand Up @@ -145,6 +147,11 @@ final class _BugseeManager implements BugseeManager {
launchOptions = _initializeLaunchOptions();
_isBugSeeInitialized = false;

if (isMock) {
_initializeBugsee(bugseeToken ?? '');
return;
}

if (kDebugMode) {
_currentState = _currentState.copyWith(
isConfigurationValid: false,
Expand All @@ -163,15 +170,12 @@ final class _BugseeManager implements BugseeManager {
);
return;
}
_initializeBugsee(bugseeToken);
}

_currentState = _currentState.copyWith(
isLogFilterEnabled: configurationData.isLogsFilterEnabled,
isLogCollectionEnabled: configurationData.isLogCollectionEnabled,
attachLogFile: configurationData.attachLogFileEnabled,
);

void _initializeBugsee(String bugseeToken) async {
if (configurationData.isBugseeEnabled ?? true) {
await _launchBugseeLogger(bugseeToken);
_isBugSeeInitialized = await _launchBugseeLogger(bugseeToken);
}

_currentState = _currentState.copyWith(
Expand All @@ -180,10 +184,14 @@ final class _BugseeManager implements BugseeManager {
isVideoCaptureEnabled: _isBugSeeInitialized &&
(configurationData.isVideoCaptureEnabled ?? true),
isDataObscured: configurationData.isDataObscured,
isLogFilterEnabled: configurationData.isLogsFilterEnabled,
isLogCollectionEnabled: configurationData.isLogCollectionEnabled,
attachLogFile: configurationData.attachLogFileEnabled,
);
}

Future _launchBugseeLogger(String bugseeToken) async {
Future<bool> _launchBugseeLogger(String bugseeToken) async {
bool isInitialized = false;
HttpOverrides.global = Bugsee.defaultHttpOverrides;
await Bugsee.launch(
bugseeToken,
Expand All @@ -193,16 +201,17 @@ final class _BugseeManager implements BugseeManager {
"BUGSEE: not initialized, verify bugsee token configuration",
);
}
_isBugSeeInitialized = isBugseeLaunched;
isInitialized = isBugseeLaunched;
},
launchOptions: launchOptions,
);
if (_currentState.isLogFilterEnabled) {
if (configurationData.isLogsFilterEnabled ?? false) {
Bugsee.setLogFilter(_filterBugseeLogs);
}
if (_currentState.attachLogFile) {
if (configurationData.attachLogFileEnabled ?? false) {
Bugsee.setAttachmentsCallback(_attachLogFile);
}
return isInitialized;
}

BugseeLaunchOptions? _initializeLaunchOptions() {
Expand Down
8 changes: 5 additions & 3 deletions src/app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Future<void> main() async {
FlutterError.onError =
GetIt.I.get<BugseeManager>().inteceptRenderExceptions;
await initializeComponents();
await registerBugseeManager();
runApp(const App());
},
GetIt.I.get<BugseeManager>().inteceptExceptions,
Expand All @@ -53,7 +54,6 @@ Future initializeComponents({bool? isMocked}) async {
WidgetsFlutterBinding.ensureInitialized();
await _registerAndLoadEnvironment();
await _registerAndLoadLoggers();
await _registerBugseeManager();

_logger.d("Initialized environment and logger.");

Expand Down Expand Up @@ -135,15 +135,17 @@ void _initializeBugseeManager() {
);
}

Future _registerBugseeManager() async {
Future registerBugseeManager({bool? isMock, String? bugseeToken}) async {
if (!GetIt.I.isRegistered<BugseeManager>()) {
_initializeBugseeManager();
}
GetIt.I.get<BugseeManager>().initialize(
bugseeToken: const String.fromEnvironment('BUGSEE_TOKEN'),
bugseeToken:
bugseeToken ?? const String.fromEnvironment('BUGSEE_TOKEN'),
logger: GetIt.I.get<Logger>(),
loggerManager: GetIt.I.get<LoggerManager>(),
bugseeRepository: GetIt.I.get<BugseeRepository>(),
isMock: isMock ?? false,
);
}

Expand Down

0 comments on commit f6a6938

Please sign in to comment.