Files
ersatztv/ErsatzTV.Tests/Controllers/SearchControllerTests.cs
T
timothyandClaude Opus 4.8 fecf16d3d2 feat(api): #288 wrap raw VMs, nullable-honest DTOs, search pageNum
24 #nullable enable flips across ErsatzTV.Core/Api; Collection/Schedule/
SmartCollection/Resolution VMs wrapped in ResponseModels (Version now
header-only, SPA-verified); pageNum threaded into GET /api/search.

Refs #288 #197

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-12 02:08:12 +02:00

197 lines
7.2 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(" ", 0, 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", 0, 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_Thread_PageNum_And_Clamp_Negative()
{
_mediator.Send(Arg.Any<GetSearchResults>(), Arg.Any<CancellationToken>())
.Returns(EmptyResults());
// A valid page number is threaded through; a negative one clamps to 0.
await _controller.Search("star", 3, 50, CancellationToken.None);
await _mediator.Received(1).Send(
Arg.Is<GetSearchResults>(q => q.Query == "star" && q.PageNum == 3 && q.PageSize == 50),
Arg.Any<CancellationToken>());
await _controller.Search("star", -7, 50, CancellationToken.None);
await _mediator.Received(1).Send(
Arg.Is<GetSearchResults>(q => q.Query == "star" && q.PageNum == 0 && q.PageSize == 50),
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", 0, 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);
}
}