using Motus.Analyzers.Analyzers;

namespace Motus.Analyzers.Tests;

[TestClass]
public class FragileSelectorAnalyzerTests
{
    [TestMethod]
    public async Task ShallowSelector_NoDiagnostic()
    {
        var source = """
            using Motus.Abstractions;

            public class Tests
            {
                public void Run(IPage page)
                {
                    page.Locator("button.submit");
                }
            }
            """;

        var diagnostics = await AnalyzerTestHelper.GetDiagnosticsAsync<FragileSelectorAnalyzer>(source);
        Assert.AreEqual(0, diagnostics.Length);
    }

    [TestMethod]
    public async Task DeepNesting_Diagnostic()
    {
        var source = """
            using Motus.Abstractions;

            public class Tests
            {
                public void Run(IPage page)
                {
                    page.Locator("div > ul > li > span > a");
                }
            }
            """;

        var diagnostics = await AnalyzerTestHelper.GetDiagnosticsAsync<FragileSelectorAnalyzer>(source);
        Assert.AreEqual(1, diagnostics.Length);
        Assert.AreEqual("MOT003", diagnostics[0].Id);
    }

    [TestMethod]
    public async Task NthChildChain_Diagnostic()
    {
        var source = """
            using Motus.Abstractions;

            public class Tests
            {
                public void Run(IPage page)
                {
                    page.Locator("ul:nth-child(2) li:nth-child(3)");
                }
            }
            """;

        var diagnostics = await AnalyzerTestHelper.GetDiagnosticsAsync<FragileSelectorAnalyzer>(source);
        Assert.AreEqual(1, diagnostics.Length);
        Assert.AreEqual("MOT003", diagnostics[0].Id);
    }

    [TestMethod]
    public async Task AutoGeneratedClassName_Diagnostic()
    {
        var source = """
            using Motus.Abstractions;

            public class Tests
            {
                public void Run(IPage page)
                {
                    page.Locator(".component-a3f8b2c1");
                }
            }
            """;

        var diagnostics = await AnalyzerTestHelper.GetDiagnosticsAsync<FragileSelectorAnalyzer>(source);
        Assert.AreEqual(1, diagnostics.Length);
        Assert.AreEqual("MOT003", diagnostics[0].Id);
    }
}
