-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.test.ts
320 lines (282 loc) · 8.92 KB
/
server.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { jest } from "@jest/globals";
import express from "express";
import request from "supertest";
import {
A2AServer,
InMemoryTaskStore,
TaskContext,
TaskYieldUpdate,
TaskStore,
configureLogger,
} from "../src/index.js";
// Set a reasonable timeout for all tests
jest.setTimeout(10000);
configureLogger({ level: "silent" });
// Define test task handler
async function* basicTaskHandler(
context: TaskContext
): AsyncGenerator<TaskYieldUpdate, void, unknown> {
// Check if task already has status, if not, use "working"
yield {
state: "working",
message: {
role: "agent",
parts: [{ type: "text", text: "Working on it..." }],
},
};
// Simulate some work
await new Promise((resolve) => setTimeout(resolve, 100));
// Check for cancellation
if (context.isCancelled()) {
yield { state: "canceled" };
return;
}
// Generate a result artifact
yield {
name: "result.txt",
parts: [
{ type: "text", text: `Task ${context.task.id} completed successfully.` },
],
};
// Final completion status
yield {
state: "completed",
message: {
role: "agent",
parts: [{ type: "text", text: "Task completed successfully!" }],
},
};
}
describe("A2AServer", () => {
let server: A2AServer;
let app: express.Express;
let taskStore: TaskStore;
// Track any pending requests for cleanup
let pendingRequests: request.Test[] = [];
beforeEach(() => {
taskStore = new InMemoryTaskStore();
server = new A2AServer({
handler: basicTaskHandler,
taskStore,
port: 0, // Don't actually listen
});
app = server.start();
pendingRequests = [];
});
afterEach(async () => {
// Ensure all pending requests are completed
await Promise.all(
pendingRequests.map((req) => {
try {
return req;
} catch (e) {
// Ignore errors during cleanup
return null;
}
})
);
await server.stop();
// Add a small delay to allow any open connections to close
await new Promise((resolve) => setTimeout(resolve, 100));
});
// Helper function to track supertest requests
const trackRequest = (req: request.Test): request.Test => {
pendingRequests.push(req);
return req;
};
describe("Agent Card", () => {
it("serves agent card at /.well-known/agent.json", async () => {
const response = await trackRequest(
request(app).get("/.well-known/agent.json")
);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty("name");
expect(response.body).toHaveProperty("capabilities");
});
it("serves agent card at /agent-card", async () => {
const response = await trackRequest(request(app).get("/agent-card"));
expect(response.status).toBe(200);
expect(response.body).toHaveProperty("name");
expect(response.body).toHaveProperty("capabilities");
});
});
describe("tasks/send", () => {
it("handles a valid task send request", async () => {
const requestBody = {
jsonrpc: "2.0",
id: "test-request-1",
method: "tasks/send",
params: {
id: "test-task-1",
message: {
role: "user",
parts: [{ type: "text", text: "Hello, world!" }],
},
},
};
const response = await trackRequest(
request(app).post("/").send(requestBody)
);
expect(response.status).toBe(200);
expect(response.body.jsonrpc).toBe("2.0");
expect(response.body.id).toBe("test-request-1");
expect(response.body.result).toBeDefined();
expect(response.body.result.id).toBe("test-task-1");
expect(response.body.result.status.state).toBe("completed");
expect(response.body.result.artifacts).toHaveLength(1);
expect(response.body.result.artifacts[0].name).toBe("result.txt");
});
it("returns an error for invalid request format", async () => {
const invalidRequest = {
// Missing required jsonrpc field
id: "invalid-req",
method: "tasks/send",
params: {
id: "task-id",
message: {
role: "user",
parts: [{ type: "text", text: "Test" }],
},
},
};
const response = await trackRequest(
request(app).post("/").send(invalidRequest)
);
expect(response.status).toBe(200);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32600); // Invalid request error
expect(response.body.error.message).toBe("Invalid request"); //todo expected "Request payload validation error" but may be caused by the jsonrpc middleware
});
it("returns an error for missing task ID", async () => {
const requestWithoutId = {
jsonrpc: "2.0",
id: "missing-id-req",
method: "tasks/send",
params: {
// Missing id field
message: {
role: "user",
parts: [{ type: "text", text: "Test" }],
},
},
};
const response = await trackRequest(
request(app).post("/").send(requestWithoutId)
);
expect(response.status).toBe(200);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32602);
expect(response.body.error.message).toBe("Invalid parameters");
});
});
describe("tasks/get", () => {
it("retrieves a task after it has been created", async () => {
// First create a task
const createRequest = {
jsonrpc: "2.0",
id: "create-req",
method: "tasks/send",
params: {
id: "retrieve-task",
message: {
role: "user",
parts: [{ type: "text", text: "Task to retrieve" }],
},
},
};
await trackRequest(request(app).post("/").send(createRequest));
// Now try to retrieve it
const getRequest = {
jsonrpc: "2.0",
id: "get-req",
method: "tasks/get",
params: {
id: "retrieve-task",
},
};
const response = await trackRequest(
request(app).post("/").send(getRequest)
);
expect(response.status).toBe(200);
expect(response.body.result).toBeDefined();
expect(response.body.result.id).toBe("retrieve-task");
expect(response.body.result.status.state).toBe("completed");
});
it("returns an error for non-existent task", async () => {
const getRequest = {
jsonrpc: "2.0",
id: "nonexistent-req",
method: "tasks/get",
params: {
id: "nonexistent-task",
},
};
const response = await trackRequest(
request(app).post("/").send(getRequest)
);
expect(response.status).toBe(200);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32001);
expect(response.body.error.message).toBe("Task not found");
});
});
describe("tasks/cancel", () => {
it("successfully cancels a task", async () => {
// First create a task
const createRequest = {
jsonrpc: "2.0",
id: "create-cancel-req",
method: "tasks/send",
params: {
id: "cancel-task",
message: {
role: "user",
parts: [{ type: "text", text: "Task to cancel" }],
},
},
};
await trackRequest(request(app).post("/").send(createRequest));
// Now try to cancel it (note: the task may complete before cancellation in this test)
const cancelRequest = {
jsonrpc: "2.0",
id: "cancel-req",
method: "tasks/cancel",
params: {
id: "cancel-task",
},
};
const response = await trackRequest(
request(app).post("/").send(cancelRequest)
);
// It's possible the task completes before we can cancel it,
// in which case we'll get a "task not cancelable" error,
// but that's also a valid test result
if (response.body.error) {
expect(response.body.error.code).toBe(-32002);
expect(response.body.error.message).toBe("Task cannot be canceled");
} else {
expect(response.status).toBe(200);
expect(response.body.result).toBeDefined();
expect(response.body.result.id).toBe("cancel-task");
expect(response.body.result.status.state).toBe("canceled");
}
});
});
describe("Method not found", () => {
it("returns a method not found error for unknown methods", async () => {
const unknownMethodRequest = {
jsonrpc: "2.0",
id: "unknown-method-req",
method: "unknown/method",
params: {},
};
const response = await trackRequest(
request(app).post("/").send(unknownMethodRequest)
);
expect(response.status).toBe(200);
expect(response.body.error).toBeDefined();
expect(response.body.error.code).toBe(-32601);
expect(response.body.error.message).toBe("Method not found");
});
});
});