merge: review2-p5a-fix-merge into phase 5a fix round 1 integration
This commit is contained in:
commit
6542562428
12 files changed
+905
-38
No files matched your search
@@ -0,0 +1,192 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json.Nodes;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Data.Projects;
|
||||
using Chronos.Data.Tests.Archiving;
|
||||
using Chronos.Data.Tests.Infrastructure;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Projects;
|
||||
|
||||
/// <summary>The merge of projects (owner review 2, R8): one photo under two names, and settings of a newer Chronos.</summary>
|
||||
public sealed class ProjectMergeTests
|
||||
{
|
||||
private static readonly DateTime Start = new(2026, 9, 28, 18, 0, 0);
|
||||
|
||||
private static FramePhoto Photo(int n) => new(n.ToString("x64", CultureInfo.InvariantCulture), 1000 + n, Start.AddSeconds(10 * n));
|
||||
|
||||
private static PendingFrame Waiting(FramePhoto photo, bool excluded = false, bool hero = false) =>
|
||||
new() { Sha256 = photo.Sha256, Size = photo.Size, CapturedAt = photo.CapturedAt, Excluded = excluded, Hero = hero };
|
||||
|
||||
[Test]
|
||||
public void EntriesOfOnePhotoMergeAsOneWithThePositionsOfBoth()
|
||||
{
|
||||
var frames = new FramePhotos([(0, Photo(1)), (1, Photo(2)), (2, Photo(3))]);
|
||||
var basis = new ProjectSettings();
|
||||
// A version stored with an exclusion and the hero of one photo in two entries (renamed alike since they were saved).
|
||||
var ours = new ProjectSettings { PendingFrames = [Waiting(Photo(2), excluded: true), Waiting(Photo(2), hero: true)] };
|
||||
var theirs = basis with { Stack = new StackSettings { Mode = StackSetting.Median } };
|
||||
|
||||
var merged = ProjectMerge.MergeWithConflicts(basis, ours, theirs);
|
||||
Assert.Empty(merged.Conflicts);
|
||||
var entry = Assert.NotNull(merged.Merged.PendingFrames).Single();
|
||||
Assert.True(entry.Excluded && entry.Hero);
|
||||
Assert.Equal(StackSetting.Median, merged.Merged.Stack.Mode);
|
||||
var placed = ProjectSettings.FromJson(ProjectContent.Place(merged.Merged.ToJson(), frames));
|
||||
Assert.SequenceEqual([1], placed.ExcludedFrames);
|
||||
Assert.Equal((int?)1, placed.HeroFrame);
|
||||
|
||||
// The same through the merge's other side, and when the other side changed the photo's flags.
|
||||
Assert.True(ProjectMerge.Merge(basis, theirs, ours).PendingFrames!.Single() is { Excluded: true, Hero: true });
|
||||
var unexcluded = new ProjectSettings { PendingFrames = [Waiting(Photo(2), hero: true)] };
|
||||
var both = ProjectMerge.MergeWithConflicts(ours, ours, unexcluded);
|
||||
Assert.Empty(both.Conflicts);
|
||||
Assert.True(both.Merged.PendingFrames!.Single() is { Excluded: false, Hero: true });
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APhotoNamedAlikeOnEverySideMergesAsOnePhoto()
|
||||
{
|
||||
// Saved here before the photo was hashed (by size and capture time) and stored at the home after (by SHA-256): once each
|
||||
// side names it as the frames hold it now, undoing the exclusion here is a change of this side only.
|
||||
var unhashed = new FramePhoto(null, Photo(2).Size, Photo(2).CapturedAt);
|
||||
var frames = new FramePhotos([(0, Photo(1)), (1, Photo(2)), (2, Photo(3))]);
|
||||
var basis = new ProjectSettings { PendingFrames = [Waiting(unhashed, excluded: true)] };
|
||||
var theirs = new ProjectSettings { PendingFrames = [Waiting(Photo(2), excluded: true)], Exposure = new ExposureSettings { ExposureOffsetEv = 0.7 } };
|
||||
var ours = new ProjectSettings();
|
||||
|
||||
ProjectSettings Named(ProjectSettings settings) => ProjectSettings.FromJson(ProjectContent.Rebind(ProjectContent.Canonical(settings.ToJson()), frames));
|
||||
var merged = ProjectMerge.MergeWithConflicts(Named(basis), Named(ours), Named(theirs));
|
||||
Assert.Empty(merged.Conflicts);
|
||||
Assert.Null(merged.Merged.PendingFrames);
|
||||
Assert.Equal(0.7, merged.Merged.Exposure.ExposureOffsetEv);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SettingsOfANewerChronosAreNeverMergedWithThisOnes()
|
||||
{
|
||||
var basis = new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 0.1 } };
|
||||
var newer = ProjectSettings.FromJson("""{ "version": 2, "exposure": { "exposureOffsetEv": 0.5 }, "grade": { "look": "film" } }""", allowNewer: true);
|
||||
var mine = basis with { Stack = new StackSettings { Mode = StackSetting.Median } };
|
||||
|
||||
// Both changed: refused, whichever side the newer one is, in a replay too.
|
||||
Assert.Throws<NewerSettingsException>(() => ProjectMerge.MergeWithConflicts(basis, mine, newer));
|
||||
Assert.Throws<NewerSettingsException>(() => ProjectMerge.Merge(basis, newer, mine));
|
||||
Assert.Throws<NewerSettingsException>(() => ProjectMerge.Replay(basis, [mine], newer));
|
||||
Assert.Throws<NewerSettingsException>(() => new ProjectMerge.Rebase(basis, newer).Apply(mine));
|
||||
|
||||
// A side that did not change takes the other's as they are: nothing is merged.
|
||||
var taken = ProjectMerge.MergeWithConflicts(basis, basis, newer);
|
||||
Assert.Empty(taken.Conflicts);
|
||||
Assert.True(taken.Merged.IsNewerFormat);
|
||||
Assert.Equal(newer.ToJson(), taken.Merged.ToJson());
|
||||
Assert.Equal(mine.ToJson(), ProjectMerge.Merge(newer, mine, newer).ToJson());
|
||||
Assert.Equal(basis.ToJson(), ProjectMerge.Merge(newer, newer, basis).ToJson());
|
||||
Assert.Equal(newer.ToJson(), ProjectMerge.Replay(basis, [basis], newer).Commits.Single().ToJson());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A newer Chronos sharing the archive (owner review 2, R8): what it writes when it stores a version.</summary>
|
||||
internal static class NewerChronos
|
||||
{
|
||||
/// <summary>
|
||||
/// A version of settings format <see cref="ProjectSettings.CurrentVersion"/> + 1 on top of <paramref name="head"/>, written
|
||||
/// as that Chronos's push writes it: the version file, then the record's head (the index adopts it from the sidecars).
|
||||
/// It holds a member this Chronos does not know (a grade) and an exposure offset of 1.5.
|
||||
/// </summary>
|
||||
public static ProjectVersion Push(ArchiveFixture fx, Sequence sequence, ProjectVersion head)
|
||||
{
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var settings = JsonNode.Parse(head.SettingsJson)!.AsObject();
|
||||
settings["version"] = ProjectSettings.CurrentVersion + 1;
|
||||
settings["grade"] = new JsonObject { ["look"] = "film" };
|
||||
settings["exposure"] = new JsonObject { ["exposureOffsetEv"] = 1.5 };
|
||||
var text = ProjectContent.Canonical(settings.ToJsonString());
|
||||
var version = new ProjectVersion
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
ProjectId = head.ProjectId,
|
||||
ParentId = head.Id,
|
||||
Number = head.Number + 1,
|
||||
Kind = ProjectVersionKind.Commit,
|
||||
ClientName = "a newer Chronos",
|
||||
SettingsJson = text,
|
||||
ContentHash = ProjectContent.Hash(text),
|
||||
SettingsFormat = ProjectSettings.CurrentVersion + 1,
|
||||
FramesVersion = head.FramesVersion,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
StoredAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
ProjectSidecars.WriteVersion(folder.VersionPath(head.ProjectId, version.Number, version.Id), version);
|
||||
var record = ProjectSidecars.ReadRecord(folder.ProjectRecordPath(head.ProjectId));
|
||||
ProjectSidecars.WriteRecord(folder.ProjectRecordPath(head.ProjectId), record with { Head = new ProjectHead(version.Id, version.Number), Updated = DateTimeOffset.UtcNow });
|
||||
return version;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Main's catch-up merge (owner review 2, R8): a mirror cut short meets settings written meanwhile.</summary>
|
||||
[Category("projects")]
|
||||
public abstract class MainMergeTests(TestContext context) : DatabaseTest(context)
|
||||
{
|
||||
private async Task<ArchiveFixture> FixtureAsync() => Own(await ArchiveFixture.CreateAsync(Context, database: await NewOptionsAsync(multiConnection: true)));
|
||||
|
||||
[Test]
|
||||
public async Task TheValuesTheMergeLeavesOutStayInMainsHistory()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, frames) = await ProjectServiceTests.UploadedAsync(fx, 4);
|
||||
var projects = fx.Service.Projects;
|
||||
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
||||
var first = await projects.HeadAsync(main.Id, fx.Ct);
|
||||
var pushed = (await projects.PushAsync(main.Id, ProjectServiceTests.Draft(first, new ProjectSettings
|
||||
{
|
||||
Stack = new StackSettings { Mode = StackSetting.Median },
|
||||
Exposure = new ExposureSettings { ExposureOffsetEv = 0.5 },
|
||||
}, frames), fx.Ct)).Version!;
|
||||
|
||||
// The push's mirror cut short, and another path writes an offset of its own and an exclusion meanwhile.
|
||||
var reverted = await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings());
|
||||
await fx.Db.Projects.SetMirroredAsync(main.Id, first.Id, reverted.SettingsVersion, fx.Ct);
|
||||
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Exposure = new ExposureSettings { ExposureOffsetEv = 1.25 }, ExcludedFrames = [3] });
|
||||
|
||||
// The pushed version wins where both changed; the other writer's settings are the version before the merge.
|
||||
var history = await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct);
|
||||
Assert.SequenceEqual([ProjectVersionKind.Merge, ProjectVersionKind.External, ProjectVersionKind.Commit, ProjectVersionKind.Imported], history.Select(v => v.Kind));
|
||||
Assert.Equal((history[1].Id, pushed.Id), (history[0].ParentId, history[1].ParentId));
|
||||
var external = ProjectSettings.FromJson(ProjectContent.Place(history[1].SettingsJson, ProjectServiceTests.Photos(frames)));
|
||||
Assert.Equal((1.25, StackSetting.None), (external.Exposure.ExposureOffsetEv, external.Stack.Mode));
|
||||
Assert.SequenceEqual([3], external.ExcludedFrames);
|
||||
var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
|
||||
Assert.Equal((0.5, StackSetting.Median), (settings.Exposure.ExposureOffsetEv, settings.Stack.Mode));
|
||||
Assert.SequenceEqual([3], settings.ExcludedFrames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ANewerChronosHeadOfMainIsNotMergedWithSettingsWrittenMeanwhile()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var (sequence, _) = await ProjectServiceTests.UploadedAsync(fx, 3);
|
||||
var projects = fx.Service.Projects;
|
||||
var main = await projects.MainAsync(sequence.Id, fx.Ct);
|
||||
var newer = NewerChronos.Push(fx, sequence, await projects.HeadAsync(main.Id, fx.Ct));
|
||||
await ProjectServiceTests.WriteSettingsAsync(fx, sequence.Id, new ProjectSettings { Stack = new StackSettings { Mode = StackSetting.Mean } });
|
||||
|
||||
// Both are left as they are: the newer head stays the head, the settings stay those written.
|
||||
Assert.Equal(newer.Id, (await projects.HeadAsync(main.Id, fx.Ct)).Id);
|
||||
Assert.Count(2, await projects.HistoryAsync(main.Id, cancellationToken: fx.Ct));
|
||||
var settings = ProjectSettings.FromJson((await fx.Db.Sequences.GetAsync(sequence.Id))!.SettingsJson!);
|
||||
Assert.Equal((StackSetting.Mean, 0.0), (settings.Stack.Mode, settings.Exposure.ExposureOffsetEv));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SqliteMainMergeTests(TestContext context) : MainMergeTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.SqliteFile;
|
||||
}
|
||||
|
||||
[RequiresEnvironment(PgTestServer.Variable)]
|
||||
public sealed class PostgresMainMergeTests(TestContext context) : MainMergeTests(context)
|
||||
{
|
||||
protected override Backend Backend => Backend.Postgres;
|
||||
}
|
||||
Reference in new issue
Block a user