using Microsoft.AspNetCore.Http;
using StellaOps.Authority.RateLimiting;
using Xunit;

namespace StellaOps.Authority.Tests.RateLimiting;

public class AuthorityRateLimiterMetadataAccessorTests
{
    [Fact]
    public void SetClientId_UpdatesFeatureMetadata()
    {
        var context = new DefaultHttpContext();
        var feature = new AuthorityRateLimiterFeature(new AuthorityRateLimiterMetadata());
        context.Features.Set<IAuthorityRateLimiterFeature>(feature);

        var accessor = new AuthorityRateLimiterMetadataAccessor(new HttpContextAccessor { HttpContext = context });

        accessor.SetClientId("client-123");
        accessor.SetTag("custom", "tag");
        accessor.SetSubjectId("subject-1");

        var metadata = accessor.GetMetadata();
        Assert.NotNull(metadata);
        Assert.Equal("client-123", metadata!.ClientId);
        Assert.Equal("subject-1", metadata.SubjectId);
        Assert.Equal("client-123", metadata.Tags["authority.client_id"]);
        Assert.Equal("subject-1", metadata.Tags["authority.subject_id"]);
        Assert.Equal("tag", metadata.Tags["custom"]);
    }
}
