// 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 DiskAvailabilityTests
{
    private const long JetStreamMaxStoreDefault = 1L * 1024 * 1024 * 1024 * 1024;

    [Fact]
    public void DiskAvailable_MissingDirectory_ShouldCreateDirectory()
    {
        var root = Path.Combine(Path.GetTempPath(), $"disk-avail-{Guid.NewGuid():N}");
        var target = Path.Combine(root, "nested");
        try
        {
            Directory.Exists(target).ShouldBeFalse();

            var available = DiskAvailability.DiskAvailable(target);

            Directory.Exists(target).ShouldBeTrue();
            available.ShouldBeGreaterThan(0L);
        }
        finally
        {
            if (Directory.Exists(root))
                Directory.Delete(root, recursive: true);
        }
    }

    [Fact]
    public void DiskAvailable_InvalidPath_ShouldReturnFallback()
    {
        var available = DiskAvailability.DiskAvailable("\0");
        available.ShouldBe(JetStreamMaxStoreDefault);
    }

    [Fact]
    public void Check_ShouldUseDiskAvailableThreshold()
    {
        var root = Path.Combine(Path.GetTempPath(), $"disk-check-{Guid.NewGuid():N}");
        try
        {
            // Use deterministic thresholds to avoid flaky comparisons between
            // two separate filesystem snapshots.
            DiskAvailability.Check(root, 0).ShouldBeTrue();
            DiskAvailability.Check(root, long.MaxValue).ShouldBeFalse();
        }
        finally
        {
            if (Directory.Exists(root))
                Directory.Delete(root, recursive: true);
        }
    }
}
