Skip to content

Commit b55e64d

Browse files
authored
tests: fix integration tests (#120)
* tests: fix integration tests - Configure host middleware (error was thrown before). - Use Fuzz "sentence similarity" algotithm to accept not identical but good enough results. - Reduced average Should_AnswerGameFromImage_ChatWithVision test execution time from 60s to 7s * remove webserver initialization as it is not needed and move PingHost to the helper class.
1 parent 66013e3 commit b55e64d

4 files changed

Lines changed: 56 additions & 47 deletions

File tree

MaIN.Core.IntegrationTests/ChatTests.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using MaIN.Core.Hub;
1+
using FuzzySharp;
2+
using MaIN.Core.Hub;
3+
using MaIN.Core.IntegrationTests.Helpers;
24
using MaIN.Domain.Entities;
35
using MaIN.Domain.Models.Concrete;
46

@@ -66,7 +68,7 @@ public async Task Should_AnswerGameFromImage_ChatWithVision()
6668

6769
var result = await AIHub.Chat()
6870
.WithModel<Llama3_2_3b>()
69-
.WithMessage("What is the title of game?")
71+
.WithMessage("What is the title of the game? Answer only this question.")
7072
.WithMemoryParams(new MemoryParams
7173
{
7274
AnswerTokens = 1000
@@ -77,13 +79,20 @@ public async Task Should_AnswerGameFromImage_ChatWithVision()
7779
Assert.True(result.Done);
7880
Assert.NotNull(result.Message);
7981
Assert.NotEmpty(result.Message.Content);
80-
Assert.Contains("call of duty", result.Message.Content.ToLower());
82+
var ratio = Fuzz.PartialRatio("call of duty", result.Message.Content.ToLowerInvariant());
83+
Assert.True(ratio > 50,
84+
$"""
85+
Fuzzy match failed!
86+
Expected > 50, but got {ratio}.
87+
Expexted: 'call of duty'
88+
Actual: '{result.Message.Content}'
89+
""");
8190
}
8291

8392
[Fact(Skip = "Require powerful GPU")]
8493
public async Task Should_GenerateImage_BasedOnPrompt()
8594
{
86-
Assert.True(PingHost("127.0.0.1", 5003, 5), "Please make sure ImageGen service is running on port 5003");
95+
Assert.True(NetworkHelper.PingHost("127.0.0.1", 5003, 5), "Please make sure ImageGen service is running on port 5003");
8796

8897
const string extension = "png";
8998

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Net.Sockets;
3+
4+
namespace MaIN.Core.IntegrationTests.Helpers;
5+
6+
public static class NetworkHelper
7+
{
8+
public static bool PingHost(string host, int port, int timeout)
9+
{
10+
try
11+
{
12+
using var client = new TcpClient();
13+
var result = client.BeginConnect(host, port, null, null);
14+
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout));
15+
16+
if (!success)
17+
{
18+
return false;
19+
}
20+
21+
client.EndConnect(result);
22+
return true;
23+
}
24+
catch
25+
{
26+
return false;
27+
}
28+
}
29+
}

MaIN.Core.IntegrationTests/IntegrationTestBase.cs

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,32 @@ public class IntegrationTestBase : IDisposable
1010
protected readonly IHost _host;
1111
protected readonly IServiceProvider _services;
1212

13-
public IntegrationTestBase()
13+
protected IntegrationTestBase()
1414
{
15-
_host = CreateHost();
15+
_host = Host.CreateDefaultBuilder()
16+
.ConfigureServices((context, services) =>
17+
{
18+
services.AddMaIN(context.Configuration);
19+
ConfigureServices(services);
20+
})
21+
.Build();
22+
23+
_host.Services.UseMaIN();
1624
_host.Start();
17-
25+
1826
_services = _host.Services;
1927
}
2028

21-
private IHost CreateHost()
29+
// Allow derived classes to add additional services or override existing ones
30+
protected virtual void ConfigureServices(IServiceCollection services)
2231
{
23-
var hostBuilder = Host.CreateDefaultBuilder()
24-
.ConfigureWebHostDefaults(webBuilder =>
25-
{
26-
webBuilder
27-
.UseUrls("http://localhost:0") // Random available port
28-
.ConfigureServices((context, services) =>
29-
{
30-
services.AddMaIN(context.Configuration);
31-
32-
var provider = services.BuildServiceProvider();
33-
provider.UseMaIN();
34-
});
35-
});
36-
37-
return hostBuilder.Build();
3832
}
3933

4034
protected T GetService<T>() where T : notnull
4135
{
4236
return _services.GetRequiredService<T>();
4337
}
4438

45-
protected static bool PingHost(string host, int port, int timeout)
46-
{
47-
try
48-
{
49-
using (var client = new TcpClient())
50-
{
51-
var result = client.BeginConnect(host, port, null, null);
52-
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeout));
53-
54-
if (!success)
55-
{
56-
return false;
57-
}
58-
59-
client.EndConnect(result);
60-
return true;
61-
}
62-
}
63-
catch
64-
{
65-
return false;
66-
}
67-
}
68-
6939
public void Dispose()
7040
{
7141
_host?.Dispose();

MaIN.Core.IntegrationTests/MaIN.Core.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="FuzzySharp" Version="2.0.2" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
1213
<PackageReference Include="xunit" Version="2.9.3" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">

0 commit comments

Comments
 (0)