using GmRelay.Shared.Features.Sessions.CreateSession;
using GmRelay.Shared.Platform;

namespace GmRelay.Bot.Tests.Platform;

public sealed class ScheduleMessageUpdateLockTests
{
    [Fact]
    public async Task AcquireAsync_ShouldSerializeSameScheduleMessage()
    {
        var updateLock = new ScheduleMessageUpdateLock();
        var message = CreateMessage("message-1");

        var first = await updateLock.AcquireAsync(message, CancellationToken.None);
        var secondTask = updateLock.AcquireAsync(message, CancellationToken.None).AsTask();

        Assert.False(secondTask.IsCompleted);

        await first.DisposeAsync();
        var second = await secondTask.WaitAsync(TimeSpan.FromSeconds(1));
        await second.DisposeAsync();
    }

    [Fact]
    public async Task AcquireAsync_ShouldNotBlockDifferentScheduleMessages()
    {
        var updateLock = new ScheduleMessageUpdateLock();

        var first = await updateLock.AcquireAsync(CreateMessage("message-1"), CancellationToken.None);
        var secondTask = updateLock.AcquireAsync(CreateMessage("message-2"), CancellationToken.None).AsTask();

        Assert.True(secondTask.IsCompleted);

        await first.DisposeAsync();
        var second = await secondTask;
        await second.DisposeAsync();
    }

    private static PlatformMessageRef CreateMessage(string messageId) =>
        new(PlatformKind.Discord, "guild-1", null, messageId);
}
