Skip to content

Commit 4782f9d

Browse files
committed
Use CreateTaskResult for task ID in tests
1 parent bdc5aa4 commit 4782f9d

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

src/client/index.test.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,16 +2013,22 @@ describe('Task-based execution', () => {
20132013

20142014
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);
20152015

2016-
// Create a task
2017-
await client.callTool({ name: 'test-tool', arguments: {} }, CallToolResultSchema, {
2016+
// Create a task using callToolStream to capture the task ID
2017+
let taskId: string | undefined;
2018+
const stream = client.callToolStream({ name: 'test-tool', arguments: {} }, CallToolResultSchema, {
20182019
task: { ttl: 60000 }
20192020
});
20202021

2021-
// Get the task ID from the task list and query task result
2022-
const taskList = await client.listTasks();
2023-
expect(taskList.tasks.length).toBeGreaterThan(0);
2024-
const taskId = taskList.tasks[0].taskId;
2025-
const result = await client.getTaskResult({ taskId }, CallToolResultSchema);
2022+
for await (const message of stream) {
2023+
if (message.type === 'taskCreated') {
2024+
taskId = message.task.taskId;
2025+
}
2026+
}
2027+
2028+
expect(taskId).toBeDefined();
2029+
2030+
// Query task result using the captured task ID
2031+
const result = await client.getTaskResult({ taskId: taskId! }, CallToolResultSchema);
20262032
expect(result.content).toEqual([{ type: 'text', text: 'Result data!' }]);
20272033
});
20282034

src/server/index.test.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,28 +2076,32 @@ describe('Task-based execution', () => {
20762076

20772077
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);
20782078

2079-
// Use callTool to create a task
2080-
await client.callTool({ name: 'test-tool', arguments: {} }, CallToolResultSchema, {
2079+
// Use callToolStream to create a task and capture the task ID
2080+
let taskId: string | undefined;
2081+
const stream = client.callToolStream({ name: 'test-tool', arguments: {} }, CallToolResultSchema, {
20812082
task: {
20822083
ttl: 60000
20832084
}
20842085
});
20852086

2087+
for await (const message of stream) {
2088+
if (message.type === 'taskCreated') {
2089+
taskId = message.task.taskId;
2090+
}
2091+
}
2092+
2093+
expect(taskId).toBeDefined();
2094+
20862095
// Wait for the task to complete
20872096
await new Promise(resolve => setTimeout(resolve, 50));
20882097

2089-
// Get the task ID from the task list since it's generated automatically
2090-
const taskList = await client.listTasks();
2091-
expect(taskList.tasks.length).toBeGreaterThan(0);
2092-
const taskId = taskList.tasks[0].taskId;
2093-
20942098
// Verify we can retrieve the task
2095-
const task = await client.getTask({ taskId });
2099+
const task = await client.getTask({ taskId: taskId! });
20962100
expect(task).toBeDefined();
20972101
expect(task.status).toBe('completed');
20982102

20992103
// Verify we can retrieve the result
2100-
const result = await client.getTaskResult({ taskId }, CallToolResultSchema);
2104+
const result = await client.getTaskResult({ taskId: taskId! }, CallToolResultSchema);
21012105
expect(result.content).toEqual([{ type: 'text', text: 'Tool executed successfully!' }]);
21022106

21032107
// Cleanup
@@ -2299,26 +2303,30 @@ describe('Task-based execution', () => {
22992303

23002304
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);
23012305

2302-
// Call tool WITH task creation
2303-
await client.callTool({ name: 'collect-info', arguments: {} }, CallToolResultSchema, {
2306+
// Call tool WITH task creation using callToolStream to capture task ID
2307+
let taskId: string | undefined;
2308+
const stream = client.callToolStream({ name: 'collect-info', arguments: {} }, CallToolResultSchema, {
23042309
task: {
23052310
ttl: 60000
23062311
}
23072312
});
23082313

2314+
for await (const message of stream) {
2315+
if (message.type === 'taskCreated') {
2316+
taskId = message.task.taskId;
2317+
}
2318+
}
2319+
2320+
expect(taskId).toBeDefined();
2321+
23092322
// Wait for completion
23102323
await new Promise(resolve => setTimeout(resolve, 50));
23112324

23122325
// Verify the nested elicitation request was made (related-task metadata is no longer automatically attached)
23132326
expect(capturedElicitRequest).toBeDefined();
23142327

2315-
// Get the task ID from the task list since it's generated automatically
2316-
const taskList = await client.listTasks();
2317-
expect(taskList.tasks.length).toBeGreaterThan(0);
2318-
const taskId = taskList.tasks[0].taskId;
2319-
23202328
// Verify tool result was correct
2321-
const result = await client.getTaskResult({ taskId }, CallToolResultSchema);
2329+
const result = await client.getTaskResult({ taskId: taskId! }, CallToolResultSchema);
23222330
expect(result.content).toEqual([
23232331
{
23242332
type: 'text',

0 commit comments

Comments
 (0)