tests: work on writing tests for console functionality

This commit is contained in:
Stevan Freeborn
2025-03-09 12:49:28 -05:00
parent 5647070fd3
commit 69a79e297d
8 changed files with 134 additions and 16 deletions
@@ -10,10 +10,11 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageReference Include="FluentAssertions" Version="7.1.0" />
<PackageReference Include="FluentAssertions" Version="7.2.0" />
</ItemGroup>
<ItemGroup>
@@ -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();
}
}
@@ -2,4 +2,82 @@ namespace AltGen.Console.Tests.Unit;
public class AppSettingsTests
{
readonly Mock<IFileSystem> _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([]));
}
}
@@ -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();
}
}
+8 -1
View File
@@ -1,3 +1,10 @@
global using RichardSzalay.MockHttp;
global using System.IO.Abstractions;
global using AltGen.Console.Config;
global using AltGen.Console.Generate;
global using FluentAssertions;
global using Moq;
global using RichardSzalay.MockHttp;
+11
View File
@@ -0,0 +1,11 @@
namespace AltGen.Console.Common;
static class JsonOptions
{
public static JsonSerializerOptions Default { get; } = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
WriteIndented = true
};
}
@@ -5,12 +5,6 @@ sealed class AddConfigCommand(
IFileSystem fileSystem
) : AsyncCommand<AddConfigCommand.Settings>
{
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<AppSettings>(existingJson, JsonOptions)
var existingAppSettings = JsonSerializer.Deserialize<AppSettings>(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;
@@ -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<AppSettings>(settingsJson) ?? throw new ConfigException("Failed to deserialize app settings.");
var appSettings = JsonSerializer.Deserialize<AppSettings>(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;