Skip to content

Commit 2d6c0c7

Browse files
committed
Add Server on top of client support.
1 parent 2eab0ac commit 2d6c0c7

File tree

5 files changed

+125
-36
lines changed

5 files changed

+125
-36
lines changed

packages/a2a_dart/test/client/a2a_client_compliance_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:a2a_dart/a2a_dart.dart';
2+
import 'package:a2a_dart/src/core/push_notification.dart';
23
import 'package:test/test.dart';
34

45
import '../fakes.dart';

packages/a2a_dart/test/core/data_models_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:a2a_dart/a2a_dart.dart';
6+
import 'package:a2a_dart/src/core/push_notification.dart';
67
import 'package:test/test.dart';
78

89
void main() {

packages/a2a_dart/test/fakes.dart

Lines changed: 121 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,161 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:convert';
67

78
import 'package:a2a_dart/a2a_dart.dart';
9+
import 'package:a2a_dart/src/core/push_notification.dart';
10+
import 'package:http/http.dart';
811

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;
1017

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+
});
1423

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+
}
1639

1740
@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;
2049
}
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});
2157

2258
@override
23-
Future<http.Response> post(
59+
Future<Response> post(
2460
Uri url, {
2561
Map<String, String>? headers,
2662
Object? body,
2763
Encoding? encoding,
2864
}) async {
29-
return http.Response(jsonEncode(response), statusCode);
65+
return Response(
66+
jsonEncode(response),
67+
statusCode,
68+
headers: {'content-type': 'application/json'},
69+
);
3070
}
3171

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+
3284
@override
3385
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
3486
}
3587

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+
37100
@override
38-
Map<String, String> authHeaders;
101+
Future<Task> createTask([Message? message]) async {
102+
return taskToReturn!;
103+
}
39104

40-
final Map<String, Object?> response;
41-
final Stream<Map<String, Object?>> stream;
105+
@override
106+
Future<void> addEvent(String taskId, Event event) async {}
42107

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 {}
48110

49111
@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;
55114
}
56115

57116
@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;
63119
}
64120

65121
@override
66-
Stream<Map<String, Object?>> sendStream(Map<String, Object?> request) {
67-
return stream;
122+
Stream<Event> resubscribeToTask(String id) {
123+
return Stream.fromIterable([]);
68124
}
69125

70126
@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+
}
72162
}

packages/a2a_dart/test/server/push_config_handlers_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void main() {
4242
getHandler = GetPushConfigHandler(fakeTaskManager);
4343
listHandler = ListPushConfigsHandler(fakeTaskManager);
4444
deleteHandler = DeletePushConfigHandler(fakeTaskManager);
45-
fakeTaskManager.ensureTaskExists(testTask);
45+
fakeTaskManager.ensureTaskExists(testTask.id);
4646
});
4747

4848
group('SetPushConfigHandler', () {

packages/a2a_dart/test/server/resubscribe_handler_test.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:async';
6-
75
import 'package:a2a_dart/a2a_dart.dart';
86
import 'package:test/test.dart';
97

@@ -22,9 +20,8 @@ void main() {
2220
);
2321
taskManager = FakeTaskManager(
2422
taskToReturn: task,
25-
stream: Stream.value({}),
2623
);
27-
await (taskManager as FakeTaskManager).ensureTaskExists(task);
24+
(taskManager as FakeTaskManager).ensureTaskExists(task.id);
2825
handler = ResubscribeHandler(taskManager);
2926

3027
final result = await handler.handle({'id': task.id});

0 commit comments

Comments
 (0)