using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using StellaOps.Scanner.Sbomer.BuildXPlugin.Cas;
using StellaOps.Scanner.Sbomer.BuildXPlugin.Tests.TestUtilities;
using Xunit;

namespace StellaOps.Scanner.Sbomer.BuildXPlugin.Tests.Cas;

public sealed class LocalCasClientTests
{
    [Fact]
    public async Task VerifyWriteAsync_WritesProbeObject()
    {
        await using var temp = new TempDirectory();
        var client = new LocalCasClient(new LocalCasOptions
        {
            RootDirectory = temp.Path,
            Algorithm = "sha256"
        });

        var result = await client.VerifyWriteAsync(CancellationToken.None);

        Assert.Equal("sha256", result.Algorithm);
        Assert.True(File.Exists(result.Path));

        var bytes = await File.ReadAllBytesAsync(result.Path);
        Assert.Equal("stellaops-buildx-probe"u8.ToArray(), bytes);

        var expectedDigest = Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant();
        Assert.Equal(expectedDigest, result.Digest);
    }
}
