ファイル

220 行
8.8 KiB
C#

using System.Text.Json;
using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Project;
using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
public sealed class UndoTreeBranchRecordTests
{
const string OpenHash = "H0";
static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root, bool withReader = true)
=> new(manager, new AsyncAwaitStatus(), [root], 100, withReader ? UndoRedoManagerReader.Create(manager) : null);
static UndoTreeRecord RoundTrip(UndoTreeHistory history)
=> JsonSerializer.Deserialize<UndoTreeRecord>(JsonSerializer.Serialize(history.ToRecord()))!;
static (UndoTreeRecord Record, IReadOnlyList<UndoTreeNodeViewModel> Nodes) Archived(UndoTreeRecord record)
{
var nodes = UndoTreeArchive.Restore(record);
nodes[0].ProjectHash = OpenHash;
return (record, nodes);
}
static async Task<(UndoTreeRecord Record, IReadOnlyList<UndoTreeNodeViewModel> Nodes)> Session()
{
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var branch = await history.CreateBranchAsync(history.Cursor);
history.RenameBranch(branch, "Trial");
Histories.Edit(manager, () => document.Y = 5);
Histories.Edit(manager, () => document.X = 2);
return Archived(RoundTrip(history));
}
[Fact]
public async Task TheRecordHoldsTheNameTheStartAndTheLastPositionOfEveryBranchAndWhichOneYouWereIn()
{
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var start = history.Cursor;
var branch = await history.CreateBranchAsync(start);
history.RenameBranch(branch, "Trial");
Histories.Edit(manager, () => document.Y = 5);
var record = RoundTrip(history);
var saved = Assert.Single(record.NamedBranches);
Assert.Equal(("Trial", start.Id, history.Cursor.Id), (saved.Name, saved.Base, saved.Head));
Assert.Equal(0, record.ActiveBranch);
Assert.Equal(history.Cursor.Id, record.CursorId);
}
[Fact]
public async Task TheLastPositionOfABranchYouLeftIsWhereYouLeftIt()
{
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var branch = await history.CreateBranchAsync(history.Cursor);
Histories.Edit(manager, () => document.Y = 5);
var last = history.Cursor;
Histories.Edit(manager, () => document.Y = 6);
await history.JumpAsync(last);
await history.SwitchToAsync(null);
var record = RoundTrip(history);
Assert.Equal(last.Id, Assert.Single(record.NamedBranches).Head);
Assert.Equal(-1, record.ActiveBranch);
Assert.NotNull(branch);
}
[Fact]
public void ARecordWrittenBeforeBranchesWereSavedStillLoads()
{
var record = JsonSerializer.Deserialize<UndoTreeRecord>("""{"SavedAt":"2026-11-01T00:00:00","Nodes":[],"CursorId":-1}""")!;
Assert.Empty(record.NamedBranches);
Assert.Equal(-1, record.ActiveBranch);
}
[Fact]
public async Task TheStartOfABranchIsMarkedInThePreviousHistory()
{
var (_, nodes) = await Session();
var marked = nodes.Where(node => node.BranchMarks.Length > 0).ToArray();
Assert.Equal(UndoTreeText.BranchMark("Trial"), Assert.Single(marked).BranchMarks);
Assert.StartsWith(UndoTreeText.BranchMark("Trial"), marked[0].DisplayText);
}
[Fact]
public async Task ABranchWhoseNodesAreMissingOrWhoseLastPositionIsNotBelowItsStartIsLeftOut()
{
var (record, nodes) = await Session();
var start = record.NamedBranches[0].Base;
var other = nodes[0].Id;
var broken = record with
{
Branches =
[
record.NamedBranches[0],
new UndoTreeRecordBranch("missing start", 999, record.NamedBranches[0].Head),
new UndoTreeRecordBranch("missing head", start, 999),
new UndoTreeRecordBranch("head above start", start, other),
],
};
var branches = UndoTreeArchive.BranchesOf(broken, nodes);
Assert.Equal("Trial", Assert.Single(branches).Name);
Assert.True(branches[0].WasActive);
}
[Fact]
public async Task ARestoredBranchHasItsStartAsTheLimitOfUndoAndEveryOperationAsItsOwnNode()
{
var (record, nodes) = await Session();
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
var branch = UndoTreeArchive.BranchesOf(record, nodes).Single();
var result = await history.RestoreBranchAsync(UndoTreeRestorePlan.For(nodes, branch.Base, OpenHash), branch.Name, branch.Chain, ConflictPolicy.Skip);
Assert.Equal(UndoTreeRestoreOutcome.Done, result.Restore.Outcome);
Assert.Equal(2, result.Applied!.Applied);
Assert.Equal((2, 5), (document.X, document.Y));
Assert.Equal("Trial", history.ActiveBranch!.Name);
Assert.Equal(4, history.Count);
await manager.UndoAsync();
await manager.UndoAsync();
Assert.False(manager.IsUndoable);
Assert.Equal((1, 0), (document.X, document.Y));
Assert.Same(history.ActiveBranch.Base, history.Cursor);
}
[Fact]
public async Task ABranchThatStartedAtTheOpenStateIsRestoredAtTheRoot()
{
var (sourceManager, source) = Histories.NewDocument("p");
UndoTreeRecord record;
using (var original = Track(sourceManager, source))
{
await original.CreateBranchAsync(original.Root);
Histories.Edit(sourceManager, () => source.X = 3);
record = RoundTrip(original);
}
var (_, nodes) = Archived(record);
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
var branch = UndoTreeArchive.BranchesOf(record, nodes).Single();
var result = await history.RestoreBranchAsync(UndoTreeRestorePlan.For(nodes, branch.Base, OpenHash), branch.Name, branch.Chain, ConflictPolicy.Skip);
Assert.Equal(UndoTreeRestoreOutcome.Done, result.Restore.Outcome);
Assert.Equal(3, document.X);
Assert.Same(history.Root, history.ActiveBranch!.Base);
await manager.UndoAsync();
Assert.False(manager.IsUndoable);
Assert.Equal(0, document.X);
}
[Fact]
public async Task AStartThatCannotBeReachedRestoresNothingAndCreatesNoBranch()
{
var (record, nodes) = await Session();
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
var branch = UndoTreeArchive.BranchesOf(record, nodes).Single();
var result = await history.RestoreBranchAsync(UndoTreeRestorePlan.For(nodes, branch.Base, "other"), branch.Name, branch.Chain, ConflictPolicy.Skip);
Assert.Equal(UndoTreeRestoreOutcome.Unavailable, result.Restore.Outcome);
Assert.Null(result.Applied);
Assert.Empty(history.NamedBranches);
Assert.Equal((0, 0), (document.X, document.Y));
}
[Fact]
public async Task WithoutRewritableListsNothingIsRestoredAndTheStateIsLeftAlone()
{
var (record, nodes) = await Session();
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document, withReader: false);
var branch = UndoTreeArchive.BranchesOf(record, nodes).Single();
await Assert.ThrowsAsync<UndoTreeOperationException>(() => history.RestoreBranchAsync(UndoTreeRestorePlan.For(nodes, branch.Base, OpenHash), branch.Name, branch.Chain, ConflictPolicy.Skip));
Assert.Equal((0, 0), (document.X, document.Y));
Assert.Empty(history.NamedBranches);
}
[Fact]
public async Task ABranchTheUserWasInIsKeptButLeftWhenAPreviousOneIsRestored()
{
var (record, nodes) = await Session();
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 9);
var current = await history.CreateBranchAsync(history.Cursor);
var branch = UndoTreeArchive.BranchesOf(record, nodes).Single();
var plan = UndoTreeRestorePlan.For(nodes, branch.Base, OpenHash);
var restored = await history.RestoreBranchAsync(plan, branch.Name, branch.Chain, ConflictPolicy.Skip);
Assert.Equal(UndoTreeRestoreOutcome.Done, restored.Restore.Outcome);
Assert.Equal(2, history.NamedBranches.Count);
Assert.Contains(current, history.NamedBranches);
Assert.Equal("Trial", history.ActiveBranch!.Name);
Assert.Equal((2, 5), (document.X, document.Y));
}
}