Introduce TestOption Enum for Enhanced Test Selection Clarity (#5586)

pull/5587/head
hunteraraujo 2023-10-06 16:13:33 -07:00 committed by GitHub
parent 0f2126e6f0
commit b2d53d8d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 25 deletions

View File

@ -0,0 +1,65 @@
/// `TestOption` is an enumeration of the available test options that can be selected in the skill tree view.
///
/// Each value of this enum represents a distinct test option that can be executed.
/// The `description` getter can be used to get the string representation of each test option.
enum TestOption {
/// Represents the option to run a single test.
runSingleTest,
/// Represents the option to run a test suite including the selected node and its ancestors.
runTestSuiteIncludingSelectedNodeAndAncestors,
/// Represents the option to run all tests in a category.
runAllTestsInCategory,
}
/// An extension on the `TestOption` enum to provide a string representation for each test option.
///
/// This extension adds a `description` getter on `TestOption` to easily retrieve the human-readable
/// string associated with each option. This is particularly helpful for UI display purposes.
extension TestOptionExtension on TestOption {
/// Gets the string representation of the test option.
///
/// Returns a human-readable string that describes the test option. This string is intended
/// to be displayed in the UI for user selection.
String get description {
switch (this) {
/// In case of a single test option, return the corresponding string.
case TestOption.runSingleTest:
return 'Run single test';
/// In case of a test suite option that includes selected node and ancestors, return the corresponding string.
case TestOption.runTestSuiteIncludingSelectedNodeAndAncestors:
return 'Run test suite including selected node and ancestors';
/// In case of an option to run all tests in a category, return the corresponding string.
case TestOption.runAllTestsInCategory:
return 'Run all tests in category';
/// In case of an undefined or unknown test option, return a generic unknown string.
/// This case should ideally never be hit if all enum values are handled.
default:
return 'Unknown';
}
}
/// Converts a [description] string to its corresponding [TestOption] enum value.
///
/// This method is helpful for converting string representations of test options
/// received from various sources (like user input or server responses) into
/// their type-safe enum equivalents.
///
/// Returns the matching [TestOption] enum value if found, otherwise returns `null`.
static TestOption? fromDescription(String description) {
switch (description) {
case 'Run single test':
return TestOption.runSingleTest;
case 'Run test suite including selected node and ancestors':
return TestOption.runTestSuiteIncludingSelectedNodeAndAncestors;
case 'Run all tests in category':
return TestOption.runAllTestsInCategory;
default:
return null; // or throw an exception, or provide a default value, as per your requirement
}
}
}

View File

@ -8,6 +8,7 @@ import 'package:auto_gpt_flutter_client/models/skill_tree/skill_tree_edge.dart';
import 'package:auto_gpt_flutter_client/models/skill_tree/skill_tree_node.dart';
import 'package:auto_gpt_flutter_client/models/step.dart';
import 'package:auto_gpt_flutter_client/models/task.dart';
import 'package:auto_gpt_flutter_client/models/test_option.dart';
import 'package:auto_gpt_flutter_client/models/test_suite.dart';
import 'package:auto_gpt_flutter_client/services/benchmark_service.dart';
import 'package:auto_gpt_flutter_client/services/leaderboard_service.dart';
@ -39,8 +40,8 @@ class SkillTreeViewModel extends ChangeNotifier {
// TODO: Potentially move to task queue view model when we create one
List<SkillTreeNode>? _selectedNodeHierarchy;
String _selectedOption = 'Run single test';
String get selectedOption => _selectedOption;
TestOption _selectedOption = TestOption.runSingleTest;
TestOption get selectedOption => _selectedOption;
List<SkillTreeNode> get skillTreeNodes => _skillTreeNodes;
List<SkillTreeEdge> get skillTreeEdges => _skillTreeEdges;
@ -110,21 +111,20 @@ class SkillTreeViewModel extends ChangeNotifier {
notifyListeners();
}
void updateSelectedNodeHierarchyBasedOnOption(String selectedOption) {
void updateSelectedNodeHierarchyBasedOnOption(TestOption selectedOption) {
_selectedOption = selectedOption;
switch (selectedOption) {
// TODO: Turn this into enum
case 'Run single test':
case TestOption.runSingleTest:
_selectedNodeHierarchy = _selectedNode != null ? [_selectedNode!] : [];
break;
case 'Run test suite including selected node and ancestors':
case TestOption.runTestSuiteIncludingSelectedNodeAndAncestors:
if (_selectedNode != null) {
populateSelectedNodeHierarchy(_selectedNode!.id);
}
break;
case 'Run all tests in category':
case TestOption.runAllTestsInCategory:
if (_selectedNode != null) {
_getAllNodesInDepthFirstOrderEnsuringParents();
}

View File

@ -1,4 +1,5 @@
import 'package:auto_gpt_flutter_client/models/benchmark/benchmark_task_status.dart';
import 'package:auto_gpt_flutter_client/models/test_option.dart';
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart';
import 'package:auto_gpt_flutter_client/views/task_queue/leaderboard_submission_button.dart';
@ -98,11 +99,11 @@ class TaskQueueView extends StatelessWidget {
// TestSuiteButton
TestSuiteButton(
isDisabled: viewModel.isBenchmarkRunning,
selectedOption: viewModel.selectedOption,
selectedOptionString: viewModel.selectedOption.description,
onOptionSelected: (selectedOption) {
print('Option Selected: $selectedOption');
viewModel.updateSelectedNodeHierarchyBasedOnOption(
selectedOption);
TestOptionExtension.fromDescription(selectedOption)!);
},
onPlayPressed: (selectedOption) {
print('Starting benchmark with option: $selectedOption');

View File

@ -1,17 +1,18 @@
import 'package:auto_gpt_flutter_client/constants/app_colors.dart';
import 'package:auto_gpt_flutter_client/models/test_option.dart';
import 'package:flutter/material.dart';
class TestSuiteButton extends StatefulWidget {
final bool isDisabled;
final Function(String) onOptionSelected;
final Function(String) onPlayPressed;
String selectedOption;
String selectedOptionString;
TestSuiteButton({
this.isDisabled = false,
required this.onOptionSelected,
required this.onPlayPressed,
required this.selectedOption,
required this.selectedOptionString,
});
@override
@ -30,24 +31,27 @@ class _TestSuiteButtonState extends State<TestSuiteButton> {
enabled: !widget.isDisabled,
onSelected: (value) {
setState(() {
widget.selectedOption = value;
widget.selectedOptionString = value;
});
widget.onOptionSelected(widget.selectedOption);
widget.onOptionSelected(widget.selectedOptionString);
},
itemBuilder: (BuildContext context) {
return [
const PopupMenuItem(
value: 'Run single test',
child: Text('Run single test'),
PopupMenuItem(
value: TestOption.runSingleTest.description,
child: Text(TestOption.runSingleTest.description),
),
const PopupMenuItem(
value: 'Run test suite including selected node and ancestors',
child: Text(
'Run test suite including selected node and ancestors'),
PopupMenuItem(
value: TestOption
.runTestSuiteIncludingSelectedNodeAndAncestors
.description,
child: Text(TestOption
.runTestSuiteIncludingSelectedNodeAndAncestors
.description),
),
const PopupMenuItem(
value: 'Run all tests in category',
child: Text('Run all tests in category'),
PopupMenuItem(
value: TestOption.runAllTestsInCategory.description,
child: Text(TestOption.runAllTestsInCategory.description),
),
];
},
@ -63,7 +67,7 @@ class _TestSuiteButtonState extends State<TestSuiteButton> {
children: [
Flexible(
child: Text(
widget.selectedOption,
widget.selectedOptionString,
style: const TextStyle(
color: Colors.white,
fontSize: 12.50,
@ -100,7 +104,7 @@ class _TestSuiteButtonState extends State<TestSuiteButton> {
onPressed: widget.isDisabled
? null
: () {
widget.onPlayPressed(widget.selectedOption);
widget.onPlayPressed(widget.selectedOptionString);
},
child: const Icon(
Icons.play_arrow,