La seconda revisione ha trovato che le copie della cartella generale di un account rimosso e aggiunto di nuovo restavano orfane per sempre: una «Tolto» o «Superato» senza file da adottare si sarebbe ricopiata da sola, contro §4.10. - export_copy tiene identificativo Instagram e nome dell'account rimosso, solo sulle copie orfane (CHECK, ix_export_copy_orphan); AccountStore.DeleteAsync li scrive. - RelinkOrphanCopiesAsync ridà all'account le sue copie della cartella generale, per identificativo o, se la copia non lo ha, per nome; AddAsync, UpdateAsync e RenameAsync lo chiamano nella loro transazione. La cartella propria resta ad AttachDestinationAsync. - Ogni CHECK della 0003 ha una prova di rifiuto su una riga valida per tutti gli altri; ambito, variante e formato sconosciuti si controllano anche sullo schema. - SPECIFICA §4.10 e §8.3, decisioni.md e stato.md aggiornati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
662 lines
44 KiB
C#
662 lines
44 KiB
C#
using Febe.Core.Domain;
|
|
using Febe.Core.Storage;
|
|
using Febe.Data;
|
|
using Febe.Tests.Support;
|
|
using static Febe.Tests.Data.ExportWorld;
|
|
|
|
namespace Febe.Tests.Data;
|
|
|
|
/// <summary>Round trips of the export, still-video and conversion stores on a real database (times at ms, UTC).</summary>
|
|
[TestClass]
|
|
public sealed class ExportStoreTests
|
|
{
|
|
[TestMethod]
|
|
public async Task StorageExposesTheThreeStores()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
IFebeStorage storage = db;
|
|
|
|
Assert.IsNotNull(storage.Exports);
|
|
Assert.IsNotNull(storage.VideoAnalyses);
|
|
Assert.IsNotNull(storage.Conversions);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task DestinationsRoundTrip()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var since = Make.T0.AddDays(3).AddMilliseconds(123);
|
|
|
|
var luca = await db.Exports.AddDestinationAsync(Destination(ExportScope.Account, w.Luca, @"\\nas\copie\luca") with
|
|
{
|
|
Label = "Disco di rete",
|
|
ContentTypes = ContentTypes.PostPhotos | ContentTypes.Stories | ContentTypes.ProfilePicture,
|
|
PhotoFormat = ExportFormat.WebpLossless,
|
|
VideoFormat = ExportFormat.Mp4,
|
|
NamingScheme = "{anno}/{codice}",
|
|
HighlightNamingScheme = "{raccolta}/{numero}",
|
|
ProfileNamingScheme = "profilo/{data}",
|
|
Automatic = true,
|
|
AutomaticSince = since,
|
|
});
|
|
|
|
var read = (await db.Exports.GetDestinationAsync(luca.Id))!;
|
|
Assert.AreEqual(luca, read);
|
|
Assert.AreEqual(since, read.AutomaticSince);
|
|
Assert.AreEqual(luca.Id, (await db.Exports.GetAccountDestinationAsync(w.Luca))!.Id);
|
|
Assert.AreEqual(w.General, (await db.Exports.GetGeneralDestinationAsync())!.Id);
|
|
var general = (await db.Exports.GetDestinationAsync(w.General))!;
|
|
Assert.IsNull(general.PhotoFormat, "«Come le impostazioni generali»");
|
|
Assert.IsNull(general.NamingScheme, "«Come la libreria»");
|
|
Assert.IsFalse(general.Automatic, "«Automatica» spenta per impostazione predefinita");
|
|
|
|
var paused = read with { Automatic = false, AutomaticSince = null, PausedAt = Make.T0.AddDays(4), LastError = "Disco non raggiungibile", Label = null, VideoFormat = null };
|
|
await db.Exports.UpdateDestinationAsync(paused);
|
|
Assert.AreEqual(paused, await db.Exports.GetDestinationAsync(luca.Id));
|
|
|
|
await db.Exports.RetireDestinationAsync(w.Own, Make.T0.AddDays(5));
|
|
CollectionAssert.AreEqual(new[] { w.General, luca.Id }, (await db.Exports.ListDestinationsAsync()).Select(d => d.Id).ToArray());
|
|
CollectionAssert.AreEqual(new[] { w.General, w.Own, luca.Id }, (await db.Exports.ListDestinationsAsync(includeRetired: true)).Select(d => d.Id).ToArray());
|
|
Assert.IsNull(await db.Exports.GetAccountDestinationAsync(w.Marta));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task DestinationsKeepTheirFolderScopeAndAccount()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var own = (await db.Exports.GetDestinationAsync(w.Own))!;
|
|
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Exports.UpdateDestinationAsync(own with { Id = 9999 }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateDestinationAsync(own with { RootPath = @"Z:\Altra" }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateDestinationAsync(own with { AccountId = w.Luca }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateDestinationAsync(own with { Scope = ExportScope.General, AccountId = null }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateDestinationAsync(own with { PhotoFormat = ExportFormat.Mp4 }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateDestinationAsync(own with { AutomaticSince = Make.T0 }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddDestinationAsync(Destination(ExportScope.General, null, @"Z:\a") with { RemovedAt = Make.T0 }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddDestinationAsync(Destination(ExportScope.Account, null, @"Z:\a")));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddDestinationAsync(Destination(ExportScope.General, w.Marta, @"Z:\a")));
|
|
|
|
await db.Exports.UpdateDestinationAsync(own with { Automatic = true });
|
|
await db.Exports.RetireDestinationAsync(w.Own, Make.T0.AddDays(1));
|
|
var retired = (await db.Exports.GetDestinationAsync(w.Own))!;
|
|
Assert.IsTrue(retired.IsRetired);
|
|
Assert.IsFalse(retired.Automatic, "una destinazione ritirata non è più automatica");
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateDestinationAsync(retired));
|
|
await db.Exports.RetireDestinationAsync(w.Own, Make.T0.AddDays(9));
|
|
Assert.AreEqual(Make.T0.AddDays(1), (await db.Exports.GetDestinationAsync(w.Own))!.RemovedAt, "ritirarla di nuovo non cambia nulla");
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Exports.RetireDestinationAsync(9999, Make.T0));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheAccountSwitchRoundTrips()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
|
|
Assert.IsNull(await db.Exports.GetAccountExportEnabledAsync(w.Marta), "nessuna scelta registrata");
|
|
await db.Exports.SetAccountExportEnabledAsync(w.Marta, true);
|
|
Assert.IsTrue(await db.Exports.GetAccountExportEnabledAsync(w.Marta));
|
|
await db.Exports.SetAccountExportEnabledAsync(w.Marta, false);
|
|
Assert.IsFalse(await db.Exports.GetAccountExportEnabledAsync(w.Marta));
|
|
Assert.IsNull(await db.Exports.GetAccountExportEnabledAsync(w.Luca));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task CopiesRoundTripAndKeepTheirIdentity()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
|
|
var frame = await db.Exports.AddCopyAsync(w.WholeCopy(@"marta.demo\2026\a_t0m01s500.png") with
|
|
{
|
|
Variant = ExportVariant.Frame,
|
|
Format = ExportFormat.Png,
|
|
FrameAt = TimeSpan.FromMilliseconds(1500),
|
|
CopySha256 = Make.Sha(2).ToUpperInvariant(),
|
|
Origin = ExportCopyOrigin.Adopted,
|
|
SourceSha256 = Make.Sha(1).ToUpperInvariant(),
|
|
});
|
|
var read = (await db.Exports.GetCopyAsync(frame.Id))!;
|
|
Assert.AreEqual(frame, read);
|
|
Assert.AreEqual("marta.demo/2026/a_t0m01s500.png", read.RelativePath, "separatore '/'");
|
|
Assert.AreEqual(Make.Sha(1), read.SourceSha256, "SHA-256 in minuscolo");
|
|
Assert.AreEqual(Make.Sha(2), read.CopySha256);
|
|
Assert.AreEqual(new ExportCopyIdentity(w.General, w.Marta, Make.Sha(1), ExportVariant.Frame, ExportFormat.Png, TimeSpan.FromMilliseconds(1500)), read.Identity);
|
|
|
|
var failed = read with { State = ExportCopyState.Failed, RelativePath = null, Bytes = null, CopySha256 = null, LastError = "Spazio esaurito", StateChangedAt = Make.T0.AddHours(2) };
|
|
await db.Exports.UpdateCopyAsync(failed);
|
|
Assert.AreEqual(failed, await db.Exports.GetCopyAsync(frame.Id));
|
|
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddCopyAsync(w.WholeCopy("x.jpg") with { AccountId = null }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddCopyAsync(w.WholeCopy("x.jpg") with { SourceSha256 = "abc" }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddCopyAsync(w.WholeCopy("x.jpg") with { Format = ExportFormat.Jpg }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateCopyAsync(failed with { AccountId = w.Luca }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateCopyAsync(failed with { FrameAt = TimeSpan.Zero }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateCopyAsync(failed with { Format = ExportFormat.Jpg }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateCopyAsync(failed with { SourceSha256 = Make.Sha(3) }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateCopyAsync(failed with { DestinationId = w.Own }));
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Exports.UpdateCopyAsync(failed with { Id = 9999 }));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task CurrentAndLatestCopyOfAnIdentity()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var identity = new ExportCopyIdentity(w.General, w.Marta, Make.Sha(1), ExportVariant.Whole, ExportFormat.Original, null);
|
|
|
|
Assert.IsNull(await db.Exports.FindCurrentCopyAsync(identity));
|
|
Assert.IsNull(await db.Exports.FindLatestCopyAsync(identity));
|
|
|
|
var first = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg"));
|
|
Assert.AreEqual(first.Id, (await db.Exports.FindCurrentCopyAsync(identity))!.Id);
|
|
await db.Exports.UpdateCopyAsync(first with { State = ExportCopyState.Removed, StateChangedAt = Make.T0.AddDays(1) });
|
|
Assert.IsNull(await db.Exports.FindCurrentCopyAsync(identity), "«Tolto» non è una copia corrente");
|
|
Assert.AreEqual(ExportCopyState.Removed, (await db.Exports.FindLatestCopyAsync(identity))!.State, "Tolto: mai accodato da solo");
|
|
|
|
var again = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a (2).jpg")); // «Copia di nuovo»
|
|
Assert.AreEqual(again.Id, (await db.Exports.FindLatestCopyAsync(identity))!.Id);
|
|
Assert.AreEqual(again.Id, (await db.Exports.FindCurrentCopyAsync(identity))!.Id);
|
|
|
|
var frameZero = new ExportCopyIdentity(w.General, w.Marta, Make.Sha(1), ExportVariant.Frame, ExportFormat.Jpg, TimeSpan.Zero);
|
|
var atZero = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a_f0.jpg") with { Variant = ExportVariant.Frame, Format = ExportFormat.Jpg, FrameAt = TimeSpan.Zero, CopySha256 = Make.Sha(4) });
|
|
Assert.AreEqual(atZero.Id, (await db.Exports.FindCurrentCopyAsync(frameZero))!.Id);
|
|
Assert.IsNull(await db.Exports.FindCurrentCopyAsync(frameZero with { FrameAt = null }), "nessun istante e l'istante 0 sono identità diverse");
|
|
Assert.IsNull(await db.Exports.FindCurrentCopyAsync(identity with { AccountId = w.Luca }));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task CopiesAreFoundBySha256AndByPath()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var general = await db.Exports.AddCopyAsync(w.WholeCopy("Marta.Demo/Caffè.jpg"));
|
|
var own = await db.Exports.AddCopyAsync(w.WholeCopy("caffè.jpg", destination: w.Own));
|
|
var luca = await db.Exports.AddCopyAsync(w.WholeCopy("luca.demo/caffè.jpg", account: w.Luca, media: w.LucaMedia));
|
|
await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/altro.jpg") with { SourceSha256 = Make.Sha(2), CopySha256 = Make.Sha(2) });
|
|
|
|
CollectionAssert.AreEqual(new[] { luca.Id, own.Id, general.Id }, (await db.Exports.ListCopiesBySha256Async(Make.Sha(1).ToUpperInvariant())).Select(c => c.Id).ToArray());
|
|
|
|
Assert.AreEqual(general.Id, (await db.Exports.FindCopyByPathAsync(w.General, "marta.demo/CAFFÈ.JPG".Replace("È", "è", StringComparison.Ordinal)))!.Id, "maiuscole ASCII diverse");
|
|
Assert.AreEqual(general.Id, (await db.Exports.FindCopyByPathAsync(w.General, @"marta.demo\Caffe" + "\u0300" + ".jpg"))!.Id, "'\\' e forma NFD");
|
|
Assert.IsNull(await db.Exports.FindCopyByPathAsync(w.Own, "Marta.Demo/Caffè.jpg"));
|
|
await db.Exports.UpdateCopyAsync(general with { State = ExportCopyState.Removed });
|
|
Assert.IsNull(await db.Exports.FindCopyByPathAsync(w.General, "Marta.Demo/Caffè.jpg"), "un percorso «Tolto» è di nuovo libero");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task CopiesOfADestinationArePagedAndCounted()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var ids = new List<long>();
|
|
for (var i = 0; i < 7; i++)
|
|
{
|
|
var copy = await db.Exports.AddCopyAsync(w.WholeCopy($"marta.demo/{i}.jpg") with { SourceSha256 = Make.Sha(100 + i), CopySha256 = Make.Sha(100 + i) });
|
|
ids.Add(copy.Id);
|
|
if (i % 3 == 0)
|
|
{
|
|
await db.Exports.UpdateCopyAsync(copy with { State = ExportCopyState.Removed });
|
|
}
|
|
}
|
|
|
|
await db.Exports.AddCopyAsync(w.WholeCopy("a.jpg", destination: w.Own));
|
|
|
|
var pages = new List<long>();
|
|
long after = 0;
|
|
while (true)
|
|
{
|
|
var page = await db.Exports.ListCopiesAsync(w.General, afterId: after, limit: 3);
|
|
if (page.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
pages.AddRange(page.Select(c => c.Id));
|
|
after = page[^1].Id;
|
|
}
|
|
|
|
CollectionAssert.AreEqual(ids, pages);
|
|
CollectionAssert.AreEqual(new[] { ids[0], ids[3], ids[6] }, (await db.Exports.ListCopiesAsync(w.General, ExportCopyState.Removed)).Select(c => c.Id).ToArray());
|
|
CollectionAssert.AreEqual(new[] { ids[4], ids[5] }, (await db.Exports.ListCopiesAsync(w.General, ExportCopyState.Copied, afterId: ids[3])).Select(c => c.Id).ToArray());
|
|
|
|
var counts = await db.Exports.CountCopiesAsync(w.General);
|
|
Assert.HasCount(Enum.GetValues<ExportCopyState>().Length, counts);
|
|
Assert.AreEqual(4, counts[ExportCopyState.Copied]);
|
|
Assert.AreEqual(3, counts[ExportCopyState.Removed]);
|
|
Assert.AreEqual(0, counts[ExportCopyState.Superseded]);
|
|
Assert.AreEqual(0, counts[ExportCopyState.Failed]);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task SupersedingFollowsTheFileOfTheAccount()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var (a, b, c) = (Make.Sha(1), Make.Sha(2), Make.Sha(3));
|
|
var copied = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg"));
|
|
var removed = await db.Exports.AddCopyAsync(w.WholeCopy("a.jpg", destination: w.Own) with { State = ExportCopyState.Removed });
|
|
var converted = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.png") with { Variant = ExportVariant.Converted, Format = ExportFormat.Png, CopySha256 = Make.Sha(4) });
|
|
var luca = await db.Exports.AddCopyAsync(w.WholeCopy("luca.demo/a.jpg", account: w.Luca, media: w.LucaMedia));
|
|
|
|
Assert.AreEqual(3, await db.Exports.SupersedeCopiesAsync(w.Marta, a.ToUpperInvariant(), b, Make.T0.AddDays(1)));
|
|
|
|
var first = (await db.Exports.GetCopyAsync(copied.Id))!;
|
|
Assert.AreEqual(ExportCopyState.Superseded, first.State);
|
|
Assert.AreEqual(b, first.SupersededBySha256);
|
|
Assert.AreEqual(Make.T0.AddDays(1), first.StateChangedAt);
|
|
Assert.AreEqual(ExportCopyState.Superseded, (await db.Exports.GetCopyAsync(converted.Id))!.State);
|
|
var stamped = (await db.Exports.GetCopyAsync(removed.Id))!;
|
|
Assert.AreEqual(ExportCopyState.Removed, stamped.State, "un «Tolto» resta tolto");
|
|
Assert.AreEqual(b, stamped.SupersededBySha256);
|
|
var other = (await db.Exports.GetCopyAsync(luca.Id))!;
|
|
Assert.AreEqual(ExportCopyState.Copied, other.State, "le copie di un altro account non cambiano");
|
|
Assert.IsNull(other.SupersededBySha256);
|
|
CollectionAssert.AreEquivalent(new[] { copied.Id, removed.Id, converted.Id }, (await db.Exports.ListCopiesBySha256Async(b)).Select(x => x.Id).ToArray());
|
|
|
|
Assert.AreEqual(3, await db.Exports.SupersedeCopiesAsync(w.Marta, b, c, Make.T0.AddDays(2)));
|
|
CollectionAssert.AreEquivalent(new[] { copied.Id, removed.Id, converted.Id }, (await db.Exports.ListCopiesBySha256Async(c)).Select(x => x.Id).ToArray());
|
|
Assert.IsEmpty(await db.Exports.ListCopiesBySha256Async(b));
|
|
Assert.AreEqual(Make.T0.AddDays(1), (await db.Exports.GetCopyAsync(copied.Id))!.StateChangedAt, "spostare in avanti non è un cambio di stato");
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.SupersedeCopiesAsync(w.Marta, c, c, Make.T0));
|
|
}
|
|
|
|
/// <summary>
|
|
/// The library file goes back to an earlier one (A→B→A, or A→B→C→A: «Tieni il più grande» compares pixels or bytes
|
|
/// and is not transitive; a rebuild keeps the file on disk): the copy matches again and is never superseded by itself.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public async Task SupersedingBackToTheOriginalRevivesTheCopy()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var (a, b, c) = (Make.Sha(1), Make.Sha(2), Make.Sha(3));
|
|
var identity = new ExportCopyIdentity(w.General, w.Marta, a, ExportVariant.Whole, ExportFormat.Original, null);
|
|
var copied = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg"));
|
|
var removed = await db.Exports.AddCopyAsync(w.WholeCopy("a.jpg", destination: w.Own) with { State = ExportCopyState.Removed });
|
|
|
|
Assert.AreEqual(2, await db.Exports.SupersedeCopiesAsync(w.Marta, a, b, Make.T0.AddDays(1)));
|
|
Assert.AreEqual(2, await db.Exports.SupersedeCopiesAsync(w.Marta, b, a, Make.T0.AddDays(2)));
|
|
|
|
var back = (await db.Exports.GetCopyAsync(copied.Id))!;
|
|
Assert.AreEqual(ExportCopyState.Copied, back.State, "la copia è di nuovo uguale al file della libreria");
|
|
Assert.IsNull(back.SupersededBySha256);
|
|
Assert.AreEqual(Make.T0.AddDays(2), back.StateChangedAt);
|
|
Assert.AreEqual(copied.Id, (await db.Exports.FindCurrentCopyAsync(identity))!.Id);
|
|
var stillRemoved = (await db.Exports.GetCopyAsync(removed.Id))!;
|
|
Assert.AreEqual(ExportCopyState.Removed, stillRemoved.State, "un «Tolto» resta tolto");
|
|
Assert.IsNull(stillRemoved.SupersededBySha256);
|
|
Assert.IsEmpty(await db.Exports.ListCopiesBySha256Async(b));
|
|
|
|
Assert.AreEqual(2, await db.Exports.SupersedeCopiesAsync(w.Marta, a, b, Make.T0.AddDays(3)));
|
|
Assert.AreEqual(2, await db.Exports.SupersedeCopiesAsync(w.Marta, b, c, Make.T0.AddDays(4)));
|
|
Assert.AreEqual(2, await db.Exports.SupersedeCopiesAsync(w.Marta, c, a, Make.T0.AddDays(5)));
|
|
Assert.AreEqual(ExportCopyState.Copied, (await db.Exports.GetCopyAsync(copied.Id))!.State, "A→B→C→A");
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(removed.Id))!.SupersededBySha256);
|
|
Assert.AreEqual(0L, await ScalarAsync(db, "SELECT COUNT(*) FROM export_copy WHERE superseded_by_sha256 = source_sha256"));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task SupersedingBackKeepsTheCurrentCopyOfTheIdentity()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var (a, b) = (Make.Sha(1), Make.Sha(2));
|
|
var identity = new ExportCopyIdentity(w.General, w.Marta, a, ExportVariant.Whole, ExportFormat.Original, null);
|
|
|
|
// Two Superseded rows of one identity: only the newest can come back, the unique index allows one current row.
|
|
var oldest = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg"));
|
|
await db.Exports.SupersedeCopiesAsync(w.Marta, a, b, Make.T0.AddDays(1));
|
|
var newer = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a (2).jpg")); // «Copia di nuovo»
|
|
await db.Exports.SupersedeCopiesAsync(w.Marta, a, b, Make.T0.AddDays(2));
|
|
|
|
Assert.AreEqual(1, await db.Exports.SupersedeCopiesAsync(w.Marta, b, a, Make.T0.AddDays(3)));
|
|
Assert.AreEqual(newer.Id, (await db.Exports.FindCurrentCopyAsync(identity))!.Id);
|
|
var history = (await db.Exports.GetCopyAsync(oldest.Id))!;
|
|
Assert.AreEqual(ExportCopyState.Superseded, history.State, "resta storia, superata dal file di prima");
|
|
Assert.AreEqual(b, history.SupersededBySha256);
|
|
|
|
// A current row made meanwhile (copied again while the library held B): the Superseded row stays history.
|
|
await db.Exports.SupersedeCopiesAsync(w.Marta, a, b, Make.T0.AddDays(4));
|
|
var third = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a (3).jpg"));
|
|
Assert.AreEqual(0, await db.Exports.SupersedeCopiesAsync(w.Marta, b, a, Make.T0.AddDays(5)));
|
|
Assert.AreEqual(third.Id, (await db.Exports.FindCurrentCopyAsync(identity))!.Id);
|
|
Assert.AreEqual(ExportCopyState.Superseded, (await db.Exports.GetCopyAsync(newer.Id))!.State);
|
|
Assert.AreEqual(b, (await db.Exports.GetCopyAsync(newer.Id))!.SupersededBySha256);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spec §4.10: «Tolto e Superato non si ricopiano mai da soli», also after the account was removed and added again
|
|
/// (a new account id): its copies of the general folder come back by its Instagram id, in every state.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public async Task RemovedCopiesOfTheGeneralFolderComeBackWithTheAccount()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
await db.Exports.UpdateDestinationAsync((await db.Exports.GetDestinationAsync(w.General))! with { Automatic = true });
|
|
var removed = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg") with { State = ExportCopyState.Removed, StateChangedAt = Make.T0.AddDays(1) });
|
|
var superseded = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/b.jpg") with
|
|
{
|
|
SourceSha256 = Make.Sha(2), CopySha256 = Make.Sha(2), State = ExportCopyState.Superseded, SupersededBySha256 = Make.Sha(3),
|
|
});
|
|
var copied = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/c.png") with { Variant = ExportVariant.Converted, Format = ExportFormat.Png, CopySha256 = Make.Sha(4) });
|
|
var own = await db.Exports.AddCopyAsync(w.WholeCopy("a.jpg", destination: w.Own) with { State = ExportCopyState.Removed });
|
|
var luca = await db.Exports.AddCopyAsync(w.WholeCopy("luca.demo/a.jpg", account: w.Luca, media: w.LucaMedia) with { State = ExportCopyState.Removed });
|
|
|
|
await db.Accounts.DeleteAsync(w.Marta);
|
|
|
|
foreach (var id in new[] { removed.Id, superseded.Id, copied.Id, own.Id })
|
|
{
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(id))!.AccountId);
|
|
Assert.AreEqual(("100", "marta.demo"), await RemovedAccountAsync(db, id), "ogni copia tiene l'account rimosso, in ogni destinazione e stato");
|
|
}
|
|
|
|
Assert.AreEqual((null, null), await RemovedAccountAsync(db, luca.Id), "le copie degli altri account non cambiano");
|
|
|
|
var again = await db.Accounts.AddAsync(Make.Account("marta.demo", "100"));
|
|
Assert.AreNotEqual(w.Marta, again.Id);
|
|
|
|
var identity = new ExportCopyIdentity(w.General, again.Id, Make.Sha(1), ExportVariant.Whole, ExportFormat.Original, null);
|
|
var latest = await db.Exports.FindLatestCopyAsync(identity);
|
|
Assert.IsNotNull(latest, "la copia «Tolto» si ritrova: l'esportazione automatica non la rifà da sola");
|
|
Assert.AreEqual(removed.Id, latest.Id);
|
|
Assert.AreEqual(ExportCopyState.Removed, latest.State);
|
|
Assert.AreEqual(Make.T0.AddDays(1), latest.StateChangedAt, "ritrovarla non è un cambio di stato");
|
|
Assert.AreEqual(superseded.Id, (await db.Exports.FindLatestCopyAsync(identity with { SourceSha256 = Make.Sha(2) }))!.Id);
|
|
Assert.AreEqual(copied.Id, (await db.Exports.FindCurrentCopyAsync(identity with { Variant = ExportVariant.Converted, Format = ExportFormat.Png }))!.Id);
|
|
foreach (var id in new[] { removed.Id, superseded.Id, copied.Id })
|
|
{
|
|
Assert.AreEqual((null, null), await RemovedAccountAsync(db, id));
|
|
}
|
|
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(own.Id))!.AccountId, "la cartella propria torna solo quando Alberto gliela ridà");
|
|
Assert.AreEqual(("100", "marta.demo"), await RemovedAccountAsync(db, own.Id));
|
|
Assert.AreEqual(w.Luca, (await db.Exports.GetCopyAsync(luca.Id))!.AccountId);
|
|
Assert.AreEqual(0, await db.Exports.RelinkOrphanCopiesAsync(again.Id), "già ritrovate");
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Exports.RelinkOrphanCopiesAsync(9999));
|
|
Assert.AreEqual(0L, await ForeignKeyProblemsAsync(db));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task CopiesWithoutAnInstagramIdComeBackByUserName()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var anna = await db.Accounts.AddAsync(Make.Account("anna.demo") with { InstagramId = null });
|
|
var copy = await db.Exports.AddCopyAsync(w.WholeCopy("anna.demo/a.jpg", account: anna.Id) with { State = ExportCopyState.Removed });
|
|
await db.Accounts.DeleteAsync(anna.Id);
|
|
Assert.AreEqual(((string?)null, "anna.demo"), await RemovedAccountAsync(db, copy.Id));
|
|
|
|
var again = await db.Accounts.AddAsync(Make.Account("@Anna.Demo", "300"));
|
|
|
|
Assert.AreEqual(again.Id, (await db.Exports.GetCopyAsync(copy.Id))!.AccountId, "per nome, senza badare alle maiuscole, se la copia non ha l'identificativo");
|
|
Assert.AreEqual((null, null), await RemovedAccountAsync(db, copy.Id));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task CopiesOfAnotherInstagramIdDoNotComeBack()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var copy = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg") with { State = ExportCopyState.Removed });
|
|
await db.Accounts.DeleteAsync(w.Marta);
|
|
|
|
var other = await db.Accounts.AddAsync(Make.Account("marta.demo", "999"));
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(copy.Id))!.AccountId, "lo stesso nome con un altro identificativo è un'altra persona");
|
|
await db.Accounts.DeleteAsync(other.Id);
|
|
var unknown = await db.Accounts.AddAsync(Make.Account("marta.demo") with { InstagramId = null });
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(copy.Id))!.AccountId, "la copia ha l'identificativo: il nome solo non basta");
|
|
await db.Accounts.RenameAsync(unknown.Id, "marta.nuova", Make.T0.AddDays(1));
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(copy.Id))!.AccountId);
|
|
Assert.AreEqual(("100", "marta.demo"), await RemovedAccountAsync(db, copy.Id));
|
|
|
|
// The Instagram id becomes known later (an imported account): the copies come back then.
|
|
await db.Accounts.UpdateAsync((await db.Accounts.GetAsync(unknown.Id))! with { InstagramId = "100" });
|
|
Assert.AreEqual(unknown.Id, (await db.Exports.GetCopyAsync(copy.Id))!.AccountId);
|
|
Assert.AreEqual((null, null), await RemovedAccountAsync(db, copy.Id));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task AdoptionByPathForgetsTheRemovedAccount()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var copy = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg"));
|
|
await db.Accounts.DeleteAsync(w.Marta);
|
|
var other = await db.Accounts.AddAsync(Make.Account("marta.demo", "999"));
|
|
|
|
var adopted = await db.Exports.RelinkOrphanCopyAsync(copy.Id, other.Id, null, Make.T0.AddDays(1));
|
|
|
|
Assert.AreEqual(other.Id, adopted.AccountId);
|
|
Assert.AreEqual((null, null), await RemovedAccountAsync(db, copy.Id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Re-linking by user name can meet an account that already has copies: a current row of an identity the account
|
|
/// already holds stays an orphan, and of two current orphans of one identity only the newest comes back.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public async Task RelinkingNeverBreaksTheIdentityOfACopy()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var anna = await db.Accounts.AddAsync(Make.Account("anna.demo") with { InstagramId = null });
|
|
var first = await db.Exports.AddCopyAsync(w.WholeCopy("anna.demo/a.jpg", account: anna.Id));
|
|
var history = await db.Exports.AddCopyAsync(w.WholeCopy("anna.demo/b.jpg", account: anna.Id) with { State = ExportCopyState.Removed });
|
|
await db.Accounts.DeleteAsync(anna.Id);
|
|
|
|
// Another account with a current copy of the same file takes the name: the old current copy stays an orphan.
|
|
var bea = await db.Accounts.AddAsync(Make.Account("bea.demo") with { InstagramId = null });
|
|
var second = await db.Exports.AddCopyAsync(w.WholeCopy("bea.demo/a.jpg", account: bea.Id));
|
|
await db.Accounts.RenameAsync(bea.Id, "anna.demo", Make.T0.AddDays(1));
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(first.Id))!.AccountId, "l'account ha già una copia corrente di quell'identità");
|
|
Assert.AreEqual(((string?)null, "anna.demo"), await RemovedAccountAsync(db, first.Id), "e la copia resta orfana, come storia");
|
|
Assert.AreEqual(bea.Id, (await db.Exports.GetCopyAsync(history.Id))!.AccountId, "un «Tolto» torna sempre");
|
|
|
|
// Two current orphans of one identity: only the newest comes back.
|
|
await db.Accounts.DeleteAsync(bea.Id);
|
|
var again = await db.Accounts.AddAsync(Make.Account("anna.demo") with { InstagramId = null });
|
|
Assert.AreEqual(again.Id, (await db.Exports.GetCopyAsync(second.Id))!.AccountId);
|
|
Assert.IsNull((await db.Exports.GetCopyAsync(first.Id))!.AccountId);
|
|
Assert.AreEqual(again.Id, (await db.Exports.GetCopyAsync(history.Id))!.AccountId);
|
|
Assert.AreEqual(1L, await ScalarAsync(db, "SELECT COUNT(*) FROM export_copy WHERE account_id = @a AND state = 'Copied'", ("@a", again.Id)));
|
|
Assert.AreEqual(0, await db.Exports.RelinkOrphanCopiesAsync(again.Id));
|
|
}
|
|
|
|
private static Task<(string? InstagramId, string? Username)> RemovedAccountAsync(FebeDatabase db, long copyId) =>
|
|
db.ReadAsync(
|
|
c =>
|
|
{
|
|
using var r = c.Command("SELECT removed_account_instagram_id, removed_account_username FROM export_copy WHERE id = @id").With("@id", copyId).ExecuteReader();
|
|
Assert.IsTrue(r.Read());
|
|
return (r.IsDBNull(0) ? null : r.GetString(0), r.IsDBNull(1) ? null : r.GetString(1));
|
|
},
|
|
default);
|
|
|
|
/// <summary>The partial unique indexes of 0003 reach the callers as <see cref="DuplicateRecordException"/>, never as a SQLite error.</summary>
|
|
[TestMethod]
|
|
public async Task CollisionsAreDuplicateRecords()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
|
|
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Exports.AddDestinationAsync(Destination(ExportScope.General, null, @"Z:\Altra")), "ux_export_destination_general");
|
|
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Exports.AddDestinationAsync(Destination(ExportScope.Account, w.Marta, @"Z:\Marta")), "ux_export_destination_account");
|
|
Assert.HasCount(2, await db.Exports.ListDestinationsAsync(includeRetired: true));
|
|
|
|
var first = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg"));
|
|
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/b.jpg")), "ux_export_copy_identity");
|
|
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Exports.AddCopyAsync(w.WholeCopy("MARTA.demo/a.jpg", account: w.Luca, media: w.LucaMedia)), "ux_export_copy_path");
|
|
await db.Exports.UpdateCopyAsync(first with { State = ExportCopyState.Removed });
|
|
var second = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/b.jpg"));
|
|
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Exports.UpdateCopyAsync(first), "un «Tolto» non torna copiato accanto alla copia corrente");
|
|
Assert.AreEqual(ExportCopyState.Removed, (await db.Exports.GetCopyAsync(first.Id))!.State);
|
|
Assert.AreEqual(ExportCopyState.Copied, (await db.Exports.GetCopyAsync(second.Id))!.State);
|
|
|
|
var pending = await db.Conversions.AddAsync(new MediaConversion { MediaId = w.MartaMedia, Format = ExportFormat.Jpg, SourceSha256 = Make.Sha(1), CreatedAt = Make.T0 });
|
|
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Conversions.AddAsync(pending with { Id = 0 }), "ux_media_conversion_current");
|
|
var removed = pending with { State = MediaConversionState.Removed, RelativePath = "post/a.jpg.jpg", Bytes = 1, Sha256 = Make.Sha(2), ConvertedAt = Make.T0 };
|
|
await db.Conversions.UpdateAsync(removed);
|
|
await db.Conversions.AddAsync(pending with { Id = 0 });
|
|
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Conversions.UpdateAsync(removed with { State = MediaConversionState.Converted }), "ux_media_conversion_current");
|
|
Assert.AreEqual(MediaConversionState.Removed, (await db.Conversions.GetAsync(pending.Id))!.State);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task RunsAndItemsRoundTrip()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
var old = await db.Exports.CreateRunAsync(new ExportRun { DestinationId = w.General, StartedAt = Make.T0, State = ExportRunState.Completed, CompletedAt = Make.T0.AddMinutes(5) });
|
|
var run = await db.Exports.CreateRunAsync(new ExportRun { DestinationId = w.General, StartedAt = Make.T0.AddDays(1), Origin = ExportRunOrigin.Automatic });
|
|
var recognize = await db.Exports.CreateRunAsync(new ExportRun { DestinationId = w.Own, StartedAt = Make.T0.AddDays(2), Kind = ExportRunKind.Recognize, State = ExportRunState.Paused, LastError = "Disco non raggiungibile" });
|
|
|
|
Assert.AreEqual(recognize, await db.Exports.GetRunAsync(recognize.Id));
|
|
CollectionAssert.AreEqual(new[] { recognize.Id, run.Id, old.Id }, (await db.Exports.ListRunsAsync()).Select(r => r.Id).ToArray());
|
|
CollectionAssert.AreEqual(new[] { run.Id, recognize.Id }, (await db.Exports.ListUnfinishedRunsAsync()).Select(r => r.Id).ToArray());
|
|
var interrupted = run with { State = ExportRunState.Interrupted };
|
|
await db.Exports.UpdateRunAsync(interrupted);
|
|
Assert.AreEqual(interrupted, await db.Exports.GetRunAsync(run.Id));
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Exports.UpdateRunAsync(run with { Id = 9999 }));
|
|
|
|
ExportItem[] items =
|
|
[
|
|
new() { RunId = run.Id, MediaId = w.MartaMedia, AccountId = w.Marta, SourceSha256 = Make.Sha(1) },
|
|
new() { RunId = run.Id, MediaId = w.MartaMedia, AccountId = w.Marta, SourceSha256 = Make.Sha(1), Variant = ExportVariant.Converted, Format = ExportFormat.Jpg },
|
|
new() { RunId = run.Id, MediaId = w.MartaMedia, AccountId = w.Marta, SourceSha256 = Make.Sha(1), Variant = ExportVariant.Frame, Format = ExportFormat.Png, FrameAt = TimeSpan.FromSeconds(2) },
|
|
new() { RunId = run.Id, MediaId = w.LucaMedia, AccountId = w.Luca, SourceSha256 = Make.Sha(1) },
|
|
];
|
|
await db.Exports.AddItemsAsync(items);
|
|
await db.Exports.AddItemsAsync(items); // resume: nothing twice
|
|
await db.Exports.AddItemsAsync([items[0] with { RunId = old.Id }]);
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddItemsAsync([items[0] with { AccountId = null }]));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.AddItemsAsync([items[0] with { MediaId = null }]), "senza file non si riconoscerebbe alla ripresa");
|
|
|
|
var listed = await db.Exports.ListItemsAsync(new() { RunId = run.Id });
|
|
Assert.HasCount(4, listed);
|
|
Assert.AreEqual(items[2] with { Id = listed[2].Id }, listed[2]);
|
|
var copy = await db.Exports.AddCopyAsync(w.WholeCopy("marta.demo/a.jpg"));
|
|
var done = listed[0] with { State = ExportItemState.Copied, RelativePath = @"marta.demo\a.jpg", CopyId = copy.Id };
|
|
var skipped = listed[3] with { State = ExportItemState.Skipped, Note = "AnimatedImage" };
|
|
await db.Exports.UpdateItemsAsync([done, skipped]);
|
|
Assert.AreEqual(done with { RelativePath = "marta.demo/a.jpg" }, await db.Exports.GetItemAsync(done.Id));
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Exports.UpdateItemsAsync([done with { Id = 9999 }]));
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Exports.UpdateItemsAsync([done with { RunId = old.Id }]));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateItemsAsync([done with { MediaId = w.LucaMedia }]));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateItemsAsync([done with { Variant = ExportVariant.Converted, Format = ExportFormat.Png }]));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateItemsAsync([listed[2] with { FrameAt = TimeSpan.FromSeconds(3) }]));
|
|
Assert.AreEqual(done with { RelativePath = "marta.demo/a.jpg" }, await db.Exports.GetItemAsync(done.Id), "l'identità del file resta");
|
|
|
|
CollectionAssert.AreEqual(new[] { listed[1].Id, listed[2].Id }, (await db.Exports.ListItemsAsync(new() { RunId = run.Id, State = ExportItemState.Pending })).Select(i => i.Id).ToArray());
|
|
CollectionAssert.AreEqual(new[] { listed[2].Id, listed[3].Id }, (await db.Exports.ListItemsAsync(new() { RunId = run.Id, AfterId = listed[1].Id, Limit = 5 })).Select(i => i.Id).ToArray());
|
|
CollectionAssert.AreEqual(new[] { listed[0].Id }, (await db.Exports.ListItemsAsync(new() { RunId = run.Id, Limit = 1 })).Select(i => i.Id).ToArray());
|
|
|
|
var summary = await db.Exports.GetRunSummaryAsync(run.Id);
|
|
Assert.AreEqual(4, summary.Total);
|
|
Assert.HasCount(Enum.GetValues<ExportItemState>().Length, summary.ByState);
|
|
Assert.AreEqual(2, summary.ByState[ExportItemState.Pending]);
|
|
Assert.AreEqual(1, summary.ByState[ExportItemState.Copied]);
|
|
Assert.AreEqual(1, summary.ByState[ExportItemState.Skipped]);
|
|
Assert.AreEqual(0, summary.ByState[ExportItemState.Failed]);
|
|
|
|
await db.Exports.DeleteRunAsync(run.Id);
|
|
Assert.IsNull(await db.Exports.GetRunAsync(run.Id));
|
|
Assert.IsEmpty(await db.Exports.ListItemsAsync(new() { RunId = run.Id }));
|
|
Assert.HasCount(1, await db.Exports.ListItemsAsync(new() { RunId = old.Id }), "gli altri lavori restano");
|
|
Assert.IsNotNull(await db.Exports.GetCopyAsync(copy.Id), "le copie restano");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task VideoAnalysesKeepTheHighestAlgorithm()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
|
|
Assert.IsNull(await db.VideoAnalyses.GetAsync(Make.Sha(1)));
|
|
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1).ToUpperInvariant(), 1, VideoAnalysisOutcome.Still, Make.T0));
|
|
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.Still, Make.T0), await db.VideoAnalyses.GetAsync(Make.Sha(1)));
|
|
|
|
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Moving, Make.T0.AddDays(1)));
|
|
Assert.AreEqual(2, (await db.VideoAnalyses.GetAsync(Make.Sha(1)))!.Algorithm, "vale l'algoritmo più alto");
|
|
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0.AddDays(2)));
|
|
Assert.AreEqual(VideoAnalysisOutcome.Moving, (await db.VideoAnalyses.GetAsync(Make.Sha(1)))!.Outcome);
|
|
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Still, Make.T0.AddDays(3)));
|
|
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Still, Make.T0.AddDays(3)), await db.VideoAnalyses.GetAsync(Make.Sha(1)), "lo stesso algoritmo si sostituisce");
|
|
Assert.AreEqual(2L, await ScalarAsync(db, "SELECT COUNT(*) FROM video_analysis"));
|
|
|
|
for (var i = 10; i < 710; i++)
|
|
{
|
|
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(i), 1, i % 2 == 0 ? VideoAnalysisOutcome.Still : VideoAnalysisOutcome.Moving, Make.T0));
|
|
}
|
|
|
|
var keys = Enumerable.Range(0, 800).Select(Make.Sha).ToList();
|
|
var many = await db.VideoAnalyses.GetManyAsync(keys);
|
|
Assert.HasCount(701, many);
|
|
Assert.AreEqual(2, many[Make.Sha(1)].Algorithm);
|
|
Assert.AreEqual(VideoAnalysisOutcome.Moving, many[Make.Sha(709)].Outcome);
|
|
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(() => db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 0, VideoAnalysisOutcome.Still, Make.T0)));
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task ConversionsRoundTrip()
|
|
{
|
|
using var temp = new TempFolder();
|
|
await using var db = await TestDatabase.OpenAsync(temp);
|
|
var w = await CreateAsync(db);
|
|
|
|
var pending = await db.Conversions.AddAsync(new MediaConversion { MediaId = w.MartaMedia, Format = ExportFormat.Jpg, SourceSha256 = Make.Sha(1), CreatedAt = Make.T0 });
|
|
Assert.AreEqual(pending, await db.Conversions.GetAsync(pending.Id));
|
|
Assert.AreEqual(MediaConversionState.Pending, pending.State);
|
|
|
|
var converted = pending with
|
|
{
|
|
State = MediaConversionState.Converted,
|
|
RelativePath = "post/a.jpg.jpg",
|
|
Bytes = 900,
|
|
Sha256 = Make.Sha(2).ToUpperInvariant(),
|
|
ConvertedAt = Make.T0.AddMinutes(1),
|
|
StateChangedAt = Make.T0.AddMinutes(1),
|
|
};
|
|
await db.Conversions.UpdateAsync(converted);
|
|
Assert.AreEqual(converted with { Sha256 = Make.Sha(2) }, await db.Conversions.GetAsync(pending.Id));
|
|
CollectionAssert.AreEqual(new[] { pending.Id }, (await db.Conversions.FindBySha256Async(Make.Sha(2))).Select(c => c.Id).ToArray());
|
|
|
|
var removed = converted with { State = MediaConversionState.Removed, StateChangedAt = Make.T0.AddDays(1) };
|
|
await db.Conversions.UpdateAsync(removed);
|
|
Assert.AreEqual(MediaConversionState.Removed, (await db.Conversions.GetAsync(pending.Id))!.State);
|
|
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Conversions.UpdateAsync(converted with { RelativePath = null }));
|
|
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Conversions.AddAsync(pending with { Format = ExportFormat.Original }));
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Conversions.UpdateAsync(converted with { Id = 9999 }));
|
|
await Assert.ThrowsExactlyAsync<KeyNotFoundException>(() => db.Conversions.UpdateAsync(converted with { Format = ExportFormat.Png }));
|
|
|
|
var png = await db.Conversions.AddAsync(new MediaConversion { MediaId = w.MartaMedia, Format = ExportFormat.Png, SourceSha256 = Make.Sha(1), CreatedAt = Make.T0 });
|
|
var again = await db.Conversions.AddAsync(new MediaConversion { MediaId = w.MartaMedia, Format = ExportFormat.Jpg, SourceSha256 = Make.Sha(1), CreatedAt = Make.T0.AddDays(2) });
|
|
var luca = await db.Conversions.AddAsync(new MediaConversion { MediaId = w.LucaMedia, Format = ExportFormat.WebpLossless, SourceSha256 = Make.Sha(1), CreatedAt = Make.T0 });
|
|
CollectionAssert.AreEqual(new[] { pending.Id, png.Id, again.Id }, (await db.Conversions.ListByMediaAsync(w.MartaMedia)).Select(c => c.Id).ToArray());
|
|
CollectionAssert.AreEqual(new[] { png.Id, again.Id, luca.Id }, (await db.Conversions.ListPendingAsync()).Select(c => c.Id).ToArray());
|
|
CollectionAssert.AreEqual(new[] { again.Id }, (await db.Conversions.ListPendingAsync(afterId: png.Id, limit: 1)).Select(c => c.Id).ToArray());
|
|
}
|
|
}
|