Files
Terminal.Gui/Tests/UnitTestsParallelizable/Configuration/SourcesManagerTests.cs
Tig 7422385457 Fixes #4057 - MASSIVE! Fully implements ColorScheme->Scheme + VisualRole + Colors.->SchemeManager. (#4062)
* touching publish.yml

* ColorScheme->Scheme

* ColorScheme->Scheme 2

* Prototype of GetAttributeForRole

* Badly broke CM

* Further Badly broke CM

* Refactored CM big-time. View still broken

* All unit test pass again. Tons added. CM is still WIP, but Schemes is not mostly refactored and working.

* Actually:
All unit test pass again.
Tons added.
CM is still WIP, but Schemes is not mostly refactored and working.

* Bug fixes.
DeepMemberWiseClone cleanup

* Further cleanup of Scope<T>, ConfigProperty, etc.

* Made ConfigManager thread safe.

* WIP: Broken

* WIP: new deep clone impl

* WIP: new deep clone impl is done. Now fixing CM

* WIP:
- config.md
- Working on AOT clean up
- Core CM is broken; but known.

* WIP

* Merged.
Removed CM from Application.Init

* WIP

* More WIP; Less broke

* All CM unit tests pass... Not sure if it actually works though

* All unit tests pass... Themes are broken though in UI Cat

* CM Ready for review?

* Fixed failures due to TextStyles PR

* Working on Scheme/Attribute

* Working on Scheme/Attribute 2

* Working on Scheme/Attribute 3

* Working on Scheme/Attribute 4

* Working on Scheme/Attribute 5

* Working on Scheme/Attribute 6

* Added test to show how awful memory usage is

* Improved schema. Updated config.json

* Nade Scope<T> concurrentdictionary and added test to prove

* Made Themes ConcrurrentDictionary. Added bunches of tests

* Code cleanup

* Code cleanup 2

* Code cleanup 3

* Tweaking Scheme

* ClearJsonErrors

* ClearJsonErrors2

* Updated Attribute API

* It all (mostly) works!

* Skip odd unit test

* Messed with Themes

* Theme tweaks

* Code reorg. New .md stuff

* Fixed Enabled. Added mock driver

* Fixed a bunch of View.Enabled related issues

* Scheme -> Get/SetScheme()

* Cleanup

* Cleanup2

* Broke something

* Fixed everything

* Made CM.Enable better

* Text Style Scenario

* Added comments

* Fixed UI Catalog Theme Changing

* Fixed more dynamic CM update stuff

* Warning cleanup

* New Default Theme

* fixed unit test

* Refactoring Scheme and Attribute to fix inheritance

* more unit tests

* ConfigProperty is not updating schemes correctly

* All unit tests pass.
Code cleanup

* All unit tests pass.
Code cleanup2

* Fixed unit tests

* Upgraded TextField and TextView

* Fixed TextView !Enabled bug

* More updates to TextView. More unit tests for SchemeManager

* Upgraded CharMap

* API docs

* Fixe HexView API

* upgrade HexView

* Fixed shortcut KeyView

* Fixed more bugs. Added new themes

* updated themes

* upgraded Border

* Fixed themes memory usage...mostly

* Fixed themes memory usage...mostly2

* Fixed themes memory usage...2

* Fixed themes memory usage...3

* Added new colors

* Fixed GetHardCodedConfig bug

* Added Themes Scenario - WIP

* Added Themes Scenario

* Tweaked Themes Scenario

* Code cleanup

* Fixed json schmea

* updated deepdives

* updated deepdives

* Tweaked Themes Scenario

* Made Schemes a concurrent dict

* Test cleanup

* Thread safe ConfigProperty tests

* trying to make things more thread safe

* more trying to make things more thread safe

* Fixing bugs in shadowview

* Fixing bugs in shadowview 2

* Refactored GetViewsUnderMouse to GetViewsUnderLocation etc...

* Fixed dupe unit tests?

* Added better description of layout and coordiantes to deep dive

* Added better description of layout and coordiantes to deep dive

* Modified tests that call v2.AddTimeout; they were returning true which means restart the timer!
This was causing mac/linux unit test failures.
I think

* Fixed auto scheme.
Broke TextView/TextField selection

* Realized Attribute.IsExplicitlySet is stupid; just use nullable

* Fixed Attribute. Simplified. MOre theme testing

* Updated themes again

* GetViewsUnderMouse to GetViewsUnderLocation broke TransparentMouse.

