diff --git a/MaIN.Core.IntegrationTests/ChatTests.cs b/MaIN.Core.IntegrationTests/ChatTests.cs index e43a5f00..93e699c7 100644 --- a/MaIN.Core.IntegrationTests/ChatTests.cs +++ b/MaIN.Core.IntegrationTests/ChatTests.cs @@ -1,4 +1,6 @@ -using MaIN.Core.Hub; +using FuzzySharp; +using MaIN.Core.Hub; +using MaIN.Core.IntegrationTests.Helpers; using MaIN.Domain.Entities; using MaIN.Domain.Models.Concrete; @@ -66,7 +68,7 @@ public async Task Should_AnswerGameFromImage_ChatWithVision() var result = await AIHub.Chat() .WithModel() - .WithMessage("What is the title of game?") + .WithMessage("What is the title of the game? Answer only this question.") .WithMemoryParams(new MemoryParams { AnswerTokens = 1000 @@ -77,13 +79,20 @@ public async Task Should_AnswerGameFromImage_ChatWithVision() Assert.True(result.Done); Assert.NotNull(result.Message); Assert.NotEmpty(result.Message.Content); - Assert.Contains("call of duty", result.Message.Content.ToLower()); + var ratio = Fuzz.PartialRatio("call of duty", result.Message.Content.ToLowerInvariant()); + Assert.True(ratio > 50, + $""" + Fuzzy match failed! + Expected > 50, but got {ratio}. + Expexted: 'call of duty' + Actual: '{result.Message.Content}' + """); } [Fact(Skip = "Require powerful GPU")] public async Task Should_GenerateImage_BasedOnPrompt() { - Assert.True(PingHost("127.0.0.1", 5003, 5), "Please make sure ImageGen service is running on port 5003"); + Assert.True(NetworkHelper.PingHost("127.0.0.1", 5003, 5), "Please make sure ImageGen service is running on port 5003"); const string extension = "png"; diff --git a/MaIN.Core.IntegrationTests/Helpers/NetworkHelper.cs b/MaIN.Core.IntegrationTests/Helpers/NetworkHelper.cs new file mode 100644 index 00000000..1482d4e4 --- /dev/null +++ b/MaIN.Core.IntegrationTests/Helpers/NetworkHelper.cs @@ -0,0 +1,29 @@ +using System; +using System.Net.Sockets; + +namespace MaIN.Core.IntegrationTests.Helpers; + +public static class NetworkHelper +{ + public static bool PingHost(string host, int port, int timeout) + { + try + { + using var client = new TcpClient(); + var result = client.BeginConnect(host, port, null, null); + var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout)); + + if (!success) + { + return false; + } + + client.EndConnect(result); + return true; + } + catch + { + return false; + } + } +} diff --git a/MaIN.Core.IntegrationTests/IntegrationTestBase.cs b/MaIN.Core.IntegrationTests/IntegrationTestBase.cs index bfbcaed9..db53171a 100644 --- a/MaIN.Core.IntegrationTests/IntegrationTestBase.cs +++ b/MaIN.Core.IntegrationTests/IntegrationTestBase.cs @@ -10,31 +10,25 @@ public class IntegrationTestBase : IDisposable protected readonly IHost _host; protected readonly IServiceProvider _services; - public IntegrationTestBase() + protected IntegrationTestBase() { - _host = CreateHost(); + _host = Host.CreateDefaultBuilder() + .ConfigureServices((context, services) => + { + services.AddMaIN(context.Configuration); + ConfigureServices(services); + }) + .Build(); + + _host.Services.UseMaIN(); _host.Start(); - + _services = _host.Services; } - private IHost CreateHost() + // Allow derived classes to add additional services or override existing ones + protected virtual void ConfigureServices(IServiceCollection services) { - var hostBuilder = Host.CreateDefaultBuilder() - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder - .UseUrls("http://localhost:0") // Random available port - .ConfigureServices((context, services) => - { - services.AddMaIN(context.Configuration); - - var provider = services.BuildServiceProvider(); - provider.UseMaIN(); - }); - }); - - return hostBuilder.Build(); } protected T GetService() where T : notnull @@ -42,30 +36,6 @@ protected T GetService() where T : notnull return _services.GetRequiredService(); } - protected static bool PingHost(string host, int port, int timeout) - { - try - { - using (var client = new TcpClient()) - { - var result = client.BeginConnect(host, port, null, null); - var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout)); - - if (!success) - { - return false; - } - - client.EndConnect(result); - return true; - } - } - catch - { - return false; - } - } - public void Dispose() { _host?.Dispose(); diff --git a/MaIN.Core.IntegrationTests/MaIN.Core.IntegrationTests.csproj b/MaIN.Core.IntegrationTests/MaIN.Core.IntegrationTests.csproj index 03e9f2ff..94edd801 100644 --- a/MaIN.Core.IntegrationTests/MaIN.Core.IntegrationTests.csproj +++ b/MaIN.Core.IntegrationTests/MaIN.Core.IntegrationTests.csproj @@ -8,6 +8,7 @@ +