diff --git a/lib/main/bindings/network/network_bindings.dart b/lib/main/bindings/network/network_bindings.dart index d997a201d..2567e469e 100644 --- a/lib/main/bindings/network/network_bindings.dart +++ b/lib/main/bindings/network/network_bindings.dart @@ -72,6 +72,7 @@ class NetworkBindings extends Bindings { Get.put(AppAuthWebPlugin()); Get.put(OIDCHttpClient(Get.find())); Get.put(AuthenticationClientBase()); + Get.put(Dio(), tag: 'prompt'); } void _bindingSharing() { @@ -147,6 +148,6 @@ class NetworkBindings extends Bindings { void _bindingServices() { Get.put(DnsLookupManager()); - Get.put(PromptService(Get.find())); + Get.put(PromptService(Get.find(tag: 'prompt'))); } } \ No newline at end of file diff --git a/scribe/lib/scribe/ai/data/service/prompt_service.dart b/scribe/lib/scribe/ai/data/service/prompt_service.dart index a767538d5..91337bf63 100644 --- a/scribe/lib/scribe/ai/data/service/prompt_service.dart +++ b/scribe/lib/scribe/ai/data/service/prompt_service.dart @@ -1,21 +1,21 @@ import 'dart:async'; import 'dart:convert'; -import 'package:core/data/network/dio_client.dart'; import 'package:core/utils/app_logger.dart'; +import 'package:dio/dio.dart'; import 'package:scribe/scribe/ai/data/model/ai_message.dart'; import 'package:scribe/scribe/ai/domain/model/prompt_data.dart'; import 'package:flutter/services.dart' show rootBundle; class PromptService { static const String _defaultAssetPath = 'packages/scribe/assets/prompts.json'; - - final DioClient _dioClient; - + + final Dio _dio; + String? _promptUrl; PromptData? _promptData; Future? _loadingFuture; - - PromptService(this._dioClient); + + PromptService(this._dio); void setPromptUrl(String? url) { if (_promptUrl == url) return; @@ -62,10 +62,11 @@ class PromptService { log('PromptService::_fetchPromptsFromUrl: Fetching from $url'); try { - final data = await _dioClient.get(url); - - final promptsMap = data is String - ? jsonDecode(data) as Map + final response = await _dio.get(url); + final data = response.data; + + final promptsMap = data is String + ? jsonDecode(data) as Map : data as Map; return PromptData.fromJson(promptsMap); diff --git a/scribe/test/scribe/ai/data/service/prompt_service_test.dart b/scribe/test/scribe/ai/data/service/prompt_service_test.dart index dd3700c63..9e236500b 100644 --- a/scribe/test/scribe/ai/data/service/prompt_service_test.dart +++ b/scribe/test/scribe/ai/data/service/prompt_service_test.dart @@ -1,54 +1,38 @@ +import 'dart:typed_data'; + import 'package:flutter_test/flutter_test.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 { +class _ThrowingAdapter implements HttpClientAdapter { @override - Future get(String path, {Map? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onReceiveProgress}) { + Future fetch( + RequestOptions options, + Stream? requestStream, + Future? cancelFuture, + ) async { throw Exception('Not found'); } @override - Future post(String path, {data, Map? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, bool useJMAPHeader = true}) { - throw UnsupportedError('post not implemented'); - } - - @override - Future delete(String path, {data, Map? queryParameters, Options? options, CancelToken? cancelToken}) { - throw UnsupportedError('delete not implemented'); - } - - @override - Future put(String path, {data, Map? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) { - throw UnsupportedError('put not implemented'); - } - - @override - Map getHeaders() { - return {}; - } + void close({bool force = false}) {} } +Dio _throwingDio() => Dio()..httpClientAdapter = _ThrowingAdapter(); + void main() { setUpAll(() { TestWidgetsFlutterBinding.ensureInitialized(); }); group('PromptService', () { - late TestDioClient testDioClient; - - setUp(() { - testDioClient = TestDioClient(); - }); - test('buildPromptByName should build prompt with input text', () async { // Arrange - final service = PromptService(testDioClient); - - // Act - this will use the real prompts.json file since the test client throws + final service = PromptService(_throwingDio()); + + // Act - this will use the real prompts.json file since the adapter throws final messages = await service.buildPromptByName('change-tone-casual', 'Hello, how are you?'); // Assert @@ -60,7 +44,7 @@ void main() { test('buildPromptByName should build prompt with input text and task', () async { // Arrange - final service = PromptService(testDioClient); + final service = PromptService(_throwingDio()); // 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');