using NATS.Server.JetStream.Storage;
using System.Text;

namespace NATS.Server.Tests;

public class JetStreamFileStoreDurabilityParityTests
{
    [Fact]
    public async Task File_store_recovers_block_index_map_after_restart_without_full_log_scan()
    {
        var dir = Path.Combine(Path.GetTempPath(), $"nats-js-fs-durable-{Guid.NewGuid():N}");
        var options = new FileStoreOptions
        {
            Directory = dir,
            BlockSizeBytes = 256,
        };

        try
        {
            await using (var store = new FileStore(options))
            {
                for (var i = 0; i < 1000; i++)
                    await store.AppendAsync("orders.created", Encoding.UTF8.GetBytes($"payload-{i}"), default);
            }

            // Block-based storage: .blk files should be present on disk.
            Directory.GetFiles(dir, "*.blk").Length.ShouldBeGreaterThan(0);

            await using var reopened = new FileStore(options);
            var state = await reopened.GetStateAsync(default);
            state.Messages.ShouldBe((ulong)1000);
            reopened.BlockCount.ShouldBeGreaterThan(1);
        }
        finally
        {
            if (Directory.Exists(dir))
                Directory.Delete(dir, recursive: true);
        }
    }
}
