|
| 1 | +using ManagedCode.CodexSharpSDK.Extensions.AgentFramework.Extensions; |
| 2 | +using Microsoft.Agents.AI; |
| 3 | +using Microsoft.Extensions.AI; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | + |
| 6 | +namespace ManagedCode.CodexSharpSDK.Tests.AgentFramework; |
| 7 | + |
| 8 | +public class CodexAgentServiceCollectionExtensionsTests |
| 9 | +{ |
| 10 | + private const string AgentDescription = "Agent description"; |
| 11 | + private const string AgentInstructions = "You are a coding assistant."; |
| 12 | + private const string AgentName = "codex-agent"; |
| 13 | + private const string ConfiguredDefaultModel = "configured-default-model"; |
| 14 | + private const string KeyedServiceName = "codex-agent"; |
| 15 | + |
| 16 | + [Test] |
| 17 | + public async Task AddCodexAIAgent_ThrowsForNullServices() |
| 18 | + { |
| 19 | + ServiceCollection? services = null; |
| 20 | + |
| 21 | + var action = () => services!.AddCodexAIAgent(); |
| 22 | + |
| 23 | + var exception = await Assert.That(action).ThrowsException(); |
| 24 | + await Assert.That(exception).IsTypeOf<ArgumentNullException>(); |
| 25 | + await Assert.That(((ArgumentNullException)exception!).ParamName).IsEqualTo("services"); |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + public async Task AddCodexAIAgent_RegistersAIAgentAndChatClient() |
| 30 | + { |
| 31 | + var services = new ServiceCollection(); |
| 32 | + services.AddCodexAIAgent(); |
| 33 | + |
| 34 | + var provider = services.BuildServiceProvider(); |
| 35 | + var agent = provider.GetService<AIAgent>(); |
| 36 | + var chatClient = provider.GetService<IChatClient>(); |
| 37 | + var resolvedAgentChatClient = agent?.GetService(typeof(IChatClient)) as IChatClient; |
| 38 | + var metadata = resolvedAgentChatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata; |
| 39 | + |
| 40 | + await Assert.That(agent).IsNotNull(); |
| 41 | + await Assert.That(chatClient).IsNotNull(); |
| 42 | + await Assert.That(resolvedAgentChatClient).IsNotNull(); |
| 43 | + await Assert.That(metadata).IsNotNull(); |
| 44 | + await Assert.That(metadata!.ProviderName).IsEqualTo("CodexCLI"); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public async Task AddCodexAIAgent_WithConfiguration_AppliesAgentOptions() |
| 49 | + { |
| 50 | + var services = new ServiceCollection(); |
| 51 | + services.AddCodexAIAgent( |
| 52 | + configureChatClient: options => options.DefaultModel = ConfiguredDefaultModel, |
| 53 | + configureAgent: options => |
| 54 | + { |
| 55 | + options.Name = AgentName; |
| 56 | + options.Description = AgentDescription; |
| 57 | + options.ChatOptions = new ChatOptions |
| 58 | + { |
| 59 | + Instructions = AgentInstructions, |
| 60 | + }; |
| 61 | + }); |
| 62 | + |
| 63 | + var provider = services.BuildServiceProvider(); |
| 64 | + var agent = provider.GetRequiredService<AIAgent>(); |
| 65 | + var chatClient = agent.GetService<IChatClient>(); |
| 66 | + var metadata = chatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata; |
| 67 | + var agentOptions = agent.GetService<ChatClientAgentOptions>(); |
| 68 | + |
| 69 | + await Assert.That(agent.Name).IsEqualTo(AgentName); |
| 70 | + await Assert.That(agent.Description).IsEqualTo(AgentDescription); |
| 71 | + await Assert.That(agentOptions).IsNotNull(); |
| 72 | + await Assert.That(agentOptions!.ChatOptions).IsNotNull(); |
| 73 | + await Assert.That(agentOptions.ChatOptions!.Instructions).IsEqualTo(AgentInstructions); |
| 74 | + await Assert.That(metadata).IsNotNull(); |
| 75 | + await Assert.That(metadata!.DefaultModelId).IsEqualTo(ConfiguredDefaultModel); |
| 76 | + } |
| 77 | + |
| 78 | + [Test] |
| 79 | + public async Task AddKeyedCodexAIAgent_RegistersKeyedAgent() |
| 80 | + { |
| 81 | + var services = new ServiceCollection(); |
| 82 | + services.AddKeyedCodexAIAgent(KeyedServiceName); |
| 83 | + |
| 84 | + var provider = services.BuildServiceProvider(); |
| 85 | + var agent = provider.GetKeyedService<AIAgent>(KeyedServiceName); |
| 86 | + var chatClient = provider.GetKeyedService<IChatClient>(KeyedServiceName); |
| 87 | + var resolvedAgentChatClient = agent?.GetService(typeof(IChatClient)) as IChatClient; |
| 88 | + var metadata = resolvedAgentChatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata; |
| 89 | + |
| 90 | + await Assert.That(agent).IsNotNull(); |
| 91 | + await Assert.That(chatClient).IsNotNull(); |
| 92 | + await Assert.That(resolvedAgentChatClient).IsNotNull(); |
| 93 | + await Assert.That(metadata).IsNotNull(); |
| 94 | + await Assert.That(metadata!.ProviderName).IsEqualTo("CodexCLI"); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public async Task AddKeyedCodexAIAgent_ThrowsForNullServiceKey() |
| 99 | + { |
| 100 | + var services = new ServiceCollection(); |
| 101 | + |
| 102 | + var action = () => services.AddKeyedCodexAIAgent(serviceKey: null!); |
| 103 | + |
| 104 | + var exception = await Assert.That(action).ThrowsException(); |
| 105 | + await Assert.That(exception).IsTypeOf<ArgumentNullException>(); |
| 106 | + await Assert.That(((ArgumentNullException)exception!).ParamName).IsEqualTo("serviceKey"); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public async Task AddKeyedCodexAIAgent_WithConfiguration_AppliesKeyedAgentOptions() |
| 111 | + { |
| 112 | + var services = new ServiceCollection(); |
| 113 | + services.AddKeyedCodexAIAgent( |
| 114 | + KeyedServiceName, |
| 115 | + configureChatClient: options => options.DefaultModel = ConfiguredDefaultModel, |
| 116 | + configureAgent: options => |
| 117 | + { |
| 118 | + options.Name = AgentName; |
| 119 | + options.ChatOptions = new ChatOptions |
| 120 | + { |
| 121 | + Instructions = AgentInstructions, |
| 122 | + }; |
| 123 | + }); |
| 124 | + |
| 125 | + var provider = services.BuildServiceProvider(); |
| 126 | + var agent = provider.GetRequiredKeyedService<AIAgent>(KeyedServiceName); |
| 127 | + var chatClient = agent.GetService<IChatClient>(); |
| 128 | + var metadata = chatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata; |
| 129 | + var agentOptions = agent.GetService<ChatClientAgentOptions>(); |
| 130 | + |
| 131 | + await Assert.That(agent.Name).IsEqualTo(AgentName); |
| 132 | + await Assert.That(agentOptions).IsNotNull(); |
| 133 | + await Assert.That(agentOptions!.ChatOptions).IsNotNull(); |
| 134 | + await Assert.That(agentOptions.ChatOptions!.Instructions).IsEqualTo(AgentInstructions); |
| 135 | + await Assert.That(metadata).IsNotNull(); |
| 136 | + await Assert.That(metadata!.DefaultModelId).IsEqualTo(ConfiguredDefaultModel); |
| 137 | + } |
| 138 | +} |
0 commit comments