using Shouldly;
using Xunit;

namespace ZB.MOM.WW.OtOpcUa.Driver.S7.Cli.Tests;

/// <summary>
///     Driver.S7.Cli-007: the S7 subscribe command — a near-verbatim copy of the Modbus
///     subscribe command — must keep the comment that explains why <c>OnDataChange</c>
///     uses <c>console.Output.WriteLine</c> (synchronous, on a driver background thread)
///     instead of <c>System.Console</c> or the async <c>WriteLineAsync</c>. The rationale
///     is non-obvious to a reader and the Modbus copy carries it; the S7 copy must too.
/// </summary>
[Trait("Category", "Unit")]
public sealed class SubscribeCommandConsoleHandlerCommentTests
{
    /// <summary>Verifies that SubscribeCommand explains why OnDataChange uses console.Output synchronously.</summary>
    [Fact]
    public void SubscribeCommand_explains_why_OnDataChange_uses_console_Output_synchronously()
    {
        var dir = new DirectoryInfo(AppContext.BaseDirectory);
        while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.OtOpcUa.slnx")))
            dir = dir.Parent;
        dir.ShouldNotBeNull();
        var source = File.ReadAllText(Path.Combine(
            dir!.FullName, "src", "Drivers", "Cli", "ZB.MOM.WW.OtOpcUa.Driver.S7.Cli",
            "Commands", "SubscribeCommand.cs"));

        // The comment must reference the CliFx console abstraction so future copy-pastes
        // do not lose the rationale.
        source.ShouldContain("CliFx console");
        source.ShouldContain("IConsole");
    }
}
