using System.Reflection; using ErsatzTV.Application.Logs; using ErsatzTV.Controllers.Api; using ErsatzTV.Core.Api.Logs; using MediatR; using Microsoft.AspNetCore.Mvc.Routing; using NSubstitute; using NUnit.Framework; using Serilog.Events; using Shouldly; namespace ErsatzTV.Tests.Controllers; [TestFixture] public class LogsControllerTests { private LogsController _controller = null!; private IMediator _mediator = null!; [SetUp] public void SetUp() { _mediator = Substitute.For(); _controller = new LogsController(_mediator); } [Test] public void Controller_Should_Expose_Idiomatic_Rest_Route() { MethodInfo action = typeof(LogsController).GetMethod(nameof(LogsController.GetLogs)) ?? throw new AssertionException("Missing action GetLogs"); HttpMethodAttribute attribute = action.GetCustomAttributes(inherit: true).Single(); attribute.HttpMethods.ShouldContain("GET"); attribute.Template.ShouldBe("/api/v1/logs"); attribute.Name.ShouldBe("GetLogs"); } [Test] public async Task GetLogs_Should_Clamp_Paging_And_Send_Query() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns(new PagedLogEntriesViewModel(0, [])); await _controller.GetLogs(-1, 500, "boom", cancellationToken: CancellationToken.None); await _mediator.Received(1).Send( Arg.Is(q => q.PageNum == 0 && q.PageSize == 100 && q.Filter == "boom"), Arg.Any()); } [Test] public async Task GetLogs_Should_Default_To_Timestamp_Descending() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns(new PagedLogEntriesViewModel(0, [])); await _controller.GetLogs(cancellationToken: CancellationToken.None); await _mediator.Received(1).Send( Arg.Is(q => q.SortDescending == true && SelectsTimestamp(q.SortExpression)), Arg.Any()); } [Test] public async Task GetLogs_Should_Sort_By_Level_Ascending_When_Requested() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns(new PagedLogEntriesViewModel(0, [])); await _controller.GetLogs(sortField: "level", sortDirection: "asc", cancellationToken: CancellationToken.None); await _mediator.Received(1).Send( Arg.Is(q => q.SortDescending == false && SelectsLevel(q.SortExpression)), Arg.Any()); } [Test] public async Task GetLogs_Should_Reject_Unknown_Sort_Field_And_Fall_Back_To_Timestamp() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns(new PagedLogEntriesViewModel(0, [])); await _controller.GetLogs(sortField: "message; DROP TABLE", cancellationToken: CancellationToken.None); await _mediator.Received(1).Send( Arg.Is(q => SelectsTimestamp(q.SortExpression)), Arg.Any()); } [Test] public async Task GetLogs_Should_Fall_Back_To_Descending_For_Unknown_Direction() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns(new PagedLogEntriesViewModel(0, [])); await _controller.GetLogs(sortDirection: "sideways", cancellationToken: CancellationToken.None); await _mediator.Received(1).Send( Arg.Is(q => q.SortDescending == true), Arg.Any()); } private static bool SelectsTimestamp(System.Linq.Expressions.Expression> expr) { var sample = new LogEntryViewModel(DateTimeOffset.UnixEpoch.AddDays(1), LogEventLevel.Error, "m"); return Equals(expr.Compile()(sample), sample.Timestamp); } private static bool SelectsLevel(System.Linq.Expressions.Expression> expr) { var sample = new LogEntryViewModel(DateTimeOffset.UnixEpoch.AddDays(1), LogEventLevel.Error, "m"); return Equals(expr.Compile()(sample), sample.Level); } [Test] public async Task GetLogs_Should_Map_Entries_To_Response_Model() { var entries = new List { new(DateTimeOffset.UnixEpoch, LogEventLevel.Warning, "uh oh"), new(DateTimeOffset.UnixEpoch.AddMinutes(1), LogEventLevel.Information, "all good") }; _mediator.Send(Arg.Any(), Arg.Any()) .Returns(new PagedLogEntriesViewModel(2, entries)); PagedLogEntriesResponseModel result = await _controller.GetLogs( cancellationToken: CancellationToken.None); result.TotalCount.ShouldBe(2); result.Page.ShouldBe( [ new LogEntryResponseModel(DateTimeOffset.UnixEpoch, "Warning", "uh oh"), new LogEntryResponseModel(DateTimeOffset.UnixEpoch.AddMinutes(1), "Information", "all good") ]); } [Test] public async Task GetLogs_Should_Return_Empty_Page_When_None_Exist() { _mediator.Send(Arg.Any(), Arg.Any()) .Returns(new PagedLogEntriesViewModel(0, [])); PagedLogEntriesResponseModel result = await _controller.GetLogs( cancellationToken: CancellationToken.None); result.TotalCount.ShouldBe(0); result.Page.ShouldBeEmpty(); } }