AnsiResponseParserTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System.Diagnostics;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace UnitTests.ConsoleDrivers;
  5. public class AnsiResponseParserTests (ITestOutputHelper output)
  6. {
  7. AnsiResponseParser<int> _parser1 = new AnsiResponseParser<int> ();
  8. AnsiResponseParser _parser2 = new AnsiResponseParser ();
  9. /// <summary>
  10. /// Used for the T value in batches that are passed to the AnsiResponseParser&lt;int&gt; (parser1)
  11. /// </summary>
  12. private int tIndex = 0;
  13. [Fact]
  14. public void TestInputProcessing ()
  15. {
  16. string ansiStream = "\x1B[<0;10;20M" + // ANSI escape for mouse move at (10, 20)
  17. "Hello" + // User types "Hello"
  18. "\x1B[0c"; // Device Attributes response (e.g., terminal identification i.e. DAR)
  19. string? response1 = null;
  20. string? response2 = null;
  21. int i = 0;
  22. // Imagine that we are expecting a DAR
  23. _parser1.ExpectResponse ("c",(s)=> response1 = s);
  24. _parser2.ExpectResponse ("c", (s) => response2 = s);
  25. // First char is Escape which we must consume incase what follows is the DAR
  26. AssertConsumed (ansiStream, ref i); // Esc
  27. for (int c = 0; c < "[<0;10;20".Length; c++)
  28. {
  29. AssertConsumed (ansiStream, ref i);
  30. }
  31. // We see the M terminator
  32. AssertReleased (ansiStream, ref i, "\x1B[<0;10;20M");
  33. // Regular user typing
  34. for (int c = 0; c < "Hello".Length; c++)
  35. {
  36. AssertIgnored (ansiStream,"Hello"[c], ref i);
  37. }
  38. // Now we have entered the actual DAR we should be consuming these
  39. for (int c = 0; c < "\x1B[0".Length; c++)
  40. {
  41. AssertConsumed (ansiStream, ref i);
  42. }
  43. // Consume the terminator 'c' and expect this to call the above event
  44. Assert.Null (response1);
  45. Assert.Null (response1);
  46. AssertConsumed (ansiStream, ref i);
  47. Assert.NotNull (response2);
  48. Assert.Equal ("\x1B[0c", response2);
  49. Assert.NotNull (response2);
  50. Assert.Equal ("\x1B[0c", response2);
  51. }
  52. [Theory]
  53. [InlineData ("\x1B[<0;10;20MHi\x1B[0c", "c", "\x1B[0c", "\x1B[<0;10;20MHi")]
  54. [InlineData ("\x1B[<1;15;25MYou\x1B[1c", "c", "\x1B[1c", "\x1B[<1;15;25MYou")]
  55. [InlineData ("\x1B[0cHi\x1B[0c", "c", "\x1B[0c", "Hi\x1B[0c")]
  56. [InlineData ("\x1B[<0;0;0MHe\x1B[3c", "c", "\x1B[3c", "\x1B[<0;0;0MHe")]
  57. [InlineData ("\x1B[<0;1;2Da\x1B[0c\x1B[1c", "c", "\x1B[0c", "\x1B[<0;1;2Da\x1B[1c")]
  58. [InlineData ("\x1B[1;1M\x1B[3cAn", "c", "\x1B[3c", "\x1B[1;1MAn")]
  59. [InlineData ("hi\x1B[2c\x1B[<5;5;5m", "c", "\x1B[2c", "hi\x1B[<5;5;5m")]
  60. [InlineData ("\x1B[3c\x1B[4c\x1B[<0;0;0MIn", "c", "\u001b[3c", "\u001b[4c\u001b[<0;0;0MIn")]
  61. [InlineData ("\x1B[<1;2;3M\x1B[0c\x1B[<1;2;3M\x1B[2c", "c", "\x1B[0c", "\x1B[<1;2;3M\x1B[<1;2;3M\u001b[2c")]
  62. [InlineData ("\x1B[<0;1;1MHi\x1B[6c\x1B[2c\x1B[<1;0;0MT", "c", "\x1B[6c", "\x1B[<0;1;1MHi\x1B[2c\x1B[<1;0;0MT")]
  63. [InlineData ("Te\x1B[<2;2;2M\x1B[7c", "c", "\x1B[7c", "Te\x1B[<2;2;2M")]
  64. [InlineData ("\x1B[0c\x1B[<0;0;0M\x1B[3c\x1B[0c\x1B[1;0MT", "c", "\x1B[0c", "\x1B[<0;0;0M\x1B[3c\x1B[0c\x1B[1;0MT")]
  65. [InlineData ("\x1B[0;0M\x1B[<0;0;0M\x1B[3cT\x1B[1c", "c", "\u001b[3c", "\u001b[0;0M\u001b[<0;0;0MT\u001b[1c")]
  66. [InlineData ("\x1B[3c\x1B[<0;0;0M\x1B[0c\x1B[<1;1;1MIn\x1B[1c", "c", "\u001b[3c", "\u001b[<0;0;0M\u001b[0c\u001b[<1;1;1MIn\u001b[1c")]
  67. [InlineData ("\x1B[<5;5;5M\x1B[7cEx\x1B[8c", "c", "\x1B[7c", "\u001b[<5;5;5MEx\u001b[8c")]
  68. // Random characters and mixed inputs
  69. [InlineData ("\x1B[<1;1;1MJJ\x1B[9c", "c", "\x1B[9c", "\x1B[<1;1;1MJJ")] // Mixed text
  70. [InlineData ("Be\x1B[0cAf", "c", "\x1B[0c", "BeAf")] // Escape in the middle of the string
  71. [InlineData ("\x1B[<0;0;0M\x1B[2cNot e", "c", "\x1B[2c", "\x1B[<0;0;0MNot e")] // Unexpected sequence followed by text
  72. [InlineData ("Just te\x1B[<0;0;0M\x1B[3c\x1B[2c\x1B[4c", "c", "\x1B[3c", "Just te\x1B[<0;0;0M\x1B[2c\x1B[4c")] // Multiple unexpected responses
  73. [InlineData ("\x1B[1;2;3M\x1B[0c\x1B[2;2M\x1B[0;0;0MTe", "c", "\x1B[0c", "\x1B[1;2;3M\x1B[2;2M\x1B[0;0;0MTe")] // Multiple commands with responses
  74. [InlineData ("\x1B[<3;3;3Mabc\x1B[4cde", "c", "\x1B[4c", "\x1B[<3;3;3Mabcde")] // Escape sequences mixed with regular text
  75. // Edge cases
  76. [InlineData ("\x1B[0c\x1B[0c\x1B[0c", "c", "\x1B[0c", "\x1B[0c\x1B[0c")] // Multiple identical responses
  77. [InlineData ("", "c", "", "")] // Empty input
  78. [InlineData ("Normal", "c", "", "Normal")] // No escape sequences
  79. [InlineData ("\x1B[<0;0;0M", "c", "", "\x1B[<0;0;0M")] // Escape sequence only
  80. [InlineData ("\x1B[1;2;3M\x1B[0c", "c", "\x1B[0c", "\x1B[1;2;3M")] // Last response consumed
  81. [InlineData ("Inpu\x1B[0c\x1B[1;0;0M", "c", "\x1B[0c", "Inpu\x1B[1;0;0M")] // Single input followed by escape
  82. [InlineData ("\x1B[2c\x1B[<5;6;7MDa", "c", "\x1B[2c", "\x1B[<5;6;7MDa")] // Multiple escape sequences followed by text
  83. [InlineData ("\x1B[0cHi\x1B[1cGo", "c", "\x1B[0c", "Hi\u001b[1cGo")] // Normal text with multiple escape sequences
  84. [InlineData ("\x1B[<1;1;1MTe", "c", "", "\x1B[<1;1;1MTe")]
  85. // Add more test cases here...
  86. public void TestInputSequences (string ansiStream, string expectedTerminator, string expectedResponse, string expectedOutput)
  87. {
  88. var swGenBatches = Stopwatch.StartNew ();
  89. int tests = 0;
  90. var permutations = GetBatchPermutations (ansiStream,5).ToArray ();
  91. swGenBatches.Stop ();
  92. var swRunTest = Stopwatch.StartNew ();
  93. foreach (var batchSet in permutations)
  94. {
  95. tIndex = 0;
  96. string response1 = string.Empty;
  97. string response2 = string.Empty;
  98. // Register the expected response with the given terminator
  99. _parser1.ExpectResponse (expectedTerminator, s => response1 = s);
  100. _parser2.ExpectResponse (expectedTerminator, s => response2 = s);
  101. // Process the input
  102. StringBuilder actualOutput1 = new StringBuilder ();
  103. StringBuilder actualOutput2 = new StringBuilder ();
  104. foreach (var batch in batchSet)
  105. {
  106. var output1 = _parser1.ProcessInput (StringToBatch (batch));
  107. actualOutput1.Append (BatchToString (output1));
  108. var output2 = _parser2.ProcessInput (batch);
  109. actualOutput2.Append (output2);
  110. }
  111. // Assert the final output minus the expected response
  112. Assert.Equal (expectedOutput, actualOutput1.ToString());
  113. Assert.Equal (expectedResponse, response1);
  114. Assert.Equal (expectedOutput, actualOutput2.ToString ());
  115. Assert.Equal (expectedResponse, response2);
  116. tests++;
  117. }
  118. output.WriteLine ($"Tested {tests} in {swRunTest.ElapsedMilliseconds} ms (gen batches took {swGenBatches.ElapsedMilliseconds} ms)" );
  119. }
  120. [Fact]
  121. public void ReleasesEscapeAfterTimeout ()
  122. {
  123. string input = "\x1B";
  124. int i = 0;
  125. // Esc on its own looks like it might be an esc sequence so should be consumed
  126. AssertConsumed (input,ref i);
  127. // We should know when the state changed
  128. Assert.Equal (ParserState.ExpectingBracket, _parser1.State);
  129. Assert.Equal (ParserState.ExpectingBracket, _parser2.State);
  130. Assert.Equal (DateTime.Now.Date, _parser1.StateChangedAt.Date);
  131. Assert.Equal (DateTime.Now.Date, _parser2.StateChangedAt.Date);
  132. AssertManualReleaseIs (input);
  133. }
  134. [Fact]
  135. public void TwoExcapesInARow ()
  136. {
  137. // Example user presses Esc key then a DAR comes in
  138. string input = "\x1B\x1B";
  139. int i = 0;
  140. // First Esc gets grabbed
  141. AssertConsumed (input, ref i);
  142. // Upon getting the second Esc we should release the first
  143. AssertReleased (input, ref i, "\x1B",0);
  144. // Assume 50ms or something has passed, lets force release as no new content
  145. // It should be the second escape that gets released (i.e. index 1)
  146. AssertManualReleaseIs ("\x1B",1);
  147. }
  148. private Tuple<char, int> [] StringToBatch (string batch)
  149. {
  150. return batch.Select ((k) => Tuple.Create (k, tIndex++)).ToArray ();
  151. }
  152. public static IEnumerable<string []> GetBatchPermutations (string input, int maxDepth = 3)
  153. {
  154. // Call the recursive method to generate batches with an initial depth of 0
  155. return GenerateBatches (input, 0, maxDepth, 0);
  156. }
  157. private static IEnumerable<string []> GenerateBatches (string input, int start, int maxDepth, int currentDepth)
  158. {
  159. // If we have reached the maximum recursion depth, return no results
  160. if (currentDepth >= maxDepth)
  161. {
  162. yield break; // No more batches can be generated at this depth
  163. }
  164. // If we have reached the end of the string, return an empty list
  165. if (start >= input.Length)
  166. {
  167. yield return new string [0];
  168. yield break;
  169. }
  170. // Iterate over the input string to create batches
  171. for (int i = start + 1; i <= input.Length; i++)
  172. {
  173. // Take a batch from 'start' to 'i'
  174. string batch = input.Substring (start, i - start);
  175. // Recursively get batches from the remaining substring, increasing the depth
  176. foreach (var remainingBatches in GenerateBatches (input, i, maxDepth, currentDepth + 1))
  177. {
  178. // Combine the current batch with the remaining batches
  179. var result = new string [1 + remainingBatches.Length];
  180. result [0] = batch;
  181. Array.Copy (remainingBatches, 0, result, 1, remainingBatches.Length);
  182. yield return result;
  183. }
  184. }
  185. }
  186. private void AssertIgnored (string ansiStream,char expected, ref int i)
  187. {
  188. var c2 = ansiStream [i];
  189. var c1 = NextChar (ansiStream, ref i);
  190. // Parser does not grab this key (i.e. driver can continue with regular operations)
  191. Assert.Equal ( c1,_parser1.ProcessInput (c1));
  192. Assert.Equal (expected,c1.Single().Item1);
  193. Assert.Equal (c2, _parser2.ProcessInput (c2.ToString()).Single());
  194. Assert.Equal (expected, c2 );
  195. }
  196. private void AssertConsumed (string ansiStream, ref int i)
  197. {
  198. // Parser grabs this key
  199. var c2 = ansiStream [i];
  200. var c1 = NextChar (ansiStream, ref i);
  201. Assert.Empty (_parser1.ProcessInput(c1));
  202. Assert.Empty (_parser2.ProcessInput (c2.ToString()));
  203. }
  204. private void AssertReleased (string ansiStream, ref int i, string expectedRelease, params int[] expectedTValues)
  205. {
  206. var c2 = ansiStream [i];
  207. var c1 = NextChar (ansiStream, ref i);
  208. // Parser realizes it has grabbed content that does not belong to an outstanding request
  209. // Parser returns false to indicate to continue
  210. var released1 = _parser1.ProcessInput (c1).ToArray ();
  211. Assert.Equal (expectedRelease, BatchToString (released1));
  212. if (expectedTValues.Length > 0)
  213. {
  214. Assert.True (expectedTValues.SequenceEqual (released1.Select (kv=>kv.Item2)));
  215. }
  216. Assert.Equal (expectedRelease, _parser2.ProcessInput (c2.ToString ()));
  217. }
  218. private string BatchToString (IEnumerable<Tuple<char, int>> processInput)
  219. {
  220. return new string(processInput.Select (a=>a.Item1).ToArray ());
  221. }
  222. private Tuple<char,int>[] NextChar (string ansiStream, ref int i)
  223. {
  224. return StringToBatch(ansiStream [i++].ToString());
  225. }
  226. private void AssertManualReleaseIs (string expectedRelease, params int [] expectedTValues)
  227. {
  228. // Consumer is responsible for determining this based on e.g. after 50ms
  229. var released1 = _parser1.Release ().ToArray ();
  230. Assert.Equal (expectedRelease, BatchToString (released1));
  231. if (expectedTValues.Length > 0)
  232. {
  233. Assert.True (expectedTValues.SequenceEqual (released1.Select (kv => kv.Item2)));
  234. }
  235. Assert.Equal (expectedRelease, _parser2.Release ());
  236. Assert.Equal (ParserState.Normal, _parser1.State);
  237. Assert.Equal (ParserState.Normal, _parser2.State);
  238. }
  239. }