using ClaudeDo.Installer.Checks;
using ClaudeDo.Installer.Core;

namespace ClaudeDo.Installer.Tests.Checks;

public sealed class GitCheckTests : IDisposable
{
    private readonly string _dir;

    public GitCheckTests()
    {
        _dir = Path.Combine(Path.GetTempPath(), $"cdgitcheck_{Guid.NewGuid():N}");
        Directory.CreateDirectory(_dir);
    }

    public void Dispose()
    {
        try { Directory.Delete(_dir, recursive: true); } catch { }
    }

    private string MakeGitExe()
    {
        var path = Path.Combine(_dir, "git.exe");
        File.WriteAllText(path, "");
        return path;
    }

    [Fact]
    public async Task Git_present_with_parseable_version_reports_ok()
    {
        MakeGitExe();
        var runner = new FakeProcessRunner((0, "git version 2.43.0.windows.1\n"));
        var check = new GitCheck(runner, pathOverride: _dir, pathExtOverride: ".exe");

        var result = await check.RunAsync(new InstallContext(), CancellationToken.None);

        Assert.Equal(CheckStatus.Ok, result.Status);
        Assert.Contains("2.43.0", result.Message);
        Assert.Contains(_dir, result.Message);
    }

    [Fact]
    public async Task Git_not_found_on_path_reports_failed()
    {
        var runner = new FakeProcessRunner();
        var check = new GitCheck(runner, pathOverride: _dir, pathExtOverride: ".exe");

        var result = await check.RunAsync(new InstallContext(), CancellationToken.None);

        Assert.Equal(CheckStatus.Failed, result.Status);
        Assert.Equal(CheckSeverity.Error, result.Severity);
        Assert.Empty(runner.Calls);
    }

    [Fact]
    public async Task Git_nonzero_exit_reports_failed()
    {
        MakeGitExe();
        var runner = new FakeProcessRunner((1, "some error"));
        var check = new GitCheck(runner, pathOverride: _dir, pathExtOverride: ".exe");

        var result = await check.RunAsync(new InstallContext(), CancellationToken.None);

        Assert.Equal(CheckStatus.Failed, result.Status);
    }
}
