|
1 | 1 | import 'dart:convert'; |
2 | 2 | import 'dart:io'; |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | +import 'package:http/testing.dart'; |
3 | 5 | import 'package:test/test.dart'; |
| 6 | +import '../tool/github.dart'; |
4 | 7 | import '../tool/sync_activity.dart'; |
5 | 8 |
|
6 | 9 | void main() { |
@@ -98,5 +101,157 @@ void main() { |
98 | 101 | final remaining = activityDir.listSync().whereType<File>().length; |
99 | 102 | expect(remaining, equals(52)); |
100 | 103 | }); |
| 104 | + |
| 105 | + test( |
| 106 | + 'pruneOldActivityFiles skips invalid filenames and handles missing dir', |
| 107 | + () { |
| 108 | + final missingDir = Directory('${tempDir.path}/non_existent'); |
| 109 | + expect(pruneOldActivityFiles(missingDir), equals(0)); |
| 110 | + |
| 111 | + final activityDir = Directory('${tempDir.path}/activity')..createSync(); |
| 112 | + File('${activityDir.path}/not-a-week.json').writeAsStringSync('{}'); |
| 113 | + File('${activityDir.path}/README.txt').writeAsStringSync('ignore'); |
| 114 | + expect( |
| 115 | + pruneOldActivityFiles(activityDir, |
| 116 | + referenceDate: DateTime.utc(2026, 8, 3)), |
| 117 | + equals(0)); |
| 118 | + }); |
| 119 | + |
| 120 | + test( |
| 121 | + 'fetchActivityForRange aggregates PRs, deduplicated reviews, and issues while excluding bots and ghost', |
| 122 | + () async { |
| 123 | + final mockClient = MockClient((request) async { |
| 124 | + final body = jsonDecode(request.body) as Map<String, dynamic>; |
| 125 | + final variables = body['variables'] as Map<String, dynamic>; |
| 126 | + final query = variables['query'] as String; |
| 127 | + |
| 128 | + if (query.contains('is:pr')) { |
| 129 | + return http.Response( |
| 130 | + jsonEncode({ |
| 131 | + 'data': { |
| 132 | + 'search': { |
| 133 | + 'pageInfo': {'hasNextPage': false, 'endCursor': null}, |
| 134 | + 'nodes': [ |
| 135 | + { |
| 136 | + 'author': {'login': 'alice'}, |
| 137 | + 'reviews': { |
| 138 | + 'nodes': [ |
| 139 | + // Self-review by author should be ignored |
| 140 | + { |
| 141 | + 'author': {'login': 'alice'} |
| 142 | + }, |
| 143 | + // Duplicate review by bob on the same PR should count once |
| 144 | + { |
| 145 | + 'author': {'login': 'bob'} |
| 146 | + }, |
| 147 | + { |
| 148 | + 'author': {'login': 'bob'} |
| 149 | + }, |
| 150 | + // Maintainer review should be recorded |
| 151 | + { |
| 152 | + 'author': {'login': 'Piinks'} |
| 153 | + }, |
| 154 | + // Bot review should be skipped |
| 155 | + { |
| 156 | + 'author': {'login': 'auto-submit'} |
| 157 | + }, |
| 158 | + ] |
| 159 | + } |
| 160 | + }, |
| 161 | + // PR by maintainer and community review on it should both be recorded |
| 162 | + { |
| 163 | + 'author': {'login': 'Piinks'}, |
| 164 | + 'reviews': { |
| 165 | + 'nodes': [ |
| 166 | + { |
| 167 | + 'author': {'login': 'alice'} |
| 168 | + } |
| 169 | + ] |
| 170 | + } |
| 171 | + }, |
| 172 | + // PR by bot should be skipped |
| 173 | + { |
| 174 | + 'author': {'login': 'engine-flutter-autoroll'}, |
| 175 | + 'reviews': {'nodes': <Map<String, dynamic>>[]} |
| 176 | + }, |
| 177 | + ] |
| 178 | + } |
| 179 | + } |
| 180 | + }), |
| 181 | + 200, |
| 182 | + ); |
| 183 | + } else { |
| 184 | + // Issue search |
| 185 | + return http.Response( |
| 186 | + jsonEncode({ |
| 187 | + 'data': { |
| 188 | + 'search': { |
| 189 | + 'pageInfo': {'hasNextPage': false, 'endCursor': null}, |
| 190 | + 'nodes': [ |
| 191 | + { |
| 192 | + 'author': {'login': 'alice'} |
| 193 | + }, |
| 194 | + { |
| 195 | + 'author': {'login': 'charlie'} |
| 196 | + }, |
| 197 | + // Ghost issue should be skipped |
| 198 | + { |
| 199 | + 'author': {'login': 'ghost'} |
| 200 | + }, |
| 201 | + { |
| 202 | + 'author': {'login': 'Piinks'} |
| 203 | + }, |
| 204 | + ] |
| 205 | + } |
| 206 | + } |
| 207 | + }), |
| 208 | + 200, |
| 209 | + ); |
| 210 | + } |
| 211 | + }); |
| 212 | + |
| 213 | + final client = GitHubClient(token: 'test', httpClient: mockClient); |
| 214 | + final stats = await fetchActivityForRange( |
| 215 | + client, |
| 216 | + since: DateTime.utc(2026, 8, 1), |
| 217 | + until: DateTime.utc(2026, 8, 8), |
| 218 | + ); |
| 219 | + |
| 220 | + expect( |
| 221 | + stats['alice'], |
| 222 | + equals({ |
| 223 | + 'merged_prs': 1, |
| 224 | + 'issues': 1, |
| 225 | + 'reviews': 1, |
| 226 | + 'total': 3, |
| 227 | + })); |
| 228 | + expect( |
| 229 | + stats['bob'], |
| 230 | + equals({ |
| 231 | + 'merged_prs': 0, |
| 232 | + 'issues': 0, |
| 233 | + 'reviews': 1, |
| 234 | + 'total': 1, |
| 235 | + })); |
| 236 | + expect( |
| 237 | + stats['charlie'], |
| 238 | + equals({ |
| 239 | + 'merged_prs': 0, |
| 240 | + 'issues': 1, |
| 241 | + 'reviews': 0, |
| 242 | + 'total': 1, |
| 243 | + })); |
| 244 | + expect( |
| 245 | + stats['Piinks'], |
| 246 | + equals({ |
| 247 | + 'merged_prs': 1, |
| 248 | + 'issues': 1, |
| 249 | + 'reviews': 1, |
| 250 | + 'total': 3, |
| 251 | + })); |
| 252 | + expect(stats.containsKey('auto-submit'), isFalse); |
| 253 | + expect(stats.containsKey('engine-flutter-autoroll'), isFalse); |
| 254 | + expect(stats.containsKey('ghost'), isFalse); |
| 255 | + }); |
101 | 256 | }); |
102 | 257 | } |
0 commit comments