The first original of an upload now creates the server sequence through its session key (the archive ingest files API-created empty sequences nowhere), and settings sent after an upload are retried when the server's own analysis edits the new sequence meanwhile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
286 lines
14 KiB
C#
286 lines
14 KiB
C#
using Titano.Core.Model;
|
|
using Titano.Data.Sync;
|
|
using Titano.Protocol;
|
|
using Titano.Protocol.Client;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Data.Tests.Sync;
|
|
|
|
/// <summary>Comparison, settings exchange and conflict resolution against an in-memory server.</summary>
|
|
[Category("sync")]
|
|
public sealed class SyncServiceTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public async Task TheTableListsLocalAndServerSequences()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
await f.LocalAsync("Comet", f.NewPhotos(2));
|
|
f.Remote.Add("Construction cam", f.NewPhotos(3));
|
|
|
|
var snapshot = await f.RefreshAsync();
|
|
|
|
Assert.Equal(2, snapshot.Entries.Count);
|
|
Assert.Equal(1, snapshot.ServerSequences);
|
|
Assert.Equal(SyncStatus.LocalOnly, (await f.EntryAsync("Comet")).Status);
|
|
var remote = await f.EntryAsync("Construction cam");
|
|
Assert.Equal(SyncStatus.RemoteOnly, remote.Status);
|
|
Assert.Null(remote.Local);
|
|
Assert.Equal(3, remote.Link!.FrameCount);
|
|
}
|
|
|
|
[Test]
|
|
public async Task LinkedCopiesWithTheSameOriginalsAreInSyncAndTheLibraryKnows()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var photos = f.NewPhotos(3);
|
|
var local = await f.LocalAsync("Harbour", photos);
|
|
var remote = f.Remote.Add("Harbour", photos);
|
|
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
|
|
|
await f.RefreshAsync();
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
|
Assert.Equal(SyncState.InSync, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SyncState);
|
|
|
|
// Nothing moved: the manifest is not fetched again.
|
|
var manifests = f.Remote.ManifestCalls;
|
|
await f.RefreshAsync();
|
|
Assert.Equal(manifests, f.Remote.ManifestCalls);
|
|
}
|
|
|
|
[Test]
|
|
public async Task OriginalsMissingOnEitherSideAreCountedFromTheManifest()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var photos = f.NewPhotos(4);
|
|
var local = await f.LocalAsync("Harbour", photos[..3]);
|
|
var remote = f.Remote.Add("Harbour", photos[..2]);
|
|
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
|
|
|
await f.RefreshAsync();
|
|
var ahead = await f.EntryAsync("Harbour");
|
|
Assert.Equal(SyncStatus.LocalAhead, ahead.Status);
|
|
Assert.Equal(1, ahead.Link!.MissingRemote);
|
|
Assert.Equal(SyncState.LocalAhead, ahead.Local!.SyncState);
|
|
|
|
remote.AddFrame(photos[2]);
|
|
remote.AddFrame(photos[3]);
|
|
await f.RefreshAsync();
|
|
var behind = await f.EntryAsync("Harbour");
|
|
Assert.Equal(SyncStatus.ServerAhead, behind.Status);
|
|
Assert.Equal((0, 1), (behind.Link!.MissingRemote, behind.Link.MissingLocal));
|
|
}
|
|
|
|
[Test]
|
|
public async Task SettingsChangedOnOneSideAreSentOrTaken()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var photos = f.NewPhotos(2);
|
|
var local = await f.LocalAsync("Harbour", photos);
|
|
var remote = f.Remote.Add("Harbour", photos);
|
|
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
|
await f.RefreshAsync();
|
|
|
|
await f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41);
|
|
await f.RefreshAsync();
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, remote.SettingsJson), "this PC's edit reached the server");
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
|
|
|
f.Remote.EditSettings(remote.Id, SyncFixture.Window33);
|
|
await f.RefreshAsync();
|
|
var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, taken.SettingsJson), "the server's edit reached this PC");
|
|
var project = await File.ReadAllTextAsync(f.Archive.Paths.Sequence(taken.Folder).ProjectPath, f.Ct);
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, project), "project.json follows the index");
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
|
}
|
|
|
|
[Test]
|
|
public async Task WithoutAutomaticSyncAnEditWaitsForTheUser()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var photos = f.NewPhotos(1);
|
|
var local = await f.LocalAsync("Harbour", photos);
|
|
var remote = f.Remote.Add("Harbour", photos);
|
|
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
|
await f.RefreshAsync();
|
|
|
|
await f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41);
|
|
await f.RefreshAsync(new SyncPolicy(AutoSyncSettings: false));
|
|
Assert.Equal(SyncStatus.LocalAhead, (await f.EntryAsync("Harbour")).Status);
|
|
Assert.Null(remote.SettingsJson);
|
|
|
|
await f.Sync.SyncSettingsAsync(f.Remote, remote.Id, f.Ct);
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, remote.SettingsJson));
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
|
}
|
|
|
|
[Test]
|
|
public async Task OnlyMetadataChangedOnBothSidesIsMergedWithoutAConflict()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var photos = f.NewPhotos(1);
|
|
var local = await f.LocalAsync("Harbour", photos);
|
|
var remote = f.Remote.Add("Harbour", photos);
|
|
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
|
await f.RefreshAsync();
|
|
|
|
// The server is edited first, so this PC's later title and rating win; tags are united.
|
|
await f.Remote.UpdateSequenceAsync(remote.Id, new UpdateSequenceRequest(null, ["boats"], 2, null, null, null), f.Ct);
|
|
await Task.Delay(20, f.Ct);
|
|
await f.Archive.Service.SetSequenceInfoAsync(local.Id, "Harbour", ["sunset"], 4, null, f.Ct);
|
|
await f.RefreshAsync();
|
|
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
|
|
var merged = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
|
|
Assert.True(merged.Tags.ToHashSet().SetEquals(["sunset", "boats"]), string.Join(",", merged.Tags));
|
|
Assert.True(remote.Tags.ToHashSet().SetEquals(["sunset", "boats"]), string.Join(",", remote.Tags));
|
|
Assert.Equal(4, remote.Rating);
|
|
}
|
|
|
|
[Test]
|
|
public async Task SettingsChangedOnBothSidesAreAConflictUntilTheUserPicks()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var (local, remote) = await ConflictAsync(f);
|
|
|
|
var entry = await f.EntryAsync("Holy grail");
|
|
Assert.Equal(SyncStatus.Conflict, entry.Status);
|
|
Assert.Equal(SyncState.Conflict, entry.Local!.SyncState);
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, entry.Link!.ConflictSettingsJson), "the server's side is kept for display");
|
|
Assert.Equal("exposure.deflickerWindow.manual", SyncRules.Differences(entry.Local.SettingsJson, entry.Link.ConflictSettingsJson)[0].Path);
|
|
|
|
// Neither side is overwritten while the conflict stands, and the comparison does not fetch it again.
|
|
var manifests = f.Remote.ManifestCalls;
|
|
await f.RefreshAsync();
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, remote.SettingsJson));
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson));
|
|
Assert.Equal(manifests, f.Remote.ManifestCalls);
|
|
}
|
|
|
|
[Test]
|
|
public async Task KeepMineSendsThisPcsSettings()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var (local, remote) = await ConflictAsync(f);
|
|
|
|
await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.KeepMine, f.Ct);
|
|
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, remote.SettingsJson));
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson));
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
|
|
await f.RefreshAsync();
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TakeServerReplacesThisPcsSettings()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var (local, remote) = await ConflictAsync(f);
|
|
|
|
await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct);
|
|
|
|
var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, taken.SettingsJson));
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, await File.ReadAllTextAsync(f.Archive.Paths.Sequence(taken.Folder).ProjectPath, f.Ct)));
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, remote.SettingsJson));
|
|
Assert.Empty(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.local-*.json"));
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
|
|
}
|
|
|
|
[Test]
|
|
public async Task SaveBothKeepsThisPcsSettingsNextToTheServers()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var (local, remote) = await ConflictAsync(f);
|
|
|
|
await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct);
|
|
|
|
var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, taken.SettingsJson));
|
|
var copy = Assert.Contains(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.local-*.json"), _ => true);
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, await File.ReadAllTextAsync(copy, f.Ct)), "this PC's version is kept");
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
|
|
}
|
|
|
|
[Test]
|
|
public async Task KeepMineRefusesWhenTheServerChangedAgainMeanwhile()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var (_, remote) = await ConflictAsync(f);
|
|
var racing = new RacingRemote(f.Remote, () => f.Remote.EditSettings(remote.Id, SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal)));
|
|
|
|
var error = await Assert.ThrowsAsync<TitanoApiException>(() => f.Sync.ResolveAsync(racing, remote.Id, SyncResolution.KeepMine, f.Ct));
|
|
Assert.Equal(System.Net.HttpStatusCode.Conflict, error.Status);
|
|
Assert.True(SyncRules.SameSettings(SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal), remote.SettingsJson), "nothing overwritten");
|
|
}
|
|
|
|
[Test]
|
|
public async Task SequencesDeletedOnTheServerAreForgotten()
|
|
{
|
|
await using var f = await SyncFixture.CreateAsync(context);
|
|
var photos = f.NewPhotos(1);
|
|
var local = await f.LocalAsync("Harbour", photos);
|
|
var remote = f.Remote.Add("Harbour", photos);
|
|
f.Remote.Add("Gone", f.NewPhotos(1));
|
|
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
|
await f.RefreshAsync();
|
|
|
|
f.Remote.Sequences.Clear();
|
|
var snapshot = await f.RefreshAsync();
|
|
|
|
Assert.Equal(0, snapshot.ServerSequences);
|
|
Assert.Equal(SyncStatus.LocalOnly, (await f.EntryAsync("Harbour")).Status);
|
|
Assert.Equal(SyncState.LocalOnly, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SyncState);
|
|
}
|
|
|
|
/// <summary>A linked sequence whose settings changed differently on both sides since they last agreed (41 here, 33 on the server).</summary>
|
|
private static async Task<(Sequence Local, FakeSequence Remote)> ConflictAsync(SyncFixture f)
|
|
{
|
|
var photos = f.NewPhotos(2);
|
|
var local = await f.LocalAsync("Holy grail", photos);
|
|
var remote = f.Remote.Add("Holy grail", photos);
|
|
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
|
await f.RefreshAsync();
|
|
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Holy grail")).Status);
|
|
|
|
local = await f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41);
|
|
f.Remote.EditSettings(remote.Id, SyncFixture.Window33);
|
|
await f.RefreshAsync();
|
|
return (local, remote);
|
|
}
|
|
|
|
/// <summary>A remote where someone else edits the settings right after the first read of a sequence.</summary>
|
|
private sealed class RacingRemote(FakeSyncRemote inner, Action race) : ISyncRemote
|
|
{
|
|
private Action? _race = race;
|
|
|
|
public string Server => inner.Server;
|
|
|
|
public async Task<SequenceDto> GetSequenceAsync(Guid id, CancellationToken cancellationToken)
|
|
{
|
|
var dto = await inner.GetSequenceAsync(id, cancellationToken);
|
|
Interlocked.Exchange(ref _race, null)?.Invoke();
|
|
return dto;
|
|
}
|
|
|
|
public Task<IReadOnlyList<SequenceDto>> ListSequencesAsync(CancellationToken cancellationToken) => inner.ListSequencesAsync(cancellationToken);
|
|
public Task<SequenceManifest> GetManifestAsync(Guid id, CancellationToken cancellationToken) => inner.GetManifestAsync(id, cancellationToken);
|
|
public Task<IReadOnlyList<FrameDto>> ListFramesAsync(Guid sequenceId, CancellationToken cancellationToken) => inner.ListFramesAsync(sequenceId, cancellationToken);
|
|
public Task<SequenceDto> UpdateSequenceAsync(Guid id, UpdateSequenceRequest request, CancellationToken cancellationToken) => inner.UpdateSequenceAsync(id, request, cancellationToken);
|
|
|
|
public Task<CompleteUploadResponse?> UploadFileAsync(string path, Guid? sequenceId, string? sessionKey, string sha256, DateTime? capturedAt,
|
|
CancellationToken cancellationToken) =>
|
|
inner.UploadFileAsync(path, sequenceId, sessionKey, sha256, capturedAt, cancellationToken);
|
|
|
|
public Task DownloadFrameAsync(Guid frameId, string kind, Stream destination, CancellationToken cancellationToken) =>
|
|
inner.DownloadFrameAsync(frameId, kind, destination, cancellationToken);
|
|
|
|
public IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken) => inner.EventsAsync(cancellationToken);
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
}
|
|
}
|