using Heimdall.Web.Services; using Microsoft.Extensions.Logging; using Microsoft.JSInterop; using Shouldly; namespace Heimdall.Web.Tests.Services; public sealed class AuthHelperTests { [Theory] [InlineData(200, true)] [InlineData(204, true)] [InlineData(401, false)] [InlineData(500, false)] [InlineData(0, false)] public async Task SignInAsync_UsesOnlyTheReturnedHttpStatus(int statusCode, bool expected) { var jsRuntime = new StubJsRuntime(statusCode); var result = await AuthHelper.SignInAsync(jsRuntime, new RecordingLogger(), new { accessToken = "synthetic" }); result.ShouldBe(expected); } [Fact] public async Task SignInAsync_LogsInteropFailureTypeWithoutExceptionMessage() { const string privateDetail = "synthetic-private-detail"; var jsRuntime = new StubJsRuntime(new JSException(privateDetail)); var logger = new RecordingLogger(); (await AuthHelper.SignInAsync(jsRuntime, logger, new { accessToken = "synthetic" })).ShouldBeFalse(); var logged = string.Join(" ", logger.Messages); logged.ShouldContain(nameof(JSException)); logged.ShouldNotContain(privateDetail); } private sealed class RecordingLogger : ILogger { public List Messages { get; } = []; public IDisposable? BeginScope(TState state) where TState : notnull => null; public bool IsEnabled(LogLevel logLevel) => true; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) => Messages.Add(formatter(state, exception)); } private sealed class StubJsRuntime : IJSRuntime { private readonly int _statusCode; private readonly Exception? _exception; public StubJsRuntime(int statusCode) => _statusCode = statusCode; public StubJsRuntime(Exception exception) => _exception = exception; public ValueTask InvokeAsync(string identifier, object?[]? args) => InvokeAsync(identifier, CancellationToken.None, args); public ValueTask InvokeAsync(string identifier, CancellationToken cancellationToken, object?[]? args) { if (_exception is not null) return ValueTask.FromException(_exception); return ValueTask.FromResult((TValue)(object)_statusCode); } } }