mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2026-01-07 08:14:25 +01:00
Implement ChatViewModel with tests and mock data
This commit introduces the ChatViewModel, which manages the business logic for chat interactions associated with tasks. The ViewModel communicates with a mock data source, offering functionalities like fetching chats for a specific task and sending chat messages. In addition to the implementation, comprehensive tests for ChatViewModel have been provided to ensure its behavior is consistent with our design goals and expectations. Key Features: Chat management in ChatViewModel. Tests covering all major functionalities of ChatViewModel. Mock data source updates to emulate chat data interactions.
This commit is contained in:
48
test/chat_viewmodel_test.dart
Normal file
48
test/chat_viewmodel_test.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'package:auto_gpt_flutter_client/models/chat.dart';
|
||||
import 'package:auto_gpt_flutter_client/models/message_type.dart';
|
||||
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
// Initialize the ChatViewModel
|
||||
// TODO: Dependency injection in view models for testing purposes when we implement services
|
||||
final viewModel = ChatViewModel();
|
||||
|
||||
group('ChatViewModel', () {
|
||||
test('fetch chats for a specific task', () {
|
||||
viewModel
|
||||
.fetchChatsForTask(1); // Assuming task with ID 1 exists in mock data
|
||||
expect(viewModel.chats.isNotEmpty, true);
|
||||
expect(viewModel.chats.every((chat) => chat.taskId == 1), true);
|
||||
});
|
||||
|
||||
test('send chat message for a specific task', () {
|
||||
final initialChatsLength = viewModel.chats.length;
|
||||
viewModel.sendChatMessage(1, 'Test message');
|
||||
expect(viewModel.chats.length,
|
||||
initialChatsLength + 2); // One user message and one agent reply
|
||||
expect(viewModel.chats.last.messageType,
|
||||
MessageType.agent); // Last message should be agent's reply
|
||||
});
|
||||
|
||||
// TODO: Refactor to return errors when we implement service
|
||||
test('fetch chats for invalid task id', () {
|
||||
viewModel.fetchChatsForTask(
|
||||
9999); // Assuming task with ID 9999 does not exist in mock data
|
||||
expect(
|
||||
viewModel.chats.where((chat) => chat.taskId == 9999).isEmpty, true);
|
||||
});
|
||||
|
||||
// TODO: Refactor to return errors when we implement service
|
||||
test('send chat message for invalid task id', () {
|
||||
final initialChatsLength = viewModel.chats.length;
|
||||
viewModel.sendChatMessage(9999, 'Invalid test message');
|
||||
expect(
|
||||
viewModel.chats.length,
|
||||
initialChatsLength +
|
||||
2); // Even for invalid tasks, we're currently adding mock replies
|
||||
expect(viewModel.chats.last.messageType,
|
||||
MessageType.agent); // Last message should be agent's reply
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart';
|
||||
import 'package:auto_gpt_flutter_client/viewmodels/mock_data.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('TaskViewModel', () {
|
||||
@@ -37,10 +37,6 @@ void main() {
|
||||
expect(hasNotified, true);
|
||||
});
|
||||
|
||||
test('Simulate error happening while fetching a task', () {
|
||||
// TODO: Implement once you have error handling in place in fetchTasks.
|
||||
});
|
||||
|
||||
test('No tasks are fetched', () {
|
||||
// Clear mock data for this test
|
||||
mockTasks.clear();
|
||||
@@ -67,6 +63,7 @@ void main() {
|
||||
});
|
||||
|
||||
test('Deletes a task with invalid id', () {
|
||||
// TODO: Update this test to expect an error once we have TaskService implemented
|
||||
final initialCount = viewModel.tasks.length;
|
||||
viewModel.deleteTask(9999); // Assuming no task with this id exists
|
||||
expect(viewModel.tasks.length, initialCount); // Count remains same
|
||||
|
||||
Reference in New Issue
Block a user