|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { DispatchRequest, RuntimeService } from "@agentdispatch/core"; |
| 3 | +import { AgentDispatchClient } from "../src/index.js"; |
| 4 | + |
| 5 | +const request: DispatchRequest = { |
| 6 | + provider: "aws", |
| 7 | + accountProfile: "dev-aws", |
| 8 | + capability: "agent-runtime", |
| 9 | + taskType: "agent.run", |
| 10 | + target: { mode: "session" }, |
| 11 | + input: { instruction: "run a long task" } |
| 12 | +}; |
| 13 | + |
| 14 | +describe("AgentDispatchClient", () => { |
| 15 | + it("delegates task lifecycle calls to the configured runtime service", async () => { |
| 16 | + const calls: string[] = []; |
| 17 | + const runtime = { |
| 18 | + dispatchTask: async (dispatchRequest: DispatchRequest) => { |
| 19 | + calls.push(`dispatch:${dispatchRequest.provider}`); |
| 20 | + return { |
| 21 | + taskId: "task_sdk", |
| 22 | + status: "provisioning", |
| 23 | + provider: dispatchRequest.provider, |
| 24 | + accountProfile: dispatchRequest.accountProfile, |
| 25 | + capability: dispatchRequest.capability, |
| 26 | + backend: "aws-agentcore", |
| 27 | + poll: { |
| 28 | + statusTool: "get_task_status", |
| 29 | + logsTool: "get_task_logs", |
| 30 | + resultTool: "get_task_result" |
| 31 | + } |
| 32 | + }; |
| 33 | + }, |
| 34 | + getTaskStatus: async (taskId: string) => { |
| 35 | + calls.push(`status:${taskId}`); |
| 36 | + return { id: taskId, status: "running" }; |
| 37 | + }, |
| 38 | + getTaskLogs: async (taskId: string, cursor?: number, limit?: number) => { |
| 39 | + calls.push(`logs:${taskId}:${cursor}:${limit}`); |
| 40 | + return { taskId, cursor, nextCursor: 5, data: "hello" }; |
| 41 | + }, |
| 42 | + getTaskResult: async (taskId: string) => { |
| 43 | + calls.push(`result:${taskId}`); |
| 44 | + return { taskId, status: "succeeded", artifacts: [] }; |
| 45 | + }, |
| 46 | + cancelTask: async (taskId: string) => { |
| 47 | + calls.push(`cancel:${taskId}`); |
| 48 | + return { status: "cancelled" }; |
| 49 | + }, |
| 50 | + listProviders: () => { |
| 51 | + calls.push("providers"); |
| 52 | + return ["aws"]; |
| 53 | + }, |
| 54 | + listCapabilities: (provider?: string) => { |
| 55 | + calls.push(`capabilities:${provider}`); |
| 56 | + return [{ adapter: "aws-agentcore", provider: "aws", capability: "agent-runtime", taskTypes: ["agent.run"], targetModes: ["session"] }]; |
| 57 | + }, |
| 58 | + listAccountProfiles: () => { |
| 59 | + calls.push("accounts"); |
| 60 | + return [{ name: "dev-aws", provider: "aws", credentialSource: "aws-sdk-default" }]; |
| 61 | + } |
| 62 | + } as unknown as RuntimeService; |
| 63 | + |
| 64 | + const client = new AgentDispatchClient(runtime); |
| 65 | + |
| 66 | + await expect(client.dispatchTask(request)).resolves.toMatchObject({ taskId: "task_sdk", backend: "aws-agentcore" }); |
| 67 | + await expect(client.getTaskStatus("task_sdk")).resolves.toMatchObject({ status: "running" }); |
| 68 | + await expect(client.getTaskLogs("task_sdk", 0, 128)).resolves.toMatchObject({ data: "hello" }); |
| 69 | + await expect(client.getTaskResult("task_sdk")).resolves.toMatchObject({ status: "succeeded" }); |
| 70 | + await expect(client.cancelTask("task_sdk")).resolves.toMatchObject({ status: "cancelled" }); |
| 71 | + expect(client.listProviders()).toEqual(["aws"]); |
| 72 | + expect(client.listCapabilities("aws")).toHaveLength(1); |
| 73 | + expect(client.listAccountProfiles()).toHaveLength(1); |
| 74 | + expect(calls).toEqual([ |
| 75 | + "dispatch:aws", |
| 76 | + "status:task_sdk", |
| 77 | + "logs:task_sdk:0:128", |
| 78 | + "result:task_sdk", |
| 79 | + "cancel:task_sdk", |
| 80 | + "providers", |
| 81 | + "capabilities:aws", |
| 82 | + "accounts" |
| 83 | + ]); |
| 84 | + }); |
| 85 | +}); |
0 commit comments