Files
Alby96andClaude Opus 5.5 f33bcac32e
CI / Build and test (push) Successful in 13m44s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 16s
Release / Build, test and publish (push) Successful in 26m34s
CI / PostgreSQL 16 integration (push) Failing after 17m36s
CI / PostgreSQL 17 integration (push) Failing after 15m36s
CI / Docker image (push) Successful in 1m12s
Release / Server image (push) Failing after 15s
feat(copies): matches between this PC and the server, Integrate and merge (owner review 6, v1.2.0) (#74)
The same time-lapse on a server and on this PC under different ids, or twice on this PC, is recognised,
compared and settled as the owner asked in owner review 6.

Matches are found after each sync refresh, through the server's new photo lookup, and after the jobs that
change photos. The default is to ask every time. Server › Matches offers:
- Keep apart, which is remembered;
- Link only and Unlink;
- the Compare dialog: photos, details line by line, projects and the plan.

Integrate completes both sides. Photos default to Union, projects are copied across, and a losing version
goes to the trash only after a hash check. Afterwards new photos move by themselves, and deletions wait for
the user.

A merge of two local sequences verifies every photo before the other sequence goes to its archive's trash.
Upload all local skips suspected matches and flags them.

Migration 0012 adds the found_copies and copy_probes tables. Jobs 25 (Integrate) and 26 (MergeSequences) are
new, and so are the server capabilities photo-lookup and upload-into.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-18 15:20:29 +01:00

193 lines
9.9 KiB
C#

using Chronos.Core.Model;
using Chronos.Core.Project;
using Chronos.Data.Copies;
using Chronos.Data.Projects;
using Chronos.Data.Tests.Archiving;
using Chronos.Testing;
namespace Chronos.Data.Tests.Projects;
/// <summary>
/// A project copied with its whole history from one home to another (owner review 6, #73; design §5c): content hashes
/// kept, a free name, a cut finished from its journal, a copy somebody took left alone, a newer format refused.
/// </summary>
[Category("projects")]
public sealed class ProjectTransplantTests(TestContext context)
{
private sealed record Homes(ArchiveFixture Fx, ArchiveProjectHome From, ArchiveProjectHome To, SequenceProject Source, List<Frame> FromFrames,
List<Frame> ToFrames);
/// <summary>Two sequences; the first has "Vertical" with two versions after its first.</summary>
private async Task<Homes> HomesAsync()
{
var fx = await ArchiveFixture.CreateAsync(context);
var (a, aFrames) = await ProjectServiceTests.UploadedAsync(fx, 3, 100);
var (b, bFrames) = await ProjectServiceTests.UploadedAsync(fx, 3, 200);
var projects = fx.Service.Projects;
var vertical = await projects.CreateAsync(a.Id, "Vertical", cancellationToken: fx.Ct);
var head = await projects.HeadAsync(vertical.Id, fx.Ct);
head = (await projects.PushAsync(vertical.Id, ProjectServiceTests.Draft(head, new ProjectSettings { ExcludedFrames = [1] }, aFrames, "one"), fx.Ct)).Version!;
await projects.PushAsync(vertical.Id, ProjectServiceTests.Draft(head, new ProjectSettings { HeroFrame = 2 }, aFrames, "two"), fx.Ct);
var source = (await projects.ListAsync(a.Id, fx.Ct)).Single(p => p.Id == vertical.Id);
return new Homes(fx, new ArchiveProjectHome(fx.Db, projects, a.Id), new ArchiveProjectHome(fx.Db, projects, b.Id), source, aFrames, bFrames);
}
private static async Task<List<string>> HashesAsync(IProjectHome home, Guid projectId, CancellationToken ct) =>
[.. (await home.VersionsAfterAsync(projectId, 0, 100, ct)).Select(v => v.ContentHash)];
[Test]
public async Task TheWholeHistoryIsCopiedWithItsContentHashes()
{
var h = await HomesAsync();
await using var _ = h.Fx;
var saved = new List<TransplantStep>();
var copy = await new ProjectTransplant().CopyAsync(h.From, h.Source, h.To, "Vertical", null, s => { saved.Add(s); return ValueTask.CompletedTask; }, h.Fx.Ct);
Assert.Equal("Vertical", copy.Name);
var source = await HashesAsync(h.From, h.Source.Id, h.Fx.Ct);
var copied = await HashesAsync(h.To, copy.Id, h.Fx.Ct);
// The copy's first version is the one it was made from, then the source's history in order.
Assert.SequenceEqual(source, copied.Skip(1));
Assert.Equal(source.Count + 1, copy.HeadNumber);
Assert.Equal(copy.Id, saved[^1].TargetId);
Assert.Equal(source.Count, saved[^1].Moves.Count);
// The source is untouched.
Assert.Equal(source.Count, (await h.From.HeadAsync(h.Source.Id, h.Fx.Ct)).Number);
}
[Test]
public async Task ANameTakenGetsTheNextFreeOne()
{
var h = await HomesAsync();
await using var _ = h.Fx;
await h.To.CreateAsync("Vertical", null, null, h.Fx.Ct);
var copy = await new ProjectTransplant().CopyAsync(h.From, h.Source, h.To, "Vertical", null, _ => ValueTask.CompletedTask, h.Fx.Ct);
Assert.Equal("Vertical (2)", copy.Name);
Assert.Equal("Vertical", ProjectTransplant.FreeName("Vertical", "STUDIO-PC", new HashSet<string> { "Main" }));
Assert.Equal("Vertical (STUDIO-PC)", ProjectTransplant.FreeName("Vertical", "STUDIO-PC", new HashSet<string> { "vertical" }));
Assert.Equal("Vertical (STUDIO-PC) (2)", ProjectTransplant.FreeName("Vertical", "STUDIO-PC", new HashSet<string> { "Vertical", "Vertical (studio-pc)" }));
var longName = new string('x', ProjectService.MaxNameLength);
Assert.True(ProjectTransplant.FreeName(longName, "STUDIO-PC", new HashSet<string> { longName }).Length <= ProjectService.MaxNameLength);
}
[Test]
public async Task ACutAfterTheCopyWasAskedForIsFinishedOnTheCopyFoundByItsName()
{
var h = await HomesAsync();
await using var _ = h.Fx;
TransplantStep? journal = null;
ValueTask Save(TransplantStep s)
{
journal = s;
return ValueTask.CompletedTask;
}
var cut = new ProjectTransplant { Step = s => { if (s == "created") throw new IOException("cut"); } };
await Assert.ThrowsAsync<IOException>(() => cut.CopyAsync(h.From, h.Source, h.To, "Vertical", null, Save, h.Fx.Ct));
Assert.Null(journal!.TargetId);
var copy = await new ProjectTransplant().CopyAsync(h.From, h.Source, h.To, "Vertical", journal, Save, h.Fx.Ct);
Assert.Count(1, (await h.To.ListAsync(h.Fx.Ct)).Where(p => !p.IsMain));
Assert.Equal("Vertical", copy.Name);
Assert.SequenceEqual(await HashesAsync(h.From, h.Source.Id, h.Fx.Ct), (await HashesAsync(h.To, copy.Id, h.Fx.Ct)).Skip(1));
}
[Test]
public async Task ACutResumedSendsNoVersionTwice()
{
var h = await HomesAsync();
await using var _ = h.Fx;
TransplantStep? journal = null;
ValueTask Save(TransplantStep s)
{
journal = s;
return ValueTask.CompletedTask;
}
var pushes = 0;
var cut = new ProjectTransplant { Step = s => { if (s == "pushed" && ++pushes == 2) throw new IOException("cut"); } };
await Assert.ThrowsAsync<IOException>(() => cut.CopyAsync(h.From, h.Source, h.To, "Vertical", null, Save, h.Fx.Ct));
var copy = await new ProjectTransplant().CopyAsync(h.From, h.Source, h.To, "Vertical", journal, Save, h.Fx.Ct);
Assert.Equal(journal!.TargetId, copy.Id);
var copied = await HashesAsync(h.To, copy.Id, h.Fx.Ct);
Assert.SequenceEqual(await HashesAsync(h.From, h.Source.Id, h.Fx.Ct), copied.Skip(1));
}
[Test]
public async Task ACopySomebodyTookIsLeftAloneAndANewNameIsTaken()
{
var h = await HomesAsync();
await using var _ = h.Fx;
TransplantStep? journal = null;
ValueTask Save(TransplantStep s)
{
journal = s;
return ValueTask.CompletedTask;
}
var cut = new ProjectTransplant { Step = s => { if (s == "pushed") throw new IOException("cut"); } };
await Assert.ThrowsAsync<IOException>(() => cut.CopyAsync(h.From, h.Source, h.To, "Vertical", null, Save, h.Fx.Ct));
var taken = journal!.TargetId!.Value;
// Somebody works on the copy meanwhile.
var theirs = ProjectServiceTests.Draft(await h.To.HeadAsync(taken, h.Fx.Ct), new ProjectSettings { HeroFrame = 0 }, h.ToFrames, "mine");
Assert.Equal(PushOutcome.Stored, (await h.To.PushAsync(taken, theirs, h.Fx.Ct)).Outcome);
var copy = await new ProjectTransplant().CopyAsync(h.From, h.Source, h.To, "Vertical", journal, Save, h.Fx.Ct);
Assert.NotEqual(taken, copy.Id);
Assert.Equal("Vertical (2)", copy.Name);
Assert.Equal(theirs.Id, (await h.To.HeadAsync(taken, h.Fx.Ct)).Id);
Assert.SequenceEqual(await HashesAsync(h.From, h.Source.Id, h.Fx.Ct), (await HashesAsync(h.To, copy.Id, h.Fx.Ct)).Skip(1));
}
[Test]
public async Task AHomeThatReadsAnOlderFormatGetsNothing()
{
var h = await HomesAsync();
await using var _ = h.Fx;
var needed = (await h.From.VersionsAfterAsync(h.Source.Id, 0, 100, h.Fx.Ct)).Max(v => int.Max(v.SettingsFormat, ProjectContent.FormatOf(v.SettingsJson)));
Assert.True(needed > 0);
var older = new OlderHome(h.To, needed - 1);
var before = (await h.To.ListAsync(h.Fx.Ct)).Count;
var refused = await Assert.ThrowsAsync<TransplantFormatException>(() =>
new ProjectTransplant().CopyAsync(h.From, h.Source, older, "Vertical", null, _ => ValueTask.CompletedTask, h.Fx.Ct));
Assert.Equal((needed, needed - 1), (refused.Needed, refused.Reads));
Assert.Equal(before, (await h.To.ListAsync(h.Fx.Ct)).Count);
}
/// <summary>A home that stores an older settings format than this Chronos writes (an older server).</summary>
private sealed class OlderHome(IProjectHome inner, int format) : IProjectHome
{
public int SettingsFormat => format;
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => inner.SequenceAsync(cancellationToken);
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => inner.ListAsync(cancellationToken);
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
inner.CreateAsync(name, fromVersionId, author, cancellationToken);
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => inner.HeadAsync(projectId, cancellationToken);
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
inner.GetVersionAsync(projectId, versionId, cancellationToken);
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
inner.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken);
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
inner.HistoryAsync(projectId, beforeNumber, limit, cancellationToken);
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
inner.PushAsync(projectId, version, cancellationToken);
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => inner.FramesAsync(cancellationToken);
}
}