// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Text.Json; using System.Text.Json.Serialization; using autoShell.Handlers; namespace autoShell.Tests; /// /// Tests for deserialization and error handling in AddAction<T>. /// public class ActionHandlerBaseTests { private record TestParams { [JsonPropertyName("name")] public string Name { get; init; } = ""; [JsonPropertyName("count")] public int Count { get; init; } = 0; } private class TestHandler : ActionHandlerBase { public TestParams? LastParams { get; private set; } public TestHandler() { AddAction("TypedAction", p => { LastParams = p; return ActionResult.Ok("ok"); }); } } private static JsonElement Parse(string json) => JsonDocument.Parse(json).RootElement; /// /// Verifies that AddAction<T> correctly deserializes valid JSON to the typed parameter record. /// [Fact] public void TypedAction_ValidJson_DeserializesToTypedParams() { var handler = new TestHandler(); var result = handler.Handle("TypedAction", Parse("""{"name":"test","count":5}""")); Assert.True(result.Success); Assert.NotNull(handler.LastParams); Assert.Equal("test", handler.LastParams!.Name); Assert.Equal(5, handler.LastParams.Count); } /// /// Verifies that AddAction<T> returns a failure when the JSON is "null". /// JsonSerializer.Deserialize returns null for the JSON literal "null". /// [Fact] public void TypedAction_NullJson_ReturnsFailure() { var handler = new TestHandler(); var result = handler.Handle("TypedAction", Parse("null")); Assert.False(result.Success); Assert.Contains("null", result.Message, StringComparison.OrdinalIgnoreCase); } /// /// Verifies that AddAction<T> returns a failure when JSON has a type mismatch /// (e.g., string where int is expected). /// [Fact] public void TypedAction_TypeMismatch_ReturnsFailure() { var handler = new TestHandler(); var result = handler.Handle("TypedAction", Parse("""{"name":"test","count":"notAnInt"}""")); Assert.False(result.Success); Assert.Contains("Invalid parameters", result.Message); } }