-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClaudeServiceCollectionExtensionsTests.cs
More file actions
141 lines (121 loc) · 6.11 KB
/
ClaudeServiceCollectionExtensionsTests.cs
File metadata and controls
141 lines (121 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using ManagedCode.ClaudeCodeSharpSDK.Extensions.AgentFramework.Extensions;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
namespace ManagedCode.ClaudeCodeSharpSDK.Tests.AgentFramework;
public class ClaudeServiceCollectionExtensionsTests
{
private const string AgentDescription = "Agent description";
private const string AgentInstructions = "You are a coding assistant.";
private const string AgentName = "claude-agent";
private const string ConfiguredDefaultModel = "configured-default-model";
private const string KeyedServiceName = "claude-agent";
private const string ProviderName = "ClaudeCodeCLI";
private const string ServicesParameterName = "services";
private const string ServiceKeyParameterName = "serviceKey";
[Test]
public async Task AddClaudeCodeAgent_ThrowsForNullServices()
{
ServiceCollection? services = null;
var action = () => services!.AddClaudeCodeAgent();
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<ArgumentNullException>();
await Assert.That(((ArgumentNullException)exception!).ParamName).IsEqualTo(ServicesParameterName);
}
[Test]
public async Task AddClaudeCodeAgent_RegistersAIAgentAndChatClient()
{
var services = new ServiceCollection();
services.AddClaudeCodeAgent();
var provider = services.BuildServiceProvider();
var agent = provider.GetService<AIAgent>();
var chatClient = provider.GetService<IChatClient>();
var resolvedAgentChatClient = agent?.GetService(typeof(IChatClient)) as IChatClient;
var metadata = resolvedAgentChatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata;
await Assert.That(agent).IsNotNull();
await Assert.That(chatClient).IsNotNull();
await Assert.That(resolvedAgentChatClient).IsNotNull();
await Assert.That(metadata).IsNotNull();
await Assert.That(metadata!.ProviderName).IsEqualTo(ProviderName);
}
[Test]
public async Task AddClaudeCodeAgent_WithConfiguration_AppliesAgentOptions()
{
var services = new ServiceCollection();
services.AddClaudeCodeAgent(
configureChatClient: options => options.DefaultModel = ConfiguredDefaultModel,
configureAgent: options =>
{
options.Name = AgentName;
options.Description = AgentDescription;
options.ChatOptions = new ChatOptions
{
Instructions = AgentInstructions,
};
});
var provider = services.BuildServiceProvider();
var agent = provider.GetRequiredService<AIAgent>();
var chatClient = agent.GetService<IChatClient>();
var metadata = chatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata;
var agentOptions = agent.GetService<ChatClientAgentOptions>();
await Assert.That(agent.Name).IsEqualTo(AgentName);
await Assert.That(agent.Description).IsEqualTo(AgentDescription);
await Assert.That(agentOptions).IsNotNull();
await Assert.That(agentOptions!.ChatOptions).IsNotNull();
await Assert.That(agentOptions.ChatOptions!.Instructions).IsEqualTo(AgentInstructions);
await Assert.That(metadata).IsNotNull();
await Assert.That(metadata!.DefaultModelId).IsEqualTo(ConfiguredDefaultModel);
}
[Test]
public async Task AddKeyedClaudeCodeAgent_RegistersKeyedAgent()
{
var services = new ServiceCollection();
services.AddKeyedClaudeCodeAgent(KeyedServiceName);
var provider = services.BuildServiceProvider();
var agent = provider.GetKeyedService<AIAgent>(KeyedServiceName);
var chatClient = provider.GetKeyedService<IChatClient>(KeyedServiceName);
var resolvedAgentChatClient = agent?.GetService(typeof(IChatClient)) as IChatClient;
var metadata = resolvedAgentChatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata;
await Assert.That(agent).IsNotNull();
await Assert.That(chatClient).IsNotNull();
await Assert.That(resolvedAgentChatClient).IsNotNull();
await Assert.That(metadata).IsNotNull();
await Assert.That(metadata!.ProviderName).IsEqualTo(ProviderName);
}
[Test]
public async Task AddKeyedClaudeCodeAgent_ThrowsForNullServiceKey()
{
var services = new ServiceCollection();
var action = () => services.AddKeyedClaudeCodeAgent(serviceKey: null!);
var exception = await Assert.That(action).ThrowsException();
await Assert.That(exception).IsTypeOf<ArgumentNullException>();
await Assert.That(((ArgumentNullException)exception!).ParamName).IsEqualTo(ServiceKeyParameterName);
}
[Test]
public async Task AddKeyedClaudeCodeAgent_WithConfiguration_AppliesKeyedAgentOptions()
{
var services = new ServiceCollection();
services.AddKeyedClaudeCodeAgent(
KeyedServiceName,
configureChatClient: options => options.DefaultModel = ConfiguredDefaultModel,
configureAgent: options =>
{
options.Name = AgentName;
options.ChatOptions = new ChatOptions
{
Instructions = AgentInstructions,
};
});
var provider = services.BuildServiceProvider();
var agent = provider.GetRequiredKeyedService<AIAgent>(KeyedServiceName);
var chatClient = agent.GetService<IChatClient>();
var metadata = chatClient?.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata;
var agentOptions = agent.GetService<ChatClientAgentOptions>();
await Assert.That(agent.Name).IsEqualTo(AgentName);
await Assert.That(agentOptions).IsNotNull();
await Assert.That(agentOptions!.ChatOptions).IsNotNull();
await Assert.That(agentOptions.ChatOptions!.Instructions).IsEqualTo(AgentInstructions);
await Assert.That(metadata).IsNotNull();
await Assert.That(metadata!.DefaultModelId).IsEqualTo(ConfiguredDefaultModel);
}
}