Files
ersatztv/ErsatzTV.Tests/Extensions/ApiResultsTests.cs
T
timothyandClaude Opus 4.8 ef2bd65c27
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 10s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 10s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 1m12s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 3m4s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m17s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 10m36s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
feat(api): #286 — mount the whole /api surface at /api/v1
Version every /api route to /api/v1 (251 controller routes + ~24 Location
headers + the scanner callback URL + the Startup request-log literal),
uniform across the machine API, auth, scanner and scripted-build surfaces.

Add ApiVersionRewriteMiddleware: a legacy unversioned /api/* request is
rewritten (NOT redirected) to /api/v1/* in-pipeline — method, body, auth
headers and query survive — carrying RFC 8594 Deprecation/Sunset headers,
so curl / the future MCP server / bookmarks keep working. An already-
versioned path passes through; a future /api/v2 is never forced to v1.

Standardize the route convention (leading-slash absolute route per method,
no class-[Route] — except the two Scanner/Scripted controllers whose ~all
actions share a parametrized {id} prefix), enforced by ApiRouteVersioningTests
(^/api/v\d+/ over the whole Controllers.Api surface; browser-nav
/auth/oidc/login is out of scope).

Regenerate v1.json (160 paths, all /api/v1)/endpoint-index/v1.d.ts; sweep 945
SPA request literals + the test mocks (regex + positional URL parsers). /api/v1
is additive-only after freeze; the legacy-rewrite shim sunsets in ~2 releases
(owner decision) with removal tracked as a Phase-3 follow-up.

Docs: decisions.md 2026-07-13, api-conventions §1/§9, rest-api/spa-conventions/
blazor-route-parity/e2e-local/domain-model.

fixes #286
refs #197

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

177 lines
5.8 KiB
C#

using ErsatzTV.Core;
using ErsatzTV.Core.Errors;
using ErsatzTV.Extensions;
using LanguageExt;
using static LanguageExt.Prelude;
using Microsoft.AspNetCore.Mvc;
using NUnit.Framework;
using Shouldly;
namespace ErsatzTV.Tests.Extensions;
[TestFixture]
public class ApiResultsTests
{
[Test]
public void ToErrorResult_Should_Map_NotFoundError_To_404()
{
IActionResult result = new NotFoundError("missing").ToErrorResult();
result.ShouldBeOfType<NotFoundObjectResult>().StatusCode.ShouldBe(404);
}
[Test]
public void ToErrorResult_Should_Map_PreconditionFailedError_To_412()
{
IActionResult result = new PreconditionFailedError("stale").ToErrorResult();
result.ShouldBeOfType<ObjectResult>().StatusCode.ShouldBe(412);
}
[Test]
public void ToErrorResult_Should_Return_ProblemDetails_For_NotFoundError()
{
IActionResult result = new NotFoundError("missing").ToErrorResult();
var notFound = result.ShouldBeOfType<NotFoundObjectResult>();
var problemDetails = notFound.Value.ShouldBeOfType<ProblemDetails>();
problemDetails.Status.ShouldBe(404);
problemDetails.Title.ShouldBe("Resource not found");
problemDetails.Detail.ShouldBe("missing");
}
[Test]
public void ToErrorResult_Should_Map_LockedError_To_409()
{
IActionResult result = new LockedError("locked").ToErrorResult();
result.ShouldBeOfType<ConflictObjectResult>().StatusCode.ShouldBe(409);
}
[Test]
public void ToErrorResult_Should_Return_ProblemDetails_For_LockedError()
{
IActionResult result = new LockedError("locked").ToErrorResult();
var conflict = result.ShouldBeOfType<ConflictObjectResult>();
var problemDetails = conflict.Value.ShouldBeOfType<ProblemDetails>();
problemDetails.Status.ShouldBe(409);
problemDetails.Detail.ShouldBe("locked");
}
[Test]
public void ToErrorResult_Should_Map_Other_Error_To_422()
{
IActionResult result = BaseError.New("bad").ToErrorResult();
result.ShouldBeOfType<UnprocessableEntityObjectResult>().StatusCode.ShouldBe(422);
}
[Test]
public void ToErrorResult_Should_Return_ProblemDetails_For_Validation_Error()
{
IActionResult result = BaseError.New("bad").ToErrorResult();
var unprocessable = result.ShouldBeOfType<UnprocessableEntityObjectResult>();
var problemDetails = unprocessable.Value.ShouldBeOfType<ProblemDetails>();
problemDetails.Status.ShouldBe(422);
problemDetails.Title.ShouldBe("Validation failed");
problemDetails.Detail.ShouldBe("bad");
}
[Test]
public void ToCreatedResult_Should_Return_201_With_Location_And_Body()
{
Either<BaseError, int> either = Right<BaseError, int>(5);
IActionResult result = either.ToCreatedResult(id => $"/api/v1/channels/{id}", id => $"body-{id}");
var created = result.ShouldBeOfType<CreatedResult>();
created.StatusCode.ShouldBe(201);
created.Location.ShouldBe("/api/v1/channels/5");
created.Value.ShouldBe("body-5");
}
[Test]
public void ToCreatedResult_Should_Map_NotFoundError_To_404()
{
Either<BaseError, int> either = Left<BaseError, int>(new NotFoundError("nope"));
IActionResult result = either.ToCreatedResult(id => $"/api/v1/channels/{id}", id => id);
result.ShouldBeOfType<NotFoundObjectResult>();
}
[Test]
public void ToUpdatedResult_Should_Return_200_On_Right()
{
Either<BaseError, string> either = Right<BaseError, string>("vm");
IActionResult result = either.ToUpdatedResult();
var ok = result.ShouldBeOfType<OkObjectResult>();
ok.Value.ShouldBe("vm");
}
[Test]
public void ToUpdatedResult_Should_Return_404_For_NotFoundError()
{
Either<BaseError, string> either = Left<BaseError, string>(new NotFoundError("missing"));
IActionResult result = either.ToUpdatedResult();
result.ShouldBeOfType<NotFoundObjectResult>();
}
[Test]
public void ToUpdatedResult_Should_Return_422_For_Other_Error()
{
Either<BaseError, string> either = Left<BaseError, string>(BaseError.New("bad"));
IActionResult result = either.ToUpdatedResult();
result.ShouldBeOfType<UnprocessableEntityObjectResult>();
}
[Test]
public void ToDeletedResult_Should_Return_204_On_Right()
{
Either<BaseError, Unit> either = Right<BaseError, Unit>(Unit.Default);
IActionResult result = either.ToDeletedResult();
result.ShouldBeOfType<NoContentResult>().StatusCode.ShouldBe(204);
}
[Test]
public void ToDeletedResult_Should_Return_404_For_NotFoundError()
{
Either<BaseError, Unit> either = Left<BaseError, Unit>(new NotFoundError("missing"));
IActionResult result = either.ToDeletedResult();
result.ShouldBeOfType<NotFoundObjectResult>();
}
[Test]
public void ToGetResult_Should_Return_200_For_Some()
{
Option<string> option = "value";
IActionResult result = option.ToGetResult();
result.ShouldBeOfType<OkObjectResult>().Value.ShouldBe("value");
}
[Test]
public void ToGetResult_Should_Return_404_For_None()
{
Option<string> option = Option<string>.None;
IActionResult result = option.ToGetResult();
result.ShouldBeOfType<NotFoundObjectResult>();
}
[Test]
public void ToGetResult_Should_Return_ProblemDetails_For_None()
{
Option<string> option = Option<string>.None;
IActionResult result = option.ToGetResult();
var notFound = result.ShouldBeOfType<NotFoundObjectResult>();
var problemDetails = notFound.Value.ShouldBeOfType<ProblemDetails>();
problemDetails.Status.ShouldBe(404);
problemDetails.Title.ShouldBe("Resource not found");
}
}