diff --git a/.ai/pr-descriptions/OSS-381.md b/.ai/pr-descriptions/OSS-381.md new file mode 100644 index 00000000..6db25351 --- /dev/null +++ b/.ai/pr-descriptions/OSS-381.md @@ -0,0 +1,73 @@ + + +## 1. Risk: M + + + +**Why this size?** + +The Sentry filter is wired into all four flavor entrypoints (`main`, `main_alpha`, `main_beta`, `main_prod`) The `SentryEventFilter` is registered in `lib/di`, and it decides which events reach Sentry in production. + +Therefore specific filter events are created. So we can determine if a background or foreground event is triggered. If an event matches one of the rules we can drop it. So it doesn't reach Sentry. + + + + +**Where should the reviewer look, and what am I unsure about?** + +Most important files are in the `lib/utilsentry/`-folder. +This was tested in Blue-bike and was shown succesfull. + +## 2. What changed and why + +**Jira:** OSS-381: https://github.com/icapps/flutter-template/issues/381 + +Sentry is cluttered with a lot of events that we do not need. Therefore we created a filter. + +## 3. Evidence: Flow / Proof of Delivery + + + +| Acceptance criterion | Evidence (test / screenshot / recording / Maestro) | Covered? | +|---|---|---| + +See test scenarios + +**Edge cases tested (M and L):** + +No edge cases + +**Tested on (M and L):** Simulator and temporariy crash. + +**Does anything deviate from the ticket?** No + + diff --git a/.bitbucket/pull_request_template.md b/.bitbucket/pull_request_template.md new file mode 100644 index 00000000..0010167c --- /dev/null +++ b/.bitbucket/pull_request_template.md @@ -0,0 +1,54 @@ + + +## 1. Risk: TODO (S / M / L) + + + +**Why this size?** TODO (1 sentence, name the criterion) + +**Where should the reviewer look, and what am I unsure about?** TODO + +## 2. What changed and why + +**Jira:** TODO (link) + +TODO (2 to 4 sentences, in your own words. What changes for the user, what changes in the code, and why this way?) + +## 3. Evidence: Flow / Proof of Delivery + + + +| Acceptance criterion | Evidence (test / screenshot / recording / Maestro) | Covered? | +|---|---|---| +| TODO | TODO | Yes / No / Partial | + +**Edge cases tested (M and L):** TODO (e.g. no network, empty state, error message, long text, rotation) + +**Tested on (M and L):** TODO (device/emulator + OS version) + +**Does anything deviate from the ticket?** TODO (if not: "No") + + diff --git a/.gitignore b/.gitignore index 9c299a14..f42803b4 100644 --- a/.gitignore +++ b/.gitignore @@ -93,4 +93,4 @@ Gemfile.lock # Fvm .fvm/ -/test/coverage_helper_test.dart +/test/coverage_helper_test.dart \ No newline at end of file diff --git a/.opencode/skills/pr-description/SKILL.md b/.opencode/skills/pr-description/SKILL.md new file mode 100644 index 00000000..b7dd70a0 --- /dev/null +++ b/.opencode/skills/pr-description/SKILL.md @@ -0,0 +1,65 @@ +--- +name: pr-description +description: "PR description preparation: builds `.ai/pr-descriptions/.md` from the branch, the Jira ticket and the diff, using `.bitbucket/pull_request_template.md`, and reports gaps for the author. Use when a branch is ready for a pull request, or when the PR description must be refreshed after more commits." +metadata: + domain: "delivery" + version: "0.2" +--- + +# PR description + +Prepare the pull request description so the reviewer can spend time on what matters. The skill fills what the repository can prove and leaves what only the author knows as `TODO`. It never presents a guess as evidence. + +`.bitbucket/pull_request_template.md` is the single source of the structure: a risk size (S, M, L), "What changed and why", and an evidence table. Read it every run; never copy its sections into this skill or into a generated file from memory. The `tool/hooks/pre-push` hook and the pipeline check the finished file. + +## 1. Resolve the ticket and the output path + +1. Read the branch with `git branch --show-current`. +2. Take the first match of `[A-Z][A-Z0-9]+-[0-9]+` (uppercase only, so `fix/dark-mode-2` is not a ticket). +3. The output path is `.ai/pr-descriptions/.md`. + +No match, or a detached HEAD: stop and ask the author for the ticket key and to rename the branch. Do not invent one. + +Done when the ticket key and the output path are known. + +## 2. Gather evidence + +- **Diff**: base is `git merge-base HEAD origin/`; read the commit list and `git diff --stat` for the range. +- **Ticket**: the description and acceptance criteria from Jira through the `tracker` skill when it is configured. Otherwise ask the author to paste the acceptance criteria. Do not reconstruct criteria from the diff. +- **Tests**: the test files the diff adds or changes, with the test names (`test(`, `testWidgets(`, `group(`). + +Done when each of the three is read, or marked unavailable with the reason. + +## 3. Propose the risk size + +Apply the criteria in the template's risk comment to the diff. State the size and the criterion that decided it. One **L** criterion makes it L, whatever the line count. The author may raise the size, never lower it below what a criterion demands. + +## 4. Fill the template + +Fill only what the evidence supports. Everything else keeps its `TODO`. + +| Field | Rule | +|---|---| +| Risk and "Why this size?" | The proposal from step 3, naming the criterion. | +| "Where should the reviewer look, and what am I unsure about?" | Keep `TODO`. You may add one `` comment naming the largest or most unusual change. | +| Jira | The ticket link. | +| What changed and why | Keep `TODO`. It must be the author's own words. | +| Acceptance criteria rows | Copy each criterion verbatim. Fill "Evidence" with a test file and test name only when a test in the diff covers it, and mark "Covered?" **Yes** only then. Otherwise keep `TODO`. | +| Edge cases, "Tested on", recordings, Maestro | Keep `TODO`. Only the author ran them. | + +For size S, the evidence minimum is a test or screenshot per criterion; for M and L the template asks for more. Do not weaken the template's minimum. + +If the output file already exists, keep every line the author changed and update only the lines derived from evidence. Show the author what changed. + +Done when the file exists at the output path and every remaining `TODO` is a field only the author can answer. + +## 5. Report the gaps + +End with a short list in the chat, not only in the file: + +- acceptance criteria no test in the diff covers +- remaining `TODO` fields, grouped by section +- evidence the chosen size still requires (recording, Maestro flow, second platform) +- a diff that is unusually large or mixes unrelated changes; suggest splitting it + +Do not push, open the pull request, or edit the ticket. diff --git a/lib/di/injectable.config.dart b/lib/di/injectable.config.dart index 95aee8fd..718c92ee 100644 --- a/lib/di/injectable.config.dart +++ b/lib/di/injectable.config.dart @@ -61,6 +61,7 @@ import 'package:flutter_template/util/locale/localization_overrides_impl.dart' as _i908; import 'package:flutter_template/util/logging/sentry_performance_logger.dart' as _i1012; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart' as _i563; import 'package:flutter_template/util/snackbar/error_util.dart' as _i235; import 'package:flutter_template/util/theme/theme_config.dart' as _i437; import 'package:flutter_template/viewmodel/debug/debug_platform_selector_viewmodel.dart' @@ -135,6 +136,7 @@ extension GetItInjectableX on _i174.GetIt { () => registerModule.provideFirebaseRemoteConfig()); gh.lazySingleton<_i235.ErrorUtil>(() => _i235.ErrorUtil()); gh.lazySingleton<_i437.ThemeConfigUtil>(() => _i437.ThemeConfigUtil()); + gh.lazySingleton<_i563.SentryEventFilter>(() => _i563.SentryEventFilter()); gh.lazySingleton<_i1012.SentryPerformanceLogger>( () => _i1012.SentryPerformanceLogger()); gh.lazySingleton<_i44.RemoteConfigRepository>( diff --git a/lib/main.dart b/lib/main.dart index 09655938..a4655f1d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -9,6 +9,7 @@ import 'package:flutter_template/util/inspector/database_inspector.dart'; import 'package:flutter_template/util/inspector/local_storage_inspector.dart'; import 'package:flutter_template/util/inspector/niddler.dart'; import 'package:flutter_template/util/logging/sentry_performance_logger.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; Future main() async { @@ -31,9 +32,11 @@ Future main() async { await di.configureDependencies(Environments.dev); await addDatabaseInspector(); await initAllStorageInspectors(); - + final sentryEventFilter = di.getIt()..init(minimumLevelToKeep: SentryLevel.debug); await SentryFlutter.init( - (options) => SentryConfig.development(options), + (options) => SentryConfig.development( + options..beforeSend = sentryEventFilter.beforeSend, + ), appRunner: () { di.getIt().startAppLoadTransaction(); runApp(const MyApp()); diff --git a/lib/main_alpha.dart b/lib/main_alpha.dart index 4d60628f..8e5136f0 100644 --- a/lib/main_alpha.dart +++ b/lib/main_alpha.dart @@ -7,6 +7,7 @@ import 'package:flutter_template/main_common.dart'; import 'package:flutter_template/util/env/sentry_config.dart'; import 'package:flutter_template/util/env/flavor_config.dart'; import 'package:flutter_template/util/logging/sentry_performance_logger.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; Future main() async { @@ -24,8 +25,11 @@ Future main() async { values: values, ); await configureDependencies(Environments.prod); + final sentryEventFilter = di.getIt()..init(minimumLevelToKeep: SentryLevel.warning); await SentryFlutter.init( - (options) => SentryConfig.alpha(options), + (options) => SentryConfig.alpha( + options..beforeSend = sentryEventFilter.beforeSend, + ), appRunner: () { di.getIt().startAppLoadTransaction(); runApp(const MyApp()); diff --git a/lib/main_beta.dart b/lib/main_beta.dart index 98b42fda..4196dae2 100644 --- a/lib/main_beta.dart +++ b/lib/main_beta.dart @@ -7,6 +7,7 @@ import 'package:flutter_template/main_common.dart'; import 'package:flutter_template/util/env/sentry_config.dart'; import 'package:flutter_template/util/env/flavor_config.dart'; import 'package:flutter_template/util/logging/sentry_performance_logger.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; Future main() async { @@ -14,7 +15,7 @@ Future main() async { const values = FlavorValues( baseUrl: 'https://jsonplaceholder.typicode.com/', logNetworkInfo: false, - showFullErrorMessages: true, + showFullErrorMessages: true, dsn: '', ); FlavorConfig( @@ -24,8 +25,11 @@ Future main() async { values: values, ); await configureDependencies(Environments.prod); + final sentryEventFilter = di.getIt()..init(minimumLevelToKeep: SentryLevel.warning); await SentryFlutter.init( - (options) => SentryConfig.beta(options), + (options) => SentryConfig.beta( + options..beforeSend = sentryEventFilter.beforeSend, + ), appRunner: () { di.getIt().startAppLoadTransaction(); runApp(const MyApp()); diff --git a/lib/main_prod.dart b/lib/main_prod.dart index 80aa9b56..b3c6741d 100644 --- a/lib/main_prod.dart +++ b/lib/main_prod.dart @@ -8,6 +8,7 @@ import 'package:flutter_template/main_common.dart'; import 'package:flutter_template/util/env/sentry_config.dart'; import 'package:flutter_template/util/env/flavor_config.dart'; import 'package:flutter_template/util/logging/sentry_performance_logger.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; Future main() async { @@ -26,8 +27,11 @@ Future main() async { values: values, ); await configureDependencies(Environments.prod); + final sentryEventFilter = di.getIt()..init(minimumLevelToKeep: SentryLevel.error); await SentryFlutter.init( - (options) => SentryConfig.prod(options), + (options) => SentryConfig.prod( + options..beforeSend = sentryEventFilter.beforeSend, + ), appRunner: () { di.getIt().startAppLoadTransaction(); runApp(const MyApp()); diff --git a/lib/navigator/main_navigator.navigator.dart b/lib/navigator/main_navigator.navigator.dart index 80fd3899..fae2b3dc 100644 --- a/lib/navigator/main_navigator.navigator.dart +++ b/lib/navigator/main_navigator.navigator.dart @@ -60,6 +60,12 @@ mixin BaseNavigator { settings: settings, fullscreenDialog: false, ); + case RouteNames.todoAddScreen: + return MaterialPageRoute( + builder: (_) => TodoAddScreen(), + settings: settings, + fullscreenDialog: false, + ); case RouteNames.logsScreen: return MaterialPageRoute( builder: (_) => LogsScreen(), @@ -96,12 +102,6 @@ mixin BaseNavigator { settings: settings, fullscreenDialog: false, ); - case RouteNames.todoAddScreen: - return MaterialPageRoute( - builder: (_) => TodoAddScreen(), - settings: settings, - fullscreenDialog: false, - ); } return null; } @@ -128,6 +128,8 @@ mixin BaseNavigator { ).toString(), arguments: {'date': date}, ); + Future goToTodoAddScreen() async => navigatorKey.currentState + ?.pushNamed(RouteNames.todoAddScreen, arguments: {}); Future goToLogsScreen() async => navigatorKey.currentState ?.pushNamed(RouteNames.logsScreen, arguments: {}); Future goToAnalyticsPermissionScreen() async => navigatorKey @@ -148,8 +150,6 @@ mixin BaseNavigator { ); Future goToDebugScreen() async => navigatorKey.currentState ?.pushNamed(RouteNames.debugScreen, arguments: {}); - Future goToTodoAddScreen() async => navigatorKey.currentState - ?.pushNamed(RouteNames.todoAddScreen, arguments: {}); void goBack() => navigatorKey.currentState?.pop(); void goBackWithResult({T? result}) => navigatorKey.currentState?.pop(result); @@ -181,6 +181,9 @@ class RouteNames { /// /log-detail static const logDetailScreen = '/log-detail'; + /// /todo-add + static const todoAddScreen = '/todo-add'; + /// /logs static const logsScreen = '/logs'; @@ -198,7 +201,4 @@ class RouteNames { /// /debug static const debugScreen = '/debug'; - - /// /todo-add - static const todoAddScreen = '/todo-add'; } diff --git a/lib/util/lifecycle/lifecycle_util.dart b/lib/util/lifecycle/lifecycle_util.dart new file mode 100644 index 00000000..e3088c88 --- /dev/null +++ b/lib/util/lifecycle/lifecycle_util.dart @@ -0,0 +1,9 @@ +import 'package:flutter/widgets.dart'; + +class LifecycleUtil { + static bool get appIsInBackground { + final state = WidgetsBinding.instance.lifecycleState; + + return state == AppLifecycleState.paused || state == AppLifecycleState.hidden; + } +} diff --git a/lib/util/sentry/background_sentry_event_filter.dart b/lib/util/sentry/background_sentry_event_filter.dart new file mode 100644 index 00000000..75a6b06f --- /dev/null +++ b/lib/util/sentry/background_sentry_event_filter.dart @@ -0,0 +1,49 @@ +import 'dart:convert'; + +import 'package:flutter_template/util/lifecycle/lifecycle_util.dart'; +import 'package:flutter_template/util/sentry/observability_error_filter.dart'; +import 'package:flutter_template/util/sentry/sentry_drop_rule.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +final class BackgroundSentryEventFilter extends SentryEventFilter { + BackgroundSentryEventFilter({ + super.minimumLevelToKeep, + }) : super.shared( + defaultRules: rules, + ); + + static final List rules = [ + SentryDropRule( + id: 'example_background_event', + matcher: _dropExampleBackgroundEvent, + ), + ]; + + @override + bool get shouldApplyRules => LifecycleUtil.appIsInBackground; + + @override + SentryEvent? beforeSend(SentryEvent event, Hint hint) { + return applyRules(event, hint); + } + + static bool _dropExampleBackgroundEvent( + SentryEvent event, + Hint hint, + ) { + return _eventTextMatches(event); + } + + static bool _eventTextMatches( + SentryEvent event, + ) { + return ObservabilityErrorFilter.shouldDropBackgroundEvent( + candidateTexts: [ + event.throwable?.toString(), + event.message?.formatted, + jsonEncode(event.toJson()), + ], + ); + } +} diff --git a/lib/util/sentry/combined_sentry_event_filter.dart b/lib/util/sentry/combined_sentry_event_filter.dart new file mode 100644 index 00000000..6121c408 --- /dev/null +++ b/lib/util/sentry/combined_sentry_event_filter.dart @@ -0,0 +1,39 @@ +import 'package:flutter_template/util/sentry/background_sentry_event_filter.dart'; +import 'package:flutter_template/util/sentry/general_sentry_event_filter.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +final class CombinedSentryEventFilter extends SentryEventFilter { + CombinedSentryEventFilter({ + SentryLevel? minimumLevelToKeep, + }) : _backgroundFilter = BackgroundSentryEventFilter( + minimumLevelToKeep: minimumLevelToKeep, + ), + _generalFilter = GeneralSentryEventFilter( + minimumLevelToKeep: minimumLevelToKeep, + ), + super.shared(defaultRules: const []); + + final BackgroundSentryEventFilter _backgroundFilter; + final GeneralSentryEventFilter _generalFilter; + + @override + bool get shouldApplyRules => true; + + @override + void init({SentryLevel? minimumLevelToKeep}) { + super.init(minimumLevelToKeep: minimumLevelToKeep); + _backgroundFilter.init(minimumLevelToKeep: minimumLevelToKeep); + _generalFilter.init(minimumLevelToKeep: minimumLevelToKeep); + } + + @override + SentryEvent? beforeSend(SentryEvent event, Hint hint) { + final backgroundResult = _backgroundFilter.beforeSend(event, hint); + if (backgroundResult == null) { + return null; + } + + return _generalFilter.beforeSend(backgroundResult, hint); + } +} diff --git a/lib/util/sentry/general_sentry_event_filter.dart b/lib/util/sentry/general_sentry_event_filter.dart new file mode 100644 index 00000000..04dddae0 --- /dev/null +++ b/lib/util/sentry/general_sentry_event_filter.dart @@ -0,0 +1,21 @@ +import 'package:flutter_template/util/sentry/sentry_drop_rule.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +final class GeneralSentryEventFilter extends SentryEventFilter { + GeneralSentryEventFilter({ + super.minimumLevelToKeep, + }) : super.shared( + defaultRules: rules, + ); + + static final List rules = []; + + @override + bool get shouldApplyRules => true; + + @override + SentryEvent? beforeSend(SentryEvent event, Hint hint) { + return applyRules(event, hint); + } +} diff --git a/lib/util/sentry/observability_error_filter.dart b/lib/util/sentry/observability_error_filter.dart new file mode 100644 index 00000000..52ac16cf --- /dev/null +++ b/lib/util/sentry/observability_error_filter.dart @@ -0,0 +1,38 @@ +import 'package:flutter_template/util/lifecycle/lifecycle_util.dart'; + +final class ObservabilityErrorFilter { + const ObservabilityErrorFilter._(); + + static bool shouldDropBackgroundEvent({ + required Iterable candidateTexts, + }) { + if (!LifecycleUtil.appIsInBackground) { + return false; + } + + return _matchesAny(candidateTexts, _isExampleMatch); + } + + static bool _matchesAny( + Iterable candidateTexts, + bool Function(String text) matcher, + ) { + for (final candidateText in candidateTexts) { + final normalizedText = candidateText?.toLowerCase(); + if (normalizedText == null) { + continue; + } + + if (matcher(normalizedText)) { + return true; + } + } + + return false; + } + + /// This is en example and will never do anything + static bool _isExampleMatch(String text) { + return false; + } +} diff --git a/lib/util/sentry/sentry_drop_rule.dart b/lib/util/sentry/sentry_drop_rule.dart new file mode 100644 index 00000000..dcc25b65 --- /dev/null +++ b/lib/util/sentry/sentry_drop_rule.dart @@ -0,0 +1,17 @@ +import 'package:sentry_flutter/sentry_flutter.dart'; + +typedef SentryDropRuleMatcher = + bool Function( + SentryEvent event, + Hint hint, + ); + +final class SentryDropRule { + const SentryDropRule({ + required this.id, + required this.matcher, + }); + + final String id; + final SentryDropRuleMatcher matcher; +} diff --git a/lib/util/sentry/sentry_event_filter.dart b/lib/util/sentry/sentry_event_filter.dart new file mode 100644 index 00000000..57139e8f --- /dev/null +++ b/lib/util/sentry/sentry_event_filter.dart @@ -0,0 +1,63 @@ +import 'package:flutter_template/util/sentry/sentry_drop_rule.dart'; +import 'package:flutter_template/util/sentry/combined_sentry_event_filter.dart'; +import 'package:injectable/injectable.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +@lazySingleton +abstract class SentryEventFilter { + @factoryMethod + factory SentryEventFilter() = CombinedSentryEventFilter; + + SentryEventFilter.shared({ + required List defaultRules, + SentryLevel? minimumLevelToKeep, + }) : _rules = [ + ...defaultRules, + ], + _minimumLevelToKeep = minimumLevelToKeep; + + final List _rules; + SentryLevel? _minimumLevelToKeep; + + bool get shouldApplyRules; + + void init({SentryLevel? minimumLevelToKeep}) { + _minimumLevelToKeep = minimumLevelToKeep; + } + + SentryEvent? beforeSend(SentryEvent event, Hint hint); + + SentryEvent? applyRules(SentryEvent event, Hint hint) { + if (!shouldApplyRules) { + return event; + } + + for (final rule in _rules) { + if (!rule.matcher(event, hint)) { + continue; + } + + if (_shouldKeepMatchedEventForLevel(event)) { + return event; + } + + return null; + } + + return event; + } + + bool _shouldKeepMatchedEventForLevel(SentryEvent event) { + final minimumLevel = _minimumLevelToKeep; + if (minimumLevel == null) { + return false; + } + + final eventLevel = event.level; + if (eventLevel == null) { + return false; + } + + return eventLevel.toDartLogLevel() >= minimumLevel.toDartLogLevel(); + } +} diff --git a/test/util/lifecycle/lifecycle_util_test.dart b/test/util/lifecycle/lifecycle_util_test.dart new file mode 100644 index 00000000..840e830e --- /dev/null +++ b/test/util/lifecycle/lifecycle_util_test.dart @@ -0,0 +1,50 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_template/util/lifecycle/lifecycle_util.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + final binding = TestWidgetsFlutterBinding.ensureInitialized(); + + setUp(() { + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + }); + + tearDown(() { + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + }); + + group('LifecycleUtil.appIsInBackground', () { + test('is false when the app is resumed', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + + expect(LifecycleUtil.appIsInBackground, isFalse); + }); + + test('is false when the app is inactive', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + + expect(LifecycleUtil.appIsInBackground, isFalse); + }); + + test('is true when the app is hidden', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden); + + expect(LifecycleUtil.appIsInBackground, isTrue); + }); + + test('is true when the app is paused', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); + + expect(LifecycleUtil.appIsInBackground, isTrue); + }); + + test('is false again after the app returns to the foreground', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); + expect(LifecycleUtil.appIsInBackground, isTrue); + + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + + expect(LifecycleUtil.appIsInBackground, isFalse); + }); + }); +} diff --git a/test/util/sentry/background_sentry_event_filter_test.dart b/test/util/sentry/background_sentry_event_filter_test.dart new file mode 100644 index 00000000..0073a485 --- /dev/null +++ b/test/util/sentry/background_sentry_event_filter_test.dart @@ -0,0 +1,106 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_template/util/sentry/background_sentry_event_filter.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +import 'sentry_test_util.dart'; + +void main() { + final binding = TestWidgetsFlutterBinding.ensureInitialized(); + + void setAppLifecycleState(AppLifecycleState state) { + binding.handleAppLifecycleStateChanged(state); + } + + setUp(() { + setAppLifecycleState(AppLifecycleState.resumed); + }); + + tearDown(() { + setAppLifecycleState(AppLifecycleState.resumed); + }); + + group('BackgroundSentryEventFilter', () { + test('does not apply rules in the foreground', () { + setAppLifecycleState(AppLifecycleState.resumed); + + expect(BackgroundSentryEventFilter().shouldApplyRules, isFalse); + }); + + test('applies rules when the app is paused', () { + setAppLifecycleState(AppLifecycleState.paused); + + expect(BackgroundSentryEventFilter().shouldApplyRules, isTrue); + }); + + test('applies rules when the app is hidden', () { + setAppLifecycleState(AppLifecycleState.hidden); + + expect(BackgroundSentryEventFilter().shouldApplyRules, isTrue); + }); + + test('registers the example rule', () { + expect(BackgroundSentryEventFilter.rules.map((rule) => rule.id), + contains('example_background_event')); + }); + + test( + 'keeps events in the background because the example rule never matches', + () { + setAppLifecycleState(AppLifecycleState.paused); + final events = [ + SentryEvent(throwable: Exception('some error')), + eventWithMessage('some message'), + SentryEvent(logger: 'some logger'), + ]; + + for (final event in events) { + expect(BackgroundSentryEventFilter().beforeSend(event, Hint()), + same(event)); + } + }); + + test('drops events matching a registered rule in the background', () { + final rule = + ruleDroppingMessage('background_test_rule', 'background drop'); + BackgroundSentryEventFilter.rules.add(rule); + addTearDown(() => BackgroundSentryEventFilter.rules.remove(rule)); + setAppLifecycleState(AppLifecycleState.paused); + + final result = BackgroundSentryEventFilter() + .beforeSend(eventWithMessage('background drop'), Hint()); + + expect(result, isNull); + }); + + test('keeps events matching a registered rule in the foreground', () { + final rule = + ruleDroppingMessage('background_test_rule', 'background drop'); + BackgroundSentryEventFilter.rules.add(rule); + addTearDown(() => BackgroundSentryEventFilter.rules.remove(rule)); + setAppLifecycleState(AppLifecycleState.resumed); + final event = eventWithMessage('background drop'); + + expect( + BackgroundSentryEventFilter().beforeSend(event, Hint()), same(event)); + }); + + test('keeps matched events at or above the minimum level in the background', + () { + final rule = + ruleDroppingMessage('background_test_rule', 'background drop'); + BackgroundSentryEventFilter.rules.add(rule); + addTearDown(() => BackgroundSentryEventFilter.rules.remove(rule)); + setAppLifecycleState(AppLifecycleState.paused); + final filter = + BackgroundSentryEventFilter(minimumLevelToKeep: SentryLevel.fatal); + final fatal = + eventWithMessage('background drop', level: SentryLevel.fatal); + final error = + eventWithMessage('background drop', level: SentryLevel.error); + + expect(filter.beforeSend(fatal, Hint()), same(fatal)); + expect(filter.beforeSend(error, Hint()), isNull); + }); + }); +} diff --git a/test/util/sentry/combined_sentry_event_filter_test.dart b/test/util/sentry/combined_sentry_event_filter_test.dart new file mode 100644 index 00000000..244594f8 --- /dev/null +++ b/test/util/sentry/combined_sentry_event_filter_test.dart @@ -0,0 +1,129 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_template/util/sentry/background_sentry_event_filter.dart'; +import 'package:flutter_template/util/sentry/combined_sentry_event_filter.dart'; +import 'package:flutter_template/util/sentry/general_sentry_event_filter.dart'; +import 'package:flutter_template/util/sentry/sentry_drop_rule.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +import 'sentry_test_util.dart'; + +void main() { + final binding = TestWidgetsFlutterBinding.ensureInitialized(); + + void setAppLifecycleState(AppLifecycleState state) { + binding.handleAppLifecycleStateChanged(state); + } + + late SentryDropRule backgroundRule; + late SentryDropRule generalRule; + + setUp(() { + setAppLifecycleState(AppLifecycleState.resumed); + backgroundRule = + ruleDroppingMessage('background_test_rule', 'background drop'); + generalRule = ruleDroppingMessage('general_test_rule', 'general drop'); + }); + + tearDown(() { + setAppLifecycleState(AppLifecycleState.resumed); + BackgroundSentryEventFilter.rules.remove(backgroundRule); + GeneralSentryEventFilter.rules.remove(generalRule); + }); + + void registerBackgroundRule() => + BackgroundSentryEventFilter.rules.add(backgroundRule); + void registerGeneralRule() => GeneralSentryEventFilter.rules.add(generalRule); + + group('CombinedSentryEventFilter', () { + test('always applies rules', () { + expect(CombinedSentryEventFilter().shouldApplyRules, isTrue); + }); + + test('keeps events by default', () { + setAppLifecycleState(AppLifecycleState.paused); + final event = SentryEvent(throwable: Exception('some error')); + + expect( + CombinedSentryEventFilter().beforeSend(event, Hint()), same(event)); + }); + + test('drops events matched by a background rule while in the background', + () { + registerBackgroundRule(); + setAppLifecycleState(AppLifecycleState.paused); + + expect( + CombinedSentryEventFilter() + .beforeSend(eventWithMessage('background drop'), Hint()), + isNull); + }); + + test('keeps events matched by a background rule while in the foreground', + () { + registerBackgroundRule(); + setAppLifecycleState(AppLifecycleState.resumed); + final event = eventWithMessage('background drop'); + + expect( + CombinedSentryEventFilter().beforeSend(event, Hint()), same(event)); + }); + + test('drops events matched by a general rule regardless of lifecycle', () { + registerGeneralRule(); + final filter = CombinedSentryEventFilter(); + + setAppLifecycleState(AppLifecycleState.resumed); + expect( + filter.beforeSend(eventWithMessage('general drop'), Hint()), isNull); + + setAppLifecycleState(AppLifecycleState.paused); + expect( + filter.beforeSend(eventWithMessage('general drop'), Hint()), isNull); + }); + + test('keeps events that match no rule', () { + registerBackgroundRule(); + registerGeneralRule(); + setAppLifecycleState(AppLifecycleState.paused); + final event = eventWithMessage('unrelated'); + + expect( + CombinedSentryEventFilter().beforeSend(event, Hint()), same(event)); + }); + + test('applies the minimum level passed to the constructor to both filters', + () { + registerBackgroundRule(); + registerGeneralRule(); + setAppLifecycleState(AppLifecycleState.paused); + final filter = + CombinedSentryEventFilter(minimumLevelToKeep: SentryLevel.error); + final backgroundEvent = + eventWithMessage('background drop', level: SentryLevel.error); + final generalEvent = + eventWithMessage('general drop', level: SentryLevel.error); + + expect(filter.beforeSend(backgroundEvent, Hint()), same(backgroundEvent)); + expect(filter.beforeSend(generalEvent, Hint()), same(generalEvent)); + }); + + test('init propagates the minimum level to both filters', () { + registerBackgroundRule(); + registerGeneralRule(); + setAppLifecycleState(AppLifecycleState.paused); + final filter = CombinedSentryEventFilter(); + final backgroundEvent = + eventWithMessage('background drop', level: SentryLevel.error); + final generalEvent = + eventWithMessage('general drop', level: SentryLevel.error); + expect(filter.beforeSend(backgroundEvent, Hint()), isNull); + expect(filter.beforeSend(generalEvent, Hint()), isNull); + + filter.init(minimumLevelToKeep: SentryLevel.error); + + expect(filter.beforeSend(backgroundEvent, Hint()), same(backgroundEvent)); + expect(filter.beforeSend(generalEvent, Hint()), same(generalEvent)); + }); + }); +} diff --git a/test/util/sentry/general_sentry_event_filter_test.dart b/test/util/sentry/general_sentry_event_filter_test.dart new file mode 100644 index 00000000..927eaa5a --- /dev/null +++ b/test/util/sentry/general_sentry_event_filter_test.dart @@ -0,0 +1,48 @@ +import 'package:flutter_template/util/sentry/general_sentry_event_filter.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +import 'sentry_test_util.dart'; + +void main() { + group('GeneralSentryEventFilter', () { + test('always applies rules', () { + expect(GeneralSentryEventFilter().shouldApplyRules, isTrue); + }); + + test('ships without default rules', () { + expect(GeneralSentryEventFilter.rules, isEmpty); + }); + + test('keeps events by default', () { + final event = + SentryEvent(throwable: Exception('general filter default behavior')); + + expect(GeneralSentryEventFilter().beforeSend(event, Hint()), same(event)); + }); + + test('drops events matching a registered rule', () { + final rule = ruleDroppingMessage('general_test_rule', 'general drop'); + GeneralSentryEventFilter.rules.add(rule); + addTearDown(() => GeneralSentryEventFilter.rules.remove(rule)); + + final filter = GeneralSentryEventFilter(); + + expect( + filter.beforeSend(eventWithMessage('general drop'), Hint()), isNull); + }); + + test('keeps matched events at or above the minimum level', () { + final rule = ruleDroppingMessage('general_test_rule', 'general drop'); + GeneralSentryEventFilter.rules.add(rule); + addTearDown(() => GeneralSentryEventFilter.rules.remove(rule)); + final filter = + GeneralSentryEventFilter(minimumLevelToKeep: SentryLevel.fatal); + final fatal = eventWithMessage('general drop', level: SentryLevel.fatal); + final error = eventWithMessage('general drop', level: SentryLevel.error); + + expect(filter.beforeSend(fatal, Hint()), same(fatal)); + expect(filter.beforeSend(error, Hint()), isNull); + }); + }); +} diff --git a/test/util/sentry/observability_error_filter_test.dart b/test/util/sentry/observability_error_filter_test.dart new file mode 100644 index 00000000..54538492 --- /dev/null +++ b/test/util/sentry/observability_error_filter_test.dart @@ -0,0 +1,55 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_template/util/sentry/observability_error_filter.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + final binding = TestWidgetsFlutterBinding.ensureInitialized(); + + setUp(() { + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + }); + + tearDown(() { + binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + }); + + group('ObservabilityErrorFilter.shouldDropBackgroundEvent', () { + test('does not drop anything in the foreground', () { + final result = ObservabilityErrorFilter.shouldDropBackgroundEvent( + candidateTexts: ['some error'], + ); + + expect(result, isFalse); + }); + + test('does not drop the template example texts in the background', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); + + final result = ObservabilityErrorFilter.shouldDropBackgroundEvent( + candidateTexts: ['some error', 'Example', null], + ); + + expect(result, isFalse); + }); + + test('handles an empty list of candidates', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); + + final result = ObservabilityErrorFilter.shouldDropBackgroundEvent( + candidateTexts: const [], + ); + + expect(result, isFalse); + }); + + test('handles null-only candidates', () { + binding.handleAppLifecycleStateChanged(AppLifecycleState.paused); + + final result = ObservabilityErrorFilter.shouldDropBackgroundEvent( + candidateTexts: const [null, null], + ); + + expect(result, isFalse); + }); + }); +} diff --git a/test/util/sentry/sentry_drop_rule_test.dart b/test/util/sentry/sentry_drop_rule_test.dart new file mode 100644 index 00000000..94116092 --- /dev/null +++ b/test/util/sentry/sentry_drop_rule_test.dart @@ -0,0 +1,37 @@ +import 'package:flutter_template/util/sentry/sentry_drop_rule.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +void main() { + group('SentryDropRule', () { + test('exposes its id and matcher', () { + bool matcher(SentryEvent event, Hint hint) => true; + + final rule = SentryDropRule(id: 'rule', matcher: matcher); + + expect(rule.id, 'rule'); + expect(rule.matcher, same(matcher)); + }); + + test('matcher receives the event and hint it is invoked with', () { + SentryEvent? receivedEvent; + Hint? receivedHint; + final rule = SentryDropRule( + id: 'capture', + matcher: (event, hint) { + receivedEvent = event; + receivedHint = hint; + return true; + }, + ); + final event = SentryEvent(); + final hint = Hint(); + + final result = rule.matcher(event, hint); + + expect(result, isTrue); + expect(receivedEvent, same(event)); + expect(receivedHint, same(hint)); + }); + }); +} diff --git a/test/util/sentry/sentry_event_filter_test.dart b/test/util/sentry/sentry_event_filter_test.dart new file mode 100644 index 00000000..000fdf14 --- /dev/null +++ b/test/util/sentry/sentry_event_filter_test.dart @@ -0,0 +1,202 @@ +import 'package:flutter_template/util/sentry/combined_sentry_event_filter.dart'; +import 'package:flutter_template/util/sentry/sentry_drop_rule.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +import 'sentry_test_util.dart'; + +void main() { + group('SentryEventFilter factory', () { + test('creates a CombinedSentryEventFilter', () { + expect(SentryEventFilter(), isA()); + }); + }); + + group('SentryEventFilter.applyRules', () { + test('keeps the event when there are no rules', () { + final filter = TestSentryEventFilter(rules: const []); + final event = eventWithMessage('anything'); + + expect(filter.beforeSend(event, Hint()), same(event)); + }); + + test('keeps the event when no rule matches', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop me')]); + final event = eventWithMessage('keep me'); + + expect(filter.beforeSend(event, Hint()), same(event)); + }); + + test('drops the event when a rule matches', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop me')]); + + expect(filter.beforeSend(eventWithMessage('please drop me'), Hint()), + isNull); + }); + + test('drops the event when only one of multiple rules matches', () { + final filter = TestSentryEventFilter( + rules: [ + ruleDroppingMessage('first', 'first'), + ruleDroppingMessage('second', 'second'), + ], + ); + + expect(filter.beforeSend(eventWithMessage('the second one'), Hint()), + isNull); + }); + + test('passes the event and hint to the matcher', () { + SentryEvent? receivedEvent; + Hint? receivedHint; + final filter = TestSentryEventFilter( + rules: [ + SentryDropRule( + id: 'capture', + matcher: (event, hint) { + receivedEvent = event; + receivedHint = hint; + return false; + }, + ), + ], + ); + final event = eventWithMessage('event'); + final hint = Hint(); + + filter.beforeSend(event, hint); + + expect(receivedEvent, same(event)); + expect(receivedHint, same(hint)); + }); + + test('ignores all rules when shouldApplyRules is false', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop me')], + applyRulesEnabled: false, + ); + final event = eventWithMessage('drop me'); + + expect(filter.beforeSend(event, Hint()), same(event)); + }); + + test('does not evaluate matchers when shouldApplyRules is false', () { + var matcherCalls = 0; + final filter = TestSentryEventFilter( + rules: [ + SentryDropRule( + id: 'counting', + matcher: (event, hint) { + matcherCalls++; + return true; + }, + ), + ], + applyRulesEnabled: false, + ); + + filter.beforeSend(eventWithMessage('x'), Hint()); + + expect(matcherCalls, 0); + }); + + test('does not mutate the default rules list passed in', () { + final rules = [ruleDroppingMessage('drop', 'drop me')]; + TestSentryEventFilter(rules: rules); + + expect(rules, hasLength(1)); + }); + }); + + group('SentryEventFilter minimumLevelToKeep', () { + test('drops a matched event when no minimum level is configured', () { + final filter = + TestSentryEventFilter(rules: [ruleDroppingMessage('drop', 'drop')]); + + expect( + filter.beforeSend( + eventWithMessage('drop', level: SentryLevel.fatal), Hint()), + isNull); + }); + + test('keeps a matched event whose level equals the minimum level', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop')], + minimumLevelToKeep: SentryLevel.error, + ); + final event = eventWithMessage('drop', level: SentryLevel.error); + + expect(filter.beforeSend(event, Hint()), same(event)); + }); + + test('keeps a matched event whose level is above the minimum level', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop')], + minimumLevelToKeep: SentryLevel.warning, + ); + final event = eventWithMessage('drop', level: SentryLevel.fatal); + + expect(filter.beforeSend(event, Hint()), same(event)); + }); + + test('drops a matched event whose level is below the minimum level', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop')], + minimumLevelToKeep: SentryLevel.fatal, + ); + + expect( + filter.beforeSend( + eventWithMessage('drop', level: SentryLevel.error), Hint()), + isNull); + }); + + test( + 'drops a matched event without level even when a minimum level is configured', + () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop')], + minimumLevelToKeep: SentryLevel.debug, + ); + + expect(filter.beforeSend(eventWithMessage('drop'), Hint()), isNull); + }); + + test('keeps unmatched events regardless of the minimum level', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop')], + minimumLevelToKeep: SentryLevel.fatal, + ); + final event = eventWithMessage('other', level: SentryLevel.debug); + + expect(filter.beforeSend(event, Hint()), same(event)); + }); + + test('init updates the minimum level', () { + final filter = + TestSentryEventFilter(rules: [ruleDroppingMessage('drop', 'drop')]); + final event = eventWithMessage('drop', level: SentryLevel.error); + expect(filter.beforeSend(event, Hint()), isNull); + + filter.init(minimumLevelToKeep: SentryLevel.error); + + expect(filter.beforeSend(event, Hint()), same(event)); + }); + + test('init without a level clears the minimum level', () { + final filter = TestSentryEventFilter( + rules: [ruleDroppingMessage('drop', 'drop')], + minimumLevelToKeep: SentryLevel.debug, + ); + final event = eventWithMessage('drop', level: SentryLevel.error); + expect(filter.beforeSend(event, Hint()), same(event)); + + filter.init(); + + expect(filter.beforeSend(event, Hint()), isNull); + }); + }); +} diff --git a/test/util/sentry/sentry_test_util.dart b/test/util/sentry/sentry_test_util.dart new file mode 100644 index 00000000..7a27fab3 --- /dev/null +++ b/test/util/sentry/sentry_test_util.dart @@ -0,0 +1,33 @@ +import 'package:flutter_template/util/sentry/sentry_drop_rule.dart'; +import 'package:flutter_template/util/sentry/sentry_event_filter.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; + +/// Minimal filter to exercise the shared rule engine in [SentryEventFilter]. +final class TestSentryEventFilter extends SentryEventFilter { + TestSentryEventFilter({ + required List rules, + this.applyRulesEnabled = true, + super.minimumLevelToKeep, + }) : super.shared(defaultRules: rules); + + final bool applyRulesEnabled; + + @override + bool get shouldApplyRules => applyRulesEnabled; + + @override + SentryEvent? beforeSend(SentryEvent event, Hint hint) => + applyRules(event, hint); +} + +SentryDropRule ruleDroppingMessage(String id, String needle) { + return SentryDropRule( + id: id, + matcher: (event, hint) => + event.message?.formatted.contains(needle) ?? false, + ); +} + +SentryEvent eventWithMessage(String message, {SentryLevel? level}) { + return SentryEvent(message: SentryMessage(message), level: level); +} diff --git a/test/util/test_util.mocks.dart b/test/util/test_util.mocks.dart index f5455131..0707f831 100644 --- a/test/util/test_util.mocks.dart +++ b/test/util/test_util.mocks.dart @@ -476,6 +476,16 @@ class MockMainNavigator extends _i1.Mock implements _i10.MainNavigator { returnValueForMissingStub: _i5.Future.value(), ) as _i5.Future); + @override + _i5.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i5.Future.value(), + returnValueForMissingStub: _i5.Future.value(), + ) as _i5.Future); + @override _i5.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -535,16 +545,6 @@ class MockMainNavigator extends _i1.Mock implements _i10.MainNavigator { returnValueForMissingStub: _i5.Future.value(), ) as _i5.Future); - @override - _i5.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i5.Future.value(), - returnValueForMissingStub: _i5.Future.value(), - ) as _i5.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/test/viewmodel/debug/debug_platform_selector_viewmodel_test.mocks.dart b/test/viewmodel/debug/debug_platform_selector_viewmodel_test.mocks.dart index 4579da7a..b39668ea 100644 --- a/test/viewmodel/debug/debug_platform_selector_viewmodel_test.mocks.dart +++ b/test/viewmodel/debug/debug_platform_selector_viewmodel_test.mocks.dart @@ -183,6 +183,16 @@ class MockMainNavigator extends _i1.Mock implements _i3.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); + @override + _i4.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + @override _i4.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -242,16 +252,6 @@ class MockMainNavigator extends _i1.Mock implements _i3.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); - @override - _i4.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i4.Future.value(), - returnValueForMissingStub: _i4.Future.value(), - ) as _i4.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/test/viewmodel/debug/debug_viewmodel_test.mocks.dart b/test/viewmodel/debug/debug_viewmodel_test.mocks.dart index 81a696dd..bcb35252 100644 --- a/test/viewmodel/debug/debug_viewmodel_test.mocks.dart +++ b/test/viewmodel/debug/debug_viewmodel_test.mocks.dart @@ -401,6 +401,16 @@ class MockMainNavigator extends _i1.Mock implements _i7.MainNavigator { returnValueForMissingStub: _i6.Future.value(), ) as _i6.Future); + @override + _i6.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i6.Future.value(), + returnValueForMissingStub: _i6.Future.value(), + ) as _i6.Future); + @override _i6.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -460,16 +470,6 @@ class MockMainNavigator extends _i1.Mock implements _i7.MainNavigator { returnValueForMissingStub: _i6.Future.value(), ) as _i6.Future); - @override - _i6.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i6.Future.value(), - returnValueForMissingStub: _i6.Future.value(), - ) as _i6.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/test/viewmodel/license/license_viewmodel_test.mocks.dart b/test/viewmodel/license/license_viewmodel_test.mocks.dart index eee431c1..d23642fe 100644 --- a/test/viewmodel/license/license_viewmodel_test.mocks.dart +++ b/test/viewmodel/license/license_viewmodel_test.mocks.dart @@ -178,6 +178,16 @@ class MockMainNavigator extends _i1.Mock implements _i3.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); + @override + _i4.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + @override _i4.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -237,16 +247,6 @@ class MockMainNavigator extends _i1.Mock implements _i3.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); - @override - _i4.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i4.Future.value(), - returnValueForMissingStub: _i4.Future.value(), - ) as _i4.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/test/viewmodel/login/login_viewmodel_test.mocks.dart b/test/viewmodel/login/login_viewmodel_test.mocks.dart index 616a1e25..bdd8c9ce 100644 --- a/test/viewmodel/login/login_viewmodel_test.mocks.dart +++ b/test/viewmodel/login/login_viewmodel_test.mocks.dart @@ -219,6 +219,16 @@ class MockMainNavigator extends _i1.Mock implements _i5.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); + @override + _i4.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + @override _i4.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -278,16 +288,6 @@ class MockMainNavigator extends _i1.Mock implements _i5.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); - @override - _i4.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i4.Future.value(), - returnValueForMissingStub: _i4.Future.value(), - ) as _i4.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/test/viewmodel/theme_selector/theme_selector_viewmodel_test.mocks.dart b/test/viewmodel/theme_selector/theme_selector_viewmodel_test.mocks.dart index a82c476d..2206b3da 100644 --- a/test/viewmodel/theme_selector/theme_selector_viewmodel_test.mocks.dart +++ b/test/viewmodel/theme_selector/theme_selector_viewmodel_test.mocks.dart @@ -431,6 +431,16 @@ class MockMainNavigator extends _i1.Mock implements _i9.MainNavigator { returnValueForMissingStub: _i5.Future.value(), ) as _i5.Future); + @override + _i5.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i5.Future.value(), + returnValueForMissingStub: _i5.Future.value(), + ) as _i5.Future); + @override _i5.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -490,16 +500,6 @@ class MockMainNavigator extends _i1.Mock implements _i9.MainNavigator { returnValueForMissingStub: _i5.Future.value(), ) as _i5.Future); - @override - _i5.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i5.Future.value(), - returnValueForMissingStub: _i5.Future.value(), - ) as _i5.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/test/viewmodel/todo/todo_add/todo_add_viewmodel_test.mocks.dart b/test/viewmodel/todo/todo_add/todo_add_viewmodel_test.mocks.dart index b31687d7..a48995ef 100644 --- a/test/viewmodel/todo/todo_add/todo_add_viewmodel_test.mocks.dart +++ b/test/viewmodel/todo/todo_add/todo_add_viewmodel_test.mocks.dart @@ -235,6 +235,16 @@ class MockMainNavigator extends _i1.Mock implements _i6.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); + @override + _i4.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + @override _i4.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -294,16 +304,6 @@ class MockMainNavigator extends _i1.Mock implements _i6.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); - @override - _i4.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i4.Future.value(), - returnValueForMissingStub: _i4.Future.value(), - ) as _i4.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.mocks.dart b/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.mocks.dart index b4068ec1..745c07e8 100644 --- a/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.mocks.dart +++ b/test/viewmodel/todo/todo_list/todo_list_viewmodel_test.mocks.dart @@ -235,6 +235,16 @@ class MockMainNavigator extends _i1.Mock implements _i6.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); + @override + _i4.Future goToTodoAddScreen() => (super.noSuchMethod( + Invocation.method( + #goToTodoAddScreen, + [], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + @override _i4.Future goToLogsScreen() => (super.noSuchMethod( Invocation.method( @@ -294,16 +304,6 @@ class MockMainNavigator extends _i1.Mock implements _i6.MainNavigator { returnValueForMissingStub: _i4.Future.value(), ) as _i4.Future); - @override - _i4.Future goToTodoAddScreen() => (super.noSuchMethod( - Invocation.method( - #goToTodoAddScreen, - [], - ), - returnValue: _i4.Future.value(), - returnValueForMissingStub: _i4.Future.value(), - ) as _i4.Future); - @override void goBack() => super.noSuchMethod( Invocation.method( diff --git a/tool/hooks/install.sh b/tool/hooks/install.sh new file mode 100755 index 00000000..a6f23b96 --- /dev/null +++ b/tool/hooks/install.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# Activates the shared git hooks in tool/hooks. Run once after cloning. +set -e +cd "$(git rev-parse --show-toplevel)" +chmod +x tool/hooks/pre-push +git config core.hooksPath tool/hooks +echo "Git hooks active: tool/hooks (pre-push checks the PR description)" diff --git a/tool/hooks/pre-push b/tool/hooks/pre-push new file mode 100755 index 00000000..61f8edc7 --- /dev/null +++ b/tool/hooks/pre-push @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Blocks a push of a ticket branch that has no finished .ai/pr-descriptions/.md. +# Checks the pushed commit, not the working tree, so the file must be committed. +# Bypass: PR_DESCRIPTION_SKIP=1 git push, or git push --no-verify. + +[ "${PR_DESCRIPTION_SKIP:-}" = "1" ] && exit 0 + +ZERO=0000000000000000000000000000000000000000 +status=0 + +while read -r local_ref local_sha remote_ref remote_sha; do + case "$local_ref" in refs/heads/*) ;; *) continue ;; esac + [ "$local_sha" = "$ZERO" ] && continue + + branch=${local_ref#refs/heads/} + case "$branch" in master | main | develop | release/*) continue ;; esac + + ticket=$(printf '%s' "$branch" | grep -oE '[A-Z][A-Z0-9]+-[0-9]+' | head -n 1) + if [ -z "$ticket" ]; then + echo "pr-description: no ticket number in branch name '$branch'." + echo " Name the branch e.g. feat/ABC-123 (uppercase)." + status=1 + continue + fi + + path=".ai/pr-descriptions/${ticket}.md" + if ! git cat-file -e "${local_sha}:${path}" 2>/dev/null; then + echo "pr-description: ${path} is missing from the pushed commit of '$branch'." + echo " Generate it with the pr-description skill, complete it and commit it." + status=1 + continue + fi + + # HTML comments (the guidance from the template) are ignored. + body=$(git show "${local_sha}:${path}" | perl -0pe 's///gs') + problems="" + + printf '%s\n' "$body" | grep -q 'TODO' && + problems="${problems} - a TODO is still present\n" + printf '%s\n' "$body" | grep -q 'Yes / No / Partial' && + problems="${problems} - the evidence table in section 3 is not filled in\n" + printf '%s\n' "$body" | grep -Eq '^## 1\. Risk: (S|M|L)([^A-Za-z]|$)' || + problems="${problems} - section 1 has no risk size (S, M or L)\n" + printf '%s\n' "$body" | grep -q '^## 2\. What changed and why' || + problems="${problems} - section 2 \"What changed and why\" is missing\n" + printf '%s\n' "$body" | grep -q '^## 3\. Evidence' || + problems="${problems} - section 3 \"Evidence\" is missing\n" + + if [ -n "$problems" ]; then + echo "pr-description: ${path} is not finished yet:" + printf '%b' "$problems" + status=1 + fi +done + +exit $status