using System.Reflection; using ErsatzTV.Application.MediaSources; using ErsatzTV.Controllers.Api; using ErsatzTV.Core.Api.MediaSources; using ErsatzTV.Core.Domain; using MediatR; using Microsoft.AspNetCore.Mvc.Routing; using NSubstitute; using NUnit.Framework; using Shouldly; namespace ErsatzTV.Tests.Controllers; [TestFixture] public class MediaSourcesControllerTests { private MediaSourcesController _controller = null!; private IMediator _mediator = null!; [SetUp] public void SetUp() { _mediator = Substitute.For(); _controller = new MediaSourcesController(_mediator); } [Test] public void Controller_Should_Expose_Idiomatic_Rest_Route() { MethodInfo action = typeof(MediaSourcesController).GetMethod(nameof(MediaSourcesController.GetAll)) ?? throw new AssertionException("Missing action GetAll"); HttpMethodAttribute attribute = action.GetCustomAttributes(inherit: true).Single(); attribute.HttpMethods.ShouldContain("GET"); attribute.Template.ShouldBe("/api/v1/media-sources"); attribute.Name.ShouldBe("GetMediaSources"); } [Test] public void CollectionsScanStatus_Should_Expose_Idiomatic_Rest_Route() { MethodInfo action = typeof(MediaSourcesController) .GetMethod(nameof(MediaSourcesController.GetCollectionsScanStatus)) ?? throw new AssertionException("Missing action GetCollectionsScanStatus"); HttpMethodAttribute attribute = action.GetCustomAttributes(inherit: true).Single(); attribute.HttpMethods.ShouldContain("GET"); attribute.Template.ShouldBe("/api/v1/media-sources/collections-scan-status"); attribute.Name.ShouldBe("GetCollectionsScanStatus"); } [Test] public async Task GetCollectionsScanStatus_Should_Return_Results_From_Mediator() { var expected = new List { new("plex"), new("emby") }; _mediator.Send(Arg.Any(), Arg.Any()) .Returns(expected); List result = await _controller.GetCollectionsScanStatus(CancellationToken.None); result.ShouldBe(expected); } [Test] public async Task GetAll_Should_Return_Results_From_Mediator() { var expected = new List { new( 1, "Local", "Local", null, [new MediaSourceLibraryResponseModel(10, "Movies", LibraryMediaKind.Movies, null, 3)]) }; _mediator.Send(Arg.Any(), Arg.Any()) .Returns(expected); List result = await _controller.GetAll(CancellationToken.None); result.ShouldBe(expected); } }