IInputOutputTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #nullable enable
  2. using System.Collections.Concurrent;
  3. using Xunit.Abstractions;
  4. using Xunit.Sdk;
  5. namespace DriverTests;
  6. /// <summary>
  7. /// Low-level tests for IInput and IOutput implementations across all drivers.
  8. /// These tests are designed to fail with good error messages when run in environments
  9. /// without a real terminal (like GitHub Actions).
  10. /// </summary>
  11. public class IInputOutputTests (ITestOutputHelper output)
  12. {
  13. private readonly ITestOutputHelper _output = output;
  14. #region DotNet Driver Tests
  15. [Fact]
  16. [Trait ("Category", "LowLevelDriver")]
  17. public void NetInput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
  18. {
  19. // Arrange & Act
  20. Exception? exception = Record.Exception (() =>
  21. {
  22. using var input = new NetInput ();
  23. _output.WriteLine ("NetInput created successfully");
  24. });
  25. // Assert
  26. if (exception != null)
  27. {
  28. _output.WriteLine ($"FAILED: NetInput constructor threw: {exception.GetType ().Name}: {exception.Message}");
  29. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  30. }
  31. Assert.Null (exception);
  32. }
  33. [Fact]
  34. [Trait ("Category", "LowLevelDriver")]
  35. public void NetInput_Peek_DoesNotThrow_WhenNoTerminalAvailable ()
  36. {
  37. // Arrange
  38. using var input = new NetInput ();
  39. // Act
  40. Exception? exception = Record.Exception (() =>
  41. {
  42. bool hasInput = input.Peek ();
  43. _output.WriteLine ($"NetInput.Peek() returned: {hasInput}");
  44. });
  45. // Assert
  46. if (exception != null)
  47. {
  48. _output.WriteLine ($"FAILED: NetInput.Peek() threw: {exception.GetType ().Name}: {exception.Message}");
  49. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  50. }
  51. Assert.Null (exception);
  52. }
  53. [Fact]
  54. [Trait ("Category", "LowLevelDriver")]
  55. public void NetInput_Read_DoesNotThrow_WhenNoTerminalAvailable ()
  56. {
  57. // Arrange
  58. using var input = new NetInput ();
  59. // Act
  60. Exception? exception = Record.Exception (() =>
  61. {
  62. List<ConsoleKeyInfo> items = input.Read ().ToList ();
  63. _output.WriteLine ($"NetInput.Read() returned {items.Count} items");
  64. });
  65. // Assert
  66. if (exception != null)
  67. {
  68. _output.WriteLine ($"FAILED: NetInput.Read() threw: {exception.GetType ().Name}: {exception.Message}");
  69. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  70. }
  71. Assert.Null (exception);
  72. }
  73. [Fact]
  74. [Trait ("Category", "LowLevelDriver")]
  75. public void NetInput_Dispose_DoesNotThrow_WhenNoTerminalAvailable ()
  76. {
  77. // Arrange
  78. var input = new NetInput ();
  79. // Act
  80. Exception? exception = Record.Exception (() =>
  81. {
  82. input.Dispose ();
  83. _output.WriteLine ("NetInput disposed successfully");
  84. });
  85. // Assert
  86. if (exception != null)
  87. {
  88. _output.WriteLine ($"FAILED: NetInput.Dispose() threw: {exception.GetType ().Name}: {exception.Message}");
  89. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  90. }
  91. Assert.Null (exception);
  92. }
  93. [Fact]
  94. [Trait ("Category", "LowLevelDriver")]
  95. public void NetOutput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
  96. {
  97. // Arrange & Act
  98. Exception? exception = Record.Exception (() =>
  99. {
  100. using var output = new NetOutput ();
  101. _output.WriteLine ("NetOutput created successfully");
  102. });
  103. // Assert
  104. if (exception != null)
  105. {
  106. _output.WriteLine ($"FAILED: NetOutput constructor threw: {exception.GetType ().Name}: {exception.Message}");
  107. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  108. }
  109. Assert.Null (exception);
  110. }
  111. [Fact]
  112. [Trait ("Category", "LowLevelDriver")]
  113. public void NetOutput_GetSize_ReturnsDefaultSize_WhenNoTerminalAvailable ()
  114. {
  115. // Arrange
  116. using var output = new NetOutput ();
  117. // Act
  118. Size size = default;
  119. Exception? exception = Record.Exception (() =>
  120. {
  121. size = output.GetSize ();
  122. _output.WriteLine ($"NetOutput.GetSize() returned: {size.Width}x{size.Height}");
  123. });
  124. // Assert
  125. if (exception != null)
  126. {
  127. _output.WriteLine ($"FAILED: NetOutput.GetSize() threw: {exception.GetType ().Name}: {exception.Message}");
  128. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  129. }
  130. Assert.Null (exception);
  131. Assert.True (size.Width > 0, "Width should be > 0");
  132. Assert.True (size.Height > 0, "Height should be > 0");
  133. }
  134. [Fact]
  135. [Trait ("Category", "LowLevelDriver")]
  136. public void NetOutput_Write_DoesNotThrow_WhenNoTerminalAvailable ()
  137. {
  138. // Arrange
  139. using var output = new NetOutput ();
  140. // Act
  141. Exception? exception = Record.Exception (() =>
  142. {
  143. ReadOnlySpan<char> text = "Test".AsSpan ();
  144. output.Write (text);
  145. _output.WriteLine ("NetOutput.Write() succeeded");
  146. });
  147. // Assert
  148. if (exception != null)
  149. {
  150. _output.WriteLine ($"FAILED: NetOutput.Write() threw: {exception.GetType ().Name}: {exception.Message}");
  151. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  152. }
  153. Assert.Null (exception);
  154. }
  155. [Fact]
  156. [Trait ("Category", "LowLevelDriver")]
  157. public void NetOutput_SetCursorPosition_DoesNotThrow_WhenNoTerminalAvailable ()
  158. {
  159. // Arrange
  160. using var output = new NetOutput ();
  161. // Act
  162. Exception? exception = Record.Exception (() =>
  163. {
  164. output.SetCursorPosition (0, 0);
  165. _output.WriteLine ("NetOutput.SetCursorPosition() succeeded");
  166. });
  167. // Assert
  168. if (exception != null)
  169. {
  170. _output.WriteLine ($"FAILED: NetOutput.SetCursorPosition() threw: {exception.GetType ().Name}: {exception.Message}");
  171. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  172. }
  173. Assert.Null (exception);
  174. }
  175. [Fact]
  176. [Trait ("Category", "LowLevelDriver")]
  177. public void NetInputProcessor_Constructor_DoesNotThrow ()
  178. {
  179. // Arrange & Act
  180. Exception? exception = Record.Exception (() =>
  181. {
  182. ConcurrentQueue<ConsoleKeyInfo> queue = new ();
  183. var processor = new NetInputProcessor (queue);
  184. _output.WriteLine ("NetInputProcessor created successfully");
  185. });
  186. // Assert
  187. if (exception != null)
  188. {
  189. _output.WriteLine ($"FAILED: NetInputProcessor constructor threw: {exception.GetType ().Name}: {exception.Message}");
  190. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  191. }
  192. Assert.Null (exception);
  193. }
  194. #endregion
  195. #region Unix Driver Tests
  196. [Fact]
  197. [Trait ("Category", "LowLevelDriver")]
  198. [Trait ("Platform", "Unix")]
  199. public void UnixInput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
  200. {
  201. // Arrange & Act
  202. Exception? exception = Record.Exception (() =>
  203. {
  204. try
  205. {
  206. using var input = new UnixInput ();
  207. _output.WriteLine ("UnixInput created successfully");
  208. }
  209. catch (Exception ex)
  210. {
  211. _output.WriteLine ($"Expected failure on non-terminal: {ex.Message}");
  212. throw new XunitException (
  213. $"UnixInput failed in non-terminal environment: {ex.Message}\nThis is expected in GitHub Actions. The driver should detect this and handle gracefully.");
  214. }
  215. });
  216. // Assert
  217. if (exception != null && !(exception is XunitException))
  218. {
  219. _output.WriteLine ($"FAILED: UnixInput constructor threw: {exception.GetType ().Name}: {exception.Message}");
  220. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  221. }
  222. }
  223. [Fact]
  224. [Trait ("Category", "LowLevelDriver")]
  225. [Trait ("Platform", "Unix")]
  226. public void UnixOutput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
  227. {
  228. if (OperatingSystem.IsWindows ())
  229. {
  230. _output.WriteLine ("Skipping Unix test on Windows");
  231. return;
  232. }
  233. // Arrange & Act
  234. Exception? exception = Record.Exception (() =>
  235. {
  236. using var output = new UnixOutput ();
  237. _output.WriteLine ("UnixOutput created successfully");
  238. });
  239. // Assert
  240. if (exception != null)
  241. {
  242. _output.WriteLine ($"FAILED: UnixOutput constructor threw: {exception.GetType ().Name}: {exception.Message}");
  243. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  244. }
  245. Assert.Null (exception);
  246. }
  247. [Fact]
  248. [Trait ("Category", "LowLevelDriver")]
  249. [Trait ("Platform", "Unix")]
  250. public void UnixOutput_GetSize_ReturnsDefaultSize_WhenNoTerminalAvailable ()
  251. {
  252. if (OperatingSystem.IsWindows ())
  253. {
  254. _output.WriteLine ("Skipping Unix test on Windows");
  255. return;
  256. }
  257. // Arrange
  258. using var output = new UnixOutput ();
  259. // Act
  260. Size size = default;
  261. Exception? exception = Record.Exception (() =>
  262. {
  263. size = output.GetSize ();
  264. _output.WriteLine ($"UnixOutput.GetSize() returned: {size.Width}x{size.Height}");
  265. });
  266. // Assert
  267. Assert.Null (exception);
  268. Assert.Equal (80, size.Width);
  269. Assert.Equal (25, size.Height);
  270. }
  271. #endregion
  272. #region Windows Driver Tests
  273. [Fact]
  274. [Trait ("Category", "LowLevelDriver")]
  275. [Trait ("Platform", "Windows")]
  276. public void WindowsInput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
  277. {
  278. if (!OperatingSystem.IsWindows ())
  279. {
  280. _output.WriteLine ("Skipping Windows test on non-Windows");
  281. return;
  282. }
  283. // Arrange & Act
  284. Exception? exception = Record.Exception (() =>
  285. {
  286. using var input = new WindowsInput ();
  287. _output.WriteLine ("WindowsInput created successfully");
  288. });
  289. // Assert
  290. if (exception != null)
  291. {
  292. _output.WriteLine ($"FAILED: WindowsInput constructor threw: {exception.GetType ().Name}: {exception.Message}");
  293. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  294. }
  295. Assert.Null (exception);
  296. }
  297. [Fact]
  298. [Trait ("Category", "LowLevelDriver")]
  299. [Trait ("Platform", "Windows")]
  300. public void WindowsOutput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
  301. {
  302. if (!OperatingSystem.IsWindows ())
  303. {
  304. _output.WriteLine ("Skipping Windows test on non-Windows");
  305. return;
  306. }
  307. // Arrange & Act
  308. Exception? exception = Record.Exception (() =>
  309. {
  310. try
  311. {
  312. using var output = new WindowsOutput ();
  313. _output.WriteLine ("WindowsOutput created successfully");
  314. }
  315. catch (Exception ex)
  316. {
  317. _output.WriteLine ($"WindowsOutput threw during construction: {ex.GetType ().Name}: {ex.Message}");
  318. throw;
  319. }
  320. });
  321. // Assert
  322. if (exception != null)
  323. {
  324. _output.WriteLine ($"FAILED: WindowsOutput constructor threw: {exception.GetType ().Name}: {exception.Message}");
  325. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  326. }
  327. Assert.Null (exception);
  328. }
  329. #endregion
  330. #region Fake Driver Tests
  331. [Fact]
  332. [Trait ("Category", "LowLevelDriver")]
  333. public void FakeInput_Constructor_DoesNotThrow ()
  334. {
  335. // Arrange & Act
  336. Exception? exception = Record.Exception (() =>
  337. {
  338. using var input = new FakeInput ();
  339. _output.WriteLine ("FakeInput created successfully");
  340. });
  341. // Assert
  342. Assert.Null (exception);
  343. }
  344. [Fact]
  345. [Trait ("Category", "LowLevelDriver")]
  346. public void FakeOutput_Constructor_DoesNotThrow ()
  347. {
  348. // Arrange & Act
  349. Exception? exception = Record.Exception (() =>
  350. {
  351. using var output = new FakeOutput ();
  352. _output.WriteLine ("FakeOutput created successfully");
  353. });
  354. // Assert
  355. Assert.Null (exception);
  356. }
  357. [Fact]
  358. [Trait ("Category", "LowLevelDriver")]
  359. public void FakeOutput_GetSize_ReturnsExpectedSize ()
  360. {
  361. // Arrange
  362. using var output = new FakeOutput ();
  363. // Act
  364. Size size = output.GetSize ();
  365. _output.WriteLine ($"FakeOutput.GetSize() returned: {size.Width}x{size.Height}");
  366. // Assert
  367. Assert.True (size.Width > 0);
  368. Assert.True (size.Height > 0);
  369. }
  370. #endregion
  371. #region Component Factory Tests
  372. [Fact]
  373. [Trait ("Category", "LowLevelDriver")]
  374. public void NetComponentFactory_CreateInput_DoesNotThrow ()
  375. {
  376. // Arrange
  377. var factory = new NetComponentFactory ();
  378. // Act
  379. Exception? exception = Record.Exception (() =>
  380. {
  381. using IInput<ConsoleKeyInfo> input = factory.CreateInput ();
  382. _output.WriteLine ("NetComponentFactory.CreateInput() succeeded");
  383. });
  384. // Assert
  385. if (exception != null)
  386. {
  387. _output.WriteLine ($"FAILED: NetComponentFactory.CreateInput() threw: {exception.GetType ().Name}: {exception.Message}");
  388. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  389. }
  390. Assert.Null (exception);
  391. }
  392. [Fact]
  393. [Trait ("Category", "LowLevelDriver")]
  394. public void NetComponentFactory_CreateOutput_DoesNotThrow ()
  395. {
  396. // Arrange
  397. var factory = new NetComponentFactory ();
  398. // Act
  399. Exception? exception = Record.Exception (() =>
  400. {
  401. using IOutput output = factory.CreateOutput ();
  402. _output.WriteLine ("NetComponentFactory.CreateOutput() succeeded");
  403. });
  404. // Assert
  405. if (exception != null)
  406. {
  407. _output.WriteLine ($"FAILED: NetComponentFactory.CreateOutput() threw: {exception.GetType ().Name}: {exception.Message}");
  408. _output.WriteLine ($"Stack trace: {exception.StackTrace}");
  409. }
  410. Assert.Null (exception);
  411. }
  412. [Fact]
  413. [Trait ("Category", "LowLevelDriver")]
  414. public void FakeComponentFactory_CreateInput_DoesNotThrow ()
  415. {
  416. // Arrange
  417. var factory = new FakeComponentFactory ();
  418. // Act
  419. Exception? exception = Record.Exception (() =>
  420. {
  421. using IInput<ConsoleKeyInfo> input = factory.CreateInput ();
  422. _output.WriteLine ("FakeComponentFactory.CreateInput() succeeded");
  423. });
  424. // Assert
  425. Assert.Null (exception);
  426. }
  427. [Fact]
  428. [Trait ("Category", "LowLevelDriver")]
  429. public void FakeComponentFactory_CreateOutput_DoesNotThrow ()
  430. {
  431. // Arrange
  432. var factory = new FakeComponentFactory ();
  433. // Act
  434. Exception? exception = Record.Exception (() =>
  435. {
  436. using IOutput output = factory.CreateOutput ();
  437. _output.WriteLine ("FakeComponentFactory.CreateOutput() succeeded");
  438. });
  439. // Assert
  440. Assert.Null (exception);
  441. }
  442. #endregion
  443. }