using System.Globalization; using System.Security.Cryptography; using Chronos.Core.Localization; using Chronos.Core.Model; using Chronos.Data.Archiving; using Chronos.Data.Jobs; using Chronos.Data.Processing; using Chronos.Data.Workspaces; using Chronos.Testing; namespace Chronos.Data.Tests.Workspaces; /// /// Moving the workspace folder with its projects (phase 5c, P6): renamed on one volume, copied and hash-verified across /// volumes (one disk told it is two), a damaged copy that never costs the source, a move stopped and taken up from its /// journal, the refusals of the plan, a failure undone, and the job that carries it out. /// [Category("workspace")] public sealed class WorkspaceMoveTests(TestContext context) { static WorkspaceMoveTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); [Test] public async Task OnOneVolumeEveryProjectIsRenamedAndTheDesktopToldOnce() { var (from, ids) = Workspace(projects: 2); File.WriteAllText(Path.Combine(from, "notes.txt"), "the user's own"); var before = Contents(from); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); var mover = new WorkspaceMover(from, to); var survey = mover.Survey(); Assert.True(survey.CanMove, survey.RefusalText); Assert.True(survey.SameVolume, "both folders lie in the test's own temporary folder"); Assert.SequenceEqual([.. ids.Select(i => i.ToString("N")).Order(StringComparer.Ordinal), WorkspaceStore.ManifestFile, ".trash"], survey.Entries); Assert.Equal(before.Keys.Count(k => k != "notes.txt" && !IsLease(k)), survey.Files, "the leases and the user's file are not counted"); Assert.True(survey.Projects.All(p => p.IsProject)); var told = new List<(string, string)>(); var log = new List(); var (_, resumed) = await mover.MoveAsync(survey.Fingerprint, (f, t) => { told.Add((f, t)); return Task.CompletedTask; }, log: log.Add, cancellationToken: context.Cancellation); Assert.False(resumed); Assert.SequenceEqual([(mover.From, mover.To)], told); // Everything of the workspace is at the target as it was, the lease with its project; the user's file stays. var after = Contents(to); foreach (var (name, sha) in before.Where(b => b.Key != "notes.txt")) Assert.Equal(sha, after.GetValueOrDefault(name), name); Assert.SequenceEqual(["notes.txt"], Directory.EnumerateFileSystemEntries(from).Select(Path.GetFileName).ToList()); Assert.False(WorkspaceMover.HasJournal(from)); Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", mover.To, 2, survey.Files), log); Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count); } [Test] public async Task AcrossVolumesEveryFileIsCopiedAndCheckedBeforeTheSourceGoes() { var (from, ids) = Workspace(projects: 3); var before = Contents(from); var times = File.GetLastWriteTimeUtc(Path.Combine(from, ids[0].ToString("N"), WorkspaceProject.BundleFile)); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); var atSwitch = (Source: new Dictionary(), Target: new Dictionary()); var mover = new WorkspaceMover(from, to) { SameVolume = false, Cut = (step, _) => { if (step != "switched") return; atSwitch = (Contents(from), Contents(to)); }, }; var survey = mover.Survey(); Assert.False(survey.SameVolume); var told = 0; await mover.MoveAsync(survey.Fingerprint, (_, _) => { told++; // Told only once everything is at the target: the source is still whole. Assert.Equal(before.Count, Contents(from).Count); return Task.CompletedTask; }, cancellationToken: context.Cancellation); Assert.Equal(1, told); // At the switch both sides held everything (the lease is the project holder's: not copied). Assert.Equal(before.Count, atSwitch.Source.Count); foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, atSwitch.Target.GetValueOrDefault(name), name); // Then the source went, the journal last, and the folder with them. Assert.False(Directory.Exists(from)); Assert.Equal(times, File.GetLastWriteTimeUtc(Path.Combine(to, ids[0].ToString("N"), WorkspaceProject.BundleFile)), "the copy keeps the file's time"); Assert.Empty(Directory.EnumerateFiles(to, "*" + WorkspaceMover.PartSuffix, SearchOption.AllDirectories)); Assert.True(Directory.Exists(Path.Combine(to, ids[0].ToString("N"), "media", "empty")), "an empty folder comes along"); Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count); } [Test] public async Task ACopyThatDoesNotMatchItsSourceNeverCostsTheSource() { var (from, _) = Workspace(projects: 2); var before = Contents(from); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); var copies = 0; var mover = new WorkspaceMover(from, to) { SameVolume = false, Cut = (step, path) => { // The fourth file is damaged on its way to the target disk. if (step != "verify" || ++copies != 4) return; var bytes = File.ReadAllBytes(path); bytes[0] ^= 0xFF; File.WriteAllBytes(path, bytes); }, }; var told = 0; var failed = await Assert.ThrowsAsync(() => mover.MoveAsync(mover.Survey().Fingerprint, (_, _) => { told++; return Task.CompletedTask; }, cancellationToken: context.Cancellation)); Assert.True(failed.Message.StartsWith(Loc.L("Errors.Workspace.MoveVerify", "§").Split('§')[0], StringComparison.Ordinal), failed.Message); Assert.Equal(0, told); Same(before, Contents(from)); Assert.False(WorkspaceMover.HasJournal(from), "the move was undone"); Assert.False(Directory.Exists(to) && Directory.EnumerateFileSystemEntries(to).Any(), "no copy is left at the target"); } [Test] public async Task AMoveStoppedAcrossVolumesGoesOnFromItsJournalWithoutCopyingTwice() { var (from, _) = Workspace(projects: 3); var before = Contents(from); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation); var copied = 0; var first = new WorkspaceMover(from, to) { SameVolume = false, Cut = (step, _) => { // The program closes after the fifth file reached the target. if (step == "copied" && ++copied == 5) stop.Cancel(); }, }; var fingerprint = first.Survey().Fingerprint; await Assert.ThrowsAsync(() => first.MoveAsync(fingerprint, cancellationToken: stop.Token)); Assert.True(WorkspaceMover.HasJournal(from), "the journal stays for the next run"); Same(before, Contents(from)); Assert.Equal(WorkspaceMoveStage.Moving, WorkspaceMover.ReadJournal(from)!.Stage); // The workspace changed since (a lease taken and let go does not count): the move taken up does not ask the plan again. var again = 0; var second = new WorkspaceMover(from, to) { SameVolume = false, Cut = (step, _) => { if (step == "copied") again++; }, }; var told = 0; var log = new List(); var (_, resumed) = await second.MoveAsync("another plan", (_, _) => { told++; return Task.CompletedTask; }, log: log.Add, cancellationToken: context.Cancellation); Assert.True(resumed); Assert.Equal(1, told); var copiedFiles = before.Keys.Count(k => !IsLease(k)); Assert.Equal(copiedFiles - 5, again, "the files verified before the stop are not copied again"); Assert.True(log.Any(l => l.StartsWith(Loc.L("Archive.Log.WorkspaceMoveResumed", "§").Split('§')[0], StringComparison.Ordinal)), string.Join("; ", log)); var after = Contents(to); foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, after.GetValueOrDefault(name), name); Assert.False(Directory.Exists(from)); } [Test] public async Task AMoveStoppedOnOneVolumeGoesOnFromItsJournal() { var (from, ids) = Workspace(projects: 3); var before = Contents(from); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation); var first = new WorkspaceMover(from, to) { SameVolume = true, Cut = (step, _) => { if (step == "moved") stop.Cancel(); }, }; await Assert.ThrowsAsync(() => first.MoveAsync(first.Survey().Fingerprint, cancellationToken: stop.Token)); Assert.True(WorkspaceMover.HasJournal(from)); Assert.True(Directory.Exists(Path.Combine(to, ids.Select(i => i.ToString("N")).Order(StringComparer.Ordinal).First())), "the first project went"); var (_, resumed) = await new WorkspaceMover(from, to) { SameVolume = true }.MoveAsync("whatever", cancellationToken: context.Cancellation); Assert.True(resumed); Same(before, Contents(to)); Assert.False(Directory.Exists(from)); } [Test] public async Task AMoveThatFailsOnOneVolumeIsUndone() { var (from, _) = Workspace(projects: 3); var before = Contents(from); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); var moved = 0; var mover = new WorkspaceMover(from, to) { SameVolume = true, Cut = (step, _) => { // The second project's rename meets a file someone holds open. if (step == "moved" && ++moved == 2) throw new IOException("The file is in use."); }, }; await Assert.ThrowsAsync(() => mover.MoveAsync(mover.Survey().Fingerprint, cancellationToken: context.Cancellation)); Same(before, Contents(from)); Assert.False(WorkspaceMover.HasJournal(from)); Assert.False(Directory.Exists(to), "the target made for the move went with it"); } [Test] public async Task APlanIsRefusedForTheFolderItselfAFullTargetNoRoomAndAChangedWorkspace() { var (from, ids) = Workspace(projects: 1); Assert.Equal(WorkspaceMoveRefusal.SameFolder, new WorkspaceMover(from, from + Path.DirectorySeparatorChar).Survey().Refusal); var inside = Path.Combine(from, "inner"); var refused = new WorkspaceMover(from, inside).Survey(); Assert.Equal(WorkspaceMoveRefusal.SameFolder, refused.Refusal); Assert.Equal(Loc.L("Errors.Workspace.MoveSameFolder", refused.To), refused.RefusalText); // A target that is a workspace already, or holds a project of this one. var other = context.CreateTempDirectory("other"); _ = new WorkspaceStore(other); Assert.Equal(WorkspaceMoveRefusal.TargetNotEmpty, new WorkspaceMover(from, other).Survey().Refusal); var holding = context.CreateTempDirectory("holding"); Directory.CreateDirectory(Path.Combine(holding, ids[0].ToString("N"))); var full = new WorkspaceMover(from, holding).Survey(); Assert.Equal(Loc.L("Errors.Workspace.MoveTargetNotEmpty", full.To), full.RefusalText); // Unrelated files do not refuse it. var used = context.CreateTempDirectory("used"); File.WriteAllText(Path.Combine(used, "readme.txt"), "x"); Assert.True(new WorkspaceMover(from, used).Survey().CanMove); // Across volumes the target's drive must hold the workspace and the reserve; a rename needs no room. var small = new WorkspaceMover(from, Path.Combine(used, "w")) { SameVolume = false, FreeSpace = _ => 1000, Reserve = 0 }.Survey(); Assert.Equal((WorkspaceMoveRefusal?)WorkspaceMoveRefusal.NoRoom, small.Refusal); Assert.Equal(small.Bytes - 1000, small.Missing); Assert.True(new WorkspaceMover(from, Path.Combine(used, "w")) { SameVolume = true, FreeSpace = _ => 1000 }.Survey().CanMove); // A file written after the plan: the move refuses it and touches nothing. var to = Path.Combine(used, "moved"); var mover = new WorkspaceMover(from, to); var fingerprint = mover.Survey().Fingerprint; Assert.Equal(fingerprint, mover.Survey().Fingerprint, "the plan reads the same twice"); File.WriteAllText(Path.Combine(from, ids[0].ToString("N"), "versions", "late.json"), "{}"); var changed = await Assert.ThrowsAsync(() => mover.MoveAsync(fingerprint, cancellationToken: context.Cancellation)); Assert.Equal(Loc.L("Errors.Workspace.MoveChanged"), changed.Message); Assert.False(Directory.Exists(to)); Assert.False(WorkspaceMover.HasJournal(from)); } [Test] public async Task TheJobMovesTheWorkspaceAndTellsTheDesktop() { var (from, ids) = Workspace(projects: 2); var before = Contents(from); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation); var told = new List<(string, string)>(); var options = new WorkspaceJobOptions(new ProcessingOptions(), () => false, (f, t) => { told.Add((f, t)); return Task.CompletedTask; }); var handler = new WorkspaceMoveJobHandler(db, options) { Movers = p => new WorkspaceMover(p.From, p.To) { SameVolume = false } }; await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) }); // Another job of the workspace waits (nothing here runs it): the move is refused. var copy = await engine.EnqueueAsync(JobKind.WorkspaceCopy, [Guid.CreateVersion7()], "copy", null, cancellationToken: context.Cancellation); var survey = new WorkspaceMover(from, to).Survey(); await engine.StartAsync(context.Cancellation); var refused = await engine.EnqueueAsync(WorkspaceJobs.Move(from, to, survey.Fingerprint), cancellationToken: context.Cancellation); var failed = await WaitAsync(db, refused.Id); Assert.Equal(JobState.Failed, failed.State); Assert.Equal(Loc.L("Errors.Workspace.JobsRunning"), failed.Error); Same(before, Contents(from)); await engine.CancelAsync(copy.Id, context.Cancellation); var job = await engine.EnqueueAsync(WorkspaceJobs.Move(from, to, survey.Fingerprint), cancellationToken: context.Cancellation); var done = await WaitAsync(db, job.Id); Assert.Equal(JobState.Done, done.State, done.Error ?? done.LogTail); Assert.SequenceEqual([(survey.From, survey.To)], told); Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count); Assert.False(Directory.Exists(from)); Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", survey.To, 2, survey.Files), done.LogTail); } [Test] public async Task AMoveCancelledWhilePausedIsUndone() { var (from, _) = Workspace(projects: 2); var before = Contents(from); var to = Path.Combine(context.CreateTempDirectory("target"), "moved"); // A move stopped half way, as a pause leaves it. using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation); var mover = new WorkspaceMover(from, to) { SameVolume = true, Cut = (step, _) => stop.Cancel() }; var fingerprint = mover.Survey().Fingerprint; await Assert.ThrowsAsync(() => mover.MoveAsync(fingerprint, cancellationToken: stop.Token)); Assert.NotEqual(before.Count, Contents(from).Count); await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation); var handler = new WorkspaceMoveJobHandler(db, new WorkspaceJobOptions(new ProcessingOptions(), () => false)) { Movers = p => new WorkspaceMover(p.From, p.To) { SameVolume = true }, }; var request = WorkspaceJobs.Move(from, to, fingerprint); await using var engine = new JobEngine(db, [handler]); var job = await engine.EnqueueAsync(request, cancellationToken: context.Cancellation) with { State = JobState.Cancelled }; await handler.CancelledAsync(job, context.Cancellation); var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(20)); while (WorkspaceMover.HasJournal(from) && DateTime.UtcNow < deadline) await Task.Delay(20, context.Cancellation); Assert.False(WorkspaceMover.HasJournal(from)); Same(before, Contents(from)); Assert.False(Directory.Exists(to)); } /// /// A workspace with projects (a working state, media with an empty folder, versions, the /// lease), its manifest and a trash entry. /// private (string Root, List Ids) Workspace(int projects) { var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace"); _ = new WorkspaceStore(root); var ids = new List(); for (var i = 0; i < projects; i++) { var id = Guid.CreateVersion7(); ids.Add(id); var folder = Path.Combine(root, id.ToString("N")); Directory.CreateDirectory(Path.Combine(folder, "media", "proxies")); Directory.CreateDirectory(Path.Combine(folder, "media", "empty")); Directory.CreateDirectory(Path.Combine(folder, "versions")); File.WriteAllText(Path.Combine(folder, WorkspaceProject.BundleFile), "{\"project\": " + i + "}"); for (var f = 0; f < 3; f++) File.WriteAllBytes(Path.Combine(folder, "media", "proxies", $"{f}.jpg"), RandomNumberGenerator.GetBytes(4096 + f * 1000)); File.WriteAllText(Path.Combine(folder, "versions", "0001.json"), "{}"); File.WriteAllText(Path.Combine(folder, ".lock.lease"), "held by nobody"); } var trash = Path.Combine(root, ".trash", "removed-1"); Directory.CreateDirectory(trash); File.WriteAllText(Path.Combine(trash, "kept.json"), "{}"); return (root, ids); } /// Every file under by its relative name, with its SHA-256; a move's journal is not counted. private static Dictionary Contents(string root) => Directory.Exists(root) ? Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories).Where(f => Path.GetFileName(f) != WorkspaceMover.JournalFile) .ToDictionary(f => Path.GetRelativePath(root, f).Replace('\\', '/'), f => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(f)))) : []; /// Both hold the same files with the same contents. private static void Same(Dictionary expected, Dictionary actual) { Assert.SequenceEqual(expected.Keys.Order(StringComparer.Ordinal), actual.Keys.Order(StringComparer.Ordinal)); foreach (var (name, sha) in expected) Assert.Equal(sha, actual[name], name); } private static bool IsLease(string name) => name.EndsWith(".lease", StringComparison.Ordinal); private async Task WaitAsync(ChronosDatabase db, Guid jobId) { var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(60)); while (true) { var job = await db.Jobs.GetAsync(jobId, context.Cancellation); if (job is { State: JobState.Done or JobState.Failed or JobState.Cancelled }) return job; if (DateTime.UtcNow > deadline) Assert.Fail($"Job {jobId} still {job?.State}"); await Task.Delay(20, context.Cancellation); } } }