using ClaudeDo.Installer.Core;
using ClaudeDo.Installer.Steps;

namespace ClaudeDo.Installer.Tests;

public sealed class WriteInstallManifestStepTests : IDisposable
{
    private readonly string _installDir;

    public WriteInstallManifestStepTests()
    {
        _installDir = Path.Combine(Path.GetTempPath(), "ClaudeDoWriteManifest-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(_installDir);
    }

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

    [Fact]
    public async Task Writes_Manifest_WithAllFields()
    {
        var ctx = new InstallContext
        {
            InstallDirectory = _installDir,
            InstalledVersion = "0.2.0",
        };

        var step = new WriteInstallManifestStep();
        var result = await step.ExecuteAsync(ctx, new Progress<string>(_ => { }), CancellationToken.None);

        Assert.True(result.Success);
        var manifest = InstallManifestStore.TryRead(_installDir);
        Assert.NotNull(manifest);
        Assert.Equal("0.2.0", manifest!.Version);
        Assert.Equal(_installDir, manifest.InstallDir);
        Assert.Equal(Path.Combine(_installDir, "worker"), manifest.WorkerDir);
    }

    [Fact]
    public async Task Fails_When_InstalledVersion_Missing()
    {
        var ctx = new InstallContext { InstallDirectory = _installDir };  // no version set

        var step = new WriteInstallManifestStep();
        var result = await step.ExecuteAsync(ctx, new Progress<string>(_ => { }), CancellationToken.None);

        Assert.False(result.Success);
    }
}
