using NATS.Client.Core;

namespace NATS.E2E.Tests.Infrastructure;

public sealed class WebSocketServerFixture : IAsyncLifetime
{
    private NatsServerProcess _server = null!;

    public int Port => _server.Port;
    public int WsPort { get; private set; }

    public async Task InitializeAsync()
    {
        WsPort = NatsServerProcess.AllocateFreePort();

        var config = $$"""
            websocket {
                listen: 127.0.0.1:{{WsPort}}
                no_tls: true
            }
            """;

        _server = NatsServerProcess.WithConfig(config);
        await _server.StartAsync();
    }

    public async Task DisposeAsync()
    {
        await _server.DisposeAsync();
    }

    public NatsConnection CreateNatsClient()
        => new(new NatsOpts { Url = $"nats://127.0.0.1:{Port}" });
}

[CollectionDefinition("E2E-WebSocket")]
public class WebSocketCollection : ICollectionFixture<WebSocketServerFixture>;
