using Microsoft.Extensions.Logging.Abstractions;
using NATS.Server.Configuration;
using NATS.Server.Monitoring;

namespace NATS.Server.Tests;

public class JetStreamMonitoringParityTests
{
    [Fact]
    public async Task Jsz_and_varz_include_expanded_runtime_fields()
    {
        var options = new NatsOptions
        {
            Host = "127.0.0.1",
            Port = 0,
            JetStream = new JetStreamOptions
            {
                StoreDir = Path.Combine(Path.GetTempPath(), $"nats-js-monitor-{Guid.NewGuid():N}"),
                MaxMemoryStore = 1024 * 1024,
                MaxFileStore = 10 * 1024 * 1024,
            },
        };

        var server = new NatsServer(options, NullLoggerFactory.Instance);
        using var cts = new CancellationTokenSource();
        _ = server.StartAsync(cts.Token);
        await server.WaitForReadyAsync();

        _ = server.JetStreamApiRouter!.Route("$JS.API.STREAM.CREATE.ORDERS", "{\"subjects\":[\"orders.*\"]}"u8);
        _ = server.JetStreamApiRouter!.Route("$JS.API.CONSUMER.CREATE.ORDERS.PULL", "{\"durable_name\":\"PULL\",\"filter_subject\":\"orders.created\"}"u8);

        var jsz = new JszHandler(server, options).Build();
        jsz.Streams.ShouldBeGreaterThanOrEqualTo(1);
        jsz.Consumers.ShouldBeGreaterThanOrEqualTo(1);
        jsz.ApiTotal.ShouldBeGreaterThanOrEqualTo((ulong)0);

        var varz = await new VarzHandler(server, options).HandleVarzAsync();
        varz.JetStream.Stats.Api.Total.ShouldBeGreaterThanOrEqualTo((ulong)0);

        await cts.CancelAsync();
        server.Dispose();
    }
}
