|
1 | 1 | // Copyright (c) Microsoft. All rights reserved. |
2 | 2 |
|
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Text.Json; |
3 | 6 | using GitHub.Copilot.SDK; |
4 | 7 | using Microsoft.Extensions.AI; |
5 | 8 |
|
@@ -262,4 +265,151 @@ public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithMultipleArg |
262 | 265 | Assert.Equal("10", functionCall.Arguments!["limit"]?.ToString()); |
263 | 266 | Assert.Equal("active=true", functionCall.Arguments!["filter"]?.ToString()); |
264 | 267 | } |
| 268 | + |
| 269 | + [Fact] |
| 270 | + public void ConvertToAgentResponseUpdate_ToolExecutionCompleteEvent_WithSuccessButNullResult_ProducesNullResult() |
| 271 | + { |
| 272 | + // Arrange |
| 273 | + var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false }); |
| 274 | + var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); |
| 275 | + |
| 276 | + var completeEvent = new ToolExecutionCompleteEvent |
| 277 | + { |
| 278 | + Data = new ToolExecutionCompleteData |
| 279 | + { |
| 280 | + ToolCallId = "call-null-result", |
| 281 | + Success = true, |
| 282 | + Result = null |
| 283 | + } |
| 284 | + }; |
| 285 | + |
| 286 | + // Act |
| 287 | + AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(completeEvent); |
| 288 | + |
| 289 | + // Assert |
| 290 | + var content = Assert.Single(result.Contents); |
| 291 | + var functionResult = Assert.IsType<FunctionResultContent>(content); |
| 292 | + Assert.Equal("call-null-result", functionResult.CallId); |
| 293 | + Assert.Null(functionResult.Result); |
| 294 | + } |
| 295 | + |
| 296 | + [Fact] |
| 297 | + public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithDictionaryArguments_ParsesDirectly() |
| 298 | + { |
| 299 | + // Arrange |
| 300 | + var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false }); |
| 301 | + var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); |
| 302 | + |
| 303 | + var args = new Dictionary<string, object?> { ["path"] = "/tmp/file.txt", ["encoding"] = "utf-8" }; |
| 304 | + var startEvent = new ToolExecutionStartEvent |
| 305 | + { |
| 306 | + Data = new ToolExecutionStartData |
| 307 | + { |
| 308 | + ToolCallId = "call-dict", |
| 309 | + ToolName = "readFile", |
| 310 | + Arguments = args |
| 311 | + } |
| 312 | + }; |
| 313 | + |
| 314 | + // Act |
| 315 | + AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent); |
| 316 | + |
| 317 | + // Assert |
| 318 | + var content = Assert.Single(result.Contents); |
| 319 | + var functionCall = Assert.IsType<FunctionCallContent>(content); |
| 320 | + Assert.Equal("call-dict", functionCall.CallId); |
| 321 | + Assert.NotNull(functionCall.Arguments); |
| 322 | + Assert.Equal("/tmp/file.txt", functionCall.Arguments!["path"]?.ToString()); |
| 323 | + Assert.Equal("utf-8", functionCall.Arguments!["encoding"]?.ToString()); |
| 324 | + } |
| 325 | + |
| 326 | + [Fact] |
| 327 | + public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithNonGenericDictionary_ParsesViaEnumeration() |
| 328 | + { |
| 329 | + // Arrange |
| 330 | + var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false }); |
| 331 | + var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); |
| 332 | + |
| 333 | + var args = new Hashtable { ["key1"] = "value1", ["key2"] = 42 }; |
| 334 | + var startEvent = new ToolExecutionStartEvent |
| 335 | + { |
| 336 | + Data = new ToolExecutionStartData |
| 337 | + { |
| 338 | + ToolCallId = "call-hashtable", |
| 339 | + ToolName = "processTool", |
| 340 | + Arguments = args |
| 341 | + } |
| 342 | + }; |
| 343 | + |
| 344 | + // Act |
| 345 | + AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent); |
| 346 | + |
| 347 | + // Assert |
| 348 | + var content = Assert.Single(result.Contents); |
| 349 | + var functionCall = Assert.IsType<FunctionCallContent>(content); |
| 350 | + Assert.Equal("call-hashtable", functionCall.CallId); |
| 351 | + Assert.NotNull(functionCall.Arguments); |
| 352 | + Assert.Equal("value1", functionCall.Arguments!["key1"]?.ToString()); |
| 353 | + Assert.Equal("42", functionCall.Arguments!["key2"]?.ToString()); |
| 354 | + } |
| 355 | + |
| 356 | + [Fact] |
| 357 | + public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithJsonElement_ParsesArguments() |
| 358 | + { |
| 359 | + // Arrange |
| 360 | + var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false }); |
| 361 | + var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); |
| 362 | + |
| 363 | + var jsonDoc = JsonDocument.Parse("{\"host\":\"localhost\",\"port\":8080}"); |
| 364 | + var startEvent = new ToolExecutionStartEvent |
| 365 | + { |
| 366 | + Data = new ToolExecutionStartData |
| 367 | + { |
| 368 | + ToolCallId = "call-json-element", |
| 369 | + ToolName = "connect", |
| 370 | + Arguments = jsonDoc.RootElement |
| 371 | + } |
| 372 | + }; |
| 373 | + |
| 374 | + // Act |
| 375 | + AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent); |
| 376 | + |
| 377 | + // Assert |
| 378 | + var content = Assert.Single(result.Contents); |
| 379 | + var functionCall = Assert.IsType<FunctionCallContent>(content); |
| 380 | + Assert.Equal("call-json-element", functionCall.CallId); |
| 381 | + Assert.NotNull(functionCall.Arguments); |
| 382 | + Assert.Equal("localhost", functionCall.Arguments!["host"]?.ToString()); |
| 383 | + Assert.Equal("8080", functionCall.Arguments!["port"]?.ToString()); |
| 384 | + } |
| 385 | + |
| 386 | + [Theory] |
| 387 | + [InlineData("")] |
| 388 | + [InlineData(" ")] |
| 389 | + [InlineData("\t")] |
| 390 | + public void ConvertToAgentResponseUpdate_ToolExecutionStartEvent_WithEmptyOrWhitespaceArguments_ProducesNullArguments(string emptyArgs) |
| 391 | + { |
| 392 | + // Arrange |
| 393 | + var copilotClient = new CopilotClient(new CopilotClientOptions { AutoStart = false }); |
| 394 | + var agent = new GitHubCopilotAgent(copilotClient, ownsClient: false, tools: null); |
| 395 | + |
| 396 | + var startEvent = new ToolExecutionStartEvent |
| 397 | + { |
| 398 | + Data = new ToolExecutionStartData |
| 399 | + { |
| 400 | + ToolCallId = "call-empty", |
| 401 | + ToolName = "noArgsTool", |
| 402 | + Arguments = emptyArgs |
| 403 | + } |
| 404 | + }; |
| 405 | + |
| 406 | + // Act |
| 407 | + AgentResponseUpdate result = agent.ConvertToAgentResponseUpdate(startEvent); |
| 408 | + |
| 409 | + // Assert |
| 410 | + var content = Assert.Single(result.Contents); |
| 411 | + var functionCall = Assert.IsType<FunctionCallContent>(content); |
| 412 | + Assert.Equal("call-empty", functionCall.CallId); |
| 413 | + Assert.Null(functionCall.Arguments); |
| 414 | + } |
265 | 415 | } |
0 commit comments