SourcesManagerTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. using System.Reflection;
  2. using System.Text.Json;
  3. namespace ConfigurationTests;
  4. public class SourcesManagerTests
  5. {
  6. #region Update (Stream)
  7. [Fact]
  8. public void Load_WithNullSettingsScope_ReturnsFalse ()
  9. {
  10. // Arrange
  11. var sourcesManager = new SourcesManager ();
  12. var stream = new MemoryStream ();
  13. var source = "Load_WithNullSettingsScope_ReturnsFalse";
  14. var location = ConfigLocations.AppCurrent;
  15. // Act
  16. bool result = sourcesManager.Load (null, stream, source, location);
  17. // Assert
  18. Assert.False (result);
  19. }
  20. [Fact]
  21. public void Load_WithValidStream_UpdatesSettingsScope ()
  22. {
  23. // Arrange
  24. var sourcesManager = new SourcesManager ();
  25. var settingsScope = new SettingsScope ();
  26. settingsScope.LoadHardCodedDefaults ();
  27. settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
  28. var json = """
  29. {
  30. "Application.QuitKey": "Ctrl+Z"
  31. }
  32. """;
  33. var location = ConfigLocations.HardCoded;
  34. var source = "Load_WithValidStream_UpdatesSettingsScope";
  35. var stream = new MemoryStream ();
  36. var writer = new StreamWriter (stream);
  37. writer.Write (json);
  38. writer.Flush ();
  39. stream.Position = 0;
  40. // Act
  41. bool result = sourcesManager.Load (settingsScope, stream, source, location);
  42. // Assert
  43. // Assert
  44. Assert.True (result);
  45. Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
  46. Assert.Contains (source, sourcesManager.Sources.Values);
  47. }
  48. #endregion
  49. #region Update (FilePath)
  50. [Fact]
  51. public void Load_WithNonExistentFile_AddsToSourcesAndReturnsTrue ()
  52. {
  53. // Arrange
  54. var sourcesManager = new SourcesManager ();
  55. var settingsScope = new SettingsScope ();
  56. var filePath = "nonexistent.json";
  57. var location = ConfigLocations.AppCurrent;
  58. // Act
  59. bool result = sourcesManager.Load (settingsScope, filePath, location);
  60. // Assert
  61. Assert.True (result);
  62. Assert.Contains (filePath, sourcesManager.Sources.Values);
  63. }
  64. [Fact]
  65. public void Load_WithValidFile_UpdatesSettingsScope ()
  66. {
  67. // Arrange
  68. var sourcesManager = new SourcesManager ();
  69. var settingsScope = new SettingsScope ();
  70. settingsScope.LoadHardCodedDefaults ();
  71. settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
  72. var json = """
  73. {
  74. "Application.QuitKey": "Ctrl+Z"
  75. }
  76. """;
  77. var source = Path.GetTempFileName ();
  78. var location = ConfigLocations.HardCoded;
  79. File.WriteAllText (source, json);
  80. try
  81. {
  82. // Act
  83. bool result = sourcesManager.Load (settingsScope, source, location);
  84. // Assert
  85. Assert.True (result);
  86. Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
  87. Assert.Contains (source, sourcesManager.Sources.Values);
  88. }
  89. finally
  90. {
  91. // Cleanup
  92. File.Delete (source);
  93. }
  94. }
  95. [Fact]
  96. public void Load_WithIOException_RetriesAndFailsGracefully ()
  97. {
  98. // Arrange
  99. var sourcesManager = new SourcesManager ();
  100. var settingsScope = new SettingsScope ();
  101. settingsScope.LoadHardCodedDefaults ();
  102. var filePath = "locked.json";
  103. var json = "{\"Application.UseSystemConsole\": true}";
  104. File.WriteAllText (filePath, json);
  105. var location = ConfigLocations.AppCurrent;
  106. try
  107. {
  108. using FileStream fileStream = File.Open (filePath, FileMode.Open, FileAccess.Read, FileShare.None);
  109. // Act
  110. bool result = sourcesManager.Load (settingsScope, filePath, location);
  111. // Assert
  112. Assert.False (result);
  113. }
  114. finally
  115. {
  116. // Cleanup
  117. File.Delete (filePath);
  118. }
  119. }
  120. #endregion
  121. #region Update (Json String)
  122. [Fact]
  123. public void Load_WithNullOrEmptyJson_ReturnsFalse ()
  124. {
  125. // Arrange
  126. var sourcesManager = new SourcesManager ();
  127. var settingsScope = new SettingsScope ();
  128. var source = "Load_WithNullOrEmptyJson_ReturnsFalse";
  129. var location = ConfigLocations.AppCurrent;
  130. // Act
  131. bool resultWithNull = sourcesManager.Load (settingsScope, json: null, source, location);
  132. bool resultWithEmpty = sourcesManager.Load (settingsScope, string.Empty, source, location);
  133. // Assert
  134. Assert.False (resultWithNull);
  135. Assert.False (resultWithEmpty);
  136. }
  137. [Fact]
  138. public void Load_WithValidJson_UpdatesSettingsScope ()
  139. {
  140. // Arrange
  141. var sourcesManager = new SourcesManager ();
  142. var settingsScope = new SettingsScope ();
  143. settingsScope.LoadHardCodedDefaults ();
  144. settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
  145. var json = """
  146. {
  147. "Application.QuitKey": "Ctrl+Z"
  148. }
  149. """;
  150. var source = "Load_WithValidJson_UpdatesSettingsScope";
  151. var location = ConfigLocations.HardCoded;
  152. // Act
  153. bool result = sourcesManager.Load (settingsScope, json, source, location);
  154. // Assert
  155. Assert.True (result);
  156. Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
  157. Assert.Contains (source, sourcesManager.Sources.Values);
  158. }
  159. //[Fact]
  160. //public void Update_WithValidJson_UpdatesThemeScope ()
  161. //{
  162. // // Arrange
  163. // var sourcesManager = new SourcesManager ();
  164. // var themeScope = new ThemeScope ();
  165. // themeScope.LoadHardCodedDefaults ();
  166. // themeScope ["Button.DefaultShadowStyle"].PropertyValue = ShadowStyle.Opaque;
  167. // var json = """
  168. // {
  169. // "Button.DefaultShadowStyle": "None"
  170. // }
  171. // """;
  172. // var source = "Update_WithValidJson_UpdatesThemeScope";
  173. // var location = ConfigLocations.HardCoded;
  174. // // Act
  175. // bool result = sourcesManager.Load (themeScope, json, source, location);
  176. // // Assert
  177. // Assert.True (result);
  178. // Assert.Equal (Key.Z.WithCtrl, themeScope ["Application.QuitKey"].PropertyValue as Key);
  179. // Assert.Contains (source, sourcesManager.Sources.Values);
  180. //}
  181. #endregion
  182. #region Load
  183. [Fact]
  184. public void Load_WithNullResourceName_ReturnsFalse ()
  185. {
  186. // Arrange
  187. var sourcesManager = new SourcesManager ();
  188. var settingsScope = new SettingsScope ();
  189. settingsScope.LoadHardCodedDefaults ();
  190. settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
  191. var assembly = Assembly.GetExecutingAssembly ();
  192. var location = ConfigLocations.AppResources;
  193. // Act
  194. bool result = sourcesManager.Load (settingsScope, assembly, string.Empty, location);
  195. // Assert
  196. Assert.False (result);
  197. }
  198. [Fact]
  199. public void Load_WithValidResource_UpdatesSettingsScope ()
  200. {
  201. // Arrange
  202. var sourcesManager = new SourcesManager ();
  203. var settingsScope = new SettingsScope ();
  204. var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
  205. var resourceName = "Terminal.Gui.Resources.config.json";
  206. var location = ConfigLocations.LibraryResources;
  207. // Act
  208. bool result = sourcesManager.Load (settingsScope, assembly!, resourceName, location);
  209. // Assert
  210. Assert.True (result);
  211. // Verify settingsScope is updated as expected
  212. }
  213. [Fact]
  214. public void Load_Runtime_Overrides ()
  215. {
  216. // Arrange
  217. var sourcesManager = new SourcesManager ();
  218. var settingsScope = new SettingsScope ();
  219. var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
  220. var resourceName = "Terminal.Gui.Resources.config.json";
  221. var location = ConfigLocations.LibraryResources;
  222. sourcesManager.Load (settingsScope, assembly!, resourceName, location);
  223. Assert.Equal (Key.Esc, settingsScope ["Application.QuitKey"].PropertyValue);
  224. var runtimeJson = """
  225. {
  226. "Application.QuitKey": "Ctrl+Z"
  227. }
  228. """;
  229. var runtimeSource = "runtime.json";
  230. var runtimeLocation = ConfigLocations.Runtime;
  231. var runtimeStream = new MemoryStream ();
  232. var writer = new StreamWriter (runtimeStream);
  233. writer.Write (runtimeJson);
  234. writer.Flush ();
  235. runtimeStream.Position = 0;
  236. // Act
  237. bool result = sourcesManager.Load (settingsScope, runtimeStream, runtimeSource, runtimeLocation);
  238. // Assert
  239. Assert.True (result);
  240. // Verify settingsScope is updated as expected
  241. Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue);
  242. }
  243. #endregion
  244. #region ToJson and ToStream
  245. [Fact]
  246. public void ToJson_WithValidScope_ReturnsJsonString ()
  247. {
  248. // Arrange
  249. var sourcesManager = new SourcesManager ();
  250. var settingsScope = new SettingsScope ();
  251. settingsScope.LoadHardCodedDefaults ();
  252. settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
  253. // Act
  254. string json = sourcesManager.ToJson (settingsScope);
  255. // Assert
  256. Assert.Contains ("""Application.QuitKey": "Ctrl+Q""", json);
  257. }
  258. [Fact]
  259. public void ToStream_WithValidScope_ReturnsStream ()
  260. {
  261. // Arrange
  262. var sourcesManager = new SourcesManager ();
  263. var settingsScope = new SettingsScope ();
  264. settingsScope.LoadHardCodedDefaults ();
  265. settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
  266. // Act
  267. var stream = sourcesManager.ToStream (settingsScope);
  268. // Assert
  269. Assert.NotNull (stream);
  270. stream.Position = 0;
  271. var reader = new StreamReader (stream);
  272. string json = reader.ReadToEnd ();
  273. Assert.Contains ("""Application.QuitKey": "Ctrl+Q""", json);
  274. }
  275. #endregion
  276. #region Sources Dictionary Tests
  277. [Fact]
  278. public void Sources_Dictionary_IsInitializedEmpty ()
  279. {
  280. // Arrange & Act
  281. var sourcesManager = new SourcesManager ();
  282. // Assert
  283. Assert.NotNull (sourcesManager.Sources);
  284. Assert.Empty (sourcesManager.Sources);
  285. }
  286. [Fact]
  287. public void Load_WhenCalledMultipleTimes_MaintainsLastSourceForLocation ()
  288. {
  289. // Arrange
  290. var sourcesManager = new SourcesManager ();
  291. var settingsScope = new SettingsScope ();
  292. // Act - Update with first source for location
  293. var firstSource = "first.json";
  294. sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+A"}""", firstSource, ConfigLocations.Runtime);
  295. // Update with second source for same location
  296. var secondSource = "second.json";
  297. sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+B"}""", secondSource, ConfigLocations.Runtime);
  298. // Assert - Only the last source should be stored for the location
  299. Assert.Single (sourcesManager.Sources);
  300. Assert.Equal (secondSource, sourcesManager.Sources [ConfigLocations.Runtime]);
  301. }
  302. [Fact]
  303. public void Load_WithDifferentLocations_AddsAllSourcesToCollection ()
  304. {
  305. // Arrange
  306. var sourcesManager = new SourcesManager ();
  307. var settingsScope = new SettingsScope ();
  308. ConfigLocations [] locations =
  309. [
  310. ConfigLocations.LibraryResources,
  311. ConfigLocations.Runtime,
  312. ConfigLocations.AppCurrent,
  313. ConfigLocations.GlobalHome
  314. ];
  315. // Act - Update with different sources for different locations
  316. foreach (var location in locations)
  317. {
  318. var source = $"config-{location}.json";
  319. sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", source, location);
  320. }
  321. // Assert
  322. Assert.Equal (locations.Length, sourcesManager.Sources.Count);
  323. foreach (var location in locations)
  324. {
  325. Assert.Contains (location, sourcesManager.Sources.Keys);
  326. Assert.Equal ($"config-{location}.json", sourcesManager.Sources [location]);
  327. }
  328. }
  329. [Fact]
  330. public void Load_AddsResourceSourceToCollection ()
  331. {
  332. // Arrange
  333. var sourcesManager = new SourcesManager ();
  334. var settingsScope = new SettingsScope ();
  335. var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
  336. var resourceName = "Terminal.Gui.Resources.config.json";
  337. var location = ConfigLocations.LibraryResources;
  338. // Act
  339. bool result = sourcesManager.Load (settingsScope, assembly!, resourceName, location);
  340. // Assert
  341. Assert.True (result);
  342. Assert.Contains (location, sourcesManager.Sources.Keys);
  343. Assert.Equal ($"resource://[{assembly!.GetName ().Name}]/{resourceName}", sourcesManager.Sources [location]);
  344. }
  345. [Fact]
  346. public void Load_WithNonExistentFileAndDifferentLocations_TracksAllSources ()
  347. {
  348. // Arrange
  349. var sourcesManager = new SourcesManager ();
  350. var settingsScope = new SettingsScope ();
  351. // Define multiple files and locations
  352. var fileLocations = new Dictionary<string, ConfigLocations> (StringComparer.InvariantCultureIgnoreCase)
  353. {
  354. { "file1.json", ConfigLocations.AppCurrent },
  355. { "file2.json", ConfigLocations.GlobalHome },
  356. { "file3.json", ConfigLocations.AppHome }
  357. };
  358. // Act
  359. foreach (var pair in fileLocations)
  360. {
  361. sourcesManager.Load (settingsScope, pair.Key, pair.Value);
  362. }
  363. // Assert
  364. Assert.Equal (fileLocations.Count, sourcesManager.Sources.Count);
  365. foreach (var pair in fileLocations)
  366. {
  367. Assert.Contains (pair.Value, sourcesManager.Sources.Keys);
  368. Assert.Equal (pair.Key, sourcesManager.Sources [pair.Value]);
  369. }
  370. }
  371. [Fact]
  372. public void Sources_IsPreservedAcrossOperations ()
  373. {
  374. // Arrange
  375. var sourcesManager = new SourcesManager ();
  376. var settingsScope = new SettingsScope ();
  377. // First operation - file update
  378. var filePath = "testfile.json";
  379. var location1 = ConfigLocations.AppCurrent;
  380. sourcesManager.Load (settingsScope, filePath, location1);
  381. // Second operation - json string update
  382. var jsonSource = "jsonstring";
  383. var location2 = ConfigLocations.Runtime;
  384. sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", jsonSource, location2);
  385. // Perform a stream operation
  386. var streamSource = "streamdata";
  387. var location3 = ConfigLocations.GlobalCurrent;
  388. var stream = new MemoryStream ();
  389. var writer = new StreamWriter (stream);
  390. writer.Write ("""{"Application.QuitKey": "Ctrl+Z"}""");
  391. writer.Flush ();
  392. stream.Position = 0;
  393. sourcesManager.Load (settingsScope, stream, streamSource, location3);
  394. // Assert - all sources should be preserved
  395. Assert.Equal (3, sourcesManager.Sources.Count);
  396. Assert.Equal (filePath, sourcesManager.Sources [location1]);
  397. Assert.Equal (jsonSource, sourcesManager.Sources [location2]);
  398. Assert.Equal (streamSource, sourcesManager.Sources [location3]);
  399. }
  400. #endregion
  401. }