From 69a79e297d46ac445efd82465da643015ebc15e5 Mon Sep 17 00:00:00 2001
From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com>
Date: Sun, 9 Mar 2025 12:49:28 -0500
Subject: [PATCH] tests: work on writing tests for console functionality
---
.../AltGen.Console.Tests.csproj | 3 +-
.../Unit/AddConfigCommandTests.cs | 10 +++
.../Unit/AppSettingsTests.cs | 78 +++++++++++++++++++
.../Unit/RemoveConfigCommandTests.cs | 18 +++++
src/AltGen.Console.Tests/Usings.cs | 11 ++-
src/AltGen.Console/Common/JsonOptions.cs | 11 +++
src/AltGen.Console/Config/AddConfigCommand.cs | 12 +--
.../Config/RemoveConfigCommand.cs | 7 +-
8 files changed, 134 insertions(+), 16 deletions(-)
create mode 100644 src/AltGen.Console/Common/JsonOptions.cs
diff --git a/src/AltGen.Console.Tests/AltGen.Console.Tests.csproj b/src/AltGen.Console.Tests/AltGen.Console.Tests.csproj
index 7995a27..94aed1c 100644
--- a/src/AltGen.Console.Tests/AltGen.Console.Tests.csproj
+++ b/src/AltGen.Console.Tests/AltGen.Console.Tests.csproj
@@ -10,10 +10,11 @@
+
-
+
diff --git a/src/AltGen.Console.Tests/Unit/AddConfigCommandTests.cs b/src/AltGen.Console.Tests/Unit/AddConfigCommandTests.cs
index 2e68060..dc5fb23 100644
--- a/src/AltGen.Console.Tests/Unit/AddConfigCommandTests.cs
+++ b/src/AltGen.Console.Tests/Unit/AddConfigCommandTests.cs
@@ -2,5 +2,15 @@ namespace AltGen.Console.Tests.Unit;
public class AddConfigCommandTests
{
+ [Fact]
+ public Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldCreateSettings()
+ {
+ throw new NotImplementedException();
+ }
+ [Fact]
+ public Task ExecuteAsync_WhenSettingsExist_ItShouldUpdateSettings()
+ {
+ throw new NotImplementedException();
+ }
}
\ No newline at end of file
diff --git a/src/AltGen.Console.Tests/Unit/AppSettingsTests.cs b/src/AltGen.Console.Tests/Unit/AppSettingsTests.cs
index c7a67d0..bb5b868 100644
--- a/src/AltGen.Console.Tests/Unit/AppSettingsTests.cs
+++ b/src/AltGen.Console.Tests/Unit/AppSettingsTests.cs
@@ -2,4 +2,82 @@ namespace AltGen.Console.Tests.Unit;
public class AppSettingsTests
{
+ readonly Mock _fileSystemMock = new();
+
+ [Fact]
+ public void AddOrUpdateProvider_WhenProviderDoesNotExist_ItShouldAddProvider()
+ {
+ var providerSettings = new ProviderSettings("provider", "key", true);
+
+ var commandSettings = new AddConfigCommand.Settings(_fileSystemMock.Object)
+ {
+ Provider = providerSettings.Provider,
+ Key = providerSettings.Key,
+ Default = providerSettings.Default,
+ };
+
+ var appSettings = new AppSettings([]);
+
+ var updatedAppSettings = appSettings.AddOrUpdateProvider(commandSettings);
+
+ updatedAppSettings.Should().BeEquivalentTo(new AppSettings([providerSettings]));
+ }
+
+ [Fact]
+ public void AddOrUpdateProvider_WhenProviderDoesExist_ItShouldUpdateProvider()
+ {
+ var providerSettings = new ProviderSettings("provider", "key", true);
+ var existingProviderSettings = new ProviderSettings("provider", "key", false);
+
+ var commandSettings = new AddConfigCommand.Settings(_fileSystemMock.Object)
+ {
+ Provider = providerSettings.Provider,
+ Key = providerSettings.Key,
+ Default = providerSettings.Default,
+ };
+
+ var appSettings = new AppSettings([existingProviderSettings]);
+ var updatedAppSettings = appSettings.AddOrUpdateProvider(commandSettings);
+
+ updatedAppSettings.Should().BeEquivalentTo(new AppSettings([providerSettings]));
+ }
+
+ [Fact]
+ public void AddOrUpdateProvider_WhenExistingProviderIsSetToDefaultAndNewDefaultGiven_ItShouldUpdateProvidersCorrectly()
+ {
+ var providerSettings = new ProviderSettings("claude", "key", true);
+ var existingProviderSettings = new ProviderSettings("gemini", "key", true);
+
+ var commandSettings = new AddConfigCommand.Settings(_fileSystemMock.Object)
+ {
+ Provider = providerSettings.Provider,
+ Key = providerSettings.Key,
+ Default = providerSettings.Default,
+ };
+
+ var appSettings = new AppSettings([existingProviderSettings]);
+ var updatedAppSettings = appSettings.AddOrUpdateProvider(commandSettings);
+
+ updatedAppSettings.Should().BeEquivalentTo(new AppSettings([
+ providerSettings,
+ existingProviderSettings with { Default = false }
+ ]));
+ }
+
+ [Fact]
+ public void RemoveProvider_WhenProviderExists_ItShouldRemoveProvider()
+ {
+ var existingProviderSettings = new ProviderSettings("provider", "key", true);
+
+ var commandSettings = new RemoveConfigCommand.Settings(_fileSystemMock.Object)
+ {
+ Provider = existingProviderSettings.Provider,
+ };
+
+ var appSettings = new AppSettings([existingProviderSettings]);
+
+ var updatedAppSettings = appSettings.RemoveProvider(commandSettings);
+
+ updatedAppSettings.Should().BeEquivalentTo(new AppSettings([]));
+ }
}
\ No newline at end of file
diff --git a/src/AltGen.Console.Tests/Unit/RemoveConfigCommandTests.cs b/src/AltGen.Console.Tests/Unit/RemoveConfigCommandTests.cs
index 2a52faf..50dea56 100644
--- a/src/AltGen.Console.Tests/Unit/RemoveConfigCommandTests.cs
+++ b/src/AltGen.Console.Tests/Unit/RemoveConfigCommandTests.cs
@@ -2,4 +2,22 @@ namespace AltGen.Console.Tests.Unit;
public class RemoveConfigCommandTests
{
+ [Fact]
+ public Task ExecuteAsync_WhenSettingsDoNotExist_ItShouldThrow()
+ {
+ throw new NotImplementedException();
+ }
+
+
+ [Fact]
+ public Task ExecuteAsync_WhenProviderDoesNotExist_ItShouldDoNothing()
+ {
+ throw new NotImplementedException();
+ }
+
+ [Fact]
+ public Task ExecuteAsync_WhenProviderExists_ItShouldRemoveProviderFromSettings()
+ {
+ throw new NotImplementedException();
+ }
}
\ No newline at end of file
diff --git a/src/AltGen.Console.Tests/Usings.cs b/src/AltGen.Console.Tests/Usings.cs
index a5fd333..cf6c14c 100644
--- a/src/AltGen.Console.Tests/Usings.cs
+++ b/src/AltGen.Console.Tests/Usings.cs
@@ -1,3 +1,10 @@
-global using RichardSzalay.MockHttp;
+global using System.IO.Abstractions;
-global using AltGen.Console.Generate;
\ No newline at end of file
+global using AltGen.Console.Config;
+global using AltGen.Console.Generate;
+
+global using FluentAssertions;
+
+global using Moq;
+
+global using RichardSzalay.MockHttp;
diff --git a/src/AltGen.Console/Common/JsonOptions.cs b/src/AltGen.Console/Common/JsonOptions.cs
new file mode 100644
index 0000000..f28d252
--- /dev/null
+++ b/src/AltGen.Console/Common/JsonOptions.cs
@@ -0,0 +1,11 @@
+namespace AltGen.Console.Common;
+
+static class JsonOptions
+{
+ public static JsonSerializerOptions Default { get; } = new()
+ {
+ PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
+ PropertyNameCaseInsensitive = true,
+ WriteIndented = true
+ };
+}
\ No newline at end of file
diff --git a/src/AltGen.Console/Config/AddConfigCommand.cs b/src/AltGen.Console/Config/AddConfigCommand.cs
index 176b38c..597bd39 100644
--- a/src/AltGen.Console/Config/AddConfigCommand.cs
+++ b/src/AltGen.Console/Config/AddConfigCommand.cs
@@ -5,12 +5,6 @@ sealed class AddConfigCommand(
IFileSystem fileSystem
) : AsyncCommand
{
- static readonly JsonSerializerOptions JsonOptions = new()
- {
- PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
- PropertyNameCaseInsensitive = true,
- WriteIndented = true
- };
readonly IAnsiConsole _console = console;
readonly IFileSystem _fileSystem = fileSystem;
@@ -43,17 +37,17 @@ sealed class AddConfigCommand(
new(settings.Provider, settings.Key, settings.Default)
]);
- var json = JsonSerializer.Serialize(appSettings, JsonOptions);
+ var json = JsonSerializer.Serialize(appSettings, JsonOptions.Default);
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, json);
_console.MarkupLine($"[bold]{settings.Provider}[/] has been configured.");
return 0;
}
var existingJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
- var existingAppSettings = JsonSerializer.Deserialize(existingJson, JsonOptions)
+ var existingAppSettings = JsonSerializer.Deserialize(existingJson, JsonOptions.Default)
?? throw new ConfigException("Failed to deserialize settings.");
var updatedAppSettings = existingAppSettings.AddOrUpdateProvider(settings);
- var udpatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions);
+ var udpatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, udpatedJson);
_console.MarkupLine($"[bold]{settings.Provider}[/] has been configured.");
return 0;
diff --git a/src/AltGen.Console/Config/RemoveConfigCommand.cs b/src/AltGen.Console/Config/RemoveConfigCommand.cs
index 47fdd8f..3af4423 100644
--- a/src/AltGen.Console/Config/RemoveConfigCommand.cs
+++ b/src/AltGen.Console/Config/RemoveConfigCommand.cs
@@ -25,14 +25,13 @@ sealed class RemoveConfigCommand(
if (exists is false)
{
- _console.MarkupLine("[bold]No existing settings found.[/]");
- return 1;
+ throw new ConfigException("No existing settings found.");
}
var settingsJson = await _fileSystem.File.ReadAllTextAsync(settings.SettingsPath);
- var appSettings = JsonSerializer.Deserialize(settingsJson) ?? throw new ConfigException("Failed to deserialize app settings.");
+ var appSettings = JsonSerializer.Deserialize(settingsJson, JsonOptions.Default) ?? throw new ConfigException("Failed to deserialize app settings.");
var updatedAppSettings = appSettings.RemoveProvider(settings);
- var updatedJson = JsonSerializer.Serialize(updatedAppSettings);
+ var updatedJson = JsonSerializer.Serialize(updatedAppSettings, JsonOptions.Default);
await _fileSystem.File.WriteAllTextAsync(settings.SettingsPath, updatedJson);
_console.MarkupLine($"[bold]{settings.Provider}[/] has been removed.");
return 0;