namespace ZB.MOM.WW.MxGateway.IntegrationTests;

public sealed class IntegrationTestEnvironmentTests
{
    /// <summary>Verifies that live MXAccess tests use correct environment variable name.</summary>
    [Fact]
    public void LiveMxAccessTests_AreOptInByEnvironmentVariable()
    {
        Assert.Equal(
            "MXGATEWAY_RUN_LIVE_MXACCESS_TESTS",
            IntegrationTestEnvironment.LiveMxAccessVariableName);
    }

    /// <summary>Verifies that worker executable uses correct environment variable name.</summary>
    [Fact]
    public void LiveMxAccessWorkerExecutable_UsesDocumentedEnvironmentVariable()
    {
        Assert.Equal(
            "MXGATEWAY_LIVE_MXACCESS_WORKER_EXE",
            IntegrationTestEnvironment.LiveMxAccessWorkerExecutableVariableName);
    }

    /// <summary>Verifies that repository root resolution accepts git worktree files.</summary>
    [Fact]
    public void ResolveRepositoryRoot_AcceptsGitWorktreeFile()
    {
        string temporaryRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
        string nestedDirectory = Path.Combine(temporaryRoot, "tests", "bin");

        try
        {
            Directory.CreateDirectory(nestedDirectory);
            Directory.CreateDirectory(Path.Combine(temporaryRoot, "src"));
            File.WriteAllText(Path.Combine(temporaryRoot, ".git"), "gitdir: ../.git/worktrees/test");

            string repositoryRoot = IntegrationTestEnvironment.ResolveRepositoryRoot(nestedDirectory);

            Assert.Equal(temporaryRoot, repositoryRoot);
        }
        finally
        {
            if (Directory.Exists(temporaryRoot))
            {
                Directory.Delete(temporaryRoot, recursive: true);
            }
        }
    }

    /// <summary>
    /// Verifies that <see cref="IntegrationTestEnvironment.ResolveRepositoryRoot"/>
    /// throws <see cref="InvalidOperationException"/> with a diagnostic message when
    /// the walk exhausts without finding a repository root. The previous silent
    /// fallback to <c>Directory.GetCurrentDirectory()</c> masked misconfiguration
    /// (IntegrationTests-022); operators get a clear, actionable failure instead.
    /// </summary>
    [Fact]
    public void ResolveRepositoryRoot_NoMarkers_ThrowsInvalidOperationExceptionNamingStartAndMarkers()
    {
        string isolatedRoot = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
        string isolatedStart = Path.Combine(isolatedRoot, "nested");

        try
        {
            Directory.CreateDirectory(isolatedStart);

            InvalidOperationException ex = Assert.Throws<InvalidOperationException>(
                () => IntegrationTestEnvironment.ResolveRepositoryRoot(isolatedStart));

            Assert.Contains(isolatedStart, ex.Message, StringComparison.Ordinal);
            Assert.Contains(".git", ex.Message, StringComparison.Ordinal);
            Assert.Contains(".sln", ex.Message, StringComparison.Ordinal);
            Assert.Contains(
                IntegrationTestEnvironment.LiveMxAccessWorkerExecutableVariableName,
                ex.Message,
                StringComparison.Ordinal);
        }
        finally
        {
            if (Directory.Exists(isolatedRoot))
            {
                Directory.Delete(isolatedRoot, recursive: true);
            }
        }
    }
}
