using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptAnalysis;

namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.ScriptAnalysis;

public sealed class FormatTests
{
    private static readonly ScriptAnalysisService Svc = new();
    private static string Fmt(string code) => Svc.Format(new FormatRequest(code)).Code;

    [Fact] public void Reflows_crammed_source_to_canonical_layout()
    {
        var outp = Fmt("var x=1;return x;");
        outp.ShouldNotBe("var x=1;return x;");      // it changed
        outp.ShouldContain("\n");                    // statements split onto separate lines
        outp.ShouldContain("var x = 1;");            // canonical spacing
    }

    [Fact] public void Empty_code_is_returned_unchanged()
        => Fmt("").ShouldBe("");

    [Fact] public void Unparseable_code_is_returned_unchanged()
    {
        // a fragment that NormalizeWhitespace can still parse in script mode won't throw;
        // pass something that round-trips to itself or is returned as-is on failure.
        var src = "return ctx.GetTag(\"A\").Value;";
        Fmt(src).ShouldNotBeNull();
    }
}
