From 7a6ab1ea3d84d1a31c28453debdbd1ecb3e9a7bf Mon Sep 17 00:00:00 2001 From: YoruDev-Ryland <159859604+YoruDev-Ryland@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:59:24 -0500 Subject: [PATCH 1/3] feat(bank): the Bank can take changes from another device Additions the sync library builds on; Local mode behaves exactly as before. - FileBankService.ApplyRemote: applies a batch from a sync server in one index write (checked first, so a refused batch changes nothing), plus IsReadOnly and Reload. - BankDecor groups box names, wallpapers and a new BankSettings file (bank-settings.json beside the index) so they can sync as part of the Bank. Each raises Changed on local edits only, and can be replaced or reloaded. --- src/PKForge.Infrastructure/BankBoxNames.cs | 24 ++++- .../BankBoxWallpapers.cs | 24 ++++- src/PKForge.Infrastructure/BankDecor.cs | 27 +++++ src/PKForge.Infrastructure/BankSettings.cs | 97 +++++++++++++++++ src/PKForge.Infrastructure/BoxKeyedFile.cs | 22 ++++ src/PKForge.Infrastructure/FileBankService.cs | 82 +++++++++++++- src/PKForge.Infrastructure/RemoteEntry.cs | 9 ++ tests/PKForge.Domain.Tests/BankDecorTests.cs | 73 +++++++++++++ .../FileBankServiceRemoteTests.cs | 100 ++++++++++++++++++ 9 files changed, 453 insertions(+), 5 deletions(-) create mode 100644 src/PKForge.Infrastructure/BankDecor.cs create mode 100644 src/PKForge.Infrastructure/BankSettings.cs create mode 100644 src/PKForge.Infrastructure/RemoteEntry.cs create mode 100644 tests/PKForge.Domain.Tests/BankDecorTests.cs create mode 100644 tests/PKForge.Domain.Tests/FileBankServiceRemoteTests.cs diff --git a/src/PKForge.Infrastructure/BankBoxNames.cs b/src/PKForge.Infrastructure/BankBoxNames.cs index a3985f1b..b39ed28a 100644 --- a/src/PKForge.Infrastructure/BankBoxNames.cs +++ b/src/PKForge.Infrastructure/BankBoxNames.cs @@ -11,12 +11,32 @@ public sealed class BankBoxNames(string bankRootDirectory) { private readonly BoxKeyedFile _file = new(bankRootDirectory, "box-names.json"); + /// Raised after this device changed a name (not when another device's names are applied). + public event EventHandler? Changed; + /// The name given to , or null when it has none. public string? Get(int box) => _file.Get(box); /// Sets several names in one write; a blank name clears that box's name. - public void SetMany(IEnumerable<(int Box, string? Name)> names) => _file.SetMany(names); + public void SetMany(IEnumerable<(int Box, string? Name)> names) + { + _file.SetMany(names); + Changed?.Invoke(this, EventArgs.Empty); + } /// Names follow their boxes through . - public void Remap(BankBoxRemap remap) => _file.Remap(remap); + public void Remap(BankBoxRemap remap) + { + _file.Remap(remap); + Changed?.Invoke(this, EventArgs.Empty); + } + + /// Reads the file again, after it was put back from a copy. + public void Reload() => _file.Reload(); + + /// Every box name, by box. + public IReadOnlyDictionary All() => _file.All(); + + /// Replaces every name in one write. + public void ReplaceAll(IReadOnlyDictionary names) => _file.ReplaceAll(names); } diff --git a/src/PKForge.Infrastructure/BankBoxWallpapers.cs b/src/PKForge.Infrastructure/BankBoxWallpapers.cs index 4ab22446..62ea4cbc 100644 --- a/src/PKForge.Infrastructure/BankBoxWallpapers.cs +++ b/src/PKForge.Infrastructure/BankBoxWallpapers.cs @@ -11,11 +11,31 @@ public sealed class BankBoxWallpapers(string bankRootDirectory) { private readonly BoxKeyedFile _file = new(bankRootDirectory, "box-wallpapers.json"); + /// Raised after this device changed a wallpaper (not when another device's wallpapers are applied). + public event EventHandler? Changed; + /// The wallpaper chosen for , or null for its default. public BankWallpaper? Get(int box) => BankWallpaper.Parse(_file.Get(box)); - public void Set(int box, BankWallpaper? wallpaper) => _file.SetMany([(box, wallpaper?.Id)]); + public void Set(int box, BankWallpaper? wallpaper) + { + _file.SetMany([(box, wallpaper?.Id)]); + Changed?.Invoke(this, EventArgs.Empty); + } /// Wallpapers follow their boxes through . - public void Remap(BankBoxRemap remap) => _file.Remap(remap); + public void Remap(BankBoxRemap remap) + { + _file.Remap(remap); + Changed?.Invoke(this, EventArgs.Empty); + } + + /// Reads the file again, after it was put back from a copy. + public void Reload() => _file.Reload(); + + /// Every chosen wallpaper's stored id, by box. + public IReadOnlyDictionary AllIds() => _file.All(); + + /// Replaces every wallpaper choice in one write. + public void ReplaceAllIds(IReadOnlyDictionary ids) => _file.ReplaceAll(ids); } diff --git a/src/PKForge.Infrastructure/BankDecor.cs b/src/PKForge.Infrastructure/BankDecor.cs new file mode 100644 index 00000000..65c1c6c4 --- /dev/null +++ b/src/PKForge.Infrastructure/BankDecor.cs @@ -0,0 +1,27 @@ +using PKForge.Domain; + +namespace PKForge.Infrastructure; + +/// Everything about the Bank besides its Pokémon (box names, wallpapers, settings), kept in its folder. +public sealed class BankDecor(string bankRootDirectory) +{ + public BankBoxNames Names { get; } = new(bankRootDirectory); + public BankBoxWallpapers Wallpapers { get; } = new(bankRootDirectory); + public BankSettings Settings { get; } = new(bankRootDirectory); + + /// Reads every file again, after the Bank folder was put back from a copy. + public void Reload() + { + Names.Reload(); + Wallpapers.Reload(); + Settings.Reload(); + } + + /// Moves names, wallpapers and the living dex region with their boxes. + public void Remap(BankBoxRemap remap) + { + Names.Remap(remap); + Wallpapers.Remap(remap); + Settings.Remap(remap); + } +} diff --git a/src/PKForge.Infrastructure/BankSettings.cs b/src/PKForge.Infrastructure/BankSettings.cs new file mode 100644 index 00000000..622c5188 --- /dev/null +++ b/src/PKForge.Infrastructure/BankSettings.cs @@ -0,0 +1,97 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using PKForge.Domain; + +namespace PKForge.Infrastructure; + +/// +/// Bank-wide settings that follow the Bank between devices, kept beside its index as +/// bank-settings.json: one JSON object, so keys a newer version adds survive this one. +/// A missing or unreadable file reads as no settings. +/// +public sealed class BankSettings +{ + private const string LivingDexStartBoxKey = "livingDexStartBox"; + private readonly string _path; + private readonly Lock _gate = new(); + private JsonObject _values; + + /// Raised after this device changed a setting (not when another device's settings are applied). + public event EventHandler? Changed; + + public BankSettings(string bankRootDirectory) + { + Directory.CreateDirectory(bankRootDirectory); + _path = Path.Combine(bankRootDirectory, "bank-settings.json"); + _values = Load(_path); + } + + /// First Bank box of the living dex region, or null before a run created one. + public int? LivingDexStartBox + { + get + { + lock (_gate) + return _values[LivingDexStartBoxKey] is JsonValue value && value.TryGetValue(out var box) && box >= 0 ? box : null; + } + set + { + lock (_gate) + { + if (value is { } box) _values[LivingDexStartBoxKey] = box; + else _values.Remove(LivingDexStartBoxKey); + Write(); + } + Changed?.Invoke(this, EventArgs.Empty); + } + } + + /// The living dex region follows its box through . + public void Remap(BankBoxRemap remap) + { + ArgumentNullException.ThrowIfNull(remap); + if (LivingDexStartBox is { } start) LivingDexStartBox = remap.Map(start); + } + + /// Every setting as one JSON object (what sync sends). + public JsonElement ToJson() + { + lock (_gate) return JsonSerializer.SerializeToElement(_values); + } + + /// Replaces every setting with another device's (what sync applies). + public void Replace(JsonElement settings) + { + lock (_gate) + { + _values = JsonNode.Parse(settings.GetRawText()) as JsonObject ?? new JsonObject(); + Write(); + } + } + + /// Reads the file again, after it was put back from a copy. + public void Reload() + { + lock (_gate) _values = Load(_path); + } + + private void Write() + { + var tmp = _path + ".tmp"; + File.WriteAllText(tmp, _values.ToJsonString()); + File.Move(tmp, _path, overwrite: true); + } + + private static JsonObject Load(string path) + { + try + { + if (File.Exists(path) && JsonNode.Parse(File.ReadAllText(path)) is JsonObject values) return values; + } + catch (Exception error) when (error is IOException or JsonException or UnauthorizedAccessException) + { + // Settings are optional: an unreadable file means defaults. + } + return new JsonObject(); + } +} diff --git a/src/PKForge.Infrastructure/BoxKeyedFile.cs b/src/PKForge.Infrastructure/BoxKeyedFile.cs index 275b83a0..0c48a891 100644 --- a/src/PKForge.Infrastructure/BoxKeyedFile.cs +++ b/src/PKForge.Infrastructure/BoxKeyedFile.cs @@ -52,6 +52,28 @@ public void Remap(BankBoxRemap remap) } } + /// Reads the file again, after it was put back from a copy. + public void Reload() + { + lock (_gate) _values = Load(_path); + } + + /// Every value, by box. + public IReadOnlyDictionary All() + { + lock (_gate) return new Dictionary(_values); + } + + /// Replaces every value in one write (sync applying another device's decoration); blank values are dropped. + public void ReplaceAll(IReadOnlyDictionary values) + { + lock (_gate) + { + _values = values.Where(v => !string.IsNullOrWhiteSpace(v.Value)).ToDictionary(v => v.Key, v => v.Value.Trim()); + Write(); + } + } + private void Write() { var tmp = _path + ".tmp"; diff --git a/src/PKForge.Infrastructure/FileBankService.cs b/src/PKForge.Infrastructure/FileBankService.cs index 97b9796a..51fae830 100644 --- a/src/PKForge.Infrastructure/FileBankService.cs +++ b/src/PKForge.Infrastructure/FileBankService.cs @@ -19,7 +19,7 @@ public sealed class FileBankService : IBankService private int _migrationVersion; // Set when an index exists on disk but neither it nor its backup could be read: the bank // then stays read-only rather than overwrite the real index with an empty fallback. - private readonly bool _indexUnreadable; + private bool _indexUnreadable; public FileBankService(string rootDirectory) { @@ -33,6 +33,15 @@ public IReadOnlyList GetAll() lock (_gate) return _entries.ToList(); } + /// Reads the index from disk again, after its files were put back from a copy. + public void Reload() + { + lock (_gate) (_entries, _boxCount, _migrationVersion, _indexUnreadable) = LoadIndex(); + } + + /// The index could not be read, so the bank refuses every write and lists nothing. + public bool IsReadOnly => _indexUnreadable; + public int BoxCount { get { lock (_gate) return _boxCount; } @@ -354,6 +363,77 @@ public void RemapBoxes(BankBoxRemap remap) } } + /// + /// Applies changes that arrived from a sync server in one index write. Entries keep the server's ids, + /// boxes and slots. The whole batch is checked first (bytes for every new entry, slots in range, one + /// entry per slot afterwards), so a refused batch changes nothing. Bytes are written before the index, + /// as does; removed entries' bytes are deleted after it. The box count follows + /// but always fits every entry. + /// + public void ApplyRemote(IReadOnlyList upserts, IReadOnlyList removals, int? boxCount = null) + { + lock (_gate) + { + if (upserts.Count == 0 && removals.Count == 0 && (boxCount is null || boxCount == _boxCount)) return; + EnsureWritable(); + + var removing = removals.ToHashSet(); + var known = _entries.Select(e => e.Id).ToHashSet(); + var incoming = new Dictionary(upserts.Count); + foreach (var upsert in upserts) + { + var entry = upsert.Entry; + if (removing.Contains(entry.Id) || !incoming.TryAdd(entry.Id, upsert)) + throw new InvalidOperationException("A remote batch names an entry twice."); + if (entry.Box < 0 || entry.Slot < 0 || entry.Slot >= SlotsPerBox) + throw new ArgumentOutOfRangeException(nameof(upserts), "Box or slot out of range."); + if (upsert.Data is null && !known.Contains(entry.Id)) + throw new InvalidOperationException("A new remote entry arrived without its bytes."); + } + + var result = _entries + .Where(e => !removing.Contains(e.Id) && !incoming.ContainsKey(e.Id)) + .Concat(incoming.Values.Select(u => u.Entry)) + .ToList(); + var slots = new HashSet<(int, int)>(); + if (result.Any(e => !slots.Add((e.Box, e.Slot)))) + throw new InvalidOperationException("Two entries would share one slot."); + + var created = new List(); + try + { + foreach (var upsert in incoming.Values.Where(u => u.Data is not null)) + { + if (!known.Contains(upsert.Entry.Id)) created.Add(upsert.Entry.Id); + File.WriteAllBytes(DataPath(upsert.Entry.Id), upsert.Data!); + } + Commit(() => + { + _entries = result; + var needed = result.Count == 0 ? 1 : result.Max(e => e.Box) + 1; + _boxCount = Math.Max(Math.Max(1, boxCount ?? _boxCount), needed); + SaveIndex(); + return 0; + }); + } + catch + { + foreach (var id in created) + { + try { File.Delete(DataPath(id)); } + catch { /* an orphan .bin is harmless: the index never lists it */ } + } + throw; + } + + foreach (var id in removing.Where(known.Contains)) + { + try { File.Delete(DataPath(id)); } + catch { /* a leftover .bin is harmless: the index no longer lists it */ } + } + } + } + private (int Box, int Slot) FirstEmpty() { var occupied = _entries.Select(e => (e.Box, e.Slot)).ToHashSet(); diff --git a/src/PKForge.Infrastructure/RemoteEntry.cs b/src/PKForge.Infrastructure/RemoteEntry.cs new file mode 100644 index 00000000..68794ecf --- /dev/null +++ b/src/PKForge.Infrastructure/RemoteEntry.cs @@ -0,0 +1,9 @@ +using PKForge.Domain; + +namespace PKForge.Infrastructure; + +/// +/// A Bank entry as another device stored it (through the sync server). is null when +/// this Bank already holds the same bytes, as for a move or a facts-only change. +/// +public sealed record RemoteEntry(BankEntry Entry, byte[]? Data); diff --git a/tests/PKForge.Domain.Tests/BankDecorTests.cs b/tests/PKForge.Domain.Tests/BankDecorTests.cs new file mode 100644 index 00000000..89574e68 --- /dev/null +++ b/tests/PKForge.Domain.Tests/BankDecorTests.cs @@ -0,0 +1,73 @@ +using System.Text.Json; +using PKForge.Domain; +using PKForge.Infrastructure; +using Xunit; + +namespace PKForge.Domain.Tests; + +public sealed class BankDecorTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "pkforge-tests", Guid.NewGuid().ToString("N")); + + public void Dispose() + { + if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); + } + + [Fact] + public void NamesAndWallpapersAreReadAndReplacedWhole() + { + var decor = new BankDecor(_root); + decor.Names.SetMany([(0, "Starters"), (2, "Legends")]); + decor.Wallpapers.Set(1, BankWallpaper.Color(3)); + + Assert.Equal(new Dictionary { [0] = "Starters", [2] = "Legends" }, decor.Names.All()); + Assert.Equal(new Dictionary { [1] = "color:3" }, decor.Wallpapers.AllIds()); + + decor.Names.ReplaceAll(new Dictionary { [1] = " Shinies ", [4] = " " }); + decor.Wallpapers.ReplaceAllIds(new Dictionary { [0] = "color:5" }); + + var reopened = new BankDecor(_root); + Assert.Equal(new Dictionary { [1] = "Shinies" }, reopened.Names.All()); + Assert.Equal(BankWallpaper.Color(5), reopened.Wallpapers.Get(0)); + Assert.Null(reopened.Wallpapers.Get(1)); + } + + [Fact] + public void TheLivingDexStartBoxPersistsAndFollowsItsBox() + { + var decor = new BankDecor(_root); + decor.Settings.LivingDexStartBox = 2; + + decor.Remap(BankBoxRemap.Insert(5, 0)); + Assert.Equal(3, new BankDecor(_root).Settings.LivingDexStartBox); + + decor.Remap(BankBoxRemap.Remove(6, 3)); + Assert.Null(new BankDecor(_root).Settings.LivingDexStartBox); + } + + [Fact] + public void SettingsRoundTripAsJsonKeepingKeysThisVersionDoesNotKnow() + { + var decor = new BankDecor(_root); + decor.Settings.Replace(JsonSerializer.SerializeToElement(new { livingDexStartBox = 4, futureSetting = "kept" })); + + decor.Settings.LivingDexStartBox = 7; + + var json = new BankDecor(_root).Settings.ToJson(); + Assert.Equal(7, json.GetProperty("livingDexStartBox").GetInt32()); + Assert.Equal("kept", json.GetProperty("futureSetting").GetString()); + } + + [Fact] + public void AnUnreadableSettingsFileReadsAsEmpty() + { + Directory.CreateDirectory(_root); + File.WriteAllText(Path.Combine(_root, "bank-settings.json"), "{not json"); + + var settings = new BankSettings(_root); + + Assert.Null(settings.LivingDexStartBox); + Assert.Equal(JsonValueKind.Object, settings.ToJson().ValueKind); + } +} diff --git a/tests/PKForge.Domain.Tests/FileBankServiceRemoteTests.cs b/tests/PKForge.Domain.Tests/FileBankServiceRemoteTests.cs new file mode 100644 index 00000000..83d5a541 --- /dev/null +++ b/tests/PKForge.Domain.Tests/FileBankServiceRemoteTests.cs @@ -0,0 +1,100 @@ +using PKForge.Domain; +using PKForge.Infrastructure; +using Xunit; + +namespace PKForge.Domain.Tests; + +public sealed class FileBankServiceRemoteTests : IDisposable +{ + private static readonly DateTimeOffset Added = new(2026, 10, 14, 12, 0, 0, TimeSpan.Zero); + private readonly string _root = Path.Combine(Path.GetTempPath(), "pkforge-tests", Guid.NewGuid().ToString("N")); + + public void Dispose() + { + if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); + } + + private static BankEntryInfo Info(int species) => new(species, 0, false, $"#{species}", 5, 8, "t", "PK8"); + private static BankEntry Entry(Guid id, int box, int slot) => new(id, box, slot, Info(25), Added); + private string BinPath(Guid id) => Path.Combine(_root, id.ToString("N") + ".bin"); + + [Fact] + public void ARemoteEntryKeepsItsIdPositionAndBytes() + { + var id = Guid.NewGuid(); + + new FileBankService(_root).ApplyRemote([new RemoteEntry(Entry(id, 4, 7), [1, 2, 3])], []); + + var reopened = new FileBankService(_root); + Assert.Equal(Entry(id, 4, 7), Assert.Single(reopened.GetAll())); + Assert.Equal([1, 2, 3], reopened.GetData(id)); + Assert.Equal(5, reopened.BoxCount); + } + + [Fact] + public void MovesAndRemovalsLandInOneWrite() + { + var bank = new FileBankService(_root); + var a = bank.Add([1], Info(1)); + var b = bank.Add([2], Info(2)); + var c = bank.Add([3], Info(3)); + + bank.ApplyRemote([new RemoteEntry(a with { Slot = b.Slot }, null), new RemoteEntry(b with { Slot = a.Slot }, null)], [c.Id]); + + var after = new FileBankService(_root).GetAll().ToDictionary(e => e.Id); + Assert.Equal(b.Slot, after[a.Id].Slot); + Assert.Equal(a.Slot, after[b.Id].Slot); + Assert.False(after.ContainsKey(c.Id)); + Assert.False(File.Exists(BinPath(c.Id))); + Assert.Equal([1], bank.GetData(a.Id)); + } + + [Fact] + public void ASlotClashIsRefusedAndChangesNothing() + { + var bank = new FileBankService(_root); + var resident = bank.Add([1], Info(1)); + var newcomer = Guid.NewGuid(); + + Assert.Throws(() => + bank.ApplyRemote([new RemoteEntry(Entry(newcomer, resident.Box, resident.Slot), [9])], [])); + + Assert.Equal(resident.Id, Assert.Single(new FileBankService(_root).GetAll()).Id); + Assert.False(File.Exists(BinPath(newcomer))); + } + + [Fact] + public void NewEntriesNeedTheirBytes() + { + var bank = new FileBankService(_root); + Assert.Throws(() => bank.ApplyRemote([new RemoteEntry(Entry(Guid.NewGuid(), 0, 0), null)], [])); + Assert.Empty(bank.GetAll()); + } + + [Fact] + public void TheBoxCountFollowsTheServerButNeverStrandsAnEntry() + { + var bank = new FileBankService(_root); + var id = Guid.NewGuid(); + + bank.ApplyRemote([new RemoteEntry(Entry(id, 3, 0), [1])], [], boxCount: 2); + Assert.Equal(4, bank.BoxCount); + bank.ApplyRemote([], [], boxCount: 6); + Assert.Equal(6, bank.BoxCount); + bank.ApplyRemote([], [id], boxCount: 1); + Assert.Equal(1, new FileBankService(_root).BoxCount); + } + + [Fact] + public void AFailedIndexWriteDeletesTheNewBytes() + { + var bank = new FileBankService(_root); + Directory.CreateDirectory(Path.Combine(_root, "index.json.tmp")); + var id = Guid.NewGuid(); + + Assert.ThrowsAny(() => bank.ApplyRemote([new RemoteEntry(Entry(id, 0, 0), [9])], [])); + + Assert.Empty(bank.GetAll()); + Assert.False(File.Exists(BinPath(id))); + } +} From 082b25b48fd12bdb144b314a9c6c29c165abdac1 Mon Sep 17 00:00:00 2001 From: YoruDev-Ryland <159859604+YoruDev-Ryland@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:59:24 -0500 Subject: [PATCH 2/3] feat(sync): PKForge.Sync, offline-first Bank sync with a PKForge Sync server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A library with no UI: every device keeps its full Bank and works offline; changes go out in groups that the server applies all-or-nothing, and edits of the same Pokémon on two devices become conflicts the user resolves instead of silent overwrites. A copy of the Bank is taken before it is ever replaced. The server (GPL-3.0-or-later, self-hostable with Docker) is a submodule at external/pkforge-sync-server; the tests start it in-process, so CI needs no running server. --- .github/workflows/build.yml | 1 + .gitmodules | 3 + PKForge.sln | 32 + external/pkforge-sync-server | 1 + src/PKForge.Sync/BankSnapshots.cs | 98 ++++ src/PKForge.Sync/PKForge.Sync.csproj | 14 + src/PKForge.Sync/PairingEntry.cs | 102 ++++ src/PKForge.Sync/ServerAddress.cs | 88 +++ src/PKForge.Sync/SyncClient.cs | 99 ++++ src/PKForge.Sync/SyncConnection.cs | 59 ++ src/PKForge.Sync/SyncCoordinator.cs | 371 ++++++++++++ src/PKForge.Sync/SyncEngine.Pull.cs | 136 +++++ src/PKForge.Sync/SyncEngine.Push.cs | 226 ++++++++ src/PKForge.Sync/SyncEngine.cs | 267 +++++++++ src/PKForge.Sync/SyncState.cs | 214 +++++++ src/PKForge.Sync/SyncValues.cs | 89 +++ src/PKForge.Sync/SyncedBankService.cs | 394 +++++++++++++ .../PKForge.Sync.Tests/BankSnapshotsTests.cs | 95 +++ tests/PKForge.Sync.Tests/ConflictTests.cs | 286 +++++++++ tests/PKForge.Sync.Tests/ConvergenceTests.cs | 148 +++++ tests/PKForge.Sync.Tests/CoordinatorTests.cs | 547 ++++++++++++++++++ tests/PKForge.Sync.Tests/EngineTests.cs | 163 ++++++ .../Infrastructure/SyncServer.cs | 64 ++ .../Infrastructure/TestData.cs | 18 + .../Infrastructure/TestDevice.cs | 59 ++ .../Infrastructure/WriteSwitch.cs | 22 + .../PKForge.Sync.Tests.csproj | 23 + tests/PKForge.Sync.Tests/PairingEntryTests.cs | 74 +++ tests/PKForge.Sync.Tests/PerformanceTests.cs | 66 +++ tests/PKForge.Sync.Tests/ResyncTests.cs | 72 +++ tests/PKForge.Sync.Tests/RobustnessTests.cs | 130 +++++ .../PKForge.Sync.Tests/ServerAddressTests.cs | 44 ++ tests/PKForge.Sync.Tests/SyncClientTests.cs | 68 +++ .../PKForge.Sync.Tests/SyncConnectionTests.cs | 74 +++ .../PKForge.Sync.Tests/SyncStateStoreTests.cs | 97 ++++ tests/PKForge.Sync.Tests/SyncValuesTests.cs | 72 +++ .../SyncedBankServiceTests.cs | 230 ++++++++ 37 files changed, 4546 insertions(+) create mode 160000 external/pkforge-sync-server create mode 100644 src/PKForge.Sync/BankSnapshots.cs create mode 100644 src/PKForge.Sync/PKForge.Sync.csproj create mode 100644 src/PKForge.Sync/PairingEntry.cs create mode 100644 src/PKForge.Sync/ServerAddress.cs create mode 100644 src/PKForge.Sync/SyncClient.cs create mode 100644 src/PKForge.Sync/SyncConnection.cs create mode 100644 src/PKForge.Sync/SyncCoordinator.cs create mode 100644 src/PKForge.Sync/SyncEngine.Pull.cs create mode 100644 src/PKForge.Sync/SyncEngine.Push.cs create mode 100644 src/PKForge.Sync/SyncEngine.cs create mode 100644 src/PKForge.Sync/SyncState.cs create mode 100644 src/PKForge.Sync/SyncValues.cs create mode 100644 src/PKForge.Sync/SyncedBankService.cs create mode 100644 tests/PKForge.Sync.Tests/BankSnapshotsTests.cs create mode 100644 tests/PKForge.Sync.Tests/ConflictTests.cs create mode 100644 tests/PKForge.Sync.Tests/ConvergenceTests.cs create mode 100644 tests/PKForge.Sync.Tests/CoordinatorTests.cs create mode 100644 tests/PKForge.Sync.Tests/EngineTests.cs create mode 100644 tests/PKForge.Sync.Tests/Infrastructure/SyncServer.cs create mode 100644 tests/PKForge.Sync.Tests/Infrastructure/TestData.cs create mode 100644 tests/PKForge.Sync.Tests/Infrastructure/TestDevice.cs create mode 100644 tests/PKForge.Sync.Tests/Infrastructure/WriteSwitch.cs create mode 100644 tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj create mode 100644 tests/PKForge.Sync.Tests/PairingEntryTests.cs create mode 100644 tests/PKForge.Sync.Tests/PerformanceTests.cs create mode 100644 tests/PKForge.Sync.Tests/ResyncTests.cs create mode 100644 tests/PKForge.Sync.Tests/RobustnessTests.cs create mode 100644 tests/PKForge.Sync.Tests/ServerAddressTests.cs create mode 100644 tests/PKForge.Sync.Tests/SyncClientTests.cs create mode 100644 tests/PKForge.Sync.Tests/SyncConnectionTests.cs create mode 100644 tests/PKForge.Sync.Tests/SyncStateStoreTests.cs create mode 100644 tests/PKForge.Sync.Tests/SyncValuesTests.cs create mode 100644 tests/PKForge.Sync.Tests/SyncedBankServiceTests.cs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 64a19103..45ec656d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -26,6 +26,7 @@ jobs: run: | dotnet test tests/PKForge.Domain.Tests/PKForge.Domain.Tests.csproj --no-restore --configuration Release dotnet test tests/PKForge.Engine.Tests/PKForge.Engine.Tests.csproj --no-restore --configuration Release + dotnet test tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj --no-restore --configuration Release - name: Decode permanent release signing key if: github.ref_type == 'tag' env: diff --git a/.gitmodules b/.gitmodules index f6a34dfc..ab3e97f7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "external/PKHeX-Plugins"] path = external/PKHeX-Plugins url = https://github.com/santacrab2/PKHeX-Plugins.git +[submodule "external/pkforge-sync-server"] + path = external/pkforge-sync-server + url = https://github.com/YoruDev-Ryland/pkforge-sync-server.git diff --git a/PKForge.sln b/PKForge.sln index 90dae749..d173e07a 100644 --- a/PKForge.sln +++ b/PKForge.sln @@ -23,6 +23,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{07C2787E EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChromePreview", "tools\ChromePreview\ChromePreview.csproj", "{8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKForge.Sync", "src\PKForge.Sync\PKForge.Sync.csproj", "{5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKForge.Sync.Tests", "tests\PKForge.Sync.Tests\PKForge.Sync.Tests.csproj", "{D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -117,6 +123,30 @@ Global {8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}.Release|x64.Build.0 = Release|Any CPU {8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}.Release|x86.ActiveCfg = Release|Any CPU {8D502784-4EC5-4B72-A0A7-4AF7DF5038A9}.Release|x86.Build.0 = Release|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x64.ActiveCfg = Debug|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x64.Build.0 = Debug|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x86.ActiveCfg = Debug|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Debug|x86.Build.0 = Debug|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|Any CPU.Build.0 = Release|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x64.ActiveCfg = Release|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x64.Build.0 = Release|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x86.ActiveCfg = Release|Any CPU + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5}.Release|x86.Build.0 = Release|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x64.ActiveCfg = Debug|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x64.Build.0 = Debug|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x86.ActiveCfg = Debug|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Debug|x86.Build.0 = Debug|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|Any CPU.Build.0 = Release|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x64.ActiveCfg = Release|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x64.Build.0 = Release|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x86.ActiveCfg = Release|Any CPU + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -124,5 +154,7 @@ Global GlobalSection(NestedProjects) = preSolution {59362056-D892-4D4D-A404-C344D7A25EEF} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} {8D502784-4EC5-4B72-A0A7-4AF7DF5038A9} = {07C2787E-EAC7-C090-1BA3-A61EC2A24D84} + {5219D9E3-D3C1-45A3-8416-F8BE1A062BB5} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {D1F7EFBE-DD10-4720-B7A4-CCB48E7C8A6B} = {0AB3BF05-4346-4AA6-1389-037BE0695223} EndGlobalSection EndGlobal diff --git a/external/pkforge-sync-server b/external/pkforge-sync-server new file mode 160000 index 00000000..40b1ae8e --- /dev/null +++ b/external/pkforge-sync-server @@ -0,0 +1 @@ +Subproject commit 40b1ae8e20ae175d3845c4f42e0e996440aebc3b diff --git a/src/PKForge.Sync/BankSnapshots.cs b/src/PKForge.Sync/BankSnapshots.cs new file mode 100644 index 00000000..3dbcfa88 --- /dev/null +++ b/src/PKForge.Sync/BankSnapshots.cs @@ -0,0 +1,98 @@ +using System.Globalization; +using System.Text.Json; + +namespace PKForge.Sync; + +/// A copy of the Bank taken before sync applies a large change. +public interface IBankSnapshots +{ + void Take(string reason); +} + +/// One copy of the Bank: its folder name, when it was taken, why, and how many Pokémon it holds (null when unreadable). +public sealed record BankSnapshotInfo(string Id, DateTimeOffset CreatedUtc, string Reason, int? PokemonCount, long SizeBytes); + +/// Copies the Bank folder's files into a timestamped folder; only the newest keep copies stay. +public sealed class FileBankSnapshots(string bankRoot, string snapshotRoot, TimeProvider? time = null, int keep = 5) : IBankSnapshots +{ + private const string StampFormat = "yyyyMMdd-HHmmss-fff"; + private const string ReasonFile = "reason.txt"; + private const string IndexFile = "index.json"; + + public void Take(string reason) => Take(reason, spare: null); + + /// Every copy, newest first. + public IReadOnlyList List() + { + if (!Directory.Exists(snapshotRoot)) return []; + var list = new List(); + foreach (var folder in Directory.GetDirectories(snapshotRoot)) + { + var id = Path.GetFileName(folder); + if (!DateTime.TryParseExact(id, StampFormat, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var stamp)) + continue; + var reasonPath = Path.Combine(folder, ReasonFile); + var reason = File.Exists(reasonPath) ? File.ReadAllText(reasonPath).Trim() : ""; + var size = Directory.EnumerateFiles(folder).Sum(f => new FileInfo(f).Length); + list.Add(new BankSnapshotInfo(id, new DateTimeOffset(stamp, TimeSpan.Zero), reason, CountPokemon(Path.Combine(folder, IndexFile)), size)); + } + return [.. list.OrderByDescending(s => s.Id, StringComparer.Ordinal)]; + } + + /// + /// Puts the Bank folder back as it was in copy , after copying the current Bank first + /// (). The index goes last, so a failure part way leaves the current index in charge. + /// The caller reloads whatever reads the Bank folder. + /// + public void Restore(string id, string reason) + { + if (string.IsNullOrWhiteSpace(id) || id != Path.GetFileName(id) || id.StartsWith('.')) + throw new ArgumentException("Not a Bank copy.", nameof(id)); + var source = Path.Combine(snapshotRoot, id); + if (!Directory.Exists(source)) throw new DirectoryNotFoundException("That Bank copy no longer exists."); + + Take(reason, spare: id); + Directory.CreateDirectory(bankRoot); + var files = Directory.EnumerateFiles(source).Select(Path.GetFileName).OfType().Where(f => f != ReasonFile).ToHashSet(); + foreach (var name in files.Where(f => f != IndexFile).Append(IndexFile).Where(files.Contains)) + { + var target = Path.Combine(bankRoot, name); + File.Copy(Path.Combine(source, name), target + ".restore", overwrite: true); + File.Move(target + ".restore", target, overwrite: true); + } + foreach (var extra in Directory.EnumerateFiles(bankRoot).Where(f => !files.Contains(Path.GetFileName(f))).ToList()) + { + try { File.Delete(extra); } + catch (IOException) { /* a leftover file the restored index doesn't list is harmless */ } + } + } + + private void Take(string reason, string? spare) + { + var stamp = (time ?? TimeProvider.System).GetUtcNow().ToString(StampFormat, CultureInfo.InvariantCulture); + var target = Path.Combine(snapshotRoot, stamp); + Directory.CreateDirectory(target); + if (Directory.Exists(bankRoot)) + { + foreach (var file in Directory.EnumerateFiles(bankRoot)) + File.Copy(file, Path.Combine(target, Path.GetFileName(file)), overwrite: true); + } + File.WriteAllText(Path.Combine(target, ReasonFile), reason); + // The copy about to be restored is never the one pushed out. + foreach (var old in Directory.GetDirectories(snapshotRoot).Where(d => Path.GetFileName(d) != spare).Order(StringComparer.Ordinal).SkipLast(keep)) + Directory.Delete(old, recursive: true); + } + + private static int? CountPokemon(string indexPath) + { + try + { + using var index = JsonDocument.Parse(File.ReadAllText(indexPath)); + return index.RootElement.TryGetProperty("Entries", out var entries) && entries.ValueKind == JsonValueKind.Array ? entries.GetArrayLength() : null; + } + catch (Exception error) when (error is IOException or JsonException or UnauthorizedAccessException) + { + return null; + } + } +} diff --git a/src/PKForge.Sync/PKForge.Sync.csproj b/src/PKForge.Sync/PKForge.Sync.csproj new file mode 100644 index 00000000..4eba3b02 --- /dev/null +++ b/src/PKForge.Sync/PKForge.Sync.csproj @@ -0,0 +1,14 @@ + + + + net10.0 + + + + + + + + + + diff --git a/src/PKForge.Sync/PairingEntry.cs b/src/PKForge.Sync/PairingEntry.cs new file mode 100644 index 00000000..fd07c20f --- /dev/null +++ b/src/PKForge.Sync/PairingEntry.cs @@ -0,0 +1,102 @@ +using System.Diagnostics.CodeAnalysis; + +namespace PKForge.Sync; + +/// +/// What a person types to add a device without a camera: the line another device shows under Add a device +/// ("sync.example.com K7QM-4XTP"), a pasted pairing link, or just the code. The server part follows the +/// rules; the code is the server's 8-character pairing code in any case, +/// with or without spaces and dashes. +/// +public static class PairingEntry +{ + /// The server's pairing alphabet (no I, L, O, 0 or 1). + public const string CodeAlphabet = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"; + public const int CodeLength = 8; + + public static bool TryParse(string? text, out Uri? server, out string code) => + TryParse(text, out server, out code, out _); + + /// Server is null when only a code was typed. On failure, error says what to fix. + public static bool TryParse(string? text, out Uri? server, out string code, out string error) + { + server = null; + code = ""; + var trimmed = text?.Trim() ?? ""; + if (trimmed.Length == 0) + { + error = "Type the server and code another device shows under Add a device."; + return false; + } + + if (PairingLink.TryParse(trimmed, out var linkServer, out var linkCode)) + { + if (!TryNormalizeCode(linkCode, out var normal)) + { + error = "That pairing link has no valid code in it."; + return false; + } + server = linkServer; + code = normal; + error = ""; + return true; + } + + // The code is the last word or two ("K7QM-4XTP", "K7QM 4XTP"); at most one word before it is the server. + var words = trimmed.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries); + for (var take = 1; take <= Math.Min(2, words.Length); take++) + { + if (words.Length - take > 1) continue; + if (!TryNormalizeCode(string.Concat(words[^take..]), out var candidate)) continue; + if (words.Length - take == 1) + { + if (!ServerAddress.TryParse(words[0], out var address, out error)) return false; + server = address; + } + code = candidate; + error = ""; + return true; + } + + error = words.Length != 1 + ? "Type the server, a space, then the code, like sync.example.com K7QM-4XTP." + : words[0].IndexOfAny(['.', ':', '/']) >= 0 + ? "Add the code after the server, like sync.example.com K7QM-4XTP." + : $"That isn't a pairing code. It is {CodeLength} letters and digits, like K7QM-4XTP."; + return false; + } + + /// Uppercases and drops spaces and dashes; true when what is left is a pairing code. + public static bool TryNormalizeCode(string? text, [NotNullWhen(true)] out string? code) + { + code = null; + if (text is null) return false; + var chars = new List(CodeLength); + foreach (var c in text) + { + if (c is ' ' or '-' || char.IsWhiteSpace(c)) continue; + var upper = char.ToUpperInvariant(c); + if (!CodeAlphabet.Contains(upper, StringComparison.Ordinal)) return false; + chars.Add(upper); + } + if (chars.Count != CodeLength) return false; + code = new string([.. chars]); + return true; + } + + /// + /// The line to show beside the QR code so it can be typed instead: "host XXXX-XXXX". The scheme is left + /// out for https (unless the host would otherwise be read as a home-network http one); the port only when default. + /// + public static string Format(Uri server, string code) + { + var display = TryNormalizeCode(code, out var normal) ? $"{normal[..4]}-{normal[4..]}" : code.Trim(); + return $"{FormatServer(server)} {display}"; + } + + public static string FormatServer(Uri server) + { + var bare = server.Scheme == "https" && !ServerAddress.IsHomeNetworkHost(server.Host); + return (bare ? "" : server.Scheme + "://") + server.Authority + server.AbsolutePath.TrimEnd('/'); + } +} diff --git a/src/PKForge.Sync/ServerAddress.cs b/src/PKForge.Sync/ServerAddress.cs new file mode 100644 index 00000000..6683707b --- /dev/null +++ b/src/PKForge.Sync/ServerAddress.cs @@ -0,0 +1,88 @@ +using System.Diagnostics.CodeAnalysis; +using System.Net; +using System.Net.Sockets; +using System.Web; + +namespace PKForge.Sync; + +/// +/// What a person types as a sync server address, made safe: https anywhere, plain http only for a server +/// on the home network (spec §11). Typing no scheme picks http for home hosts and https otherwise. +/// +public static class ServerAddress +{ + public static bool TryParse(string? input, [NotNullWhen(true)] out Uri? address, out string error) + { + address = null; + var text = input?.Trim() ?? ""; + if (text.Length == 0) + { + error = "Enter the server's address."; + return false; + } + if (!text.Contains("://", StringComparison.Ordinal)) + { + var host = text.Split('/', ':')[0]; + text = (IsHomeNetworkHost(host) ? "http://" : "https://") + text; + } + if (!Uri.TryCreate(text, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https") || string.IsNullOrEmpty(uri.Host)) + { + error = "That is not a web address (http:// or https://)."; + return false; + } + if (uri.Query.Length > 0 || uri.Fragment.Length > 0) + { + error = "Leave out anything after the address (? or #)."; + return false; + } + if (uri.Scheme == "http" && !IsHomeNetworkHost(uri.Host)) + { + error = "Plain http:// only works for servers on your home network. Use https:// for anything else."; + return false; + } + address = new Uri(uri.GetLeftPart(UriPartial.Path).TrimEnd('/') + "/"); + error = ""; + return true; + } + + /// Private and link-local addresses, Tailscale's range, localhost, and names without a public domain. + public static bool IsHomeNetworkHost(string host) + { + host = host.Trim('[', ']'); + if (IPAddress.TryParse(host, out var ip)) + { + if (IPAddress.IsLoopback(ip)) return true; + if (ip.AddressFamily == AddressFamily.InterNetworkV6) return ip.IsIPv6LinkLocal || ip.IsIPv6UniqueLocal; + var b = ip.GetAddressBytes(); + return b[0] == 10 + || (b[0] == 172 && b[1] is >= 16 and <= 31) + || (b[0] == 192 && b[1] == 168) + || (b[0] == 169 && b[1] == 254) + || (b[0] == 100 && b[1] is >= 64 and <= 127); + } + return host.Equals("localhost", StringComparison.OrdinalIgnoreCase) + || !host.Contains('.', StringComparison.Ordinal) + || host.EndsWith(".local", StringComparison.OrdinalIgnoreCase) + || host.EndsWith(".lan", StringComparison.OrdinalIgnoreCase) + || host.EndsWith(".home.arpa", StringComparison.OrdinalIgnoreCase) + || host.EndsWith(".internal", StringComparison.OrdinalIgnoreCase); + } +} + +/// The QR payload a signed-in device shows to add another: pkforge-sync://pair?server=…&code=…. +public static class PairingLink +{ + public static bool TryParse(string? payload, [NotNullWhen(true)] out Uri? server, [NotNullWhen(true)] out string? code) + { + server = null; + code = null; + if (!Uri.TryCreate(payload?.Trim(), UriKind.Absolute, out var link) || link.Scheme != "pkforge-sync" || link.Host != "pair") + return false; + var query = HttpUtility.ParseQueryString(link.Query); + if (string.IsNullOrWhiteSpace(query["code"]) || !ServerAddress.TryParse(query["server"], out var address, out _)) + return false; + server = address; + code = query["code"]!.Trim(); + return true; + } +} diff --git a/src/PKForge.Sync/SyncClient.cs b/src/PKForge.Sync/SyncClient.cs new file mode 100644 index 00000000..e139efe0 --- /dev/null +++ b/src/PKForge.Sync/SyncClient.cs @@ -0,0 +1,99 @@ +using System.Globalization; +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Text.Json; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +/// An error answer from the sync server ( is one of ). +public sealed class SyncApiException(int status, string code, string message) : Exception(message) +{ + public int Status { get; } = status; + public string Code { get; } = code; +} + +/// +/// The sync server's HTTP API. The 's base address is the server address +/// (ending in "/"); every call names the protocol version and, once signed in, the device key. +/// +public sealed class SyncClient(HttpClient http) +{ + public string? DeviceKey { get; set; } + + public Task GetServerInfoAsync(CancellationToken ct = default) => GetAsync("v1/server-info", ct); + public Task RegisterAsync(RegisterRequest request, CancellationToken ct = default) => PostAsync("v1/accounts", request, ct); + public Task LoginAsync(LoginRequest request, CancellationToken ct = default) => PostAsync("v1/auth/login", request, ct); + public Task RedeemPairingAsync(RedeemPairingRequest request, CancellationToken ct = default) => PostAsync("v1/pairing/redeem", request, ct); + public Task CreatePairingCodeAsync(CancellationToken ct = default) => PostAsync("v1/pairing", null, ct); + public Task> GetDevicesAsync(CancellationToken ct = default) => GetAsync>("v1/devices", ct); + public Task RemoveDeviceAsync(Guid id, CancellationToken ct = default) => SendAsync(HttpMethod.Delete, $"v1/devices/{id}", null, ct); + public Task ChangePasswordAsync(ChangePasswordRequest request, CancellationToken ct = default) => SendAsync(HttpMethod.Post, "v1/auth/password", request, ct); + public Task RequestPasswordResetAsync(string email, CancellationToken ct = default) => SendAsync(HttpMethod.Post, "v1/auth/password-reset", new PasswordResetRequest(email), ct); + public Task ResendVerificationAsync(CancellationToken ct = default) => SendAsync(HttpMethod.Post, "v1/auth/resend-verification", null, ct); + public Task DeleteAccountAsync(string password, CancellationToken ct = default) => SendAsync(HttpMethod.Delete, "v1/account", new DeleteAccountRequest(password), ct); + + public async Task> MissingBlobsAsync(IReadOnlyList hashes, CancellationToken ct = default) => + (await PostAsync("v1/blobs/missing", new MissingBlobsRequest(hashes), ct).ConfigureAwait(false)).Missing; + + public Task UploadBlobsAsync(IReadOnlyList blobs, CancellationToken ct = default) => + SendAsync(HttpMethod.Post, "v1/blobs/upload", new UploadBlobsRequest(blobs), ct); + + public async Task> DownloadBlobsAsync(IReadOnlyList hashes, CancellationToken ct = default) => + (await PostAsync("v1/blobs/download", new DownloadBlobsRequest(hashes), ct).ConfigureAwait(false)).Blobs; + + public Task PushAsync(PushRequest request, CancellationToken ct = default) => PostAsync("v1/sync/push", request, ct); + + public Task GetChangesAsync(long since, string? epoch, int limit, CancellationToken ct = default) => + GetAsync( + $"v1/sync/changes?since={since.ToString(CultureInfo.InvariantCulture)}&limit={limit.ToString(CultureInfo.InvariantCulture)}" + + (epoch is null ? "" : "&epoch=" + Uri.EscapeDataString(epoch)), ct); + + private async Task GetAsync(string path, CancellationToken ct) + { + using var response = await SendRawAsync(HttpMethod.Get, path, null, ct).ConfigureAwait(false); + return await ReadAsync(response, ct).ConfigureAwait(false); + } + + private async Task PostAsync(string path, object? body, CancellationToken ct) + { + using var response = await SendRawAsync(HttpMethod.Post, path, body, ct).ConfigureAwait(false); + return await ReadAsync(response, ct).ConfigureAwait(false); + } + + private async Task SendAsync(HttpMethod method, string path, object? body, CancellationToken ct) + { + using var response = await SendRawAsync(method, path, body, ct).ConfigureAwait(false); + } + + private async Task SendRawAsync(HttpMethod method, string path, object? body, CancellationToken ct) + { + using var request = new HttpRequestMessage(method, path); + request.Headers.Add(SyncProtocol.VersionHeader, SyncProtocol.Version.ToString(CultureInfo.InvariantCulture)); + if (DeviceKey is { } key) request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", key); + if (body is not null) request.Content = JsonContent.Create(body, body.GetType(), options: ProtocolJson.Options); + + var response = await http.SendAsync(request, ct).ConfigureAwait(false); + if (response.IsSuccessStatusCode) return response; + using (response) throw await ErrorAsync(response, ct).ConfigureAwait(false); + } + + private static async Task ReadAsync(HttpResponseMessage response, CancellationToken ct) => + await response.Content.ReadFromJsonAsync(ProtocolJson.Options, ct).ConfigureAwait(false) + ?? throw new SyncApiException((int)response.StatusCode, "empty_response", "The server sent an empty answer."); + + private static async Task ErrorAsync(HttpResponseMessage response, CancellationToken ct) + { + var status = (int)response.StatusCode; + try + { + if (await response.Content.ReadFromJsonAsync(ProtocolJson.Options, ct).ConfigureAwait(false) is { Code: not null } error) + return new SyncApiException(status, error.Code, error.Message); + } + catch (JsonException) + { + // Not one of our error bodies (a proxy's page, say). + } + return new SyncApiException(status, $"http_{status}", $"The server answered {status} {response.ReasonPhrase}."); + } +} diff --git a/src/PKForge.Sync/SyncConnection.cs b/src/PKForge.Sync/SyncConnection.cs new file mode 100644 index 00000000..d7bc98b1 --- /dev/null +++ b/src/PKForge.Sync/SyncConnection.cs @@ -0,0 +1,59 @@ +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +/// Where the device key lives (the app keeps it in Android's secure storage). +public interface ISyncSecrets +{ + string? DeviceKey { get; set; } +} + +/// Connecting this device to a sync server (sign up, sign in, pair) and disconnecting it. +public sealed class SyncConnection(SyncStateStore store, ISyncSecrets secrets, Func httpFor) +{ + public bool IsConnected => store.Read(s => s.ServerUrl) is not null && secrets.DeviceKey is not null; + + public Task ProbeAsync(Uri server, CancellationToken ct = default) => + new SyncClient(httpFor(server)).GetServerInfoAsync(ct); + + public Task SignUpAsync(Uri server, string email, string password, string deviceName, CancellationToken ct = default) => + ConnectAsync(server, client => client.RegisterAsync(new RegisterRequest(email, password, deviceName), ct)); + + public Task SignInAsync(Uri server, string email, string password, string deviceName, CancellationToken ct = default) => + ConnectAsync(server, client => client.LoginAsync(new LoginRequest(email, password, deviceName), ct)); + + public Task PairAsync(Uri server, string code, string deviceName, CancellationToken ct = default) => + ConnectAsync(server, client => client.RedeemPairingAsync(new RedeemPairingRequest(code, deviceName), ct)); + + /// A client signed in as this device. + public SyncClient CreateClient() + { + var server = store.Read(s => s.ServerUrl) ?? throw new InvalidOperationException("This device is not connected to a sync server."); + return new SyncClient(httpFor(new Uri(server))) { DeviceKey = secrets.DeviceKey }; + } + + /// Forgets the server, the account and what was synced. The local Bank is untouched. + public void SignOut() + { + secrets.DeviceKey = null; + store.Reset(); + } + + private async Task ConnectAsync(Uri server, Func> connect) + { + var client = new SyncClient(httpFor(server)); + var key = await connect(client).ConfigureAwait(false); + string? name = null; + try { name = (await client.GetServerInfoAsync().ConfigureAwait(false)).Name; } + catch (Exception error) when (error is HttpRequestException or SyncApiException or TaskCanceledException) { } + store.Reset(); + store.Update(s => + { + s.ServerUrl = server.ToString(); + s.AccountId = key.AccountId; + s.DeviceId = key.DeviceId; + s.ServerName = string.IsNullOrWhiteSpace(name) ? null : name.Trim(); + }); + secrets.DeviceKey = key.DeviceKey; + } +} diff --git a/src/PKForge.Sync/SyncCoordinator.cs b/src/PKForge.Sync/SyncCoordinator.cs new file mode 100644 index 00000000..6ed41b32 --- /dev/null +++ b/src/PKForge.Sync/SyncCoordinator.cs @@ -0,0 +1,371 @@ +using System.Text.Json; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +public enum SyncPhase { NotConnected, NeedsJoin, Idle, Syncing, Offline, SignedOut, UpdateRequired, EmailNotVerified, Paused, Failed } + +/// What the app shows about sync. +/// is the server's reason when it isn't accepting this account's changes (null: it is). +public sealed record SyncStatus( + SyncPhase Phase, DateTimeOffset? LastSynced, int Conflicts, bool PendingChanges, string? WritesDisabledMessage, string? Message) +{ + public bool WritesDisabled => WritesDisabledMessage is not null; +} + +/// +/// Runs sync for the app: syncs when it starts or resumes, shortly after a Bank change, when the +/// network returns and when asked; retries failures with growing delays (spec §10); and turns every +/// outcome into a . One sync runs at a time. +/// +public sealed class SyncCoordinator : IDisposable +{ + /// A local change syncs this long after the last change of a burst... + public static readonly TimeSpan ChangeDebounce = TimeSpan.FromSeconds(1); + /// ...but never later than this after the burst's first change. + public static readonly TimeSpan ChangeMaxWait = TimeSpan.FromSeconds(3); + public static readonly TimeSpan FirstRetry = TimeSpan.FromSeconds(30); + public static readonly TimeSpan MaxRetry = TimeSpan.FromMinutes(15); + /// While the app is in front and idle, other devices' changes are fetched this often. + public static readonly TimeSpan PollInterval = TimeSpan.FromMinutes(2); + + private readonly SyncedBankService _bank; + private readonly IBankSnapshots? _snapshots; + private readonly TimeProvider _time; + private readonly SemaphoreSlim _running = new(1, 1); + private readonly Lock _gate = new(); + private ITimer? _timer; + private DateTimeOffset _due; + private bool _dueFromChange; + private DateTimeOffset? _burstStart; + private bool _changedWhileRunning; + private TimeSpan _retry = TimeSpan.Zero; + private string? _writesDisabled; + private bool _suspended; + private SyncEngine? _engine; + private string? _engineFor; + + public SyncCoordinator(SyncConnection connection, SyncedBankService bank, SyncStateStore store, IBankSnapshots? snapshots = null, TimeProvider? time = null) + { + Connection = connection; + Store = store; + _bank = bank; + _snapshots = snapshots; + _time = time ?? TimeProvider.System; + _bank.LocalChanged += (_, _) => + { + Trace?.Invoke("Bank changed here"); + RequestChange(); + }; + Status = Settled(null); + } + + public SyncConnection Connection { get; } + public SyncStateStore Store { get; } + public SyncStatus Status { get; private set; } + public event EventHandler? StatusChanged; + + /// Diagnostics: what scheduling decided and why (the app writes it to its log). + public Action? Trace { get; set; } + + public int LocalPokemonCount => _bank.GetAll().Count; + + /// Schedules a sync after ; an earlier request wins. Ignored until connected and joined. + public void RequestSync(TimeSpan delay = default) => Schedule($"RequestSync({delay.TotalSeconds}s)", delay, fromChange: false); + + /// + /// A local change: syncs after the last change, but at most + /// after the first change of the burst. + /// + private void RequestChange() + { + lock (_gate) + { + if (_running.CurrentCount == 0) _changedWhileRunning = true; + var now = _time.GetUtcNow(); + _burstStart ??= now; + var due = now + ChangeDebounce; + var latest = _burstStart.Value + ChangeMaxWait; + if (due > latest) due = latest; + Schedule($"Change debounce (burst began {(now - _burstStart.Value).TotalSeconds:0.0#}s ago)", due - now, fromChange: true); + } + } + + private void Schedule(string what, TimeSpan delay, bool fromChange) + { + if (!Connection.IsConnected || !Store.Read(s => s.Joined && !s.DeviceRemoved)) + { + Trace?.Invoke($"{what} ignored: connected={Connection.IsConnected} joined={Store.Read(s => s.Joined)} removed={Store.Read(s => s.DeviceRemoved)}"); + return; + } + lock (_gate) + { + if (_suspended) + { + Trace?.Invoke($"{what} ignored: suspended"); + return; + } + var due = _time.GetUtcNow() + delay; + // A change may move its own pending timer later (the debounce); anything else only moves a timer earlier. + var replaceable = fromChange && _dueFromChange; + if (_timer is not null && _due <= due && !replaceable) + { + Trace?.Invoke($"{what} kept the sync already due at {_due:HH:mm:ss.fff}"); + return; + } + Trace?.Invoke($"{what} scheduled for {due:HH:mm:ss.fff} (in {delay.TotalSeconds:0.0#}s)"); + _timer?.Dispose(); + _due = due; + _dueFromChange = fromChange; + // Stored before it is started: a timer that fires at once (on this thread, under this lock) + // must still find itself here to clear, or a dead timer would hold back every later request. + ITimer? timer = null; + timer = _time.CreateTimer(_ => + { + lock (_gate) + { + // A timer replaced by an earlier one may still fire once; the replacement runs instead. + if (timer is null || !ReferenceEquals(_timer, timer)) return; + timer.Dispose(); + _timer = null; + _dueFromChange = false; + _burstStart = null; + } + Trace?.Invoke("Scheduled sync starting"); + _ = RunAsync(engine => engine.SyncAsync(), CancellationToken.None); + }, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); + _timer = timer; + timer.Change(delay, Timeout.InfiniteTimeSpan); + } + } + + /// The app went to the background: no scheduled syncs until . + public void Suspend() + { + Trace?.Invoke("Suspend"); + // Changes made just before leaving are sent now, as far as Android's background window allows. + if (Connection.IsConnected && Store.Read(s => s.Joined && !s.DeviceRemoved) && _bank.HasUnsyncedChanges()) + _ = RunAsync(engine => engine.SyncAsync(), CancellationToken.None); + lock (_gate) + { + _suspended = true; + _timer?.Dispose(); + _timer = null; + _burstStart = null; + } + Store.Flush(); + } + + /// The app is in front again: sync now, then keep polling. + public void Resume() + { + Trace?.Invoke("Resume"); + lock (_gate) _suspended = false; + RequestSync(); + } + + /// The network is back: try now instead of waiting for the next retry. + public void NetworkAvailable() + { + if (Status.Phase is SyncPhase.Offline or SyncPhase.Failed || Status.PendingChanges) RequestSync(); + } + + /// Re-reads connection state (after connecting or disconnecting elsewhere). + public void Refresh() => Publish(Settled(null)); + + public Task SyncNowAsync(CancellationToken ct = default) => RunAsync(engine => engine.SyncAsync(ct), ct); + + public Task JoinAsync(JoinMode mode, CancellationToken ct = default) => RunAsync(engine => engine.JoinAsync(mode, ct), ct); + + /// Whether the server's Bank already holds Pokémon (deciding whether joining needs a choice). + public async Task ServerHasPokemonAsync(CancellationToken ct = default) + { + var client = Connection.CreateClient(); + long since = 0; + while (true) + { + var page = await client.GetChangesAsync(since, null, 1000, ct).ConfigureAwait(false); + if (page.Items.Any(i => i.Kind == ItemKinds.Mon && !i.Deleted)) return true; + if (!page.HasMore) return false; + since = page.Next; + } + } + + public async Task ResolveAsync(Guid conflictId, ConflictChoice choice, CancellationToken ct = default) + { + await _running.WaitAsync(ct).ConfigureAwait(false); + try + { + await Engine().ResolveAsync(conflictId, choice, ct).ConfigureAwait(false); + } + finally + { + _running.Release(); + } + Publish(Settled(null)); + RequestSync(); + } + + /// Stops syncing on this device; the Bank stays as it is. + public void SignOut() + { + Trace?.Invoke("SignOut"); + lock (_gate) + { + _timer?.Dispose(); + _timer = null; + _burstStart = null; + } + Connection.SignOut(); + _engine = null; + _engineFor = null; + _retry = TimeSpan.Zero; + _writesDisabled = null; + Publish(Settled(null)); + } + + /// Saves sync state now (the app is going to the background). + public void Flush() => Store.Flush(); + + /// Completes when no sync is running (tests, and the app before showing results). + public async Task WhenIdleAsync() + { + await _running.WaitAsync().ConfigureAwait(false); + _running.Release(); + } + + public void Dispose() + { + lock (_gate) _timer?.Dispose(); + _running.Dispose(); + } + + private async Task RunAsync(Func> work, CancellationToken ct) + { + await _running.WaitAsync(ct).ConfigureAwait(false); + try + { + if (!Connection.IsConnected) + { + Publish(Settled(null)); + return null; + } + lock (_gate) _changedWhileRunning = false; + Trace?.Invoke("Sync running"); + Publish(Status with { Phase = SyncPhase.Syncing, Message = null }); + try + { + var report = await work(Engine()).ConfigureAwait(false); + _retry = TimeSpan.Zero; + Trace?.Invoke($"Sync done: sent {report.Uploaded}, received {report.Downloaded}"); + var at = _time.GetUtcNow(); + Store.Update(s => s.LastSyncedUtc = at); + Publish(Settled(report)); + bool followUp; + lock (_gate) { followUp = _changedWhileRunning; _changedWhileRunning = false; } + if (followUp) Trace?.Invoke("Bank changed during the sync: syncing again at once"); + RequestSync(followUp ? TimeSpan.Zero : PollInterval); + return report; + } + catch (SyncApiException error) when (error.Status == 401) + { + Store.Update(s => s.DeviceRemoved = true); + Publish(Settled(null)); + } + catch (SyncApiException error) when (error.Status == 426) + { + Fail(SyncPhase.UpdateRequired, error.Message, retry: false); + } + catch (SyncApiException error) when (error.Code == ErrorCodes.EmailNotVerified) + { + Fail(SyncPhase.EmailNotVerified, "Confirm your email address to start syncing: check your inbox for the link.", retry: true); + } + catch (SyncApiException error) + { + Fail(SyncPhase.Failed, error.Message, retry: true); + } + catch (OperationCanceledException) when (ct.IsCancellationRequested) + { + Publish(Settled(null)); // the person cancelled: nothing failed + } + catch (Exception error) when (error is HttpRequestException or IOException or TaskCanceledException) + { + Fail(SyncPhase.Offline, "Not synced: the server can't be reached. Trying again on its own.", retry: true); + } + catch (JsonException) + { + Fail(SyncPhase.Failed, "The address answered, but not as a PKForge Sync server (a Wi-Fi sign-in page, perhaps). Trying again on its own.", retry: true); + } + catch (Exception error) + { + // Anything else still ends the sync cleanly: never stuck on "Syncing", always retried. + Fail(SyncPhase.Failed, $"Sync failed: {error.Message}", retry: true); + } + return null; + } + finally + { + Store.Flush(); + _running.Release(); + } + } + + /// + /// Publishes a failure. Before the first join the device stays at "finish setting up" (with the reason), + /// since scheduled syncs only run once joined. + /// + private void Fail(SyncPhase phase, string message, bool retry) + { + if (!Store.Read(s => s.Joined)) + { + Publish(Settled(null) with { Message = message }); + return; + } + Publish(Settled(null) with { Phase = phase, Message = message }); + if (retry) ScheduleRetry(); + } + + private void ScheduleRetry() + { + _retry = _retry == TimeSpan.Zero ? FirstRetry : TimeSpan.FromTicks(Math.Min(_retry.Ticks * 2, MaxRetry.Ticks)); + RequestSync(_retry); + } + + private SyncEngine Engine() + { + // One engine per connection: two would each run "one sync at a time" on the same state. + lock (_gate) + { + var identity = Store.Read(s => $"{s.ServerUrl}|{s.DeviceId}"); + if (_engine is null || _engineFor != identity) + { + _engine = new SyncEngine(Connection.CreateClient(), _bank, Store, _snapshots, _time); + _engineFor = identity; + } + return _engine; + } + } + + /// The resting state: from the connection, the last report (if any) and the stored state. + private SyncStatus Settled(SyncReport? report) + { + if (!Connection.IsConnected) return new SyncStatus(SyncPhase.NotConnected, null, 0, false, null, null); + var (joined, last, conflicts, removed) = Store.Read(s => (s.Joined, s.LastSyncedUtc, s.Conflicts.Count, s.DeviceRemoved)); + if (removed) + return new SyncStatus(SyncPhase.SignedOut, last, conflicts, _bank.HasUnsyncedChanges(), null, + "This device was removed from the account, or its sign-in expired. Sign in again to keep syncing."); + if (report is not null) + _writesDisabled = report.WritesDisabled ? report.WritesDisabledMessage ?? "This server isn't accepting changes for this account." : null; + if (!joined) return new SyncStatus(SyncPhase.NeedsJoin, last, conflicts, false, null, null); + var pending = report?.PendingChanges ?? _bank.HasUnsyncedChanges(); + return report?.Paused == true + ? new SyncStatus(SyncPhase.Paused, last, conflicts, pending, _writesDisabled, "Sync paused: this device's Bank could not be read.") + : new SyncStatus(SyncPhase.Idle, last, conflicts, pending, _writesDisabled, null); + } + + private void Publish(SyncStatus status) + { + Status = status; + StatusChanged?.Invoke(this, status); + } +} diff --git a/src/PKForge.Sync/SyncEngine.Pull.cs b/src/PKForge.Sync/SyncEngine.Pull.cs new file mode 100644 index 00000000..abfceb65 --- /dev/null +++ b/src/PKForge.Sync/SyncEngine.Pull.cs @@ -0,0 +1,136 @@ +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +public sealed partial class SyncEngine +{ + /// Downloads every change since the cursor and applies what this device has not changed itself. + private async Task PullAsync(SyncReport report, CancellationToken ct) + { + var (since, epoch, behind) = _store.Read(s => (s.Cursor, s.Epoch, s.Behind.Values.ToList())); + var (latest, cursor, newEpoch) = await DownloadAllAsync(since, epoch, ct).ConfigureAwait(false); + foreach (var item in behind) + { + var key = SyncValues.Key(item.Kind, item.Id); + if (!latest.TryGetValue(key, out var newer) || newer.Version < item.Version) latest[key] = item; + } + + var local = Snapshot(); + var (known, held) = _store.Read(s => (s.Known.ToDictionary(k => k.Key, k => k.Value), s.Conflicts.Select(c => c.Key).ToHashSet())); + var adopted = new List(); + var stillBehind = new List(); + var refreshed = new List(); + var toApply = new List(); + var expected = new Dictionary(); + foreach (var (key, item) in latest) + { + var serverFingerprint = SyncValues.Fingerprint(item.Value); + var localFingerprint = local.Fingerprint(key); + if (held.Contains(key)) refreshed.Add(item); + else if (serverFingerprint == localFingerprint) adopted.Add(item); + else if (localFingerprint != (known.TryGetValue(key, out var k) ? k.Fingerprint : SyncValues.Deleted)) stillBehind.Add(item); + else + { + toApply.Add(item); + expected[key] = localFingerprint; + } + } + + _store.Update(s => + { + foreach (var item in adopted) s.Adopt(SyncValues.Key(item.Kind, item.Id), item.Version, SyncValues.Fingerprint(item.Value)); + foreach (var item in stillBehind) s.Behind[SyncValues.Key(item.Kind, item.Id)] = item; + foreach (var item in refreshed) + { + var key = SyncValues.Key(item.Kind, item.Id); + var index = s.Conflicts.FindIndex(c => c.Key == key); + if (index >= 0 && s.Conflicts[index].Server.Version < item.Version) s.Conflicts[index] = s.Conflicts[index] with { Server = item }; + } + }); + + var removals = toApply.Count(i => SyncValues.IsMon(SyncValues.Key(i.Kind, i.Id)) && i.Value is null && local.Values.ContainsKey(SyncValues.Key(i.Kind, i.Id))); + if (removals > LargeRemoval) _snapshots?.Take($"Before releasing {removals} Pokémon released on another device."); + report.Downloaded += await ApplyAsync(toApply, expected, local, report, ct).ConfigureAwait(false); + _store.Update(s => + { + s.Cursor = cursor; + s.Epoch = newEpoch; + }); + } + + /// Every change after , newest version per item. + private async Task<(Dictionary Latest, long Cursor, string Epoch)> DownloadAllAsync(long since, string? epoch, CancellationToken ct) + { + var latest = new Dictionary(); + var cursor = since; + var currentEpoch = epoch ?? ""; + while (true) + { + var page = await _client.GetChangesAsync(cursor, cursor > 0 ? epoch : null, PageSize, ct).ConfigureAwait(false); + foreach (var item in page.Items) latest[SyncValues.Key(item.Kind, item.Id)] = item; + cursor = page.Next; + currentEpoch = page.Epoch; + epoch = page.Epoch; + if (!page.HasMore) break; + } + return (latest, cursor, currentEpoch); + } + + /// Writes server items locally (with any bytes this device lacks); returns how many were applied. + private async Task ApplyAsync(List items, Dictionary expected, LocalSnapshot local, SyncReport report, CancellationToken ct) + { + if (items.Count == 0) return 0; + var changes = await PrepareAsync(items, local, ct).ConfigureAwait(false); + var result = _bank.ApplyRemote(changes, expected); + var byKey = items.ToDictionary(i => SyncValues.Key(i.Kind, i.Id)); + // A Pokémon is remembered as this device renders it: facts a newer app version added (which this + // one cannot store) must not look like a local edit and be stripped from the server. + var rendered = result.Applied.Any(k => SyncValues.IsMon(k) && byKey[k].Value is not null) ? _bank.CurrentValues().Values : null; + var at = _time.GetUtcNow(); + _store.Update(s => + { + foreach (var key in result.Applied) + { + var item = byKey[key]; + var fingerprint = SyncValues.IsMon(key) && rendered is not null && rendered.TryGetValue(key, out var mine) + ? SyncValues.Fingerprint(mine) + : SyncValues.Fingerprint(item.Value); + s.Adopt(key, item.Version, fingerprint); + } + foreach (var key in result.Skipped) s.Behind[key] = byKey[key]; + foreach (var (_, box, slot) in result.Relocated) + s.AddActivity(at, $"Moved a Pokémon to box {box + 1}, slot {slot + 1}: another device put one where it was."); + }); + report.Relocated += result.Relocated.Count; + return result.Applied.Count; + } + + /// Server items as local changes, downloading (and checking) bytes for Pokémon this device does not hold. + private async Task> PrepareAsync(IReadOnlyList items, LocalSnapshot local, CancellationToken ct) + { + var needed = new HashSet(); + foreach (var item in items) + { + if (item.Kind != ItemKinds.Mon || item.Value is not { } value) continue; + var blob = SyncValues.ReadMon(value).Blob; + if (!local.Blobs.TryGetValue(SyncValues.Key(item.Kind, item.Id), out var held) || held != blob) needed.Add(blob); + } + var bytes = new Dictionary(); + foreach (var batch in needed.Chunk(BlobBatch)) + { + foreach (var blob in await _client.DownloadBlobsAsync(batch, ct).ConfigureAwait(false)) + { + if (BlobHash.Compute(blob.Data) != blob.Hash) throw new InvalidDataException("A Pokémon's bytes arrived damaged."); + bytes[blob.Hash] = blob.Data; + } + } + if (needed.Any(hash => !bytes.ContainsKey(hash))) throw new InvalidDataException("The server is missing a Pokémon's bytes."); + + return items.Select(item => + { + byte[]? data = null; + if (item.Kind == ItemKinds.Mon && item.Value is { } value) bytes.TryGetValue(SyncValues.ReadMon(value).Blob, out data); + return new RemoteChange(SyncValues.Key(item.Kind, item.Id), item.Value, data); + }).ToList(); + } +} diff --git a/src/PKForge.Sync/SyncEngine.Push.cs b/src/PKForge.Sync/SyncEngine.Push.cs new file mode 100644 index 00000000..06430030 --- /dev/null +++ b/src/PKForge.Sync/SyncEngine.Push.cs @@ -0,0 +1,226 @@ +using System.Text.Json; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +public sealed partial class SyncEngine +{ + /// Sends changed items in their groups. Returns true when another round is worthwhile. + private async Task PushAsync(SyncReport report, CancellationToken ct) + { + var local = Snapshot(); + var dirty = DirtyKeys(local).ToHashSet(); + if (dirty.Count == 0) return false; + + // An item is a background change only while no person's edit waits for it in another group + // (read before grouping, which drops groups whose items another group already claimed). + var userKeys = _store.Read(s => s.Pending.Where(g => !g.Background).SelectMany(g => g.Keys).ToHashSet()); + + // Every dirty item belongs to exactly one group; changes no operation recorded form one more group. + var groups = _store.Update(s => + { + var covered = s.Pending.SelectMany(g => g.Keys).Where(dirty.Contains).ToHashSet(); + var leftovers = dirty.Where(k => !covered.Contains(k)).ToList(); + // No operation tied these together, so they need no atomicity: modest groups keep requests small. + foreach (var part in leftovers.Chunk(LeftoverGroupSize)) s.RecordChange(part, background: false); + var claimed = new HashSet(); + var result = new List<(PendingGroup Group, List Keys)>(); + foreach (var group in s.Pending.ToList()) + { + var keys = group.Keys.Where(k => dirty.Contains(k) && claimed.Add(k)).ToList(); + if (keys.Count == 0) s.Pending.Remove(group); + else result.Add((group, keys)); + } + // The box layout goes first, so Pokémon placed in a new box find it on the server. + var boxesFirst = result.FindIndex(g => g.Keys.Contains(SyncValues.BoxesKey)); + if (boxesFirst > 0) + { + var layout = result[boxesFirst]; + result.RemoveAt(boxesFirst); + result.Insert(0, layout); + } + return result; + }); + + await UploadBlobsAsync(local, groups.SelectMany(g => g.Keys), ct).ConfigureAwait(false); + + var (since, epoch, versions) = _store.Read(s => (s.Cursor, s.Epoch, s.Known.ToDictionary(k => k.Key, k => k.Value.Version))); + var again = false; + foreach (var chunk in Chunk(groups)) + { + var request = new PushRequest( + chunk.Select(g => new ChangeGroup(g.Group.Id, g.Keys.Select(k => Write(k, local, versions)).ToList())).ToList(), + since, epoch); + _store.Update(s => + { + foreach (var (group, keys) in chunk) + { + group.Sent = true; + foreach (var key in keys) s.AddInFlight(key, local.Fingerprint(key)); + } + }); + + PushResponse response; + try + { + response = await _client.PushAsync(request, ct).ConfigureAwait(false); + } + catch (SyncApiException error) when (error.Code == ErrorCodes.WritesDisabled) + { + report.WritesDisabled = true; + report.WritesDisabledMessage = error.Message; + return false; + } + + // Every slot the server reported taken, so a relocated Pokémon never lands on another reported one. + var taken = response.Results.Where(r => r.Status == GroupStatus.SlotTaken) + .SelectMany(r => r.Items).Where(i => i.Value is not null) + .Select(i => SyncValues.ReadMon(i.Value!.Value)).Select(m => (m.Box, m.Slot)).ToHashSet(); + // Server versions to take are gathered across the whole answer and written in one batch. + var takeServer = new List(); + foreach (var result in response.Results) + { + var sent = chunk.First(g => g.Group.Id == result.ClientGroupId); + again |= HandleResult(result, sent, local, taken, userKeys, takeServer, report); + } + if (takeServer.Count > 0) + { + var expected = takeServer.ToDictionary(i => SyncValues.Key(i.Kind, i.Id), i => local.Fingerprint(SyncValues.Key(i.Kind, i.Id))); + report.Downloaded += await ApplyAsync(takeServer, expected, local, report, ct).ConfigureAwait(false); + } + _store.Flush(); + } + return again; + } + + private bool HandleResult(GroupResult result, (PendingGroup Group, List Keys) sent, LocalSnapshot local, + IReadOnlyCollection<(int Box, int Slot)> reportedTaken, IReadOnlySet userKeys, List takeServer, SyncReport report) + { + // State is saved once per answer (the caller flushes); fingerprints in flight were saved before sending. + _store.Update(s => s.Pending.Remove(sent.Group), persist: false); + switch (result.Status) + { + case GroupStatus.Accepted: + _store.Update(s => + { + foreach (var item in result.Items) s.Adopt(SyncValues.Key(item.Kind, item.Id), item.Version, SyncValues.Fingerprint(item.Value)); + }, persist: false); + report.Uploaded += result.Items.Count; + return false; + + case GroupStatus.Conflict: + foreach (var item in result.Items) + ResolveAutomatically(item, sent.Group.Background && !userKeys.Contains(SyncValues.Key(item.Kind, item.Id)), local, takeServer, report); + return true; + + case GroupStatus.SlotTaken: + foreach (var occupant in result.Items) + { + if (occupant.Value is not { } taken) continue; + var spot = SyncValues.ReadMon(taken); + var ours = sent.Keys.FirstOrDefault(k => SyncValues.IsMon(k) && local.Value(k) is { } mine + && SyncValues.ReadMon(mine) is var m && (m.Box, m.Slot) == (spot.Box, spot.Slot)); + if (ours is null || _bank.MoveToFreeSlot(SyncValues.MonId(ours), reportedTaken) is not { } moved) continue; + report.Relocated++; + var at = _time.GetUtcNow(); + _store.Update(s => s.AddActivity(at, + $"Moved a Pokémon to box {moved.Box + 1}, slot {moved.Slot + 1}: box {spot.Box + 1}, slot {spot.Slot + 1} was filled on another device."), persist: false); + } + return true; + + default: + if (result.Error == ErrorCodes.MissingBlob) return true; // uploaded again next round + var refusedAt = _time.GetUtcNow(); + _store.Update(s => + { + foreach (var key in sent.Keys) + { + s.Refused[key] = local.Fingerprint(key); + s.RefusedCodes[key] = result.Error ?? ErrorCodes.InvalidItem; + s.InFlight.Remove(key); + } + s.AddActivity(refusedAt, $"The server refused a change: {result.Message}"); + }); + return false; + } + } + + /// + /// Settles a conflicting item without asking when the answer is clear (spec §8.1): our own earlier + /// write, identical values, background fact rewrites, or a Pokémon only moved on both devices. + /// Everything else becomes a recorded conflict. + /// + private void ResolveAutomatically(SyncItem server, bool background, LocalSnapshot local, List takeServer, SyncReport report) + { + var key = SyncValues.Key(server.Kind, server.Id); + var serverFingerprint = SyncValues.Fingerprint(server.Value); + if (serverFingerprint == local.Fingerprint(key) || _store.Read(s => s.IsInFlight(key, serverFingerprint))) + { + _store.Update(s => s.Adopt(key, server.Version, serverFingerprint), persist: false); + return; + } + + var mine = local.Value(key); + if (mine is null && server.Value is null) + { + _store.Update(s => s.Adopt(key, server.Version, serverFingerprint), persist: false); + return; + } + if (background && server.Value is not null) + { + takeServer.Add(server); + return; + } + if (mine is { } ours && server.Value is { } theirs && SyncValues.IsMon(key) && SyncValues.SameExceptPosition(ours, theirs)) + { + if (SyncValues.ReadMon(ours).MovedUtc > SyncValues.ReadMon(theirs).MovedUtc) + _store.Update(s => s.Adopt(key, server.Version, serverFingerprint), persist: false); // our later move is sent again on top + else + takeServer.Add(server); + return; + } + RecordConflict(key, mine, server, report); + } + + private async Task UploadBlobsAsync(LocalSnapshot local, IEnumerable keys, CancellationToken ct) + { + var owners = new Dictionary(); + foreach (var key in keys) + { + if (local.Blobs.TryGetValue(key, out var hash)) owners.TryAdd(hash, SyncValues.MonId(key)); + } + foreach (var batch in owners.Keys.Chunk(500)) + { + var missing = await _client.MissingBlobsAsync(batch, ct).ConfigureAwait(false); + foreach (var upload in missing.Chunk(BlobBatch)) + await _client.UploadBlobsAsync(upload.Select(hash => new BlobData(hash, _bank.GetData(owners[hash]))).ToList(), ct).ConfigureAwait(false); + } + } + + private static ItemWrite Write(string key, LocalSnapshot local, IReadOnlyDictionary versions) + { + var (kind, id) = SyncValues.SplitKey(key); + var baseVersion = versions.TryGetValue(key, out var version) ? version : 0; + return local.Value(key) is { } value + ? new ItemWrite(kind, id, baseVersion, value) + : new ItemWrite(kind, id, baseVersion, null, Deleted: true); + } + + private static IEnumerable Keys)>> Chunk(List<(PendingGroup Group, List Keys)> groups) + { + var chunk = new List<(PendingGroup Group, List Keys)>(); + var writes = 0; + foreach (var group in groups) + { + if (chunk.Count > 0 && (chunk.Count == MaxGroupsPerPush || writes + group.Keys.Count > WritesPerRequest)) + { + yield return chunk; + chunk = []; + writes = 0; + } + chunk.Add(group); + writes += group.Keys.Count; + } + if (chunk.Count > 0) yield return chunk; + } +} diff --git a/src/PKForge.Sync/SyncEngine.cs b/src/PKForge.Sync/SyncEngine.cs new file mode 100644 index 00000000..f4df417c --- /dev/null +++ b/src/PKForge.Sync/SyncEngine.cs @@ -0,0 +1,267 @@ +using System.Text.Json; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +public enum JoinMode { Merge, UseServer } + +/// What one sync did. +public sealed class SyncReport +{ + public int Uploaded { get; set; } + public int Downloaded { get; set; } + public int NewConflicts { get; set; } + public int Relocated { get; set; } + public bool Resynced { get; set; } + /// The server refused this device's changes (403 writes_disabled); reads still worked. + public bool WritesDisabled { get; set; } + /// The server's explanation when . + public string? WritesDisabledMessage { get; set; } + /// Nothing was sent or applied: this device's Bank could not be read (spec §10). + public bool Paused { get; set; } + /// Changes are still waiting (offline, refused, or more rounds needed). + public bool PendingChanges { get; set; } +} + +/// +/// Keeps the local Bank and the server's in step (spec §6). Each sync runs rounds of push-then-pull +/// until nothing is left to send; a 410 answer re-downloads everything and compares (§6.5). Only one +/// sync runs at a time. +/// +public sealed partial class SyncEngine(SyncClient client, SyncedBankService bank, SyncStateStore store, IBankSnapshots? snapshots = null, TimeProvider? time = null) +{ + public const int MaxRounds = 5; + /// More remote releases than this take a Bank snapshot first (spec §8.3). + public const int LargeRemoval = 30; + private const int PageSize = 1000; + private const int BlobBatch = 200; + private const int MaxGroupsPerPush = 500; + /// Requests carry about this many writes; one bigger group (a whole-Bank remap) still goes alone. + private const int WritesPerRequest = 2_000; + /// Size of the groups formed from changes no operation recorded. + private const int LeftoverGroupSize = 1_000; + + private readonly SyncClient _client = client; + private readonly SyncedBankService _bank = bank; + private readonly SyncStateStore _store = store; + private readonly IBankSnapshots? _snapshots = snapshots; + private readonly SemaphoreSlim _gate = new(1, 1); + private readonly TimeProvider _time = time ?? TimeProvider.System; + + private enum ResyncMode { Merge, UseServer, Recover } + + /// First sync of this device: merges with the server's Bank, or replaces this one with it. + public Task JoinAsync(JoinMode mode, CancellationToken ct = default) => + RunAsync(mode == JoinMode.UseServer ? ResyncMode.UseServer : ResyncMode.Merge, ct); + + public Task SyncAsync(CancellationToken ct = default) + { + var (joined, needsResync) = _store.Read(s => (s.Joined, s.NeedsResync)); + if (!joined) throw new InvalidOperationException("Join the server's Bank before syncing."); + return RunAsync(needsResync ? ResyncMode.Recover : null, ct); + } + + /// Applies the person's decision about a conflict; the next sync sends the result. + public async Task ResolveAsync(Guid conflictId, ConflictChoice choice, CancellationToken ct = default) + { + await _gate.WaitAsync(ct).ConfigureAwait(false); + try + { + var conflict = _store.Read(s => s.Conflicts.FirstOrDefault(c => c.Id == conflictId)) + ?? throw new InvalidOperationException("That conflict was already resolved."); + var server = conflict.Server; + var serverFingerprint = SyncValues.Fingerprint(server.Value); + switch (choice) + { + case ConflictChoice.KeepThisDevice: + // Ours stays as it is and is sent with the server's version as its base. + _store.Update(s => s.Adopt(conflict.Key, server.Version, serverFingerprint)); + break; + case ConflictChoice.KeepBoth when SyncValues.IsMon(conflict.Key) && conflict.Local is not null && server.Value is not null: + _bank.KeepCopy(SyncValues.MonId(conflict.Key)); + await TakeServerVersionAsync(server, ct).ConfigureAwait(false); + break; + case ConflictChoice.KeepBoth: + throw new ArgumentException("Keeping both only applies to a Pokémon edited on both devices.", nameof(choice)); + default: + await TakeServerVersionAsync(server, ct).ConfigureAwait(false); + break; + } + _store.Update(s => s.Conflicts.RemoveAll(c => c.Id == conflictId)); + } + finally + { + _gate.Release(); + } + } + + private async Task RunAsync(ResyncMode? resync, CancellationToken ct) + { + await _gate.WaitAsync(ct).ConfigureAwait(false); + try + { + var report = new SyncReport(); + if (_bank.IsReadOnly) + { + // An unreadable index lists no Pokémon; syncing it would release every one of them everywhere. + report.Paused = true; + var at = _time.GetUtcNow(); + _store.Update(s => s.AddActivity(at, "Sync paused: this device's Bank could not be read.")); + return report; + } + if (resync is { } mode) await ResyncAsync(mode, report, ct).ConfigureAwait(false); + for (var round = 0; round < MaxRounds; round++) + { + try + { + var again = await PushAsync(report, ct).ConfigureAwait(false); + await PullAsync(report, ct).ConfigureAwait(false); + if (!again) break; + } + catch (SyncApiException error) when (error.Code == ErrorCodes.ResyncRequired) + { + await ResyncAsync(ResyncMode.Recover, report, ct).ConfigureAwait(false); + } + } + report.PendingChanges = DirtyKeys(Snapshot(), includeRefused: true).Count > 0; + return report; + } + finally + { + _store.Flush(); + _gate.Release(); + } + } + + /// + /// Re-downloads the whole Bank and compares it with this device's (spec §6.3, §6.5). Items unchanged + /// here since the last sync take the server's version; items changed here become conflicts (Merge and + /// Recover) or are replaced (UseServer); Pokémon the server lacks stay and are uploaded as new. + /// + private async Task ResyncAsync(ResyncMode mode, SyncReport report, CancellationToken ct) + { + _store.Update(s => s.NeedsResync = true); + var (latest, cursor, epoch) = await DownloadAllAsync(0, null, ct).ConfigureAwait(false); + if (mode == ResyncMode.UseServer) _snapshots?.Take("Before replacing this Bank with the server's."); + + var local = Snapshot(); + var (oldKnown, held) = _store.Read(s => ( + s.Known.ToDictionary(k => k.Key, k => k.Value.Fingerprint), + s.Conflicts.Select(c => c.Key).ToHashSet())); + var adopted = new List(); + var toApply = new List(); + var expected = new Dictionary(); + foreach (var (key, item) in latest) + { + if (held.Contains(key)) continue; + var serverFingerprint = SyncValues.Fingerprint(item.Value); + var localFingerprint = local.Fingerprint(key); + if (serverFingerprint == localFingerprint) + { + adopted.Add(item); + continue; + } + var changedHere = mode != ResyncMode.UseServer + && localFingerprint != oldKnown.GetValueOrDefault(key, SyncValues.Deleted); + var serverWins = !changedHere || (mode == ResyncMode.Merge && !SyncValues.IsMon(key)); + if (serverWins) + { + toApply.Add(item); + expected[key] = localFingerprint; + } + else + { + RecordConflict(key, local.Value(key), item, report); + } + } + if (mode == ResyncMode.UseServer) + { + foreach (var key in local.Values.Keys.Where(k => SyncValues.IsMon(k) && !latest.ContainsKey(k))) + { + var (kind, id) = SyncValues.SplitKey(key); + toApply.Add(new SyncItem(kind, id, 0, true, null)); + expected[key] = local.Fingerprint(key); + } + } + + _store.Update(s => + { + s.Known.Clear(); + s.Behind.Clear(); + s.InFlight.Clear(); + if (mode == ResyncMode.UseServer) s.Pending.Clear(); + foreach (var item in adopted) s.Adopt(SyncValues.Key(item.Kind, item.Id), item.Version, SyncValues.Fingerprint(item.Value)); + }); + var applied = await ApplyAsync(toApply, expected, local, report, ct).ConfigureAwait(false); + _store.Update(s => + { + s.Cursor = cursor; + s.Epoch = epoch; + s.Joined = true; + s.NeedsResync = false; + }); + report.Resynced = true; + report.Downloaded += applied; + } + + private async Task TakeServerVersionAsync(SyncItem server, CancellationToken ct) + { + var local = Snapshot(); + var changes = await PrepareAsync([server], local, ct).ConfigureAwait(false); + var result = _bank.ApplyRemote(changes, new Dictionary()); + if (result.Applied.Count > 0) + _store.Update(s => s.Adopt(SyncValues.Key(server.Kind, server.Id), server.Version, SyncValues.Fingerprint(server.Value))); + } + + private void RecordConflict(string key, JsonElement? local, SyncItem server, SyncReport report) + { + var kind = (server.Kind, local is not null, server.Value is not null) switch + { + (ItemKinds.Boxes, _, _) => ConflictKind.Boxes, + (ItemKinds.Settings, _, _) => ConflictKind.Settings, + (_, true, true) => ConflictKind.BothEdited, + (_, true, false) => ConflictKind.EditedHereReleasedThere, + _ => ConflictKind.ReleasedHereEditedThere, + }; + var found = _time.GetUtcNow(); + _store.Update(s => + { + s.Conflicts.RemoveAll(c => c.Key == key); + s.Conflicts.Add(new SyncConflict(Guid.NewGuid(), key, kind, local, server, found)); + s.InFlight.Remove(key); + s.Behind.Remove(key); + }); + report.NewConflicts++; + } + + /// Items whose local value differs from what was last synced (not held by a conflict; refused values only when asked). + private List DirtyKeys(LocalSnapshot local, bool includeRefused = false) => _store.Read(s => + { + var keys = new HashSet(local.Values.Keys); + keys.UnionWith(s.Known.Keys); + var held = s.Conflicts.Select(c => c.Key).ToHashSet(); + return keys.Where(key => + { + var fingerprint = local.Fingerprint(key); + if (held.Contains(key)) return false; + if (fingerprint == (s.Known.TryGetValue(key, out var known) ? known.Fingerprint : SyncValues.Deleted)) return false; + return includeRefused || !(s.Refused.TryGetValue(key, out var refused) && refused == fingerprint); + }).ToList(); + }); + + private LocalSnapshot Snapshot() => new(_bank.CurrentValues()); + + /// The local values at one moment, with fingerprints computed once. + private sealed class LocalSnapshot(LocalValues values) + { + private readonly Dictionary _fingerprints = []; + + public IReadOnlyDictionary Values => values.Values; + public IReadOnlyDictionary Blobs => values.Blobs; + + public JsonElement? Value(string key) => Values.TryGetValue(key, out var value) ? value : null; + + public string Fingerprint(string key) => + _fingerprints.TryGetValue(key, out var known) ? known : _fingerprints[key] = SyncValues.Fingerprint(Value(key)); + } +} diff --git a/src/PKForge.Sync/SyncState.cs b/src/PKForge.Sync/SyncState.cs new file mode 100644 index 00000000..6782dfc2 --- /dev/null +++ b/src/PKForge.Sync/SyncState.cs @@ -0,0 +1,214 @@ +using System.Text.Json; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +/// The server version and fingerprint of an item as this device last saw it in sync. +public sealed record KnownItem(long Version, string Fingerprint); + +/// Items one Bank operation touched; pushed together so the server applies them all or none. +public sealed class PendingGroup +{ + public Guid Id { get; set; } = Guid.NewGuid(); + public List Keys { get; set; } = []; + /// Only automatic fact rewrites: on conflict the other device's version wins. + public bool Background { get; set; } + /// Sent at least once: its id may already have a receipt, so its contents never change. + public bool Sent { get; set; } +} + +public enum ConflictKind { BothEdited, EditedHereReleasedThere, ReleasedHereEditedThere, Boxes, Settings } + +public enum ConflictChoice { KeepThisDevice, KeepOtherDevice, KeepBoth } + +/// A change the person decides about. Until then this device keeps . +public sealed record SyncConflict(Guid Id, string Key, ConflictKind Kind, JsonElement? Local, SyncItem Server, DateTimeOffset FoundUtc); + +public sealed record ActivityEntry(DateTimeOffset Utc, string Message); + +/// Everything this device remembers about syncing its Bank. +public sealed class SyncStateData +{ + public const int ActivityLimit = 200; + + public string? ServerUrl { get; set; } + public Guid AccountId { get; set; } + public Guid DeviceId { get; set; } + /// The local Bank has been merged with (or replaced by) the server's. + public bool Joined { get; set; } + /// What was synced is unknown (unreadable state): rebuild it from a full download. + public bool NeedsResync { get; set; } + public long Cursor { get; set; } + public string? Epoch { get; set; } + /// When a sync last finished. + public DateTimeOffset? LastSyncedUtc { get; set; } + /// The server's display name, as it described itself when this device connected (null: show the host). + public string? ServerName { get; set; } + /// The server no longer knows this device's key (removed from the account): sign in again. + public bool DeviceRemoved { get; set; } + public Dictionary Known { get; set; } = []; + /// SHA-256 of each local Pokémon's bytes, so they are read only when they change. + public Dictionary BlobHashes { get; set; } = []; + /// When this device last moved each Pokémon (absent: when it was added). + public Dictionary MovedUtc { get; set; } = []; + public List Pending { get; set; } = []; + /// Fingerprints pushed but not yet acknowledged: a conflict with one of them is our own write. + public Dictionary> InFlight { get; set; } = []; + /// Server changes not applied because this device had changed the item too. + public Dictionary Behind { get; set; } = []; + /// Values the server refused, by fingerprint: not sent again until they change. + public Dictionary Refused { get; set; } = []; + /// Why each refused item was refused (an value), so the refusal can lift when its cause does. + public Dictionary RefusedCodes { get; set; } = []; + public List Conflicts { get; set; } = []; + public List Activity { get; set; } = []; + + /// Notes items one operation changed, merging with any unsent group that shares an item. + public void RecordChange(IReadOnlyCollection keys, bool background) + { + if (keys.Count == 0) return; + var merged = new HashSet(keys); + var allBackground = background; + // A merged group keeps the place of its earliest member: order matters when a box is added + // before a Pokémon is put in it. + var position = -1; + for (var i = Pending.Count - 1; i >= 0; i--) + { + var group = Pending[i]; + if (group.Sent || !group.Keys.Any(merged.Contains)) continue; + merged.UnionWith(group.Keys); + allBackground &= group.Background; + Pending.RemoveAt(i); + position = i; + } + var combined = new PendingGroup { Keys = [.. merged], Background = allBackground }; + if (position < 0) Pending.Add(combined); + else Pending.Insert(position, combined); + } + + /// The server holds at , and this device knows it. + public void Adopt(string key, long version, string fingerprint) + { + Known[key] = new KnownItem(version, fingerprint); + InFlight.Remove(key); + Behind.Remove(key); + Refused.Remove(key); + RefusedCodes.Remove(key); + // Refusals lift with their cause: a synced box layout may make room, a release frees fair-use space. + var lifted = key == SyncValues.BoxesKey ? ErrorCodes.InvalidItem + : SyncValues.IsMon(key) && fingerprint == SyncValues.Deleted ? ErrorCodes.QuotaExceeded + : null; + if (lifted is null) return; + foreach (var refused in RefusedCodes.Where(r => r.Value == lifted).Select(r => r.Key).ToList()) + { + Refused.Remove(refused); + RefusedCodes.Remove(refused); + } + } + + public void AddInFlight(string key, string fingerprint) + { + if (!InFlight.TryGetValue(key, out var sent)) InFlight[key] = sent = []; + if (!sent.Contains(fingerprint)) sent.Add(fingerprint); + } + + public bool IsInFlight(string key, string fingerprint) => InFlight.TryGetValue(key, out var sent) && sent.Contains(fingerprint); + + public void AddActivity(DateTimeOffset utc, string message) + { + Activity.Add(new ActivityEntry(utc, message)); + if (Activity.Count > ActivityLimit) Activity.RemoveRange(0, Activity.Count - ActivityLimit); + } +} + +/// +/// Keeps in sync-state.json, written atomically (previous copy kept as +/// .bak). Frequent small changes can wait for ; nothing is lost by waiting, because +/// the engine finds unsynced changes by comparing the Bank with what was last synced. +/// +public sealed class SyncStateStore +{ + private readonly string _path; + private readonly Lock _gate = new(); + private SyncStateData _state; + private bool _unsaved; + + public SyncStateStore(string directory) + { + Directory.CreateDirectory(directory); + _path = Path.Combine(directory, "sync-state.json"); + _state = Load(_path); + } + + public T Read(Func read) + { + lock (_gate) return read(_state); + } + + public void Update(Action change, bool persist = true) + { + lock (_gate) + { + change(_state); + _unsaved = true; + if (persist) Save(); + } + } + + public T Update(Func change, bool persist = true) + { + lock (_gate) + { + var result = change(_state); + _unsaved = true; + if (persist) Save(); + return result; + } + } + + public void Flush() + { + lock (_gate) + { + if (_unsaved) Save(); + } + } + + /// Forgets everything (sign out). + public void Reset() + { + lock (_gate) + { + _state = new SyncStateData(); + Save(); + } + } + + private void Save() + { + var tmp = _path + ".tmp"; + File.WriteAllText(tmp, JsonSerializer.Serialize(_state, ProtocolJson.Options)); + if (File.Exists(_path)) File.Copy(_path, _path + ".bak", overwrite: true); + File.Move(tmp, _path, overwrite: true); + _unsaved = false; + } + + private static SyncStateData Load(string path) + { + var existed = false; + foreach (var candidate in new[] { path, path + ".bak" }) + { + try + { + if (!File.Exists(candidate)) continue; + existed = true; + if (JsonSerializer.Deserialize(File.ReadAllText(candidate), ProtocolJson.Options) is { } state) return state; + } + catch (Exception error) when (error is IOException or JsonException or UnauthorizedAccessException) + { + // Try the backup next. + } + } + return new SyncStateData { NeedsResync = existed }; + } +} diff --git a/src/PKForge.Sync/SyncValues.cs b/src/PKForge.Sync/SyncValues.cs new file mode 100644 index 00000000..c48090f1 --- /dev/null +++ b/src/PKForge.Sync/SyncValues.cs @@ -0,0 +1,89 @@ +using System.Security.Cryptography; +using System.Text; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Nodes; +using PKForge.Domain; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +/// +/// How the local Bank becomes sync items (keys and JSON values) and back, and how two values are +/// compared: by a fingerprint of their canonical JSON, so formatting and escaping never look like edits. +/// +public static class SyncValues +{ + /// The fingerprint of an item that does not exist (never created, or released). + public const string Deleted = "deleted"; + + private static readonly JsonSerializerOptions Canonical = new() { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; + + public static readonly string BoxesKey = Key(ItemKinds.Boxes, ItemKinds.SingletonId); + public static readonly string SettingsKey = Key(ItemKinds.Settings, ItemKinds.SingletonId); + + public static string Key(string kind, string id) => $"{kind}:{id}"; + public static string MonKey(Guid id) => Key(ItemKinds.Mon, id.ToString("D")); + + public static (string Kind, string Id) SplitKey(string key) + { + var colon = key.IndexOf(':', StringComparison.Ordinal); + return (key[..colon], key[(colon + 1)..]); + } + + public static bool IsMon(string key) => key.StartsWith(ItemKinds.Mon + ":", StringComparison.Ordinal); + public static Guid MonId(string key) => Guid.Parse(SplitKey(key).Id); + + public static JsonElement Mon(BankEntry entry, string blob, DateTimeOffset movedUtc) => + JsonSerializer.SerializeToElement( + new MonValue(entry.Box, entry.Slot, movedUtc, JsonSerializer.SerializeToElement(entry.Info, ProtocolJson.Options), entry.AddedUtc, blob), + ProtocolJson.Options); + + public static MonValue ReadMon(JsonElement value) => + value.Deserialize(ProtocolJson.Options) ?? throw new InvalidDataException("Not a Pokémon value."); + + public static BankEntry ToEntry(Guid id, MonValue mon) => + new(id, mon.Box, mon.Slot, + mon.Info.Deserialize(ProtocolJson.Options) ?? throw new InvalidDataException("A Pokémon value without its facts."), + mon.AddedUtc); + + /// True when two Pokémon values differ only in where they sit (and when they were moved). + public static bool SameExceptPosition(JsonElement a, JsonElement b) + { + var x = ReadMon(a); + var y = ReadMon(b); + return x.Blob == y.Blob && x.AddedUtc == y.AddedUtc && Fingerprint(x.Info) == Fingerprint(y.Info); + } + + public static JsonElement Boxes(int count, IReadOnlyDictionary names, IReadOnlyDictionary wallpapers) => + JsonSerializer.SerializeToElement(new BoxesValue(count, Indexed(names, count), Indexed(wallpapers, count)), ProtocolJson.Options); + + public static BoxesValue ReadBoxes(JsonElement value) => + value.Deserialize(ProtocolJson.Options) ?? throw new InvalidDataException("Not a boxes value."); + + /// An indexed list (null = nothing set) back to a box → value map. + public static Dictionary FromIndexed(IReadOnlyList? values) + { + var result = new Dictionary(); + for (var i = 0; i < (values?.Count ?? 0); i++) + { + if (!string.IsNullOrWhiteSpace(values![i])) result[i] = values[i]!; + } + return result; + } + + public static string Fingerprint(JsonElement? value) + { + if (value is not { } element) return Deleted; + var canonical = JsonSerializer.Serialize(JsonNode.Parse(element.GetRawText()), Canonical); + return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(canonical))); + } + + private static List Indexed(IReadOnlyDictionary values, int count) + { + var length = values.Keys.Where(k => k >= 0 && k < count).DefaultIfEmpty(-1).Max() + 1; + var list = new List(length); + for (var i = 0; i < length; i++) list.Add(values.TryGetValue(i, out var value) ? value : null); + return list; + } +} diff --git a/src/PKForge.Sync/SyncedBankService.cs b/src/PKForge.Sync/SyncedBankService.cs new file mode 100644 index 00000000..c8b85a5e --- /dev/null +++ b/src/PKForge.Sync/SyncedBankService.cs @@ -0,0 +1,394 @@ +using System.Text.Json; +using PKForge.Domain; +using PKForge.Infrastructure; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync; + +/// Every sync item's current local value, plus each Pokémon's blob id. +public sealed record LocalValues(IReadOnlyDictionary Values, IReadOnlyDictionary Blobs); + +/// A server value to apply locally (null value = released). carries bytes this device lacks. +public sealed record RemoteChange(string Key, JsonElement? Value, byte[]? Data = null); + +public sealed record ApplyResult(IReadOnlyList Applied, IReadOnlyList Skipped, IReadOnlyList<(Guid Id, int Box, int Slot)> Relocated); + +/// +/// The Bank while connected to a sync server: underneath, so everything works +/// offline exactly as in Local mode, plus a note of which items each operation touched (pushed together +/// later) and the hooks the sync engine uses to read and apply items. +/// +public sealed class SyncedBankService : IBankService +{ + private readonly Lock _gate = new(); + private readonly FileBankService _inner; + private readonly BankDecor _decor; + private readonly SyncStateStore _state; + private readonly TimeProvider _time; + + public SyncedBankService(FileBankService inner, BankDecor decor, SyncStateStore state, TimeProvider? time = null) + { + _inner = inner; + _decor = decor; + _state = state; + _time = time ?? TimeProvider.System; + // Box names, wallpapers and settings are edited straight on the decor, outside this service. + decor.Names.Changed += (_, _) => Record([SyncValues.BoxesKey], _inner.BoxCount); + decor.Wallpapers.Changed += (_, _) => Record([SyncValues.BoxesKey], _inner.BoxCount); + decor.Settings.Changed += (_, _) => Record([SyncValues.SettingsKey], _inner.BoxCount); + } + + /// Raised after changes from another device were written (any thread). + public event EventHandler? RemoteChangesApplied; + + /// Raised after this device changed the Bank (any thread); the app schedules a sync. + public event EventHandler? LocalChanged; + + public BankDecor Decor => _decor; + + /// The Bank's index could not be read: it looks empty, so nothing may be synced from it. + public bool IsReadOnly => _inner.IsReadOnly; + + public IReadOnlyList GetAll() => _inner.GetAll(); + public int BoxCount => _inner.BoxCount; + public byte[] GetData(Guid id) => _inner.GetData(id); + public int MigrationVersion => _inner.MigrationVersion; + + public BankEntry Add(byte[] data, BankEntryInfo info) + { + lock (_gate) + { + var boxes = _inner.BoxCount; + var entry = _inner.Add(data, info); + Record([SyncValues.MonKey(entry.Id)], boxes); + return entry; + } + } + + public IReadOnlyList AddMany(IReadOnlyList deposits) + { + lock (_gate) + { + var boxes = _inner.BoxCount; + var entries = _inner.AddMany(deposits); + Record(entries.Select(e => SyncValues.MonKey(e.Id)), boxes); + return entries; + } + } + + public void Move(Guid id, int box, int slot) + { + lock (_gate) + { + var boxes = _inner.BoxCount; + var occupant = _inner.GetAll().FirstOrDefault(e => e.Box == box && e.Slot == slot && e.Id != id); + _inner.Move(id, box, slot); + Guid[] moved = occupant is null ? [id] : [id, occupant.Id]; + Record(moved.Select(SyncValues.MonKey), boxes, moved: moved); + } + } + + public void Remove(Guid id) + { + lock (_gate) + { + var existed = _inner.GetAll().Any(e => e.Id == id); + _inner.Remove(id); + if (existed) Forget([id]); + } + } + + public int RemoveMany(IReadOnlyList ids) + { + lock (_gate) + { + var present = _inner.GetAll().Select(e => e.Id).Intersect(ids).ToList(); + var removed = _inner.RemoveMany(ids); + Forget(present); + return removed; + } + } + + public void Replace(Guid id, byte[] data, BankEntryInfo info) + { + lock (_gate) + { + // Forgotten durably before the bytes change: a stale hash would hide this edit for good. + _state.Update(s => s.BlobHashes.Remove(id)); + _inner.Replace(id, data, info); + Record([SyncValues.MonKey(id)], _inner.BoxCount); + } + } + + public void AddBox() + { + lock (_gate) + { + _inner.AddBox(); + Record([SyncValues.BoxesKey], _inner.BoxCount); + } + } + + public void RemapBoxes(BankBoxRemap remap) + { + lock (_gate) + { + var before = _inner.GetAll().ToDictionary(e => e.Id, e => e.Box); + _inner.RemapBoxes(remap); + var moved = _inner.GetAll().Where(e => before.TryGetValue(e.Id, out var box) && box != e.Box).Select(e => e.Id).ToList(); + Record([SyncValues.BoxesKey, .. moved.Select(SyncValues.MonKey)], _inner.BoxCount, moved: moved); + } + } + + public int UpdateInfo(IReadOnlyList<(Guid Id, BankEntryInfo Info)> updates) + { + lock (_gate) + { + var before = _inner.GetAll().ToDictionary(e => e.Id, e => e.Info); + var changed = _inner.UpdateInfo(updates); + RecordInfoChanges(before); + return changed; + } + } + + public int CompleteMigration(int version, IReadOnlyList<(Guid Id, BankEntryInfo Expected, BankEntryInfo Info)> updates) + { + lock (_gate) + { + var before = _inner.GetAll().ToDictionary(e => e.Id, e => e.Info); + var changed = _inner.CompleteMigration(version, updates); + RecordInfoChanges(before); + return changed; + } + } + + public int Place(IReadOnlyList<(Guid Id, int Box, int Slot)> placements) + { + lock (_gate) + { + var boxes = _inner.BoxCount; + var before = _inner.GetAll().ToDictionary(e => e.Id, e => (e.Box, e.Slot)); + var count = _inner.Place(placements); + var moved = _inner.GetAll().Where(e => before.TryGetValue(e.Id, out var at) && at != (e.Box, e.Slot)).Select(e => e.Id).ToList(); + Record(moved.Select(SyncValues.MonKey), boxes, moved: moved); + return count; + } + } + + /// True when an operation recorded a change that no sync has sent yet. + public bool HasUnsyncedChanges() => _state.Read(s => s.Pending.Count > 0); + + /// Every item's current value, computed under the write lock. + public LocalValues CurrentValues() + { + lock (_gate) return CurrentValuesLocked(); + } + + /// + /// Writes server values. A change is skipped when its item no longer matches the fingerprint in + /// (someone edited it after the engine looked), so a download never + /// overwrites a fresh edit. Local Pokémon sitting where an arriving one lands move to free slots. + /// + public ApplyResult ApplyRemote(IReadOnlyList changes, IReadOnlyDictionary expected) + { + ApplyResult result; + lock (_gate) + { + var current = CurrentValuesLocked().Values; + var applied = new List(); + var skipped = new List(); + var upserts = new List(); + var removals = new List(); + var arriving = new Dictionary(); + int? boxCount = null; + BoxesValue? boxes = null; + JsonElement? settings = null; + + foreach (var change in changes) + { + var now = current.TryGetValue(change.Key, out var value) ? SyncValues.Fingerprint(value) : SyncValues.Deleted; + if (expected.TryGetValue(change.Key, out var seen) && seen != now) + { + skipped.Add(change.Key); + continue; + } + var (kind, _) = SyncValues.SplitKey(change.Key); + switch (kind) + { + case ItemKinds.Mon when change.Value is { } mon: + var id = SyncValues.MonId(change.Key); + var read = SyncValues.ReadMon(mon); + upserts.Add(new RemoteEntry(SyncValues.ToEntry(id, read), change.Data)); + arriving[id] = read; + break; + case ItemKinds.Mon: + removals.Add(SyncValues.MonId(change.Key)); + break; + case ItemKinds.Boxes when change.Value is { } box: + boxes = SyncValues.ReadBoxes(box); + boxCount = boxes.Count; + break; + case ItemKinds.Settings when change.Value is { } values: + settings = values; + break; + default: + skipped.Add(change.Key); + continue; + } + applied.Add(change.Key); + } + + var relocated = MakeRoom(upserts, removals, boxCount); + var rewritten = upserts.Where(u => u.Data is not null).Select(u => u.Entry.Id).ToList(); + if (rewritten.Count > 0) _state.Update(s => { foreach (var id in rewritten) s.BlobHashes.Remove(id); }); + _inner.ApplyRemote(upserts, removals, boxCount); + if (boxes is not null) + { + _decor.Names.ReplaceAll(SyncValues.FromIndexed(boxes.Names)); + _decor.Wallpapers.ReplaceAllIds(SyncValues.FromIndexed(boxes.Wallpapers)); + } + if (settings is { } newSettings) _decor.Settings.Replace(newSettings); + + var movedAt = _time.GetUtcNow(); + _state.Update(s => + { + foreach (var (id, mon) in arriving) + { + s.MovedUtc[id] = mon.MovedUtc; + s.BlobHashes[id] = mon.Blob; + } + foreach (var id in removals) + { + s.MovedUtc.Remove(id); + s.BlobHashes.Remove(id); + } + foreach (var (id, _, _) in relocated) s.MovedUtc[id] = movedAt; + s.RecordChange(relocated.Select(r => SyncValues.MonKey(r.Id)).ToList(), background: false); + }, persist: false); + result = new ApplyResult(applied, skipped, relocated); + } + if (result.Applied.Count > 0 || result.Relocated.Count > 0) RemoteChangesApplied?.Invoke(this, EventArgs.Empty); + return result; + } + + /// + /// Moves a Pokémon to the first free slot (adding a box if needed), skipping : + /// slots the server reported taken, which may look free here until the next download. Null when it is gone. + /// + public (int Box, int Slot)? MoveToFreeSlot(Guid id, IReadOnlyCollection<(int Box, int Slot)>? avoid = null) + { + lock (_gate) + { + var entries = _inner.GetAll(); + if (entries.All(e => e.Id != id)) return null; + var boxes = _inner.BoxCount; + var count = boxes; + var occupied = entries.Select(e => (e.Box, e.Slot)).ToHashSet(); + occupied.UnionWith(avoid ?? []); + var free = FirstFree(occupied, ref count); + _inner.Place([(id, free.Box, free.Slot)]); + Record([SyncValues.MonKey(id)], boxes, moved: [id]); + return free; + } + } + + /// Adds a copy of a Pokémon under a new id (a conflict resolved by keeping both). + public Guid KeepCopy(Guid id) + { + lock (_gate) + { + var boxes = _inner.BoxCount; + var entry = _inner.GetAll().First(e => e.Id == id); + var copy = _inner.Add(_inner.GetData(id), entry.Info); + Record([SyncValues.MonKey(copy.Id)], boxes); + return copy.Id; + } + } + + private LocalValues CurrentValuesLocked() + { + var (hashes, moved) = _state.Read(s => (new Dictionary(s.BlobHashes), new Dictionary(s.MovedUtc))); + var values = new Dictionary(); + var blobs = new Dictionary(); + var computed = new Dictionary(); + foreach (var entry in _inner.GetAll()) + { + if (!hashes.TryGetValue(entry.Id, out var hash)) computed[entry.Id] = hash = BlobHash.Compute(_inner.GetData(entry.Id)); + var key = SyncValues.MonKey(entry.Id); + values[key] = SyncValues.Mon(entry, hash, moved.TryGetValue(entry.Id, out var at) ? at : entry.AddedUtc); + blobs[key] = hash; + } + if (computed.Count > 0) + _state.Update(s => { foreach (var (id, hash) in computed) s.BlobHashes[id] = hash; }, persist: false); + values[SyncValues.BoxesKey] = SyncValues.Boxes(_inner.BoxCount, _decor.Names.All(), _decor.Wallpapers.AllIds()); + values[SyncValues.SettingsKey] = _decor.Settings.ToJson(); + return new LocalValues(values, blobs); + } + + /// Adds moves for local Pokémon that sit where arriving ones land; returns where they went. + private List<(Guid Id, int Box, int Slot)> MakeRoom(List upserts, List removals, int? boxCount) + { + var arrivingIds = upserts.Select(u => u.Entry.Id).ToHashSet(); + var leaving = removals.ToHashSet(); + var landing = upserts.Select(u => (u.Entry.Box, u.Entry.Slot)).ToHashSet(); + var staying = _inner.GetAll().Where(e => !arrivingIds.Contains(e.Id) && !leaving.Contains(e.Id)).ToList(); + var occupied = new HashSet<(int, int)>(landing); + foreach (var entry in staying.Where(e => !landing.Contains((e.Box, e.Slot)))) occupied.Add((entry.Box, entry.Slot)); + + var count = Math.Max(boxCount ?? _inner.BoxCount, 1); + var relocated = new List<(Guid, int, int)>(); + foreach (var entry in staying.Where(e => landing.Contains((e.Box, e.Slot)))) + { + var free = FirstFree(occupied, ref count); + occupied.Add(free); + upserts.Add(new RemoteEntry(entry with { Box = free.Box, Slot = free.Slot }, null)); + relocated.Add((entry.Id, free.Box, free.Slot)); + } + return relocated; + } + + private static (int Box, int Slot) FirstFree(HashSet<(int, int)> occupied, ref int count) + { + for (var box = 0; box < count; box++) + { + for (var slot = 0; slot < IBankService.SlotsPerBox; slot++) + { + if (!occupied.Contains((box, slot))) return (box, slot); + } + } + return (count++, 0); + } + + private void Record(IEnumerable keys, int boxCountBefore, bool background = false, IEnumerable? moved = null) + { + var list = keys.ToList(); + if (_inner.BoxCount != boxCountBefore) list.Add(SyncValues.BoxesKey); + var at = _time.GetUtcNow(); + _state.Update(s => + { + foreach (var id in moved ?? []) s.MovedUtc[id] = at; + s.RecordChange(list, background); + }, persist: false); + LocalChanged?.Invoke(this, EventArgs.Empty); + } + + private void RecordInfoChanges(Dictionary before) + { + var changed = _inner.GetAll().Where(e => before.TryGetValue(e.Id, out var info) && info != e.Info).Select(e => SyncValues.MonKey(e.Id)).ToList(); + Record(changed, _inner.BoxCount, background: true); + } + + private void Forget(IReadOnlyCollection ids) + { + _state.Update(s => + { + foreach (var id in ids) + { + s.BlobHashes.Remove(id); + s.MovedUtc.Remove(id); + } + s.RecordChange(ids.Select(SyncValues.MonKey).ToList(), background: false); + }, persist: false); + LocalChanged?.Invoke(this, EventArgs.Empty); + } +} diff --git a/tests/PKForge.Sync.Tests/BankSnapshotsTests.cs b/tests/PKForge.Sync.Tests/BankSnapshotsTests.cs new file mode 100644 index 00000000..0a5d13f5 --- /dev/null +++ b/tests/PKForge.Sync.Tests/BankSnapshotsTests.cs @@ -0,0 +1,95 @@ +using Microsoft.Extensions.Time.Testing; +using PKForge.Domain; +using PKForge.Infrastructure; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class BankSnapshotsTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "pkforge-tests", Guid.NewGuid().ToString("N")); + private readonly FakeTimeProvider _time = new(TestData.T0); + private string BankRoot => Path.Combine(_root, "bank"); + private string SnapshotRoot => Path.Combine(_root, "snapshots"); + + public void Dispose() + { + if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); + } + + private FileBankSnapshots Snapshots(int keep = 5) => new(BankRoot, SnapshotRoot, _time, keep); + + [Fact] + public void CopiesAreListedNewestFirstWithTheirReasonAndPokemonCount() + { + var bank = new FileBankService(BankRoot); + bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + Snapshots().Take("First"); + _time.Advance(TimeSpan.FromMinutes(5)); + bank.Add(TestData.Bytes(2), TestData.Info(2, "B")); + Snapshots().Take("Second"); + + var list = Snapshots().List(); + + Assert.Equal(["Second", "First"], list.Select(s => s.Reason)); + Assert.Equal([2, 1], list.Select(s => s.PokemonCount)); + Assert.Equal(TestData.T0.AddMinutes(5), list[0].CreatedUtc); + } + + [Fact] + public void RestoringBringsBackTheCopiedBankAndKeepsTheCurrentOneFirst() + { + var bank = new FileBankService(BankRoot); + var decor = new BankDecor(BankRoot); + var kept = bank.Add(TestData.Bytes(1), TestData.Info(1, "Kept")); + decor.Names.SetMany([(0, "Old name")]); + Snapshots().Take("Before replacing"); + var copy = Snapshots().List().Single(); + + _time.Advance(TimeSpan.FromMinutes(1)); + bank.Remove(kept.Id); + var later = bank.Add(TestData.Bytes(2), TestData.Info(2, "Later")); + decor.Names.SetMany([(0, "New name")]); + + Snapshots().Restore(copy.Id, "Before restoring"); + bank.Reload(); + decor.Reload(); + + Assert.Equal([kept.Id], bank.GetAll().Select(e => e.Id)); + Assert.Equal(TestData.Bytes(1), bank.GetData(kept.Id)); + Assert.False(File.Exists(Path.Combine(BankRoot, later.Id.ToString("N") + ".bin"))); + Assert.Equal("Old name", decor.Names.Get(0)); + var safety = Snapshots().List().First(); + Assert.Equal("Before restoring", safety.Reason); + Assert.Equal(1, safety.PokemonCount); + } + + [Fact] + public void TheSafetyCopyNeverPushesOutTheCopyBeingRestored() + { + new FileBankService(BankRoot).Add(TestData.Bytes(1), TestData.Info(1, "A")); + Snapshots(keep: 2).Take("Oldest"); + var oldest = Snapshots(keep: 2).List().Single(); + _time.Advance(TimeSpan.FromMinutes(1)); + Snapshots(keep: 2).Take("Newer"); + + _time.Advance(TimeSpan.FromMinutes(1)); + Snapshots(keep: 2).Restore(oldest.Id, "Before restoring"); + + Assert.Contains(Snapshots(keep: 2).List(), s => s.Id == oldest.Id); + Assert.Equal(TestData.Bytes(1), new FileBankService(BankRoot).GetData(new FileBankService(BankRoot).GetAll().Single().Id)); + } + + [Fact] + public void AnUnknownCopyIsRefusedWithoutTouchingTheBank() + { + var bank = new FileBankService(BankRoot); + var a = bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + + Assert.Throws(() => Snapshots().Restore("20260101-000000-000", "Before restoring")); + Assert.Throws(() => Snapshots().Restore("../bank", "Before restoring")); + + Assert.Equal([a.Id], new FileBankService(BankRoot).GetAll().Select(e => e.Id)); + } +} diff --git a/tests/PKForge.Sync.Tests/ConflictTests.cs b/tests/PKForge.Sync.Tests/ConflictTests.cs new file mode 100644 index 00000000..a8ee64d7 --- /dev/null +++ b/tests/PKForge.Sync.Tests/ConflictTests.cs @@ -0,0 +1,286 @@ +using PKForge.Domain; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class ConflictTests +{ + private static async Task<(SyncServer Server, TestDevice Phone, TestDevice Deck, BankEntry Shared)> SharedPokemonAsync() + { + var server = new SyncServer(); + var phone = new TestDevice(server, await server.SignUpAsync()); + var shared = phone.Deposit(1, "Sparky"); + await phone.JoinAsync(); + var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + return (server, phone, deck, shared); + } + + private static string Nickname(TestDevice device, Guid id) => device.Bank.GetAll().Single(e => e.Id == id).Info.Nickname; + + [Theory] + [InlineData(ConflictChoice.KeepThisDevice, "Deck name", 1)] + [InlineData(ConflictChoice.KeepOtherDevice, "Phone name", 1)] + [InlineData(ConflictChoice.KeepBoth, "Phone name", 2)] + public async Task EditingTheSamePokemonOnTwoDevicesAsksWhichToKeep(ConflictChoice choice, string survivor, int count) + { + var (server, phone, deck, shared) = await SharedPokemonAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + phone.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Phone name" }); + deck.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Deck name" }); + await phone.SyncAsync(); + + var report = await deck.SyncAsync(); + var conflict = deck.Store.Read(s => s.Conflicts.Single()); + Assert.Equal(1, report.NewConflicts); + Assert.Equal(ConflictKind.BothEdited, conflict.Kind); + Assert.Equal("Deck name", Nickname(deck, shared.Id)); + + await deck.Engine.ResolveAsync(conflict.Id, choice); + await deck.SyncAsync(); + await phone.SyncAsync(); + + Assert.Equal(survivor, Nickname(phone, shared.Id)); + Assert.Equal(count, phone.Bank.GetAll().Count); + TestDevice.SameContents(phone, deck); + } + + [Fact] + public async Task TheLaterMoveWinsWithoutAsking() + { + var (server, phone, deck, shared) = await SharedPokemonAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + phone.Time.Advance(TimeSpan.FromMinutes(1)); + phone.Bank.Move(shared.Id, 0, 5); + deck.Time.Advance(TimeSpan.FromMinutes(2)); + deck.Bank.Move(shared.Id, 0, 9); + + await phone.SyncAsync(); + var report = await deck.SyncAsync(); + await phone.SyncAsync(); + + Assert.Equal(0, report.NewConflicts); + Assert.Equal(9, phone.Bank.GetAll().Single().Slot); + TestDevice.SameContents(phone, deck); + } + + [Fact] + public async Task TheEarlierMoveGivesWayWithoutAsking() + { + var (server, phone, deck, shared) = await SharedPokemonAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + deck.Time.Advance(TimeSpan.FromMinutes(1)); + deck.Bank.Move(shared.Id, 0, 9); + phone.Time.Advance(TimeSpan.FromMinutes(2)); + phone.Bank.Move(shared.Id, 0, 5); + + await phone.SyncAsync(); + var report = await deck.SyncAsync(); + + Assert.Equal(0, report.NewConflicts); + Assert.Equal(5, deck.Bank.GetAll().Single().Slot); + } + + [Fact] + public async Task TwoDevicesFillingTheSameSlotsKeepEveryPokemon() + { + var (server, phone, deck, _) = await SharedPokemonAsync(); + using var __ = server; + using var ___ = phone; + using var ____ = deck; + var fromPhone = Enumerable.Range(10, 3).Select(i => phone.Deposit(i).Id).ToList(); + var fromDeck = Enumerable.Range(20, 3).Select(i => deck.Deposit(i).Id).ToList(); + + await phone.SyncAsync(); + var report = await deck.SyncAsync(); + await phone.SyncAsync(); + + Assert.Equal(3, report.Relocated); + Assert.Equal(7, phone.Bank.GetAll().Count); + Assert.Contains(deck.Store.Read(s => s.Activity), a => a.Message.Contains("filled on another device", StringComparison.Ordinal)); + TestDevice.SameContents(phone, deck); + } + + [Fact] + public async Task EditedHereButReleasedThereKeepsTheEditedCopyWhenChosen() + { + var (server, phone, deck, shared) = await SharedPokemonAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + phone.Bank.Remove(shared.Id); + deck.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Still loved" }); + await phone.SyncAsync(); + + await deck.SyncAsync(); + var conflict = deck.Store.Read(s => s.Conflicts.Single()); + Assert.Equal(ConflictKind.EditedHereReleasedThere, conflict.Kind); + await deck.Engine.ResolveAsync(conflict.Id, ConflictChoice.KeepThisDevice); + await deck.SyncAsync(); + await phone.SyncAsync(); + + Assert.Equal("Still loved", Nickname(phone, shared.Id)); + } + + [Fact] + public async Task ReleasedHereButEditedThereCanBringItBack() + { + var (server, phone, deck, shared) = await SharedPokemonAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + phone.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Edited" }); + deck.Bank.Remove(shared.Id); + await phone.SyncAsync(); + + await deck.SyncAsync(); + var conflict = deck.Store.Read(s => s.Conflicts.Single()); + Assert.Equal(ConflictKind.ReleasedHereEditedThere, conflict.Kind); + await deck.Engine.ResolveAsync(conflict.Id, ConflictChoice.KeepOtherDevice); + + Assert.Equal("Edited", Nickname(deck, shared.Id)); + } + + [Fact] + public async Task BackgroundFactRewritesNeverAsk() + { + var (server, phone, deck, shared) = await SharedPokemonAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + phone.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "User edit" }); + deck.Bank.UpdateInfo([(shared.Id, shared.Info with { HeldItem = 9 })]); + await phone.SyncAsync(); + + var report = await deck.SyncAsync(); + + Assert.Equal(0, report.NewConflicts); + Assert.Equal("User edit", Nickname(deck, shared.Id)); + } + + [Fact] + public async Task BoxLayoutChangedOnBothAsksWhichToKeep() + { + var (server, phone, deck, _) = await SharedPokemonAsync(); + using var __ = server; + using var ___ = phone; + using var ____ = deck; + phone.Bank.Decor.Names.SetMany([(0, "Alpha")]); + deck.Bank.Decor.Names.SetMany([(0, "Beta")]); + await phone.SyncAsync(); + + await deck.SyncAsync(); + var conflict = deck.Store.Read(s => s.Conflicts.Single()); + Assert.Equal(ConflictKind.Boxes, conflict.Kind); + await deck.Engine.ResolveAsync(conflict.Id, ConflictChoice.KeepOtherDevice); + + Assert.Equal("Alpha", deck.Bank.Decor.Names.Get(0)); + } + + [Fact] + public async Task SwappingBoxesMovesEveryPokemonAndName() + { + var (server, phone, deck, shared) = await SharedPokemonAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + phone.Bank.Decor.Names.SetMany([(0, "First")]); + await phone.SyncAsync(); + await deck.SyncAsync(); + + var remap = BankBoxRemap.Swap(phone.Bank.BoxCount, 0, 1); + phone.Bank.RemapBoxes(remap); + phone.Bank.Decor.Remap(remap); + await phone.SyncAsync(); + await deck.SyncAsync(); + + Assert.Equal(1, deck.Bank.GetAll().Single(e => e.Id == shared.Id).Box); + Assert.Equal("First", deck.Bank.Decor.Names.Get(1)); + TestDevice.SameContents(phone, deck); + } + + [Fact] + public async Task RefusedChangesAreNotSentAgainUntilTheyChange() + { + using var server = new SyncServer(); + server.Settings["Sync:MaxLiveMonsPerAccount"] = "1"; + using var phone = new TestDevice(server, await server.SignUpAsync()); + phone.Deposit(1); + phone.Deposit(2); + + var first = await phone.JoinAsync(); + var second = await phone.SyncAsync(); + + Assert.True(first.PendingChanges); + Assert.Single(phone.Store.Read(s => s.Activity), a => a.Message.StartsWith("The server refused", StringComparison.Ordinal)); + Assert.Equal(0, second.Uploaded); + } + + [Fact] + public async Task ARefusedPushStillDownloadsAndCarriesTheServersReason() + { + using var server = new SyncServer(); + var key = await server.SignUpAsync(); + using var phone = new TestDevice(server, key); + phone.Deposit(1); + await phone.JoinAsync(); + server.Writes.Deny("Storage quota reached."); + using var deck = new TestDevice(server, await server.SignInAsync()); + + var report = await deck.JoinAsync(); + deck.Deposit(2); + var push = await deck.SyncAsync(); + + Assert.Equal(1, report.Downloaded); // the Pokémon; empty boxes and settings already match + Assert.True(push.WritesDisabled); + Assert.Equal("Storage quota reached.", push.WritesDisabledMessage); + Assert.True(push.PendingChanges); + Assert.Equal(0, push.Uploaded); + } + + [Fact] + public async Task ARefusalWithoutAReasonCarriesTheServersNeutralMessage() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + await phone.JoinAsync(); + server.Writes.Deny(); + + phone.Deposit(1); + var push = await phone.SyncAsync(); + + Assert.True(push.WritesDisabled); + Assert.Equal(PKForge.Sync.Server.Sync.SyncService.WritesDisabledMessage, push.WritesDisabledMessage); + } + + [Fact] + public async Task OnceWritesAreAllowedAgainTheWaitingChangesGoUp() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + server.Writes.Deny("Read-only for maintenance."); + + phone.Deposit(1); + var refused = await phone.SyncAsync(); + server.Writes.Allow(); + var later = await phone.SyncAsync(); + await deck.SyncAsync(); + + Assert.True(refused.WritesDisabled); + Assert.False(later.WritesDisabled); + Assert.Null(later.WritesDisabledMessage); + Assert.Equal(1, later.Uploaded); + Assert.False(later.PendingChanges); + Assert.Single(deck.Bank.GetAll()); + } +} diff --git a/tests/PKForge.Sync.Tests/ConvergenceTests.cs b/tests/PKForge.Sync.Tests/ConvergenceTests.cs new file mode 100644 index 00000000..1fa94d24 --- /dev/null +++ b/tests/PKForge.Sync.Tests/ConvergenceTests.cs @@ -0,0 +1,148 @@ +using PKForge.Domain; +using PKForge.Sync.Protocol; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +/// +/// Three devices make random offline edits and sync in random order; conflicts are resolved the way a +/// careful person would (keep both, keep the edited copy). Afterwards every device holds the same Bank, +/// and every Pokémon's bytes that no device released or replaced are still there (spec §12). +/// +public sealed class ConvergenceTests +{ + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(4)] + [InlineData(5)] + public async Task RandomOfflineEditsConvergeWithoutLosingAPokemon(int seed) + { + var random = new Random(seed); + using var server = new SyncServer(); + var devices = new List { new(server, await server.SignUpAsync()) }; + await devices[0].JoinAsync(); + for (var i = 1; i < 3; i++) + { + devices.Add(new TestDevice(server, await server.SignInAsync(device: $"Device {i}"))); + await devices[i].JoinAsync(); + } + + var created = new HashSet(); + var doomed = new HashSet(); + var nextSeed = 1000 * seed; + try + { + for (var round = 0; round < 6; round++) + { + foreach (var device in devices) + { + for (var op = 0; op < 4; op++) Edit(device, random, created, doomed, ref nextSeed); + device.Time.Advance(TimeSpan.FromSeconds(random.Next(1, 60))); + } + foreach (var device in devices.OrderBy(_ => random.Next())) await SyncAndResolveAsync(device); + } + + for (var pass = 0; pass < 4; pass++) + { + foreach (var device in devices) await SyncAndResolveAsync(device); + } + + TestDevice.SameContents([.. devices]); + var held = devices[0].Bank.GetAll().Select(e => BlobHash.Compute(devices[0].Bank.GetData(e.Id))).ToHashSet(); + Assert.Empty(created.Except(doomed).Except(held)); + Assert.All(devices, d => Assert.Empty(d.Store.Read(s => s.Conflicts))); + } + finally + { + foreach (var device in devices) device.Dispose(); + } + } + + private static void Edit(TestDevice device, Random random, HashSet created, HashSet doomed, ref int nextSeed) + { + var entries = device.Bank.GetAll(); + switch (entries.Count == 0 ? 0 : random.Next(11)) + { + case 0: + case 1: + var bytes = TestData.Bytes(nextSeed++); + created.Add(BlobHash.Compute(bytes)); + device.Bank.Add(bytes, TestData.Info(random.Next(1, 1000), $"N{nextSeed}")); + break; + case 2: + var mover = entries[random.Next(entries.Count)]; + device.Bank.Move(mover.Id, random.Next(device.Bank.BoxCount), random.Next(IBankService.SlotsPerBox)); + break; + case 3: + var released = entries[random.Next(entries.Count)]; + doomed.Add(BlobHash.Compute(device.Bank.GetData(released.Id))); + device.Bank.Remove(released.Id); + break; + case 4: + var edited = entries[random.Next(entries.Count)]; + var newBytes = TestData.Bytes(nextSeed++); + doomed.Add(BlobHash.Compute(device.Bank.GetData(edited.Id))); + created.Add(BlobHash.Compute(newBytes)); + device.Bank.Replace(edited.Id, newBytes, edited.Info with { Nickname = $"E{nextSeed}" }); + break; + case 5: + device.Bank.Decor.Names.SetMany([(random.Next(device.Bank.BoxCount), $"Box{random.Next(100)}")]); + break; + case 6: + device.Bank.AddBox(); + break; + case 7: + var count = device.Bank.BoxCount; + var remap = random.Next(3) switch + { + 0 => BankBoxRemap.Swap(count, random.Next(count), random.Next(count)), + 1 => BankBoxRemap.Move(count, random.Next(count), random.Next(count)), + _ => BankBoxRemap.Insert(count, random.Next(count + 1)), + }; + device.Bank.RemapBoxes(remap); + device.Bank.Decor.Remap(remap); + break; + case 8: + // An organizer sort: a few Pokémon trade places. + var sorted = entries.OrderBy(_ => random.Next()).Take(Math.Min(4, entries.Count)).ToList(); + var places = sorted.Select(e => (e.Box, e.Slot)).OrderBy(_ => random.Next()).ToList(); + device.Bank.Place(sorted.Select((e, i) => (e.Id, places[i].Box, places[i].Slot)).ToList()); + break; + case 9: + var deposits = new List(); + for (var i = 0; i < 2; i++) + { + var data = TestData.Bytes(nextSeed++); + created.Add(BlobHash.Compute(data)); + deposits.Add(new BankDeposit(data, TestData.Info(random.Next(1, 1000), $"N{nextSeed}"))); + } + device.Bank.AddMany(deposits); + break; + default: + // A background fact rewrite, like the held-item backfill. + var backfilled = entries[random.Next(entries.Count)]; + device.Bank.UpdateInfo([(backfilled.Id, backfilled.Info with { HeldItem = random.Next(1, 50) })]); + break; + } + } + + private static async Task SyncAndResolveAsync(TestDevice device) + { + await device.SyncAsync(); + foreach (var conflict in device.Store.Read(s => s.Conflicts.ToList())) + { + var choice = conflict.Kind switch + { + ConflictKind.BothEdited => ConflictChoice.KeepBoth, + ConflictKind.EditedHereReleasedThere => ConflictChoice.KeepThisDevice, + ConflictKind.ReleasedHereEditedThere => ConflictChoice.KeepOtherDevice, + _ => ConflictChoice.KeepThisDevice, + }; + await device.Engine.ResolveAsync(conflict.Id, choice); + } + if (device.Store.Read(s => s.Conflicts.Count) == 0) await device.SyncAsync(); + } +} diff --git a/tests/PKForge.Sync.Tests/CoordinatorTests.cs b/tests/PKForge.Sync.Tests/CoordinatorTests.cs new file mode 100644 index 00000000..ffd468f3 --- /dev/null +++ b/tests/PKForge.Sync.Tests/CoordinatorTests.cs @@ -0,0 +1,547 @@ +using System.Net.Http; +using Microsoft.Extensions.Time.Testing; +using PKForge.Infrastructure; +using PKForge.Sync.Protocol; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class CoordinatorTests : IDisposable +{ + private sealed class MemorySecrets : ISyncSecrets + { + public string? DeviceKey { get; set; } + } + + /// Fails every request while is set, like a server out of reach. + private sealed class Outage : DelegatingHandler + { + public bool Down { get; set; } + public int Attempts; + protected override Task SendAsync(HttpRequestMessage request, CancellationToken ct) + { + Interlocked.Increment(ref Attempts); + return Down ? throw new HttpRequestException("No route to host") : base.SendAsync(request, ct); + } + } + + /// Answers the change feed with a web page, like a captive portal or another service at that address. + private sealed class NotAServer : DelegatingHandler + { + public bool On { get; set; } + public int Attempts; + protected override Task SendAsync(HttpRequestMessage request, CancellationToken ct) + { + Interlocked.Increment(ref Attempts); + return On && request.RequestUri!.AbsolutePath.Contains("/v1/sync/", StringComparison.Ordinal) + ? Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent("Sign in to the Wi-Fi", System.Text.Encoding.UTF8, "text/html") }) + : base.SendAsync(request, ct); + } + } + + /// Counts pushes, and holds them while is set. + private sealed class PushCounter : DelegatingHandler + { + public int Pushes; + public TaskCompletionSource? Hold { get; set; } + public TaskCompletionSource Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken ct) + { + if (request.RequestUri!.AbsolutePath.EndsWith("/v1/sync/push", StringComparison.Ordinal)) + { + Interlocked.Increment(ref Pushes); + Started.TrySetResult(); + if (Hold is { } hold) await hold.Task.ConfigureAwait(false); + } + return await base.SendAsync(request, ct).ConfigureAwait(false); + } + } + + private readonly List _roots = []; + private static readonly Uri Server = new("http://localhost/"); + private static readonly Dictionary Secrets = []; + + public void Dispose() + { + foreach (var root in _roots) + { + try { Directory.Delete(root, recursive: true); } + catch (IOException) { } + } + } + + private (SyncCoordinator Coordinator, SyncedBankService Bank, FakeTimeProvider Time) Device(SyncServer server, DelegatingHandler? outage = null, string? root = null) + { + root ??= Path.Combine(Path.GetTempPath(), "pkforge-tests", "coordinator-" + Guid.NewGuid().ToString("N")); + _roots.Add(root); + var time = new FakeTimeProvider(TestData.T0); + var store = new SyncStateStore(Path.Combine(root, "sync")); + var bankRoot = Path.Combine(root, "bank"); + var bank = new SyncedBankService(new FileBankService(bankRoot), new BankDecor(bankRoot), store, time); + // A handler can sit in only one client's pipeline, so the device keeps one client (as the app does). + HttpClient? shared = null; + var secrets = Secrets.GetValueOrDefault(root) ?? (Secrets[root] = new MemorySecrets()); + var connection = new SyncConnection(store, secrets, _ => shared ??= outage is null ? server.CreateClient() : server.CreateDefaultClient(outage)); + return (new SyncCoordinator(connection, bank, store, null, time), bank, time); + } + + [Fact] + public async Task WithoutAServerItStaysLocal() + { + using var server = new SyncServer(); + var (sync, _, _) = Device(server); + + Assert.Null(await sync.SyncNowAsync()); + Assert.Equal(SyncPhase.NotConnected, sync.Status.Phase); + } + + [Fact] + public async Task AfterSigningUpItAsksToJoinThenSyncs() + { + using var server = new SyncServer(); + var (sync, bank, _) = Device(server); + bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + sync.Refresh(); + Assert.Equal(SyncPhase.NeedsJoin, sync.Status.Phase); + Assert.False(await sync.ServerHasPokemonAsync()); + + var report = await sync.JoinAsync(JoinMode.Merge); + + Assert.NotNull(report); + Assert.Equal(SyncPhase.Idle, sync.Status.Phase); + Assert.NotNull(sync.Status.LastSynced); + Assert.Equal("PKForge Sync", sync.Store.Read(s => s.ServerName)); + Assert.True(await sync.ServerHasPokemonAsync()); + } + + [Fact] + public async Task ItRemembersTheServersName() + { + using var server = new SyncServer(); + server.Settings["Sync:ServerName"] = " Misty's Bank Box "; + var (sync, _, _) = Device(server); + + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + + Assert.Equal("Misty's Bank Box", sync.Store.Read(s => s.ServerName)); + sync.SignOut(); + Assert.Null(sync.Store.Read(s => s.ServerName)); + } + + [Fact] + public void AnOldStateFileWithAServerKindStillLoads() + { + var root = Path.Combine(Path.GetTempPath(), "pkforge-tests", "coordinator-" + Guid.NewGuid().ToString("N")); + _roots.Add(root); + var store = new SyncStateStore(Path.Combine(root, "sync")); + store.Update(s => { s.ServerUrl = "http://localhost/"; s.Joined = true; s.Cursor = 7; }); + store.Flush(); + var file = Directory.GetFiles(Path.Combine(root, "sync"), "*.json").Single(); + var json = System.Text.Json.Nodes.JsonNode.Parse(File.ReadAllText(file))!.AsObject(); + json.Remove("serverName"); + json["serverKind"] = "cloud"; + File.WriteAllText(file, json.ToJsonString()); + + var reopened = new SyncStateStore(Path.Combine(root, "sync")); + + Assert.Equal(("http://localhost/", 7L, (string?)null), reopened.Read(s => (s.ServerUrl, s.Cursor, s.ServerName))); + } + + [Fact] + public async Task ARefusedPushShowsTheServersReasonUntilAPushIsAccepted() + { + using var server = new SyncServer(); + var (sync, bank, _) = Device(server); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + server.Writes.Deny("Storage quota reached."); + + bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + await sync.SyncNowAsync(); + + Assert.Equal(SyncPhase.Idle, sync.Status.Phase); + Assert.True(sync.Status.WritesDisabled); + Assert.Equal("Storage quota reached.", sync.Status.WritesDisabledMessage); + Assert.True(sync.Status.PendingChanges); + + server.Writes.Allow(); + await sync.SyncNowAsync(); + + Assert.False(sync.Status.WritesDisabled); + Assert.Null(sync.Status.WritesDisabledMessage); + Assert.False(sync.Status.PendingChanges); + Assert.True(await sync.ServerHasPokemonAsync()); + } + + [Fact] + public async Task ALocalChangeSyncsShortlyAfter() + { + using var server = new SyncServer(); + var (sync, bank, time) = Device(server); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + + var added = bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + time.Advance(SyncCoordinator.ChangeMaxWait); + await sync.WhenIdleAsync(); + + var deck = await server.SignInAsync(); + var changes = await server.Client(deck).GetChangesAsync(0, null, 100); + Assert.Contains(changes.Items, i => i.Id == added.Id.ToString()); + } + + [Fact] + public async Task ASingleChangePushesOneSecondLaterNotBefore() + { + using var server = new SyncServer(); + var counter = new PushCounter(); + var (sync, bank, time) = Device(server, counter); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + await sync.WhenIdleAsync(); + var before = counter.Pushes; + + bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + time.Advance(SyncCoordinator.ChangeDebounce - TimeSpan.FromMilliseconds(1)); + await sync.WhenIdleAsync(); + Assert.Equal(before, counter.Pushes); + + time.Advance(TimeSpan.FromMilliseconds(1)); + await sync.WhenIdleAsync(); + Assert.Equal(before + 1, counter.Pushes); + Assert.False(bank.HasUnsyncedChanges()); + } + + [Fact] + public async Task ABurstOfChangesIsPushedWithinTheMaxWaitAndTheRestFollows() + { + using var server = new SyncServer(); + var counter = new PushCounter(); + var (sync, bank, time) = Device(server, counter); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + await sync.WhenIdleAsync(); + var before = counter.Pushes; + + var step = TimeSpan.FromMilliseconds(500); + var elapsed = TimeSpan.Zero; + TimeSpan? firstPush = null; + for (var i = 0; i < 10; i++) // a change every 0.5 s for 5 s + { + bank.Add(TestData.Bytes((byte)(i + 1)), TestData.Info(25, "Sparky" + i)); + time.Advance(step); + elapsed += step; + await sync.WhenIdleAsync(); + if (firstPush is null && counter.Pushes > before) firstPush = elapsed; + } + Assert.NotNull(firstPush); + Assert.True(firstPush <= SyncCoordinator.ChangeMaxWait, $"first push at {firstPush}"); + + time.Advance(SyncCoordinator.ChangeDebounce); + await sync.WhenIdleAsync(); + Assert.True(counter.Pushes >= before + 2); + Assert.False(bank.HasUnsyncedChanges()); + var changes = await server.Client(await server.SignInAsync()).GetChangesAsync(0, null, 100); + Assert.Equal(10, changes.Items.Count(i => i.Kind == ItemKinds.Mon)); + } + + [Fact] + public async Task AChangeDuringASyncIsSentByAFollowUpRightAfter() + { + using var server = new SyncServer(); + var counter = new PushCounter(); + var (sync, bank, time) = Device(server, counter); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + await sync.WhenIdleAsync(); + var before = counter.Pushes; + + counter.Hold = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + time.Advance(SyncCoordinator.ChangeDebounce); + await counter.Started.Task.WaitAsync(TimeSpan.FromSeconds(10)); + var second = bank.Add(TestData.Bytes(2), TestData.Info(4, "Ember")); // while the sync is running + var release = counter.Hold; + counter.Hold = null; + release.SetResult(); + await Task.Delay(50); + await sync.WhenIdleAsync(); + + // No clock time passed since the first sync began: the follow-up needed no debounce wait. + Assert.Equal(before + 2, counter.Pushes); + Assert.False(bank.HasUnsyncedChanges()); + var changes = await server.Client(await server.SignInAsync()).GetChangesAsync(0, null, 100); + Assert.Contains(changes.Items, i => i.Id == second.Id.ToString()); + } + + [Fact] + public async Task AnUnreachableServerIsRetriedWithGrowingDelays() + { + using var server = new SyncServer(); + var outage = new Outage(); + var (sync, bank, time) = Device(server, outage); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + + outage.Down = true; + bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + time.Advance(SyncCoordinator.ChangeMaxWait); + await sync.WhenIdleAsync(); + Assert.Equal(SyncPhase.Offline, sync.Status.Phase); + Assert.True(sync.Status.PendingChanges); + + var before = outage.Attempts; + time.Advance(SyncCoordinator.FirstRetry - TimeSpan.FromSeconds(1)); + await sync.WhenIdleAsync(); + Assert.Equal(before, outage.Attempts); + time.Advance(TimeSpan.FromSeconds(1)); + await sync.WhenIdleAsync(); + Assert.True(outage.Attempts > before); + + outage.Down = false; + time.Advance(SyncCoordinator.FirstRetry * 2); + await sync.WhenIdleAsync(); + Assert.Equal(SyncPhase.Idle, sync.Status.Phase); + Assert.False(sync.Status.PendingChanges); + } + + [Fact] + public async Task NetworkComingBackSyncsAtOnce() + { + using var server = new SyncServer(); + var outage = new Outage(); + var (sync, bank, time) = Device(server, outage); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + outage.Down = true; + bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + time.Advance(SyncCoordinator.ChangeMaxWait); + await sync.WhenIdleAsync(); + + outage.Down = false; + sync.NetworkAvailable(); + await sync.WhenIdleAsync(); + + Assert.Equal(SyncPhase.Idle, sync.Status.Phase); + } + + [Fact] + public async Task ARemovedDeviceIsToldItWasSignedOut() + { + using var server = new SyncServer(); + var (sync, _, _) = Device(server); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Deck"); + await sync.JoinAsync(JoinMode.Merge); + var phone = server.Client(await server.SignInAsync(device: "Phone")); + var deck = (await phone.GetDevicesAsync()).Single(d => d.Name == "Deck"); + + await phone.RemoveDeviceAsync(deck.Id); + await sync.SyncNowAsync(); + + Assert.Equal(SyncPhase.SignedOut, sync.Status.Phase); + Assert.False(string.IsNullOrWhiteSpace(sync.Status.Message)); + } + + [Fact] + public async Task ResolvingAConflictSendsTheChoice() + { + using var server = new SyncServer(); + var (sync, bank, _) = Device(server); + var shared = bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Deck"); + await sync.JoinAsync(JoinMode.Merge); + using var phone = new TestDevice(server, await server.SignInAsync(device: "Phone")); + await phone.JoinAsync(); + phone.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Phone name" }); + await phone.SyncAsync(); + bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Deck name" }); + + await sync.SyncNowAsync(); + Assert.Equal(1, sync.Status.Conflicts); + await sync.ResolveAsync(sync.Store.Read(s => s.Conflicts.Single().Id), ConflictChoice.KeepThisDevice); + await sync.WhenIdleAsync(); + await phone.SyncAsync(); + + Assert.Equal(0, sync.Status.Conflicts); + Assert.Equal("Deck name", phone.Bank.GetAll().Single().Info.Nickname); + } + + [Fact] + public async Task SigningOutKeepsTheBankAndGoesLocal() + { + using var server = new SyncServer(); + var (sync, bank, _) = Device(server); + bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + + sync.SignOut(); + + Assert.Equal(SyncPhase.NotConnected, sync.Status.Phase); + Assert.Single(bank.GetAll()); + } + + [Fact] + public async Task OtherDevicesChangesArriveWhileTheAppSitsOpen() + { + using var server = new SyncServer(); + var (sync, bank, time) = Device(server); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + using var deck = new TestDevice(server, await server.SignInAsync(device: "Deck")); + await deck.JoinAsync(); + var arriving = deck.Deposit(7); + await deck.SyncAsync(); + + time.Advance(SyncCoordinator.PollInterval); + await sync.WhenIdleAsync(); + + Assert.Contains(bank.GetAll(), e => e.Id == arriving.Id); + } + + [Fact] + public async Task AnAnswerThatIsNotFromASyncServerFailsAndIsRetried() + { + using var server = new SyncServer(); + var portal = new NotAServer(); + var (sync, _, time) = Device(server, portal); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + + portal.On = true; + await sync.SyncNowAsync(); + Assert.NotEqual(SyncPhase.Syncing, sync.Status.Phase); + Assert.NotEqual(SyncPhase.Idle, sync.Status.Phase); + + var before = portal.Attempts; + time.Advance(SyncCoordinator.FirstRetry); + await sync.WhenIdleAsync(); + Assert.True(portal.Attempts > before); + } + + [Fact] + public async Task ChangesMadeJustBeforeLeavingTheAppAreSent() + { + using var server = new SyncServer(); + var (sync, bank, _) = Device(server); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + await sync.JoinAsync(JoinMode.Merge); + + var added = bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + sync.Suspend(); + await sync.WhenIdleAsync(); + + var changes = await server.Client(await server.SignInAsync()).GetChangesAsync(0, null, 100); + Assert.Contains(changes.Items, i => i.Id == added.Id.ToString()); + } + + [Fact] + public async Task AFailedFirstJoinStillAsksToFinishSettingUp() + { + using var server = new SyncServer(); + var outage = new Outage(); + var (sync, _, _) = Device(server, outage); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Phone"); + + outage.Down = true; + var report = await sync.JoinAsync(JoinMode.Merge); + + Assert.Null(report); + Assert.Equal(SyncPhase.NeedsJoin, sync.Status.Phase); + Assert.False(string.IsNullOrWhiteSpace(sync.Status.Message)); + } + + [Fact] + public async Task ARemovedDeviceStaysSignedOutAfterARestart() + { + using var server = new SyncServer(); + var root = Path.Combine(Path.GetTempPath(), "pkforge-tests", "coordinator-" + Guid.NewGuid().ToString("N")); + var (sync, _, _) = Device(server, root: root); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Deck"); + await sync.JoinAsync(JoinMode.Merge); + var phone = server.Client(await server.SignInAsync(device: "Phone")); + await phone.RemoveDeviceAsync((await phone.GetDevicesAsync()).Single(d => d.Name == "Deck").Id); + await sync.SyncNowAsync(); + + var (restarted, _, _) = Device(server, root: root); + + Assert.Equal(SyncPhase.SignedOut, restarted.Status.Phase); + } + + [Fact] + public async Task ALocalChangeSyncsOnItsOwnAfterMovingToAnotherServer() + { + using var serverA = new SyncServer(); + using var serverB = new SyncServer(); + var root = Path.Combine(Path.GetTempPath(), "pkforge-tests", "coordinator-" + Guid.NewGuid().ToString("N")); + _roots.Add(root); + var time = new FakeTimeProvider(TestData.T0); + var store = new SyncStateStore(Path.Combine(root, "sync")); + var bankRoot = Path.Combine(root, "bank"); + var bank = new SyncedBankService(new FileBankService(bankRoot), new BankDecor(bankRoot), store, time); + var a = new Uri("http://server-a/"); + var b = new Uri("http://server-b/"); + HttpClient? clientA = null, clientB = null; + var connection = new SyncConnection(store, new MemorySecrets(), + uri => uri.Host == a.Host ? clientA ??= serverA.CreateClient() : clientB ??= serverB.CreateClient()); + using var sync = new SyncCoordinator(connection, bank, store, null, time); + var first = bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + + await sync.Connection.SignUpAsync(a, "ash@example.com", SyncServer.Password, "Odin"); + sync.Refresh(); + await sync.ServerHasPokemonAsync(); + await sync.JoinAsync(JoinMode.Merge); + bank.Move(first.Id, 0, 3); + time.Advance(SyncCoordinator.ChangeMaxWait); + await sync.WhenIdleAsync(); + + sync.SignOut(); + await sync.Connection.SignUpAsync(b, "ash@example.com", SyncServer.Password, "Odin"); + sync.Refresh(); + Assert.False(await sync.ServerHasPokemonAsync()); + await sync.JoinAsync(JoinMode.Merge); + await sync.WhenIdleAsync(); + + bank.Move(first.Id, 0, 7); + Assert.True(bank.HasUnsyncedChanges()); + time.Advance(SyncCoordinator.ChangeMaxWait); + await sync.WhenIdleAsync(); + + Assert.False(bank.HasUnsyncedChanges()); + var deck = serverB.Client(await serverB.SignInAsync()); + var mon = (await deck.GetChangesAsync(0, null, 100)).Items.Single(i => i.Id == first.Id.ToString()); + Assert.Contains("\"slot\":7", mon.Value!.Value.GetRawText()); + } + + [Fact] + public async Task WhileTheAppIsAwayChangesWaitAndOnceBackTheySyncOnTheirOwn() + { + // Suspend holds every scheduled sync until Resume: the app must always resume it (a recreated + // activity never called OnResume, so changes stopped syncing until the app restarted). + using var server = new SyncServer(); + var (sync, bank, time) = Device(server); + await sync.Connection.SignUpAsync(Server, "ash@example.com", SyncServer.Password, "Odin"); + await sync.JoinAsync(JoinMode.Merge); + sync.Suspend(); + await sync.WhenIdleAsync(); + + var first = bank.Add(TestData.Bytes(1), TestData.Info(25, "Sparky")); + time.Advance(SyncCoordinator.ChangeMaxWait); + await sync.WhenIdleAsync(); + Assert.True(bank.HasUnsyncedChanges()); + + sync.Resume(); + await sync.WhenIdleAsync(); + var second = bank.Add(TestData.Bytes(2), TestData.Info(4, "Ember")); + time.Advance(SyncCoordinator.ChangeMaxWait); + await sync.WhenIdleAsync(); + + Assert.False(bank.HasUnsyncedChanges()); + var changes = await server.Client(await server.SignInAsync()).GetChangesAsync(0, null, 100); + Assert.Contains(changes.Items, i => i.Id == first.Id.ToString()); + Assert.Contains(changes.Items, i => i.Id == second.Id.ToString()); + } +} diff --git a/tests/PKForge.Sync.Tests/EngineTests.cs b/tests/PKForge.Sync.Tests/EngineTests.cs new file mode 100644 index 00000000..4e708b6b --- /dev/null +++ b/tests/PKForge.Sync.Tests/EngineTests.cs @@ -0,0 +1,163 @@ +using PKForge.Domain; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class EngineTests +{ + private static async Task<(SyncServer Server, TestDevice Phone, TestDevice Deck)> TwoJoinedDevicesAsync() + { + var server = new SyncServer(); + var phone = new TestDevice(server, await server.SignUpAsync()); + await phone.JoinAsync(); + var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + return (server, phone, deck); + } + + [Fact] + public async Task SyncingBeforeJoiningIsRefused() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + await Assert.ThrowsAsync(() => phone.SyncAsync()); + } + + [Fact] + public async Task TheFirstDeviceUploadsAndTheSecondDownloads() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + phone.Deposit(1); + phone.Deposit(2); + phone.Bank.Decor.Names.SetMany([(0, "Starters")]); + phone.Bank.Decor.Settings.LivingDexStartBox = 2; + + var uploaded = await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + var downloaded = await deck.JoinAsync(); + + Assert.Equal(4, uploaded.Uploaded); // two Pokémon, boxes, settings + Assert.Equal(4, downloaded.Downloaded); + Assert.False(uploaded.PendingChanges); + TestDevice.SameContents(phone, deck); + } + + [Fact] + public async Task ChangesFlowBothWays() + { + var (server, phone, deck) = await TwoJoinedDevicesAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + var a = phone.Deposit(1); + var b = phone.Deposit(2); + await phone.SyncAsync(); + await deck.SyncAsync(); + + deck.Bank.Move(a.Id, 1, 4); + deck.Bank.Remove(b.Id); + deck.Bank.Replace(a.Id, TestData.Bytes(11), a.Info with { Nickname = "Evolved" }); + await deck.SyncAsync(); + var report = await phone.SyncAsync(); + + Assert.Equal(2, report.Downloaded); + TestDevice.SameContents(phone, deck); + Assert.Equal("Evolved", phone.Bank.GetAll().Single().Info.Nickname); + } + + [Fact] + public async Task ASecondSyncWithNothingNewDoesNothing() + { + var (server, phone, deck) = await TwoJoinedDevicesAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + phone.Deposit(1); + await phone.SyncAsync(); + + var again = await phone.SyncAsync(); + + Assert.Equal((0, 0), (again.Uploaded, again.Downloaded)); + } + + [Fact] + public async Task MergingKeepsBothBanks() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + phone.Deposit(1); + phone.Deposit(2); + await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + deck.Deposit(3); + deck.Deposit(4); + + await deck.JoinAsync(JoinMode.Merge); + await phone.SyncAsync(); + + Assert.Equal(4, deck.Bank.GetAll().Count); + TestDevice.SameContents(phone, deck); + } + + [Fact] + public async Task UsingTheServersBankReplacesTheLocalOneAfterASnapshot() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + phone.Deposit(1); + await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + var local = deck.Deposit(9); + + await deck.JoinAsync(JoinMode.UseServer); + + TestDevice.SameContents(phone, deck); + var snapshot = Assert.Single(Directory.GetDirectories(deck.Snapshots)); + Assert.True(File.Exists(Path.Combine(snapshot, local.Id.ToString("N") + ".bin"))); + } + + [Fact] + public async Task ManyReleasesFromAnotherDeviceTakeASnapshotFirst() + { + var (server, phone, deck) = await TwoJoinedDevicesAsync(); + using var _ = server; + using var __ = phone; + using var ___ = deck; + var ids = Enumerable.Range(1, SyncEngine.LargeRemoval + 1).Select(i => phone.Deposit(i).Id).ToList(); + await phone.SyncAsync(); + await deck.SyncAsync(); + + phone.Bank.RemoveMany(ids); + await phone.SyncAsync(); + await deck.SyncAsync(); + + Assert.Empty(deck.Bank.GetAll()); + Assert.Single(Directory.GetDirectories(deck.Snapshots)); + } + + [Fact] + public async Task AnUndoneLocalChangeStillReceivesTheServersVersion() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + phone.Bank.Decor.Names.SetMany([(0, "Original")]); + await phone.JoinAsync(); + // The deck cannot upload (writes refused), so its own change stays local and the pull must skip the server's. + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + + phone.Bank.Decor.Names.SetMany([(0, "From the phone")]); + await phone.SyncAsync(); + server.Writes.Deny(); + deck.Bank.Decor.Names.SetMany([(0, "Typed on the deck")]); + Assert.True((await deck.SyncAsync()).WritesDisabled); + Assert.Equal("Typed on the deck", deck.Bank.Decor.Names.Get(0)); + + deck.Bank.Decor.Names.SetMany([(0, "Original")]); // undone: nothing local is left to protect + await deck.SyncAsync(); + + Assert.Equal("From the phone", deck.Bank.Decor.Names.Get(0)); + } +} diff --git a/tests/PKForge.Sync.Tests/Infrastructure/SyncServer.cs b/tests/PKForge.Sync.Tests/Infrastructure/SyncServer.cs new file mode 100644 index 00000000..130f5d4b --- /dev/null +++ b/tests/PKForge.Sync.Tests/Infrastructure/SyncServer.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using PKForge.Sync.Protocol; +using PKForge.Sync.Server.Accounts; +using PKForge.Sync.Server.Data; + +namespace PKForge.Sync.Tests.Infrastructure; + +/// The real PKForge Sync server, in-process, on a private temp directory. +public sealed class SyncServer : WebApplicationFactory +{ + public const string Password = "pikachu123"; + + public string DataDirectory { get; } = Path.Combine(Path.GetTempPath(), "pkforge-tests", "server-" + Guid.NewGuid().ToString("N")); + public Dictionary Settings { get; } = new() + { + ["Sync:AllowRegistration"] = "true", + ["Sync:AuthRequestsPerMinute"] = "100000", + ["Sync:EnablePruning"] = "false", + ["Sync:PublicBaseUrl"] = "http://localhost", + }; + + /// Whether the server accepts pushes; flip it to simulate a server that isn't accepting an account's changes. + public WriteSwitch Writes { get; } = new(); + + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.UseSetting("Sync:DataDirectory", DataDirectory); + foreach (var (key, value) in Settings) builder.UseSetting(key, value); + builder.ConfigureServices(services => + { + services.RemoveAll(); + services.AddSingleton(Writes); + }); + } + + /// A client for this server, signed in as when given. + public SyncClient Client(DeviceKeyResponse? key = null, params DelegatingHandler[] handlers) => + new(handlers.Length == 0 ? CreateClient() : CreateDefaultClient(handlers)) { DeviceKey = key?.DeviceKey }; + + public Task SignUpAsync(string email = "ash@example.com", string device = "Phone") => + Client().RegisterAsync(new RegisterRequest(email, Password, device)); + + public Task SignInAsync(string email = "ash@example.com", string device = "Deck") => + Client().LoginAsync(new LoginRequest(email, Password, device)); + + /// Direct database access, for simulating server-side events such as a restore. + public (IServiceScope Scope, SyncDbContext Db) Database() + { + var scope = Services.CreateScope(); + return (scope, scope.ServiceProvider.GetRequiredService()); + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools(); + try { Directory.Delete(DataDirectory, recursive: true); } + catch (DirectoryNotFoundException) { } + catch (IOException) { } + } +} diff --git a/tests/PKForge.Sync.Tests/Infrastructure/TestData.cs b/tests/PKForge.Sync.Tests/Infrastructure/TestData.cs new file mode 100644 index 00000000..d9575608 --- /dev/null +++ b/tests/PKForge.Sync.Tests/Infrastructure/TestData.cs @@ -0,0 +1,18 @@ +using PKForge.Domain; + +namespace PKForge.Sync.Tests.Infrastructure; + +public static class TestData +{ + public static readonly DateTimeOffset T0 = new(2026, 10, 14, 12, 0, 0, TimeSpan.Zero); + + /// Stand-in Pokémon bytes, unique per seed. + public static byte[] Bytes(int seed) + { + var bytes = new byte[344]; + new Random(seed).NextBytes(bytes); + return bytes; + } + + public static BankEntryInfo Info(int species, string nickname) => new(species, 0, false, nickname, 50, 8, "Test", "PK8"); +} diff --git a/tests/PKForge.Sync.Tests/Infrastructure/TestDevice.cs b/tests/PKForge.Sync.Tests/Infrastructure/TestDevice.cs new file mode 100644 index 00000000..418d687d --- /dev/null +++ b/tests/PKForge.Sync.Tests/Infrastructure/TestDevice.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.Time.Testing; +using PKForge.Domain; +using PKForge.Infrastructure; +using PKForge.Sync.Protocol; + +namespace PKForge.Sync.Tests.Infrastructure; + +/// One simulated device: its own Bank folder, sync state and engine, signed in to the test server. +public sealed class TestDevice : IDisposable +{ + private readonly bool _keepFiles; + + /// Reuse an existing device folder (default: a new temp folder). + /// Leave the folder on dispose, to reopen it later. + /// Intercepts this device's HTTP calls (e.g. an outage). + public TestDevice(SyncServer server, DeviceKeyResponse key, string? root = null, bool keepFiles = false, DelegatingHandler? handler = null) + { + Root = root ?? Path.Combine(Path.GetTempPath(), "pkforge-tests", "device-" + Guid.NewGuid().ToString("N")); + _keepFiles = keepFiles; + var bankRoot = Path.Combine(Root, "bank"); + Store = new SyncStateStore(Path.Combine(Root, "sync")); + Bank = new SyncedBankService(new FileBankService(bankRoot), new BankDecor(bankRoot), Store, Time); + Snapshots = Path.Combine(Root, "snapshots"); + Engine = new SyncEngine(server.Client(key, handler is null ? [] : [handler]), Bank, Store, new FileBankSnapshots(bankRoot, Snapshots, Time), Time); + } + + public string Root { get; } + public string Snapshots { get; } + public FakeTimeProvider Time { get; } = new(TestData.T0); + public SyncStateStore Store { get; } + public SyncedBankService Bank { get; } + public SyncEngine Engine { get; } + + public BankEntry Deposit(int seed, string? nickname = null) => Bank.Add(TestData.Bytes(seed), TestData.Info(25, nickname ?? $"Mon{seed}")); + public Task SyncAsync() => Engine.SyncAsync(); + public Task JoinAsync(JoinMode mode = JoinMode.Merge) => Engine.JoinAsync(mode); + + /// The Bank as comparable data: Pokémon (id, place, facts, bytes), box count, names, wallpapers, settings. + public string Contents() + { + var mons = Bank.GetAll().OrderBy(e => e.Id) + .Select(e => $"{e.Id}@{e.Box}/{e.Slot}:{e.Info}:{BlobHash.Compute(Bank.GetData(e.Id))}"); + var names = string.Join(",", Bank.Decor.Names.All().OrderBy(n => n.Key).Select(n => $"{n.Key}={n.Value}")); + var walls = string.Join(",", Bank.Decor.Wallpapers.AllIds().OrderBy(n => n.Key).Select(n => $"{n.Key}={n.Value}")); + return string.Join("\n", mons) + $"\nboxes={Bank.BoxCount}\nnames={names}\nwalls={walls}\nsettings={Bank.Decor.Settings.ToJson().GetRawText()}"; + } + + public static void SameContents(params TestDevice[] devices) + { + foreach (var device in devices.Skip(1)) Xunit.Assert.Equal(devices[0].Contents(), device.Contents()); + } + + public void Dispose() + { + if (_keepFiles) return; + try { Directory.Delete(Root, recursive: true); } + catch (IOException) { } + } +} diff --git a/tests/PKForge.Sync.Tests/Infrastructure/WriteSwitch.cs b/tests/PKForge.Sync.Tests/Infrastructure/WriteSwitch.cs new file mode 100644 index 00000000..5d7f5201 --- /dev/null +++ b/tests/PKForge.Sync.Tests/Infrastructure/WriteSwitch.cs @@ -0,0 +1,22 @@ +using PKForge.Sync.Server.Accounts; + +namespace PKForge.Sync.Tests.Infrastructure; + +/// The test server's : allows every push until is set. +public sealed class WriteSwitch : IAccountWritePolicy +{ + private volatile string? _denyReason; + private volatile bool _denied; + + /// Refuses every account's pushes with (null: the server's own neutral message). + public void Deny(string? reason = null) + { + _denyReason = reason; + _denied = true; + } + + public void Allow() => _denied = false; + + public Task CheckAsync(Guid accountId, CancellationToken ct) => + Task.FromResult(_denied ? new WriteDecision(false, _denyReason) : WriteDecision.Allow); +} diff --git a/tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj b/tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj new file mode 100644 index 00000000..50c1d79d --- /dev/null +++ b/tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj @@ -0,0 +1,23 @@ + + + + net10.0 + false + true + + + + + + + + + + + + + + + + + diff --git a/tests/PKForge.Sync.Tests/PairingEntryTests.cs b/tests/PKForge.Sync.Tests/PairingEntryTests.cs new file mode 100644 index 00000000..baf3e960 --- /dev/null +++ b/tests/PKForge.Sync.Tests/PairingEntryTests.cs @@ -0,0 +1,74 @@ +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class PairingEntryTests +{ + [Theory] + [InlineData("pkforge.example.com K7QM-4XTP", "https://pkforge.example.com/", "K7QM4XTP")] + [InlineData("https://host.example.com:8080 k7qm4xtp", "https://host.example.com:8080/", "K7QM4XTP")] + [InlineData("https://host:8080 k7qm4xtp", "https://host:8080/", "K7QM4XTP")] + [InlineData("host K7QM 4XTP", "http://host/", "K7QM4XTP")] + [InlineData(" 192.168.1.10:8080\tk7qm-4xtp ", "http://192.168.1.10:8080/", "K7QM4XTP")] + [InlineData("sync.example.com/pkforge K7QM-4XTP", "https://sync.example.com/pkforge/", "K7QM4XTP")] + [InlineData("pkforge-sync://pair?server=http%3A%2F%2F192.168.1.10%3A8080&code=ABCD2345", "http://192.168.1.10:8080/", "ABCD2345")] + [InlineData("pkforge-sync://pair?server=https%3A%2F%2Fsync.example.com&code=abcd-2345", "https://sync.example.com/", "ABCD2345")] + public void ServerAndCode(string text, string server, string code) + { + Assert.True(PairingEntry.TryParse(text, out var parsedServer, out var parsedCode, out var error), error); + Assert.Equal(server, parsedServer!.ToString()); + Assert.Equal(code, parsedCode); + } + + [Theory] + [InlineData("K7QM-4XTP")] + [InlineData("k7qm4xtp")] + [InlineData(" K7QM 4XTP ")] + [InlineData("k7-qm-4x-tp")] + public void CodeOnly(string text) + { + Assert.True(PairingEntry.TryParse(text, out var server, out var code)); + Assert.Null(server); + Assert.Equal("K7QM4XTP", code); + } + + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData("K7QM-4XT")] // too short + [InlineData("K7QM-4XTPP")] // too long + [InlineData("K7QM-4XT0")] // 0 is not in the alphabet + [InlineData("IL1O-4XTP")] // nor I, L, 1, O + [InlineData("K7QM_4XTP")] // only spaces and dashes are dropped + [InlineData("sync.example.com")] // server without a code + [InlineData("http://sync.example.com K7QM-4XTP")] // plain http off the home network + [InlineData("ftp://nas K7QM-4XTP")] + [InlineData("https://sync.example.com/?x=1 K7QM-4XTP")] + [InlineData("one two K7QM-4XTP")] // more than one word before the code + [InlineData("pkforge-sync://pair?server=https%3A%2F%2Fsync.example.com&code=NOPE")] + [InlineData("pkforge-sync://pair?server=http%3A%2F%2Fevil.example.com&code=K7QM4XTP")] + public void Rejected(string text) + { + Assert.False(PairingEntry.TryParse(text, out var server, out var code, out var error)); + Assert.Null(server); + Assert.Equal("", code); + Assert.False(string.IsNullOrWhiteSpace(error)); + } + + [Theory] + [InlineData("https://sync.example.com/", "sync.example.com K7QM-4XTP")] + [InlineData("https://sync.example.com:8443/", "sync.example.com:8443 K7QM-4XTP")] + [InlineData("https://sync.example.com/pkforge/", "sync.example.com/pkforge K7QM-4XTP")] + [InlineData("http://192.168.1.10:8080/", "http://192.168.1.10:8080 K7QM-4XTP")] + [InlineData("http://nas/", "http://nas K7QM-4XTP")] + [InlineData("https://nas.local/", "https://nas.local K7QM-4XTP")] + [InlineData("http://[fd00::5]:8080/", "http://[fd00::5]:8080 K7QM-4XTP")] + public void TheShownLineTypesBackToTheSameServerAndCode(string server, string expectedLine) + { + var line = PairingEntry.Format(new Uri(server), "K7QM4XTP"); + Assert.Equal(expectedLine, line); + Assert.True(PairingEntry.TryParse(line, out var parsedServer, out var code, out var error), error); + Assert.Equal(server, parsedServer!.ToString()); + Assert.Equal("K7QM4XTP", code); + } +} diff --git a/tests/PKForge.Sync.Tests/PerformanceTests.cs b/tests/PKForge.Sync.Tests/PerformanceTests.cs new file mode 100644 index 00000000..cfc82407 --- /dev/null +++ b/tests/PKForge.Sync.Tests/PerformanceTests.cs @@ -0,0 +1,66 @@ +using System.Diagnostics; +using System.Net.Http; +using PKForge.Domain; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +/// Sync work grows with the number of changes, not with changes × Bank size. +public sealed class PerformanceTests +{ + [Fact] + public async Task TheSameBoxInsertedOnTwoDevicesSettlesQuickly() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + phone.Bank.AddMany(Enumerable.Range(1, 2000).Select(i => new BankDeposit(TestData.Bytes(i), TestData.Info(25, $"N{i}"))).ToList()); + await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + + phone.Time.Advance(TimeSpan.FromMinutes(2)); // the phone moved them later: the deck takes every move + foreach (var device in new[] { phone, deck }) + { + var remap = BankBoxRemap.Insert(device.Bank.BoxCount, 0); + device.Bank.RemapBoxes(remap); + device.Bank.Decor.Remap(remap); + } + await phone.SyncAsync(); + var clock = Stopwatch.StartNew(); + var report = await deck.SyncAsync(); + clock.Stop(); + + Assert.Equal(0, report.NewConflicts); + TestDevice.SameContents(phone, deck); + Assert.True(clock.Elapsed < TimeSpan.FromSeconds(10), $"Took {clock.Elapsed}."); + } + + private sealed class PushCounter : DelegatingHandler + { + public int Pushes; + protected override Task SendAsync(HttpRequestMessage request, CancellationToken ct) + { + if (request.RequestUri!.AbsolutePath.EndsWith("/v1/sync/push", StringComparison.Ordinal)) Interlocked.Increment(ref Pushes); + return base.SendAsync(request, ct); + } + } + + [Fact] + public async Task ABankJoiningWithoutRecordedChangesUploadsInModestRequests() + { + using var server = new SyncServer(); + var counter = new PushCounter(); + using var phone = new TestDevice(server, await server.SignUpAsync(), handler: counter); + phone.Bank.AddMany(Enumerable.Range(1, 2500).Select(i => new BankDeposit(TestData.Bytes(i), TestData.Info(25, $"N{i}"))).ToList()); + phone.Store.Update(s => s.Pending.Clear()); // as after signing in: nothing recorded, everything new to the server + + var report = await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + + Assert.False(report.PendingChanges); + Assert.True(counter.Pushes >= 2, $"{counter.Pushes} push request(s)."); + Assert.Equal(2500, deck.Bank.GetAll().Count); + } +} diff --git a/tests/PKForge.Sync.Tests/ResyncTests.cs b/tests/PKForge.Sync.Tests/ResyncTests.cs new file mode 100644 index 00000000..35af5e1b --- /dev/null +++ b/tests/PKForge.Sync.Tests/ResyncTests.cs @@ -0,0 +1,72 @@ +using Microsoft.EntityFrameworkCore; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class ResyncTests +{ + /// Puts the server back to how it was at , as restoring an old backup would. + private static async Task RestoreServerAsync(SyncServer server, long version) + { + var (scope, db) = server.Database(); + using (scope) + { + await db.Items.Where(i => i.Version > version).ExecuteDeleteAsync(); + await db.Accounts.ExecuteUpdateAsync(s => s.SetProperty(a => a.Version, version)); + } + } + + [Fact] + public async Task ARestoredServerGetsBackWhatDevicesStillHold() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + await phone.JoinAsync(); // v1 boxes, v2 settings + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + phone.Deposit(1); + await phone.SyncAsync(); // v3 + phone.Deposit(2); + phone.Deposit(3); + await phone.SyncAsync(); // v4, v5 + await deck.SyncAsync(); + + await RestoreServerAsync(server, 3); + var recovered = await phone.SyncAsync(); + await deck.SyncAsync(); + await phone.SyncAsync(); + + Assert.True(recovered.Resynced); + Assert.Equal(3, deck.Bank.GetAll().Count); + TestDevice.SameContents(phone, deck); + Assert.Empty(phone.Store.Read(s => s.Conflicts)); + } + + [Fact] + public async Task AnUnreadableStateFileRejoinsWithoutDuplicates() + { + using var server = new SyncServer(); + var key = await server.SignUpAsync(); + string root; + using (var phone = new TestDevice(server, key, keepFiles: true)) + { + phone.Deposit(1); + phone.Deposit(2); + await phone.JoinAsync(); + root = phone.Root; + } + File.WriteAllText(Path.Combine(root, "sync", "sync-state.json"), "{broken"); + File.WriteAllText(Path.Combine(root, "sync", "sync-state.json.bak"), "{broken"); + + using var reopened = new TestDevice(server, key, root: root); + Assert.True(reopened.Store.Read(s => s.NeedsResync)); + var report = await reopened.JoinAsync(); + using var fresh = new TestDevice(server, await server.SignInAsync()); + await fresh.JoinAsync(); + + Assert.Equal(0, report.NewConflicts); + Assert.Equal(2, reopened.Bank.GetAll().Count); + TestDevice.SameContents(fresh, reopened); + } +} diff --git a/tests/PKForge.Sync.Tests/RobustnessTests.cs b/tests/PKForge.Sync.Tests/RobustnessTests.cs new file mode 100644 index 00000000..fa39dff4 --- /dev/null +++ b/tests/PKForge.Sync.Tests/RobustnessTests.cs @@ -0,0 +1,130 @@ +using System.Text.Json; +using PKForge.Domain; +using PKForge.Sync.Protocol; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +/// Sync under the things that go wrong on a real handheld: damaged files, killed apps, odd operation orders. +public sealed class RobustnessTests +{ + [Fact] + public async Task AnUnreadableBankPausesSyncInsteadOfReleasingEverything() + { + using var server = new SyncServer(); + var key = await server.SignUpAsync(); + string root; + using (var phone = new TestDevice(server, key, keepFiles: true)) + { + phone.Deposit(1); + phone.Deposit(2); + await phone.JoinAsync(); + root = phone.Root; + } + File.WriteAllText(Path.Combine(root, "bank", "index.json"), "{broken"); + File.WriteAllText(Path.Combine(root, "bank", "index.json.bak"), "{broken"); + + using var damaged = new TestDevice(server, key, root: root); + var report = await damaged.SyncAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + + Assert.True(report.Paused); + Assert.Equal(2, deck.Bank.GetAll().Count); + } + + [Fact] + public async Task AnEditToABanksBytesSurvivesTheAppBeingKilled() + { + using var server = new SyncServer(); + var key = await server.SignUpAsync(); + string root; + Guid id; + using (var phone = new TestDevice(server, key, keepFiles: true)) + { + var entry = phone.Deposit(1); + id = entry.Id; + await phone.JoinAsync(); + phone.Bank.Replace(id, TestData.Bytes(99), entry.Info); // new moves or IVs: same facts, new bytes + root = phone.Root; // killed before the next sync: nothing flushed + } + + using (var restarted = new TestDevice(server, key, root: root)) await restarted.SyncAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + + Assert.Equal(TestData.Bytes(99), deck.Bank.GetData(id)); + } + + [Fact] + public async Task APokemonInANewBoxReachesOtherDevices() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + + phone.Bank.AddBox(); + var newcomer = phone.Deposit(1); + phone.Bank.Move(newcomer.Id, phone.Bank.BoxCount - 1, 0); + phone.Bank.AddBox(); + var report = await phone.SyncAsync(); + await deck.SyncAsync(); + + Assert.False(report.PendingChanges); + Assert.Contains(deck.Bank.GetAll(), e => e.Id == newcomer.Id); + TestDevice.SameContents(phone, deck); + } + + [Fact] + public async Task ABackgroundRewriteNeverOverridesALaterEdit() + { + using var server = new SyncServer(); + using var phone = new TestDevice(server, await server.SignUpAsync()); + var shared = phone.Deposit(1, "Sparky"); + await phone.JoinAsync(); + using var deck = new TestDevice(server, await server.SignInAsync()); + await deck.JoinAsync(); + + phone.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Phone edit" }); + await phone.SyncAsync(); + deck.Bank.UpdateInfo([(shared.Id, shared.Info with { HeldItem = 3 })]); + deck.Store.Update(s => s.Pending.ForEach(g => g.Sent = true)); // the background push was cut off + deck.Bank.Replace(shared.Id, TestData.Bytes(1), shared.Info with { Nickname = "Deck edit", HeldItem = 3 }); + var report = await deck.SyncAsync(); + + Assert.Equal(1, report.NewConflicts); // two people edited it: ask, don't overwrite + Assert.Equal("Deck edit", deck.Bank.GetAll().Single().Info.Nickname); + } + + [Fact] + public async Task FactsFromANewerAppVersionAreNotStrippedByThisOne() + { + using var server = new SyncServer(); + var key = await server.SignUpAsync(); + using var phone = new TestDevice(server, key); + await phone.JoinAsync(); + var newer = server.Client(key); + var bytes = TestData.Bytes(5); + var hash = BlobHash.Compute(bytes); + await newer.UploadBlobsAsync([new BlobData(hash, bytes)]); + var id = Guid.NewGuid(); + var info = JsonSerializer.SerializeToElement(new + { + species = 25, form = 0, shiny = false, nickname = "Future", level = 5, generation = 9, sourceName = "SV", format = "PK9", + futureFact = "kept", + }); + var value = JsonSerializer.SerializeToElement(new MonValue(0, 5, TestData.T0, info, TestData.T0, hash), ProtocolJson.Options); + await newer.PushAsync(new PushRequest([new ChangeGroup(Guid.NewGuid(), [new ItemWrite(ItemKinds.Mon, id.ToString(), 0, value)])])); + + await phone.SyncAsync(); + var again = await phone.SyncAsync(); + + var stored = (await newer.GetChangesAsync(0, null, 100)).Items.Single(i => i.Id == id.ToString()); + Assert.Equal("Future", phone.Bank.GetAll().Single().Info.Nickname); + Assert.Equal(0, again.Uploaded); + Assert.Equal("kept", stored.Value!.Value.GetProperty("info").GetProperty("futureFact").GetString()); + } +} diff --git a/tests/PKForge.Sync.Tests/ServerAddressTests.cs b/tests/PKForge.Sync.Tests/ServerAddressTests.cs new file mode 100644 index 00000000..504c061a --- /dev/null +++ b/tests/PKForge.Sync.Tests/ServerAddressTests.cs @@ -0,0 +1,44 @@ +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class ServerAddressTests +{ + [Theory] + [InlineData("https://sync.example.com", "https://sync.example.com/")] + [InlineData("https://sync.example.com/pkforge/", "https://sync.example.com/pkforge/")] + [InlineData("sync.example.com", "https://sync.example.com/")] + [InlineData("192.168.1.10:8080", "http://192.168.1.10:8080/")] + [InlineData("http://10.0.0.5:8080", "http://10.0.0.5:8080/")] + [InlineData("http://nas:8080", "http://nas:8080/")] + [InlineData("http://nas.local", "http://nas.local/")] + [InlineData("http://100.101.102.103", "http://100.101.102.103/")] + [InlineData(" http://localhost:5000 ", "http://localhost:5000/")] + public void AcceptedAddresses(string input, string expected) + { + Assert.True(ServerAddress.TryParse(input, out var address, out var error), error); + Assert.Equal(expected, address!.ToString()); + } + + [Theory] + [InlineData("http://sync.example.com")] + [InlineData("http://8.8.8.8")] + [InlineData("ftp://nas")] + [InlineData("")] + [InlineData("https://sync.example.com/?x=1")] + public void RefusedAddresses(string input) + { + Assert.False(ServerAddress.TryParse(input, out _, out var error)); + Assert.False(string.IsNullOrWhiteSpace(error)); + } + + [Fact] + public void PairingLinksCarryTheServerAndCode() + { + Assert.True(PairingLink.TryParse("pkforge-sync://pair?server=http%3A%2F%2F192.168.1.10%3A8080&code=ABCD2345", out var server, out var code)); + Assert.Equal("http://192.168.1.10:8080/", server!.ToString()); + Assert.Equal("ABCD2345", code); + Assert.False(PairingLink.TryParse("https://example.com/pair?code=X", out _, out _)); + Assert.False(PairingLink.TryParse("pkforge-sync://pair?server=http%3A%2F%2Fevil.example.com&code=X", out _, out _)); + } +} diff --git a/tests/PKForge.Sync.Tests/SyncClientTests.cs b/tests/PKForge.Sync.Tests/SyncClientTests.cs new file mode 100644 index 00000000..97fc405f --- /dev/null +++ b/tests/PKForge.Sync.Tests/SyncClientTests.cs @@ -0,0 +1,68 @@ +using System.Text.Json; +using PKForge.Sync.Protocol; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class SyncClientTests +{ + [Fact] + public async Task ServerInfoNeedsNoAccount() + { + using var server = new SyncServer(); + var info = await server.Client().GetServerInfoAsync(); + Assert.Equal(SyncProtocol.Version, info.MaxProtocol); + } + + [Fact] + public async Task ServerErrorsBecomeSyncApiExceptions() + { + using var server = new SyncServer(); + await server.SignUpAsync(); + + var error = await Assert.ThrowsAsync(() => + server.Client().LoginAsync(new LoginRequest("ash@example.com", "wrong-password", "Deck"))); + + Assert.Equal(401, error.Status); + Assert.Equal(ErrorCodes.InvalidCredentials, error.Code); + } + + [Fact] + public async Task BlobsPushAndChangesRoundTrip() + { + using var server = new SyncServer(); + var client = server.Client(await server.SignUpAsync()); + var bytes = TestData.Bytes(1); + var hash = BlobHash.Compute(bytes); + + Assert.Equal([hash], await client.MissingBlobsAsync([hash])); + await client.UploadBlobsAsync([new BlobData(hash, bytes)]); + var entry = new PKForge.Domain.BankEntry(Guid.NewGuid(), 0, 0, TestData.Info(25, "Sparky"), TestData.T0); + var push = await client.PushAsync(new PushRequest([new ChangeGroup(Guid.NewGuid(), + [new ItemWrite(ItemKinds.Mon, entry.Id.ToString(), 0, SyncValues.Mon(entry, hash, TestData.T0))])])); + var changes = await client.GetChangesAsync(0, null, 100); + var downloaded = await client.DownloadBlobsAsync([hash]); + + Assert.Equal(GroupStatus.Accepted, push.Results[0].Status); + Assert.Equal(entry, SyncValues.ToEntry(entry.Id, SyncValues.ReadMon(Assert.Single(changes.Items).Value!.Value))); + Assert.Equal(bytes, Assert.Single(downloaded).Data); + Assert.Empty(await client.MissingBlobsAsync([hash])); + } + + [Fact] + public async Task DevicesAndPairingCodes() + { + using var server = new SyncServer(); + var phone = server.Client(await server.SignUpAsync()); + + var code = await phone.CreatePairingCodeAsync(); + var deck = await server.Client().RedeemPairingAsync(new RedeemPairingRequest(code.Code, "Deck")); + var devices = await phone.GetDevicesAsync(); + await phone.RemoveDeviceAsync(deck.DeviceId); + + Assert.Equal(["Phone", "Deck"], devices.Select(d => d.Name).Order().Reverse()); + var error = await Assert.ThrowsAsync(() => server.Client(deck).GetDevicesAsync()); + Assert.Equal(401, error.Status); + } +} diff --git a/tests/PKForge.Sync.Tests/SyncConnectionTests.cs b/tests/PKForge.Sync.Tests/SyncConnectionTests.cs new file mode 100644 index 00000000..109ea912 --- /dev/null +++ b/tests/PKForge.Sync.Tests/SyncConnectionTests.cs @@ -0,0 +1,74 @@ +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class SyncConnectionTests : IDisposable +{ + private sealed class MemorySecrets : ISyncSecrets + { + public string? DeviceKey { get; set; } + } + + private readonly string _root = Path.Combine(Path.GetTempPath(), "pkforge-tests", Guid.NewGuid().ToString("N")); + + public void Dispose() + { + if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); + } + + private (SyncConnection Connection, SyncStateStore Store, MemorySecrets Secrets) Device(SyncServer server, string name) + { + var store = new SyncStateStore(Path.Combine(_root, name)); + var secrets = new MemorySecrets(); + return (new SyncConnection(store, secrets, _ => server.CreateClient()), store, secrets); + } + + [Fact] + public async Task SigningUpRemembersTheServerAndAccount() + { + using var server = new SyncServer(); + var (connection, store, secrets) = Device(server, "phone"); + + var info = await connection.ProbeAsync(new Uri("http://localhost/")); + await connection.SignUpAsync(new Uri("http://localhost/"), "ash@example.com", SyncServer.Password, "Phone"); + + Assert.True(info.RegistrationOpen); + Assert.True(connection.IsConnected); + Assert.Equal("http://localhost/", store.Read(s => s.ServerUrl)); + Assert.NotNull(secrets.DeviceKey); + Assert.NotEmpty(await connection.CreateClient().GetDevicesAsync()); + } + + [Fact] + public async Task PairingJoinsTheSameAccount() + { + using var server = new SyncServer(); + var (phone, phoneStore, _) = Device(server, "phone"); + var (deck, deckStore, _) = Device(server, "deck"); + await phone.SignUpAsync(new Uri("http://localhost/"), "ash@example.com", SyncServer.Password, "Phone"); + var code = await phone.CreateClient().CreatePairingCodeAsync(); + + await deck.PairAsync(new Uri("http://localhost/"), code.Code, "Deck"); + + Assert.Equal(phoneStore.Read(s => s.AccountId), deckStore.Read(s => s.AccountId)); + Assert.NotEqual(phoneStore.Read(s => s.DeviceId), deckStore.Read(s => s.DeviceId)); + } + + [Fact] + public async Task SigningOutForgetsEverything() + { + using var server = new SyncServer(); + var (connection, store, secrets) = Device(server, "phone"); + await connection.SignInAsync(new Uri("http://localhost/"), "ash@example.com", SyncServer.Password, "Phone") + .ContinueWith(_ => { }); // no account yet: sign-in fails, nothing is stored + Assert.False(connection.IsConnected); + + await connection.SignUpAsync(new Uri("http://localhost/"), "ash@example.com", SyncServer.Password, "Phone"); + connection.SignOut(); + + Assert.False(connection.IsConnected); + Assert.Null(secrets.DeviceKey); + Assert.Null(store.Read(s => s.ServerUrl)); + } +} diff --git a/tests/PKForge.Sync.Tests/SyncStateStoreTests.cs b/tests/PKForge.Sync.Tests/SyncStateStoreTests.cs new file mode 100644 index 00000000..dee0fc01 --- /dev/null +++ b/tests/PKForge.Sync.Tests/SyncStateStoreTests.cs @@ -0,0 +1,97 @@ +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class SyncStateStoreTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "pkforge-tests", Guid.NewGuid().ToString("N")); + + public void Dispose() + { + if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); + } + + [Fact] + public void StatePersistsAcrossRestarts() + { + var store = new SyncStateStore(_root); + store.Update(s => + { + s.ServerUrl = "http://nas:8080/"; + s.Cursor = 41; + s.Adopt("mon:x", 41, "abc"); + }); + + var reopened = new SyncStateStore(_root); + + Assert.Equal(("http://nas:8080/", 41L), reopened.Read(s => (s.ServerUrl, s.Cursor))); + Assert.Equal(new KnownItem(41, "abc"), reopened.Read(s => s.Known["mon:x"])); + } + + [Fact] + public void UnsavedChangesWaitForFlush() + { + var store = new SyncStateStore(_root); + store.Update(s => s.Cursor = 7, persist: false); + + Assert.Equal(0, new SyncStateStore(_root).Read(s => s.Cursor)); + store.Flush(); + Assert.Equal(7, new SyncStateStore(_root).Read(s => s.Cursor)); + } + + [Fact] + public void RecordedChangesMergeIntoUnsentGroupsOnly() + { + var store = new SyncStateStore(_root); + store.Update(s => s.RecordChange(["mon:a", "mon:b"], background: false)); + store.Update(s => s.RecordChange(["mon:b", "mon:c"], background: true)); + Assert.Equal(["mon:a", "mon:b", "mon:c"], store.Read(s => s.Pending.Single().Keys.Order().ToList())); + Assert.False(store.Read(s => s.Pending.Single().Background)); + + store.Update(s => s.Pending.Single().Sent = true); + store.Update(s => s.RecordChange(["mon:c", "mon:d"], background: true)); + + Assert.Equal(2, store.Read(s => s.Pending.Count)); + Assert.True(store.Read(s => s.Pending[1].Background)); + } + + [Fact] + public void AdoptingAnItemClearsWhatWasWaitingOnIt() + { + var store = new SyncStateStore(_root); + store.Update(s => + { + s.AddInFlight("mon:a", "f1"); + s.Refused["mon:a"] = "f1"; + s.Adopt("mon:a", 9, "f2"); + }); + + Assert.False(store.Read(s => s.IsInFlight("mon:a", "f1"))); + Assert.False(store.Read(s => s.Refused.ContainsKey("mon:a"))); + } + + [Fact] + public void TheActivityLogKeepsTheNewestEntries() + { + var store = new SyncStateStore(_root); + store.Update(s => + { + for (var i = 0; i < SyncStateData.ActivityLimit + 5; i++) s.AddActivity(DateTimeOffset.UnixEpoch.AddMinutes(i), $"#{i}"); + }); + + Assert.Equal(SyncStateData.ActivityLimit, store.Read(s => s.Activity.Count)); + Assert.Equal("#5", store.Read(s => s.Activity[0].Message)); + } + + [Fact] + public void AnUnreadableStateFileAsksForAFullResync() + { + Directory.CreateDirectory(_root); + File.WriteAllText(Path.Combine(_root, "sync-state.json"), "{broken"); + + var store = new SyncStateStore(_root); + + Assert.True(store.Read(s => s.NeedsResync)); + Assert.False(store.Read(s => s.Joined)); + } +} diff --git a/tests/PKForge.Sync.Tests/SyncValuesTests.cs b/tests/PKForge.Sync.Tests/SyncValuesTests.cs new file mode 100644 index 00000000..0f963265 --- /dev/null +++ b/tests/PKForge.Sync.Tests/SyncValuesTests.cs @@ -0,0 +1,72 @@ +using System.Text.Json; +using PKForge.Domain; +using PKForge.Sync.Protocol; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class SyncValuesTests +{ + [Fact] + public void AMonRoundTripsThroughItsValue() + { + var entry = new BankEntry(Guid.NewGuid(), 3, 17, TestData.Info(25, "Sparky") with { HeldItem = 4, Traits = new SpriteTraits(Female: true) }, TestData.T0); + var moved = TestData.T0.AddHours(2); + + var value = SyncValues.Mon(entry, new string('a', 64), moved); + var mon = SyncValues.ReadMon(value); + + Assert.Equal(entry, SyncValues.ToEntry(entry.Id, mon)); + Assert.Equal(new string('a', 64), mon.Blob); + Assert.Equal(moved, mon.MovedUtc); + } + + [Fact] + public void KeysSplitBackIntoKindAndId() + { + var id = Guid.NewGuid(); + Assert.Equal((ItemKinds.Mon, id.ToString("D")), SyncValues.SplitKey(SyncValues.MonKey(id))); + Assert.Equal(id, SyncValues.MonId(SyncValues.MonKey(id))); + Assert.True(SyncValues.IsMon(SyncValues.MonKey(id))); + Assert.False(SyncValues.IsMon(SyncValues.BoxesKey)); + } + + [Fact] + public void BoxesKeepNamesAndWallpapersByIndexWithinTheCount() + { + var value = SyncValues.Boxes(4, new Dictionary { [1] = "Shinies", [9] = "Gone" }, new Dictionary { [3] = "color:2" }); + + var boxes = SyncValues.ReadBoxes(value); + + Assert.Equal(4, boxes.Count); + Assert.Equal([null, "Shinies"], boxes.Names!); + Assert.Equal(new Dictionary { [1] = "Shinies" }, SyncValues.FromIndexed(boxes.Names)); + Assert.Equal(new Dictionary { [3] = "color:2" }, SyncValues.FromIndexed(boxes.Wallpapers)); + } + + [Fact] + public void FingerprintsIgnoreEscapingAndFormattingButNotContent() + { + var plain = JsonDocument.Parse("""{"nickname":"Flabébé +1"}""").RootElement; + var escaped = JsonDocument.Parse("""{ "nickname" : "Flabébé +1" }""").RootElement; + var other = JsonDocument.Parse("""{"nickname":"Flabebe +1"}""").RootElement; + + Assert.Equal(SyncValues.Fingerprint(plain), SyncValues.Fingerprint(escaped)); + Assert.NotEqual(SyncValues.Fingerprint(plain), SyncValues.Fingerprint(other)); + Assert.Equal(SyncValues.Deleted, SyncValues.Fingerprint(null)); + } + + [Fact] + public void SameExceptPositionComparesEverythingButWhereItSits() + { + var entry = new BankEntry(Guid.NewGuid(), 0, 0, TestData.Info(25, "Sparky"), TestData.T0); + var blob = new string('b', 64); + var here = SyncValues.Mon(entry, blob, TestData.T0); + var moved = SyncValues.Mon(entry with { Box = 2, Slot = 9 }, blob, TestData.T0.AddMinutes(5)); + var renamed = SyncValues.Mon(entry with { Info = entry.Info with { Nickname = "Volt" } }, blob, TestData.T0); + + Assert.True(SyncValues.SameExceptPosition(here, moved)); + Assert.False(SyncValues.SameExceptPosition(here, renamed)); + } +} diff --git a/tests/PKForge.Sync.Tests/SyncedBankServiceTests.cs b/tests/PKForge.Sync.Tests/SyncedBankServiceTests.cs new file mode 100644 index 00000000..4668040d --- /dev/null +++ b/tests/PKForge.Sync.Tests/SyncedBankServiceTests.cs @@ -0,0 +1,230 @@ +using System.Text.Json; +using Microsoft.Extensions.Time.Testing; +using PKForge.Domain; +using PKForge.Infrastructure; +using PKForge.Sync.Protocol; +using PKForge.Sync.Tests.Infrastructure; +using Xunit; + +namespace PKForge.Sync.Tests; + +public sealed class SyncedBankServiceTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "pkforge-tests", Guid.NewGuid().ToString("N")); + private readonly FakeTimeProvider _time = new(TestData.T0); + private readonly SyncStateStore _store; + private readonly FileBankService _inner; + private readonly SyncedBankService _bank; + + public SyncedBankServiceTests() + { + _inner = new FileBankService(Path.Combine(_root, "bank")); + _store = new SyncStateStore(Path.Combine(_root, "sync")); + _bank = new SyncedBankService(_inner, new BankDecor(Path.Combine(_root, "bank")), _store, _time); + } + + public void Dispose() + { + if (Directory.Exists(_root)) Directory.Delete(_root, recursive: true); + } + + private List> Groups() => _store.Read(s => s.Pending.Select(g => g.Keys.ToHashSet()).ToList()); + + [Fact] + public void EachOperationRecordsTheItemsItTouched() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + var b = _bank.Add(TestData.Bytes(2), TestData.Info(2, "B")); + _store.Update(s => s.Pending.Clear()); + + _time.Advance(TimeSpan.FromMinutes(1)); + _bank.Move(a.Id, b.Box, b.Slot); + + Assert.Equal(new[] { SyncValues.MonKey(a.Id), SyncValues.MonKey(b.Id) }.Order(), Groups().Single().Order()); + Assert.Equal(_time.GetUtcNow(), _store.Read(s => s.MovedUtc[a.Id])); + } + + [Fact] + public void BoxRemapsRecordTheBoxesAndEveryPokemonThatMoved() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + _store.Update(s => s.Pending.Clear()); + + _bank.RemapBoxes(BankBoxRemap.Insert(_bank.BoxCount, 0)); + + Assert.Equal([SyncValues.BoxesKey, SyncValues.MonKey(a.Id)], Groups().Single().Order()); + } + + [Fact] + public void AutomaticFactRewritesAreBackgroundChanges() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + _store.Update(s => s.Pending.Clear()); + + _bank.UpdateInfo([(a.Id, a.Info with { HeldItem = 5 })]); + + Assert.True(_store.Read(s => s.Pending.Single().Background)); + } + + [Fact] + public void CurrentValuesDescribeEveryItem() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + _bank.Decor.Names.SetMany([(1, "Shinies")]); + _bank.Decor.Settings.LivingDexStartBox = 2; + + var local = _bank.CurrentValues(); + + Assert.Equal(a, SyncValues.ToEntry(a.Id, SyncValues.ReadMon(local.Values[SyncValues.MonKey(a.Id)]))); + Assert.Equal(BlobHash.Compute(TestData.Bytes(1)), local.Blobs[SyncValues.MonKey(a.Id)]); + Assert.Equal("Shinies", SyncValues.ReadBoxes(local.Values[SyncValues.BoxesKey]).Names![1]); + Assert.Equal(2, local.Values[SyncValues.SettingsKey].GetProperty("livingDexStartBox").GetInt32()); + } + + [Fact] + public void ApplyingRemoteChangesWritesThemAndRaisesTheEvent() + { + var raised = 0; + _bank.RemoteChangesApplied += (_, _) => raised++; + var id = Guid.NewGuid(); + var entry = new BankEntry(id, 2, 5, TestData.Info(25, "Sparky"), TestData.T0); + var blob = BlobHash.Compute(TestData.Bytes(9)); + var boxes = SyncValues.Boxes(4, new Dictionary { [0] = "Starters" }, new Dictionary()); + + var result = _bank.ApplyRemote( + [new RemoteChange(SyncValues.MonKey(id), SyncValues.Mon(entry, blob, TestData.T0), TestData.Bytes(9)), + new RemoteChange(SyncValues.BoxesKey, boxes)], + new Dictionary()); + + Assert.Equal(2, result.Applied.Count); + Assert.Equal(entry, _bank.GetAll().Single()); + Assert.Equal(4, _bank.BoxCount); + Assert.Equal("Starters", _bank.Decor.Names.Get(0)); + Assert.Equal(blob, _store.Read(s => s.BlobHashes[id])); + Assert.Equal(1, raised); + } + + [Fact] + public void ApplyRemoteSkipsItemsChangedSinceTheEngineLooked() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + var seen = SyncValues.Fingerprint(_bank.CurrentValues().Values[SyncValues.MonKey(a.Id)]); + _bank.Replace(a.Id, TestData.Bytes(1), a.Info with { Nickname = "Edited just now" }); + var remote = SyncValues.Mon(a with { Info = a.Info with { Nickname = "From the server" } }, BlobHash.Compute(TestData.Bytes(1)), TestData.T0); + + var result = _bank.ApplyRemote([new RemoteChange(SyncValues.MonKey(a.Id), remote)], + new Dictionary { [SyncValues.MonKey(a.Id)] = seen }); + + Assert.Equal([SyncValues.MonKey(a.Id)], result.Skipped); + Assert.Equal("Edited just now", _bank.GetAll().Single().Info.Nickname); + } + + [Fact] + public void ALocalPokemonInTheWayOfAnArrivingOneMovesAside() + { + var resident = _bank.Add(TestData.Bytes(1), TestData.Info(1, "Resident")); + _store.Update(s => s.Pending.Clear()); + var newcomer = new BankEntry(Guid.NewGuid(), resident.Box, resident.Slot, TestData.Info(2, "Newcomer"), TestData.T0); + + var result = _bank.ApplyRemote( + [new RemoteChange(SyncValues.MonKey(newcomer.Id), SyncValues.Mon(newcomer, BlobHash.Compute(TestData.Bytes(2)), TestData.T0), TestData.Bytes(2))], + new Dictionary()); + + var moved = _bank.GetAll().Single(e => e.Id == resident.Id); + Assert.NotEqual((resident.Box, resident.Slot), (moved.Box, moved.Slot)); + Assert.Equal((resident.Id, moved.Box, moved.Slot), Assert.Single(result.Relocated)); + Assert.Contains(SyncValues.MonKey(resident.Id), Groups().Single()); + } + + [Fact] + public void MoveToFreeSlotLeavesTheCurrentSlot() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + + var moved = _bank.MoveToFreeSlot(a.Id); + + Assert.NotNull(moved); + Assert.NotEqual((a.Box, a.Slot), moved!.Value); + Assert.Equal(moved.Value, (_bank.GetAll().Single().Box, _bank.GetAll().Single().Slot)); + } + + [Fact] + public void MoveToFreeSlotAvoidsSlotsTheServerSaidAreTaken() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); // (0,0) + var b = _bank.Add(TestData.Bytes(2), TestData.Info(2, "B")); // (0,1) + _bank.MoveToFreeSlot(a.Id, avoid: [(0, 0), (0, 2)]); + + var moved = _bank.MoveToFreeSlot(b.Id, avoid: [(0, 0), (0, 1), (0, 2)]); + + Assert.Equal((0, 4), moved); + Assert.Equal((0, 3), (_bank.GetAll().Single(e => e.Id == a.Id).Box, _bank.GetAll().Single(e => e.Id == a.Id).Slot)); + } + + [Fact] + public void KeepCopyAddsAnIdenticalPokemonUnderANewId() + { + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + + var copy = _bank.KeepCopy(a.Id); + + Assert.NotEqual(a.Id, copy); + Assert.Equal(a.Info, _bank.GetAll().Single(e => e.Id == copy).Info); + Assert.Equal(TestData.Bytes(1), _bank.GetData(copy)); + } + + [Fact] + public void LocalChangesAreAnnouncedButRemoteOnesAreNot() + { + var local = 0; + _bank.LocalChanged += (_, _) => local++; + var a = _bank.Add(TestData.Bytes(1), TestData.Info(1, "A")); + _bank.Move(a.Id, 0, 5); + var id = Guid.NewGuid(); + var entry = new BankEntry(id, 1, 0, TestData.Info(2, "B"), TestData.T0); + + _bank.ApplyRemote([new RemoteChange(SyncValues.MonKey(id), SyncValues.Mon(entry, BlobHash.Compute(TestData.Bytes(2)), TestData.T0), TestData.Bytes(2))], + new Dictionary()); + + Assert.Equal(2, local); + } + [Fact] + public void BoxDecorEditsAreRecordedAndAnnouncedLikeAnyChange() + { + var local = 0; + _bank.LocalChanged += (_, _) => local++; + + _bank.Decor.Wallpapers.Set(0, BankWallpaper.Color(3)); + _bank.Decor.Names.SetMany([(1, "Shiny hunts")]); + + Assert.Equal(2, local); + Assert.Contains(SyncValues.BoxesKey, Groups().Single()); + } + + [Fact] + public void SettingsEditsAreRecordedAndAnnounced() + { + var local = 0; + _bank.LocalChanged += (_, _) => local++; + + _bank.Decor.Settings.LivingDexStartBox = 2; + + Assert.Equal(1, local); + Assert.Contains(SyncValues.SettingsKey, Groups().Single()); + } + + [Fact] + public void DecorFromAnotherDeviceIsNotAnnounced() + { + var local = 0; + _bank.LocalChanged += (_, _) => local++; + var boxes = SyncValues.Boxes(1, new Dictionary { [0] = "Far" }, new Dictionary { [0] = "color:2" }); + using var settings = JsonDocument.Parse("""{"livingDexStartBox":1}"""); + + _bank.ApplyRemote([new RemoteChange(SyncValues.BoxesKey, boxes), new RemoteChange(SyncValues.SettingsKey, settings.RootElement.Clone())], + new Dictionary()); + + Assert.Equal(0, local); + Assert.Equal("Far", _bank.Decor.Names.Get(0)); + } +} From 815fb7812a0bd63ec819c8bd159a3b317e11c4b6 Mon Sep 17 00:00:00 2001 From: YoruDev-Ryland <159859604+YoruDev-Ryland@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:59:24 -0500 Subject: [PATCH 3/3] feat(app): Bank Sync page, Bank restore points - Bank Sync page: connect with an address and sign in, a pairing code (typed or from a QR picture, for devices without a camera), status, conflicts, devices and Stop syncing. Syncs a second after a change, when the app returns, and every two minutes. - Restore points also list the Bank copies sync takes; restoring one turns Bank Sync off first so the old Bank isn't sent to other devices. - The living dex start box moves once from device preferences into the Bank's settings, so every device fills the same boxes. - App lifecycle: Android can recreate the activity without OnResume; the return to the foreground is now completed when the window activates. --- README.md | 6 + src/PKForge.App/App.cs | 47 +- src/PKForge.App/MauiProgram.cs | 23 +- src/PKForge.App/PKForge.App.csproj | 2 + .../Platforms/Android/AndroidManifest.xml | 11 +- src/PKForge.App/Platforms/Android/linker.xml | 3 + src/PKForge.App/Services/BankBoxDecor.cs | 16 +- src/PKForge.App/Services/BankRestorePoints.cs | 39 ++ src/PKForge.App/Services/BankServiceSwitch.cs | 32 + .../Services/LivingDexAutopilot.cs | 28 +- src/PKForge.App/Services/QrEntityService.cs | 8 +- src/PKForge.App/Services/SyncPlatform.cs | 78 +++ .../ViewModels/BackupHistoryViewModel.cs | 41 +- src/PKForge.App/Views/BackupHistoryPage.cs | 10 + src/PKForge.App/Views/BankPage.cs | 47 +- src/PKForge.App/Views/BankSyncPage.cs | 644 ++++++++++++++++++ src/PKForge.App/Views/DsKit.cs | 11 +- src/PKForge.App/Views/HomePage.cs | 3 + src/PKForge.App/Views/QrPopup.cs | 21 +- src/PKForge.App/Views/TextPopup.cs | 4 +- src/PKForge.Domain/AppForeground.cs | 28 + .../AppForegroundTests.cs | 48 ++ 22 files changed, 1115 insertions(+), 35 deletions(-) create mode 100644 src/PKForge.App/Services/BankRestorePoints.cs create mode 100644 src/PKForge.App/Services/BankServiceSwitch.cs create mode 100644 src/PKForge.App/Services/SyncPlatform.cs create mode 100644 src/PKForge.App/Views/BankSyncPage.cs create mode 100644 src/PKForge.Domain/AppForeground.cs create mode 100644 tests/PKForge.Domain.Tests/AppForegroundTests.cs diff --git a/README.md b/README.md index 7af6a7a5..250246ef 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,10 @@ Not affiliated with Nintendo, Game Freak, or The Pokémon Company. - **A Bank like Pokémon Home.** Store your whole collection in a cross-game Bank with as many themed boxes as you need. Organize with the gamepad, keep living dexes, and pull anything back out into any save whenever you need it. +- **Bank Sync (optional).** Keep one Bank on all your devices through a + [PKForge Sync server](https://github.com/YoruDev-Ryland/pkforge-sync-server) you run with Docker. + Every device keeps working offline, and changes made on two devices are shown to you, never + silently lost. - **Track your collection.** The Living Dex tracker follows all 1025 Pokémon across every generation, including shiny variants, so you always know what you still need. - **Living Dex Autopilot.** Builds a living dex from the Pokémon you already own across @@ -185,6 +189,7 @@ Join for updates, support, bug reports, feature requests, or just to chat about git submodule update --init --recursive dotnet test tests/PKForge.Domain.Tests/PKForge.Domain.Tests.csproj dotnet test tests/PKForge.Engine.Tests/PKForge.Engine.Tests.csproj +dotnet test tests/PKForge.Sync.Tests/PKForge.Sync.Tests.csproj dotnet build src/PKForge.App/PKForge.App.csproj -f net10.0-android ``` @@ -197,6 +202,7 @@ src/PKForge.Domain contracts and DTOs, no engine or Android dependencie src/PKForge.Engine adapters over pinned PKHeX.Core src/PKForge.AutoMod compiles the Auto Legality Mod against our Core src/PKForge.Infrastructure bank, backups, atomic save writer +src/PKForge.Sync Bank sync with a PKForge Sync server (offline-first, conflict-aware) src/PKForge.App MAUI app, SkiaSharp UI, gamepad and second screen src/PKForge.Chrome the design system: tokens and painters, pure Skia tools/ChromePreview renders the design system off-device diff --git a/src/PKForge.App/App.cs b/src/PKForge.App/App.cs index 4c33ef25..ab551407 100644 --- a/src/PKForge.App/App.cs +++ b/src/PKForge.App/App.cs @@ -5,13 +5,24 @@ public sealed class App : Application /// Raised when Android resumes the app (including after the install-permission screen). public static event Action? Resumed; public static event Action? Suspended; + + /// Android can stop the activity and recreate it without an OnResume; see . + private static readonly Domain.AppForeground Foreground = new(); + protected override void OnSleep() { - Suspended?.Invoke(); - Music?.PauseForBackground(); + if (Foreground.EnterBackground()) + { + Suspended?.Invoke(); + Music?.PauseForBackground(); + Sync?.Suspend(); + } base.OnSleep(); } + private static PKForge.Sync.SyncCoordinator? Sync => + IPlatformApplication.Current?.Services.GetService(); + private static Platforms.Android.MusicPlayer? Music => IPlatformApplication.Current?.Services.GetService() as Platforms.Android.MusicPlayer; @@ -20,13 +31,38 @@ protected override void OnStart() { base.OnStart(); Music?.MaybeAutostart(); + // Bank Sync: a first sync now, and another whenever the network comes back. + Connectivity.Current.ConnectivityChanged += (_, args) => + { + if (args.NetworkAccess != NetworkAccess.None) Sync?.NetworkAvailable(); + }; + Sync?.RequestSync(); } protected override void OnResume() { base.OnResume(); + BackInFront(); + } + + /// + /// A recreated activity (a configuration change, or Android reclaiming it while the process + /// lives) only activates its new window and never calls OnResume, which left Bank Sync paused + /// until the app restarted. Activation finishes the return in that case. + /// + private static void OnWindowActivated(object? sender, EventArgs args) + { + if (BackInFront()) Services.AppLog.Info("app", "Back in front without OnResume (the activity was recreated)"); + } + + /// Undoes OnSleep, once per return. False when the app never left. + private static bool BackInFront() + { + if (!Foreground.EnterForeground()) return false; Music?.ResumeFromBackground(); + Sync?.Resume(); Resumed?.Invoke(); + return true; } public App() @@ -36,6 +72,9 @@ public App() // Every page reads its chrome colors when it is built, so the scheme comes first. Services.ColorThemeSetting.ApplyStored(); + // Bank Sync: the living dex start box now lives with the Bank; move it before anything touches boxes. + Services.LivingDexAutopilot.MoveLegacyStartBox(); + // Warm both Skia faces off the ctor: the party view paints before the first // save opens, and its cached nickname font must never pin the placeholder. _ = Views.PixelFont.WarmAsync(); @@ -100,7 +139,9 @@ protected override Window CreateWindow(IActivationState? activationState) { var services = IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("MAUI services are unavailable."); - return new Window(CreateRoot(services)); + var window = new Window(CreateRoot(services)); + window.Activated += OnWindowActivated; + return window; } catch (Exception error) { diff --git a/src/PKForge.App/MauiProgram.cs b/src/PKForge.App/MauiProgram.cs index bd386409..0f602eda 100644 --- a/src/PKForge.App/MauiProgram.cs +++ b/src/PKForge.App/MauiProgram.cs @@ -6,6 +6,7 @@ using PKForge.Domain; using PKForge.Engine; using PKForge.Infrastructure; +using PKForge.Sync; using SkiaSharp.Views.Maui.Controls.Hosting; namespace PKForge.App; @@ -40,9 +41,28 @@ public static MauiApp CreateMauiApp() builder.Services.AddSingleton(); builder.Services.AddSingleton(_ => new FileBackupService(Path.Combine(FileSystem.AppDataDirectory, "backups"))); + var bankRoot = Path.Combine(FileSystem.AppDataDirectory, "bank"); + builder.Services.AddSingleton(_ => new FileBankService(bankRoot)); + builder.Services.AddSingleton(_ => BankBoxDecor.Decor); + builder.Services.AddSingleton(_ => new SyncStateStore(Path.Combine(FileSystem.AppDataDirectory, "sync"))); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(sp => new SyncConnection( + sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService().For)); + builder.Services.AddSingleton(sp => new SyncedBankService( + sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService())); + builder.Services.AddSingleton(_ => new FileBankSnapshots(bankRoot, Path.Combine(FileSystem.AppDataDirectory, "bank-snapshots"))); + builder.Services.AddSingleton(sp => new SyncCoordinator( + sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService(), + sp.GetRequiredService()) + { + Trace = message => Services.AppLog.Info("sync", message), + }); + builder.Services.AddSingleton(); builder.Services.AddSingleton(sp => { - var bank = new FileBankService(Path.Combine(FileSystem.AppDataDirectory, "bank")); + var bank = new BankServiceSwitch( + sp.GetRequiredService(), sp.GetRequiredService(), sp.GetRequiredService()); // One-time index migration, off the UI thread: legacy entries get their exact format // (only when provable) and sprite traits recorded, then the index is marked so no // entry is read again on later launches. Concurrent bank edits are never undone. @@ -103,6 +123,7 @@ public static MauiApp CreateMauiApp() builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); + builder.Services.AddTransient(); App.Trace("builder.Build()"); return builder.Build(); } diff --git a/src/PKForge.App/PKForge.App.csproj b/src/PKForge.App/PKForge.App.csproj index 721f7e9c..0fc76e73 100644 --- a/src/PKForge.App/PKForge.App.csproj +++ b/src/PKForge.App/PKForge.App.csproj @@ -90,5 +90,7 @@ + + diff --git a/src/PKForge.App/Platforms/Android/AndroidManifest.xml b/src/PKForge.App/Platforms/Android/AndroidManifest.xml index d0167391..ed2cd5db 100644 --- a/src/PKForge.App/Platforms/Android/AndroidManifest.xml +++ b/src/PKForge.App/Platforms/Android/AndroidManifest.xml @@ -1,5 +1,14 @@ - + + + + + + + + + diff --git a/src/PKForge.App/Platforms/Android/linker.xml b/src/PKForge.App/Platforms/Android/linker.xml index 677ae6c2..05c48d94 100644 --- a/src/PKForge.App/Platforms/Android/linker.xml +++ b/src/PKForge.App/Platforms/Android/linker.xml @@ -6,4 +6,7 @@ Preserve both engine assemblies completely. --> + + + diff --git a/src/PKForge.App/Services/BankBoxDecor.cs b/src/PKForge.App/Services/BankBoxDecor.cs index eeca70ef..3c1f7b36 100644 --- a/src/PKForge.App/Services/BankBoxDecor.cs +++ b/src/PKForge.App/Services/BankBoxDecor.cs @@ -2,17 +2,17 @@ namespace PKForge.App.Services; -/// The Bank's box names and wallpapers, stored beside its index. +/// +/// The Bank's box names, wallpapers and settings, stored beside its index. One instance for the whole +/// app (sync applies another device's decor through the same objects the pages read). +/// public static class BankBoxDecor { - private static readonly Lazy LazyNames = new(() => new BankBoxNames(Root)); - private static readonly Lazy LazyWallpapers = new(() => new BankBoxWallpapers(Root)); + private static readonly Lazy LazyDecor = new(() => new BankDecor(Path.Combine(FileSystem.AppDataDirectory, "bank"))); - private static string Root => Path.Combine(FileSystem.AppDataDirectory, "bank"); - - public static BankBoxNames Names => LazyNames.Value; - - public static BankBoxWallpapers Wallpapers => LazyWallpapers.Value; + public static BankDecor Decor => LazyDecor.Value; + public static BankBoxNames Names => Decor.Names; + public static BankBoxWallpapers Wallpapers => Decor.Wallpapers; /// The box's name, or "Box 07" when it has none. public static string Label(int box) => Names.Get(box) ?? $"Box {box + 1:00}"; diff --git a/src/PKForge.App/Services/BankRestorePoints.cs b/src/PKForge.App/Services/BankRestorePoints.cs new file mode 100644 index 00000000..47957741 --- /dev/null +++ b/src/PKForge.App/Services/BankRestorePoints.cs @@ -0,0 +1,39 @@ +using PKForge.Domain; +using PKForge.Infrastructure; +using PKForge.Sync; + +namespace PKForge.App.Services; + +/// +/// The Bank's own restore points (copies taken before Bank Sync replaced the whole Bank), listed beside the +/// save restore points. Restoring stops Bank Sync first: otherwise the old Bank would go out as new edits. +/// +public sealed class BankRestorePoints(FileBankSnapshots snapshots, FileBankService bank, BankDecor decor, SyncCoordinator sync) +{ + private const string IdPrefix = "bank:"; + + public static bool IsBank(BackupInfo point) => point.BackupId.StartsWith(IdPrefix, StringComparison.Ordinal); + + public bool SyncIsOn => sync.Connection.IsConnected; + + public IReadOnlyList List() => + [.. snapshots.List().Select(s => new BackupInfo(IdPrefix + s.Id, s.CreatedUtc, "", "PKForge Bank", "Bank", 0, s.SizeBytes, + s.PokemonCount is { } count ? $"{s.Reason} {count} Pokémon." : s.Reason))]; + + /// Puts the Bank back as it was in ; the current Bank becomes a restore point first. + public async Task RestoreAsync(BackupInfo point) + { + if (!IsBank(point)) throw new ArgumentException("Not a Bank restore point.", nameof(point)); + if (sync.Connection.IsConnected) + { + sync.SignOut(); + await sync.WhenIdleAsync(); + } + await Task.Run(() => + { + snapshots.Restore(point.BackupId[IdPrefix.Length..], $"Before restoring the Bank from {point.CreatedUtc:yyyy-MM-dd HH:mm} UTC."); + bank.Reload(); + decor.Reload(); + }); + } +} diff --git a/src/PKForge.App/Services/BankServiceSwitch.cs b/src/PKForge.App/Services/BankServiceSwitch.cs new file mode 100644 index 00000000..79ee3597 --- /dev/null +++ b/src/PKForge.App/Services/BankServiceSwitch.cs @@ -0,0 +1,32 @@ +using PKForge.Domain; +using PKForge.Infrastructure; +using PKForge.Sync; + +namespace PKForge.App.Services; + +/// +/// The Bank the app uses: the plain in Local mode, the +/// over the same files while this device is connected to a sync server. +/// Both wrap one Bank, so switching never moves or copies anything. +/// +public sealed class BankServiceSwitch(FileBankService local, SyncedBankService synced, SyncConnection connection) : IBankService +{ + private IBankService Current => connection.IsConnected ? synced : local; + + public IReadOnlyList GetAll() => Current.GetAll(); + public int BoxCount => Current.BoxCount; + public BankEntry Add(byte[] data, BankEntryInfo info) => Current.Add(data, info); + public IReadOnlyList AddMany(IReadOnlyList deposits) => Current.AddMany(deposits); + public byte[] GetData(Guid id) => Current.GetData(id); + public void Move(Guid id, int box, int slot) => Current.Move(id, box, slot); + public void Remove(Guid id) => Current.Remove(id); + public int RemoveMany(IReadOnlyList ids) => Current.RemoveMany(ids); + public void Replace(Guid id, byte[] data, BankEntryInfo info) => Current.Replace(id, data, info); + public void AddBox() => Current.AddBox(); + public void RemapBoxes(BankBoxRemap remap) => Current.RemapBoxes(remap); + public int UpdateInfo(IReadOnlyList<(Guid Id, BankEntryInfo Info)> updates) => Current.UpdateInfo(updates); + public int MigrationVersion => Current.MigrationVersion; + public int CompleteMigration(int version, IReadOnlyList<(Guid Id, BankEntryInfo Expected, BankEntryInfo Info)> updates) => + Current.CompleteMigration(version, updates); + public int Place(IReadOnlyList<(Guid Id, int Box, int Slot)> placements) => Current.Place(placements); +} diff --git a/src/PKForge.App/Services/LivingDexAutopilot.cs b/src/PKForge.App/Services/LivingDexAutopilot.cs index ba6b337e..9a11e091 100644 --- a/src/PKForge.App/Services/LivingDexAutopilot.cs +++ b/src/PKForge.App/Services/LivingDexAutopilot.cs @@ -48,17 +48,37 @@ public static class LivingDexAutopilot { private const string BankStartKey = "livingdex_bank_start_box"; - /// The Bank's living dex region, once a run created it (kept so later runs fill the same boxes). + /// + /// The Bank's living dex region, once a run created it (kept so later runs fill the same boxes). + /// Stored in the Bank's settings so it follows the Bank to every synced device. + /// public static int? BankStartBox { - get => Preferences.Default.Get(BankStartKey, -1) is var box and >= 0 ? box : null; + get + { + MoveLegacyStartBox(); + return BankBoxDecor.Decor.Settings.LivingDexStartBox; + } set { - if (value is { } box) Preferences.Default.Set(BankStartKey, box); - else Preferences.Default.Remove(BankStartKey); + MoveLegacyStartBox(); + BankBoxDecor.Decor.Settings.LivingDexStartBox = value; } } + /// + /// Older versions kept the start box in device preferences: moved once, value kept. Runs at app start, + /// before anything can rearrange boxes; the new copy is written before the old one is removed. + /// + public static void MoveLegacyStartBox() + { + if (!Preferences.Default.ContainsKey(BankStartKey)) return; + var legacy = Preferences.Default.Get(BankStartKey, -1); + if (legacy >= 0 && BankBoxDecor.Decor.Settings.LivingDexStartBox is null) + BankBoxDecor.Decor.Settings.LivingDexStartBox = legacy; + Preferences.Default.Remove(BankStartKey); + } + public static async Task ReadShelfAsync(IProgress? progress, CancellationToken cancellationToken) { var services = IPlatformApplication.Current?.Services ?? throw new InvalidOperationException("No services."); diff --git a/src/PKForge.App/Services/QrEntityService.cs b/src/PKForge.App/Services/QrEntityService.cs index 90dd18d8..7cf4c7e4 100644 --- a/src/PKForge.App/Services/QrEntityService.cs +++ b/src/PKForge.App/Services/QrEntityService.cs @@ -98,7 +98,11 @@ private static string OriginalTrainerOf(ISaveEngine engine, byte[] entityBytes, } /// Reads one QR's raw bytes out of an image; null when it holds no QR. - private static byte[]? TryDecodeQr(byte[] imageBytes) + private static byte[]? TryDecodeQr(byte[] imageBytes) => + TryDecodeText(imageBytes) is { } text ? Encoding.Latin1.GetBytes(text) : null; + + /// Reads one QR code's text out of an image; null when it holds no QR. + public static string? TryDecodeText(byte[] imageBytes) { try { @@ -121,7 +125,7 @@ private static string OriginalTrainerOf(ISaveEngine engine, byte[] entityBytes, }; var result = reader.Decode(new RGBLuminanceSource( rgba, scaled.Width, scaled.Height, RGBLuminanceSource.BitmapFormat.RGBA32)); - return result is null ? null : Encoding.Latin1.GetBytes(result.Text); + return result?.Text; } catch (Exception) { diff --git a/src/PKForge.App/Services/SyncPlatform.cs b/src/PKForge.App/Services/SyncPlatform.cs new file mode 100644 index 00000000..46322a60 --- /dev/null +++ b/src/PKForge.App/Services/SyncPlatform.cs @@ -0,0 +1,78 @@ +using System.Net.Http.Headers; +using PKForge.Sync; + +namespace PKForge.App.Services; + +/// Where the Bank Sync device key lives: Android's secure storage, read once. +public sealed class SecureSyncSecrets : ISyncSecrets +{ + private const string Key = "pkforge.sync.device-key.v1"; + private readonly Lock _gate = new(); + private bool _loaded; + private string? _value; + + public string? DeviceKey + { + get + { + lock (_gate) + { + if (!_loaded) + { + try { _value = SecureStorage.Default.GetAsync(Key).GetAwaiter().GetResult(); } + catch (Exception error) { AppLog.Warn("sync", $"Secure storage unreadable: {error.GetType().Name}"); _value = null; } + _loaded = true; + } + return _value; + } + } + set + { + lock (_gate) + { + _value = value; + _loaded = true; + if (value is null) SecureStorage.Default.Remove(Key); + else SecureStorage.Default.SetAsync(Key, value).GetAwaiter().GetResult(); + } + } + } +} + +/// One HttpClient per sync server address, reused for the life of the app. +public sealed class SyncHttp +{ + private readonly Lock _gate = new(); + private readonly Dictionary _clients = []; + + public HttpClient For(Uri server) + { + lock (_gate) + { + if (_clients.TryGetValue(server, out var client)) return client; + client = new HttpClient(new SocketsHttpHandler { ConnectTimeout = TimeSpan.FromSeconds(15), PooledConnectionLifetime = TimeSpan.FromMinutes(5) }) + { + BaseAddress = server, + Timeout = TimeSpan.FromSeconds(100), + }; + client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("PKForge", AppInfo.Current.VersionString)); + _clients[server] = client; + return client; + } + } +} + +public static class SyncPlatform +{ + /// How this device appears in the Devices list. + public static string DeviceName + { + get + { + var name = DeviceInfo.Current.Name?.Trim(); + if (!string.IsNullOrEmpty(name)) return name.Length > 100 ? name[..100] : name; + var model = $"{DeviceInfo.Current.Manufacturer} {DeviceInfo.Current.Model}".Trim(); + return model.Length > 0 ? model : "PKForge device"; + } + } +} diff --git a/src/PKForge.App/ViewModels/BackupHistoryViewModel.cs b/src/PKForge.App/ViewModels/BackupHistoryViewModel.cs index 7489437d..e3baa3e6 100644 --- a/src/PKForge.App/ViewModels/BackupHistoryViewModel.cs +++ b/src/PKForge.App/ViewModels/BackupHistoryViewModel.cs @@ -14,10 +14,12 @@ public partial class BackupHistoryViewModel : ObservableObject private readonly BoxBrowserViewModel _boxBrowser; private readonly ISaveEngine _engine; private readonly IBankService _bank; + private readonly BankRestorePoints _bankPoints; public BackupHistoryViewModel(IBackupService backups, ISaveSessionService sessions, ISafeSaveWriter writer, BoxBrowserViewModel boxBrowser, - ISaveEngine engine, IBankService bank) + ISaveEngine engine, IBankService bank, BankRestorePoints bankPoints) { + _bankPoints = bankPoints; _engine = engine; _bank = bank; _backups = backups; @@ -39,9 +41,10 @@ private async Task LoadAsync() Backups.Clear(); // With a save open, only its own restore points are listed: restoring writes into it. var open = _sessions.Current; - foreach (var info in await _backups.ListAsync()) - if (open is null || info.BelongsTo(open.Document.DocumentId, open.Snapshot)) - Backups.Add(info); + var saves = (await _backups.ListAsync()).Where(info => open is null || info.BelongsTo(open.Document.DocumentId, open.Snapshot)); + // The Bank's own restore points are listed whatever save is open: they restore the Bank, not a save. + foreach (var info in saves.Concat(_bankPoints.List()).OrderByDescending(info => info.CreatedUtc)) + Backups.Add(info); Status = Backups.Count == 0 ? "No backups yet. One is created automatically before every write." : CanRestore @@ -71,9 +74,18 @@ private async Task LoadAsync() } } + /// Whether restoring would stop Bank Sync first. + public bool RestoreStopsSync(BackupInfo backup) => BankRestorePoints.IsBank(backup) && _bankPoints.SyncIsOn; + /// Writes the backup's bytes into the open document; the current state is itself backed up first. public async Task RestoreAsync(BackupInfo backup) { + if (BankRestorePoints.IsBank(backup)) + { + await RestoreBankAsync(backup); + return; + } + var session = _sessions.Current; if (session is null) { @@ -115,4 +127,25 @@ public async Task RestoreAsync(BackupInfo backup) IsBusy = false; } } + + private async Task RestoreBankAsync(BackupInfo backup) + { + if (IsBusy) return; + try + { + IsBusy = true; + Status = "Restoring the Bank…"; + await _bankPoints.RestoreAsync(backup); + await LoadAsync(); + Status = "Bank restored. The Bank as it was just before is kept as a restore point."; + } + catch (Exception error) + { + Status = $"Restore aborted: {error.Message}"; + } + finally + { + IsBusy = false; + } + } } diff --git a/src/PKForge.App/Views/BackupHistoryPage.cs b/src/PKForge.App/Views/BackupHistoryPage.cs index 9214743f..c66e93a9 100644 --- a/src/PKForge.App/Views/BackupHistoryPage.cs +++ b/src/PKForge.App/Views/BackupHistoryPage.cs @@ -191,6 +191,16 @@ private async void OnSelected(object? sender, SelectionChangedEventArgs args) private async Task ConfirmRestoreAsync(BackupInfo backup) { + if (BankRestorePoints.IsBank(backup)) + { + var message = $"Put the Bank back as it was at {backup.CreatedUtc:yyyy-MM-dd HH:mm} UTC? The Bank as it is now is kept as a restore point first."; + if (_viewModel.RestoreStopsSync(backup)) + message += " Bank Sync is turned off first, so the old Bank isn't sent to your other devices. Turn it back on to choose whether to keep both Banks or use the server's."; + if (await PadMenu.ConfirmAsync(_hostGrid, "Restore the Bank?", message, "Restore")) + await _viewModel.RestoreAsync(backup); + return; + } + if (!_viewModel.CanRestore) { await PadMenu.ShowAsync(_hostGrid, "Restore", "Connect to a save first - restoring writes into the connected save.", "OK"); diff --git a/src/PKForge.App/Views/BankPage.cs b/src/PKForge.App/Views/BankPage.cs index f31c63ca..bc702f92 100644 --- a/src/PKForge.App/Views/BankPage.cs +++ b/src/PKForge.App/Views/BankPage.cs @@ -139,6 +139,9 @@ Button Tool(string text, Action action) ("-", HasSecondScreen ? "Page" : "Summary", () => OnPadButton(PadButton.Select)), ("+", "Menu", () => _ = OpenCursorMenuAsync())); + // The strip's word follows Bank Sync ("Open" while this Bank is local). + var strip = DsChrome.StatusStrip("Bank", out var flags, SyncFlag(Sync?.Status)); + _syncFlag = flags[0]; var root = new Grid { RowDefinitions = @@ -146,7 +149,7 @@ Button Tool(string text, Action action) new(GridLength.Auto), new(GridLength.Auto), new(GridLength.Star), new(GridLength.Auto), new(GridLength.Auto), ], - Children = { DsChrome.TitleBar(), DsChrome.StatusStrip("Bank", "Open"), bodyHost, statusHost, footer }, + Children = { DsChrome.TitleBar(), strip, bodyHost, statusHost, footer }, }; Grid.SetRow((View)root.Children[1], 1); Grid.SetRow(bodyHost, 2); @@ -172,11 +175,19 @@ protected override void OnAppearing() UpdatePreview(); UpdateStatus(); StartFactRotation(); + if (Sync is { } sync) + { + sync.StatusChanged += OnSyncStatus; + _syncFlag.Text = SyncFlag(sync.Status); + } + if (Synced is { } synced) synced.RemoteChangesApplied += OnRemoteChanges; } protected override void OnDisappearing() { base.OnDisappearing(); + if (Sync is { } sync) sync.StatusChanged -= OnSyncStatus; + if (Synced is { } synced) synced.RemoteChangesApplied -= OnRemoteChanges; _factTimer?.Stop(); IPlatformApplication.Current?.Services.GetService()?.Remove(this); _inspectCancel?.Cancel(); @@ -189,6 +200,34 @@ protected override void OnDisappearing() _ = Task.Run(facts.FlushHeldItemBackfill); } + private Label _syncFlag = null!; + + private static PKForge.Sync.SyncCoordinator? Sync => + IPlatformApplication.Current?.Services.GetService(); + + private static PKForge.Sync.SyncedBankService? Synced => + IPlatformApplication.Current?.Services.GetService(); + + /// The Bank's word in the status strip: "Open" while local, the sync state while connected. + private static string SyncFlag(PKForge.Sync.SyncStatus? status) => status?.Phase switch + { + null or PKForge.Sync.SyncPhase.NotConnected => "Open", + PKForge.Sync.SyncPhase.Syncing => "Syncing", + PKForge.Sync.SyncPhase.Offline => "Offline", + PKForge.Sync.SyncPhase.Idle when status.WritesDisabled => "Read-only", + PKForge.Sync.SyncPhase.Idle when status.Conflicts > 0 => status.Conflicts == 1 ? "1 conflict" : $"{status.Conflicts} conflicts", + PKForge.Sync.SyncPhase.Idle when status.PendingChanges => "Sync pending", + PKForge.Sync.SyncPhase.Idle => "Synced", + PKForge.Sync.SyncPhase.NeedsJoin => "Sync setup", + _ => "Sync paused", + }; + + private void OnSyncStatus(object? sender, PKForge.Sync.SyncStatus status) => + MainThread.BeginInvokeOnMainThread(() => _syncFlag.Text = SyncFlag(status)); + + /// Another device changed the Bank: show it without leaving the page. + private void OnRemoteChanges(object? sender, EventArgs args) => MainThread.BeginInvokeOnMainThread(() => ChangeBox(0)); + private SecondScreenClaim? _secondClaim; private async Task OpenLivingDexAsync() @@ -1115,10 +1154,8 @@ public bool ApplyBoxRemap(BankBoxRemap remap) _boxViewModel.Status = error.Message; return false; } - BankBoxDecor.Names.Remap(remap); - BankBoxDecor.Wallpapers.Remap(remap); - if (LivingDexAutopilot.BankStartBox is { } start) - LivingDexAutopilot.BankStartBox = remap.Map(start); + // Names, wallpapers and the living dex region follow their boxes together. + BankBoxDecor.Decor.Remap(remap); var marks = _marked.Where(m => remap.Map(m.Box) is not null).Select(m => (remap.Map(m.Box)!.Value, m.Slot)).ToList(); _marked.Clear(); _marked.UnionWith(marks); diff --git a/src/PKForge.App/Views/BankSyncPage.cs b/src/PKForge.App/Views/BankSyncPage.cs new file mode 100644 index 00000000..e4c811ef --- /dev/null +++ b/src/PKForge.App/Views/BankSyncPage.cs @@ -0,0 +1,644 @@ +using System.Text.Json; +using PKForge.App.Services; +using PKForge.App.Theme; +using PKForge.Sync; +using PKForge.Sync.Protocol; + +namespace PKForge.App.Views; + +/// +/// Bank Sync: connects this device's Bank to a PKForge Sync server +/// and holds everything about that link: syncing now, conflicts, devices, pairing, signing out. +/// +public sealed class BankSyncPage : ContentPage, IPadHandler +{ + private sealed record SyncAction(string Title, string Detail, string Icon, Func Run); + + private readonly SyncCoordinator _sync; + private readonly Grid _hostGrid; + private readonly Label _status; + private readonly VerticalStackLayout _rows = new() { Spacing = 4 }; + private readonly ScrollView _scroll; + private readonly List<(Border Row, SyncAction Action)> _actions = []; + private int _index; + private bool _busy; + + public BankSyncPage(SyncCoordinator sync) + { + _sync = sync; + Title = "Bank Sync"; + BackgroundColor = UiTokens.Housing; + NavigationPage.SetHasNavigationBar(this, false); + + var back = Kit.MiniCapsule("Back", UiTokens.Ink0); + back.WidthRequest = 72; + back.Clicked += async (_, _) => await Navigation.PopAsync(); + var header = Kit.HeaderBar("Bank Sync"); + header.VerticalOptions = LayoutOptions.Center; + var titleRow = new Grid + { + ColumnSpacing = 10, + ColumnDefinitions = [new(GridLength.Auto), new(GridLength.Star)], + Children = { back, header }, + }; + Grid.SetColumn(header, 1); + + _status = new Label + { + TextColor = UiTokens.InkSoft, + FontFamily = DsChrome.PixelFont, + FontSize = UiTokens.TextBody, + LineBreakMode = LineBreakMode.WordWrap, + }; + _scroll = new ScrollView { Content = _rows }; + var terminal = new Grid + { + RowSpacing = 10, + RowDefinitions = [new(GridLength.Auto), new(GridLength.Star)], + Children = { Kit.Well(_status, padding: 8), _scroll }, + }; + Grid.SetRow(_scroll, 1); + + var panel = Kit.DevicePanel(terminal); + var root = new Grid + { + Padding = new Thickness(14, 10), + RowSpacing = 10, + RowDefinitions = [new(GridLength.Auto), new(GridLength.Star)], + Children = { titleRow, panel }, + }; + Grid.SetRow(panel, 1); + _hostGrid = new Grid { Children = { DsChrome.GridBackground(), root } }; + Content = _hostGrid; + } + + protected override void OnAppearing() + { + base.OnAppearing(); + IPlatformApplication.Current?.Services.GetService()?.Push(this); + _sync.StatusChanged += OnStatus; + Rebuild(); + } + + protected override void OnDisappearing() + { + base.OnDisappearing(); + _sync.StatusChanged -= OnStatus; + IPlatformApplication.Current?.Services.GetService()?.Remove(this); + } + + private void OnStatus(object? sender, SyncStatus status) => MainThread.BeginInvokeOnMainThread(() => + { + if (!_busy) Rebuild(); + }); + + public bool OnPadButton(PadButton button) + { + switch (button) + { + case PadButton.Up: Focus(_index - 1); return true; + case PadButton.Down: Focus(_index + 1); return true; + case PadButton.A: + if (_index >= 0 && _index < _actions.Count) _ = RunAsync(_actions[_index].Action); + return true; + case PadButton.B: _ = Navigation.PopAsync(); return true; + default: return true; // own the pad while this page is up + } + } + + private void Focus(int index) + { + if (_actions.Count == 0) return; + _index = Math.Clamp(index, 0, _actions.Count - 1); + for (var i = 0; i < _actions.Count; i++) Kit.SetRowFocus(_actions[i].Row, i == _index, shaded: true); + ScrollIntoView(_actions[_index].Row); + } + + /// + /// More actions than fit on a handheld: the list rides along with the cursor (same as + /// PadMenu / BoxLayoutEditor) or the cursor walks off the bottom edge. Rebuild makes + /// new rows that have no bounds yet, so those scroll once they are laid out. + /// + private void ScrollIntoView(Border row) + { + if (row.Height > 0) + { + ScrollTo(row); + return; + } + void OnSized(object? sender, EventArgs e) + { + row.SizeChanged -= OnSized; + Dispatcher.Dispatch(() => ScrollTo(row)); + } + row.SizeChanged += OnSized; + } + + /// + /// Only while the row is still the focused one in the list: a status change rebuilds the rows, + /// and scrolling to a removed row throws ("element does not belong to this ScrollView"). + /// + private void ScrollTo(Border row) + { + if (row.Parent != _rows || _index >= _actions.Count || !ReferenceEquals(_actions[_index].Row, row)) return; + _ = _scroll.ScrollToAsync(row, ScrollToPosition.MakeVisible, false); + } + + private void Rebuild() + { + _status.Text = StatusText(_sync.Status); + _rows.Children.Clear(); + _actions.Clear(); + foreach (var action in Actions()) + { + var title = new Label { Text = action.Title, TextColor = UiTokens.Ink0, FontFamily = DsChrome.PixelFont, FontSize = 15 }; + var detail = new Label + { + Text = action.Detail, + TextColor = UiTokens.InkSoft, + FontFamily = DsChrome.PixelFont, + FontSize = UiTokens.TextSmall, + LineBreakMode = LineBreakMode.WordWrap, + }; + var icon = PksmIcons.Icon(action.Icon, 22); + var text = new VerticalStackLayout { Spacing = 2, Children = { title, detail } }; + var content = new Grid + { + ColumnSpacing = 10, + ColumnDefinitions = [new(GridLength.Auto), new(GridLength.Star)], + Children = { icon, text }, + }; + Grid.SetColumn(text, 1); + var row = Kit.Row(content, shaded: true, new Thickness(10, 8)); + var tap = new TapGestureRecognizer(); + var captured = action; + tap.Tapped += (_, _) => _ = RunAsync(captured); + row.GestureRecognizers.Add(tap); + _rows.Children.Add(row); + _actions.Add((row, action)); + } + Focus(_index); + } + + private async Task RunAsync(SyncAction action) + { + if (_busy) return; + _busy = true; + try { await action.Run(); } + catch (Exception error) + { + AppLog.Error("sync", $"{action.Title} failed", error); + await PadMenu.ShowAsync(_hostGrid, action.Title, $"Something went wrong: {error.Message}", "OK"); + } + finally + { + _busy = false; + Rebuild(); + } + } + + private IEnumerable Actions() + { + var status = _sync.Status; + if (status.Phase == SyncPhase.NotConnected) + { + var camera = HasCamera(); + var withCode = new SyncAction("Connect with a code", "Type the server and code another device shows under Add a device.", "key", ConnectWithCodeAsync); + if (!camera) yield return withCode; + yield return camera + ? new("Connect with a QR code", "Photograph the code another device shows under Bank Sync, Add a device.", "qr", ConnectWithQrAsync) + : new("Connect with a QR code picture", "Pick a screenshot or photo of the code another device shows.", "qr", ConnectWithQrAsync); + if (camera) yield return withCode; + yield return new("Connect to a server", "Type the address of a PKForge Sync server.", "link", ConnectToServerAsync); + yield break; + } + if (status.Phase == SyncPhase.SignedOut) + { + if (_sync.Store.Read(s => s.ServerUrl) is { } url) + yield return new("Sign in again", "Connect this device to the same server again. Its Bank is merged back in.", "key", () => ConnectToAsync(new Uri(url))); + yield return new("Stop syncing on this device", "Sign out. This device keeps its Bank as it is now.", "unlink", SignOutAsync); + yield break; + } + if (status.Phase == SyncPhase.NeedsJoin) + { + yield return new("Finish setting up", "Bring this device's Bank and the server's Bank together.", "confirm", JoinAsync); + } + else + { + yield return new("Sync now", "Send this device's changes and get the others'.", "refresh", SyncNowAsync); + if (status.Conflicts > 0) + yield return new($"Conflicts ({status.Conflicts})", "Changes made on two devices that need your choice.", "warning", ConflictsAsync); + yield return new("Add a device", "Show a code and QR code to connect another device to this Bank.", "qr", AddDeviceAsync); + yield return new("Devices", "See or remove the devices that sync this Bank.", "profile", DevicesAsync); + yield return new("Activity", "What sync did on its own, such as moving a Pokémon to a free slot.", "history", ActivityAsync); + if (status.Phase == SyncPhase.EmailNotVerified) + yield return new("Resend confirmation email", "Syncing starts once you open the link in it.", "send", ResendConfirmationAsync); + yield return new("Change password", "For the account this Bank belongs to.", "padlock", ChangePasswordAsync); + } + yield return new("Stop syncing on this device", "Sign out. This device keeps its Bank as it is now.", "unlink", SignOutAsync); + } + + private string StatusText(SyncStatus status) + { + if (status.Phase == SyncPhase.NotConnected) + return "Local: this Bank lives on this device only. Connect to a PKForge Sync server to keep one Bank on all your devices."; + var (url, name) = _sync.Store.Read(s => (s.ServerUrl, s.ServerName)); + var host = url is not null ? new Uri(url).Authority : "the server"; + var state = status.Phase switch + { + SyncPhase.NeedsJoin => "Connected. Finish setting up to start syncing.", + SyncPhase.Syncing => "Syncing…", + SyncPhase.Idle when status.PendingChanges => "Some changes are still waiting to sync.", + SyncPhase.Idle => "Up to date.", + _ => status.Message ?? "Sync is paused.", + }; + var server = name is null ? host : $"{name} ({host})"; + var lines = new List { status.Phase == SyncPhase.SignedOut ? $"Server: {server}" : $"Connected to {server}", state }; + if (status.LastSynced is { } last) lines.Add($"Last synced: {last.ToLocalTime():yyyy-MM-dd HH:mm}"); + if (status.Conflicts > 0) lines.Add(status.Conflicts == 1 ? "1 change needs your choice." : $"{status.Conflicts} changes need your choice."); + if (status.WritesDisabledMessage is { } reason) lines.Add(WritesDisabledText(reason)); + return string.Join("\n", lines); + } + + // ---- Connecting ---- + + private async Task ConnectToServerAsync() + { + var typed = await TextPopup.ShowLineAsync(_hostGrid, "Server address", "192.168.1.10:8080 or https://sync.example.com", keyboard: Keyboard.Url); + if (string.IsNullOrWhiteSpace(typed)) return; + if (!ServerAddress.TryParse(typed, out var server, out var error)) + { + await PadMenu.ShowAsync(_hostGrid, "Server address", error, "OK"); + return; + } + await ConnectToAsync(server); + } + + private async Task ConnectToAsync(Uri server) + { + var info = await WithLoadingAsync("Connecting", server.Authority, ct => _sync.Connection.ProbeAsync(server, ct)); + if (info is null) return; + var options = new List { new("Sign in", IconPath: "key", Detail: "Use the account you already have on this server.") }; + if (info.RegistrationOpen) options.Add(new("Create an account", IconPath: "profile", Detail: "Start a new Bank account on this server.")); + options.Add(new("Enter a code", IconPath: "qr", Detail: "The code another of your devices shows under Add a device.")); + var choice = await PadMenu.ShowAsync(_hostGrid, server.Authority, ServerBlurb(info), [.. options]); + switch (choice) + { + case "Sign in": await SignInAsync(server, create: false); break; + case "Create an account": await SignInAsync(server, create: true); break; + case "Enter a code": await PairWithTypedCodeAsync(server); break; + } + } + + /// How a refused push reads: the server's own reason, then what it means for this device. + private static string WritesDisabledText(string reason) => + $"This server isn't accepting changes for your account: {reason.Trim().TrimEnd('.')}. Your Bank stays on this device."; + + private static string ServerBlurb(ServerInfo info) => + (string.IsNullOrWhiteSpace(info.Name) ? "A PKForge Sync server." : $"{info.Name.Trim()}: a PKForge Sync server.") + + (info.EmailEnabled ? "" : " It can't send email, so a forgotten password is reset by whoever runs it."); + + private async Task SignInAsync(Uri server, bool create) + { + var title = create ? "Create an account" : "Sign in"; + var email = await TextPopup.ShowLineAsync(_hostGrid, title, "Email address", keyboard: Keyboard.Email); + if (string.IsNullOrWhiteSpace(email)) return; + var password = await TextPopup.ShowLineAsync(_hostGrid, "Password", create ? "At least 8 characters" : "Password", password: true); + if (string.IsNullOrEmpty(password)) return; + if (create) + { + var again = await TextPopup.ShowLineAsync(_hostGrid, "Password again", "The same password", password: true); + if (again != password) + { + await PadMenu.ShowAsync(_hostGrid, title, "The passwords don't match.", "OK"); + return; + } + } + var done = await WithLoadingAsync(create ? "Creating your account" : "Signing in", server.Authority, async ct => + { + if (create) await _sync.Connection.SignUpAsync(server, email, password, SyncPlatform.DeviceName, ct); + else await _sync.Connection.SignInAsync(server, email, password, SyncPlatform.DeviceName, ct); + return true; + }); + if (done) await AfterConnectingAsync(); + } + + private static bool HasCamera() + { + try { return MediaPicker.Default.IsCaptureSupported; } + catch (Exception error) when (error is FeatureNotSupportedException or NotImplementedException) { return false; } + } + + private async Task ConnectWithQrAsync() + { + byte[]? image = null; + try + { + var photo = HasCamera() + ? await MediaPicker.Default.CapturePhotoAsync(new MediaPickerOptions { Title = "Photograph the pairing QR code" }) + : await FilePicker.Default.PickAsync(new PickOptions { PickerTitle = "Pick a photo of the pairing QR code", FileTypes = FilePickerFileType.Images }); + if (photo is null) return; + await using var stream = await photo.OpenReadAsync(); + using var buffer = new MemoryStream(); + await stream.CopyToAsync(buffer); + image = buffer.ToArray(); + } + catch (Exception error) when (error is PermissionException or FeatureNotSupportedException) + { + await PadMenu.ShowAsync(_hostGrid, "QR code", "The camera isn't available. Use Connect with a code and type it instead.", "OK"); + return; + } + var text = await Task.Run(() => QrEntityService.TryDecodeText(image)); + if (!PairingLink.TryParse(text, out var server, out var code)) + { + await PadMenu.ShowAsync(_hostGrid, "QR code", "No PKForge Sync code found in that picture. Try again closer, or type the code.", "OK"); + return; + } + await ConfirmAndPairAsync(server, code); + } + + /// A code names its server: say which one before this Bank is shared with it. + private async Task ConfirmAndPairAsync(Uri server, string code) + { + if (!await PadMenu.ConfirmAsync(_hostGrid, $"Connect to {server.Authority}?", + $"This device's Bank will sync with the Bank at {server.Authority}. Only continue if the code came from one of your own devices.", "Connect")) + return; + await PairAsync(server, code); + } + + /// For devices without a camera: type the line another device shows under Add a device. + private async Task ConnectWithCodeAsync() + { + const string Title = "Server and code"; + var typed = ""; + Uri? server; + string code; + while (true) + { + var entry = await TextPopup.ShowLineAsync(_hostGrid, Title, "e.g. sync.example.com K7QM-4XTP", typed, keyboard: Keyboard.Url); + if (string.IsNullOrWhiteSpace(entry)) return; + typed = entry.Trim(); + if (PairingEntry.TryParse(typed, out server, out code, out var error)) break; + if (await PadMenu.ShowAsync(_hostGrid, Title, error, "Try again", "Cancel") != "Try again") return; + } + if (server is null) + { + var remembered = _sync.Store.Read(s => s.ServerUrl) is { } url && Uri.TryCreate(url, UriKind.Absolute, out var last) + ? PairingEntry.FormatServer(last) + : ""; + while (server is null) + { + var address = await TextPopup.ShowLineAsync(_hostGrid, "Server address", "The server your other device uses, e.g. sync.example.com", remembered, keyboard: Keyboard.Url); + if (string.IsNullOrWhiteSpace(address)) return; + remembered = address.Trim(); + if (ServerAddress.TryParse(remembered, out var parsed, out var error)) server = parsed; + else if (await PadMenu.ShowAsync(_hostGrid, "Server address", error, "Try again", "Cancel") != "Try again") return; + } + } + await ConfirmAndPairAsync(server, code); + } + + private async Task PairWithTypedCodeAsync(Uri server) + { + var code = await TextPopup.ShowLineAsync(_hostGrid, "Code from your other device", "8 letters and digits"); + if (string.IsNullOrWhiteSpace(code)) return; + await PairAsync(server, code); + } + + private async Task PairAsync(Uri server, string code) + { + var done = await WithLoadingAsync("Adding this device", server.Authority, async ct => + { + await _sync.Connection.PairAsync(server, code, SyncPlatform.DeviceName, ct); + return true; + }); + if (done) await AfterConnectingAsync(); + } + + private async Task AfterConnectingAsync() + { + _sync.Refresh(); + await JoinAsync(); + } + + private async Task JoinAsync() + { + var serverHas = await WithLoadingAsync("Looking at the server's Bank", "", async ct => await _sync.ServerHasPokemonAsync(ct)); + if (serverHas is null) return; + var mode = JoinMode.Merge; + if (serverHas == true && _sync.LocalPokemonCount > 0) + { + const string Both = "Keep both"; + const string Server = "Use the server's Bank"; + var choice = await PadMenu.ShowAsync(_hostGrid, "Two Banks", + $"This device has {_sync.LocalPokemonCount} Pokémon, and the server's Bank has Pokémon too.", + new PadOption(Both, IconPath: "confirm", Detail: "Every Pokémon from both Banks ends up in the one Bank. Nothing is lost."), + new PadOption(Server, IconPath: "download", Detail: "This device's Bank is replaced by the server's. A copy of it is kept first.")); + if (choice is null) return; + mode = choice == Server ? JoinMode.UseServer : JoinMode.Merge; + } + var report = await WithLoadingAsync("Syncing", "The first sync of a big Bank can take a while.", ct => _sync.JoinAsync(mode, ct)); + if (report is not null) await ReportAsync("Bank Sync is on", report); + else if (_sync.Status.Message is { } why) await PadMenu.ShowAsync(_hostGrid, "Not set up yet", $"{why}\nUse Finish setting up to try again.", "OK"); + } + + // ---- While connected ---- + + private async Task SyncNowAsync() + { + var report = await WithLoadingAsync("Syncing", "", ct => _sync.SyncNowAsync(ct)); + if (report is not null && (report.NewConflicts > 0 || report.Relocated > 0)) await ReportAsync("Synced", report); + } + + private async Task ReportAsync(string title, SyncReport report) + { + var lines = new List { $"Sent {report.Uploaded} and received {report.Downloaded} changes." }; + if (report.Relocated > 0) lines.Add($"{report.Relocated} Pokémon moved to free slots because another device used theirs (see Activity)."); + if (report.NewConflicts > 0) lines.Add($"{report.NewConflicts} changes need your choice: open Conflicts."); + if (report.WritesDisabled) lines.Add(WritesDisabledText(report.WritesDisabledMessage ?? "it refused them")); + await PadMenu.ShowAsync(_hostGrid, title, string.Join("\n", lines), "OK"); + } + + private async Task ConflictsAsync() + { + while (_sync.Store.Read(s => s.Conflicts.ToList()) is { Count: > 0 } conflicts) + { + var items = conflicts.Select((c, i) => new PickItem(i, Headline(c), Detail: Kind(c.Kind))).ToList(); + var picked = await PickerMenu.ShowAsync(_hostGrid, "Conflicts", items); + if (picked is null) return; + await ResolveAsync(conflicts[picked.Id]); + } + } + + private async Task ResolveAsync(SyncConflict conflict) + { + var (message, options) = conflict.Kind switch + { + ConflictKind.BothEdited => ( + $"Changed on this device: {Describe(conflict.Local)}.\nChanged on another device: {Describe(conflict.Server.Value)}.", + new[] { ("Keep both", ConflictChoice.KeepBoth), ("Keep this device's", ConflictChoice.KeepThisDevice), ("Keep the other device's", ConflictChoice.KeepOtherDevice) }), + ConflictKind.EditedHereReleasedThere => ( + $"You changed {Describe(conflict.Local)} here, but it was released on another device.", + new[] { ("Keep the edited copy", ConflictChoice.KeepThisDevice), ("Let it go", ConflictChoice.KeepOtherDevice) }), + ConflictKind.ReleasedHereEditedThere => ( + $"You released it here, but it was changed on another device: {Describe(conflict.Server.Value)}.", + new[] { ("Bring it back", ConflictChoice.KeepOtherDevice), ("Let it go", ConflictChoice.KeepThisDevice) }), + ConflictKind.Boxes => ( + "The box layout (names, wallpapers or order) changed on two devices. The Pokémon stay safe either way.", + new[] { ("Keep this device's layout", ConflictChoice.KeepThisDevice), ("Use the other device's layout", ConflictChoice.KeepOtherDevice) }), + _ => ( + "Bank settings (such as where the living dex starts) changed on two devices.", + new[] { ("Keep this device's", ConflictChoice.KeepThisDevice), ("Use the other device's", ConflictChoice.KeepOtherDevice) }), + }; + var picked = await PadMenu.ShowAsync(_hostGrid, Headline(conflict), message, options.Select(o => o.Item1).ToArray()); + if (picked is null) return; + var choice = options.First(o => o.Item1 == picked).Item2; + await WithLoadingAsync("Saving your choice", "", async ct => + { + await _sync.ResolveAsync(conflict.Id, choice, ct); + return true; + }); + } + + private static string Headline(SyncConflict conflict) => conflict.Kind switch + { + ConflictKind.Boxes => "Box layout", + ConflictKind.Settings => "Bank settings", + _ => Name(conflict.Local) ?? Name(conflict.Server.Value) ?? "A Pokémon", + }; + + private static string Kind(ConflictKind kind) => kind switch + { + ConflictKind.BothEdited => "Changed on two devices", + ConflictKind.EditedHereReleasedThere => "Changed here, released elsewhere", + ConflictKind.ReleasedHereEditedThere => "Released here, changed elsewhere", + ConflictKind.Boxes => "Box names, wallpapers or order", + _ => "Bank settings", + }; + + private static string? Name(JsonElement? value) + { + if (value is not { } mon) return null; + try { return SyncValues.ToEntry(Guid.Empty, SyncValues.ReadMon(mon)).Info.Nickname; } + catch (Exception error) when (error is JsonException or InvalidDataException) { return null; } + } + + private static string Describe(JsonElement? value) + { + if (value is not { } mon) return "released"; + try + { + var read = SyncValues.ReadMon(mon); + var info = SyncValues.ToEntry(Guid.Empty, read).Info; + return $"{info.Nickname}, Lv. {info.Level}, box {read.Box + 1} slot {read.Slot + 1}"; + } + catch (Exception error) when (error is JsonException or InvalidDataException) { return "a Pokémon"; } + } + + private async Task AddDeviceAsync() + { + var code = await WithLoadingAsync("Making a code", "", ct => _sync.Connection.CreateClient().CreatePairingCodeAsync(ct)); + if (code is null) return; + // The same server and code as a line to type on a device without a camera (Connect with a code). + var server = PairingLink.TryParse(code.QrPayload, out var linked, out _) ? linked + : _sync.Store.Read(s => s.ServerUrl) is { } url ? new Uri(url) : null; + var typeable = server is null ? null : $"No camera? Use Connect with a code and type:\n{PairingEntry.Format(server, code.Code)}"; + await QrPopup.ShowAsync(_hostGrid, $"Code {code.Code[..4]}-{code.Code[4..]} · 5 minutes", code.QrPayload, typeable); + } + + private async Task DevicesAsync() + { + var devices = await WithLoadingAsync("Getting devices", "", async ct => await _sync.Connection.CreateClient().GetDevicesAsync(ct)); + if (devices is null) return; + var items = devices.Select((d, i) => new PickItem(i, d.IsCurrent ? $"{d.Name} (this device)" : d.Name, + Detail: d.LastSyncUtc is { } at ? $"Last synced {at.ToLocalTime():yyyy-MM-dd HH:mm}" : "Not synced yet")).ToList(); + var picked = await PickerMenu.ShowAsync(_hostGrid, "Devices", items); + if (picked is null) return; + var device = devices[picked.Id]; + if (device.IsCurrent) + { + await PadMenu.ShowAsync(_hostGrid, device.Name, "This is this device. To sign it out, use Stop syncing on this device.", "OK"); + return; + } + if (!await PadMenu.ConfirmAsync(_hostGrid, $"Remove {device.Name}?", "It stops syncing at once and keeps its own copy of the Bank. It can be added again later.", "Remove")) + return; + await WithLoadingAsync("Removing", device.Name, async ct => + { + await _sync.Connection.CreateClient().RemoveDeviceAsync(device.Id, ct); + return true; + }); + } + + private async Task ActivityAsync() + { + var entries = _sync.Store.Read(s => s.Activity.AsEnumerable().Reverse().Take(50).ToList()); + if (entries.Count == 0) + { + await PadMenu.ShowAsync(_hostGrid, "Activity", "Nothing yet. Sync notes here anything it did on its own.", "OK"); + return; + } + await PickerMenu.ShowAsync(_hostGrid, "Activity", + entries.Select((e, i) => new PickItem(i, e.Message, Detail: e.Utc.ToLocalTime().ToString("yyyy-MM-dd HH:mm"))).ToList()); + } + + private async Task ResendConfirmationAsync() + { + if (await WithLoadingAsync("Sending", "", async ct => { await _sync.Connection.CreateClient().ResendVerificationAsync(ct); return true; })) + await PadMenu.ShowAsync(_hostGrid, "Confirmation email", "Sent. Open the link in it, then sync again.", "OK"); + } + + private async Task ChangePasswordAsync() + { + var current = await TextPopup.ShowLineAsync(_hostGrid, "Current password", "Password", password: true); + if (string.IsNullOrEmpty(current)) return; + var next = await TextPopup.ShowLineAsync(_hostGrid, "New password", "At least 8 characters", password: true); + if (string.IsNullOrEmpty(next)) return; + const string SignOut = "Sign them out"; + var others = await PadMenu.ShowAsync(_hostGrid, "Other devices", "Keep your other devices signed in?", "Keep them signed in", SignOut); + if (others is null) return; + if (await WithLoadingAsync("Changing password", "", async ct => + { + await _sync.Connection.CreateClient().ChangePasswordAsync(new ChangePasswordRequest(current, next, others == SignOut), ct); + return true; + })) + await PadMenu.ShowAsync(_hostGrid, "Password", "Changed.", "OK"); + } + + private async Task SignOutAsync() + { + if (!await PadMenu.ConfirmAsync(_hostGrid, "Stop syncing on this device?", + "This device keeps its Bank exactly as it is now. Changes made elsewhere no longer arrive here, and changes made here stay here.", + "Stop syncing")) + return; + _sync.SignOut(); + } + + /// Runs server work behind a loading window; explains failures and returns default for them. + private async Task WithLoadingAsync(string title, string subtitle, Func> work) + { + var loading = LoadingOverlay.Show(_hostGrid, title, subtitle); + string? problem = null; + try + { + return await Task.Run(() => work(loading.Cancellation.Token)); + } + catch (SyncApiException error) + { + problem = error.Message; + } + catch (OperationCanceledException) when (loading.Cancellation.IsCancellationRequested) + { + return default; + } + catch (Exception error) when (error is HttpRequestException or TaskCanceledException or IOException) + { + problem = "The server can't be reached. Check the address, and that this device is on the same network as the server."; + } + finally + { + loading.Close(); + } + await PadMenu.ShowAsync(_hostGrid, title, problem, "OK"); + return default; + } +} diff --git a/src/PKForge.App/Views/DsKit.cs b/src/PKForge.App/Views/DsKit.cs index 7e2b8c9a..63ac775d 100644 --- a/src/PKForge.App/Views/DsKit.cs +++ b/src/PKForge.App/Views/DsKit.cs @@ -416,15 +416,22 @@ public static View TitleBar(string title = "PKForge") } /// Dark status strip: a title on the left, status words + a battery on the right. - public static View StatusStrip(string left, params string[] flags) + public static View StatusStrip(string left, params string[] flags) => StatusStrip(left, out _, flags); + + /// The same strip, handing back its flag labels so a page can keep them current. + public static View StatusStrip(string left, out IReadOnlyList