microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell.Tests/ActionDispatcherTests.cs
185lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using autoShell.Handlers; |
| 6 | using autoShell.Logging; |
| 7 | using Moq; |
| 8 | |
| 9 | namespace autoShell.Tests; |
| 10 | |
| 11 | public class ActionDispatcherTests |
| 12 | { |
| 13 | private readonly Mock<ILogger> _loggerMock = new(); |
| 14 | private readonly ActionDispatcher _dispatcher; |
| 15 | |
| 16 | public ActionDispatcherTests() |
| 17 | { |
| 18 | _dispatcher = new ActionDispatcher(_loggerMock.Object); |
| 19 | } |
| 20 | |
| 21 | private static JsonElement Parse(string json) => JsonDocument.Parse(json).RootElement; |
| 22 | |
| 23 | /// <summary> |
| 24 | /// Verifies that dispatching a JSON object with a "quit" actionName returns a quit ActionResult. |
| 25 | /// </summary> |
| 26 | [Fact] |
| 27 | public void Dispatch_QuitKey_ReturnsQuitResult() |
| 28 | { |
| 29 | var json = Parse("""{"actionName":"quit","parameters":{}}"""); |
| 30 | ActionResult result = _dispatcher.Dispatch(json); |
| 31 | Assert.NotNull(result); |
| 32 | Assert.True(result.Success); |
| 33 | Assert.True(result.IsQuit); |
| 34 | } |
| 35 | |
| 36 | /// <summary> |
| 37 | /// Verifies that dispatching a non-quit command returns a successful ActionResult. |
| 38 | /// </summary> |
| 39 | [Fact] |
| 40 | public void Dispatch_NonQuitKey_ReturnsSuccessResult() |
| 41 | { |
| 42 | _dispatcher.Register(new StubHandler("TestCmd")); |
| 43 | var json = Parse("""{"actionName":"TestCmd","parameters":{}}"""); |
| 44 | ActionResult result = _dispatcher.Dispatch(json); |
| 45 | Assert.NotNull(result); |
| 46 | Assert.True(result.Success); |
| 47 | } |
| 48 | |
| 49 | /// <summary> |
| 50 | /// Verifies that commands are routed to the correct handler with the expected key and parameters. |
| 51 | /// </summary> |
| 52 | [Fact] |
| 53 | public void Dispatch_RoutesToCorrectHandler() |
| 54 | { |
| 55 | var handler = new StubHandler("Alpha", "Beta"); |
| 56 | _dispatcher.Register(handler); |
| 57 | |
| 58 | _dispatcher.Dispatch(Parse("""{"actionName":"Alpha","parameters":{}}""")); |
| 59 | Assert.Equal("Alpha", handler.LastKey); |
| 60 | Assert.NotNull(handler.LastParameters); |
| 61 | |
| 62 | _dispatcher.Dispatch(Parse("""{"actionName":"Beta","parameters":{}}""")); |
| 63 | Assert.Equal("Beta", handler.LastKey); |
| 64 | Assert.NotNull(handler.LastParameters); |
| 65 | } |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Verifies that dispatching an unrecognized command returns a failure result. |
| 69 | /// </summary> |
| 70 | [Fact] |
| 71 | public void Dispatch_UnknownCommand_ReturnsFailure() |
| 72 | { |
| 73 | var json = Parse("""{"actionName":"UnknownCmd","parameters":{}}"""); |
| 74 | ActionResult result = _dispatcher.Dispatch(json); |
| 75 | Assert.NotNull(result); |
| 76 | Assert.False(result.Success); |
| 77 | Assert.Contains("Unknown action", result.Message); |
| 78 | } |
| 79 | |
| 80 | /// <summary> |
| 81 | /// Verifies that dispatching an empty JSON object returns a failure result. |
| 82 | /// </summary> |
| 83 | [Fact] |
| 84 | public void Dispatch_EmptyObject_ReturnsFailure() |
| 85 | { |
| 86 | ActionResult result = _dispatcher.Dispatch(Parse("{}")); |
| 87 | Assert.NotNull(result); |
| 88 | Assert.False(result.Success); |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
| 92 | /// Verifies that a quit dispatch does not invoke any registered handlers. |
| 93 | /// </summary> |
| 94 | [Fact] |
| 95 | public void Dispatch_QuitStopsProcessingSubsequentKeys() |
| 96 | { |
| 97 | var handler = new StubHandler("After"); |
| 98 | _dispatcher.Register(handler); |
| 99 | |
| 100 | _dispatcher.Dispatch(Parse("""{"actionName":"quit","parameters":{}}""")); |
| 101 | |
| 102 | Assert.Null(handler.LastKey); |
| 103 | } |
| 104 | |
| 105 | /// <summary> |
| 106 | /// Verifies that an exception thrown by a handler returns a failure result. |
| 107 | /// </summary> |
| 108 | [Fact] |
| 109 | public void Dispatch_HandlerException_ReturnsFailure() |
| 110 | { |
| 111 | var handler = new ThrowingHandler("Boom"); |
| 112 | _dispatcher.Register(handler); |
| 113 | |
| 114 | var json = Parse("""{"actionName":"Boom","parameters":{}}"""); |
| 115 | ActionResult result = _dispatcher.Dispatch(json); |
| 116 | Assert.NotNull(result); |
| 117 | Assert.False(result.Success); |
| 118 | Assert.Contains("Boom", result.Message); |
| 119 | } |
| 120 | |
| 121 | /// <summary> |
| 122 | /// Verifies that a handler exception does not prevent subsequent dispatches from working. |
| 123 | /// </summary> |
| 124 | [Fact] |
| 125 | public void Dispatch_HandlerException_DoesNotAffectSubsequentDispatches() |
| 126 | { |
| 127 | var throwing = new ThrowingHandler("Boom"); |
| 128 | var normal = new StubHandler("Ok"); |
| 129 | _dispatcher.Register(throwing, normal); |
| 130 | |
| 131 | _dispatcher.Dispatch(Parse("""{"actionName":"Boom","parameters":{}}""")); |
| 132 | _dispatcher.Dispatch(Parse("""{"actionName":"Ok","parameters":{}}""")); |
| 133 | Assert.Equal("Ok", normal.LastKey); |
| 134 | } |
| 135 | |
| 136 | /// <summary> |
| 137 | /// Verifies that registering duplicate action names throws. |
| 138 | /// </summary> |
| 139 | [Fact] |
| 140 | public void Register_DuplicateCommand_Throws() |
| 141 | { |
| 142 | _dispatcher.Register(new StubHandler("Dup")); |
| 143 | Assert.Throws<InvalidOperationException>(() => _dispatcher.Register(new StubHandler("Dup"))); |
| 144 | } |
| 145 | |
| 146 | /// <summary> |
| 147 | /// Stub handler that records the last key and parameters it received. |
| 148 | /// </summary> |
| 149 | private class StubHandler : IActionHandler |
| 150 | { |
| 151 | public IEnumerable<string> SupportedActions { get; } |
| 152 | public string? LastKey { get; private set; } |
| 153 | public JsonElement? LastParameters { get; private set; } |
| 154 | |
| 155 | public StubHandler(params string[] commands) |
| 156 | { |
| 157 | SupportedActions = commands; |
| 158 | } |
| 159 | |
| 160 | public ActionResult Handle(string key, JsonElement parameters) |
| 161 | { |
| 162 | LastKey = key; |
| 163 | LastParameters = parameters; |
| 164 | return ActionResult.Ok($"Handled {key}"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// <summary> |
| 169 | /// Handler that always throws, for testing exception isolation. |
| 170 | /// </summary> |
| 171 | private class ThrowingHandler : IActionHandler |
| 172 | { |
| 173 | public IEnumerable<string> SupportedActions { get; } |
| 174 | |
| 175 | public ThrowingHandler(params string[] commands) |
| 176 | { |
| 177 | SupportedActions = commands; |
| 178 | } |
| 179 | |
| 180 | public ActionResult Handle(string key, JsonElement parameters) |
| 181 | { |
| 182 | throw new InvalidOperationException("Test exception"); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |