using System.Text.Json; using FluentAssertions; using Xunit; namespace VRCAuthProxy.Tests.Unit { public class ConfigTests { [Fact] public void Config_LoadsAccountsFromJsonFile() { // Arrange var tempFile = Path.GetTempFileName(); var configData = new { accounts = new object[] { new { username = "user1", password = "pass1", totpSecret = "secret1" }, new { username = "user2", password = "pass2" } } }; File.WriteAllText(tempFile, JsonSerializer.Serialize(configData)); try { // Override the default config path for testing string originalConfigPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json"); string backupPath = originalConfigPath + ".bak"; if (File.Exists(originalConfigPath)) { File.Move(originalConfigPath, backupPath, true); } File.Copy(tempFile, originalConfigPath, true); // Act var config = Config.Load(); // Assert config.Should().NotBeNull(); config.Accounts.Should().HaveCount(2); config.Accounts[0].username.Should().Be("user1"); config.Accounts[0].password.Should().Be("pass1"); config.Accounts[0].totpSecret.Should().Be("secret1"); config.Accounts[1].username.Should().Be("user2"); config.Accounts[1].password.Should().Be("pass2"); config.Accounts[1].totpSecret.Should().BeNull(); // Restore original config if (File.Exists(backupPath)) { File.Move(backupPath, originalConfigPath, true); } } finally { // Clean up the temp file if (File.Exists(tempFile)) { File.Delete(tempFile); } } } [Fact] public void Config_Save_WritesConfigToFile() { // Arrange var tempFile = Path.GetTempFileName(); var originalConfigPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json"); var backupPath = originalConfigPath + ".bak"; if (File.Exists(originalConfigPath)) { File.Move(originalConfigPath, backupPath, true); } File.Copy(tempFile, originalConfigPath, true); try { var config = new Config { Accounts = new List { new ConfigAccount { username = "test1", password = "pass1", totpSecret = "secret1" } } }; // Act config.Save(); // Assert File.Exists(originalConfigPath).Should().BeTrue(); var loadedConfig = JsonSerializer.Deserialize(File.ReadAllText(originalConfigPath)); if (loadedConfig != null) { loadedConfig.accounts.Should().HaveCount(1); loadedConfig.accounts[0].username.Should().Be("test1"); loadedConfig.accounts[0].password.Should().Be("pass1"); loadedConfig.accounts[0].totpSecret.Should().Be("secret1"); } // Restore original config if (File.Exists(backupPath)) { File.Move(backupPath, originalConfigPath, true); } } finally { // Clean up the temp file if (File.Exists(tempFile)) { File.Delete(tempFile); } } } } }