334 行
12 KiB
C#
334 行
12 KiB
C#
using System.IO;
|
|
using YukkuriMovieMaker.Project;
|
|
using YukkuriMovieMaker.UndoRedo;
|
|
|
|
namespace UndoTree.Tests;
|
|
|
|
[Collection("Settings")]
|
|
public sealed class UndoTreeRecorderTests : IDisposable
|
|
{
|
|
const string ProjectA = @"C:\work\a.ymmp";
|
|
const string ProjectB = @"C:\work\b.ymmp";
|
|
|
|
readonly string directory = Path.Combine(Path.GetTempPath(), "UndoTreeTests", Guid.NewGuid().ToString("N"));
|
|
readonly bool originalSave = UndoTreeSettings.Default.SaveHistory;
|
|
string? path = ProjectA;
|
|
|
|
public UndoTreeRecorderTests() => UndoTreeSettings.Default.SaveHistory = true;
|
|
|
|
public void Dispose()
|
|
{
|
|
UndoTreeSettings.Default.SaveHistory = originalSave;
|
|
if (System.IO.Directory.Exists(directory))
|
|
System.IO.Directory.Delete(directory, true);
|
|
}
|
|
|
|
UndoTreeRecordStore Store => new(directory);
|
|
|
|
static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root)
|
|
=> new(manager, new AsyncAwaitStatus(), [root], 100, UndoRedoManagerReader.Create(manager));
|
|
|
|
UndoTreeRecorder Record(UndoTreeHistory history) => new(history, Store, () => path);
|
|
|
|
string MovedProject(string content)
|
|
{
|
|
var moved = Path.Combine(Path.GetTempPath(), $"undotree-moved-{Guid.NewGuid():N}.ymmp");
|
|
File.WriteAllText(moved, content);
|
|
return moved;
|
|
}
|
|
|
|
static UndoTreeRecord Hashed(string label, string hash)
|
|
=> new(DateTime.Now, [new UndoTreeRecordNode(0, -1, string.Empty, DateTime.MinValue, [], ProjectHash: hash), new UndoTreeRecordNode(1, 0, label, DateTime.Now, [])]);
|
|
|
|
[Fact]
|
|
public void AMovedProjectIsLinkedToTheHistoryOfItsOldPlaceByTheContentOfTheFile()
|
|
{
|
|
var moved = MovedProject("content of the project");
|
|
try
|
|
{
|
|
Store.Save(ProjectA, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, ProjectA, null, Hashed("before moving", UndoTreeProject.HashOf(moved)!)));
|
|
path = moved;
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
|
|
Assert.True(recorder.HasPrevious());
|
|
Assert.Equal(["before moving"], recorder.Previous()!.Nodes.Skip(1).Select(node => node.Label));
|
|
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
recorder.Flush();
|
|
var written = Store.Load(moved)!;
|
|
|
|
Assert.Equal(["before moving"], written.Previous!.Nodes.Skip(1).Select(node => node.Label));
|
|
Assert.Equal(["Document / X"], written.Current!.Nodes.Skip(1).Select(node => node.Label));
|
|
Assert.NotNull(Store.Load(ProjectA));
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(moved);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AProjectWhoseContentMatchesNoRecordHasNoPreviousHistory()
|
|
{
|
|
var moved = MovedProject("content nobody recorded");
|
|
try
|
|
{
|
|
Store.Save(ProjectA, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, ProjectA, null, Hashed("other", "SOMETHING ELSE")));
|
|
path = moved;
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
|
|
Assert.False(recorder.HasPrevious());
|
|
Assert.Null(recorder.Previous());
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(moved);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AnEmptyRecordOfThePathItselfIsNotReplacedByALinkByContent()
|
|
{
|
|
var moved = MovedProject("content of the project");
|
|
try
|
|
{
|
|
Store.Save(ProjectA, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, ProjectA, null, Hashed("elsewhere", UndoTreeProject.HashOf(moved)!)));
|
|
Store.Save(moved, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, moved, null, new UndoTreeRecord(DateTime.Now, [new UndoTreeRecordNode(0, -1, string.Empty, DateTime.MinValue, [])])));
|
|
path = moved;
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
|
|
Assert.Null(recorder.Previous());
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(moved);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void AProjectThatChangesIsLookedUpAgain()
|
|
{
|
|
var moved = MovedProject("content nobody recorded");
|
|
try
|
|
{
|
|
path = moved;
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
Assert.False(recorder.HasPrevious());
|
|
|
|
File.WriteAllText(moved, "now a project that was recorded elsewhere");
|
|
Store.Save(ProjectA, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, ProjectA, null, Hashed("recorded", UndoTreeProject.HashOf(moved)!)));
|
|
|
|
Assert.True(recorder.HasPrevious());
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(moved);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void TheRecordOfThePathItselfWinsOverALinkByContent()
|
|
{
|
|
var moved = MovedProject("content of the project");
|
|
try
|
|
{
|
|
var hash = UndoTreeProject.HashOf(moved)!;
|
|
Store.Save(ProjectA, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, ProjectA, null, Hashed("elsewhere", hash)));
|
|
Store.Save(moved, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, moved, null, Hashed("here", "ANOTHER")));
|
|
path = moved;
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
|
|
Assert.Equal(["here"], recorder.Previous()!.Nodes.Skip(1).Select(node => node.Label));
|
|
}
|
|
finally
|
|
{
|
|
File.Delete(moved);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void TheCurrentHistoryIsWrittenUnderTheProjectOfTheMoment()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
Histories.Edit(manager, () => document.Y = 2);
|
|
|
|
recorder.Flush();
|
|
var file = Store.Load(ProjectA);
|
|
|
|
Assert.NotNull(file);
|
|
Assert.Equal(ProjectA, file.ProjectPath);
|
|
Assert.Null(file.Previous);
|
|
Assert.Equal(["Document / X", "Document / Y"], file.Current!.Nodes.Skip(1).Select(node => node.Label));
|
|
Assert.Equal(-1, file.Current.Nodes[0].Parent);
|
|
Assert.Equal(file.Current.Nodes.Take(2).Select(node => node.Id), file.Current.Nodes.Skip(1).Select(node => node.Parent));
|
|
}
|
|
|
|
[Fact]
|
|
public void NothingIsWrittenWhileSavingIsOffOrTheProjectIsNotSaved()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
|
|
UndoTreeSettings.Default.SaveHistory = false;
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
recorder.Flush();
|
|
Assert.False(Store.Exists(ProjectA));
|
|
|
|
UndoTreeSettings.Default.SaveHistory = true;
|
|
path = null;
|
|
Histories.Edit(manager, () => document.X = 2);
|
|
recorder.Flush();
|
|
Assert.Equal(0, Store.Usage().Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void ThePreviousSessionBecomesThePreviousRecordOfTheNewOne()
|
|
{
|
|
var store = Store;
|
|
store.Save(ProjectA, new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, ProjectA, null, new UndoTreeRecord(DateTime.Now, [new UndoTreeRecordNode(0, -1, string.Empty, DateTime.MinValue, []), new UndoTreeRecordNode(1, 0, "last time", DateTime.Now, [])])));
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
|
|
Assert.True(recorder.HasPrevious());
|
|
Assert.Equal("last time", recorder.Previous()!.Nodes[1].Label);
|
|
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
recorder.Flush();
|
|
var file = store.Load(ProjectA)!;
|
|
|
|
Assert.Equal("last time", file.Previous!.Nodes[1].Label);
|
|
Assert.Equal("Document / X", file.Current!.Nodes[1].Label);
|
|
Assert.Equal("last time", recorder.Previous()!.Nodes[1].Label);
|
|
}
|
|
|
|
[Fact]
|
|
public void SeveralSavesOfOneSessionKeepTheSamePreviousRecord()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
recorder.Flush();
|
|
Histories.Edit(manager, () => document.X = 2);
|
|
recorder.Flush();
|
|
|
|
var file = Store.Load(ProjectA)!;
|
|
|
|
Assert.Null(file.Previous);
|
|
Assert.Equal(3, file.Current!.Nodes.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void ClearingTheHistoryStartsANewSessionWhoseNeighborIsTheOldOne()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
recorder.Flush();
|
|
|
|
manager.Clear();
|
|
manager.Subscribe(document);
|
|
Assert.Equal(1, history.Count);
|
|
Histories.Edit(manager, () => document.Y = 2);
|
|
recorder.Flush();
|
|
var file = Store.Load(ProjectA)!;
|
|
|
|
Assert.Equal("Document / X", file.Previous!.Nodes[1].Label);
|
|
Assert.Equal("Document / Y", file.Current!.Nodes[1].Label);
|
|
}
|
|
|
|
[Fact]
|
|
public void SavingUnderANewNameContinuesTheHistoryThereAndLeavesTheOldFileAlone()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
recorder.Flush();
|
|
|
|
path = ProjectB;
|
|
Histories.Edit(manager, () => document.Y = 2);
|
|
recorder.Flush();
|
|
|
|
var old = Store.Load(ProjectA)!;
|
|
var renamed = Store.Load(ProjectB)!;
|
|
Assert.Equal(2, old.Current!.Nodes.Count);
|
|
Assert.Null(renamed.Previous);
|
|
Assert.Equal(3, renamed.Current!.Nodes.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void TheFirstSaveOfASessionPrunesOldFiles()
|
|
{
|
|
var store = Store;
|
|
store.Save(@"C:\work\ancient.ymmp", new UndoTreeRecordFile(UndoTreeRecordFile.CurrentVersion, @"C:\work\ancient.ymmp", null, new UndoTreeRecord(DateTime.Now, [])));
|
|
File.SetLastWriteTime(store.FileOf(@"C:\work\ancient.ymmp"), DateTime.Now.AddDays(-400));
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
recorder.Flush();
|
|
|
|
Assert.False(store.Exists(@"C:\work\ancient.ymmp"));
|
|
Assert.True(store.Exists(ProjectA));
|
|
}
|
|
|
|
[Fact]
|
|
public void DisposingTheRecorderSavesWhatIsStillPending()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
var recorder = Record(history);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
recorder.Dispose();
|
|
|
|
Assert.True(Store.Exists(ProjectA));
|
|
}
|
|
|
|
[Fact]
|
|
public void WithoutAProjectThereIsNoPreviousRecord()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
using var recorder = Record(history);
|
|
path = null;
|
|
|
|
Assert.False(recorder.HasPrevious());
|
|
Assert.Null(recorder.Previous());
|
|
}
|
|
|
|
[Fact]
|
|
public void TheRecordKeepsTheTreeShapeAndTheCommandsOfEveryNode()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
var record = history.ToRecord();
|
|
|
|
Assert.Equal(2, record.Nodes.Count);
|
|
Assert.Equal(-1, record.Nodes[0].Parent);
|
|
Assert.Equal(record.Nodes[0].Id, record.Nodes[1].Parent);
|
|
var command = Assert.Single(record.Nodes[1].Commands);
|
|
Assert.Equal((nameof(CommandKind.Property), "Document", "X", "0 → 1"), (command.Kind, command.Target, command.Member, command.Detail));
|
|
}
|
|
}
|