Rework PromptService

- Use dependency injection and Get.find instead of manual singleton pattern
- Move from domain to data layer
- Prevents multiple API calls if the previous request hasn't finished
This commit is contained in:
Théo Poizat
2026-02-26 11:47:00 +01:00
committed by Dat H. Pham
parent 6d8d10cf26
commit 1982f199ac
8 changed files with 182 additions and 155 deletions
@@ -1,28 +1,54 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:scribe/scribe/ai/domain/service/prompt_service.dart';
import 'package:core/data/network/dio_client.dart';
import 'package:dio/dio.dart';
import 'package:scribe/scribe/ai/data/service/prompt_service.dart';
import 'package:scribe/scribe/ai/domain/model/prompt_data.dart';
import 'package:scribe/scribe/ai/data/model/ai_message.dart';
class TestDioClient implements DioClient {
@override
Future<dynamic> get(String path, {Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onReceiveProgress}) {
throw Exception('Not found');
}
@override
Future<dynamic> post(String path, {data, Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, bool useJMAPHeader = true}) {
throw UnsupportedError('post not implemented');
}
@override
Future<dynamic> delete(String path, {data, Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken}) {
throw UnsupportedError('delete not implemented');
}
@override
Future<dynamic> put(String path, {data, Map<String, dynamic>? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) {
throw UnsupportedError('put not implemented');
}
@override
Map<String, dynamic> getHeaders() {
return {};
}
}
void main() {
setUpAll(() {
TestWidgetsFlutterBinding.ensureInitialized();
});
group('PromptService', () {
test('PromptService should be singleton', () {
// Act
final service1 = PromptService();
final service2 = PromptService();
// Assert
expect(service1, same(service2));
late TestDioClient testDioClient;
setUp(() {
testDioClient = TestDioClient();
});
test('buildPromptByName should build prompt with input text', () async {
// Arrange
final service = PromptService();
final service = PromptService(testDioClient);
// Act - this will use the real prompts.json file
// Act - this will use the real prompts.json file since the test client throws
final messages = await service.buildPromptByName('change-tone-casual', 'Hello, how are you?');
// Assert
@@ -34,9 +60,9 @@ void main() {
test('buildPromptByName should build prompt with input text and task', () async {
// Arrange
final service = PromptService();
final service = PromptService(testDioClient);
// Act - this will use the real prompts.json file
// Act - this will use the real prompts.json file since the test client throws
final messages = await service.buildPromptByName('custom-prompt-mail', 'Hello, how are you?', task: 'Make it more casual');
// Assert