|
| 1 | +// Copyright 2026 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:buildbucket/buildbucket_pb.dart' as bbv2; |
| 6 | +import 'package:cocoon_common/task_status.dart'; |
| 7 | +import 'package:cocoon_integration_test/testing.dart'; |
| 8 | +import 'package:cocoon_server_test/mocks.dart'; |
| 9 | +import 'package:cocoon_server_test/test_logging.dart'; |
| 10 | +import 'package:cocoon_service/cocoon_service.dart'; |
| 11 | +import 'package:cocoon_service/src/model/commit_ref.dart'; |
| 12 | +import 'package:cocoon_service/src/model/common/presubmit_completed_check.dart'; |
| 13 | +import 'package:cocoon_service/src/service/luci_build_service/user_data.dart'; |
| 14 | +import 'package:fixnum/fixnum.dart'; |
| 15 | +import 'package:github/github.dart' as github; |
| 16 | +import 'package:mockito/mockito.dart'; |
| 17 | +import 'package:test/test.dart'; |
| 18 | + |
| 19 | +import '../src/request_handling/subscription_tester.dart'; |
| 20 | + |
| 21 | +void main() { |
| 22 | + useTestLoggerPerTest(); |
| 23 | + |
| 24 | + late PresubmitLuciSubscription handler; |
| 25 | + late FakeConfig config; |
| 26 | + late MockGitHub mockGitHubClient; |
| 27 | + late FakeHttpRequest request; |
| 28 | + late SubscriptionTester tester; |
| 29 | + late MockRepositoriesService mockRepositoriesService; |
| 30 | + late MockGithubChecksService mockGithubChecksService; |
| 31 | + late FakeCiYamlFetcher ciYamlFetcher; |
| 32 | + late MockScheduler mockScheduler; |
| 33 | + late FakeFirestoreService firestore; |
| 34 | + |
| 35 | + setUp(() async { |
| 36 | + firestore = FakeFirestoreService(); |
| 37 | + |
| 38 | + config = FakeConfig( |
| 39 | + dynamicConfig: DynamicConfig.fromJson({ |
| 40 | + 'closeMqGuardAfterPresubmit': true, |
| 41 | + }), |
| 42 | + ); |
| 43 | + mockGithubChecksService = MockGithubChecksService(); |
| 44 | + mockScheduler = MockScheduler(); |
| 45 | + |
| 46 | + ciYamlFetcher = FakeCiYamlFetcher( |
| 47 | + ciYaml: examplePresubmitRescheduleFusionConfig, |
| 48 | + ); |
| 49 | + |
| 50 | + handler = PresubmitLuciSubscription( |
| 51 | + cache: CacheService(inMemory: true), |
| 52 | + config: config, |
| 53 | + luciBuildService: FakeLuciBuildService( |
| 54 | + config: config, |
| 55 | + firestore: firestore, |
| 56 | + ), |
| 57 | + githubChecksService: mockGithubChecksService, |
| 58 | + authProvider: FakeDashboardAuthentication(), |
| 59 | + scheduler: mockScheduler, |
| 60 | + ciYamlFetcher: ciYamlFetcher, |
| 61 | + firestore: firestore, |
| 62 | + ); |
| 63 | + request = FakeHttpRequest(); |
| 64 | + |
| 65 | + tester = SubscriptionTester(request: request); |
| 66 | + |
| 67 | + mockGitHubClient = MockGitHub(); |
| 68 | + mockRepositoriesService = MockRepositoriesService(); |
| 69 | + when(mockGitHubClient.repositories).thenReturn(mockRepositoriesService); |
| 70 | + config.githubClient = mockGitHubClient; |
| 71 | + }); |
| 72 | + |
| 73 | + test( |
| 74 | + 'Requests when task failed and is suppressed reports neutral to scheduler', |
| 75 | + () async { |
| 76 | + final userData = PresubmitUserData( |
| 77 | + commit: CommitRef( |
| 78 | + sha: 'abc', |
| 79 | + branch: 'master', |
| 80 | + slug: github.RepositorySlug('flutter', 'flutter'), |
| 81 | + ), |
| 82 | + checkRunId: 1, |
| 83 | + checkSuiteId: 2, |
| 84 | + ); |
| 85 | + |
| 86 | + // Setup Firestore to mark the test as suppressed |
| 87 | + firestore.putDocument( |
| 88 | + SuppressedTest( |
| 89 | + name: 'Linux A', |
| 90 | + repository: 'flutter/flutter', |
| 91 | + issueLink: 'https://github.com/flutter/flutter/issues/123', |
| 92 | + isSuppressed: true, |
| 93 | + createTimestamp: DateTime.now(), |
| 94 | + ) |
| 95 | + ..name = firestore.resolveDocumentName( |
| 96 | + SuppressedTest.kCollectionId, |
| 97 | + 'suppressed_1', |
| 98 | + ), |
| 99 | + ); |
| 100 | + |
| 101 | + when( |
| 102 | + mockGithubChecksService.updateCheckStatus( |
| 103 | + build: anyNamed('build'), |
| 104 | + checkRunId: anyNamed('checkRunId'), |
| 105 | + luciBuildService: anyNamed('luciBuildService'), |
| 106 | + slug: anyNamed('slug'), |
| 107 | + conclusionOverride: github.CheckRunConclusion.neutral, |
| 108 | + summaryPrepend: anyNamed('summaryPrepend'), |
| 109 | + ), |
| 110 | + ).thenAnswer((_) async => true); |
| 111 | + |
| 112 | + when( |
| 113 | + mockScheduler.processCheckRunCompleted(any), |
| 114 | + ).thenAnswer((_) async => true); |
| 115 | + |
| 116 | + tester.message = createPushMessage( |
| 117 | + Int64(1), |
| 118 | + status: bbv2.Status.FAILURE, |
| 119 | + builder: 'Linux A', |
| 120 | + userData: userData, |
| 121 | + ); |
| 122 | + |
| 123 | + await tester.post(handler); |
| 124 | + |
| 125 | + // Verify that updateCheckStatus was called with neutral override |
| 126 | + verify( |
| 127 | + mockGithubChecksService.updateCheckStatus( |
| 128 | + build: anyNamed('build'), |
| 129 | + checkRunId: 1, |
| 130 | + luciBuildService: anyNamed('luciBuildService'), |
| 131 | + slug: github.RepositorySlug('flutter', 'flutter'), |
| 132 | + conclusionOverride: github.CheckRunConclusion.neutral, |
| 133 | + summaryPrepend: argThat( |
| 134 | + contains( |
| 135 | + '### ⚠️ Test failed but marked as suppressed on dashboard', |
| 136 | + ), |
| 137 | + named: 'summaryPrepend', |
| 138 | + ), |
| 139 | + ), |
| 140 | + ).called(1); |
| 141 | + |
| 142 | + // Verify that processCheckRunCompleted was called with TaskStatus.neutral |
| 143 | + final captured = verify( |
| 144 | + mockScheduler.processCheckRunCompleted(captureAny), |
| 145 | + ).captured; |
| 146 | + expect(captured, hasLength(1)); |
| 147 | + expect( |
| 148 | + captured[0], |
| 149 | + isA<PresubmitCompletedCheck>().having( |
| 150 | + (e) => e.status, |
| 151 | + 'status', |
| 152 | + TaskStatus.neutral, |
| 153 | + ), |
| 154 | + ); |
| 155 | + }, |
| 156 | + ); |
| 157 | + |
| 158 | + test( |
| 159 | + 'Requests when task failed and is NOT suppressed reports failure to scheduler', |
| 160 | + () async { |
| 161 | + final userData = PresubmitUserData( |
| 162 | + commit: CommitRef( |
| 163 | + sha: 'abc', |
| 164 | + branch: 'master', |
| 165 | + slug: github.RepositorySlug('flutter', 'flutter'), |
| 166 | + ), |
| 167 | + checkRunId: 1, |
| 168 | + checkSuiteId: 2, |
| 169 | + ); |
| 170 | + |
| 171 | + // Suppression is NOT set up in Firestore |
| 172 | + |
| 173 | + when( |
| 174 | + mockGithubChecksService.updateCheckStatus( |
| 175 | + build: anyNamed('build'), |
| 176 | + checkRunId: anyNamed('checkRunId'), |
| 177 | + luciBuildService: anyNamed('luciBuildService'), |
| 178 | + slug: anyNamed('slug'), |
| 179 | + conclusionOverride: null, |
| 180 | + summaryPrepend: null, |
| 181 | + ), |
| 182 | + ).thenAnswer((_) async => true); |
| 183 | + |
| 184 | + when( |
| 185 | + mockScheduler.processCheckRunCompleted(any), |
| 186 | + ).thenAnswer((_) async => true); |
| 187 | + |
| 188 | + tester.message = createPushMessage( |
| 189 | + Int64(1), |
| 190 | + status: bbv2.Status.FAILURE, |
| 191 | + builder: 'Linux A', |
| 192 | + userData: userData, |
| 193 | + ); |
| 194 | + |
| 195 | + await tester.post(handler); |
| 196 | + |
| 197 | + // Verify that updateCheckStatus was called without neutral override |
| 198 | + verify( |
| 199 | + mockGithubChecksService.updateCheckStatus( |
| 200 | + build: anyNamed('build'), |
| 201 | + checkRunId: 1, |
| 202 | + luciBuildService: anyNamed('luciBuildService'), |
| 203 | + slug: github.RepositorySlug('flutter', 'flutter'), |
| 204 | + conclusionOverride: null, |
| 205 | + summaryPrepend: null, |
| 206 | + ), |
| 207 | + ).called(1); |
| 208 | + |
| 209 | + // Verify that processCheckRunCompleted was called with TaskStatus.failed |
| 210 | + final captured = verify( |
| 211 | + mockScheduler.processCheckRunCompleted(captureAny), |
| 212 | + ).captured; |
| 213 | + expect(captured, hasLength(1)); |
| 214 | + expect( |
| 215 | + captured[0], |
| 216 | + isA<PresubmitCompletedCheck>().having( |
| 217 | + (e) => e.status, |
| 218 | + 'status', |
| 219 | + TaskStatus.failed, |
| 220 | + ), |
| 221 | + ); |
| 222 | + }, |
| 223 | + ); |
| 224 | +} |
0 commit comments