|
| 1 | +// Copyright 2020 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 'dart:convert'; |
| 6 | + |
| 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/src/foundation/github_checks_util.dart'; |
| 11 | +import 'package:github/github.dart' as github; |
| 12 | +import 'package:mockito/mockito.dart'; |
| 13 | +import 'package:test/test.dart'; |
| 14 | + |
| 15 | +void main() { |
| 16 | + useTestLoggerPerTest(); |
| 17 | + |
| 18 | + group('GithubChecksUtil', () { |
| 19 | + late GithubChecksUtil githubChecksUtil; |
| 20 | + late MockGitHub mockGitHub; |
| 21 | + late FakeConfig config; |
| 22 | + late github.RepositorySlug slug; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + githubChecksUtil = const GithubChecksUtil(); |
| 26 | + mockGitHub = MockGitHub(); |
| 27 | + config = FakeConfig(githubClient: mockGitHub); |
| 28 | + slug = github.RepositorySlug('flutter', 'flutter'); |
| 29 | + }); |
| 30 | + |
| 31 | + test( |
| 32 | + 'resetCheckRun sends PATCH request with null conclusion and completed_at', |
| 33 | + () async { |
| 34 | + final checkRun = github.CheckRun.fromJson( |
| 35 | + jsonDecode( |
| 36 | + '{"name": "Guard", "id": 1234, "status": "completed", "conclusion": "failure", "started_at": "2020-05-10T02:49:31Z", "head_sha": "the_sha", "check_suite": {"id": 1}}', |
| 37 | + ) |
| 38 | + as Map<String, dynamic>, |
| 39 | + ); |
| 40 | + |
| 41 | + when( |
| 42 | + // ignore: discarded_futures |
| 43 | + mockGitHub.requestJson<Map<String, dynamic>, github.CheckRun>( |
| 44 | + 'PATCH', |
| 45 | + '/repos/flutter/flutter/check-runs/1234', |
| 46 | + statusCode: github.StatusCodes.OK, |
| 47 | + preview: 'application/vnd.github.antiope-preview+json', |
| 48 | + body: anyNamed('body'), |
| 49 | + convert: anyNamed('convert'), |
| 50 | + ), |
| 51 | + ).thenAnswer((_) async { |
| 52 | + return github.CheckRun.fromJson( |
| 53 | + jsonDecode( |
| 54 | + '{"name": "Guard", "id": 1234, "status": "in_progress", "conclusion": null, "started_at": "2020-05-10T02:49:31Z", "head_sha": "the_sha", "check_suite": {"id": 1}}', |
| 55 | + ) |
| 56 | + as Map<String, dynamic>, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + await githubChecksUtil.resetCheckRun( |
| 61 | + config, |
| 62 | + slug, |
| 63 | + checkRun, |
| 64 | + status: github.CheckRunStatus.inProgress, |
| 65 | + detailsUrl: 'https://example.com/details', |
| 66 | + ); |
| 67 | + |
| 68 | + final captured = verify( |
| 69 | + mockGitHub.requestJson<Map<String, dynamic>, github.CheckRun>( |
| 70 | + 'PATCH', |
| 71 | + '/repos/flutter/flutter/check-runs/1234', |
| 72 | + statusCode: github.StatusCodes.OK, |
| 73 | + preview: 'application/vnd.github.antiope-preview+json', |
| 74 | + body: captureAnyNamed('body'), |
| 75 | + convert: anyNamed('convert'), |
| 76 | + ), |
| 77 | + ).captured; |
| 78 | + |
| 79 | + final body = |
| 80 | + jsonDecode(captured.single as String) as Map<String, dynamic>; |
| 81 | + expect(body['status'], 'in_progress'); |
| 82 | + expect(body['details_url'], 'https://example.com/details'); |
| 83 | + expect(body['conclusion'], isNull); |
| 84 | + expect(body['completed_at'], isNull); |
| 85 | + }, |
| 86 | + ); |
| 87 | + }); |
| 88 | +} |
0 commit comments