// Copyright 2012-2026 The NATS Authors // Licensed under the Apache License, Version 2.0 using Shouldly; using ZB.MOM.NatsNet.Server; namespace ZB.MOM.NatsNet.Server.Tests.JetStream; public sealed class JetStreamFileStoreTests { [Fact] public void StoreMsg_LoadAndPurge_ShouldRoundTrip() { var root = Path.Combine(Path.GetTempPath(), $"fs-{Guid.NewGuid():N}"); Directory.CreateDirectory(root); try { var fs = NewStore(root); var (seq1, _) = fs.StoreMsg("foo", [1], [2, 3], 0); var (seq2, _) = fs.StoreMsg("bar", null, [4, 5], 0); seq1.ShouldBe(1UL); seq2.ShouldBe(2UL); fs.State().Msgs.ShouldBe(2UL); var msg = fs.LoadMsg(1, null); msg.ShouldNotBeNull(); msg!.Subject.ShouldBe("foo"); fs.SubjectForSeq(2).Subject.ShouldBe("bar"); fs.SubjectsTotals(string.Empty).Count.ShouldBe(2); var (removed, remErr) = fs.RemoveMsg(1); removed.ShouldBeTrue(); remErr.ShouldBeNull(); fs.State().Msgs.ShouldBe(1UL); var (purged, purgeErr) = fs.Purge(); purgeErr.ShouldBeNull(); purged.ShouldBe(1UL); fs.State().Msgs.ShouldBe(0UL); var (snapshot, snapErr) = fs.Snapshot(TimeSpan.FromSeconds(1), includeConsumers: false, checkMsgs: false); snapErr.ShouldBeNull(); snapshot.ShouldNotBeNull(); snapshot!.Reader.ShouldNotBeNull(); var (total, reported, utilErr) = fs.Utilization(); utilErr.ShouldBeNull(); total.ShouldBe(reported); fs.Stop(); } finally { Directory.Delete(root, recursive: true); } } private static JetStreamFileStore NewStore(string root) { return new JetStreamFileStore( new FileStoreConfig { StoreDir = root }, new FileStreamInfo { Created = DateTime.UtcNow, Config = new StreamConfig { Name = "S", Storage = StorageType.FileStorage, Subjects = ["foo", "bar"], }, }); } }