|
| 1 | +// Copyright 2013 The Flutter Authors |
| 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 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:path/path.dart' as p; |
| 10 | + |
| 11 | +void main() { |
| 12 | + group('Evals structure consistency', () { |
| 13 | + test('all evals.json files across skills share consistent expected keys', () { |
| 14 | + final String packageRoot = Directory.current.path; |
| 15 | + |
| 16 | + final List<File> evalsFiles = [ |
| 17 | + ..._findEvalsFiles(Directory(p.join(packageRoot, '.agents', 'skills'))), |
| 18 | + ..._findEvalsFiles(Directory(p.join(packageRoot, 'evals'))), |
| 19 | + ]..sort((a, b) => a.path.compareTo(b.path)); |
| 20 | + |
| 21 | + expect( |
| 22 | + evalsFiles, |
| 23 | + isNotEmpty, |
| 24 | + reason: 'Should find at least one evals.json file in .agents/skills or evals.', |
| 25 | + ); |
| 26 | + |
| 27 | + _verifyStructuralConsistency(evalsFiles, 'evals'); |
| 28 | + }); |
| 29 | + |
| 30 | + test('all rubric JSON files in evals/ share consistent structure and keys', () { |
| 31 | + final String packageRoot = Directory.current.path; |
| 32 | + |
| 33 | + final rubricsDir = Directory(p.join(packageRoot, 'evals')); |
| 34 | + if (!rubricsDir.existsSync()) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + final List<File> rubricFiles = |
| 39 | + rubricsDir |
| 40 | + .listSync() |
| 41 | + .whereType<File>() |
| 42 | + .where((File f) => f.path.endsWith('.json') && !f.path.endsWith('_evals.json')) |
| 43 | + .toList() |
| 44 | + ..sort((a, b) => a.path.compareTo(b.path)); |
| 45 | + |
| 46 | + if (rubricFiles.isEmpty) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + _verifyStructuralConsistency(rubricFiles, 'evals'); |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +void _verifyStructuralConsistency(List<File> files, String itemsKey) { |
| 56 | + Set<String>? expectedRootKeys; |
| 57 | + String? expectedRootKeysFilePath; |
| 58 | + Set<String>? expectedItemKeys; |
| 59 | + String? expectedItemFilePath; |
| 60 | + |
| 61 | + for (final file in files) { |
| 62 | + final Object? decoded = jsonDecode(file.readAsStringSync()); |
| 63 | + final Map<String, dynamic> decodedMap = switch (decoded) { |
| 64 | + final Map<String, dynamic> map => map, |
| 65 | + _ => fail('${file.path} must be a JSON map.'), |
| 66 | + }; |
| 67 | + final Set<String> rootKeys = decodedMap.keys.toSet(); |
| 68 | + if (expectedRootKeys == null) { |
| 69 | + expectedRootKeys = rootKeys; |
| 70 | + expectedRootKeysFilePath = file.path; |
| 71 | + } else { |
| 72 | + expect( |
| 73 | + rootKeys, |
| 74 | + equals(expectedRootKeys), |
| 75 | + reason: |
| 76 | + '${file.path} root keys do not match consistency pattern. ' |
| 77 | + 'Expected keys to match the first processed file ($expectedRootKeysFilePath).', |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + final Object? itemsRaw = decodedMap[itemsKey]; |
| 82 | + final List<dynamic> itemsList = switch (itemsRaw) { |
| 83 | + final List<dynamic> list => list, |
| 84 | + _ => fail('$itemsKey key in ${file.path} must be a List.'), |
| 85 | + }; |
| 86 | + for (final Object? item in itemsList) { |
| 87 | + final Map<String, dynamic> itemMap = switch (item) { |
| 88 | + final Map<String, dynamic> map => map, |
| 89 | + _ => fail('Item in $itemsKey list in ${file.path} must be a JSON map.'), |
| 90 | + }; |
| 91 | + final Set<String> itemKeys = itemMap.keys.toSet(); |
| 92 | + if (expectedItemKeys == null) { |
| 93 | + expectedItemKeys = itemKeys; |
| 94 | + expectedItemFilePath = file.path; |
| 95 | + } else { |
| 96 | + expect( |
| 97 | + itemKeys, |
| 98 | + equals(expectedItemKeys), |
| 99 | + reason: |
| 100 | + 'Item in ${file.path} keys do not match consistency pattern. ' |
| 101 | + 'Expected item keys to match the first processed file ($expectedItemFilePath).', |
| 102 | + ); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +List<File> _findEvalsFiles(Directory baseDir) { |
| 109 | + if (!baseDir.existsSync()) { |
| 110 | + return []; |
| 111 | + } |
| 112 | + return baseDir.listSync(recursive: true).whereType<File>().where((File f) { |
| 113 | + final String name = p.basename(f.path); |
| 114 | + return name == 'evals.json' || name.endsWith('_evals.json'); |
| 115 | + }).toList(); |
| 116 | +} |
0 commit comments