|
| 1 | +using ManagedCode.CodexSharpSDK.Extensions.AI.Content; |
| 2 | +using ManagedCode.CodexSharpSDK.Extensions.AI.Internal; |
| 3 | +using ManagedCode.CodexSharpSDK.Models; |
| 4 | +using Microsoft.Extensions.AI; |
| 5 | + |
| 6 | +namespace ManagedCode.CodexSharpSDK.Extensions.AI.Tests; |
| 7 | + |
| 8 | +public class ChatResponseMapperTests |
| 9 | +{ |
| 10 | + [Test] |
| 11 | + public async Task ToChatResponse_BasicResult_MapsCorrectly() |
| 12 | + { |
| 13 | + var result = new RunResult([], "Hello from Codex", new Usage(100, 10, 50)); |
| 14 | + var response = ChatResponseMapper.ToChatResponse(result, "thread-123"); |
| 15 | + |
| 16 | + await Assert.That(response.Text).Contains("Hello from Codex"); |
| 17 | + await Assert.That(response.ConversationId).IsEqualTo("thread-123"); |
| 18 | + await Assert.That(response.Usage).IsNotNull(); |
| 19 | + await Assert.That(response.Usage!.InputTokenCount).IsEqualTo(100); |
| 20 | + await Assert.That(response.Usage!.OutputTokenCount).IsEqualTo(50); |
| 21 | + await Assert.That(response.Usage!.TotalTokenCount).IsEqualTo(150); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public async Task ToChatResponse_NullUsage_NoUsageSet() |
| 26 | + { |
| 27 | + var result = new RunResult([], "Response", null); |
| 28 | + var response = ChatResponseMapper.ToChatResponse(result, null); |
| 29 | + await Assert.That(response.Usage).IsNull(); |
| 30 | + await Assert.That(response.ConversationId).IsNull(); |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public async Task ToChatResponse_WithReasoningItem_MapsToTextReasoningContent() |
| 35 | + { |
| 36 | + var items = new List<ThreadItem> |
| 37 | + { |
| 38 | + new ReasoningItem("r1", "thinking about this..."), |
| 39 | + }; |
| 40 | + var result = new RunResult(items, "Final answer", null); |
| 41 | + var response = ChatResponseMapper.ToChatResponse(result, null); |
| 42 | + |
| 43 | + var contents = response.Messages[0].Contents; |
| 44 | + await Assert.That(contents.OfType<TextReasoningContent>().Count()).IsEqualTo(1); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public async Task ToChatResponse_WithCommandExecution_MapsToCustomContent() |
| 49 | + { |
| 50 | + var items = new List<ThreadItem> |
| 51 | + { |
| 52 | + new CommandExecutionItem("c1", "npm test", "all passed", 0, CommandExecutionStatus.Completed), |
| 53 | + }; |
| 54 | + var result = new RunResult(items, "Done", null); |
| 55 | + var response = ChatResponseMapper.ToChatResponse(result, null); |
| 56 | + |
| 57 | + var cmdContent = response.Messages[0].Contents.OfType<CommandExecutionContent>().Single(); |
| 58 | + await Assert.That(cmdContent.Command).IsEqualTo("npm test"); |
| 59 | + await Assert.That(cmdContent.ExitCode).IsEqualTo(0); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public async Task ToChatResponse_WithFileChange_MapsToCustomContent() |
| 64 | + { |
| 65 | + var items = new List<ThreadItem> |
| 66 | + { |
| 67 | + new FileChangeItem("f1", [new FileUpdateChange("src/app.cs", PatchChangeKind.Update)], PatchApplyStatus.Completed), |
| 68 | + }; |
| 69 | + var result = new RunResult(items, "Fixed", null); |
| 70 | + var response = ChatResponseMapper.ToChatResponse(result, null); |
| 71 | + |
| 72 | + var fileContent = response.Messages[0].Contents.OfType<FileChangeContent>().Single(); |
| 73 | + await Assert.That(fileContent.Changes).Count().IsEqualTo(1); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public async Task ToChatResponse_CachedTokens_IncludedInUsage() |
| 78 | + { |
| 79 | + var result = new RunResult([], "Response", new Usage(100, 50, 25)); |
| 80 | + var response = ChatResponseMapper.ToChatResponse(result, null); |
| 81 | + await Assert.That(response.Usage!.CachedInputTokenCount).IsEqualTo(50); |
| 82 | + } |
| 83 | +} |
0 commit comments