diff --git a/README.md b/README.md index dbbbd59..342b8c4 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ The one deliberate break from Clean Architecture is that the Presentation projec - Logging using [Serilog](https://github.com/serilog/serilog), with [Seq](https://datalust.co/seq) when running in Docker - Mediator pattern using [Mediator](https://github.com/martinothamar/Mediator) - Validation using [FluentValidation](https://github.com/FluentValidation/FluentValidation) in an Application pipeline behaviour +- Typed tool results published with an output schema and returned as structured content as well as JSON text - Tool errors mapped from Application exceptions by a single call-tool filter, with messages that tell the agent how to recover - Object mapping using [Mapperly](https://mapperly.riok.app) - In-memory data using [Entity Framework Core](https://learn.microsoft.com/ef/core/providers/in-memory/) seeded with [Bogus](https://github.com/bchavez/Bogus) diff --git a/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs b/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs index 3e4bf3d..5e3a61b 100644 --- a/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs +++ b/src/Presentation/Extensions/WebApplicationBuilderExtensions.cs @@ -9,8 +9,8 @@ namespace CleanModelContextProtocol.Presentation.Extensions; using CleanModelContextProtocol.Presentation.Serialization; using Infrastructure; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http.Json; using Microsoft.Extensions.DependencyInjection; +using ModelContextProtocol; using Serilog; [ExcludeFromCodeCoverage] @@ -34,15 +34,12 @@ public static WebApplicationBuilder ConfigureApplicationBuilder(this WebApplicat #region Serialisation - _ = builder.Services.Configure(opt => + var serializerOptions = new JsonSerializerOptions(McpJsonUtilities.DefaultOptions) { - opt.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; - opt.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; - opt.SerializerOptions.PropertyNameCaseInsensitive = true; - opt.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; - opt.SerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); - opt.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); - }); + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + }; + serializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); #endregion Serialisation @@ -85,7 +82,7 @@ A review links one author to one movie with a star rating from 1 (worst) to 5 (b """; }) .WithHttpTransport(options => options.Stateless = true) - .WithToolsFromAssembly() + .WithToolsFromAssembly(serializerOptions: serializerOptions) .WithRequestFilters(filters => filters.AddCallToolFilter(ToolErrorFilter.Filter)); #endregion Model Context Protocol diff --git a/src/Presentation/Models/DeletedReview.cs b/src/Presentation/Models/DeletedReview.cs new file mode 100644 index 0000000..4920453 --- /dev/null +++ b/src/Presentation/Models/DeletedReview.cs @@ -0,0 +1,12 @@ +namespace CleanModelContextProtocol.Presentation.Models; + +using System.ComponentModel; + +/// +/// The result of deleting a review. +/// +/// The id of the review that was deleted. +/// Whether the review was deleted. +public record DeletedReview( + [property: Description("The id of the review that was deleted.")] Guid ReviewId, + [property: Description("Whether the review was deleted.")] bool Deleted); diff --git a/src/Presentation/Serialization/AppJsonSerializerContext.cs b/src/Presentation/Serialization/AppJsonSerializerContext.cs index f7b82e0..449d5a0 100644 --- a/src/Presentation/Serialization/AppJsonSerializerContext.cs +++ b/src/Presentation/Serialization/AppJsonSerializerContext.cs @@ -5,6 +5,7 @@ namespace CleanModelContextProtocol.Presentation.Serialization; using Application.Movies.Entities; using Application.Reviews.Entities; using Application.Versions.Entities; +using CleanModelContextProtocol.Presentation.Models; /// /// JSON serialization context for compile-time source generation. @@ -26,6 +27,7 @@ namespace CleanModelContextProtocol.Presentation.Serialization; [JsonSerializable(typeof(ReviewedMovie))] [JsonSerializable(typeof(Version))] [JsonSerializable(typeof(List))] +[JsonSerializable(typeof(DeletedReview))] public partial class AppJsonSerializerContext : JsonSerializerContext { } diff --git a/src/Presentation/Tools/AuthorTools.cs b/src/Presentation/Tools/AuthorTools.cs index bbfa88e..85bfa04 100644 --- a/src/Presentation/Tools/AuthorTools.cs +++ b/src/Presentation/Tools/AuthorTools.cs @@ -1,33 +1,31 @@ namespace CleanModelContextProtocol.Presentation.Tools; using System.ComponentModel; -using System.Text.Json; using Mediator; using ModelContextProtocol.Server; +using Entities = Application.Authors.Entities; using Queries = Application.Authors.Queries; [McpServerToolType] public static class AuthorTools { - [McpServerTool(Name = "authors_list", Title = "List authors", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "authors_list", Title = "List authors", ReadOnly = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("List every author who has written reviews, with their id, first and last name, and the reviews they have written. " + "Use this to find an author's id before calling authors_get, reviews_create or reviews_update.")] - public static async Task ListAuthors(ISender sender) + public static async Task> ListAuthors(ISender sender) { - var result = await sender.Send(new Queries.GetAuthors.GetAuthorsQuery()); - - return JsonSerializer.Serialize(result); + return await sender.Send(new Queries.GetAuthors.GetAuthorsQuery()); } - [McpServerTool(Name = "authors_get", Title = "Get author", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "authors_get", Title = "Get author", ReadOnly = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("Get one author by id, including every review they have written and the movie each review is for.")] - public static async Task GetAuthor( + public static async Task GetAuthor( ISender sender, [Description("The author's id (a GUID). Use authors_list to find it.")] Guid authorId) { - return JsonSerializer.Serialize(await sender.Send(new Queries.GetAuthorById.GetAuthorByIdQuery + return await sender.Send(new Queries.GetAuthorById.GetAuthorByIdQuery { Id = authorId - })); + }); } } diff --git a/src/Presentation/Tools/MovieTools.cs b/src/Presentation/Tools/MovieTools.cs index bc84866..096e62b 100644 --- a/src/Presentation/Tools/MovieTools.cs +++ b/src/Presentation/Tools/MovieTools.cs @@ -1,33 +1,31 @@ namespace CleanModelContextProtocol.Presentation.Tools; using System.ComponentModel; -using System.Text.Json; using Mediator; using ModelContextProtocol.Server; +using Entities = Application.Movies.Entities; using Queries = Application.Movies.Queries; [McpServerToolType] public static class MovieTools { - [McpServerTool(Name = "movies_list", Title = "List movies", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "movies_list", Title = "List movies", ReadOnly = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("List every movie with its id, title and reviews. " + "Use this to find a movie's id before calling movies_get, reviews_create or reviews_update.")] - public static async Task ListMovies(ISender sender) + public static async Task> ListMovies(ISender sender) { - var result = await sender.Send(new Queries.GetMovies.GetMoviesQuery()); - - return JsonSerializer.Serialize(result); + return await sender.Send(new Queries.GetMovies.GetMoviesQuery()); } - [McpServerTool(Name = "movies_get", Title = "Get movie", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "movies_get", Title = "Get movie", ReadOnly = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("Get one movie by id, including every review of it with the star rating and the author who wrote it.")] - public static async Task GetMovie( + public static async Task GetMovie( ISender sender, [Description("The movie's id (a GUID). Use movies_list to find it.")] Guid movieId) { - return JsonSerializer.Serialize(await sender.Send(new Queries.GetMovieById.GetMovieByIdQuery + return await sender.Send(new Queries.GetMovieById.GetMovieByIdQuery { Id = movieId - })); + }); } } diff --git a/src/Presentation/Tools/ReviewTools.cs b/src/Presentation/Tools/ReviewTools.cs index d8d7bfc..86ac7a6 100644 --- a/src/Presentation/Tools/ReviewTools.cs +++ b/src/Presentation/Tools/ReviewTools.cs @@ -1,59 +1,56 @@ namespace CleanModelContextProtocol.Presentation.Tools; using System.ComponentModel; -using System.Text.Json; +using CleanModelContextProtocol.Presentation.Models; using Mediator; using ModelContextProtocol.Server; using Commands = Application.Reviews.Commands; +using Entities = Application.Reviews.Entities; using Queries = Application.Reviews.Queries; [McpServerToolType] public static class ReviewTools { - [McpServerTool(Name = "reviews_list", Title = "List reviews", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "reviews_list", Title = "List reviews", ReadOnly = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("List every review with its id, star rating (1 to 5), the movie it is for and the author who wrote it.")] - public static async Task ListReviews(ISender sender) + public static async Task> ListReviews(ISender sender) { - var result = await sender.Send(new Queries.GetReviews.GetReviewsQuery()); - - return JsonSerializer.Serialize(result); + return await sender.Send(new Queries.GetReviews.GetReviewsQuery()); } - [McpServerTool(Name = "reviews_get", Title = "Get review", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "reviews_get", Title = "Get review", ReadOnly = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("Get one review by id, with its star rating, the movie it is for and the author who wrote it.")] - public static async Task GetReview( + public static async Task GetReview( ISender sender, [Description("The review's id (a GUID). Use reviews_list to find it.")] Guid reviewId) { - return JsonSerializer.Serialize(await sender.Send(new Queries.GetReviewById.GetReviewByIdQuery + return await sender.Send(new Queries.GetReviewById.GetReviewByIdQuery { Id = reviewId - })); + }); } - [McpServerTool(Name = "reviews_create", Title = "Create review", ReadOnly = false, Destructive = false, Idempotent = false, OpenWorld = false)] + [McpServerTool(Name = "reviews_create", Title = "Create review", ReadOnly = false, Destructive = false, Idempotent = false, OpenWorld = false, UseStructuredContent = true)] [Description("Create a review of a movie by an author with a star rating from 1 to 5, and return the new review. " + "Each call creates another review, so don't retry a call that succeeded.")] - public static async Task CreateReview( + public static async Task CreateReview( ISender sender, [Description("The id of the author writing the review. Use authors_list to find it.")] Guid authorId, [Description("The id of the movie being reviewed. Use movies_list to find it.")] Guid movieId, [Description("The star rating, a whole number from 1 (worst) to 5 (best).")] int stars) { - var result = await sender.Send(new Commands.CreateReview.CreateReviewCommand + return await sender.Send(new Commands.CreateReview.CreateReviewCommand { AuthorId = authorId, MovieId = movieId, Stars = stars }); - - return JsonSerializer.Serialize(result); } - [McpServerTool(Name = "reviews_update", Title = "Update review", ReadOnly = false, Destructive = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "reviews_update", Title = "Update review", ReadOnly = false, Destructive = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("Replace an existing review's author, movie and star rating. Every field is overwritten, " + - "so pass the current author and movie ids when you only want to change the stars.")] - public static async Task UpdateReview( + "so pass the current author and movie ids when you only want to change the stars. Returns the updated review.")] + public static async Task UpdateReview( ISender sender, [Description("The id of the review to update. Use reviews_list to find it.")] Guid reviewId, [Description("The id of the author writing the review. Use authors_list to find it.")] Guid authorId, @@ -68,20 +65,23 @@ public static async Task UpdateReview( Stars = stars }); - return "Review updated successfully"; + return await sender.Send(new Queries.GetReviewById.GetReviewByIdQuery + { + Id = reviewId + }); } - [McpServerTool(Name = "reviews_delete", Title = "Delete review", ReadOnly = false, Destructive = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "reviews_delete", Title = "Delete review", ReadOnly = false, Destructive = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("Permanently delete a review. This can't be undone, so confirm with the user first.")] - public static async Task DeleteReview( + public static async Task DeleteReview( ISender sender, [Description("The id of the review to delete. Use reviews_list to find it.")] Guid reviewId) { - _ = await sender.Send(new Commands.DeleteReview.DeleteReviewCommand + var deleted = await sender.Send(new Commands.DeleteReview.DeleteReviewCommand { Id = reviewId }); - return "Review deleted successfully"; + return new DeletedReview(reviewId, deleted); } } diff --git a/src/Presentation/Tools/VersionTools.cs b/src/Presentation/Tools/VersionTools.cs index 53baf03..2020752 100644 --- a/src/Presentation/Tools/VersionTools.cs +++ b/src/Presentation/Tools/VersionTools.cs @@ -3,22 +3,16 @@ namespace CleanModelContextProtocol.Presentation.Tools; using System.ComponentModel; using Mediator; using ModelContextProtocol.Server; +using Entities = Application.Versions.Entities; using Queries = Application.Versions.Queries; [McpServerToolType] public static class VersionTools { - [McpServerTool(Name = "server_get_version", Title = "Get server version", ReadOnly = true, Idempotent = true, OpenWorld = false)] + [McpServerTool(Name = "server_get_version", Title = "Get server version", ReadOnly = true, Idempotent = true, OpenWorld = false, UseStructuredContent = true)] [Description("Get the version of this server: the file version and the informational version, which includes the commit it was built from.")] - public static async Task GetVersion(ISender sender) + public static async Task GetVersion(ISender sender) { - var result = await sender.Send(new Queries.GetVersion.GetVersionQuery()); - - return $""" - | Property | Value | - | --- | --- | - | File Version | {result.FileVersion} | - | Informational Version | {result.InformationalVersion} | - """; + return await sender.Send(new Queries.GetVersion.GetVersionQuery()); } } diff --git a/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs b/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs index c8b9541..860d818 100644 --- a/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/ReviewToolTests.cs @@ -1,5 +1,6 @@ namespace CleanModelContextProtocol.Presentation.Tests.Integration.Tools; +using CleanModelContextProtocol.Presentation.Models; using Extensions; using ModelContextProtocol; using ModelContextProtocol.Protocol; @@ -121,7 +122,7 @@ public async Task CreateReview_ShouldReturn_Review() } [Fact] - public async Task UpdateReview_ShouldReturn_Success() + public async Task UpdateReview_ShouldReturn_Review() { // Arrange var authorResult = await this.Client.CallToolAsync("authors_list"); @@ -148,12 +149,7 @@ public async Task UpdateReview_ShouldReturn_Success() var text = result.Content.OfType().First().Text; // Assert - text.ShouldBe("Review updated successfully"); - - var validateResult = await this.Client.CallToolAsync("reviews_get", - new Dictionary { ["reviewId"] = existingReview.Id }); - var validateText = validateResult.Content.OfType().First().Text; - var updatedReview = validateText.Deserialize(); + var updatedReview = text.Deserialize(); _ = updatedReview.ShouldNotBeNull(); @@ -171,7 +167,7 @@ public async Task UpdateReview_ShouldReturn_Success() } [Fact] - public async Task DeleteReview_ShouldReturn_Success() + public async Task DeleteReview_ShouldReturn_Result() { // Arrange var reviewsResult = await this.Client.CallToolAsync("reviews_list"); @@ -184,7 +180,10 @@ public async Task DeleteReview_ShouldReturn_Success() var text = result.Content.OfType().First().Text; // Assert - text.ShouldBe("Review deleted successfully"); + var deleted = text.Deserialize(); + + deleted.ReviewId.ShouldBe(existingReview.Id); + deleted.Deleted.ShouldBeTrue(); var validateResult = await this.Client.CallToolAsync("reviews_get", new Dictionary { ["reviewId"] = existingReview.Id }); diff --git a/tests/Presentation.Tests.Integration/Tools/ToolListTests.cs b/tests/Presentation.Tests.Integration/Tools/ToolListTests.cs index 0849c36..4a28f69 100644 --- a/tests/Presentation.Tests.Integration/Tools/ToolListTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/ToolListTests.cs @@ -50,6 +50,8 @@ public async Task ListTools_ShouldDescribe_Tool(string name, string title, bool tool.Annotations.DestructiveHint.ShouldBe(destructive); } + _ = tool.OutputSchema.ShouldNotBeNull(); + foreach (var property in tool.InputSchema.GetProperty("properties").EnumerateObject()) { property.Value.GetProperty("description").GetString().ShouldNotBeNullOrWhiteSpace(); diff --git a/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs b/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs index 41b038d..db4e21a 100644 --- a/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs +++ b/tests/Presentation.Tests.Integration/Tools/VersionToolTests.cs @@ -1,5 +1,6 @@ namespace CleanModelContextProtocol.Presentation.Tests.Integration.Tools; +using Extensions; using ModelContextProtocol.Protocol; using Shouldly; using Xunit; @@ -13,12 +14,15 @@ public async Task GetVersion_ShouldReturn_Version() var result = await this.Client.CallToolAsync("server_get_version"); var text = result.Content.OfType().First().Text; + var version = text.Deserialize(); + // Assert - _ = text.ShouldNotBeNull(); - _ = text.ShouldBeOfType(); + _ = version.ShouldNotBeNull(); + + version.FileVersion.ShouldNotBeNullOrWhiteSpace(); + version.InformationalVersion.ShouldNotBeNullOrWhiteSpace(); - text.ShouldNotBeNullOrWhiteSpace(); - text.ShouldContain("File Version"); - text.ShouldContain("Informational Version"); + _ = result.StructuredContent.ShouldNotBeNull(); + result.StructuredContent.Value.GetProperty("fileVersion").GetString().ShouldBe(version.FileVersion); } } diff --git a/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs b/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs index 197dbce..1cd9184 100644 --- a/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs +++ b/tests/Presentation.Tests.Unit/Tools/AuthorToolTests.cs @@ -1,6 +1,5 @@ namespace CleanModelContextProtocol.Presentation.Tests.Unit.Tools; -using System.Text.Json; using CleanModelContextProtocol.Presentation.Tools; using Mediator; using NSubstitute; @@ -11,10 +10,8 @@ namespace CleanModelContextProtocol.Presentation.Tests.Unit.Tools; public class AuthorToolTests { - private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; - [Fact] - public async Task GetAllAuthors_ShouldReturn_JsonResult() + public async Task GetAllAuthors_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -30,7 +27,7 @@ public async Task GetAllAuthors_ShouldReturn_JsonResult() var response = await AuthorTools.ListAuthors(sender); // Assert - var result = JsonSerializer.Deserialize>(response, JsonOptions); + var result = response; result.ShouldNotBeNull(); result[0].Id.ShouldBe(Guid.Empty); @@ -40,7 +37,7 @@ public async Task GetAllAuthors_ShouldReturn_JsonResult() } [Fact] - public async Task GetAuthorById_ShouldReturn_JsonResult() + public async Task GetAuthorById_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -53,7 +50,7 @@ public async Task GetAuthorById_ShouldReturn_JsonResult() var response = await AuthorTools.GetAuthor(sender, Guid.Empty); // Assert - var result = JsonSerializer.Deserialize(response, JsonOptions); + var result = response; result.ShouldNotBeNull(); result.Id.ShouldBe(Guid.Empty); diff --git a/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs b/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs index bfd5e01..68f07f1 100644 --- a/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs +++ b/tests/Presentation.Tests.Unit/Tools/MovieToolTests.cs @@ -1,6 +1,5 @@ namespace CleanModelContextProtocol.Presentation.Tests.Unit.Tools; -using System.Text.Json; using CleanModelContextProtocol.Presentation.Tools; using Mediator; using NSubstitute; @@ -11,10 +10,8 @@ namespace CleanModelContextProtocol.Presentation.Tests.Unit.Tools; public class MovieToolTests { - private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; - [Fact] - public async Task GetAllMovies_ShouldReturn_JsonResult() + public async Task GetAllMovies_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -30,7 +27,7 @@ public async Task GetAllMovies_ShouldReturn_JsonResult() var response = await MovieTools.ListMovies(sender); // Assert - var result = JsonSerializer.Deserialize>(response, JsonOptions); + var result = response; result.ShouldNotBeNull(); result[0].Id.ShouldBe(Guid.Empty); @@ -39,7 +36,7 @@ public async Task GetAllMovies_ShouldReturn_JsonResult() } [Fact] - public async Task GetMovieById_ShouldReturn_JsonResult() + public async Task GetMovieById_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -52,7 +49,7 @@ public async Task GetMovieById_ShouldReturn_JsonResult() var response = await MovieTools.GetMovie(sender, Guid.Empty); // Assert - var result = JsonSerializer.Deserialize(response, JsonOptions); + var result = response; result.ShouldNotBeNull(); result.Id.ShouldBe(Guid.Empty); diff --git a/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs b/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs index f096ed8..d011719 100644 --- a/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs +++ b/tests/Presentation.Tests.Unit/Tools/ReviewToolTests.cs @@ -1,6 +1,5 @@ namespace CleanModelContextProtocol.Presentation.Tests.Unit.Tools; -using System.Text.Json; using Application.Authors.Entities; using Application.Movies.Entities; using CleanModelContextProtocol.Presentation.Tools; @@ -14,8 +13,6 @@ namespace CleanModelContextProtocol.Presentation.Tests.Unit.Tools; public class ReviewToolTests { - private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNameCaseInsensitive = true }; - private static Entities.Review SampleReview() { return new( @@ -26,7 +23,7 @@ private static Entities.Review SampleReview() } [Fact] - public async Task GetAllReviews_ShouldReturn_JsonResult() + public async Task GetAllReviews_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -39,7 +36,7 @@ public async Task GetAllReviews_ShouldReturn_JsonResult() var response = await ReviewTools.ListReviews(sender); // Assert - var result = JsonSerializer.Deserialize>(response, JsonOptions); + var result = response; result.ShouldNotBeNull(); result[0].Id.ShouldBe(Guid.Empty); @@ -52,7 +49,7 @@ public async Task GetAllReviews_ShouldReturn_JsonResult() } [Fact] - public async Task GetReviewById_ShouldReturn_JsonResult() + public async Task GetReviewById_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -65,7 +62,7 @@ public async Task GetReviewById_ShouldReturn_JsonResult() var response = await ReviewTools.GetReview(sender, Guid.Empty); // Assert - var result = JsonSerializer.Deserialize(response, JsonOptions); + var result = response; result.ShouldNotBeNull(); result.Id.ShouldBe(Guid.Empty); @@ -78,7 +75,7 @@ public async Task GetReviewById_ShouldReturn_JsonResult() } [Fact] - public async Task CreateReview_ShouldReturn_JsonResult() + public async Task CreateReview_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -91,7 +88,7 @@ public async Task CreateReview_ShouldReturn_JsonResult() var response = await ReviewTools.CreateReview(sender, Guid.Empty, Guid.Empty, 5); // Assert - var result = JsonSerializer.Deserialize(response, JsonOptions); + var result = response; result.ShouldNotBeNull(); result.Id.ShouldBe(Guid.Empty); @@ -104,7 +101,7 @@ public async Task CreateReview_ShouldReturn_JsonResult() } [Fact] - public async Task UpdateReview_ShouldReturn_Success() + public async Task UpdateReview_ShouldReturn_Review() { // Arrange var sender = Substitute.For(); @@ -113,15 +110,20 @@ public async Task UpdateReview_ShouldReturn_Success() .Send(Arg.Any()) .ReturnsForAnyArgs(true); + _ = sender + .Send(Arg.Any()) + .ReturnsForAnyArgs(SampleReview()); + // Act var response = await ReviewTools.UpdateReview(sender, Guid.Empty, Guid.Empty, Guid.Empty, 5); // Assert - response.ShouldBe("Review updated successfully"); + response.Id.ShouldBe(Guid.Empty); + response.Stars.ShouldBe(5); } [Fact] - public async Task DeleteReview_ShouldReturn_Success() + public async Task DeleteReview_ShouldReturn_Result() { // Arrange var sender = Substitute.For(); @@ -134,6 +136,7 @@ public async Task DeleteReview_ShouldReturn_Success() var response = await ReviewTools.DeleteReview(sender, Guid.Empty); // Assert - response.ShouldBe("Review deleted successfully"); + response.ReviewId.ShouldBe(Guid.Empty); + response.Deleted.ShouldBeTrue(); } } diff --git a/tests/Presentation.Tests.Unit/Tools/VersionToolTests.cs b/tests/Presentation.Tests.Unit/Tools/VersionToolTests.cs index 19693de..8c759cc 100644 --- a/tests/Presentation.Tests.Unit/Tools/VersionToolTests.cs +++ b/tests/Presentation.Tests.Unit/Tools/VersionToolTests.cs @@ -11,7 +11,7 @@ namespace CleanModelContextProtocol.Presentation.Tests.Unit.Tools; public class VersionToolTests { [Fact] - public async Task GetVersion_ShouldReturn_MarkdownTable() + public async Task GetVersion_ShouldReturn_Version() { // Arrange var sender = Substitute.For(); @@ -28,7 +28,7 @@ public async Task GetVersion_ShouldReturn_MarkdownTable() var response = await VersionTools.GetVersion(sender); // Assert - response.ShouldContain("| File Version | 1.2.3.4 |"); - response.ShouldContain("| Informational Version | 5.6.7.8 |"); + response.FileVersion.ShouldBe("1.2.3.4"); + response.InformationalVersion.ShouldBe("5.6.7.8"); } }