diff --git a/UndoTree.Tests/UndoTreeCaptureTests.cs b/UndoTree.Tests/UndoTreeCaptureTests.cs index 450ee47..2b7fec3 100644 --- a/UndoTree.Tests/UndoTreeCaptureTests.cs +++ b/UndoTree.Tests/UndoTreeCaptureTests.cs @@ -287,10 +287,13 @@ public sealed class UndoTreeCaptureTests Assert.Equal(UndoTreeProject.HashOf(first), history.ToRecord().Nodes[0].ProjectHash); manager.Clear(); + manager.Subscribe(document); model.ProjectFilePath = second; Histories.Edit(manager, () => document.X = 2); - Assert.Equal(UndoTreeProject.HashOf(second), history.ToRecord().Nodes[0].ProjectHash); + var record = history.ToRecord(); + Assert.Equal(2, record.Nodes.Count); + Assert.Equal(UndoTreeProject.HashOf(second), record.Nodes[0].ProjectHash); } finally { @@ -299,6 +302,54 @@ public sealed class UndoTreeCaptureTests } } + [Fact] + public void TheRootHashIsTheFileAsItWasWhenTheFirstEditWasMadeEvenAfterASave() + { + var path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"undotree-c-{Guid.NewGuid():N}.ymmp"); + System.IO.File.WriteAllText(path, "opened"); + try + { + var (manager, document) = Histories.NewDocument(); + using var history = Track(manager, document); + history.Host = new UndoTreeHostModel(new HostModel(manager, path)); + var opened = UndoTreeProject.HashOf(path); + + Histories.Edit(manager, () => document.X = 1); + System.IO.File.WriteAllText(path, "saved after the edit"); + history.MarkSaved(); + + var nodes = history.ToRecord().Nodes; + Assert.Equal(opened, nodes[0].ProjectHash); + Assert.Equal(UndoTreeProject.HashOf(path), nodes[1].ProjectHash); + } + finally + { + System.IO.File.Delete(path); + } + } + + [Fact] + public void ATreeThatWasCreatedAfterTheEditsHasNoRootHash() + { + var path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"undotree-d-{Guid.NewGuid():N}.ymmp"); + System.IO.File.WriteAllText(path, "unknown state"); + try + { + var (manager, document) = Histories.NewDocument(); + Histories.Edit(manager, () => document.X = 1); + using var history = Track(manager, document); + history.Host = new UndoTreeHostModel(new HostModel(manager, path)); + + Histories.Edit(manager, () => document.X = 2); + + Assert.Equal(string.Empty, history.ToRecord().Nodes[0].ProjectHash); + } + finally + { + System.IO.File.Delete(path); + } + } + [Fact] public void TheHashOfAProjectFileIsItsSha256AndAMissingFileHasNone() { diff --git a/UndoTree/UndoTreeHistory.cs b/UndoTree/UndoTreeHistory.cs index 3ec3e9f..27e3759 100644 --- a/UndoTree/UndoTreeHistory.cs +++ b/UndoTree/UndoTreeHistory.cs @@ -27,6 +27,7 @@ internal class UndoTreeHistory : IDisposable UndoTreeOwners? owners; string? rootHash; bool rootTrimmed; + bool observing; bool ownersCurrent; internal UndoTreeHistory(UndoRedoManager manager, AsyncAwaitStatus asyncAwaitStatus, IReadOnlyList roots, int capacity, UndoRedoManagerReader? reader, Action? report = null) @@ -42,6 +43,7 @@ internal class UndoTreeHistory : IDisposable Cursor = Root; manager.HistoryChanged += Manager_HistoryChanged; Reconcile(); + observing = true; } public UndoTreeNodeViewModel Root { get; private set; } @@ -415,7 +417,7 @@ internal class UndoTreeHistory : IDisposable return new UndoTreeRecord(DateTime.Now, nodes.ToArray()); } - string RootHash() => rootHash ??= Nodes.Any(node => node.IsSaved) ? string.Empty : ProjectHash(); + string RootHash() => rootHash ?? string.Empty; public IReadOnlyList DiffFromCurrent(UndoTreeNodeViewModel node) => Diff(Cursor, node); @@ -545,6 +547,7 @@ internal class UndoTreeHistory : IDisposable } var changed = false; + var wasEmpty = Root.Children.Count == 0; var node = Root; for (var index = 0; index < undo.Count; index++) { @@ -570,6 +573,9 @@ internal class UndoTreeHistory : IDisposable node = child; } + if (wasEmpty && Root.Children.Count > 0 && observing) + rootHash ??= ProjectHash(); + if (undo.Count >= manager.Capacity) rootTrimmed = true;