using Flyshot.Runtime.Common;
using Flyshot.Runtime.Fanuc;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace Flyshot.Core.Tests;

/// <summary>
/// 验证 FANUC 运行时通过依赖注入解析时，能够稳定拿到日志依赖。
/// </summary>
public sealed class FanucControllerRuntimeLoggingRegistrationTests
{
    /// <summary>
    /// 验证宿主按 IControllerRuntime 解析 FANUC 运行时时，不会因为可选日志参数而退回到无日志实例。
    /// </summary>
    [Fact]
    public void ServiceProvider_Resolves_FanucControllerRuntime_WithLogger()
    {
        var services = new ServiceCollection();
        services.AddLogging();
        services.AddSingleton<IControllerRuntime, FanucControllerRuntime>();

        using var serviceProvider = services.BuildServiceProvider();

        var runtime = serviceProvider.GetRequiredService<IControllerRuntime>();
        var concreteRuntime = Assert.IsType<FanucControllerRuntime>(runtime);
        var loggerField = typeof(FanucControllerRuntime).GetField("_logger", BindingFlags.Instance | BindingFlags.NonPublic);

        Assert.NotNull(loggerField);
        Assert.NotNull(loggerField!.GetValue(concreteRuntime));
    }
}
