using YukkuriMovieMaker.Plugin; using YukkuriMovieMaker.Project; using YukkuriMovieMaker.UndoRedo; namespace UndoTree.Tests; public sealed class UndoTreeHistoryTests { static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable document, bool withReader = true, int capacity = 100, bool preferReplay = false) => new(manager, new AsyncAwaitStatus(), [document], capacity, withReader ? UndoRedoManagerReader.Create(manager) : null) { PreferReplay = preferReplay }; static string[] Labels(IEnumerable nodes) => nodes.Select(node => node.IsRoot ? "root" : node.Label).ToArray(); [Fact] public void ARecordedEditBecomesTheCurrentNodeUnderTheRoot() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); Assert.Equal(2, history.Count); Assert.Equal("Document / X", history.Cursor.Label); Assert.Same(history.Root, history.Cursor.Parent); Assert.True(history.Cursor.IsCurrent); Assert.True(history.Root.IsOnPath); } [Fact] public void ThePartLabelNamesTheTargetOfAChange() { var (manager, document) = Histories.NewDocument(); var part = new Part("blur"); document.Parts.Add(part); manager.Record(); using var history = Track(manager, document); Histories.Edit(manager, () => part.Value = 7); Assert.Equal("blur / Value", history.Cursor.Label); Assert.Equal("0 → 7", history.Cursor.Commands[0].Detail); } [Fact] public void AnEditWithSeveralChangesIsLabelledWithTheFirstChangeAndTheRest() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => { document.X = 1; document.Y = 2; document.Parts.Add(new Part("p")); }); Assert.Equal(3, history.Cursor.Commands.Count); Assert.Equal(string.Format(Texts.OperationAndMore, history.Cursor.Commands[0].Text, 2), history.Cursor.Label); } [Fact] public void AListChangeIsDescribedByTheNumberOfItemsAddedAndRemoved() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.Parts.Add(new Part("p"))); Assert.Equal(CommandKind.ListItems, history.Cursor.Commands[0].Kind); Assert.Equal(string.Format(Texts.ListAdded, 1), history.Cursor.Label); } [Fact] public async Task UndoingMovesTheCursorBackAndKeepsTheUndoneNodeRedoable() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); var first = history.Cursor; Histories.Edit(manager, () => document.Y = 2); var second = history.Cursor; await history.UndoAsync(); Assert.Same(first, history.Cursor); Assert.True(second.IsRedoable); Assert.False(first.IsRedoable); } [Fact] public async Task RecordingAfterAnUndoKeepsTheDiscardedBranch() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); var first = history.Cursor; Histories.Edit(manager, () => document.Y = 2); var discarded = history.Cursor; await history.UndoAsync(); Histories.Edit(manager, () => document.Y = 9); Assert.Equal(2, first.Children.Count); Assert.Contains(discarded, first.Children); Assert.True(first.HasSideBranches); Assert.Same(history.Cursor, first.Primary); Assert.Equal([discarded], first.SideBranches); } [Fact] public void ClearingTheManagerResetsTheTree() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); manager.Clear(); Assert.Equal(1, history.Count); Assert.Same(history.Root, history.Cursor); } [Fact] public void TheChangedEventFiresOnceForAnEditAndNotForANoOp() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); var fired = 0; history.Changed += (_, _) => fired++; Histories.Edit(manager, () => document.X = 1); history.Reconcile(); Assert.Equal(1, fired); } [Fact] public void ABrandNewHistoryStartsFromTheExistingLines() { var (manager, document) = Histories.NewDocument(); Histories.Edit(manager, () => document.X = 1); Histories.Edit(manager, () => document.Y = 2); using var history = Track(manager, document); Assert.Equal(3, history.Count); Assert.Equal(UndoRedoHistorySource.Name, history.Source); Assert.True(history.IsSupported); } [Fact] public void WithoutTheReaderTheMirrorSupportsAHistoryThatStartedEmpty() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document, withReader: false); Histories.Edit(manager, () => document.X = 1); Assert.Equal(UndoRedoHistorySource.Events, history.Source); Assert.Equal(2, history.Count); Assert.False(history.CanReplay); } [Fact] public void WithoutTheReaderAHistoryThatAlreadyHasEditsIsUnsupportedUntilCleared() { var (manager, document) = Histories.NewDocument(); Histories.Edit(manager, () => document.X = 1); using var history = Track(manager, document, withReader: false); var before = history.IsSupported; manager.Clear(); Assert.False(before); Assert.True(history.IsSupported); } [Fact] public void ATagIsTrimmedNotifiedOnceAndKeptInTheRecord() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); var node = history.Cursor; var changes = 0; history.Changed += (_, _) => changes++; history.SetTag(node, " before rework "); history.SetTag(node, "before rework"); Assert.Equal("before rework", node.Tag); Assert.Equal(1, changes); Assert.Equal("[before rework] Document / X", node.DisplayText); Assert.Equal(["", "before rework"], history.ToRecord().Nodes.Select(item => item.Tag)); history.SetTag(node, string.Empty); Assert.Equal("Document / X", node.DisplayText); Assert.Equal(2, changes); } [Fact] public void TheCurrentPathIsNeverCutEvenWhenItIsLongerThanTheCapacity() { var (manager, document) = Histories.NewDocument(); using var history = new UndoTreeHistory(manager, new AsyncAwaitStatus(), [document], 3, UndoRedoManagerReader.Create(manager)); for (var index = 1; index <= 6; index++) Histories.Edit(manager, () => document.X = index); Assert.Equal(7, history.Count); Assert.Equal(6, history.Cursor.Depth); } [Fact] public async Task ACapacityDropsTheOldestSideBranchFirst() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document, capacity: 5); Histories.Edit(manager, () => document.X = 1); var shared = history.Cursor; Histories.Edit(manager, () => document.Y = 1); var oldest = history.Cursor; await history.JumpAsync(shared); Histories.Edit(manager, () => document.Y = 2); var newer = history.Cursor; await history.JumpAsync(shared); Histories.Edit(manager, () => document.Y = 3); Histories.Edit(manager, () => document.X = 5); Assert.Equal(5, history.Count); Assert.DoesNotContain(oldest, shared.Children); Assert.Contains(newer, shared.Children); } [Fact] public async Task DroppingSideBranchesKeepsOnlyThePathAndTheRedoLine() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); var shared = history.Cursor; Histories.Edit(manager, () => document.Y = 1); await history.UndoAsync(); Histories.Edit(manager, () => document.Y = 2); var kept = history.Cursor; history.DropSideBranches(); Assert.Equal([kept], shared.Children); Assert.Equal(3, history.Count); } [Fact] public void TheHistoryOfAManagerIsSharedByEveryViewOfIt() { var (manager, document) = Histories.NewDocument(); var info = new TimelineToolInfo(null!, new Scenes(true), manager, new AsyncAwaitStatus()); var first = UndoTreeHistory.For(info); var second = UndoTreeHistory.For(info); Assert.Same(first, second); first.Dispose(); } [Fact] public void AnInjectedCommandOfAnUnknownTypeIsDescribedByItsTypeName() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); manager.AddCommand(new UnknownCommand()); manager.Record(); Assert.Equal(CommandKind.Other, history.Cursor.Commands[0].Kind); Assert.Equal(nameof(UnknownCommand), history.Cursor.Label); } [Fact] public async Task TheOwnersAreKeptWhileOnlyValuesChangeAndRebuiltWhenTheStructureChanges() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); var part = new Part("p"); Histories.Edit(manager, () => document.Parts.Add(part)); var first = history.Owners; Assert.Equal([document], first.OwnersOf(part)); Histories.Edit(manager, () => document.X = 1); Histories.Edit(manager, () => part.Value = 2); Assert.Same(first, history.Owners); var other = new Part("q"); Histories.Edit(manager, () => document.Parts.Add(other)); var second = history.Owners; Assert.NotSame(first, second); Assert.Equal([document], second.OwnersOf(other)); await manager.UndoAsync(); var third = history.Owners; Assert.NotSame(second, third); Assert.Empty(third.OwnersOf(other)); await manager.UndoAsync(); Assert.Same(third, history.Owners); } [Fact] public void ReplacingAListOrAChildObjectRebuildsTheOwners() { var (manager, sheet) = Histories.NewSheet(); using var history = Track(manager, sheet); var part = new Part("p"); Histories.Edit(manager, () => sheet.Parts = sheet.Parts.Add(part)); var first = history.Owners; Histories.Edit(manager, () => sheet.X = 1); Assert.Same(first, history.Owners); Assert.Equal([sheet], first.OwnersOf(part)); var replacement = new Part("r"); Histories.Edit(manager, () => sheet.Parts = [replacement]); Assert.NotSame(first, history.Owners); Assert.Equal([sheet], history.Owners.OwnersOf(replacement)); Assert.Empty(history.Owners.OwnersOf(part)); } [Fact] public async Task JumpingAcrossAStructuralStepRebuildsTheOwnersButNotAcrossValueSteps() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); var root = history.Cursor; var part = new Part("p"); Histories.Edit(manager, () => document.Parts.Add(part)); var added = history.Cursor; Histories.Edit(manager, () => document.X = 1); var edited = history.Cursor; var owners = history.Owners; await history.JumpAsync(added); Assert.Same(owners, history.Owners); await history.JumpAsync(root); Assert.NotSame(owners, history.Owners); Assert.Empty(history.Owners.OwnersOf(part)); var afterRoot = history.Owners; await history.JumpAsync(edited); Assert.NotSame(afterRoot, history.Owners); Assert.Equal([document], history.Owners.OwnersOf(part)); } [Fact] public void ClearingTheManagerDropsTheOwners() { var (manager, document) = Histories.NewDocument(); using var history = Track(manager, document); var part = new Part("p"); Histories.Edit(manager, () => document.Parts.Add(part)); var first = history.Owners; manager.Clear(); manager.Subscribe(document); Assert.NotSame(first, history.Owners); } [Fact] public void TheOwnersOfALargeGraphAreCompleteWhateverTheSpeedOfTheMachine() { var document = new Document(); var parts = Enumerable.Range(0, 60000).Select(index => new Part(index.ToString())).ToArray(); foreach (var part in parts) document.Parts.Add(part); var owners = new UndoTreeOwners([document]); Assert.All(parts, part => Assert.Equal([document], owners.OwnersOf(part))); } sealed class UnknownCommand : IUndoRedoCommandBase { public bool IsAction => false; public bool IsEmpty => false; public bool CanConnect(IUndoRedoCommandBase command) => false; public IUndoRedoCommandBase Connect(IUndoRedoCommandBase command) => throw new NotSupportedException(); } }