Skip to content

Commit 48cd8a3

Browse files
committed
fix unit test
1 parent 742f80f commit 48cd8a3

File tree

5 files changed

+37
-239
lines changed

5 files changed

+37
-239
lines changed

client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func TestA2AClient_SetPushNotification(t *testing.T) {
206206
TaskID: taskID,
207207
PushNotificationConfig: jsonprotocol.PushNotificationConfig{
208208
URL: "https://example.com/webhook",
209-
Authentication: &jsonprotocol.AuthenticationInfo{
209+
Authentication: &jsonprotocol.PushNotificationAuthenticationInfo{
210210
Schemes: []string{"bearer"},
211211
},
212212
},
@@ -314,7 +314,7 @@ func TestA2AClient_GetPushNotification(t *testing.T) {
314314
TaskID: taskID,
315315
PushNotificationConfig: jsonprotocol.PushNotificationConfig{
316316
URL: "https://example.com/webhook",
317-
Authentication: &jsonprotocol.AuthenticationInfo{
317+
Authentication: &jsonprotocol.PushNotificationAuthenticationInfo{
318318
Schemes: []string{"bearer"},
319319
},
320320
},

protocol/jsonprotocol/types.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -659,25 +659,6 @@ func NewTaskArtifactUpdateEvent(taskID, contextID string, artifact Artifact, las
659659
}
660660
}
661661

662-
// SendTaskParams defines the parameters for the tasks_send and tasks_sendSubscribe RPC methods.
663-
// See A2A Spec section on RPC Methods.
664-
type SendTaskParams struct {
665-
// RPCID is the ID of json-rpc.
666-
RPCID string `json:"-"`
667-
// ID is the ID of the task.
668-
ID string `json:"id"`
669-
// SessionID is the optional session ID.
670-
SessionID *string `json:"sessionId,omitempty"`
671-
// Message is the user's message initiating the task.
672-
Message Message `json:"message"`
673-
// PushNotification contains optional push notification settings.
674-
PushNotification *PushNotificationConfig `json:"pushNotification,omitempty"`
675-
// HistoryLength is the requested history length in response.
676-
HistoryLength *int `json:"historyLength,omitempty"`
677-
// Metadata is the optional metadata.
678-
Metadata map[string]interface{} `json:"metadata,omitempty"`
679-
}
680-
681662
// TaskQueryParams defines the parameters for the tasks_get RPC method.
682663
// See A2A Spec section on RPC Methods.
683664
type TaskQueryParams struct {

server/server.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (s *A2AServer) handleAgentCard(w http.ResponseWriter, r *http.Request) {
180180
}
181181

182182
// handleJSONRPC is the main handler for all JSON-RPC 2.0 requests.
183-
// Routes methods like tasks/send, tasks/get, etc., as defined in A2A Spec.
183+
// Routes methods like message/send, tasks/get, tasks/cancel, etc., as defined in A2A Spec.
184184
func (s *A2AServer) handleJSONRPC(w http.ResponseWriter, r *http.Request) {
185185
// --- CORS Handling ---
186186
if s.corsEnabled {
@@ -555,7 +555,7 @@ func (s *A2AServer) handleTasksResubscribe(ctx context.Context, w http.ResponseW
555555
}
556556

557557
// Use the helper function to handle the SSE stream
558-
handleSSEStream(ctx, s.corsEnabled, w, flusher, eventsChan, request.ID.(string), true)
558+
handleSSEStream(ctx, s.corsEnabled, w, flusher, eventsChan, request.ID, true)
559559
}
560560

561561
// handleMessageSend handles the message_send method.
@@ -613,7 +613,7 @@ func (s *A2AServer) handleMessageStream(ctx context.Context, w http.ResponseWrit
613613
}
614614

615615
// Use the helper function to handle the SSE stream
616-
handleSSEStream(ctx, s.corsEnabled, w, flusher, eventsChan, request.ID.(string), false)
616+
handleSSEStream(ctx, s.corsEnabled, w, flusher, eventsChan, request.ID, false)
617617
}
618618

619619
// handleSSEStream handles an SSE stream for a task, including setup and event forwarding.
@@ -624,7 +624,7 @@ func handleSSEStream(
624624
w http.ResponseWriter,
625625
flusher http.Flusher,
626626
eventsChan <-chan jsonprotocol.StreamingMessageEvent,
627-
rpcID string,
627+
rpcID interface{},
628628
isResubscribe bool,
629629
) {
630630
// Set headers for SSE.
@@ -641,9 +641,9 @@ func handleSSEStream(
641641

642642
// Log appropriate message based on whether this is a new subscription or resubscribe
643643
if isResubscribe {
644-
log.Infof("SSE stream reopened for request ID: %v)", rpcID)
644+
log.Debugf("SSE stream reopened for request ID: %v)", rpcID)
645645
} else {
646-
log.Infof("SSE stream opened for request ID: %v)", rpcID)
646+
log.Debugf("SSE stream opened for request ID: %v)", rpcID)
647647
}
648648

649649
// Use request context to detect client disconnection.
@@ -669,13 +669,13 @@ func handleSSEStream(
669669
flusher.Flush()
670670
case <-clientClosed:
671671
// Client disconnected (request context canceled).
672-
log.Infof("SSE client disconnected for request ID: %s. Closing stream.", rpcID)
672+
log.Debugf("SSE client disconnected for request ID: %s. Closing stream.", rpcID)
673673
return // Exit the handler.
674674
}
675675
}
676676
}
677677

678-
func sendSSEEvent(w http.ResponseWriter, rpcID string, event interface{}) error {
678+
func sendSSEEvent(w http.ResponseWriter, rpcID interface{}, event interface{}) error {
679679
// Determine event type string for SSE.
680680
var eventType string
681681
var actualEvent jsonprotocol.Event

server/server_handlers_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
"github.com/a2aproject/a2a-go/auth"
1515
"github.com/a2aproject/a2a-go/internal/jsonrpc"
16-
jsonrpc1 "github.com/a2aproject/a2a-go/protocol/jsonprotocol"
16+
"github.com/a2aproject/a2a-go/protocol/jsonprotocol"
1717
"github.com/a2aproject/a2a-go/taskmanager"
1818
"github.com/stretchr/testify/assert"
1919
"github.com/stretchr/testify/require"
@@ -126,7 +126,7 @@ func TestA2AServer_HandlerErrors(t *testing.T) {
126126
// Test wrong content type
127127
t.Run("Wrong Content Type", func(t *testing.T) {
128128
reqBody := bytes.NewBufferString(
129-
`{"jsonrpc":"2.0","method":"tasks/send","params":{},"id":"test-id"}`)
129+
`{"jsonrpc":"2.0","method":"tasks/get","params":{},"id":"test-id"}`)
130130
testJSONRPCErrorResponse(t, testServer, http.MethodPost, reqBody, "text/plain",
131131
jsonrpc.CodeInvalidRequest, "Content-Type")
132132
})
@@ -141,7 +141,7 @@ func TestA2AServer_HandlerErrors(t *testing.T) {
141141
// Test wrong JSONRPC version
142142
t.Run("Wrong JSONRPC Version", func(t *testing.T) {
143143
reqBody := bytes.NewBufferString(
144-
`{"jsonrpc":"1.0","method":"tasks/send","params":{},"id":"test-id"}`)
144+
`{"jsonrpc":"1.0","method":"tasks/get","params":{},"id":"test-id"}`)
145145
testJSONRPCErrorResponse(t, testServer, http.MethodPost, reqBody, "application/json",
146146
jsonrpc.CodeInvalidRequest, "jsonrpc field must be '2.0'")
147147
})
@@ -156,10 +156,10 @@ func TestA2AServer_HandlerErrors(t *testing.T) {
156156

157157
// Test invalid parameters
158158
t.Run("Invalid Parameters", func(t *testing.T) {
159-
// Missing required fields in params (Message with no parts)
159+
// Message with empty parts array for message/stream
160160
reqBody := bytes.NewBufferString(
161-
`{"jsonrpc":"2.0","method":"tasks/send",
162-
"params":{"id":"test-task","message":{"role":"user","parts":[]}},"id":"test-id"}`)
161+
`{"jsonrpc":"2.0","method":"message/stream",
162+
"params":{"message":{"role":"user","parts":[],"messageId":"msg-test","kind":"message"}},"id":"test-id"}`)
163163
testJSONRPCErrorResponse(t, testServer, http.MethodPost, reqBody, "application/json",
164164
jsonrpc.CodeInvalidParams, "")
165165
})
@@ -186,15 +186,15 @@ func TestA2AServer_AuthMiddleware(t *testing.T) {
186186

187187
t.Run("Auth Success", func(t *testing.T) {
188188
// Configure mock task manager to succeed
189-
mockTM.GetResponse = &jsonrpc1.Task{
189+
mockTM.GetResponse = &jsonprotocol.Task{
190190
ID: "test-task-auth",
191-
Status: jsonrpc1.TaskStatus{State: jsonrpc1.TaskStateCompleted},
191+
Status: jsonprotocol.TaskStatus{State: jsonprotocol.TaskStateCompleted},
192192
}
193193
mockTM.GetError = nil
194194

195195
// Create valid request with auth header
196-
req, _ := createJSONRPCRequest(t, jsonrpc1.MethodTasksGet,
197-
jsonrpc1.TaskQueryParams{ID: "test-task-auth"}, "req-auth-1")
196+
req, _ := createJSONRPCRequest(t, jsonprotocol.MethodTasksGet,
197+
jsonprotocol.TaskQueryParams{ID: "test-task-auth"}, "req-auth-1")
198198
req.Header.Set("X-API-Key", "test-api-key") // Valid API key
199199

200200
resp := executeRequest(t, testServer, req, testServer.URL+"/")
@@ -208,8 +208,8 @@ func TestA2AServer_AuthMiddleware(t *testing.T) {
208208

209209
t.Run("Auth Failure", func(t *testing.T) {
210210
// Create request with invalid auth
211-
req, _ := createJSONRPCRequest(t, jsonrpc1.MethodTasksGet,
212-
jsonrpc1.TaskQueryParams{ID: "test-task-auth"}, "req-auth-2")
211+
req, _ := createJSONRPCRequest(t, jsonprotocol.MethodTasksGet,
212+
jsonprotocol.TaskQueryParams{ID: "test-task-auth"}, "req-auth-2")
213213
req.Header.Set("X-API-Key", "invalid-key") // Invalid API key
214214

215215
resp := executeRequest(t, testServer, req, testServer.URL+"/")
@@ -220,7 +220,7 @@ func TestA2AServer_AuthMiddleware(t *testing.T) {
220220

221221
// Test that agent card endpoint is still accessible without auth
222222
t.Run("AgentCard_NoAuth", func(t *testing.T) {
223-
req, err := http.NewRequest(http.MethodGet, testServer.URL+jsonrpc1.AgentCardPath, nil)
223+
req, err := http.NewRequest(http.MethodGet, testServer.URL+jsonprotocol.AgentCardPath, nil)
224224
require.NoError(t, err)
225225

226226
resp, err := testServer.Client().Do(req)
@@ -244,44 +244,44 @@ func TestA2AServer_Resubscribe(t *testing.T) {
244244

245245
t.Run("Resubscribe_Success", func(t *testing.T) {
246246
// Configure mock events
247-
workingEvent := jsonrpc1.TaskStatusUpdateEvent{
247+
workingEvent := jsonprotocol.TaskStatusUpdateEvent{
248248
TaskID: "resubscribe-task",
249249
ContextID: "test-context",
250-
Kind: jsonrpc1.KindTaskStatusUpdate,
251-
Status: jsonrpc1.TaskStatus{State: jsonrpc1.TaskStateWorking},
250+
Kind: jsonprotocol.KindTaskStatusUpdate,
251+
Status: jsonprotocol.TaskStatus{State: jsonprotocol.TaskStateWorking},
252252
}
253253
finalPtr := true
254-
completedEvent := jsonrpc1.TaskStatusUpdateEvent{
254+
completedEvent := jsonprotocol.TaskStatusUpdateEvent{
255255
TaskID: "resubscribe-task",
256256
ContextID: "test-context",
257-
Kind: jsonrpc1.KindTaskStatusUpdate,
258-
Status: jsonrpc1.TaskStatus{State: jsonrpc1.TaskStateCompleted},
257+
Kind: jsonprotocol.KindTaskStatusUpdate,
258+
Status: jsonprotocol.TaskStatus{State: jsonprotocol.TaskStateCompleted},
259259
Final: finalPtr,
260260
}
261-
mockTM.SubscribeEvents = []jsonrpc1.StreamingMessageEvent{
261+
mockTM.SubscribeEvents = []jsonprotocol.StreamingMessageEvent{
262262
{Result: &workingEvent},
263263
{Result: &completedEvent},
264264
}
265265
mockTM.SubscribeError = nil
266266

267267
// Add task to mock task manager to ensure it exists
268-
mockTM.tasks["resubscribe-task"] = &jsonrpc1.Task{
268+
mockTM.tasks["resubscribe-task"] = &jsonprotocol.Task{
269269
ID: "resubscribe-task",
270-
Status: jsonrpc1.TaskStatus{
271-
State: jsonrpc1.TaskStateWorking,
270+
Status: jsonprotocol.TaskStatus{
271+
State: jsonprotocol.TaskStateWorking,
272272
Timestamp: getCurrentTimestamp(),
273273
},
274274
}
275275

276276
// Create request - resubscribe expects SSE response
277-
params := jsonrpc1.TaskIDParams{
277+
params := jsonprotocol.TaskIDParams{
278278
ID: "resubscribe-task",
279279
}
280280
paramsBytes, _ := json.Marshal(params)
281281

282282
reqBody := jsonrpc.Request{
283283
Message: jsonrpc.Message{JSONRPC: "2.0", ID: "req-resub-1"},
284-
Method: jsonrpc1.MethodTasksResubscribe,
284+
Method: jsonprotocol.MethodTasksResubscribe,
285285
Params: json.RawMessage(paramsBytes),
286286
}
287287
reqBytes, _ := json.Marshal(reqBody)
@@ -317,14 +317,14 @@ func TestA2AServer_Resubscribe(t *testing.T) {
317317
mockTM.SubscribeError = taskmanager.ErrTaskNotFound("nonexistent-task")
318318

319319
// Create request
320-
params := jsonrpc1.TaskIDParams{
320+
params := jsonprotocol.TaskIDParams{
321321
ID: "nonexistent-task",
322322
}
323323

324324
resp := performJSONRPCRequest(
325325
t,
326326
testServer,
327-
jsonrpc1.MethodTasksResubscribe,
327+
jsonprotocol.MethodTasksResubscribe,
328328
params,
329329
"req-resub-err",
330330
)

0 commit comments

Comments
 (0)