using Chronos.Core.Model; using Chronos.Data.Archiving; using Chronos.Data.Copies; using Chronos.Data.Tests.Archiving; using Chronos.Testing; namespace Chronos.Data.Tests.Copies; /// /// What a merge or an integration leaves in the archive's journal folder (owner review 6, #73): the maintenance pass keeps /// the journal a retry can use and clears the one no job will read again, as it does for conversions. /// [Category("archive")] public sealed class CopyJournalMaintenanceTests(TestContext context) { [Test] public async Task MaintenanceClearsTheJournalOfAMergeOrIntegrateNoJobWillReadAgain() { await using var fx = await ArchiveFixture.CreateAsync(context); var failed = Guid.CreateVersion7(); var done = Guid.CreateVersion7(); var cancelled = Guid.CreateVersion7(); var paused = Guid.CreateVersion7(); foreach (var (id, kind, state) in new[] { (failed, JobKind.MergeSequences, JobState.Failed), (done, JobKind.Integrate, JobState.Done), (cancelled, JobKind.MergeSequences, JobState.Cancelled), (paused, JobKind.Integrate, JobState.Paused), }) await fx.Db.Jobs.EnqueueAsync(new Job { Id = id, Kind = kind, State = state, Created = DateTimeOffset.UtcNow }, fx.Ct); // Deleted without a cancel: the index no longer knows the job. var deleted = Guid.CreateVersion7(); var recent = Guid.CreateVersion7(); const int Old = Housekeeping.CleanupSteps.DefaultHistoryDays + 1; string Write(string path, int daysOld) { Directory.CreateDirectory(Path.GetDirectoryName(path)!); File.WriteAllText(path, "{}"); File.SetLastWriteTimeUtc(path, DateTime.UtcNow.AddDays(-daysOld)); return path; } var failedJournal = Write(MergeJournal.PathFor(fx.Paths, failed), Old); var doneJournal = Write(IntegrateJournal.PathFor(fx.Paths, done), 0); var cancelledJournal = Write(MergeJournal.PathFor(fx.Paths, cancelled), 0); var pausedJournal = Write(IntegrateJournal.PathFor(fx.Paths, paused), Old); var deletedJournal = Write(MergeJournal.PathFor(fx.Paths, deleted), Old); var recentJournal = Write(IntegrateJournal.PathFor(fx.Paths, recent), 1); var other = Write(Path.Combine(fx.Paths.JournalDirectory, "merge-notes.json"), Old); var dry = await fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: true, 0, _ => ValueTask.CompletedTask, fx.Ct); Assert.SequenceEqual(new[] { doneJournal, cancelledJournal, deletedJournal }.Select(fx.Paths.ToRelative).Order(), dry.TemporaryFiles.Order()); Assert.FileExists(deletedJournal); await fx.Service.Maintenance.RunAsync(fx.Archive, dryRun: false, 0, _ => ValueTask.CompletedTask, fx.Ct); Assert.FileMissing(doneJournal); Assert.FileMissing(cancelledJournal); Assert.FileMissing(deletedJournal); // A retry still reads these: the failed and the paused job's, and one another host may still be writing. Assert.FileExists(failedJournal); Assert.FileExists(pausedJournal); Assert.FileExists(recentJournal); Assert.FileExists(other); } [Test] public void OnlyTheJournalsOfMergesAndIntegrationsAreRecognised() { var job = Guid.CreateVersion7(); Assert.Equal(job, ArchiveMaintenance.CopyJournalJob($"merge-{job:N}.json")); Assert.Equal(job, ArchiveMaintenance.CopyJournalJob($"integrate-{job:N}.json")); Assert.Null(ArchiveMaintenance.CopyJournalJob($"convert-{job:N}.json")); Assert.Null(ArchiveMaintenance.CopyJournalJob($"merge-{job:N}.json.tmp")); Assert.Null(ArchiveMaintenance.CopyJournalJob($"merge-{job:D}.json")); } }