E1: le copie della cartella generale tornano con l'account, un rifiuto per ogni CHECK (#58)
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>
This commit is contained in:
1 parent
ea4666d49b
commit
980212fb50
10 files changed
+470
-32
No files matched your search
@@ -30,13 +30,25 @@ public sealed class ExportConstraintTests
|
||||
await InsertAsync(db, "media_conversion", w.ConversionRow());
|
||||
await InsertAsync(db, "export_copy", w.CopyRow());
|
||||
|
||||
// Each row below breaks only the CHECK it names, but three: an unknown scope also breaks the two CHECKs that pair
|
||||
// scope with account and user name (one wants both NULL unless 'Account', the other one of them set unless
|
||||
// 'General': no other scope satisfies both), and an unknown variant or format also breaks the CHECK that pairs
|
||||
// variant with format, which lists the valid ones itself. No row can break those IN lists alone, so that they
|
||||
// are still declared is read from the schema.
|
||||
await RejectedAsync(db, "export_destination", DestinationRow(("scope", "Tutti")), "ambito sconosciuto");
|
||||
await SchemaDeclaresAsync(db, "export_destination", "CHECK (scope IN ('General', 'Account'))");
|
||||
foreach (var table in new[] { "export_copy", "export_item" })
|
||||
{
|
||||
await SchemaDeclaresAsync(db, table, "CHECK (variant IN ('Whole', 'Converted', 'Frame'))");
|
||||
await SchemaDeclaresAsync(db, table, "CHECK (format IN ('Original', 'Jpg', 'Png', 'WebpLossless', 'Mp4'))");
|
||||
}
|
||||
|
||||
await RejectedAsync(db, "export_destination", DestinationRow(("photo_format", "Mp4")), "formato video per le foto");
|
||||
await RejectedAsync(db, "export_destination", DestinationRow(("video_format", "Jpg")), "formato foto per i video");
|
||||
await RejectedAsync(db, "export_destination", DestinationRow(("content_types", -1L)), "tipi negativi");
|
||||
await RejectedAsync(db, "export_account", new Dictionary<string, object?> { ["account_id"] = w.Marta, ["enabled"] = 2L }, "interruttore non booleano");
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("state", "Gone"), ("relative_path", "x/1.jpg")), "stato della copia");
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("variant", "Intero"), ("relative_path", "x/2.jpg")), "variante");
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("variant", "Intero"), ("copy_sha256", Make.Sha(2)), ("relative_path", "x/2.jpg")), "variante");
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("format", "Gif"), ("variant", "Converted"), ("copy_sha256", Make.Sha(2)), ("relative_path", "x/3.jpg")), "formato");
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("origin", "Copied"), ("account_id", w.Luca), ("media_id", w.LucaMedia), ("relative_path", "x/4.jpg")), "origine");
|
||||
await RejectedAsync(db, "export_run", w.RunRow(("kind", "Export")), "tipo di lavoro");
|
||||
@@ -114,6 +126,95 @@ public sealed class ExportConstraintTests
|
||||
await InsertAsync(db, "export_copy", w.CopyRow(("state", "Failed"), ("relative_path", null), ("bytes", null), ("copy_sha256", null)));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task EveryConversionPastPendingAndFailedHadAFile()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
var w = await CreateAsync(db);
|
||||
|
||||
foreach (var state in new[] { "Converted", "Superseded", "Removed" })
|
||||
{
|
||||
foreach (var missing in new[] { "relative_path", "bytes", "sha256", "converted_at" })
|
||||
{
|
||||
await RejectedAsync(db, "media_conversion", Converted(w, "Jpg", Make.Sha(2), ("state", state), (missing, null)), $"{state} senza {missing}");
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var state in new[] { "Converted", "Superseded", "Removed" })
|
||||
{
|
||||
await InsertAsync(db, "media_conversion", Converted(w, "Jpg", Make.Sha(2), ("state", state)));
|
||||
}
|
||||
|
||||
await InsertAsync(db, "media_conversion", w.ConversionRow(("format", "Png")));
|
||||
await InsertAsync(db, "media_conversion", w.ConversionRow(("format", "WebpLossless"), ("state", "Failed")));
|
||||
}
|
||||
|
||||
/// <summary>The CHECKs on lengths, counts, instants and the «Automatica» switch: each row breaks that CHECK alone (the same row with a valid value is accepted).</summary>
|
||||
[TestMethod]
|
||||
public async Task LengthsCountsInstantsAndSwitchesAreChecked()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
var w = await CreateAsync(db);
|
||||
var run = await InsertAsync(db, "export_run", w.RunRow());
|
||||
|
||||
foreach (var column in new[] { "label", "naming_scheme", "highlight_naming_scheme", "profile_naming_scheme" })
|
||||
{
|
||||
await RejectedAsync(db, "export_destination", DestinationRow((column, "")), $"{column} vuoto");
|
||||
await InsertAsync(db, "export_destination", DestinationRow((column, "x")));
|
||||
}
|
||||
|
||||
(string, object?)[] orphanFolder = [("scope", "Account"), ("removed_at", 1L)];
|
||||
await RejectedAsync(db, "export_destination", DestinationRow([.. orphanFolder, ("removed_account_username", "")]), "nome dell'account rimosso vuoto");
|
||||
await InsertAsync(db, "export_destination", DestinationRow([.. orphanFolder, ("removed_account_username", "x")]));
|
||||
(string, object?)[] lucaFolder = [("scope", "Account"), ("account_id", w.Luca), ("removed_at", null)];
|
||||
await RejectedAsync(db, "export_destination", DestinationRow([.. lucaFolder, ("automatic", 2L)]), "«Automatica» non booleana");
|
||||
await InsertAsync(db, "export_destination", DestinationRow([.. lucaFolder, ("automatic", 1L)]));
|
||||
|
||||
(string, object?)[] frame = [("variant", "Frame"), ("format", "Jpg"), ("copy_sha256", Make.Sha(2))];
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow([.. frame, ("frame_at_ms", -1L), ("relative_path", "f/1.jpg")]), "istante negativo della copia");
|
||||
await InsertAsync(db, "export_copy", w.CopyRow([.. frame, ("frame_at_ms", 0L), ("relative_path", "f/1.jpg")]));
|
||||
await RejectedAsync(db, "export_item", w.ItemRow(run, ("variant", "Frame"), ("format", "Jpg"), ("frame_at_ms", -1L)), "istante negativo del file");
|
||||
await InsertAsync(db, "export_item", w.ItemRow(run, ("variant", "Frame"), ("format", "Jpg"), ("frame_at_ms", 0L)));
|
||||
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("bytes", -1L), ("relative_path", "b/1.jpg")), "byte negativi della copia");
|
||||
await InsertAsync(db, "export_copy", w.CopyRow(("bytes", 0L), ("relative_path", "b/1.jpg")));
|
||||
await RejectedAsync(db, "export_item", w.ItemRow(run, ("relative_path", "")), "percorso vuoto del file");
|
||||
await InsertAsync(db, "export_item", w.ItemRow(run, ("relative_path", "marta.demo/a.jpg")));
|
||||
|
||||
await RejectedAsync(db, "media_conversion", Converted(w, "Jpg", Make.Sha(2), ("bytes", -1L)), "byte negativi del convertito");
|
||||
await RejectedAsync(db, "media_conversion", Converted(w, "Jpg", Make.Sha(2), ("relative_path", "")), "percorso vuoto del convertito");
|
||||
await InsertAsync(db, "media_conversion", Converted(w, "Jpg", Make.Sha(2), ("bytes", 0L)));
|
||||
}
|
||||
|
||||
/// <summary>The removed account kept on a copy: only on a row without account, never empty, and both may be unknown.</summary>
|
||||
[TestMethod]
|
||||
public async Task TheRemovedAccountIsKeptOnlyOnOrphanCopies()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
var w = await CreateAsync(db);
|
||||
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("removed_account_username", "marta.demo"), ("relative_path", "o/1.jpg")), "un account e il nome di un account rimosso");
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow(("removed_account_instagram_id", "100"), ("relative_path", "o/2.jpg")), "un account e l'identificativo di un account rimosso");
|
||||
|
||||
(string, object?)[] orphan = [("account_id", null)];
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow([.. orphan, ("removed_account_username", ""), ("relative_path", "o/3.jpg")]), "nome vuoto");
|
||||
await RejectedAsync(db, "export_copy", w.CopyRow([.. orphan, ("removed_account_instagram_id", ""), ("relative_path", "o/4.jpg")]), "identificativo vuoto");
|
||||
|
||||
await InsertAsync(db, "export_copy", w.CopyRow([.. orphan, ("removed_account_instagram_id", "100"), ("removed_account_username", "marta.demo"), ("relative_path", "o/1.jpg")]));
|
||||
await InsertAsync(db, "export_copy", w.CopyRow([.. orphan, ("removed_account_username", "marta.demo"), ("relative_path", "o/2.jpg")]));
|
||||
await InsertAsync(db, "export_copy", w.CopyRow([.. orphan, ("relative_path", "o/3.jpg")]));
|
||||
await InsertAsync(db, "export_copy", w.CopyRow(("relative_path", "o/4.jpg")));
|
||||
}
|
||||
|
||||
private static async Task SchemaDeclaresAsync(FebeDatabase db, string table, string fragment)
|
||||
{
|
||||
var sql = await db.ReadAsync(c => (string)c.Command("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = @t").With("@t", table).ExecuteScalar()!, default);
|
||||
Assert.Contains(fragment, sql, $"{table}: {fragment}");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task SupersededNeedsTheFileThatTookItsPlace()
|
||||
{
|
||||
@@ -155,8 +256,16 @@ public sealed class ExportConstraintTests
|
||||
await InsertAsync(db, "media_conversion", Converted(w, "WebpLossless", Make.Sha(4)));
|
||||
}
|
||||
|
||||
private static Dictionary<string, object?> Converted(ExportWorld w, string format, string sha256) =>
|
||||
w.ConversionRow(("format", format), ("state", "Converted"), ("relative_path", $"post/a.jpg.{format}"), ("bytes", 1L), ("sha256", sha256), ("converted_at", 0L));
|
||||
private static Dictionary<string, object?> Converted(ExportWorld w, string format, string sha256, params (string Column, object? Value)[] changes)
|
||||
{
|
||||
var row = w.ConversionRow(("format", format), ("state", "Converted"), ("relative_path", $"post/a.jpg.{format}"), ("bytes", 1L), ("sha256", sha256), ("converted_at", 0L));
|
||||
foreach (var (column, value) in changes)
|
||||
{
|
||||
row[column] = value;
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DestinationsKeepTheirRules()
|
||||
@@ -349,7 +458,11 @@ public sealed class ExportConstraintTests
|
||||
Assert.IsFalse(orphan.Automatic, "rimuovere l'account la disattiva");
|
||||
Assert.IsNull(orphan.AutomaticSince);
|
||||
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Exports.UpdateDestinationAsync(orphan with { Automatic = true }), "senza account non si accende");
|
||||
|
||||
// The general copy of an unknown account (a raw delete would leave it so): it comes back only by adoption.
|
||||
await ExecuteAsync(db, "UPDATE export_copy SET removed_account_instagram_id = NULL, removed_account_username = NULL WHERE id = @id", ("@id", generalCopy.Id));
|
||||
var again = await db.Accounts.AddAsync(Make.Account("marta.demo", "100"));
|
||||
Assert.IsNull((await db.Exports.GetCopyAsync(ownCopy.Id))!.AccountId, "le copie della cartella propria tornano solo quando Alberto gliela ridà");
|
||||
await db.Exports.AttachDestinationAsync(w.Own, again.Id);
|
||||
|
||||
var own = (await db.Exports.GetDestinationAsync(w.Own))!;
|
||||
@@ -361,7 +474,8 @@ public sealed class ExportConstraintTests
|
||||
Assert.AreEqual(Make.T0.AddDays(3), (await db.Exports.GetDestinationAsync(w.Own))!.AutomaticSince);
|
||||
Assert.AreEqual(own.Id, (await db.Exports.GetAccountDestinationAsync(again.Id))!.Id);
|
||||
Assert.AreEqual(again.Id, (await db.Exports.GetCopyAsync(ownCopy.Id))!.AccountId);
|
||||
Assert.IsNull((await db.Exports.GetCopyAsync(generalCopy.Id))!.AccountId, "le copie di altre destinazioni restano orfane finché non si adottano");
|
||||
Assert.AreEqual(1L, await ScalarAsync(db, "SELECT COUNT(*) FROM export_copy WHERE id = @id AND removed_account_username IS NULL AND removed_account_instagram_id IS NULL", ("@id", ownCopy.Id)), "e l'account rimosso si dimentica");
|
||||
Assert.IsNull((await db.Exports.GetCopyAsync(generalCopy.Id))!.AccountId, "una copia orfana di un account sconosciuto resta orfana finché non si adotta");
|
||||
|
||||
var relinked = await db.Exports.RelinkOrphanCopyAsync(generalCopy.Id, again.Id, null, Make.T0.AddDays(2));
|
||||
Assert.AreEqual(again.Id, relinked.AccountId);
|
||||
@@ -499,6 +613,20 @@ public sealed class ExportConstraintTests
|
||||
}
|
||||
}
|
||||
|
||||
// Re-linking at each added account reads the orphans of that account through their index, never the copies of
|
||||
// the general folder (the handful of destinations is scanned, and the orphans found are grouped).
|
||||
foreach (var (name, sql) in new[] { ("RelinkCurrentOrphansSql", ExportStore.RelinkCurrentOrphansSql), ("RelinkHistoryOrphansSql", ExportStore.RelinkHistoryOrphansSql) })
|
||||
{
|
||||
var plan = await PlanAsync(db, sql, ("@a", w.Marta), ("@iid", "100"), ("@u", "marta.demo"));
|
||||
if (!plan.Contains("SEARCH o USING INTEGER PRIMARY KEY", StringComparison.Ordinal)
|
||||
|| !plan.Contains("ix_export_copy_orphan (removed_account_instagram_id=?)", StringComparison.Ordinal)
|
||||
|| !plan.Contains("ix_export_copy_orphan (removed_account_instagram_id=? AND removed_account_username=?)", StringComparison.Ordinal)
|
||||
|| plan.Contains("SCAN o", StringComparison.Ordinal) || plan.Contains("SCAN export_copy", StringComparison.Ordinal))
|
||||
{
|
||||
problems.Add($"{name}: {plan}");
|
||||
}
|
||||
}
|
||||
|
||||
Assert.IsEmpty(problems, $"{when}: {string.Join(" || ", problems)}");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user