Files
Chronos/tests/Chronos.Data.Tests/Projects/ProjectTransplantTests.cs
T
Alby96andClaude Opus 5.5 461dfcf4a3 feat(copies): integrate local and server, filtered transfers, project transplant (owner review 6, P3)
A local sequence and its server twin can now be integrated into one linked
pair by a resumable job instead of by hand. The planner runs the dry run of
design 5c: photo steps per policy (the wins policies decide only the Different
class; Only here and Only there always move), left out and AutoPhotos, last
copies, the one portable payload, projects copied across under free names,
space, rights and every block, and a fingerprint the job checks again.

The Integrate job keeps an atomic journal and resumes at each stage: link,
photos both ways through filtered transfers that never create or retarget a
server sequence (a 404 marks the pair remote gone), trash only once the winner
is present (locally through the photo trash inside the job, on the server at
the version its dry run read), the agreed details, then projects through
ProjectTransplant. Refusals are kept and reported; a failure or a cancel sets
the pair back to Linked or its previous state.

Linked uploads now file into their target when the server announces
upload-into. The plan context travels with the job so rights and names give
the same plan on the job side.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 11:24:18 +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);
}
}