using System.Globalization; using System.IO; using Titano.App.Import; using Titano.Core.Import; using Titano.Core.Localization; using Titano.Core.Model; using Titano.Data; using Titano.Data.Archiving; using Titano.Data.Jobs; using Titano.Testing; using static Titano.App.Tests.Import.ImportTestData; namespace Titano.App.Tests.Import; /// Card to archive and card to folder on real files and a real index, and the card-erase gate around them. [Category("import")] public sealed class ImportFlowTests(TestContext context) { private static readonly CardVolume Volume = new("3F2A9C11-EOS_DIGITAL-127865454592", 0x3F2A9C11, "EOS_DIGITAL", 127_865_454_592); [Test] public async Task AVerifiedArchiveImportUnlocksDeleteAndFormat() { Loc.SetCulture(CultureInfo.GetCultureInfo("en")); await using var db = await TitanoDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "titano.db")), context.Cancellation); var archives = new ArchiveService(db, new ArchiveServiceOptions { Owner = "test" }); var archive = await archives.AdoptAsync(context.CreateTempDirectory("archive"), "Main", context.Cancellation); var card = Card(context.CreateTempDirectory("card"), 10, Start, Start.AddHours(3), Start.AddDays(1)); var media = await archives.Imports.RegisterCardAsync(Volume, context.Cancellation); var destination = new ArchiveDestination(archives, archive); var choices = new ImportChoices(); var plan = await destination.PlanAsync(card, choices.ToOptions(), media.Id, null, context.Cancellation); Assert.Equal(3, plan.Sessions.Count); Assert.Equal(30, plan.CopyCount); var before = new CardActions(ImportDestinationKind.Archive, true, true, true, null, null, plan.FileCount); Assert.False(before.CanCheck, "no import, no erase"); Assert.False(before.CanDelete); Assert.False(before.CanFormat); Assert.Equal(Loc.L("Import.Blocked.NothingImported"), before.CheckBlocker); var unverified = await archives.PlanCardEraseAsync(archive.Id, media.Id, card, null, context.Cancellation); Assert.Empty(unverified.ToDelete); await using (var engine = new JobEngine(db, archives.CreateJobHandlers(), new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) })) { await engine.StartAsync(context.Cancellation); var job = await engine.EnqueueAsync(ArchiveJobs.Import(archive, plan, media.Id, choices.VerifyCopies), cancellationToken: context.Cancellation); await WaitDoneAsync(db, job.Id); } var outcome = await ImportOutcome.CheckAsync(plan, choices.VerifyCopies, async (item, ct) => (await db.Frames.FindBySizeAndCaptureAsync(archive.Id, item.Size, item.CapturedAt, ct)).Count > 0, context.Cancellation); Assert.Equal(30, outcome.Copied); Assert.True(outcome.IsComplete); var afterImport = new CardActions(ImportDestinationKind.Archive, true, true, true, outcome, null, plan.FileCount); Assert.True(afterImport.CanCheck); Assert.False(afterImport.CanDelete, "the dry run must come first"); Assert.Equal(Loc.L("Import.Blocked.NoDryRun"), afterImport.DeleteBlocker); var erase = await archives.PlanCardEraseAsync(archive.Id, media.Id, card, null, context.Cancellation); var ready = afterImport with { Erase = erase }; Assert.True(ready.CanDelete); Assert.True(ready.CanFormat); Assert.Equal(30, ready.DeleteCount); Assert.Equal("30 verified on the card ยท " + Infrastructure.Formatting.Bytes(ready.DeleteBytes), ready.EraseText); // A photo added to the card after the import is not verified: formatting would lose it. Assert.False((ready with { CardPhotos = 31 }).CanFormat); Assert.False((ready with { Outcome = outcome with { Verified = false } }).CanFormat); Assert.False((ready with { Destination = ImportDestinationKind.Folder }).CanDelete); Assert.False((ready with { IsInserted = false }).CanDelete); Assert.False((ready with { IsRemovable = false }).CanEject); var result = CardEraser.Execute(erase, context.Cancellation); Assert.Equal(30, result.Deleted); Assert.Empty(Directory.EnumerateFiles(Path.Combine(card, "DCIM", "100CANON"))); } [Test] public async Task AFolderImportCopiesWithoutAnIndexAndFindsItsOwnCopiesAgain() { Loc.SetCulture(CultureInfo.GetCultureInfo("en")); var card = Card(context.CreateTempDirectory("card"), 8, Start, Start.AddHours(2)); var target = context.CreateTempDirectory("folder"); var destination = new UnindexedDestination(ImportDestinationKind.Folder, target); var plan = await destination.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation); Assert.Equal(2, plan.Sessions.Count); Assert.Equal(16, plan.CopyCount); var reports = new List(); var outcome = await LocalImport.RunAsync(plan, new FolderImportSink(target), verify: true, new Progress(reports), context.Cancellation); Assert.Equal(16, outcome.Copied); Assert.True(outcome.IsComplete); Assert.True(outcome.Verified); Assert.NotEmpty(reports); foreach (var s in plan.Sessions) foreach (var item in s.Items) Assert.FileExists(Path.Combine(target, s.Folder, item.FileName)); var again = await LocalImport.RunAsync(await destination.PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation), new FolderImportSink(target), verify: true, null, context.Cancellation); Assert.Equal(0, again.Copied); Assert.Equal(16, again.Duplicates); Assert.False(new CardActions(ImportDestinationKind.Folder, false, true, true, outcome, null, 16).CanCheck); } [Test] public async Task TheIndexCheckReportsPlannedCopiesThatNeverArrived() { Loc.SetCulture(CultureInfo.GetCultureInfo("en")); var plan = Plan(null, null, Session(1, "f", Item(1, Start), Item(2, Start.AddSeconds(2)), Item(3, Start.AddSeconds(4), action: ImportAction.SkipImported))); var outcome = await ImportOutcome.CheckAsync(plan, true, (item, _) => Task.FromResult(!item.SourcePath.EndsWith("0002.CR2", StringComparison.Ordinal)), context.Cancellation); Assert.Equal(1, outcome.Copied); Assert.Equal(1, outcome.Skipped); Assert.Equal("DCIM/100CANON/IMG_0002.CR2", outcome.Failures.Single().SourcePath); Assert.False(outcome.IsComplete); } private async Task WaitDoneAsync(TitanoDatabase db, Guid jobId) { var until = DateTime.UtcNow.AddSeconds(60); while (DateTime.UtcNow < until) { var job = await db.Jobs.GetAsync(jobId, context.Cancellation); if (job?.State == JobState.Done) return; if (job?.State is JobState.Failed or JobState.Cancelled) Assert.Fail($"Import job ended {job.State}: {job.Error}\n{job.LogTail}"); await Task.Delay(25, context.Cancellation); } Assert.Fail("The import job did not finish"); } /// Collects reports synchronously (System.Progress posts to the thread pool). private sealed class Progress(List into) : IProgress { public void Report(ImportProgress value) => into.Add(value); } }