Files
ersatztv/ErsatzTV.Tests/Controllers/SearchControllerTests.cs
T
timothy 819e751cab feat(api): playlist add-items + search all-items endpoints (#208 #209)
- POST /api/playlists/{id:int}/items wraps the existing AddItemsToPlaylist
  command (mirrors CollectionController.AddItems); controller pre-checks
  playlist existence for a real 404, and the handler now rejects adds to
  system (generated) playlists, matching the guard already applied to
  rename/delete/replace-items so the Blazor path gets the same protection.
- GET /api/search/all-items wraps the existing QuerySearchIndexAllItems
  query, returning a new SearchResultAllItemsResponseModel (never expose
  the VM directly) so the SPA's shared "add all to collection/playlist"
  component can materialize ids before calling the add endpoints, same
  two-step flow Blazor's Search.razor already uses.
- Show-detail DTO check: ShowDetailResponseModel already exposes
  libraryId, title, and mediaSourceKind (serialized as a string enum via
  the global StringEnumConverter) - no changes needed.

Adds controller tests (route table + per-action) for both endpoints and
regenerates the OpenAPI document, endpoint index, and SPA client types.
2026-07-09 23:56:50 +02:00

179 lines
6.4 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 void Controller_Should_Expose_SearchAllItems_Route_With_Stable_Operation_Name()
{
MethodInfo action = typeof(SearchController).GetMethod(nameof(SearchController.SearchAllItems))
?? throw new AssertionException($"Missing action {nameof(SearchController.SearchAllItems)}");
var attribute = action.GetCustomAttributes<HttpGetAttribute>().Single();
attribute.Template.ShouldBe("/api/search/all-items");
attribute.Name.ShouldBe("SearchAllItems");
}
[Test]
public async Task SearchAllItems_Should_Return_422_For_Empty_Query()
{
IActionResult result = await _controller.SearchAllItems(" ", CancellationToken.None);
var unprocessable = result.ShouldBeOfType<UnprocessableEntityObjectResult>();
unprocessable.StatusCode.ShouldBe(422);
await _mediator.DidNotReceive().Send(Arg.Any<QuerySearchIndexAllItems>(), Arg.Any<CancellationToken>());
}
[Test]
public async Task SearchAllItems_Should_Map_Id_Lists()
{
_mediator.Send(Arg.Any<QuerySearchIndexAllItems>(), Arg.Any<CancellationToken>())
.Returns(new SearchResultAllItemsViewModel(
[1, 2],
[3],
[],
[],
[],
[],
[],
[],
[],
[]));
IActionResult result = await _controller.SearchAllItems("star", CancellationToken.None);
var body = result.ShouldBeOfType<OkObjectResult>().Value
.ShouldBeOfType<SearchResultAllItemsResponseModel>();
body.MovieIds.ShouldBe(new List<int> { 1, 2 });
body.ShowIds.ShouldBe(new List<int> { 3 });
await _mediator.Received(1).Send(
Arg.Is<QuerySearchIndexAllItems>(q => q.Query == "star"),
Arg.Any<CancellationToken>());
}
[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);
}
}