Expand a few DI-related tests (#201)
This commit is contained in:
parent
4ecb6bc240
commit
a04cf6a2f5
2 changed files with 55 additions and 15 deletions
|
|
@ -508,20 +508,33 @@ public class McpServerBuilderExtensionsToolsTests : LoggedTest, IAsyncDisposable
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(false)]
|
[InlineData(ServiceLifetime.Singleton)]
|
||||||
[InlineData(true)]
|
[InlineData(ServiceLifetime.Scoped)]
|
||||||
public void WithToolsFromAssembly_Parameters_Satisfiable_From_DI(bool parameterInServices)
|
[InlineData(ServiceLifetime.Transient)]
|
||||||
|
[InlineData(null)]
|
||||||
|
public void WithToolsFromAssembly_Parameters_Satisfiable_From_DI(ServiceLifetime? lifetime)
|
||||||
{
|
{
|
||||||
ServiceCollection sc = new();
|
ServiceCollection sc = new();
|
||||||
if (parameterInServices)
|
switch (lifetime)
|
||||||
{
|
{
|
||||||
sc.AddSingleton(new ComplexObject());
|
case ServiceLifetime.Singleton:
|
||||||
|
sc.AddSingleton(new ComplexObject());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ServiceLifetime.Scoped:
|
||||||
|
sc.AddScoped(_ => new ComplexObject());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ServiceLifetime.Transient:
|
||||||
|
sc.AddTransient(_ => new ComplexObject());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
sc.AddMcpServer().WithToolsFromAssembly();
|
sc.AddMcpServer().WithToolsFromAssembly();
|
||||||
IServiceProvider services = sc.BuildServiceProvider();
|
IServiceProvider services = sc.BuildServiceProvider();
|
||||||
|
|
||||||
McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "EchoComplex");
|
McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "EchoComplex");
|
||||||
if (parameterInServices)
|
if (lifetime is not null)
|
||||||
{
|
{
|
||||||
Assert.DoesNotContain("\"complex\"", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
|
Assert.DoesNotContain("\"complex\"", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,20 +45,47 @@ public class McpServerToolTests
|
||||||
Assert.Equal("42", result.Content[0].Text);
|
Assert.Equal("42", result.Content[0].Text);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public async Task SupportsServiceFromDI()
|
[InlineData(ServiceLifetime.Singleton)]
|
||||||
|
[InlineData(ServiceLifetime.Scoped)]
|
||||||
|
[InlineData(ServiceLifetime.Transient)]
|
||||||
|
public async Task SupportsServiceFromDI(ServiceLifetime injectedArgumentLifetime)
|
||||||
{
|
{
|
||||||
MyService expectedMyService = new();
|
MyService singletonService = new();
|
||||||
|
|
||||||
ServiceCollection sc = new();
|
ServiceCollection sc = new();
|
||||||
sc.AddSingleton(expectedMyService);
|
switch (injectedArgumentLifetime)
|
||||||
|
{
|
||||||
|
case ServiceLifetime.Singleton:
|
||||||
|
sc.AddSingleton(singletonService);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ServiceLifetime.Scoped:
|
||||||
|
sc.AddScoped(_ => new MyService());
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ServiceLifetime.Transient:
|
||||||
|
sc.AddTransient(_ => new MyService());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc.AddSingleton(services =>
|
||||||
|
{
|
||||||
|
return McpServerTool.Create((MyService actualMyService) =>
|
||||||
|
{
|
||||||
|
Assert.NotNull(actualMyService);
|
||||||
|
if (injectedArgumentLifetime == ServiceLifetime.Singleton)
|
||||||
|
{
|
||||||
|
Assert.Same(singletonService, actualMyService);
|
||||||
|
}
|
||||||
|
|
||||||
|
return "42";
|
||||||
|
}, new() { Services = services });
|
||||||
|
});
|
||||||
|
|
||||||
IServiceProvider services = sc.BuildServiceProvider();
|
IServiceProvider services = sc.BuildServiceProvider();
|
||||||
|
|
||||||
McpServerTool tool = McpServerTool.Create((MyService actualMyService) =>
|
McpServerTool tool = services.GetRequiredService<McpServerTool>();
|
||||||
{
|
|
||||||
Assert.Same(expectedMyService, actualMyService);
|
|
||||||
return "42";
|
|
||||||
}, new() { Services = services });
|
|
||||||
|
|
||||||
Assert.DoesNotContain("actualMyService", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
|
Assert.DoesNotContain("actualMyService", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue