Files
Chronos/tests/Chronos.Data.Tests/Sync/SyncServiceTests.cs
T
Alby96 c3756d03c9 fix: keep render filing retryable and writers out of a folder another index moved
Third fix round of owner review 2, phase 4a (Reorganize), cross-cutting items.

- Server render filing now runs under the sequence lease, so it can meet a busy
  sequence or an offline archive. Both completions (client render uploads and
  render-node results) used to answer 500 and leave the upload stuck in
  Committing until a restart. They now answer 503 sequence_busy or
  archive_offline, which clients retry. The upload goes back to receiving with
  its part file and the node keeps its job (UploadService.ReceiveAgainAsync).
- Save both: when this PC's copy cannot be written (another index moved the
  folder), the server's version is no longer applied, even if this index's scan
  relocates the row before the apply would run. A copy that has to be dropped is
  deleted from where the folder is now, as a best effort.
- Export rebase on a folder move (scan and Reorganize): the sequence's own
  export follows the folder unless its file is still at the old path and not at
  the new one. A file written into a re-created old folder therefore keeps a row
  that finds it, so the file is never stranded.
- A render job refuses to start when its folder is not where the index has it,
  which also stops it from re-creating the old folder. Renders fetched by sync
  are now filed under the sequence lease into the current folder, or not at all
  (new string Errors.Archive.FolderMoved, en and it).
- The proxy pass holds the thumbnail lease one batch at a time (a quarter of
  the lease wait), then lets it go for longer than a waiter's poll and reads
  the folder and sidecar again. Before, another process's Reorganize, sequence
  deletion or pack compaction could wait out its lease wait and fail. Updated
  the stale comments on that lease and ARCHITECTURE.md to match.

Tests cover each fix, and each new test fails with its fix reverted. Also
covered: proxy store and pass in a moved folder, RenderStore with the folder
gone, Take server with the folder moved, and an editor save with the folder
moved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 811509d32a118ed50ad10d47d4b002e534da9d77)
2026-10-27 19:16:28 +01:00

794 lines
41 KiB
C#

