Le prove della tappa E2 (WP5a) coprono il montaggio di /esporta, la radice, il controllo delle cartelle, le destinazioni, i percorsi, la copia, gli stati «Tolto» e «Superato», lo stesso post in due account, il lavoro in sottofondo (interrompi, riprendi, annulla, riavvio, porta condivisa con importazione e ricostruzione, sessione del motore, interruttore dell'account, Docker senza montaggio) e il filtro «Copie». Si appoggiano a un banco comune con falsi per disco, sessione, montaggi e file system, senza permessi dei file, così girano anche da root. Le prove hanno trovato due difetti, corretti: - MarkCopiesRemovedAsync riusava il comando in cache del primo blocco di 900 id: «Controlla le copie» falliva con più di 900 file mancanti in una pagina. - ReplacedStatesSql senza statistiche leggeva tutte le copie tolte o superate della destinazione: «+state» la tiene sul suo indice. Il controllo delle cartelle accetta una scrittura di prova sostituibile, per provare il guasto della sonda senza permessi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
392 lines
22 KiB
C#
392 lines
22 KiB
C#
using System.Security.Cryptography;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Export;
|
|
using Febe.Core.Library;
|
|
using Febe.Tests.Export.Support;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Export;
|
|
|
|
/// <summary>
|
|
/// The copy job on real folders (spec §4.10 «Come si copia», §4.10 l.272-274; the E2 row: «nessuna sovrascrittura»,
|
|
/// «adozione», «radice mai creata»). The cases already in <see cref="ExportCopyJobTests"/> (account segment, «Tolto»,
|
|
/// adoption next to a taken name, missing root, sessions and space, FAT32, leftovers after a restart) are not repeated.
|
|
/// </summary>
|
|
[TestClass]
|
|
public sealed class ExportCopierTests
|
|
{
|
|
public TestContext TestContext { get; set; } = null!;
|
|
|
|
[TestMethod]
|
|
public async Task CopiesOriginalsByteForByteAndRecordsThem()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var post = await bed.Library.SaveAsync(bed.Marta, Make.Entry(bed.Marta.Id, "950", Make.T0.AddDays(-3)), [ExportTestBed.Photo(1, 21), TestFile.Video(2, 70_000)]);
|
|
var usb = await bed.GeneralAsync(ct: ct);
|
|
|
|
var start = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var items = await bed.ItemsAsync(start.Runs.Single().Id, ct);
|
|
Assert.HasCount(2, items);
|
|
foreach (var media in post.Media)
|
|
{
|
|
var item = items.Single(i => i.MediaId == media.Id);
|
|
Assert.AreEqual((ExportItemState.Copied, ExportVariant.Whole, ExportFormat.Original), (item.State, item.Variant, item.Format));
|
|
var copy = (await bed.Db.Exports.GetCopyAsync(item.CopyId!.Value, ct))!;
|
|
Assert.AreEqual((ExportCopyState.Copied, ExportCopyOrigin.Written), (copy.State, copy.Origin));
|
|
Assert.AreEqual((usb.Id, bed.Marta.Id, media.Id), (copy.DestinationId, copy.AccountId, copy.MediaId));
|
|
Assert.AreEqual((media.Sha256, media.Sha256, media.Bytes), (copy.SourceSha256, copy.CopySha256, copy.Bytes));
|
|
Assert.AreEqual(item.RelativePath, copy.RelativePath);
|
|
Assert.AreEqual("marta.demo/" + media.RelativePath, copy.RelativePath);
|
|
Assert.AreEqual(bed.Clock.Now, copy.CopiedAt);
|
|
|
|
var file = Path.Combine(bed.Usb, LibraryLayout.ToPlatform(copy.RelativePath!));
|
|
Assert.AreEqual(media.Sha256, Convert.ToHexStringLower(SHA256.HashData(await File.ReadAllBytesAsync(file, ct))));
|
|
Assert.AreEqual(post.Entry.PublishedAt.UtcDateTime, File.GetLastWriteTimeUtc(file)); // «libreria.data-file-pubblicazione» is on
|
|
}
|
|
|
|
Assert.IsEmpty(Fingerprint.Temporaries(bed.Usb));
|
|
var run = await bed.RunAsync(start.Runs.Single().Id, ct);
|
|
Assert.AreEqual((ExportRunState.Completed, ExportRunKind.Copy, ExportRunOrigin.Manual), (run.State, run.Kind, run.Origin));
|
|
var completed = (await bed.Db.Events.ListAsync(new() { Limit = 50 }, ct)).Single(e => e.Code == "esportazione-completata");
|
|
Assert.AreEqual(EventSeverity.Success, completed.Severity);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheFileTimeIsLeftAloneWhenTheLibrarySaysSo()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var post = await bed.PostAsync(bed.Marta, "951", 22);
|
|
await bed.Db.Settings.SetAsync(Core.Storage.SettingKeys.SetFileTimeToPublication, "false", ct);
|
|
await bed.GeneralAsync(ct: ct);
|
|
await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
Assert.AreNotEqual(post.Entry.PublishedAt.UtcDateTime, File.GetLastWriteTimeUtc(bed.GeneralCopy(bed.Marta, post.Media[0])));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheRootIsNeverCreatedWhenTheDiskGoesDuringAJob()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
var unplug = new Unplug();
|
|
await using var bed = await ExportTestBed.CreateAsync(env => env with { FileSystem = unplug });
|
|
var post = await bed.PostAsync(bed.Marta, "952", 23, 24);
|
|
var usb = await bed.GeneralAsync(ct: ct);
|
|
unplug.Root = bed.Usb;
|
|
unplug.When = path => path.EndsWith("_2.jpg", StringComparison.Ordinal); // the disk goes while the second file starts
|
|
|
|
var start = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var run = await bed.RunAsync(start.Runs.Single().Id, ct);
|
|
Assert.AreEqual(ExportRunState.Paused, run.State);
|
|
Assert.IsFalse(Directory.Exists(bed.Usb), "the root is never created again");
|
|
Assert.IsTrue(post.Media.All(m => File.Exists(bed.LibraryFile(bed.Marta, m))), "the library is untouched");
|
|
var items = (await bed.ItemsAsync(run.Id, ct)).ToDictionary(i => i.MediaId!.Value);
|
|
Assert.AreEqual(ExportItemState.Copied, items[post.Media[0].Id].State);
|
|
Assert.AreEqual(ExportItemState.Pending, items[post.Media[1].Id].State);
|
|
Assert.AreEqual(ExportCopyState.Copied, (await bed.Db.Exports.GetCopyAsync(items[post.Media[0].Id].CopyId!.Value, ct))!.State, "never «Tolto» because the disk went");
|
|
Assert.IsNotNull((await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.PausedAt);
|
|
|
|
// Plugged in again (empty): «Riprendi» writes the second file only.
|
|
unplug.When = _ => false;
|
|
Directory.CreateDirectory(bed.Usb);
|
|
await bed.Service.ResumeAsync([run.Id], ct);
|
|
await bed.Service.WaitAsync(ct);
|
|
Assert.AreEqual(ExportRunState.Completed, (await bed.RunAsync(run.Id, ct)).State);
|
|
Assert.HasCount(1, Fingerprint.Of(bed.Usb));
|
|
Assert.IsTrue(File.Exists(bed.GeneralCopy(bed.Marta, post.Media[1])));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AnIdenticalFileAtThePathIsAdoptedAndASameSizeDifferentFileIsNot()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var same = await bed.PostAsync(bed.Marta, "953", 25);
|
|
var other = await bed.PostAsync(bed.Marta, "954", 26);
|
|
await bed.GeneralAsync(ct: ct);
|
|
|
|
// An earlier copy by hand, with its own date, and a file of the same size but other bytes.
|
|
var adopted = bed.GeneralCopy(bed.Marta, same.Media[0]);
|
|
var foreign = bed.GeneralCopy(bed.Marta, other.Media[0]);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(adopted)!);
|
|
File.Copy(bed.LibraryFile(bed.Marta, same.Media[0]), adopted);
|
|
var handDate = new DateTime(2020, 5, 1, 12, 0, 0, DateTimeKind.Utc);
|
|
File.SetLastWriteTimeUtc(adopted, handDate);
|
|
var bytes = await File.ReadAllBytesAsync(bed.LibraryFile(bed.Marta, other.Media[0]), ct);
|
|
bytes[^3] ^= 0x01;
|
|
await File.WriteAllBytesAsync(foreign, bytes, ct);
|
|
var before = Fingerprint.Of(bed.Usb);
|
|
|
|
var start = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var items = (await bed.ItemsAsync(start.Runs.Single().Id, ct)).ToDictionary(i => i.MediaId!.Value);
|
|
var adoption = items[same.Media[0].Id];
|
|
Assert.AreEqual(ExportItemState.Adopted, adoption.State);
|
|
var copy = (await bed.Db.Exports.GetCopyAsync(adoption.CopyId!.Value, ct))!;
|
|
Assert.AreEqual((ExportCopyState.Copied, ExportCopyOrigin.Adopted), (copy.State, copy.Origin));
|
|
Assert.AreEqual(handDate, File.GetLastWriteTimeUtc(adopted), "adopted, not written again");
|
|
|
|
var written = items[other.Media[0].Id];
|
|
Assert.AreEqual((ExportItemState.Copied, ExportText.Notes.NameTaken), (written.State, written.Note));
|
|
StringAssert.EndsWith(written.RelativePath, " (2).jpg");
|
|
|
|
// Both files that were there are exactly as they were; one new file beside them.
|
|
var after = Fingerprint.Of(bed.Usb);
|
|
foreach (var (path, print) in before)
|
|
{
|
|
Assert.AreEqual(print, after[path], path);
|
|
}
|
|
|
|
Assert.AreEqual(before.Count + 1, after.Count);
|
|
Assert.IsTrue(after.ContainsKey(written.RelativePath!));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AnOrphanRowOnThePathIsRelinkedOnAdoption()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
await bed.PostAsync(bed.Marta, "955", 27);
|
|
await bed.GeneralAsync(ct: ct);
|
|
var first = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var copyId = (await bed.ItemsAsync(first.Runs.Single().Id, ct)).Single().CopyId!.Value;
|
|
|
|
// marta.demo is removed from Febe (her copy stays, without an account) and another account with that name comes:
|
|
// another Instagram id, so the copy does not come back by itself.
|
|
await bed.Db.Accounts.DeleteAsync(bed.Marta.Id, ct);
|
|
Directory.Delete(bed.Library.Layout.AccountFolder(bed.Marta), recursive: true);
|
|
Assert.IsNull((await bed.Db.Exports.GetCopyAsync(copyId, ct))!.AccountId);
|
|
var again = await bed.Db.Accounts.AddAsync(Make.Account("marta.demo", "999"), ct);
|
|
Assert.IsNull((await bed.Db.Exports.GetCopyAsync(copyId, ct))!.AccountId);
|
|
await bed.PostAsync(again, "955", 27); // the same post, the same bytes
|
|
|
|
var files = Fingerprint.Of(bed.Usb);
|
|
var start = await bed.CopyAccountAsync(again, ct: ct);
|
|
var item = (await bed.ItemsAsync(start.Runs.Single().Id, ct)).Single();
|
|
Assert.AreEqual((ExportItemState.Adopted, copyId), (item.State, item.CopyId));
|
|
var relinked = (await bed.Db.Exports.GetCopyAsync(copyId, ct))!;
|
|
Assert.AreEqual((again.Id, ExportCopyState.Copied), (relinked.AccountId, relinked.State));
|
|
Fingerprint.AreEqual(files, Fingerprint.Of(bed.Usb), "nothing written");
|
|
Assert.AreEqual(0, (await bed.Service.PlanAsync(new ExportJobRequest(new ExportSelection.Account(again.Id)), ct)).Files);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AResumedItemAdoptsItsOwnFileAfterACrash()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var post = await bed.PostAsync(bed.Marta, "956", 28);
|
|
var usb = await bed.GeneralAsync(ct: ct);
|
|
var planned = "marta.demo/" + post.Media[0].RelativePath;
|
|
|
|
// Before the crash: the planned name was taken by another file, ours was renamed «(2)», the record never came.
|
|
var taken = Path.Combine(bed.Usb, LibraryLayout.ToPlatform(planned));
|
|
Directory.CreateDirectory(Path.GetDirectoryName(taken)!);
|
|
await File.WriteAllBytesAsync(taken, [7, 7, 7], ct);
|
|
var ours = Path.Combine(bed.Usb, LibraryLayout.ToPlatform(ExportPaths.NumberedName(bed.Usb, planned, 2)));
|
|
File.Copy(bed.LibraryFile(bed.Marta, post.Media[0]), ours);
|
|
var run = await bed.Db.Exports.CreateRunAsync(new ExportRun { DestinationId = usb.Id, State = ExportRunState.Running, StartedAt = bed.Clock.Now }, ct);
|
|
await bed.Db.Exports.AddItemsAsync([new ExportItem { RunId = run.Id, MediaId = post.Media[0].Id, AccountId = bed.Marta.Id, SourceSha256 = post.Media[0].Sha256 }], ct);
|
|
var pending = (await bed.ItemsAsync(run.Id, ct)).Single();
|
|
await bed.Db.Exports.UpdateItemsAsync([pending with { RelativePath = planned }], ct);
|
|
var files = Fingerprint.Of(bed.Usb);
|
|
|
|
Assert.AreEqual(1, await bed.Service.RecoverAsync(ct));
|
|
await bed.Service.ResumeAsync(null, ct);
|
|
await bed.Service.WaitAsync(ct);
|
|
|
|
var item = (await bed.ItemsAsync(run.Id, ct)).Single();
|
|
Assert.AreEqual(ExportItemState.Adopted, item.State);
|
|
Assert.AreEqual(ExportPaths.NumberedName(bed.Usb, planned, 2), item.RelativePath);
|
|
Fingerprint.AreEqual(files, Fingerprint.Of(bed.Usb), "no second file, nothing overwritten");
|
|
Assert.AreEqual(ExportCopyOrigin.Adopted, (await bed.Db.Exports.GetCopyAsync(item.CopyId!.Value, ct))!.Origin);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ACorruptedReadIsNotRenamedAndAFailedCopyIsRetried()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
var corrupt = true;
|
|
await using var bed = await ExportTestBed.CreateAsync(env => env with { FileSystem = new CorruptingExportFileSystem(_ => Volatile.Read(ref corrupt)) });
|
|
var post = await bed.PostAsync(bed.Marta, "957", 29);
|
|
var usb = await bed.GeneralAsync(ct: ct);
|
|
|
|
var start = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var item = (await bed.ItemsAsync(start.Runs.Single().Id, ct)).Single();
|
|
Assert.AreEqual((ExportItemState.Failed, ExportText.Notes.SourceChanged), (item.State, item.Note));
|
|
Assert.AreEqual("Il file della libreria non corrisponde a quello registrato.", item.LastError);
|
|
Assert.IsEmpty(Fingerprint.Of(bed.Usb), "no final file, no .part left");
|
|
var failed = (await bed.Db.Exports.GetCopyAsync(item.CopyId!.Value, ct))!;
|
|
Assert.AreEqual(ExportCopyState.Failed, failed.State);
|
|
Assert.IsNull(failed.RelativePath);
|
|
Assert.AreEqual(ExportRunState.Completed, (await bed.RunAsync(start.Runs.Single().Id, ct)).State, "the job goes on");
|
|
Assert.AreEqual(EventSeverity.Warning, (await bed.Db.Events.ListAsync(new() { Limit = 50 }, ct)).Single(e => e.Code == "esportazione-completata").Severity);
|
|
Assert.AreEqual(new Core.Storage.ExportAccountCounts(1, 1, 0, 1), await bed.Db.Exports.CountAccountFilesAsync(bed.Marta.Id, ct));
|
|
|
|
// The cable is fixed: the next «Copia in…» tries again, on the same copy row.
|
|
var plan = await bed.Service.PlanAsync(new ExportJobRequest(new ExportSelection.Account(bed.Marta.Id)), ct);
|
|
Assert.AreEqual(1, plan.Files);
|
|
Volatile.Write(ref corrupt, false);
|
|
var again = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var retried = (await bed.ItemsAsync(again.Runs.Single().Id, ct)).Single();
|
|
Assert.AreEqual((ExportItemState.Copied, failed.Id), (retried.State, retried.CopyId));
|
|
var copy = (await bed.Db.Exports.GetCopyAsync(failed.Id, ct))!;
|
|
Assert.AreEqual((ExportCopyState.Copied, (string?)null), (copy.State, copy.LastError));
|
|
Assert.IsTrue(File.Exists(bed.GeneralCopy(bed.Marta, post.Media[0])));
|
|
Assert.HasCount(1, await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AFileMissingInTheLibraryIsReportedNotDownloaded()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var present = await bed.PostAsync(bed.Marta, "958", 30, 31);
|
|
var waiting = await bed.Db.Entries.AddAsync(Make.Entry(bed.Marta.Id, "959", Make.T0.AddDays(-2)), [Make.Photo(1)], ct); // still to download
|
|
await bed.GeneralAsync(ct: ct);
|
|
File.Delete(bed.LibraryFile(bed.Marta, present.Media[1])); // gone from the disk behind Febe's back
|
|
|
|
var request = new ExportJobRequest(new ExportSelection.Entries([present.Entry.Id, waiting.Stored.Entry.Id]));
|
|
var plan = await bed.Service.PlanAsync(request, ct);
|
|
Assert.AreEqual((2, 1), (plan.Files, plan.Missing)); // the plan reads the database only
|
|
|
|
var start = await bed.CopyAsync(request, ct);
|
|
var items = (await bed.ItemsAsync(start.Runs.Single().Id, ct)).ToDictionary(i => i.MediaId!.Value);
|
|
Assert.HasCount(2, items); // the file to download has no item: it is only counted
|
|
Assert.AreEqual(ExportItemState.Copied, items[present.Media[0].Id].State);
|
|
var missing = items[present.Media[1].Id];
|
|
Assert.AreEqual((ExportItemState.Missing, ExportText.Notes.SourceMissing), (missing.State, missing.Note));
|
|
Assert.AreEqual("File non presente in libreria", ExportText.Note(missing.Note!));
|
|
Assert.HasCount(1, Fingerprint.Of(bed.Usb));
|
|
Assert.IsFalse(File.Exists(bed.LibraryFile(bed.Marta, present.Media[1])), "nothing is downloaded or restored");
|
|
var report = await bed.Service.Reports.BuildAsync(start.Runs.Single().Id, ct);
|
|
Assert.AreEqual(1, report.ByState[ExportItemState.Missing]);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task NotEnoughSpacePausesTheDestinationAtTheMargin()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var post = await bed.PostAsync(bed.Marta, "960", 32);
|
|
var usb = await bed.GeneralAsync(ct: ct);
|
|
var bytes = post.Media[0].Bytes!.Value;
|
|
|
|
bed.Disk.Free = bytes + ExportSpace.Margin - 1;
|
|
var start = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var run = await bed.RunAsync(start.Runs.Single().Id, ct);
|
|
Assert.AreEqual(ExportRunState.Paused, run.State);
|
|
var paused = (await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!;
|
|
Assert.IsNotNull(paused.PausedAt);
|
|
Assert.AreEqual(run.LastError, paused.LastError);
|
|
StringAssert.Contains(paused.LastError, "margine di 64 MB");
|
|
Assert.IsEmpty(Fingerprint.Of(bed.Usb));
|
|
Assert.IsTrue((await bed.Service.PlanAsync(new ExportJobRequest(new ExportSelection.Account(bed.Marta.Id)), ct)).Destinations.Single().Paused);
|
|
|
|
bed.Disk.Free = bytes + ExportSpace.Margin; // exactly the margin is enough
|
|
await bed.Service.ResumeAsync([run.Id], ct);
|
|
await bed.Service.WaitAsync(ct);
|
|
Assert.AreEqual(ExportRunState.Completed, (await bed.RunAsync(run.Id, ct)).State);
|
|
Assert.IsNull((await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.PausedAt);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AProbeFailurePausesAndResumeCarriesOn()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
var probes = 0;
|
|
var failAt = -1;
|
|
await using var bed = await ExportTestBed.CreateAsync(env => env with
|
|
{
|
|
GroupFiles = 1, // a probe before every file
|
|
WriteProbe = path =>
|
|
{
|
|
if (Interlocked.Increment(ref probes) == Volatile.Read(ref failAt))
|
|
{
|
|
throw new IOException("Il supporto è protetto da scrittura.");
|
|
}
|
|
|
|
File.WriteAllText(path, "Febe");
|
|
},
|
|
});
|
|
var post = await bed.PostAsync(bed.Marta, "961", 33, 34);
|
|
var usb = await bed.GeneralAsync(ct: ct);
|
|
Volatile.Write(ref failAt, Volatile.Read(ref probes) + 2); // the probe before the second file fails
|
|
|
|
var start = await bed.CopyAccountAsync(bed.Marta, ct: ct);
|
|
var run = await bed.RunAsync(start.Runs.Single().Id, ct);
|
|
Assert.AreEqual(ExportRunState.Paused, run.State);
|
|
StringAssert.StartsWith(run.LastError, $"Febe non riesce a scrivere in {Path.GetFullPath(bed.Usb)}");
|
|
var items = (await bed.ItemsAsync(run.Id, ct)).ToDictionary(i => i.MediaId!.Value);
|
|
Assert.AreEqual(ExportItemState.Copied, items[post.Media[0].Id].State);
|
|
Assert.AreEqual(ExportItemState.Pending, items[post.Media[1].Id].State);
|
|
Assert.IsNotNull((await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.PausedAt);
|
|
var alert = (await bed.Db.Events.ListAsync(new() { Limit = 50 }, ct)).Where(e => e.Code == ExportPause.PausedEventCode).ToList();
|
|
Assert.HasCount(1, alert);
|
|
Assert.AreNotEqual(EventSeverity.Error, alert[0].Severity, "a pause, never the red strip");
|
|
|
|
await bed.Service.ResumeAsync(null, ct);
|
|
await bed.Service.WaitAsync(ct);
|
|
Assert.AreEqual(ExportRunState.Completed, (await bed.RunAsync(run.Id, ct)).State);
|
|
Assert.AreEqual(ExportItemState.Copied, (await bed.ItemsAsync(run.Id, ct)).Single(i => i.MediaId == post.Media[1].Id).State);
|
|
Assert.IsNull((await bed.Db.Exports.GetDestinationAsync(usb.Id, ct))!.PausedAt);
|
|
Assert.HasCount(2, Fingerprint.Of(bed.Usb));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ThePartFilesOfADiscardedRunAreCleanedOnlyInItsFolders()
|
|
{
|
|
var ct = TestContext.CancellationToken;
|
|
await using var bed = await ExportTestBed.CreateAsync();
|
|
var post = await bed.PostAsync(bed.Marta, "962", 35);
|
|
var usb = await bed.GeneralAsync(ct: ct);
|
|
var relative = "marta.demo/" + post.Media[0].RelativePath;
|
|
var folder = Path.GetDirectoryName(Path.Combine(bed.Usb, LibraryLayout.ToPlatform(relative)))!;
|
|
var elsewhere = bed.Temp.Combine("usb", "altro");
|
|
Directory.CreateDirectory(folder);
|
|
Directory.CreateDirectory(elsewhere);
|
|
var ours = Path.Combine(folder, ".febe-0123456789ab.part");
|
|
var notInAFolderOfItems = Path.Combine(elsewhere, ".febe-ba9876543210.part");
|
|
var alberto = Path.Combine(folder, "appunti.part"); // not one of our names
|
|
foreach (var file in new[] { ours, notInAFolderOfItems, alberto })
|
|
{
|
|
await File.WriteAllBytesAsync(file, [9], ct);
|
|
}
|
|
|
|
var run = await bed.Db.Exports.CreateRunAsync(new ExportRun { DestinationId = usb.Id, State = ExportRunState.Interrupted, StartedAt = bed.Clock.Now }, ct);
|
|
await bed.Db.Exports.AddItemsAsync([new ExportItem { RunId = run.Id, MediaId = post.Media[0].Id, AccountId = bed.Marta.Id, SourceSha256 = post.Media[0].Sha256 }], ct);
|
|
var item = (await bed.ItemsAsync(run.Id, ct)).Single();
|
|
await bed.Db.Exports.UpdateItemsAsync([item with { RelativePath = relative }], ct);
|
|
|
|
await bed.Service.DiscardAsync(run.Id, ct);
|
|
Assert.AreEqual(ExportRunState.Cancelled, (await bed.RunAsync(run.Id, ct)).State);
|
|
Assert.IsFalse(File.Exists(ours));
|
|
Assert.IsTrue(File.Exists(notInAFolderOfItems));
|
|
Assert.IsTrue(File.Exists(alberto));
|
|
await Assert.ThrowsExactlyAsync<InvalidOperationException>(() => bed.Service.ResumeAsync([run.Id], ct));
|
|
}
|
|
|
|
/// <summary>The real file system; opening a chosen library file first takes the destination disk away.</summary>
|
|
private sealed class Unplug : IExportFileSystem
|
|
{
|
|
public string? Root { get; set; }
|
|
|
|
public Func<string, bool> When { get; set; } = _ => false;
|
|
|
|
public Stream OpenRead(string path)
|
|
{
|
|
if (Root is { } root && When(path) && Directory.Exists(root))
|
|
{
|
|
Directory.Delete(root, recursive: true);
|
|
}
|
|
|
|
return ExportFileSystem.Instance.OpenRead(path);
|
|
}
|
|
|
|
public Task<string> ComputeSha256Async(string path, CancellationToken ct) => ExportFileSystem.Instance.ComputeSha256Async(path, ct);
|
|
|
|
public bool FileExists(string path) => ExportFileSystem.Instance.FileExists(path);
|
|
|
|
public long? FileLength(string path) => ExportFileSystem.Instance.FileLength(path);
|
|
}
|
|
}
|