// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Combinatorial.MSTest;

using Microsoft.Testing.Platform.Acceptance.IntegrationTests;
using Microsoft.Testing.Platform.Acceptance.IntegrationTests.Helpers;
using Microsoft.Testing.Platform.Helpers;

namespace MSTest.Acceptance.IntegrationTests;

[TestClass]
public sealed class InconclusiveTests : AcceptanceTestBase<InconclusiveTests.TestAssetFixture>
{
    public enum Lifecycle
    {
        AssemblyInitialize,
        ClassInitialize,
        Constructor,
        TestInitialize,
        TestMethod,
        TestCleanup,
        ClassCleanup,
        AssemblyCleanup,
    }

    // [CombinatorialData] cross-products every inconclusive lifecycle step (auto-expanded from the
    // Lifecycle enum) with the metadata modes under test (reflection and, unless globally disabled,
    // the two source-generation paths) so the same expectations validate every metadata path.
    [TestMethod]
    [CombinatorialData]
    public async Task TestOutcomeShouldBeRespectedCorrectly(Lifecycle inconclusiveStep, [MetadataModeValues] MetadataMode metadataMode)
    {
        var testHost = TestHost.LocateFrom(AssetFixture.ProjectPath, TestAssetFixture.ProjectName, TargetFrameworks.NetCurrent, metadataMode: metadataMode);
        TestHostResult testHostResult = await testHost.ExecuteAsync(
            "--settings my.runsettings",
            environmentVariables: new Dictionary<string, string?>
            {
                [$"{inconclusiveStep}Inconclusive"] = "1",
            },
            cancellationToken: TestContext.CancellationToken);

        if (inconclusiveStep >= Lifecycle.ClassCleanup)
        {
            testHostResult.AssertExitCodeIs(ExitCode.AtLeastOneTestFailed);
            testHostResult.AssertOutputContainsSummary(failed: 1, passed: 1, skipped: 0);
        }
        else
        {
            testHostResult.AssertExitCodeIs(ExitCode.Success);
            testHostResult.AssertOutputContainsSummary(failed: 0, passed: 0, skipped: 1);
        }

        testHostResult.AssertOutputContains("AssemblyInitialize called");

        if (inconclusiveStep >= Lifecycle.Constructor)
        {
            testHostResult.AssertOutputContains("Constructor called");
        }
        else
        {
            testHostResult.AssertOutputDoesNotContain("Constructor called");
        }

        if (inconclusiveStep >= Lifecycle.ClassInitialize)
        {
            testHostResult.AssertOutputContains("ClassInitialize called");
        }
        else
        {
            testHostResult.AssertOutputDoesNotContain("ClassInitialize called");
        }

        if (inconclusiveStep >= Lifecycle.TestInitialize)
        {
            testHostResult.AssertOutputContains("TestInitialize called");
        }
        else
        {
            testHostResult.AssertOutputDoesNotContain("TestInitialize called");
        }

        if (inconclusiveStep >= Lifecycle.TestMethod)
        {
            testHostResult.AssertOutputContains("TestMethod called");
        }
        else
        {
            testHostResult.AssertOutputDoesNotContain("TestMethod called");
        }

        if (inconclusiveStep >= Lifecycle.TestInitialize)
        {
            testHostResult.AssertOutputContains("TestCleanup called");
        }
        else
        {
            testHostResult.AssertOutputDoesNotContain("TestCleanup called");
        }

        if (inconclusiveStep >= Lifecycle.ClassInitialize)
        {
            testHostResult.AssertOutputContains("ClassCleanup called");
        }
        else
        {
            testHostResult.AssertOutputDoesNotContain("ClassCleanup called");
        }

        testHostResult.AssertOutputContains("AssemblyCleanup called");
    }

    public sealed class TestAssetFixture() : TestAssetFixtureBase()
    {
        public const string ProjectName = "TestInconclusive";

        public string ProjectPath => GetAssetPath(ProjectName);

        protected override IReadOnlyList<MetadataMode> SourceGenMetadataModes { get; }
            = [MetadataMode.SourceGeneration, MetadataMode.AotSourceGeneration];

        public override (string ID, string Name, string Code) GetAssetsToGenerate() => (ProjectName, ProjectName,
                SourceCode
                .PatchTargetFrameworks(TargetFrameworks.NetCurrent)
                .PatchCodeWithReplace("$MSTestVersion$", MSTestVersion));

        private const string SourceCode = """
#file TestInconclusive.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <EnableMSTestRunner>true</EnableMSTestRunner>
    <TargetFrameworks>$TargetFrameworks$</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MSTest.TestAdapter" Version="$MSTestVersion$" />
    <PackageReference Include="MSTest.TestFramework" Version="$MSTestVersion$" />
  </ItemGroup>

  <ItemGroup>
    <None Update="*.runsettings">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

#file my.runsettings
<RunSettings>
  <MSTest>
    <CaptureTraceOutput>false</CaptureTraceOutput>
  </MSTest>
</RunSettings>

#file UnitTest1.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class UnitTest1
{
    public UnitTest1()
    {
        Console.WriteLine("Constructor called");
        if (Environment.GetEnvironmentVariable("ConstructorInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }

    [AssemblyInitialize]
    public static void AsmInitialize(TestContext _)
    {
        Console.WriteLine("AssemblyInitialize called");
        if (Environment.GetEnvironmentVariable("AssemblyInitializeInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }

    [ClassInitialize]
    public static void ClassInit(TestContext _)
    {
        Console.WriteLine("ClassInitialize called");
        if (Environment.GetEnvironmentVariable("ClassInitializeInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }

    [TestInitialize]
    public void TestInit()
    {
        Console.WriteLine("TestInitialize called");
        if (Environment.GetEnvironmentVariable("TestInitializeInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }

    [TestMethod]
    public void TestMethod()
    {
        Console.WriteLine("TestMethod called");
        if (Environment.GetEnvironmentVariable("TestMethodInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }

    [TestCleanup]
    public void TestCleanup()
    {
        Console.WriteLine("TestCleanup called");
        if (Environment.GetEnvironmentVariable("TestCleanupInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }

    [ClassCleanup]
    public static void ClassCleanup(TestContext _)
    {
        Console.WriteLine("ClassCleanup called");
        if (Environment.GetEnvironmentVariable("ClassCleanupInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }

    [AssemblyCleanup]
    public static void AsmCleanup(TestContext _)
    {
        Console.WriteLine("AssemblyCleanup called");
        if (Environment.GetEnvironmentVariable("AssemblyCleanupInconclusive") == "1")
        {
            Assert.Inconclusive();
        }
    }
}
""";
    }

    public TestContext TestContext { get; set; }
}
