using YukkuriMovieMaker.Plugin; using YukkuriMovieMaker.Project; using YukkuriMovieMaker.UndoRedo; namespace UndoTree.Tests; public sealed class UndoTreeMoveTests { static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable document) => new(manager, new AsyncAwaitStatus(), [document], 100, UndoRedoManagerReader.Create(manager)); static async Task<(UndoTreeHistory History, UndoRedoManager Manager, Document Document, UndoTreeNodeViewModel A, UndoTreeNodeViewModel B, UndoTreeNodeViewModel C)> Forked() { var (manager, document) = Histories.NewDocument(); var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); var a = history.Cursor; Histories.Edit(manager, () => document.X = 2); var b = history.Cursor; await history.JumpAsync(a); Histories.Edit(manager, () => document.Y = 5); return (history, manager, document, a, b, history.Cursor); } [Fact] public async Task TheCreationOrderCrossesBranchesInTheOrderTheNodesWereMade() { var (history, _, _, a, b, c) = await Forked(); using var _ = history; Assert.Same(b, history.TargetOf(UndoTreeMove.PreviousCreated)); Assert.Null(history.TargetOf(UndoTreeMove.NextCreated)); await history.JumpAsync(b); Assert.Same(a, history.TargetOf(UndoTreeMove.PreviousCreated)); Assert.Same(c, history.TargetOf(UndoTreeMove.NextCreated)); await history.JumpAsync(a); Assert.Same(history.Root, history.TargetOf(UndoTreeMove.PreviousCreated)); Assert.Same(b, history.TargetOf(UndoTreeMove.NextCreated)); await history.JumpAsync(history.Root); Assert.Null(history.TargetOf(UndoTreeMove.PreviousCreated)); Assert.Same(a, history.TargetOf(UndoTreeMove.NextCreated)); } [Fact] public async Task TheNeighbouringBranchIsReachedAtItsEnd() { var (history, _, _, _, b, c) = await Forked(); using var _ = history; Assert.Same(b, history.TargetOf(UndoTreeMove.PreviousBranch)); Assert.Null(history.TargetOf(UndoTreeMove.NextBranch)); await history.JumpAsync(b); Assert.Null(history.TargetOf(UndoTreeMove.PreviousBranch)); Assert.Same(c, history.TargetOf(UndoTreeMove.NextBranch)); } [Fact] public async Task AForkAtTheCurrentNodeHasNoNeighbourToTheSide() { var (history, _, _, a, _, _) = await Forked(); using var _ = history; await history.JumpAsync(a); Assert.Null(history.TargetOf(UndoTreeMove.PreviousBranch)); Assert.Null(history.TargetOf(UndoTreeMove.NextBranch)); } [Fact] public async Task TheEndOfTheNeighbouringBranchFollowsTheLastVisitedPath() { var (history, manager, document, _, b, c) = await Forked(); using var _ = history; await history.JumpAsync(b); Histories.Edit(manager, () => document.Parts.Add(new Part("p2"))); var d = history.Cursor; await history.JumpAsync(b); Histories.Edit(manager, () => document.Parts.Add(new Part("p3"))); var e = history.Cursor; await history.JumpAsync(d); await history.JumpAsync(c); Assert.Same(d, history.TargetOf(UndoTreeMove.PreviousBranch)); await history.JumpAsync(e); await history.JumpAsync(c); Assert.Same(e, history.TargetOf(UndoTreeMove.PreviousBranch)); } [Fact] public async Task InsideANamedBranchTheMovesStayInsideItsSubtree() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); var one = history.Cursor; Histories.Edit(manager, () => document.X = 2); var two = history.Cursor; await history.JumpAsync(one); Histories.Edit(manager, () => document.Y = 9); var side = history.Cursor; await history.JumpAsync(two); await history.CreateBranchAsync(two); Histories.Edit(manager, () => document.X = 3); var three = history.Cursor; Assert.Same(two, history.TargetOf(UndoTreeMove.PreviousCreated)); Assert.Null(history.TargetOf(UndoTreeMove.NextCreated)); await history.JumpAsync(two); Assert.Null(history.TargetOf(UndoTreeMove.PreviousCreated)); Assert.Same(three, history.TargetOf(UndoTreeMove.NextCreated)); Assert.True(side.Id > two.Id && side.Id < three.Id); await history.JumpAsync(three); Assert.Null(history.TargetOf(UndoTreeMove.PreviousBranch)); Assert.Null(history.TargetOf(UndoTreeMove.NextBranch)); } }