using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Archiving;
using Chronos.Data.Sync;
using Chronos.Data.Tests.Archiving;
using Chronos.Protocol;
using Chronos.Protocol.Client;
using Chronos.Testing;
namespace Chronos.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 SettingsTakenWhileAReorganizeMovesTheFolderGoWhereTheSequenceIsNow()
{
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();
var left = f.Archive.Paths.Sequence((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Folder).Folder;
// The server's edit is taken after a Reorganize moved the folder of the copy the comparison had read.
f.Remote.EditSettings(remote.Id, SyncFixture.Window33);
f.Remote.OnceBeforeManifest = async () =>
{
var plan = await f.Archive.Service.PlanRenameAsync(f.Archive.Archive.Id, [local.Id], "moved/{title}", null, f.Ct);
await f.Archive.Service.ApplyRenameAsync(plan, null, f.Ct);
};
await f.RefreshAsync();
Assert.Null(f.Remote.OnceBeforeManifest, "the folder moved during the comparison");
var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.Equal("moved/Harbour", taken.Folder);
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 in the moved folder follows the index");
Assert.False(Directory.Exists(left), "the folder it left is not created again");
}
[Test]
public async Task PositionsSentToTheServerNameItsFramesAndWaitForPhotosItLacks()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(5);
var local = await f.LocalAsync("Harbour", photos);
// The server lacks the earliest photo (not uploaded yet).
var remote = f.Remote.Add("Harbour", photos[1..]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
await f.EditLocalSettingsAsync(local.Id, new ProjectSettings { ExcludedFrames = [0, 2], HeroFrame = 4 }.ToJson());
await f.RefreshAsync();
var sent = ProjectSettings.FromJson(remote.SettingsJson!);
Assert.SequenceEqual([Sha(photos[2])], sent.ExcludedFrames.Select(i => remote.Frames[i].Sha256));
Assert.Equal(Sha(photos[4]), remote.Frames[sent.HeroFrame!.Value].Sha256);
Assert.Count(1, sent.PendingFrames!);
var waiting = sent.PendingFrames![0];
Assert.True(waiting.Excluded && waiting.Sha256 == Sha(photos[0]), "the exclusion of the photo the server lacks waits for it");
Assert.Equal(remote.FramesVersion, f.Remote.Updates[^1].ExpectedFramesVersion);
// This PC's project keeps its own positions.
Assert.SequenceEqual([0, 2], ProjectSettings.FromJson((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson!).ExcludedFrames);
Assert.Equal(SyncStatus.LocalAhead, (await f.EntryAsync("Harbour")).Status);
Assert.Equal(1, (await f.EntryAsync("Harbour")).Link!.MissingRemote);
}
[Test]
public async Task SettingsSentWhileTheServersFramesChangeAreTranslatedAgain()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(4);
var local = await f.LocalAsync("Harbour", photos[1..]);
var remote = f.Remote.Add("Harbour", photos[1..]);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
await f.EditLocalSettingsAsync(local.Id, new ProjectSettings { ExcludedFrames = [1] }.ToJson());
// An earlier photo is filed on the server after the manifest was read, before the settings arrive.
var racing = new RacingRemote(f.Remote, () => remote.InsertFrame(0, photos[0]), beforeUpdate: true);
await f.Sync.RefreshAsync(racing, new SyncPolicy(), f.Ct);
Assert.Empty(f.Remote.Updates);
Assert.Equal(SyncStatus.LocalAhead, (await f.EntryAsync("Harbour")).Status);
await f.RefreshAsync();
Assert.SequenceEqual([Sha(photos[2])], ProjectSettings.FromJson(remote.SettingsJson!).ExcludedFrames.Select(i => remote.Frames[i].Sha256));
Assert.Equal((SyncStatus.ServerAhead, 1), ((await f.EntryAsync("Harbour")).Status, (await f.EntryAsync("Harbour")).Link!.MissingLocal));
}
[Test]
public async Task SettingsSentWhileAnImportHereMovesTheirPositionsStayOnTheirPhotos()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(4);
var local = await f.LocalAsync("Harbour", photos[1..]);
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, new ProjectSettings { ExcludedFrames = [1], HeroFrame = 2 }.ToJson());
// The earliest photo is imported here after the comparison read this copy, before its settings are translated.
var racing = new RacingRemote(f.Remote, () => f.AddLocalAsync(local.Id, photos[0]).GetAwaiter().GetResult(), beforeManifest: true);
await f.Sync.RefreshAsync(racing, new SyncPolicy(), f.Ct);
var mine = ProjectSettings.FromJson((await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson!);
Assert.SequenceEqual([2], mine.ExcludedFrames);
Assert.Equal(3, mine.HeroFrame);
var sent = ProjectSettings.FromJson(remote.SettingsJson!);
Assert.SequenceEqual([Sha(photos[2])], sent.ExcludedFrames.Select(i => remote.Frames[i].Sha256));
Assert.Equal(Sha(photos[3]), remote.Frames[sent.HeroFrame!.Value].Sha256);
Assert.Null(sent.PendingFrames);
}
[Test]
public async Task SettingsComparedWhileAnImportHereMovesTheirPositionsAreAgreed()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(4);
var local = await f.LocalAsync("Harbour", photos[1..]);
var remote = f.Remote.Add("Harbour", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
// Both sides exclude the same photo, each by its own frames.
await f.EditLocalSettingsAsync(local.Id, new ProjectSettings { ExcludedFrames = [1] }.ToJson());
f.Remote.EditSettings(remote.Id, new ProjectSettings { ExcludedFrames = [2] }.ToJson());
var racing = new RacingRemote(f.Remote, () => f.AddLocalAsync(local.Id, photos[0]).GetAwaiter().GetResult(), beforeManifest: true);
await f.Sync.RefreshAsync(racing, new SyncPolicy(), f.Ct);
var entry = await f.EntryAsync("Harbour");
Assert.Null(entry.Link!.ConflictRemote);
Assert.Equal(entry.Local!.SettingsVersion, entry.Link.BaseLocalSettings);
Assert.Equal(remote.SettingsVersion, entry.Link.BaseRemoteSettings);
Assert.SequenceEqual([2], ProjectSettings.FromJson(entry.Local.SettingsJson!).ExcludedFrames);
}
[Test]
public async Task TheSameSettingsFromTheServerAreAgreedWhileTheArchiveIsOffline()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(2);
var local = await f.LocalAsync("Harbour", photos, SyncFixture.Window41);
var remote = f.Remote.Add("Harbour", photos, SyncFixture.Window41);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
// The server's version moves with the same content (another PC sent the same values); the archive's drive is unplugged.
f.Remote.EditSettings(remote.Id, SyncFixture.Window41);
var aside = f.Archive.Paths.ManifestPath + ".unplugged";
File.Move(f.Archive.Paths.ManifestPath, aside);
try
{
await f.RefreshAsync();
}
finally
{
File.Move(aside, f.Archive.Paths.ManifestPath);
}
var entry = await f.EntryAsync("Harbour");
Assert.Null(entry.Link!.Error);
Assert.Equal(SyncStatus.InSync, entry.Status);
Assert.Equal(local.SettingsVersion, entry.Local!.SettingsVersion);
}
[Test]
public async Task AnEditSavedHereDuringTheComparisonIsNotReplacedByTheServers()
{
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's copy changes; the editor saves this PC's edit after the comparison read the local copy, before it
// takes the server's version.
f.Remote.EditSettings(remote.Id, SyncFixture.Window33);
var racing = new RacingRemote(f.Remote, () => f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41).GetAwaiter().GetResult(),
beforeManifest: true);
await f.Sync.RefreshAsync(racing, new SyncPolicy(), f.Ct);
var entry = await f.EntryAsync("Harbour");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, entry.Local!.SettingsJson), "this PC's edit is kept");
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, await File.ReadAllTextAsync(f.Archive.Paths.Sequence(entry.Local.Folder).ProjectPath, f.Ct)));
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, entry.Link!.ConflictRemote!.SettingsJson), "the user picks");
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, remote.SettingsJson));
}
[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, new SyncPolicy(AutoSyncSettings: false), f.Ct);
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, remote.SettingsJson));
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
}
[Test]
public async Task TagsChangedDifferentlyOnBothSidesAreAConflictToo()
{
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.Remote.UpdateSequenceAsync(remote.Id, new UpdateSequenceRequest(null, ["boats"], null, null, null, null), f.Ct);
await f.Archive.Service.SetSequenceInfoAsync(local.Id, "Harbour", ["sunset"], 0, null, f.Ct);
await f.RefreshAsync();
// No side wins silently: a tag removed on one side must not come back, and an edit must not be lost.
var entry = await f.EntryAsync("Harbour");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.SequenceEqual(["boats"], entry.Link!.ConflictRemote!.Tags);
Assert.Equal("sequence.tags", SyncRules.Differences(SyncPayload.Of(entry.Local!), entry.Link.ConflictRemote)[0].Path);
Assert.SequenceEqual(["boats"], remote.Tags);
Assert.SequenceEqual(["sunset"], (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.Tags);
}
[Test]
public async Task InfoTakenFromTheServerLeavesPhotosAnotherWriterAddedHereToTheNextScan()
{
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();
// A desktop sharing this PC's archive imports a photo into the sequence: its own index follows the sidecar, this one not yet.
var (desktop, archive) = await f.Archive.OtherWriterAsync();
var staged = f.Archive.Stage(ArchiveFixture.Start.AddMinutes(10), seed: 90);
await desktop.Ingest.CommitAsync(new IngestRequest(archive.Id, local.Id, null, null, staged.Path, "IMG_0009.JPG", staged.Size, staged.Sha256, null, null),
f.Ct);
// The server's new title reaches this PC meanwhile, in its index and the sidecar.
await f.Remote.UpdateSequenceAsync(remote.Id, new UpdateSequenceRequest("Harbour at dusk", null, null, null, null, null), f.Ct);
await f.RefreshAsync();
var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.Equal(("Harbour at dusk", 2), (taken.Title, taken.FrameCount));
// The next scan still finds the folder changed, and indexes the photo.
Assert.Contains(local.Id, (await f.Archive.ScanAsync()).Sequences);
var row = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.Equal(("Harbour at dusk", 3), (row.Title, row.FrameCount));
}
[Test]
public async Task TheSameEditOnBothSidesIsAgreedWithoutAConflict()
{
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.Remote.UpdateSequenceAsync(remote.Id, new UpdateSequenceRequest(null, ["sunset"], null, null, SyncFixture.Window41, null), f.Ct);
await f.Archive.Service.SetSequenceInfoAsync(local.Id, "Harbour", ["SUNSET"], 0, null, f.Ct);
await f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41);
await f.RefreshAsync();
Assert.Equal(SyncStatus.InSync, (await f.EntryAsync("Harbour")).Status);
}
[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!.ConflictRemote!.SettingsJson), "the server's side is kept for display");
Assert.Equal("exposure.deflickerWindow.manual", SyncRules.Differences(entry.Local.SettingsJson, entry.Link.ConflictRemote.SettingsJson)[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.this-pc-*.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);
var copy = 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));
Assert.Equal(copy, Assert.Contains(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.this-pc-*.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 SaveBothWhileAReorganizeMovesTheFolderKeepsTheCopyWhereTheSequenceIsNow()
{
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await ConflictAsync(f);
var left = f.Archive.Paths.Sequence(local.Folder).Folder;
// A Reorganize moves the folder while the server's version is read.
f.Remote.OnceBeforeManifest = async () =>
{
var plan = await f.Archive.Service.PlanRenameAsync(f.Archive.Archive.Id, [local.Id], "moved/{title}", null, f.Ct);
await f.Archive.Service.ApplyRenameAsync(plan, null, f.Ct);
};
var copy = await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct);
Assert.Null(f.Remote.OnceBeforeManifest, "the folder moved during the resolution");
var taken = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.Equal("moved/Holy grail", taken.Folder);
Assert.Equal(copy, Assert.Contains(Directory.GetFiles(f.Archive.Paths.Sequence(taken.Folder).MetaDirectory, "project.this-pc-*.json"), _ => true));
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, await File.ReadAllTextAsync(copy!, f.Ct)), "this PC's version is kept");
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, taken.SettingsJson));
Assert.False(Directory.Exists(left), "the folder it left is not created again");
}
[Test]
public async Task SaveBothThatCannotKeepThisPcsVersionTakesNothing()
{
await using var f = await SyncFixture.CreateAsync(context);
// The server holds a photo this PC does not have yet, and its settings name a frame: taking them reads the server's
// frames, after this PC's copy was kept and before the server's version is written.
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Holy grail", photos[..2]);
var remote = f.Remote.Add("Holy grail", photos);
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
await f.RefreshAsync();
local = await f.EditLocalSettingsAsync(local.Id, SyncFixture.Window41);
var theirs = """{"heroFrame":1,"exposure":{"deflickerWindow":{"isAuto":false,"manual":33}}}""";
f.Remote.EditSettings(remote.Id, theirs);
await f.RefreshAsync();
Assert.NotNull((await f.EntryAsync("Holy grail")).Link!.ConflictRemote);
var left = f.Archive.Paths.Sequence(local.Folder).Folder;
// Another index moves the folder while the server's version is read, so this PC's version cannot be kept there; this
// index's scan finds the folder before the server's version would be written.
var (other, archive) = await f.Archive.OtherWriterAsync();
string? moved = null;
f.Remote.OnceBeforeManifest = async () =>
{
var plan = await other.PlanRenameAsync(archive.Id, [local.Id], "moved/{title}", null, f.Ct);
await other.ApplyRenameAsync(plan, null, f.Ct);
moved = plan.Steps.Single().ToFolder;
};
f.Remote.OnceBeforeFrames = () => f.Archive.ScanAsync();
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct));
// The server's frames were not read for a version that is not taken: the scan comes now.
f.Remote.OnceBeforeFrames = null;
await f.Archive.ScanAsync();
var kept = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.Equal(moved, kept.Folder, "the scan found the folder");
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, kept.SettingsJson), "this PC's version is not replaced without its copy");
var meta = f.Archive.Paths.Sequence(kept.Folder).MetaDirectory;
Assert.Empty(Directory.GetFiles(meta, SyncService.SavedCopyPattern));
Assert.False(Directory.Exists(left), "the folder it left is not created again");
Assert.NotNull((await f.EntryAsync("Holy grail")).Link!.ConflictRemote, "still a conflict");
// Asked again where the sequence is now, both versions are kept.
var copy = await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct);
Assert.Equal(copy, Assert.Contains(Directory.GetFiles(meta, SyncService.SavedCopyPattern), _ => true));
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, await File.ReadAllTextAsync(copy!, f.Ct)), "this PC's version is kept");
}
[Test]
public async Task TakeServerWhereAnotherIndexMovedTheFolderWritesNothing()
{
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await ConflictAsync(f);
var left = f.Archive.Paths.Sequence(local.Folder).Folder;
// Another index moves the folder while the server's version is read; this index has not scanned it since.
var (other, archive) = await f.Archive.OtherWriterAsync();
f.Remote.OnceBeforeManifest = async () =>
{
var plan = await other.PlanRenameAsync(archive.Id, [local.Id], "moved/{title}", null, f.Ct);
await other.ApplyRenameAsync(plan, null, f.Ct);
};
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct));
Assert.Null(f.Remote.OnceBeforeManifest, "the folder moved during the resolution");
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson), "nothing applied");
Assert.False(Directory.Exists(left), "the folder it left is not created again");
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, await File.ReadAllTextAsync(f.Archive.Paths.Sequence("moved/Holy grail").ProjectPath, f.Ct)),
"the moved project is this PC's version");
}
[Test]
public async Task KeepMineRefusesWhenTheServerChangedAfterTheConflictWasShown()
{
await using var f = await SyncFixture.CreateAsync(context);
var (_, remote) = await ConflictAsync(f);
var later = SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal);
f.Remote.EditSettings(remote.Id, later);
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.KeepMine, f.Ct));
Assert.True(SyncRules.SameSettings(later, remote.SettingsJson), "the edit the user never saw is not overwritten");
var entry = await f.EntryAsync("Holy grail");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.True(SyncRules.SameSettings(later, entry.Link!.ConflictRemote!.SettingsJson), "the conflict now shows the server's latest edit");
await f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.KeepMine, f.Ct);
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, remote.SettingsJson));
}
[Test]
public async Task KeepMineRefusesWhenTheServerChangesWhileItIsSent()
{
await using var f = await SyncFixture.CreateAsync(context);
var (_, remote) = await ConflictAsync(f);
var later = SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal);
var racing = new RacingRemote(f.Remote, () => f.Remote.EditSettings(remote.Id, later), beforeUpdate: true);
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(racing, remote.Id, SyncResolution.KeepMine, f.Ct));
Assert.True(SyncRules.SameSettings(later, remote.SettingsJson), "nothing overwritten");
}
[Test]
public async Task TakeServerRefusesWhenThisPcChangedAfterTheConflictWasShown()
{
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await ConflictAsync(f);
var later = SyncFixture.Window41.Replace("41", "45", StringComparison.Ordinal);
await f.EditLocalSettingsAsync(local.Id, later);
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.TakeServer, f.Ct));
Assert.True(SyncRules.SameSettings(later, (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!.SettingsJson), "this PC's later edit is kept");
Assert.True(SyncRules.SameSettings(SyncFixture.Window33, remote.SettingsJson));
var entry = await f.EntryAsync("Holy grail");
Assert.Equal(SyncStatus.Conflict, entry.Status);
Assert.Equal(entry.Local!.SettingsVersion, entry.Link!.ConflictLocalSettings);
}
[Test]
public async Task TakeServerRefusesWhenTheServerChangedAfterTheConflictWasShown()
{
await using var f = await SyncFixture.CreateAsync(context);
var (local, remote) = await ConflictAsync(f);
f.Remote.EditSettings(remote.Id, SyncFixture.Window33.Replace("33", "35", StringComparison.Ordinal));
await Assert.ThrowsAsync<SyncConflictChangedException>(() => f.Sync.ResolveAsync(f.Remote, remote.Id, SyncResolution.SaveBoth, f.Ct));
var kept = (await f.Db.Sequences.GetAsync(local.Id, f.Ct))!;
Assert.True(SyncRules.SameSettings(SyncFixture.Window41, kept.SettingsJson), "nothing taken that the user did not see");
Assert.Empty(Directory.GetFiles(f.Archive.Paths.Sequence(kept.Folder).MetaDirectory, "project.this-pc-*.json"));
}
[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);
}
[Test]
public async Task ALinkIsForgottenOnlyWhenTheServerSaysTheSequenceIsGone()
{
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();
// A listing whose pages moved while they were read misses the sequence: it is asked for by id instead.
f.Remote.Unlisted.Add(remote.Id);
await f.RefreshAsync();
var kept = await f.EntryAsync("Harbour");
Assert.Equal(SyncStatus.InSync, kept.Status);
Assert.Equal(remote.Id, kept.Link!.RemoteId);
f.Remote.Sequences.Remove(remote.Id);
await f.RefreshAsync();
Assert.Equal(SyncStatus.LocalOnly, (await f.EntryAsync("Harbour")).Status);
}
[Test]
public async Task AServerRefusalOnOneSequenceIsRecordedAndTheOthersAreStillCompared()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(4);
var harbour = await f.LocalAsync("Harbour", photos[..2]);
var aurora = await f.LocalAsync("Aurora", photos[2..]);
var refused = f.Remote.Add("Harbour", photos[..2]);
var fine = f.Remote.Add("Aurora", photos[2..]);
await f.Sync.LinkAsync(f.Server, refused.Id, harbour.Id, f.Ct);
await f.Sync.LinkAsync(f.Server, fine.Id, aurora.Id, f.Ct);
f.Remote.Refused.Add(refused.Id);
await f.RefreshAsync();
Assert.NotNull((await f.EntryAsync("Harbour")).Link!.Error);
var compared = await f.EntryAsync("Aurora");
Assert.Equal(SyncStatus.InSync, compared.Status);
Assert.Null(compared.Link!.Error);
f.Remote.Refused.Clear();
await f.RefreshAsync();
var recovered = await f.EntryAsync("Harbour");
Assert.Null(recovered.Link!.Error);
Assert.Equal(SyncStatus.InSync, recovered.Status);
}
[Test]
public async Task WithoutTheEditRightThisPcsEditsWaitInsteadOfFailing()
{
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(CanEdit: false));
Assert.Null(remote.SettingsJson);
var entry = await f.EntryAsync("Harbour");
Assert.Equal(SyncStatus.LocalAhead, entry.Status);
Assert.Null(entry.Link!.Error);
}
[Test]
public async Task CopiesHoldingTheSameOriginalsAreLinkedByContent()
{
await using var f = await SyncFixture.CreateAsync(context);
var photos = f.NewPhotos(3);
var local = await f.LocalAsync("Comet", photos);
// The Uploader sent the same card to the server; nothing links the two yet.
var remote = f.Remote.Add("2026-09-28 Canon", photos);
remote.FirstCapture = local.FirstCapture;
remote.CameraModel = local.CameraModel;
var other = f.Remote.Add("Other card", f.NewPhotos(2));
other.FirstCapture = local.FirstCapture;
other.CameraModel = local.CameraModel;
await f.RefreshAsync();
var entry = await f.EntryAsync("Comet");
Assert.Equal(remote.Id, entry.Link?.RemoteId);
Assert.Equal((0, 0), (entry.Link!.MissingRemote, entry.Link.MissingLocal));
Assert.Null((await f.EntryAsync("Other card")).Local);
var manifests = f.Remote.ManifestCalls;
await f.RefreshAsync();
Assert.Equal(manifests, f.Remote.ManifestCalls);
}
[Test]
public async Task ABackgroundProxyDownloadGivesWayToTheUsersRequest()
{
await using var f = await SyncFixture.CreateAsync(context);
var remote = f.Remote.Add("Construction cam", f.NewPhotos(1));
await f.RefreshAsync();
await using var engine = new Chronos.Data.Jobs.JobEngine(f.Db, f.Sync.CreateJobHandlers(new SyncFixture.Connector(f.Remote)));
var background = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Proxies, null, JobPriority.Background);
Assert.Equal(background.Id, (await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Proxies, null,
JobPriority.Background)).Id);
var opened = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Proxies, null);
Assert.NotEqual(background.Id, opened.Id);
Assert.Equal(JobPriority.User, opened.Priority);
Assert.Equal(JobState.Cancelled, (await f.Db.Jobs.GetAsync(background.Id, f.Ct))!.State);
var originals = await f.Sync.EnqueueDownloadAsync(engine, f.Server, remote.Id, "Construction cam", SyncDownloadMode.Originals, f.Archive.Archive.Id);
Assert.NotEqual(opened.Id, originals.Id);
Assert.Equal(SyncTransferKind.DownloadOriginals, SyncService.TransferKind(originals));
Assert.Equal(originals.Id, (await f.EntryAsync("Construction cam")).Transfer!.Id);
}
private static string Sha(byte[] bytes) => Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes));
/// <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 edits right after the first read of a sequence, right before the first edit, or right
/// before the first manifest is read.
/// </summary>
private sealed class RacingRemote(FakeSyncRemote inner, Action race, bool beforeUpdate = false, bool beforeManifest = false) : 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);
if (!beforeUpdate && !beforeManifest) 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)
{
if (beforeManifest) Interlocked.Exchange(ref _race, null)?.Invoke();
return 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)
{
if (beforeUpdate) Interlocked.Exchange(ref _race, null)?.Invoke();
return 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 Task<string?> DownloadRenderAsync(Guid renderId, Stream destination, CancellationToken cancellationToken) =>
inner.DownloadRenderAsync(renderId, destination, cancellationToken);
public Task<RenderDto?> UploadRenderAsync(Guid sequenceId, string path, RenderUploadRequest request, CancellationToken cancellationToken) =>
inner.UploadRenderAsync(sequenceId, path, request, cancellationToken);
public IAsyncEnumerable<ServerEvent> EventsAsync(CancellationToken cancellationToken) => inner.EventsAsync(cancellationToken);
public void Dispose()
{
}
}
}