The review of the found-copies work showed pairs that could stick, jobs that could act on a pair the user had answered otherwise, and detection gaps: - forgetting a link on an address-keyed server deletes the pair and Candidate rows of that remote and resets the probe, so the sequence is found again when it is back; a Linked/Integrated row with no link is asked again - 404s count only on id-keyed servers, at most once per recheck interval - probes store the server's newest update time (copy_probes.server_newest, migration 0012) instead of comparing with this PC's clock; the lookup sample carries hashed photos by size and capture time too, for servers that have not hashed their copy - the older-server gate no longer re-compares unchanged manifests - a failed Integrate keeps its job id so a retry resumes only an untouched pair; cancel and stale plans release the pair (ReleaseAsync) - automatic photo transfers never create or retarget a sequence - a first upload files photos the server holds elsewhere into the new sequence - Unlink re-compares before keeping apart; Trash here names only true last copies; Drop... trashes one version on both sides after re-checking the other - a merge keeping the pair's other sequence swaps the dialog's columns and re-keys Different choices; Upload all reads every pair Each fix has a regression test where it makes sense. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
7.5 KiB
C#
146 lines
7.5 KiB
C#
using Chronos.Core.Model;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Data.Sync;
|
|
using Chronos.Data.Tests.Archiving;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Sync;
|
|
|
|
/// <summary>A local archive and index, the sync engine over them and an in-memory server.</summary>
|
|
internal sealed class SyncFixture : IAsyncDisposable
|
|
{
|
|
public const string Window41 = """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":41}}}""";
|
|
public const string Window33 = """{"exposure":{"deflickerWindow":{"isAuto":false,"manual":33}}}""";
|
|
|
|
private readonly List<IAsyncDisposable> _owned = [];
|
|
private int _seed;
|
|
|
|
private SyncFixture(ArchiveFixture archive, SyncService sync)
|
|
{
|
|
Archive = archive;
|
|
Sync = sync;
|
|
_owned.Add(archive);
|
|
}
|
|
|
|
public ArchiveFixture Archive { get; }
|
|
public SyncService Sync { get; }
|
|
public FakeSyncRemote Remote { get; } = new();
|
|
public ChronosDatabase Db => Archive.Db;
|
|
public string Server => Remote.Server;
|
|
public CancellationToken Ct => Archive.Ct;
|
|
|
|
public static async Task<SyncFixture> CreateAsync(TestContext context)
|
|
{
|
|
var archive = await ArchiveFixture.CreateAsync(context);
|
|
var sync = new SyncService(archive.Db, archive.Service, new SyncOptions
|
|
{
|
|
Archive = new ArchiveServiceOptions { Owner = "test", LeaseWait = TimeSpan.FromSeconds(20) },
|
|
ProxyCacheDirectory = context.CreateTempDirectory("proxies"),
|
|
});
|
|
return new SyncFixture(archive, sync);
|
|
}
|
|
|
|
public Task<SyncSnapshot> RefreshAsync(SyncPolicy? policy = null) => Sync.RefreshAsync(Remote, policy ?? new SyncPolicy(), Ct);
|
|
|
|
public async Task<SyncEntry> EntryAsync(string title) =>
|
|
(await Sync.ListAsync(Server, Ct)).Single(e => e.Title == title);
|
|
|
|
/// <summary>Distinct photos (the same bytes on both sides make the same original).</summary>
|
|
public List<byte[]> NewPhotos(int count)
|
|
{
|
|
var photos = new List<byte[]>(count);
|
|
for (var i = 0; i < count; i++, _seed++) photos.Add(Photos.Jpeg(ArchiveFixture.Start.AddSeconds(10 * _seed), 1000 + _seed));
|
|
return photos;
|
|
}
|
|
|
|
/// <summary>
|
|
/// A local sequence made through the archive ingest (hashed frames, a real sidecar), each frame captured 10 s after the
|
|
/// one before from <see cref="ArchiveFixture.Start"/>, or at <paramref name="capturedAt"/>.
|
|
/// </summary>
|
|
public async Task<Sequence> LocalAsync(string title, IReadOnlyList<byte[]> originals, string? settingsJson = null, IReadOnlyList<DateTime>? capturedAt = null)
|
|
{
|
|
Guid? id = null;
|
|
var staging = Path.Combine(Archive.Paths.StagingDirectory, "test");
|
|
Directory.CreateDirectory(staging);
|
|
for (var i = 0; i < originals.Count; i++)
|
|
{
|
|
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part");
|
|
await File.WriteAllBytesAsync(path, originals[i], Ct);
|
|
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(originals[i]));
|
|
var result = await Archive.Service.Ingest.CommitAsync(new IngestRequest(Archive.Archive.Id, id, id is null ? title : null, null, path,
|
|
$"IMG_{i + 1:0000}.JPG", originals[i].Length, sha, capturedAt?[i] ?? ArchiveFixture.Start.AddSeconds(10 * i), null), Ct);
|
|
id = result.SequenceId;
|
|
}
|
|
var sequence = (await Db.Sequences.GetAsync(id!.Value, Ct))!;
|
|
if (settingsJson is not null) sequence = await EditLocalSettingsAsync(sequence.Id, settingsJson);
|
|
return sequence;
|
|
}
|
|
|
|
/// <summary>Files one more original into a local sequence through the archive ingest (an earlier photo lands before the others).</summary>
|
|
public async Task AddLocalAsync(Guid sequenceId, byte[] original)
|
|
{
|
|
var staging = Path.Combine(Archive.Paths.StagingDirectory, "test");
|
|
Directory.CreateDirectory(staging);
|
|
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.part");
|
|
await File.WriteAllBytesAsync(path, original, Ct);
|
|
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(original));
|
|
await Archive.Service.Ingest.CommitAsync(new IngestRequest(Archive.Archive.Id, sequenceId, null, null, path, $"IMG_{sha[..8]}.JPG", original.Length, sha,
|
|
null, null), Ct);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Replaces the file of the local frame at <paramref name="index"/> with <paramref name="converted"/>, as a conversion's
|
|
/// Replace does on this PC (owner review 2, R7): the same frame, a tombstone naming the new content.
|
|
/// </summary>
|
|
public async Task ReplaceLocalAsync(Guid sequenceId, int index, byte[] converted)
|
|
{
|
|
var sequence = (await Db.Sequences.GetAsync(sequenceId, Ct))!;
|
|
var frame = Chronos.Core.Archiving.ManifestStore.ReadSequence(Archive.Paths.Sequence(sequence.Folder).ManifestPath).Frames[index];
|
|
var staging = Path.Combine(Archive.Paths.StagingDirectory, "convert-test");
|
|
Directory.CreateDirectory(staging);
|
|
var path = Path.Combine(staging, $"{Guid.NewGuid():N}.jpeg");
|
|
await File.WriteAllBytesAsync(path, converted, Ct);
|
|
var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(converted));
|
|
var swap = new FrameSwap(frame.Id, Archive.Paths.ToRelative(path), [], (current, _) => Path.ChangeExtension(current.File, ".jpeg"), current => current with
|
|
{
|
|
Sha256 = sha,
|
|
Size = converted.Length,
|
|
Source = new Chronos.Core.Archiving.FrameSource { Sha256 = current.Sha256, Format = current.Format, File = current.File, Size = current.Size },
|
|
});
|
|
Assert.NotNull(await Archive.Service.FrameTrash.ReplaceAsync(Archive.Archive, sequenceId, null, [swap], null, null, null, null, Ct));
|
|
}
|
|
|
|
/// <summary>An edit on this PC, as the editor makes it (project file and index, settings version + 1).</summary>
|
|
public async Task<Sequence> EditLocalSettingsAsync(Guid id, string settingsJson)
|
|
{
|
|
var sequence = (await Db.Sequences.GetAsync(id, Ct))!;
|
|
var folder = Archive.Paths.Sequence(sequence.Folder);
|
|
await File.WriteAllTextAsync(folder.ProjectPath, settingsJson, Ct);
|
|
var result = await Db.Sequences.EditAsync(id, new Chronos.Data.Repositories.SequenceEdit { SettingsJson = settingsJson }, sequence.SettingsVersion, Ct);
|
|
return result.Sequence!;
|
|
}
|
|
|
|
/// <summary>A started engine with the archive and sync handlers; jobs reach the server through <paramref name="connector"/> (the fake by default).</summary>
|
|
public async Task<JobEngine> StartEngineAsync(ISyncConnector? connector = null)
|
|
{
|
|
var engine = new JobEngine(Db, [.. Archive.Service.CreateJobHandlers(), .. Sync.CreateJobHandlers(connector ?? new Connector(Remote))],
|
|
new JobEngineOptions { Workers = 2, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
|
_owned.Insert(0, engine);
|
|
await engine.StartAsync(Ct);
|
|
return engine;
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
foreach (var owned in _owned) await owned.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>Hands out <paramref name="remote"/> for its own server; null for any other, or always when <paramref name="remote"/> is null.</summary>
|
|
public sealed class Connector(ISyncRemote? remote) : ISyncConnector
|
|
{
|
|
public ValueTask<ISyncRemote?> ConnectAsync(string server, CancellationToken cancellationToken) =>
|
|
ValueTask.FromResult(remote is not null && remote.Server == server ? remote : null);
|
|
}
|
|
}
|