using System.Collections.Immutable;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using StellaOps.Vexer.Core;
using StellaOps.Vexer.Export;
using System.IO.Abstractions.TestingHelpers;

namespace StellaOps.Vexer.Export.Tests;

public sealed class FileSystemArtifactStoreTests
{
    [Fact]
    public async Task SaveAsync_WritesArtifactToDisk()
    {
        var fs = new MockFileSystem();
        var options = Options.Create(new FileSystemArtifactStoreOptions { RootPath = "/exports" });
        var store = new FileSystemArtifactStore(options, NullLogger<FileSystemArtifactStore>.Instance, fs);

        var content = new byte[] { 1, 2, 3 };
        var artifact = new VexExportArtifact(
            new VexContentAddress("sha256", "deadbeef"),
            VexExportFormat.Json,
            content,
            ImmutableDictionary<string, string>.Empty);

        var stored = await store.SaveAsync(artifact, CancellationToken.None);

        Assert.Equal(artifact.Content.Length, stored.SizeBytes);
        var filePath = fs.Path.Combine(options.Value.RootPath, stored.Location);
        Assert.True(fs.FileExists(filePath));
        Assert.Equal(content, fs.File.ReadAllBytes(filePath));
    }
}
