using NATS.Server.JetStream.Storage;

namespace NATS.Server.JetStream.Tests.JetStream;

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

        try
        {
            await using (var store = new FileStore(options))
            {
                await store.AppendAsync("orders.created", "a"u8.ToArray(), default);
                await store.AppendAsync("orders.created", "b"u8.ToArray(), default);
                await store.RemoveAsync(2, default);
            }

            await using var reopened = new FileStore(options);
            var state = await reopened.GetStateAsync(default);
            state.Messages.ShouldBe((ulong)1);
            state.LastSeq.ShouldBe((ulong)1);

            var msg1 = await reopened.LoadAsync(1, default);
            msg1.ShouldNotBeNull();
            msg1.Subject.ShouldBe("orders.created");
        }
        finally
        {
            if (Directory.Exists(dir))
                Directory.Delete(dir, recursive: true);
        }
    }
}
