using Febe.Core.Domain; using Febe.Core.Library; using Febe.Core.Storage; using Febe.Tests.Support; namespace Febe.Tests.Data; [TestClass] public sealed class AccountStoreTests { [TestMethod] public async Task AddReadUpdateRoundTripsEveryField() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var account = Make.Account("studio.lumen.demo", "17841400000000001") with { DisplayName = "Studio Lumen", IsPrivate = true, IsFollowed = true, Frequency = Frequency.Custom, CustomPostInterval = TimeSpan.FromHours(5), CustomStoryInterval = TimeSpan.FromHours(4), ContentTypes = ContentTypes.PostPhotos | ContentTypes.Stories, Limits = new AccountLimits { Depth = HistoryDepth.LastPosts, LastPosts = 500, BackfillPostsPerDay = 120, MaxVideoBytes = 200L * 1024 * 1024, StoryVideos = false, }, Folder = "/altrove/studio", BackfillCursor = "cursore-opaco", PostCount = 1280, NextProfileCheckAt = Make.T0.AddHours(3), }; var added = await db.Accounts.AddAsync(account); Assert.IsGreaterThan(0L, added.Id); var read = await db.Accounts.GetAsync(added.Id); Assert.AreEqual(added, read); var updated = read! with { Frequency = Frequency.High, CustomPostInterval = null, CustomStoryInterval = null, KeepUpdated = false }; await db.Accounts.UpdateAsync(updated); Assert.AreEqual(updated, await db.Accounts.GetAsync(added.Id)); Assert.AreEqual(added.Id, (await db.Accounts.FindByInstagramIdAsync("17841400000000001"))!.Id); Assert.AreEqual(added.Id, (await db.Accounts.FindByUsernameAsync("@Studio.Lumen.Demo"))!.Id); Assert.IsNull(await db.Accounts.FindByUsernameAsync("nessuno")); } [TestMethod] public void StartingPointsFollowTheSpecification() { var now = new DateTimeOffset(2026, 10, 13, 15, 0, 0, TimeSpan.Zero); var all = Account.Create("a", StartingPoint.EverythingAndKeepUpdated, now, TestZones.Rome); Assert.AreEqual(HistoryDepth.All, all.Limits.Depth); Assert.IsFalse(all.ContentTypes.HasFlag(ContentTypes.Tagged)); Assert.IsTrue(all.ContentTypes.HasFlag(ContentTypes.ProfilePicture)); Assert.IsNull(all.Limits.MaxVideoBytes); Assert.IsTrue(all.Limits.StoryVideos); Assert.AreEqual(300, all.Limits.BackfillPostsPerDay); Assert.AreEqual(Frequency.Normal, all.Frequency); var mine = Account.Create("tu", StartingPoint.EverythingAndKeepUpdated, now, TestZones.Rome, isMine: true); Assert.IsTrue(mine.ContentTypes.HasFlag(ContentTypes.Tagged)); var fromToday = Account.Create("b", StartingPoint.OnlyNewFromToday, now, TestZones.Rome, CautionProfile.Balanced); Assert.AreEqual(HistoryDepth.FromDate, fromToday.Limits.Depth); Assert.AreEqual(new DateTimeOffset(2026, 10, 12, 22, 0, 0, TimeSpan.Zero), fromToday.Limits.FromDate); // local midnight, CEST Assert.AreEqual(500, fromToday.Limits.BackfillPostsPerDay); var stories = Account.Create("c", StartingPoint.OnlyStories, now, TestZones.Rome); Assert.AreEqual(ContentTypes.Stories | ContentTypes.Highlights, stories.ContentTypes); Assert.IsTrue(stories.KeepUpdated); } [TestMethod] public async Task RenameKeepsTheFolderAndRaisesTheNotice() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var layout = new LibraryLayout(temp.Combine("libreria")); var account = await db.Accounts.AddAsync(Make.Account("marta.demo", "100")); var before = layout.AccountFolder(account); await db.Accounts.RenameAsync(account.Id, "marta.nuova", Make.T0); var renamed = (await db.Accounts.GetAsync(account.Id))!; Assert.AreEqual("marta.nuova", renamed.Username); Assert.AreEqual("marta.demo", renamed.PreviousUsername); Assert.AreEqual(before, layout.AccountFolder(renamed)); Assert.IsTrue(renamed.Notices.HasFlag(AccountNotices.Renamed)); Assert.IsTrue(renamed.NeedsAttention); await db.Accounts.ClearNoticesAsync(account.Id, AccountNotices.Renamed); Assert.IsFalse((await db.Accounts.GetAsync(account.Id))!.NeedsAttention); } [TestMethod] public async Task StateChecksAndBackfillAreUpdatedInPlace() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var account = await db.Accounts.AddAsync(Make.Account("marta.demo")); await db.Accounts.SetStateAsync(account.Id, AccountState.Unreachable, Make.T0.AddHours(1), UnreachableReason.PrivateNotFollowed); await db.Accounts.UpdateChecksAsync(account.Id, new AccountCheckTimes { LastProfileCheckAt = Make.T0, NextProfileCheckAt = Make.T0.AddHours(3), PostCount = 42, IsPrivate = true }); await db.Accounts.UpdateChecksAsync(account.Id, new AccountCheckTimes { LastStoriesCheckAt = Make.T0.AddMinutes(5) }); await db.Accounts.SetBackfillAsync(account.Id, "pagina-7", null); await db.Accounts.AddNoticesAsync(account.Id, AccountNotices.UnrecognizedFormat, Make.T0.AddHours(2)); var read = (await db.Accounts.GetAsync(account.Id))!; Assert.AreEqual(AccountState.Unreachable, read.State); Assert.AreEqual(UnreachableReason.PrivateNotFollowed, read.UnreachableReason); Assert.AreEqual(Make.T0.AddHours(1), read.StateChangedAt); Assert.AreEqual(Make.T0, read.LastProfileCheckAt); Assert.AreEqual(Make.T0.AddHours(3), read.NextProfileCheckAt); Assert.AreEqual(Make.T0.AddMinutes(5), read.LastStoriesCheckAt); Assert.AreEqual(42, read.PostCount); Assert.IsTrue(read.IsPrivate); Assert.AreEqual("pagina-7", read.BackfillCursor); Assert.AreEqual(Make.T0.AddHours(2), read.FormatUnrecognizedAt); await db.Accounts.SetStateAsync(account.Id, AccountState.UpToDate, Make.T0.AddHours(5)); read = (await db.Accounts.GetAsync(account.Id))!; Assert.IsNull(read.UnreachableReason); await Assert.ThrowsExactlyAsync(() => db.Accounts.SetStateAsync(999, AccountState.Paused, Make.T0)); } [TestMethod] public async Task SummariesAndStatsCountTheLibrary() { using var temp = new TempFolder(); await using var db = await TestDatabase.OpenAsync(temp); var account = await db.Accounts.AddAsync(Make.Account("marta.demo") with { PostCount = 10 }); await db.Entries.AddAsync(Make.Entry(account.Id, "p1", Make.T0), [Make.Stored(Make.Photo(), Make.Sha(1), "post/a.jpg")]); await db.Entries.AddAsync(Make.Entry(account.Id, "p2", Make.T0.AddHours(1)), [Make.Stored(Make.Photo(), Make.Sha(2), "post/b.jpg"), Make.Video(2)]); await db.Entries.AddAsync(Make.Entry(account.Id, "s1", Make.T0, EntryKind.Story) with { DiscoveredAt = Make.T0.AddHours(2) }, [Make.Stored(Make.Video(), Make.Sha(3), "storie/c.mp4")]); await db.Entries.AddAsync(Make.Entry(account.Id, "pp", Make.T0, EntryKind.ProfilePicture), [Make.Stored(Make.Photo(), Make.Sha(4), "profilo/d.jpg")]); await db.Entries.AddAsync(Make.Entry(account.Id, "todo", Make.T0), [Make.Photo()]); await db.Highlights.UpsertAsync(new Highlight { AccountId = account.Id, InstagramId = "h", Title = "Estate", DiscoveredAt = Make.T0 }); var summary = (await db.Accounts.ListSummariesAsync(Make.T0.AddHours(1))).Single(); Assert.AreEqual(3, summary.EntriesInLibrary); Assert.AreEqual(2, summary.Posts); Assert.AreEqual(0, summary.NewPostsSince); Assert.AreEqual(1, summary.NewStoriesSince); var stats = await db.Accounts.GetStatsAsync(account.Id); Assert.AreEqual(2, stats.Posts); Assert.AreEqual(1, stats.Stories); Assert.AreEqual(1, stats.ProfilePictures); Assert.AreEqual(3, stats.EntriesInLibrary); Assert.AreEqual(1, stats.HighlightCollections); Assert.AreEqual(4000L, stats.Bytes); Assert.AreEqual(2, stats.PendingFiles); Assert.AreEqual(Make.T0.AddHours(1), stats.LastPublishedAt); } }