namespace StellaOps.Plugin.Testing;
using StellaOps.Plugin.Abstractions;
using StellaOps.Plugin.Abstractions.Health;
///
/// Test host for running plugins in isolation during testing.
///
public sealed class PluginTestHost : IAsyncDisposable
{
private readonly List _plugins = new();
///
/// Gets the test context for assertions.
///
public TestPluginContext Context { get; }
///
/// Creates a new plugin test host.
///
/// Optional configuration action.
public PluginTestHost(Action? configure = null)
{
var options = new PluginTestHostOptions();
configure?.Invoke(options);
Context = new TestPluginContext(options);
}
///
/// Load and initialize a plugin.
///
/// Plugin type.
/// Cancellation token.
/// Initialized plugin instance.
public async Task LoadPluginAsync(CancellationToken ct = default) where T : IPlugin, new()
{
var plugin = new T();
await plugin.InitializeAsync(Context, ct);
_plugins.Add(plugin);
return plugin;
}
///
/// Load and initialize a plugin with custom configuration.
///
/// Plugin type.
/// Configuration values to set.
/// Cancellation token.
/// Initialized plugin instance.
public async Task LoadPluginAsync(
Dictionary configuration,
CancellationToken ct = default) where T : IPlugin, new()
{
foreach (var (key, value) in configuration)
{
Context.Configuration.SetValue(key, value);
}
return await LoadPluginAsync(ct);
}
///
/// Verify plugin health.
///
/// Plugin type.
/// Plugin to check.
/// Cancellation token.
/// Health check result.
public async Task CheckHealthAsync(T plugin, CancellationToken ct = default)
where T : IPlugin
{
return await plugin.HealthCheckAsync(ct);
}
///
public async ValueTask DisposeAsync()
{
foreach (var plugin in _plugins)
{
await plugin.DisposeAsync();
}
_plugins.Clear();
}
}