using Chronos.Core.IO; using Chronos.Data.Projects; using Chronos.Data.Sync; using Chronos.Protocol; using Chronos.Testing; namespace Chronos.Data.Tests.Sync; /// How the desktop names a server (owner review 5, #66): by its announced id, its address keys kept as aliases. [Category("sync")] public sealed class ServerIdentityTests { private static readonly Guid Id = Guid.Parse("0192a4c1-7b3e-7cc0-8a51-2f9d0e6b1a01"); private static ServerInfo Info(Guid? id) => new("Studio NAS", "1.1.0", 1, [], null, [], new ServerLimits(1, 1), null, null, id); [Test] public void AServerThatAnnouncesAnIdIsKeyedByIt() { var key = ServerIdentity.Key(Info(Id), new Uri("https://NAS.local:8443/")); Assert.Equal("id:" + Id.ToString("N"), key); Assert.True(ServerIdentity.IsIdKey(key)); Assert.Equal(Id, ServerIdentity.IdOf(key)); // Another address of the same server: the same key. Assert.Equal(key, ServerIdentity.Key(Info(Id), new Uri("https://192.168.1.5:8443"))); } [Test] public void AnOlderServerKeepsItsAddressKey() { var address = new Uri("https://NAS.local:8443/"); Assert.Equal("https://nas.local:8443", ServerIdentity.Key(Info(null), address)); Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key((ServerInfo?)null, address)); Assert.Equal(SyncService.ServerKey(address), ServerIdentity.Key(Guid.Empty, address)); Assert.False(ServerIdentity.IsIdKey(SyncService.ServerKey(address))); } [Test] public void AliasesNameTheServerOfTheirId() { var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443", "https://192.168.1.5:8443"])]); var key = ServerIdentity.IdKey(Id); Assert.Equal(key, identity.Canonical("https://NAS.local:8443")); Assert.Equal(key, identity.Canonical(key.ToUpperInvariant())); Assert.Equal("https://other:1", identity.Canonical("https://other:1")); Assert.True(identity.Same("https://nas.local:8443", "https://192.168.1.5:8443")); Assert.True(identity.Same(key, "https://192.168.1.5:8443")); Assert.False(identity.Same(key, "https://other:1")); Assert.True(identity.Same(null, null)); Assert.False(identity.Same(key, null)); Assert.Equal(Id.ToString("N"), identity.FolderName("https://192.168.1.5:8443")); Assert.Equal(HomeFolders.ServerFolder("https://other:1"), identity.FolderName("https://other:1")); Assert.SequenceEqual(new[] { key, "https://nas.local:8443", "https://192.168.1.5:8443" }, identity.KeysOf("https://192.168.1.5:8443")); Assert.SequenceEqual(new[] { "https://other:1" }, identity.KeysOf("https://other:1")); Assert.Equal(2, identity.AliasesOf(Id).Count); // Without the table every key is its own. Assert.False(ServerIdentity.None.Same(key, "https://nas.local:8443")); } [Test] public void AnAddressChangeKeepsTheKeyOnceTheAliasIsKnown() { var identity = new ServerIdentity([new KnownServerKey(Id, ["https://nas.local:8443"])]); var widened = identity.With(Id, "https://nas.local:9443"); Assert.True(widened.Same("https://nas.local:8443", "https://nas.local:9443")); Assert.Same(widened, widened.With(Id, "HTTPS://nas.local:9443")); Assert.False(identity.Same("https://nas.local:8443", "https://nas.local:9443"), "the table it came from is unchanged"); var other = Guid.CreateVersion7(); Assert.Equal(ServerIdentity.IdKey(other), widened.With(other, "https://second:1").Canonical("https://second:1")); // An address two ids claim stays with the first one listed. var clash = new ServerIdentity([new KnownServerKey(Id, ["https://x"]), new KnownServerKey(other, ["https://x"])]); Assert.Equal(ServerIdentity.IdKey(Id), clash.Canonical("https://x")); } [Test] public void TheArchiveIdOfAnyAliasIsTheServers() { string[] keys = [ServerIdentity.IdKey(Id), "https://nas.local:8443"]; Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://nas.local:8443"), keys)); Assert.True(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf(keys[0]), keys)); Assert.False(ServerProjectHome.IsArchiveIdOf(ServerProjectHome.ArchiveIdOf("https://other:1"), keys)); } }