using Febe.Core.Domain; using Febe.Core.Export; using Febe.Tests.Export.Support; using Febe.Tests.Support; namespace Febe.Tests.Export; /// /// Destinations and «Esporta questo account» (spec §4.10 «Destinazioni»; decisions D1, D2, D8 and WP1.10 of tappa E2). /// [TestClass] public sealed class ExportDestinationsTests { public TestContext TestContext { get; set; } = null!; [TestMethod] public async Task TheAccountSwitchIsOnWhenNothingIsRecorded() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); Assert.IsTrue(ExportAccounts.IsEnabled(null)); Assert.IsNull(await bed.Db.Exports.GetAccountExportEnabledAsync(bed.Marta.Id, ct)); Assert.IsEmpty(await bed.Db.Exports.ListAccountExportSwitchesAsync(ct)); var before = await bed.Service.Destinations.ResolveAsync(bed.Marta.Id, ct); Assert.IsTrue(before.Enabled); Assert.IsFalse(before.Exports); // no folder yet await bed.GeneralAsync(ct: ct); var all = await bed.Service.Destinations.ResolveAllAsync(ct); Assert.IsTrue(all[bed.Marta.Id].Exports && all[bed.Luca.Id].Exports); Assert.IsTrue(all[bed.Marta.Id].Wants(ContentTypes.Stories)); Assert.IsEmpty(await bed.Db.Exports.ListAccountExportSwitchesAsync(ct)); // reading never writes a row } [TestMethod] public async Task TheSwitchOffIsRemembered() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); await bed.GeneralAsync(ct: ct); await bed.Service.Destinations.SetAccountEnabledAsync(bed.Marta.Id, false, ct); var again = new ExportDestinations(bed.Environment); // another service reads the database, not a cache var marta = await again.ResolveAsync(bed.Marta.Id, ct); Assert.IsFalse(marta.Enabled); Assert.IsTrue(marta.HasDestination); Assert.IsFalse(marta.Exports); Assert.IsFalse(marta.Wants(ContentTypes.PostPhotos)); Assert.IsTrue((await again.ResolveAllAsync(ct))[bed.Luca.Id].Exports); Assert.IsFalse((await bed.Db.Exports.ListAccountExportSwitchesAsync(ct))[bed.Marta.Id]); await again.SetAccountEnabledAsync(bed.Marta.Id, true, ct); Assert.IsTrue(await bed.Db.Exports.GetAccountExportEnabledAsync(bed.Marta.Id, ct)); // a row with «on» now Assert.IsTrue((await again.ResolveAsync(bed.Marta.Id, ct)).Exports); await Assert.ThrowsExactlyAsync(() => again.SetAccountEnabledAsync(9999, false, ct)); } [TestMethod] public async Task TheOwnFolderReplacesTheGeneralOne() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); var general = await bed.GeneralAsync(ct: ct); var own = await bed.OwnAsync(bed.Marta, "marta", ContentTypes.PostPhotos, ct); var all = await bed.Service.Destinations.ResolveAllAsync(ct); Assert.AreEqual(own.Id, all[bed.Marta.Id].Destination!.Id); Assert.IsTrue(all[bed.Marta.Id].IsOwn); Assert.IsFalse(all[bed.Marta.Id].Wants(ContentTypes.Stories)); // the own folder's types, not the general ones Assert.AreEqual(general.Id, all[bed.Luca.Id].Destination!.Id); Assert.IsFalse(all[bed.Luca.Id].IsOwn); Assert.AreEqual(own.Id, (await bed.Service.Destinations.ResolveAsync(bed.Marta.Id, ct)).Destination!.Id); Assert.AreEqual("marta", ExportDestinations.Label(own)); var second = await Assert.ThrowsExactlyAsync(() => bed.GeneralAsync(folder: bed.Temp.Combine("marta"), ct: ct)); StringAssert.StartsWith(second.Message, "C'è già una cartella generale"); Directory.CreateDirectory(bed.Temp.Combine("marta2")); var twice = await Assert.ThrowsExactlyAsync(() => bed.OwnAsync(bed.Marta, "marta2", ct: ct)); StringAssert.StartsWith(twice.Message, "@marta.demo ha già una cartella sua"); await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.CreateAsync( new DestinationSpec(ExportScope.Account, null, bed.Temp.Combine("marta2"), null, ContentTypes.All, null, null, null), ct)); // «Usa la cartella generale»: the own folder goes, the general one is back. await bed.Service.Destinations.RetireAsync(own.Id, ct); Assert.AreEqual(general.Id, (await bed.Service.Destinations.ResolveAsync(bed.Marta.Id, ct)).Destination!.Id); } [TestMethod] public async Task RetiringKeepsTheCopiesAndTheFiles() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); var post = await bed.PostAsync(bed.Marta, "910", 1, 2); var usb = await bed.GeneralAsync(ct: ct); var start = await bed.CopyAccountAsync(bed.Marta, ct: ct); var copies = await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct); Assert.HasCount(2, copies); var files = Fingerprint.Of(bed.Usb); Assert.HasCount(2, files); await bed.Service.Destinations.RetireAsync(usb.Id, ct); Fingerprint.AreEqual(files, Fingerprint.Of(bed.Usb), "the files stay"); var retired = (await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!; Assert.IsTrue(retired.IsRetired); foreach (var copy in copies) { Assert.AreEqual(ExportCopyState.Copied, (await bed.Db.Exports.GetCopyAsync(copy.Id, ct))!.State); } Assert.AreEqual(ExportRunState.Completed, (await bed.RunAsync(start.Runs.Single().Id, ct)).State); Assert.IsFalse((await bed.Service.Destinations.ResolveAsync(bed.Marta.Id, ct)).HasDestination); var status = await bed.Service.Destinations.StatusAsync(usb.Id, ct); Assert.AreEqual((false, "Questa cartella è stata tolta."), (status.Reachable, status.Problem)); Assert.AreEqual(2, status.Counts[ExportCopyState.Copied]); await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.UpdateAsync( usb.Id, new DestinationSpec(ExportScope.General, null, bed.Usb, "Altro", ContentTypes.All, null, null, null), ct)); await bed.Service.Destinations.RetireAsync(usb.Id, ct); // twice: nothing happens // Nothing is due any more: the plan says the files are not exported. var plan = await bed.Service.PlanAsync(new ExportJobRequest(new ExportSelection.Entries([post.Entry.Id])), ct); Assert.AreEqual((0, 2), (plan.Files, plan.NotExported)); // A new general folder on the same disk starts from nothing: the files there are adopted, never written twice. var back = await bed.GeneralAsync(ct: ct); var again = await bed.CopyAccountAsync(bed.Marta, ct: ct); Assert.IsTrue((await bed.ItemsAsync(again.Runs.Single().Id, ct)).All(i => i.State == ExportItemState.Adopted)); Fingerprint.AreEqual(files, Fingerprint.Of(bed.Usb), "adopted, not written"); Assert.AreNotEqual(usb.Id, back.Id); } [TestMethod] public async Task ChangingTheFolderRetiresAndAddsAndIsRefusedWithAnUnfinishedRun() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); var usb = await bed.GeneralAsync(ct: ct); var nas = bed.Temp.Combine("nas"); Directory.CreateDirectory(nas); DestinationSpec Spec(string path, string? label = null) => new(ExportScope.General, null, path, label, ContentTypes.PostPhotos | ContentTypes.Stories, null, null, null); // The same folder: the row changes in place. var renamed = await bed.Service.Destinations.UpdateAsync(usb.Id, Spec(bed.Usb + Path.DirectorySeparatorChar, " Disco nero "), ct); Assert.AreEqual(usb.Id, renamed.Destination.Id); Assert.AreEqual("Disco nero", (await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.Label); Assert.AreEqual(ContentTypes.PostPhotos | ContentTypes.Stories, (await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.ContentTypes); // Another folder, while a job writes to the old one: refused, nothing changes. var run = await bed.Db.Exports.CreateRunAsync(new ExportRun { DestinationId = usb.Id, State = ExportRunState.Interrupted, StartedAt = bed.Clock.Now }, ct); var refused = await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.UpdateAsync(usb.Id, Spec(nas), ct)); Assert.AreEqual(ExportDestinations.UnfinishedRunText, refused.Message); await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.RetireAsync(usb.Id, ct)); Assert.IsFalse((await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.IsRetired); Assert.AreEqual(usb.Id, (await bed.Service.Destinations.UpdateAsync(usb.Id, Spec(bed.Usb, "Solo il nome"), ct)).Destination.Id); // the label still changes // The job is gone: the old row is retired, a new one is added. await bed.Service.DiscardAsync(run.Id, ct); var moved = await bed.Service.Destinations.UpdateAsync(usb.Id, Spec(nas), ct); Assert.AreNotEqual(usb.Id, moved.Destination.Id); Assert.AreEqual(Path.GetFullPath(nas), moved.Destination.RootPath); Assert.IsTrue((await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.IsRetired); Assert.AreEqual(moved.Destination.Id, (await bed.Service.Destinations.ResolveAsync(bed.Marta.Id, ct)).Destination!.Id); Assert.HasCount(1, await bed.Db.Exports.ListDestinationsAsync(includeRetired: false, ct)); // A folder that cannot be a destination leaves the destination as it was. await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.UpdateAsync(moved.Destination.Id, Spec(bed.Temp.Combine("assente")), ct)); await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.UpdateAsync( moved.Destination.Id, new DestinationSpec(ExportScope.Account, bed.Marta.Id, nas, null, ContentTypes.All, null, null, null), ct)); await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.UpdateAsync(9999, Spec(nas), ct)); Assert.IsFalse((await bed.Db.Exports.GetDestinationAsync(moved.Destination.Id, ct))!.IsRetired); } [TestMethod] public async Task SchemesAreValidatedLikeTheLibraryOnes() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); DestinationSpec Spec(string? general, string? highlight = null, string? profile = null) => new(ExportScope.General, null, bed.Usb, null, ContentTypes.All, general, highlight, profile); var unknown = await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.CreateAsync(Spec(@"{anno}\{inventato}"), ct)); StringAssert.StartsWith(unknown.Message, "Schema: Segnaposto sconosciuto: {inventato}."); var collection = await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.CreateAsync(Spec(@"{anno}\{raccolta}\{codice}_{n}"), ct)); StringAssert.StartsWith(collection.Message, "Schema: {raccolta} si usa solo nello schema per le raccolte in evidenza."); var profile = await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.CreateAsync(Spec(null, null, @"{raccolta}\{codice}"), ct)); StringAssert.StartsWith(profile.Message, "Schema per le foto del profilo: {raccolta} si usa solo"); var brace = await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.CreateAsync(Spec(null, @"{raccolta\{codice}"), ct)); StringAssert.StartsWith(brace.Message, "Schema per le raccolte in evidenza: "); Assert.IsEmpty(await bed.Db.Exports.ListDestinationsAsync(includeRetired: true, ct)); // Warnings do not block, and come back with their prefix; blank schemes are «Come la libreria». var saved = await bed.Service.Destinations.CreateAsync(Spec(@"{anno}\{codice}", @"{account}\{data}_{codice}_{n}", " "), ct); CollectionAssert.Contains(saved.Warnings.ToList(), "Schema: Senza {n} i file di un post con più foto o video hanno lo stesso nome: Febe aggiunge un numero per distinguerli."); CollectionAssert.Contains(saved.Warnings.ToList(), "Schema per le raccolte in evidenza: Senza {raccolta} i contenuti delle diverse raccolte finiscono nella stessa cartella."); var stored = (await bed.Db.Exports.GetDestinationAsync(saved.Destination.Id, ct))!; Assert.AreEqual(@"{anno}\{codice}", stored.NamingScheme); Assert.IsNull(stored.ProfileNamingScheme); } [TestMethod] public async Task NoTypeIsRefused() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); foreach (var types in new[] { ContentTypes.None, (ContentTypes)64, ContentTypes.Stories | (ContentTypes)128 }) { var refused = await Assert.ThrowsExactlyAsync(() => bed.GeneralAsync(types, ct: ct), types.ToString()); StringAssert.StartsWith(refused.Message, "Scegli almeno un tipo di contenuto."); } var usb = await bed.GeneralAsync(ContentTypes.Tagged, ct: ct); await Assert.ThrowsExactlyAsync(() => bed.Service.Destinations.UpdateAsync( usb.Id, new DestinationSpec(ExportScope.General, null, bed.Usb, null, ContentTypes.None, null, null, null), ct)); Assert.AreEqual(ContentTypes.Tagged, (await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.ContentTypes); } [TestMethod] public async Task AutomaticIsStoredButNothingIsQueued() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); await bed.PostAsync(bed.Marta, "920", 1); var spec = new DestinationSpec(ExportScope.General, null, bed.Usb, null, ContentTypes.All, null, null, null, Automatic: true, OnlyNew: true); var saved = (await bed.Service.Destinations.CreateAsync(spec, ct)).Destination; var stored = (await bed.Db.Exports.GetDestinationAsync(saved.Id, ct))!; Assert.IsTrue(stored.Automatic); Assert.AreEqual(Make.T0, stored.AutomaticSince); // Tappa E2 does not act on it: no job, nothing written, nothing running. Assert.IsFalse(bed.Service.IsBusy); Assert.IsEmpty(await bed.Db.Exports.ListRunsAsync(ct)); Assert.IsEmpty(Directory.GetFileSystemEntries(bed.Usb)); // Still on: «da quando» is kept; off: it goes; on with «tutti»: no date. bed.Clock.Now = Make.T0.AddDays(3); await bed.Service.Destinations.UpdateAsync(saved.Id, spec with { Label = "Disco" }, ct); Assert.AreEqual(Make.T0, (await bed.Db.Exports.GetDestinationAsync(saved.Id, ct))!.AutomaticSince); await bed.Service.Destinations.UpdateAsync(saved.Id, spec with { Automatic = false }, ct); Assert.IsTrue((await bed.Db.Exports.GetDestinationAsync(saved.Id, ct)) is { Automatic: false, AutomaticSince: null }); await bed.Service.Destinations.UpdateAsync(saved.Id, spec with { OnlyNew = false }, ct); Assert.IsTrue((await bed.Db.Exports.GetDestinationAsync(saved.Id, ct)) is { Automatic: true, AutomaticSince: null }); await bed.Service.Destinations.UpdateAsync(saved.Id, spec with { Automatic = false }, ct); await bed.Service.Destinations.UpdateAsync(saved.Id, spec, ct); Assert.AreEqual(Make.T0.AddDays(3), (await bed.Db.Exports.GetDestinationAsync(saved.Id, ct))!.AutomaticSince); Assert.IsEmpty(await bed.Db.Exports.ListRunsAsync(ct)); } [TestMethod] public async Task AnAccountFolderInsideADestinationIsRefusedOnRecheck() { var ct = TestContext.CancellationToken; await using var bed = await ExportTestBed.CreateAsync(); var usb = await bed.GeneralAsync(ct: ct); var own = await bed.OwnAsync(bed.Marta, "marta", ct: ct); Assert.IsEmpty(await bed.Service.Destinations.RecheckAsync(null, ct)); // The folder Alberto is about to give luca.demo sits inside the general folder. var planned = Path.Combine(bed.Usb, "luca"); var conflicts = await bed.Service.Destinations.RecheckAsync([planned], ct); var conflict = conflicts.Single(); Assert.AreEqual(usb.Id, conflict.Destination.Id); Assert.AreEqual(ExportFolderProblemKind.ContainsProtected, conflict.Problem.Kind); Assert.AreEqual($"{Path.GetFullPath(bed.Usb)} contiene la cartella di un account: scegli una cartella che non la contenga.", conflict.Problem.Message); Assert.IsFalse(Directory.Exists(planned)); // checked as text: the folder is not created // A new library root that would hold marta's own folder. var root = await bed.Service.Destinations.RecheckAgainstAsync([new(bed.Temp.Path, ProtectedFolderKind.Library, null)], ct); CollectionAssert.AreEquivalent(new[] { usb.Id, own.Id }, root.Select(c => c.Destination.Id).ToArray()); Assert.IsTrue(root.All(c => c.Problem.Kind == ExportFolderProblemKind.InsideProtected)); // After a rebuild the account folder really is there: the destination pauses, nothing is interactive. Directory.CreateDirectory(planned); await bed.Db.Accounts.UpdateAsync(bed.Luca with { Folder = planned }, ct); var paused = await bed.Service.Destinations.RecheckAndPauseAsync(ct); Assert.AreEqual(usb.Id, paused.Single().Destination.Id); var row = (await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!; Assert.IsNotNull(row.PausedAt); StringAssert.Contains(row.LastError, "contiene la cartella dell'account @luca.demo"); Assert.IsNull((await bed.Db.Exports.GetDestinationAsync(own.Id, ct))!.PausedAt); Assert.IsFalse(row.IsRetired); } }