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;
///
/// 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.
///
[Category("projects")]
public sealed class ProjectTransplantTests(TestContext context)
{
private sealed record Homes(ArchiveFixture Fx, ArchiveProjectHome From, ArchiveProjectHome To, SequenceProject Source, List FromFrames,
List ToFrames);
/// Two sequences; the first has "Vertical" with two versions after its first.
private async Task 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> 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();
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 { "Main" }));
Assert.Equal("Vertical (STUDIO-PC)", ProjectTransplant.FreeName("Vertical", "STUDIO-PC", new HashSet { "vertical" }));
Assert.Equal("Vertical (STUDIO-PC) (2)", ProjectTransplant.FreeName("Vertical", "STUDIO-PC", new HashSet { "Vertical", "Vertical (studio-pc)" }));
var longName = new string('x', ProjectService.MaxNameLength);
Assert.True(ProjectTransplant.FreeName(longName, "STUDIO-PC", new HashSet { 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(() => 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(() => 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(() => 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(() =>
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);
}
/// A home that stores an older settings format than this Chronos writes (an older server).
private sealed class OlderHome(IProjectHome inner, int format) : IProjectHome
{
public int SettingsFormat => format;
public Task SequenceAsync(CancellationToken cancellationToken) => inner.SequenceAsync(cancellationToken);
public Task> ListAsync(CancellationToken cancellationToken) => inner.ListAsync(cancellationToken);
public Task CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
inner.CreateAsync(name, fromVersionId, author, cancellationToken);
public Task HeadAsync(Guid projectId, CancellationToken cancellationToken) => inner.HeadAsync(projectId, cancellationToken);
public Task GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
inner.GetVersionAsync(projectId, versionId, cancellationToken);
public Task> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
inner.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken);
public Task> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
inner.HistoryAsync(projectId, beforeNumber, limit, cancellationToken);
public Task PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
inner.PushAsync(projectId, version, cancellationToken);
public Task FramesAsync(CancellationToken cancellationToken) => inner.FramesAsync(cancellationToken);
}
}