* Fixing mouseunder bugs

* rewriting...

* All working again.
Shadows are now slick as snot.
GetViewsUnderLocation is rewritten to actually work and be readable.
Tons more low-level unit tests.
Margin is now actually ViewportSettings.Transparent.

* Code cleanup

* Code cleanup

* Code cleanup of color apis

* Fixed Hover/Highlight

* Update Examples/UICatalog/Scenarios/AllViewsTester.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/Clipping.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fixed race condition?

* reverted

* Simplified Attribute API by removing events from SetAttributeForRole

* Removed recursion from GetViewsAtLocation

* Removed unneeded code

* Code clean up.
Fixed Scheme bug.

* reverted temporary disable

* Adjusted scheme algo

* Upgraded TextValidateField

* Fixed TextValidate bugs

* Tweaks

* Frameview rounded border by default

* API doc cleanup

* Readme fix

* Addressed tznind feeback

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true 2

* cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-29 13:55:54 -06:00

535 lines
17 KiB
C#

using System.Reflection;
using System.Text.Json;
using Terminal.Gui.Configuration;
public class SourcesManagerTests
{
#region Update (Stream)
[Fact]
public void Update_WithNullSettingsScope_ReturnsFalse ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var stream = new MemoryStream ();
var source = "test.json";
var location = ConfigLocations.AppCurrent;
// Act
bool result = sourcesManager.Load (null, stream, source, location);
// Assert
Assert.False (result);
}
[Fact]
public void Update_WithValidStream_UpdatesSettingsScope ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
settingsScope.LoadHardCodedDefaults ();
settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
var json = """
{
"Application.QuitKey": "Ctrl+Z"
}
""";
var location = ConfigLocations.HardCoded;
var source = "stream";
var stream = new MemoryStream ();
var writer = new StreamWriter (stream);
writer.Write (json);
writer.Flush ();
stream.Position = 0;
// Act
bool result = sourcesManager.Load (settingsScope, stream, source, location);
// Assert
// Assert
Assert.True (result);
Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
Assert.Contains (source, sourcesManager.Sources.Values);
}
[Fact]
public void Update_WithInvalidJson_AddsJsonError ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
var invalidJson = "{ invalid json }";
var stream = new MemoryStream ();
var writer = new StreamWriter (stream);
writer.Write (invalidJson);
writer.Flush ();
stream.Position = 0;
var source = "test.json";
var location = ConfigLocations.AppCurrent;
// Act
bool result = sourcesManager.Load (settingsScope, stream, source, location);
// Assert
Assert.False (result);
// Assuming AddJsonError logs errors, verify the error was logged (mock or inspect logs if possible).
}
#endregion
#region Update (FilePath)
[Fact]
public void Update_WithNonExistentFile_AddsToSourcesAndReturnsTrue ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
var filePath = "nonexistent.json";
var location = ConfigLocations.AppCurrent;
// Act
bool result = sourcesManager.Load (settingsScope, filePath, location);
// Assert
Assert.True (result);
Assert.Contains (filePath, sourcesManager.Sources.Values);
}
[Fact]
public void Update_WithValidFile_UpdatesSettingsScope ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
settingsScope.LoadHardCodedDefaults ();
settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
var json = """
{
"Application.QuitKey": "Ctrl+Z"
}
""";
var source = Path.GetTempFileName ();
var location = ConfigLocations.HardCoded;
File.WriteAllText (source, json);
try
{
// Act
bool result = sourcesManager.Load (settingsScope, source, location);
// Assert
Assert.True (result);
Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
Assert.Contains (source, sourcesManager.Sources.Values);
}
finally
{
// Cleanup
File.Delete (source);
}
}
[Fact]
public void Update_WithIOException_RetriesAndFailsGracefully ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
settingsScope.LoadHardCodedDefaults ();
var filePath = "locked.json";
var json = "{\"Application.UseSystemConsole\": true}";
File.WriteAllText (filePath, json);
var location = ConfigLocations.AppCurrent;
try
{
using FileStream fileStream = File.Open (filePath, FileMode.Open, FileAccess.Read, FileShare.None);
// Act
bool result = sourcesManager.Load (settingsScope, filePath, location);
// Assert
Assert.False (result);
}
finally
{
// Cleanup
File.Delete (filePath);
}
}
#endregion
#region Update (Json String)
[Fact]
public void Update_WithNullOrEmptyJson_ReturnsFalse ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
var source = "test.json";
var location = ConfigLocations.AppCurrent;
// Act
bool resultWithNull = sourcesManager.Load (settingsScope, json: null, source, location);
bool resultWithEmpty = sourcesManager.Load (settingsScope, string.Empty, source, location);
// Assert
Assert.False (resultWithNull);
Assert.False (resultWithEmpty);
}
[Fact]
public void Update_WithValidJson_UpdatesSettingsScope ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
settingsScope.LoadHardCodedDefaults ();
settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
var json = """
{
"Application.QuitKey": "Ctrl+Z"
}
""";
var source = "test.json";
var location = ConfigLocations.HardCoded;
// Act
bool result = sourcesManager.Load (settingsScope, json, source, location);
// Assert
Assert.True (result);
Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue as Key);
Assert.Contains (source, sourcesManager.Sources.Values);
}
#endregion
#region Load
[Fact]
public void Load_WithNullResourceName_ReturnsFalse ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
settingsScope.LoadHardCodedDefaults ();
settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
var assembly = Assembly.GetExecutingAssembly ();
var location = ConfigLocations.AppResources;
// Act
bool result = sourcesManager.Load (settingsScope, assembly, null, location);
// Assert
Assert.False (result);
}
[Fact]
public void Load_WithValidResource_UpdatesSettingsScope ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
var resourceName = "Terminal.Gui.Resources.config.json";
var location = ConfigLocations.LibraryResources;
// Act
bool result = sourcesManager.Load (settingsScope, assembly!, resourceName, location);
// Assert
Assert.True (result);
// Verify settingsScope is updated as expected
}
[Fact]
public void Load_Runtime_Overrides ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
var resourceName = "Terminal.Gui.Resources.config.json";
var location = ConfigLocations.LibraryResources;
sourcesManager.Load (settingsScope, assembly!, resourceName, location);
Assert.Equal (Key.Esc, settingsScope ["Application.QuitKey"].PropertyValue);
var runtimeJson = """
{
"Application.QuitKey": "Ctrl+Z"
}
""";
var runtimeSource = "runtime.json";
var runtimeLocation = ConfigLocations.Runtime;
var runtimeStream = new MemoryStream ();
var writer = new StreamWriter (runtimeStream);
writer.Write (runtimeJson);
writer.Flush ();
runtimeStream.Position = 0;
// Act
bool result = sourcesManager.Load (settingsScope, runtimeStream, runtimeSource, runtimeLocation);
// Assert
Assert.True (result);
// Verify settingsScope is updated as expected
Assert.Equal (Key.Z.WithCtrl, settingsScope ["Application.QuitKey"].PropertyValue);
}
#endregion
#region ToJson and ToStream
[Fact]
public void ToJson_WithValidScope_ReturnsJsonString ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
settingsScope.LoadHardCodedDefaults ();
settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
// Act
string json = sourcesManager.ToJson (settingsScope);
// Assert
Assert.Contains ("""Application.QuitKey": "Ctrl+Q""", json);
}
[Fact]
public void ToStream_WithValidScope_ReturnsStream ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
settingsScope.LoadHardCodedDefaults ();
settingsScope ["Application.QuitKey"].PropertyValue = Key.Q.WithCtrl;
// Act
var stream = sourcesManager.ToStream (settingsScope);
// Assert
Assert.NotNull (stream);
stream.Position = 0;
var reader = new StreamReader (stream);
string json = reader.ReadToEnd ();
Assert.Contains ("""Application.QuitKey": "Ctrl+Q""", json);
}
#endregion
#region Sources Dictionary Tests
[Fact]
public void Sources_Dictionary_IsInitializedEmpty ()
{
// Arrange & Act
var sourcesManager = new SourcesManager ();
// Assert
Assert.NotNull (sourcesManager.Sources);
Assert.Empty (sourcesManager.Sources);
}
[Fact]
public void Update_WhenCalledMultipleTimes_MaintainsLastSourceForLocation ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
// Act - Update with first source for location
var firstSource = "first.json";
sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+A"}""", firstSource, ConfigLocations.Runtime);
// Update with second source for same location
var secondSource = "second.json";
sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+B"}""", secondSource, ConfigLocations.Runtime);
// Assert - Only the last source should be stored for the location
Assert.Single (sourcesManager.Sources);
Assert.Equal (secondSource, sourcesManager.Sources [ConfigLocations.Runtime]);
}
[Fact]
public void Update_WithDifferentLocations_AddsAllSourcesToCollection ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
ConfigLocations [] locations =
[
ConfigLocations.LibraryResources,
ConfigLocations.Runtime,
ConfigLocations.AppCurrent,
ConfigLocations.GlobalHome
];
// Act - Update with different sources for different locations
foreach (var location in locations)
{
var source = $"config-{location}.json";
sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", source, location);
}
// Assert
Assert.Equal (locations.Length, sourcesManager.Sources.Count);
foreach (var location in locations)
{
Assert.Contains (location, sourcesManager.Sources.Keys);
Assert.Equal ($"config-{location}.json", sourcesManager.Sources [location]);
}
}
[Fact]
public void Load_AddsResourceSourceToCollection ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
var assembly = Assembly.GetAssembly (typeof (ConfigurationManager));
var resourceName = "Terminal.Gui.Resources.config.json";
var location = ConfigLocations.LibraryResources;
// Act
bool result = sourcesManager.Load (settingsScope, assembly!, resourceName, location);
// Assert
Assert.True (result);
Assert.Contains (location, sourcesManager.Sources.Keys);
Assert.Equal ($"resource://[{assembly!.GetName ().Name}]/{resourceName}", sourcesManager.Sources [location]);
}
[Fact]
public void Update_WithNonExistentFileAndDifferentLocations_TracksAllSources ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
// Define multiple files and locations
var fileLocations = new Dictionary<string, ConfigLocations> (StringComparer.InvariantCultureIgnoreCase)
{
{ "file1.json", ConfigLocations.AppCurrent },
{ "file2.json", ConfigLocations.GlobalHome },
{ "file3.json", ConfigLocations.AppHome }
};
// Act
foreach (var pair in fileLocations)
{
sourcesManager.Load (settingsScope, pair.Key, pair.Value);
}
// Assert
Assert.Equal (fileLocations.Count, sourcesManager.Sources.Count);
foreach (var pair in fileLocations)
{
Assert.Contains (pair.Value, sourcesManager.Sources.Keys);
Assert.Equal (pair.Key, sourcesManager.Sources [pair.Value]);
}
}
[Fact]
public void Sources_IsPreservedAcrossOperations ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
// First operation - file update
var filePath = "testfile.json";
var location1 = ConfigLocations.AppCurrent;
sourcesManager.Load (settingsScope, filePath, location1);
// Second operation - json string update
var jsonSource = "jsonstring";
var location2 = ConfigLocations.Runtime;
sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", jsonSource, location2);
// Perform a stream operation
var streamSource = "streamdata";
var location3 = ConfigLocations.GlobalCurrent;
var stream = new MemoryStream ();
var writer = new StreamWriter (stream);
writer.Write ("""{"Application.QuitKey": "Ctrl+Z"}""");
writer.Flush ();
stream.Position = 0;
sourcesManager.Load (settingsScope, stream, streamSource, location3);
// Assert - all sources should be preserved
Assert.Equal (3, sourcesManager.Sources.Count);
Assert.Equal (filePath, sourcesManager.Sources [location1]);
Assert.Equal (jsonSource, sourcesManager.Sources [location2]);
Assert.Equal (streamSource, sourcesManager.Sources [location3]);
}
[Fact]
public void Sources_StaysConsistentWhenUpdateFails ()
{
// Arrange
var sourcesManager = new SourcesManager ();
var settingsScope = new SettingsScope ();
// Add one successful source
var validSource = "valid.json";
var validLocation = ConfigLocations.Runtime;
sourcesManager.Load (settingsScope, """{"Application.QuitKey": "Ctrl+Z"}""", validSource, validLocation);
try
{
// Configure to throw on errors
ConfigurationManager.ThrowOnJsonErrors = true;
// Act & Assert - attempt to update with invalid JSON
var invalidSource = "invalid.json";
var invalidLocation = ConfigLocations.AppCurrent;
var invalidJson = "{ invalid json }";
Assert.Throws<JsonException> (
() =>
sourcesManager.Load (settingsScope, invalidJson, invalidSource, invalidLocation));
// The valid source should still be there
Assert.Single (sourcesManager.Sources);
Assert.Equal (validSource, sourcesManager.Sources [validLocation]);
// The invalid source should not have been added
Assert.DoesNotContain (invalidLocation, sourcesManager.Sources.Keys);
}
finally
{
// Reset for other tests
ConfigurationManager.ThrowOnJsonErrors = false;
}
}
#endregion
}