microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/cache-regex-for-string-part

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

dotnet/autoShell.Tests/CommandDispatcherTests.cs

166lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using autoShell.Handlers;
5using autoShell.Logging;
6using Moq;
7using Newtonsoft.Json.Linq;
8
9namespace autoShell.Tests;
10
11public class CommandDispatcherTests
12{
13 private readonly Mock<ILogger> _loggerMock = new();
14 private readonly CommandDispatcher _dispatcher;
15
16 public CommandDispatcherTests()
17 {
18 _dispatcher = new CommandDispatcher(_loggerMock.Object);
19 }
20
21 /// <summary>
22 /// Verifies that dispatching a JSON object with a "quit" key returns true.
23 /// </summary>
24 [Fact]
25 public void Dispatch_QuitKey_ReturnsTrue()
26 {
27 var json = JObject.Parse("""{"quit": true}""");
28 bool result = _dispatcher.Dispatch(json);
29 Assert.True(result);
30 }
31
32 /// <summary>
33 /// Verifies that dispatching a non-quit command returns false.
34 /// </summary>
35 [Fact]
36 public void Dispatch_NonQuitKey_ReturnsFalse()
37 {
38 _dispatcher.Register(new StubHandler("TestCmd"));
39 var json = JObject.Parse("""{"TestCmd": "value"}""");
40 bool result = _dispatcher.Dispatch(json);
41 Assert.False(result);
42 }
43
44 /// <summary>
45 /// Verifies that commands are routed to the correct handler with the expected key and value.
46 /// </summary>
47 [Fact]
48 public void Dispatch_RoutesToCorrectHandler()
49 {
50 var handler = new StubHandler("Alpha", "Beta");
51 _dispatcher.Register(handler);
52
53 _dispatcher.Dispatch(JObject.Parse("""{"Alpha": "1"}"""));
54 Assert.Equal("Alpha", handler.LastKey);
55 Assert.Equal("1", handler.LastValue);
56
57 _dispatcher.Dispatch(JObject.Parse("""{"Beta": "2"}"""));
58 Assert.Equal("Beta", handler.LastKey);
59 Assert.Equal("2", handler.LastValue);
60 }
61
62 /// <summary>
63 /// Verifies that dispatching an unrecognized command does not throw an exception.
64 /// </summary>
65 [Fact]
66 public void Dispatch_UnknownCommand_DoesNotThrow()
67 {
68 var json = JObject.Parse("""{"UnknownCmd": "value"}""");
69 var ex = Record.Exception(() => _dispatcher.Dispatch(json));
70 Assert.Null(ex);
71 }
72
73 /// <summary>
74 /// Verifies that dispatching an empty JSON object returns false.
75 /// </summary>
76 [Fact]
77 public void Dispatch_EmptyObject_ReturnsFalse()
78 {
79 bool result = _dispatcher.Dispatch([]);
80 Assert.False(result);
81 }
82
83 /// <summary>
84 /// Verifies that a "quit" key stops processing of any subsequent keys in the same JSON object.
85 /// </summary>
86 [Fact]
87 public void Dispatch_QuitStopsProcessingSubsequentKeys()
88 {
89 var handler = new StubHandler("After");
90 _dispatcher.Register(handler);
91
92 // quit comes first — handler for "After" should not be called
93 var json = JObject.Parse("""{"quit": true, "After": "value"}""");
94 bool result = _dispatcher.Dispatch(json);
95
96 Assert.True(result);
97 Assert.Null(handler.LastKey);
98 }
99
100 /// <summary>
101 /// Verifies that an exception thrown by a handler does not propagate to the caller.
102 /// </summary>
103 [Fact]
104 public void Dispatch_HandlerException_DoesNotBubbleUp()
105 {
106 var handler = new ThrowingHandler("Boom");
107 _dispatcher.Register(handler);
108
109 var json = JObject.Parse("""{"Boom": "value"}""");
110 var ex = Record.Exception(() => _dispatcher.Dispatch(json));
111 Assert.Null(ex);
112 }
113
114 /// <summary>
115 /// Verifies that after a handler throws, subsequent keys in the same dispatch are still processed.
116 /// </summary>
117 [Fact]
118 public void Dispatch_HandlerException_ContinuesToNextKey()
119 {
120 var throwing = new ThrowingHandler("Boom");
121 var normal = new StubHandler("Ok");
122 _dispatcher.Register(throwing, normal);
123
124 _dispatcher.Dispatch(JObject.Parse("""{"Boom": "x", "Ok": "y"}"""));
125 Assert.Equal("Ok", normal.LastKey);
126 }
127
128 /// <summary>
129 /// Stub handler that records the last key/value it received.
130 /// </summary>
131 private class StubHandler : ICommandHandler
132 {
133 public IEnumerable<string> SupportedCommands { get; }
134 public string? LastKey { get; private set; }
135 public string? LastValue { get; private set; }
136
137 public StubHandler(params string[] commands)
138 {
139 SupportedCommands = commands;
140 }
141
142 public void Handle(string key, string value, JToken rawValue)
143 {
144 LastKey = key;
145 LastValue = value;
146 }
147 }
148
149 /// <summary>
150 /// Handler that always throws, for testing exception isolation.
151 /// </summary>
152 private class ThrowingHandler : ICommandHandler
153 {
154 public IEnumerable<string> SupportedCommands { get; }
155
156 public ThrowingHandler(params string[] commands)
157 {
158 SupportedCommands = commands;
159 }
160
161 public void Handle(string key, string value, JToken rawValue)
162 {
163 throw new InvalidOperationException("Test exception");
164 }
165 }
166}
167