|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'dart:async'; |
5 | 6 | import 'dart:convert'; |
6 | 7 |
|
7 | 8 | import 'package:a2a_dart/a2a_dart.dart'; |
| 9 | +import 'package:a2a_dart/src/core/push_notification.dart'; |
| 10 | +import 'package:http/http.dart'; |
8 | 11 |
|
9 | | -import 'package:http/http.dart' as http; |
| 12 | +class FakeTransport extends Transport { |
| 13 | + Map<String, Object?> response; |
| 14 | + final Stream<Map<String, Object?>>? stream; |
| 15 | + @override |
| 16 | + final Map<String, String> authHeaders; |
10 | 17 |
|
11 | | -class FakeHttpClient implements http.Client { |
12 | | - final Map<String, Object?> response; |
13 | | - final int statusCode; |
| 18 | + FakeTransport({ |
| 19 | + required this.response, |
| 20 | + this.stream, |
| 21 | + this.authHeaders = const {}, |
| 22 | + }); |
14 | 23 |
|
15 | | - FakeHttpClient(this.response, {this.statusCode = 200}); |
| 24 | + @override |
| 25 | + Future<Map<String, Object?>> send( |
| 26 | + Map<String, Object?> request, { |
| 27 | + String path = '/rpc', |
| 28 | + }) async { |
| 29 | + return response; |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + Stream<Map<String, Object?>> sendStream(Map<String, Object?> request) { |
| 34 | + if (stream == null) { |
| 35 | + throw UnimplementedError(); |
| 36 | + } |
| 37 | + return stream!; |
| 38 | + } |
16 | 39 |
|
17 | 40 | @override |
18 | | - Future<http.Response> get(Uri url, {Map<String, String>? headers}) async { |
19 | | - return http.Response(jsonEncode(response), statusCode); |
| 41 | + void close() {} |
| 42 | + |
| 43 | + @override |
| 44 | + Future<Map<String, Object?>> get( |
| 45 | + String path, { |
| 46 | + Map<String, String>? headers, |
| 47 | + }) async { |
| 48 | + return response; |
20 | 49 | } |
| 50 | +} |
| 51 | + |
| 52 | +class FakeHttpClient implements Client { |
| 53 | + final Map<String, Object?> response; |
| 54 | + final int statusCode; |
| 55 | + |
| 56 | + FakeHttpClient(this.response, {this.statusCode = 200}); |
21 | 57 |
|
22 | 58 | @override |
23 | | - Future<http.Response> post( |
| 59 | + Future<Response> post( |
24 | 60 | Uri url, { |
25 | 61 | Map<String, String>? headers, |
26 | 62 | Object? body, |
27 | 63 | Encoding? encoding, |
28 | 64 | }) async { |
29 | | - return http.Response(jsonEncode(response), statusCode); |
| 65 | + return Response( |
| 66 | + jsonEncode(response), |
| 67 | + statusCode, |
| 68 | + headers: {'content-type': 'application/json'}, |
| 69 | + ); |
30 | 70 | } |
31 | 71 |
|
| 72 | + @override |
| 73 | + Future<Response> get(Uri url, {Map<String, String>? headers}) async { |
| 74 | + return Response( |
| 75 | + jsonEncode(response), |
| 76 | + statusCode, |
| 77 | + headers: {'content-type': 'application/json'}, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @override |
| 82 | + void close() {} |
| 83 | + |
32 | 84 | @override |
33 | 85 | dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
34 | 86 | } |
35 | 87 |
|
36 | | -class FakeTransport implements Transport { |
| 88 | +class FakeTaskManager implements TaskManager { |
| 89 | + Task? taskToReturn; |
| 90 | + final Map<String, Map<String, PushNotificationConfig>> _pushConfigs = {}; |
| 91 | + |
| 92 | + FakeTaskManager({this.taskToReturn}); |
| 93 | + |
| 94 | + void ensureTaskExists(String taskId) { |
| 95 | + if (taskToReturn?.id != taskId) { |
| 96 | + throw const A2AException.taskNotFound(message: 'Task not found'); |
| 97 | + } |
| 98 | + } |
| 99 | + |
37 | 100 | @override |
38 | | - Map<String, String> authHeaders; |
| 101 | + Future<Task> createTask([Message? message]) async { |
| 102 | + return taskToReturn!; |
| 103 | + } |
39 | 104 |
|
40 | | - final Map<String, Object?> response; |
41 | | - final Stream<Map<String, Object?>> stream; |
| 105 | + @override |
| 106 | + Future<void> addEvent(String taskId, Event event) async {} |
42 | 107 |
|
43 | | - FakeTransport({ |
44 | | - required this.response, |
45 | | - Stream<Map<String, Object?>>? stream, |
46 | | - this.authHeaders = const {}, |
47 | | - }) : stream = stream ?? Stream.value(response); |
| 108 | + @override |
| 109 | + Future<void> updateTask(Task task) async {} |
48 | 110 |
|
49 | 111 | @override |
50 | | - Future<Map<String, Object?>> get( |
51 | | - String path, { |
52 | | - Map<String, String> headers = const {}, |
53 | | - }) async { |
54 | | - return response; |
| 112 | + Future<Task?> getTask(String id) async { |
| 113 | + return taskToReturn; |
55 | 114 | } |
56 | 115 |
|
57 | 116 | @override |
58 | | - Future<Map<String, Object?>> send( |
59 | | - Map<String, Object?> request, { |
60 | | - String path = '/rpc', |
61 | | - }) async { |
62 | | - return response; |
| 117 | + Future<Task?> cancelTask(String id) async { |
| 118 | + return taskToReturn; |
63 | 119 | } |
64 | 120 |
|
65 | 121 | @override |
66 | | - Stream<Map<String, Object?>> sendStream(Map<String, Object?> request) { |
67 | | - return stream; |
| 122 | + Stream<Event> resubscribeToTask(String id) { |
| 123 | + return Stream.fromIterable([]); |
68 | 124 | } |
69 | 125 |
|
70 | 126 | @override |
71 | | - void close() {} |
| 127 | + Future<ListTasksResult> listTasks(ListTasksParams params) async { |
| 128 | + return ListTasksResult( |
| 129 | + tasks: [if (taskToReturn != null) taskToReturn!], |
| 130 | + totalSize: taskToReturn == null ? 0 : 1, |
| 131 | + pageSize: 1, |
| 132 | + nextPageToken: '', |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + @override |
| 137 | + Future<void> setPushNotificationConfig( |
| 138 | + String taskId, |
| 139 | + PushNotificationConfig config, |
| 140 | + ) async { |
| 141 | + (_pushConfigs[taskId] ??= {})[config.id!] = config; |
| 142 | + } |
| 143 | + |
| 144 | + @override |
| 145 | + Future<PushNotificationConfig?> getPushNotificationConfig( |
| 146 | + String taskId, |
| 147 | + String configId, |
| 148 | + ) async => _pushConfigs[taskId]?[configId]; |
| 149 | + |
| 150 | + @override |
| 151 | + Future<List<PushNotificationConfig>> listPushNotificationConfigs( |
| 152 | + String taskId, |
| 153 | + ) async => _pushConfigs[taskId]?.values.toList() ?? []; |
| 154 | + |
| 155 | + @override |
| 156 | + Future<void> deletePushNotificationConfig( |
| 157 | + String taskId, |
| 158 | + String configId, |
| 159 | + ) async { |
| 160 | + _pushConfigs[taskId]?.remove(configId); |
| 161 | + } |
72 | 162 | } |
0 commit comments