Forge
csharp3407750f
1using AgentForge.Abstractions;
2using AgentForge.Services;
3using Microsoft.AspNetCore.Hosting;
4using Microsoft.AspNetCore.Mvc.Testing;
5using Microsoft.Extensions.DependencyInjection;
6using Microsoft.Extensions.DependencyInjection.Extensions;
7
8namespace AgentForge.Tests;
9
10public sealed class ForgeWebApplicationFactory : WebApplicationFactory<Program>
11{
12 private string? _previousPluginBundle;
13
14 public string DatabasePath { get; } = Path.Combine(Path.GetTempPath(), $"forge-test-{Guid.NewGuid():N}.witdb");
15
16 public string ReposPath { get; } = Path.Combine(Path.GetTempPath(), $"forge-repos-{Guid.NewGuid():N}");
17
18 public GitRepoService Git => Services.GetRequiredService<GitRepoService>();
19
20 protected override void ConfigureWebHost(IWebHostBuilder builder)
21 {
22 builder.UseContentRoot(AppContext.BaseDirectory);
23 builder.UseEnvironment("Development");
24 Environment.SetEnvironmentVariable("FORGE_DATABASE_PATH", DatabasePath);
25 Environment.SetEnvironmentVariable("FORGE_REPOS_PATH", ReposPath);
26 Environment.SetEnvironmentVariable("FORGE_PUBLIC_BASE_URL", "http://test.local");
27 Environment.SetEnvironmentVariable("FORGE_GIT_BASE_URL", "ssh://git@test.local");
28
29 _previousPluginBundle = Environment.GetEnvironmentVariable("FORGE_PLUGIN_BUNDLE");
30 if (PluginBundle is not null)
31 Environment.SetEnvironmentVariable("FORGE_PLUGIN_BUNDLE", PluginBundle);
32 else
33 Environment.SetEnvironmentVariable("FORGE_PLUGIN_BUNDLE", null);
34
35 builder.ConfigureServices(services =>
36 {
37 services.RemoveAll(typeof(IForgeMcpHttpClientSource));
38 services.AddSingleton<IForgeMcpHttpClientSource>(new TestForgeMcpHttpClientSource(this));
39
40 if (HostLifetime is not null)
41 {
42 services.RemoveAll(typeof(IForgeHostLifetime));
43 services.AddSingleton<IForgeHostLifetime>(HostLifetime);
44 }
45 });
46 }
47
48 /// <summary>When set, overrides plugin bundle for this factory instance.</summary>
49 public string? PluginBundle { get; init; }
50
51 public TestForgeHostLifetime? HostLifetime { get; init; }
52
53 protected override void Dispose(bool disposing)
54 {
55 Environment.SetEnvironmentVariable("FORGE_PLUGIN_BUNDLE", _previousPluginBundle);
56 base.Dispose(disposing);
57 try
58 {
59 if (File.Exists(DatabasePath))
60 File.Delete(DatabasePath);
61 }
62 catch
63 {
64 // WitDB / AV may still hold the file on Windows.
65 }
66
67 try
68 {
69 if (Directory.Exists(ReposPath))
70 Directory.Delete(ReposPath, recursive: true);
71 }
72 catch
73 {
74 // Bare .git objects may still be locked briefly after LibGit2Sharp use.
75 }
76 }
77
78}
79
80
81
View only · write via MCP/CIDE