using VoiceCat.Core; namespace VoiceCat.Tests; public sealed class ServerProfileTests { [Fact] public void ProfilesRoundTripWithoutPasswordMaterial() { string directory = Path.Combine(Path.GetTempPath(), "voicecat-profile-" + Guid.NewGuid().ToString("N")); string path = Path.Combine(directory, "servers.json"); try { var guest = ServerProfile.Create(" voice.example ", 8384, ServerAuthentication.Guest, nickname: " Cat "); var account = ServerProfile.Create("secure.example", 9443, ServerAuthentication.Account, username: " talon "); var store = new ServerProfileStore(path); store.Save([guest, account]); Assert.Equal([guest, account], store.Load()); string json = File.ReadAllText(path); Assert.Contains("\"authentication\": \"Account\"", json); Assert.DoesNotContain("password", json, StringComparison.OrdinalIgnoreCase); } finally { if (Directory.Exists(directory)) Directory.Delete(directory, true); } } [Fact] public void MissingCorruptAndInvalidProfilesDoNotBreakStartup() { string directory = Path.Combine(Path.GetTempPath(), "voicecat-profile-" + Guid.NewGuid().ToString("N")); string path = Path.Combine(directory, "servers.json"); try { var store = new ServerProfileStore(path); Assert.Empty(store.Load()); Directory.CreateDirectory(directory); File.WriteAllText(path, "not json"); Assert.Empty(store.Load()); File.WriteAllText(path, "[{\"id\":\"00000000-0000-0000-0000-000000000000\",\"host\":\"\",\"port\":0,\"authentication\":\"Guest\"}]"); Assert.Empty(store.Load()); } finally { if (Directory.Exists(directory)) Directory.Delete(directory, true); } } [Fact] public void AccountProfilesRequireAUsername() { Assert.Throws(() => ServerProfile.Create("voice.example", 8384, ServerAuthentication.Account)); } [Fact] public void SwiftProfilesImportAndAreBackedUpOnManagedSave() { string directory = Path.Combine(Path.GetTempPath(), "voicecat-profile-" + Guid.NewGuid().ToString("N")); string path = Path.Combine(directory, "servers.json"); Guid accountId = Guid.NewGuid(); try { Directory.CreateDirectory(directory); File.WriteAllText(path, $$""" [{"id":"{{accountId:D}}","host":"voice.example","port":8384,"authMode":"password","savedUsername":"talon","nickname":null,"keychainTag":"voicecat.server.legacy"}] """); var store = new ServerProfileStore(path); ServerProfile profile = Assert.Single(store.Load()); Assert.Equal(ServerAuthentication.Account, profile.Authentication); Assert.Equal("talon", profile.Username); Assert.Equal("voicecat.server.legacy", profile.LegacyKeychainTag); store.Save([profile]); Assert.True(File.Exists(path + ".swift-backup.json")); string managed = File.ReadAllText(path); Assert.Contains("\"authentication\": \"Account\"", managed); Assert.DoesNotContain("keychainTag", managed); } finally { if (Directory.Exists(directory)) Directory.Delete(directory, true); } } }