M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
69 files changed
+12472
No files matched your search
@@ -0,0 +1,148 @@
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Storage;
|
||||
using Febe.Data;
|
||||
using Febe.Tests.Support;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace Febe.Tests.Data;
|
||||
|
||||
[TestClass]
|
||||
public sealed class ConstraintTests
|
||||
{
|
||||
[TestMethod]
|
||||
public async Task EntryIsUniquePerAccountKindAndInstagramId()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
var marta = await db.Accounts.AddAsync(Make.Account("marta.demo", "100"));
|
||||
var luca = await db.Accounts.AddAsync(Make.Account("luca.demo", "200"));
|
||||
|
||||
var first = await db.Entries.AddAsync(Make.Entry(marta.Id, "555", Make.T0), [Make.Photo()]);
|
||||
var again = await db.Entries.AddAsync(Make.Entry(marta.Id, "555", Make.T0.AddDays(1), caption: "diversa"), [Make.Photo(), Make.Photo(2)]);
|
||||
Assert.IsTrue(first.Created);
|
||||
Assert.IsFalse(again.Created);
|
||||
Assert.AreEqual(first.Stored.Entry.Id, again.Stored.Entry.Id);
|
||||
Assert.HasCount(1, again.Stored.Media);
|
||||
|
||||
// Same post as a tagged post of another account, and as another kind: separate entries (spec §8.3).
|
||||
var tagged = await db.Entries.AddAsync(Make.Entry(luca.Id, "555", Make.T0, EntryKind.Tagged), [Make.Photo()]);
|
||||
var story = await db.Entries.AddAsync(Make.Entry(marta.Id, "555", Make.T0, EntryKind.Story), [Make.Photo()]);
|
||||
Assert.IsTrue(tagged.Created);
|
||||
Assert.IsTrue(story.Created);
|
||||
|
||||
var raw = await Assert.ThrowsExactlyAsync<SqliteException>(() => db.WriteAsync(
|
||||
w => w.Command("""
|
||||
INSERT INTO entry (account_id, kind, instagram_id, published_at, state, provenance, discovered_at)
|
||||
VALUES (@a, 'Post', '555', 0, 'ToDownload', 'Downloaded', 0)
|
||||
""").With("@a", marta.Id).ExecuteNonQuery(),
|
||||
default));
|
||||
Assert.IsTrue(raw.IsUniqueViolation());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task MediaIsUniquePerEntryAndPosition()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
var account = await db.Accounts.AddAsync(Make.Account("marta.demo"));
|
||||
|
||||
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Entries.AddAsync(Make.Entry(account.Id, "1", Make.T0), [Make.Photo(1), Make.Video(1)]));
|
||||
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.Entries.AddAsync(Make.Entry(account.Id, "1", Make.T0), [Make.Photo(0)]));
|
||||
|
||||
var added = await db.Entries.AddAsync(Make.Entry(account.Id, "1", Make.T0), [Make.Photo(1), Make.Video(2)]);
|
||||
var raw = await Assert.ThrowsExactlyAsync<SqliteException>(() => db.WriteAsync(
|
||||
w => w.Command("INSERT INTO media (entry_id, position, kind, state) VALUES (@e, 2, 'Photo', 'ToDownload')")
|
||||
.With("@e", added.Stored.Entry.Id).ExecuteNonQuery(),
|
||||
default));
|
||||
Assert.IsTrue(raw.IsUniqueViolation());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task ForeignKeysAreEnforcedAndCascade()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
|
||||
var orphan = await Assert.ThrowsExactlyAsync<SqliteException>(() => db.WriteAsync(
|
||||
w => w.Command("INSERT INTO media (entry_id, position, kind, state) VALUES (9999, 1, 'Photo', 'ToDownload')").ExecuteNonQuery(),
|
||||
default));
|
||||
Assert.IsTrue(orphan.IsConstraintViolation());
|
||||
|
||||
var account = await db.Accounts.AddAsync(Make.Account("marta.demo"));
|
||||
var entry = await db.Entries.AddAsync(Make.Entry(account.Id, "1", Make.T0), [Make.Photo(1), Make.Photo(2)]);
|
||||
var highlight = await db.Highlights.UpsertAsync(new Highlight { AccountId = account.Id, InstagramId = "h1", Title = "Estate", DiscoveredAt = Make.T0 });
|
||||
await db.Highlights.AddItemAsync(highlight.Id, entry.Stored.Entry.Id);
|
||||
await db.ActionLog.AppendAsync(new ActionLogRecord { At = Make.T0, Category = Core.Abstractions.ActionKind.ProfileOpen, AccountId = account.Id });
|
||||
|
||||
await db.Accounts.DeleteAsync(account.Id);
|
||||
|
||||
Assert.IsNull(await db.Entries.GetAsync(entry.Stored.Entry.Id));
|
||||
Assert.IsEmpty(await db.Media.ListByEntryAsync(entry.Stored.Entry.Id));
|
||||
Assert.IsNull(await db.Highlights.GetAsync(highlight.Id));
|
||||
// The action log keeps counting for the budgets: only the account reference goes.
|
||||
var actions = await db.ActionLog.ListAsync(Make.T0.AddHours(-1), Make.T0.AddHours(1));
|
||||
Assert.HasCount(1, actions);
|
||||
Assert.IsNull(actions[0].AccountId);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task AccountInstagramIdAndOwnProfileAreUnique()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
await db.Accounts.AddAsync(Make.Account("tuo.profilo", "1", mine: true));
|
||||
|
||||
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Accounts.AddAsync(Make.Account("altro", "1")));
|
||||
await Assert.ThrowsExactlyAsync<DuplicateRecordException>(() => db.Accounts.AddAsync(Make.Account("secondo.tu", "2", mine: true)));
|
||||
|
||||
// Archive-only accounts without an identifier can coexist.
|
||||
await db.Accounts.AddAsync(Make.Account("archivio.uno") with { InstagramId = null, State = AccountState.ArchiveOnly });
|
||||
await db.Accounts.AddAsync(Make.Account("archivio.due") with { InstagramId = null, State = AccountState.ArchiveOnly });
|
||||
Assert.HasCount(3, await db.Accounts.ListAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task RequiredIndexesExist()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
var indexes = await db.ReadAsync(
|
||||
c =>
|
||||
{
|
||||
using var r = c.Command("SELECT name, tbl_name, sql FROM sqlite_master WHERE type = 'index' AND sql IS NOT NULL").ExecuteReader();
|
||||
var list = new List<(string Name, string Table, string Sql)>();
|
||||
while (r.Read())
|
||||
{
|
||||
list.Add((r.GetString(0), r.GetString(1), r.GetString(2)));
|
||||
}
|
||||
|
||||
return list;
|
||||
},
|
||||
default);
|
||||
|
||||
// Spec §8.3: indexes on (account, publication date), SHA-256, perceptual hash, state.
|
||||
Assert.Contains(i => i.Table == "entry" && i.Sql.Contains("(account_id, published_at)", StringComparison.Ordinal), indexes);
|
||||
Assert.Contains(i => i.Table == "media" && i.Sql.Contains("(sha256)", StringComparison.Ordinal), indexes);
|
||||
Assert.Contains(i => i.Table == "media" && i.Sql.Contains("(phash)", StringComparison.Ordinal), indexes);
|
||||
Assert.Contains(i => i.Table == "entry" && i.Sql.Contains("(state)", StringComparison.Ordinal), indexes);
|
||||
Assert.Contains(i => i.Table == "media" && i.Sql.Contains("(state)", StringComparison.Ordinal), indexes);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TimesAreStoredInUtc()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
await using var db = await TestDatabase.OpenAsync(temp);
|
||||
var account = await db.Accounts.AddAsync(Make.Account("marta.demo"));
|
||||
var local = new DateTimeOffset(2026, 10, 10, 18, 22, 5, TimeSpan.FromHours(2));
|
||||
var added = await db.Entries.AddAsync(Make.Entry(account.Id, "1", local), [Make.Photo()]);
|
||||
|
||||
var raw = await db.ReadAsync(c => c.Command("SELECT published_at FROM entry WHERE id = @id").With("@id", added.Stored.Entry.Id).ScalarInt64(), default);
|
||||
Assert.AreEqual(new DateTimeOffset(2026, 10, 10, 16, 22, 5, TimeSpan.Zero).ToUnixTimeMilliseconds(), raw);
|
||||
|
||||
var read = await db.Entries.GetAsync(added.Stored.Entry.Id);
|
||||
Assert.IsNotNull(read);
|
||||
Assert.AreEqual(local, read.PublishedAt);
|
||||
Assert.AreEqual(TimeSpan.Zero, read.PublishedAt.Offset);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user