86 lines
2.6 KiB
C#
86 lines
2.6 KiB
C#
namespace StellaOps.Plugin.Testing;
|
|
|
|
using StellaOps.Plugin.Abstractions.Context;
|
|
|
|
/// <summary>
|
|
/// Test implementation of IPluginContext.
|
|
/// </summary>
|
|
public sealed class TestPluginContext : IPluginContext
|
|
{
|
|
/// <summary>
|
|
/// Gets the test configuration.
|
|
/// </summary>
|
|
public TestPluginConfiguration Configuration { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the test logger.
|
|
/// </summary>
|
|
public TestPluginLogger Logger { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the test services.
|
|
/// </summary>
|
|
public TestPluginServices Services { get; }
|
|
|
|
/// <inheritdoc />
|
|
public Guid? TenantId { get; set; }
|
|
|
|
/// <inheritdoc />
|
|
public Guid InstanceId { get; }
|
|
|
|
/// <inheritdoc />
|
|
public CancellationToken ShutdownToken { get; }
|
|
|
|
/// <inheritdoc />
|
|
public TimeProvider TimeProvider { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the GUID generator for deterministic testing.
|
|
/// </summary>
|
|
public SequentialGuidGenerator GuidGenerator { get; }
|
|
|
|
/// <summary>
|
|
/// Gets the HTTP client factory for test mocking.
|
|
/// </summary>
|
|
public TestHttpClientFactory HttpClientFactory { get; }
|
|
|
|
IPluginConfiguration IPluginContext.Configuration => Configuration;
|
|
IPluginLogger IPluginContext.Logger => Logger;
|
|
IPluginServices IPluginContext.Services => Services;
|
|
|
|
/// <summary>
|
|
/// Creates a new test context.
|
|
/// </summary>
|
|
/// <param name="options">Test host options.</param>
|
|
public TestPluginContext(PluginTestHostOptions options)
|
|
{
|
|
Configuration = new TestPluginConfiguration(options.Configuration, options.Secrets);
|
|
Logger = new TestPluginLogger(options.MinLogLevel, options.EnableLogging);
|
|
Services = new TestPluginServices();
|
|
|
|
TimeProvider = options.TimeProvider
|
|
?? new FakeTimeProvider(options.StartTime ?? DateTimeOffset.UtcNow);
|
|
|
|
GuidGenerator = new SequentialGuidGenerator();
|
|
HttpClientFactory = new TestHttpClientFactory();
|
|
|
|
InstanceId = GuidGenerator.NewGuid();
|
|
ShutdownToken = CancellationToken.None;
|
|
|
|
// Register common services
|
|
Services.Register<IHttpClientFactory>(HttpClientFactory);
|
|
Services.Register(TimeProvider);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new test context with a custom shutdown token.
|
|
/// </summary>
|
|
/// <param name="options">Test host options.</param>
|
|
/// <param name="shutdownToken">Shutdown cancellation token.</param>
|
|
public TestPluginContext(PluginTestHostOptions options, CancellationToken shutdownToken)
|
|
: this(options)
|
|
{
|
|
ShutdownToken = shutdownToken;
|
|
}
|
|
}
|