|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/a2aproject/a2a-go/a2a" |
| 13 | + "github.com/a2aproject/a2a-go/a2asrv" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + a2aserver "github.com/docker/cagent/pkg/a2a" |
| 18 | + "github.com/docker/cagent/pkg/cli" |
| 19 | + "github.com/docker/cagent/pkg/config" |
| 20 | +) |
| 21 | + |
| 22 | +type Response struct { |
| 23 | + Jsonrpc string `json:"jsonrpc"` |
| 24 | + ID string `json:"id"` |
| 25 | + Result *Result `json:"result,omitempty"` |
| 26 | + Error any `json:"error,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +type Result struct { |
| 30 | + Artifacts []Artifact `json:"artifacts"` |
| 31 | +} |
| 32 | + |
| 33 | +type Artifact struct { |
| 34 | + Parts []Part `json:"parts"` |
| 35 | +} |
| 36 | + |
| 37 | +type Part struct { |
| 38 | + Kind string `json:"kind"` |
| 39 | + Text string `json:"text"` |
| 40 | +} |
| 41 | + |
| 42 | +func TestA2AServer_AgentCard(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + _, runtimeConfig := startRecordingAIProxy(t) |
| 46 | + agentCard := startA2AServer(t, "testdata/basic.yaml", runtimeConfig) |
| 47 | + |
| 48 | + assert.Equal(t, "root", agentCard.Name) |
| 49 | + assert.NotEmpty(t, agentCard.Description) |
| 50 | + assert.Equal(t, a2a.TransportProtocolJSONRPC, agentCard.PreferredTransport) |
| 51 | + assert.Contains(t, agentCard.URL, "/invoke") |
| 52 | + assert.True(t, agentCard.Capabilities.Streaming) |
| 53 | + assert.NotEmpty(t, agentCard.Version) |
| 54 | +} |
| 55 | + |
| 56 | +func TestA2AServer_Invoke(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + |
| 59 | + _, runtimeConfig := startRecordingAIProxy(t) |
| 60 | + agentCard := startA2AServer(t, "testdata/basic.yaml", runtimeConfig) |
| 61 | + |
| 62 | + requestID := "test-request-1" |
| 63 | + jsonRPCRequest := map[string]any{ |
| 64 | + "jsonrpc": "2.0", |
| 65 | + "id": requestID, |
| 66 | + "method": "message/send", |
| 67 | + "params": map[string]any{ |
| 68 | + "message": map[string]any{ |
| 69 | + "role": "user", |
| 70 | + "parts": []map[string]any{ |
| 71 | + { |
| 72 | + "kind": "text", |
| 73 | + "text": "What is 2+2? Answer with just the number.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + requestBody, err := json.Marshal(jsonRPCRequest) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, agentCard.URL, bytes.NewReader(requestBody)) |
| 84 | + require.NoError(t, err) |
| 85 | + req.Header.Set("Content-Type", "application/json") |
| 86 | + |
| 87 | + resp, err := http.DefaultClient.Do(req) |
| 88 | + require.NoError(t, err) |
| 89 | + defer resp.Body.Close() |
| 90 | + |
| 91 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 92 | + |
| 93 | + responseBody, err := io.ReadAll(resp.Body) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + var jsonRPCResponse Response |
| 97 | + err = json.Unmarshal(responseBody, &jsonRPCResponse) |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + assert.Equal(t, "2.0", jsonRPCResponse.Jsonrpc) |
| 101 | + assert.Equal(t, requestID, jsonRPCResponse.ID) |
| 102 | + assert.Nil(t, jsonRPCResponse.Error) |
| 103 | + require.NotNil(t, jsonRPCResponse.Result) |
| 104 | + assert.Len(t, jsonRPCResponse.Result.Artifacts, 1) |
| 105 | + assert.Len(t, jsonRPCResponse.Result.Artifacts[0].Parts, 2) |
| 106 | + assert.Equal(t, "text", jsonRPCResponse.Result.Artifacts[0].Parts[0].Kind) |
| 107 | + assert.Equal(t, "4", jsonRPCResponse.Result.Artifacts[0].Parts[0].Text) |
| 108 | + assert.Equal(t, "text", jsonRPCResponse.Result.Artifacts[0].Parts[1].Kind) |
| 109 | + assert.Equal(t, "4", jsonRPCResponse.Result.Artifacts[0].Parts[1].Text) |
| 110 | +} |
| 111 | + |
| 112 | +func TestA2AServer_MultipleRequests(t *testing.T) { |
| 113 | + t.Parallel() |
| 114 | + |
| 115 | + _, runtimeConfig := startRecordingAIProxy(t) |
| 116 | + agentCard := startA2AServer(t, "testdata/basic.yaml", runtimeConfig) |
| 117 | + |
| 118 | + messages := []string{ |
| 119 | + "Say 'hello' in one word.", |
| 120 | + "Say 'goodbye' in one word.", |
| 121 | + } |
| 122 | + |
| 123 | + for i, message := range messages { |
| 124 | + t.Run(fmt.Sprintf("request_%d", i), func(t *testing.T) { |
| 125 | + requestID := fmt.Sprintf("test-request-%d", i) |
| 126 | + jsonRPCRequest := map[string]any{ |
| 127 | + "jsonrpc": "2.0", |
| 128 | + "id": requestID, |
| 129 | + "method": "message/send", |
| 130 | + "params": map[string]any{ |
| 131 | + "message": map[string]any{ |
| 132 | + "role": "user", |
| 133 | + "parts": []map[string]any{ |
| 134 | + { |
| 135 | + "kind": "text", |
| 136 | + "text": message, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + requestBody, err := json.Marshal(jsonRPCRequest) |
| 144 | + require.NoError(t, err) |
| 145 | + |
| 146 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, agentCard.URL, bytes.NewReader(requestBody)) |
| 147 | + require.NoError(t, err) |
| 148 | + req.Header.Set("Content-Type", "application/json") |
| 149 | + |
| 150 | + resp, err := http.DefaultClient.Do(req) |
| 151 | + require.NoError(t, err) |
| 152 | + defer resp.Body.Close() |
| 153 | + |
| 154 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 155 | + |
| 156 | + responseBody, err := io.ReadAll(resp.Body) |
| 157 | + require.NoError(t, err) |
| 158 | + |
| 159 | + var jsonRPCResponse Response |
| 160 | + err = json.Unmarshal(responseBody, &jsonRPCResponse) |
| 161 | + require.NoError(t, err) |
| 162 | + |
| 163 | + assert.Equal(t, requestID, jsonRPCResponse.ID) |
| 164 | + assert.Nil(t, jsonRPCResponse.Error) |
| 165 | + assert.NotNil(t, jsonRPCResponse.Result) |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func TestA2AServer_MultiAgent(t *testing.T) { |
| 171 | + t.Parallel() |
| 172 | + |
| 173 | + _, runtimeConfig := startRecordingAIProxy(t) |
| 174 | + agentCard := startA2AServer(t, "testdata/multi.yaml", runtimeConfig) |
| 175 | + |
| 176 | + requestID := "test-multi-1" |
| 177 | + jsonRPCRequest := map[string]any{ |
| 178 | + "jsonrpc": "2.0", |
| 179 | + "id": requestID, |
| 180 | + "method": "message/send", |
| 181 | + "params": map[string]any{ |
| 182 | + "message": map[string]any{ |
| 183 | + "role": "user", |
| 184 | + "parts": []map[string]any{ |
| 185 | + { |
| 186 | + "kind": "text", |
| 187 | + "text": "Say hello.", |
| 188 | + }, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + |
| 194 | + requestBody, err := json.Marshal(jsonRPCRequest) |
| 195 | + require.NoError(t, err) |
| 196 | + |
| 197 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, agentCard.URL, bytes.NewReader(requestBody)) |
| 198 | + require.NoError(t, err) |
| 199 | + req.Header.Set("Content-Type", "application/json") |
| 200 | + |
| 201 | + resp, err := http.DefaultClient.Do(req) |
| 202 | + require.NoError(t, err) |
| 203 | + defer resp.Body.Close() |
| 204 | + |
| 205 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 206 | + |
| 207 | + responseBody, err := io.ReadAll(resp.Body) |
| 208 | + require.NoError(t, err) |
| 209 | + |
| 210 | + var jsonRPCResponse Response |
| 211 | + err = json.Unmarshal(responseBody, &jsonRPCResponse) |
| 212 | + require.NoError(t, err) |
| 213 | + |
| 214 | + assert.Equal(t, requestID, jsonRPCResponse.ID) |
| 215 | + assert.Nil(t, jsonRPCResponse.Error) |
| 216 | + assert.NotNil(t, jsonRPCResponse.Result) |
| 217 | + |
| 218 | + t.Logf("Multi-agent response: %s", string(responseBody)) |
| 219 | +} |
| 220 | + |
| 221 | +func startA2AServer(t *testing.T, agentFile string, runConfig config.RuntimeConfig) a2a.AgentCard { |
| 222 | + t.Helper() |
| 223 | + |
| 224 | + var lc net.ListenConfig |
| 225 | + ln, err := lc.Listen(t.Context(), "tcp", ":0") |
| 226 | + |
| 227 | + require.NoError(t, err) |
| 228 | + |
| 229 | + go func() { |
| 230 | + out := cli.NewPrinter(io.Discard) |
| 231 | + _ = a2aserver.Start(t.Context(), out, agentFile, "root", runConfig, ln) |
| 232 | + }() |
| 233 | + |
| 234 | + port := ln.Addr().(*net.TCPAddr).Port |
| 235 | + serverURL := fmt.Sprintf("http://localhost:%d", port) |
| 236 | + |
| 237 | + resp, err := http.Get(serverURL + a2asrv.WellKnownAgentCardPath) |
| 238 | + require.NoError(t, err) |
| 239 | + defer resp.Body.Close() |
| 240 | + |
| 241 | + require.Equal(t, http.StatusOK, resp.StatusCode) |
| 242 | + |
| 243 | + var agentCard a2a.AgentCard |
| 244 | + err = json.NewDecoder(resp.Body).Decode(&agentCard) |
| 245 | + require.NoError(t, err) |
| 246 | + |
| 247 | + return agentCard |
| 248 | +} |
0 commit comments