using NATS.Server.JetStream.Storage;

namespace NATS.Server.JetStream.Tests.JetStream.Storage;

public sealed class FileStoreOptimizationGuardTests
{
    [Fact]
    public async Task PurgeEx_updates_last_by_subject_after_recovery()
    {
        var dir = Directory.CreateTempSubdirectory();

        await using (var store = new FileStore(new FileStoreOptions { Directory = dir.FullName }))
        {
            store.StoreMsg("events.a", null, "one"u8.ToArray(), 0L);
            store.StoreMsg("events.a", null, "two"u8.ToArray(), 0L);
            store.StoreMsg("events.b", null, "other"u8.ToArray(), 0L);
            store.PurgeEx("events.a", 0, 1);
            await store.FlushAllPending();
        }

        await using var recovered = new FileStore(new FileStoreOptions { Directory = dir.FullName });
        var last = await recovered.LoadLastBySubjectAsync("events.a", default);

        last.ShouldNotBeNull();
        last.Sequence.ShouldBe(2UL);
        last.Payload.ToArray().ShouldBe("two"u8.ToArray());
    }
}
