Files
ersatztv/ErsatzTV.Tests/Controllers/SearchControllerTests.cs
T
timothyandClaude Fable 5 6b5bc8e29a feat(api): REST endpoints for blocks, block groups, items, preview + scheduling search pickers
Adds BlockController (block groups + blocks CRUD, items GET, full replace,
non-persisting preview) mirroring ScheduleController, plus four scheduling
search picker endpoints on SearchController (collections, television shows,
television seasons, smart collections). Response DTOs in ErsatzTV.Core/Api/
Scheduling; request DTOs with ToCommand index auto-assignment.

#144 S1 (#162)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 17:42:40 +02:00

131 lines
4.5 KiB
C#

using System.Reflection;
using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Application.MediaItems;
using ErsatzTV.Application.Search;
using ErsatzTV.Controllers.Api;
using ErsatzTV.Core.Api.Scheduling;
using ErsatzTV.Core.Api.Search;
using ErsatzTV.Core.Domain;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
namespace ErsatzTV.Tests.Controllers;
[TestFixture]
public class SearchControllerTests
{
private SearchController _controller = null!;
private IMediator _mediator = null!;
[SetUp]
public void SetUp()
{
_mediator = Substitute.For<IMediator>();
_controller = new SearchController(_mediator);
}
[Test]
public void Controller_Should_Expose_Search_Route_With_Stable_Operation_Name()
{
MethodInfo action = typeof(SearchController).GetMethod(nameof(SearchController.Search))
?? throw new AssertionException($"Missing action {nameof(SearchController.Search)}");
var attribute = action.GetCustomAttributes<HttpGetAttribute>().Single();
attribute.Template.ShouldBe("/api/search");
attribute.Name.ShouldBe("Search");
}
[Test]
public async Task Search_Should_Return_422_For_Empty_Query()
{
IActionResult result = await _controller.Search(" ", 50, CancellationToken.None);
var unprocessable = result.ShouldBeOfType<UnprocessableEntityObjectResult>();
unprocessable.StatusCode.ShouldBe(422);
await _mediator.DidNotReceive().Send(Arg.Any<GetSearchResults>(), Arg.Any<CancellationToken>());
}
[Test]
public async Task Search_Should_Clamp_PageSize_And_Send_Query()
{
_mediator.Send(Arg.Any<GetSearchResults>(), Arg.Any<CancellationToken>())
.Returns(EmptyResults());
IActionResult result = await _controller.Search("star", 500, CancellationToken.None);
result.ShouldBeOfType<OkObjectResult>();
await _mediator.Received(1).Send(
Arg.Is<GetSearchResults>(q => q.Query == "star" && q.PageSize == 100),
Arg.Any<CancellationToken>());
}
[Test]
public async Task Search_Should_Return_Grouped_Results()
{
SearchResultsResponseModel results = EmptyResults();
_mediator.Send(Arg.Any<GetSearchResults>(), Arg.Any<CancellationToken>()).Returns(results);
IActionResult result = await _controller.Search("star", 50, CancellationToken.None);
var ok = result.ShouldBeOfType<OkObjectResult>();
ok.Value.ShouldBe(results);
}
[Test]
public async Task SearchCollections_Should_Map_To_Picker_Options()
{
_mediator.Send(Arg.Any<SearchCollections>(), Arg.Any<CancellationToken>())
.Returns(new List<MediaCollectionViewModel>
{
new(CollectionType.Collection, 3, "Movies", false, new MediaItemState())
});
List<SchedulingPickerOptionResponseModel> result =
await _controller.SearchCollections("mov", CancellationToken.None);
result.ShouldHaveSingleItem();
result[0].Id.ShouldBe(3);
result[0].Name.ShouldBe("Movies");
await _mediator.Received(1).Send(
Arg.Is<SearchCollections>(q => q.Query == "mov"),
Arg.Any<CancellationToken>());
}
[Test]
public async Task SearchTelevisionShows_Should_Map_MediaItemId_To_Option_Id()
{
_mediator.Send(Arg.Any<SearchTelevisionShows>(), Arg.Any<CancellationToken>())
.Returns(new List<NamedMediaItemViewModel> { new(42, "The Show") });
List<SchedulingPickerOptionResponseModel> result =
await _controller.SearchTelevisionShows("show", CancellationToken.None);
result.ShouldHaveSingleItem();
result[0].Id.ShouldBe(42);
result[0].Name.ShouldBe("The Show");
}
[Test]
public async Task SearchSmartCollections_Should_Map_To_Picker_Options()
{
_mediator.Send(Arg.Any<SearchSmartCollections>(), Arg.Any<CancellationToken>())
.Returns(new List<SmartCollectionViewModel> { new(9, "Recent", "query") });
List<SchedulingPickerOptionResponseModel> result =
await _controller.SearchSmartCollections("rec", CancellationToken.None);
result.ShouldHaveSingleItem();
result[0].Id.ShouldBe(9);
}
private static SearchResultsResponseModel EmptyResults()
{
var empty = new SearchResultGroupResponseModel(0, []);
return new SearchResultsResponseModel(empty, empty, empty, empty, empty, empty, empty, empty, empty, empty);
}
}