using System.Text.Json; using Febe.Core.Collection; using Febe.Core.Collection.Readers; using Febe.Core.Domain; namespace Febe.Tests.Collection; /// /// The generic reader (spec §6.4) on the cleaned samples of tests/campioni/prova: captured from the test site, /// cleaned with «febe-sonda ripulisci» and checked with «febe-sonda verifica». The real Instagram samples have their /// own tests (). /// [TestClass] public sealed class ReaderTests { private static readonly ShapeReader Reader = new(ReaderVocabulary.LoadTestSite()); private static ReadResult ReadSample(string name) { var file = Directory.EnumerateFiles(Path.Combine(AppContext.BaseDirectory, "campioni", "prova"), $"*-{name}.json").Single(); using var document = JsonDocument.Parse(File.ReadAllText(file)); return Reader.Read(document.RootElement.GetProperty("corpo")); } [TestMethod] public void APostWithSeveralFilesKeepsTheirOrderAndTheBestVersions() { var result = ReadSample("post-serie"); Assert.AreEqual(ReadShape.Recognized, result.Shape); var post = result.Contents.Single(); Assert.IsTrue(post.IsComplete); Assert.IsNotNull(post.Code); Assert.AreEqual(3, post.Files.Count); CollectionAssert.AreEqual(new[] { MediaKind.Photo, MediaKind.Video, MediaKind.Photo }, post.Files.Select(f => f.Kind).ToArray()); Assert.AreEqual((960, 1200), (post.Files[0].Best.Width, post.Files[0].Best.Height), "The largest image, not the first one."); Assert.IsTrue(post.Files.All(f => f.InstagramId is not null), "Files of a series have their own identifiers."); Assert.IsNotNull(post.Owner); } [TestMethod] public void AVideoTakesTheBestRenditionWithAudioAndItsCover() { var video = ReadSample("post-serie").Contents.Single().Files[1]; Assert.AreEqual(MediaKind.Video, video.Kind); Assert.AreEqual(720, video.Best.Width, "The 1080 rendition has no audio."); Assert.AreEqual(true, video.Best.HasAudio); Assert.AreEqual(TimeSpan.FromSeconds(2), video.Duration); Assert.IsNotNull(video.Cover); Assert.AreEqual(720, video.Cover.Width); var stories = ReadSample("storie").Contents; Assert.AreEqual(4, stories.Count); Assert.IsTrue(stories.All(s => s.ExpiresAt > s.TakenAt)); Assert.AreEqual(1, stories.Count(s => s.Files[0].Kind == MediaKind.Video)); Assert.AreEqual(1, stories.Select(s => s.Owner!.Id).Distinct().Count(), "The author of the list is inherited by every story."); } [TestMethod] public void APrivateProfileNotFollowedGivesItsStateAndNoContent() { var result = ReadSample("profilo-privato-non-seguito"); Assert.AreEqual(ReadShape.Recognized, result.Shape); var profile = result.Profiles.Single(); Assert.AreEqual(true, profile.IsPrivate); Assert.AreEqual(false, profile.IsFollowed); Assert.AreEqual(0, result.Contents.Count); Assert.AreEqual(false, result.Page?.HasMore); } [TestMethod] public void AnEmptyPageIsEmptyNotUnknown() { var result = ReadSample("pagina-vuota"); Assert.AreEqual(ReadShape.Empty, result.Shape); Assert.AreEqual(false, result.Page?.HasMore); Assert.IsNull(result.KeyStructure); } [TestMethod] public void AnUnknownShapeIsReportedWithItsKeyStructureOnly() { var file = Directory.EnumerateFiles(Path.Combine(AppContext.BaseDirectory, "campioni", "prova"), "*-forma-sconosciuta.json").Single(); using var document = JsonDocument.Parse(File.ReadAllText(file)); var body = document.RootElement.GetProperty("corpo"); var result = Reader.Read(body); Assert.AreEqual(ReadShape.Unrecognized, result.Shape); Assert.AreEqual("{risultati:{voci:[{chiave,valore,dettagli:{x,y}}]},versione}", result.KeyStructure); foreach (var value in body.GetProperty("risultati").GetProperty("voci").EnumerateArray().Select(v => v.GetProperty("valore").GetString()!)) { Assert.IsFalse(result.KeyStructure!.Contains(value, StringComparison.Ordinal), "No value in the event."); } } [TestMethod] public void TheHomeGivesTheConnectedAccountTheStoriesBarAndIncompleteSeries() { var result = ReadSample("inizio"); Assert.IsNotNull(result.Viewer, "H10 on the test site: the connected account comes with the data."); var bar = result.Profiles.Where(p => p.LatestStoryAt is not null).ToList(); Assert.AreEqual(4, bar.Count); Assert.AreEqual(6, result.Contents.Count); Assert.IsTrue(result.Contents.Any(c => !c.IsComplete && c.Files.Count == 1), "A series with only its first file: incomplete."); Assert.AreEqual(true, result.Page?.HasMore); Assert.AreEqual(2, result.Page?.Cursor?.Length, "An opaque resume point (cleaned: same shape as «c6»)."); } [TestMethod] public void AProfilePageGivesPinnedPostsHighlightsAndTheNextPage() { var result = ReadSample("profilo-pubblico"); Assert.AreEqual(12, result.Contents.Count); Assert.AreEqual(2, result.Contents.Count(c => c.Pinned)); Assert.IsTrue(result.Contents.Take(2).All(c => c.Pinned), "Pinned posts come first on the grid."); CollectionAssert.AreEqual(new[] { "Qspblr", "Flvwvq" }.Select(t => t.Length).ToArray(), result.Highlights.Select(h => h.Title.Length).ToArray()); Assert.AreEqual(3, result.Page?.Cursor?.Length); var profile = result.Profiles.First(p => p.PostCount is not null); Assert.AreEqual(18, profile.PostCount); Assert.IsNotNull(profile.Picture); Assert.AreEqual(320, profile.Picture.Width, "The largest profile picture."); } [TestMethod] public void AHighlightCarriesItsItemsInOrder() { var result = ReadSample("raccolta"); var highlight = result.Highlights.Single(); Assert.AreEqual(3, highlight.Items.Count); Assert.IsTrue(highlight.Items.All(i => i.HighlightId == highlight.Id)); CollectionAssert.AreEqual(new[] { 0, 1, 2 }, highlight.Items.Select(i => i.Order).ToArray()); } [TestMethod] public void TheFollowingListAndTheTaggedTabAreRead() { var following = ReadSample("seguiti"); Assert.AreEqual(10, following.Profiles.Count); Assert.AreEqual(3, following.Page?.Cursor?.Length); var tagged = ReadSample("taggati"); Assert.AreEqual(3, tagged.Contents.Count); Assert.AreEqual(1, tagged.Profiles.Count(p => p.PostCount is not null), "The tagged account itself."); Assert.IsTrue(tagged.Contents.All(c => c.Owner!.Id != tagged.Profiles.First(p => p.PostCount is not null).Id), "Tagged posts belong to others."); } [TestMethod] public void DatesAreReadAsIsoTextOrUnixSeconds() { var iso = Reader.Read("""{"numero":"1","pubblicato":"2026-10-13T08:00:00Z","foto":[{"indirizzo":"https://cdn.esempio/a.jpg","larghezza":1,"altezza":1}]}"""); var unix = Reader.Read("""{"numero":"1","pubblicato":1791878400,"foto":[{"indirizzo":"https://cdn.esempio/a.jpg","larghezza":1,"altezza":1}]}"""); var millis = Reader.Read("""{"numero":"1","pubblicato":1791878400000,"foto":[{"indirizzo":"https://cdn.esempio/a.jpg","larghezza":1,"altezza":1}]}"""); Assert.AreEqual(new DateTimeOffset(2026, 10, 13, 8, 0, 0, TimeSpan.Zero), iso.Contents.Single().TakenAt); Assert.AreEqual(unix.Contents.Single().TakenAt, millis.Contents.Single().TakenAt); } [TestMethod] public void AVideoWithoutAnyAudioRenditionTakesTheLargest() { var result = Reader.Read(""" {"numero":"9","pubblicato":"2026-10-13T08:00:00Z","durata":3.5, "filmati":[{"indirizzo":"https://cdn.esempio/a.mp4","larghezza":360,"altezza":640,"audio":false}, {"indirizzo":"https://cdn.esempio/b.mp4","larghezza":720,"altezza":1280,"audio":false}]} """); var file = result.Contents.Single().Files.Single(); Assert.AreEqual(720, file.Best.Width); Assert.AreEqual(TimeSpan.FromSeconds(3.5), file.Duration); } [TestMethod] public void MappingGivesDomainRecords() { var item = ReadSample("post-serie").Contents.Single(); var now = new DateTimeOffset(2026, 10, 13, 9, 0, 0, TimeSpan.Zero); var entry = ReadMapping.ToEntry(item, 7, EntryKind.Post, now); Assert.AreEqual(item.Id, entry.InstagramId); Assert.AreEqual(item.Code, entry.Code); Assert.AreEqual(item.TakenAt, entry.PublishedAt); Assert.IsNull(entry.ExpiresAt); var media = ReadMapping.ToMedia(item, now); CollectionAssert.AreEqual(new[] { 1, 2, 3 }, media.Select(m => m.Position).ToArray()); Assert.IsTrue(media.All(m => m.SourceUrl is not null && m.SourceSeenAt == now)); Assert.IsNotNull(media[1].CoverUrl); var story = ReadSample("storie").Contents[0]; Assert.AreEqual(story.ExpiresAt, ReadMapping.ToEntry(story, 7, EntryKind.Story, now).ExpiresAt); var tagged = ReadMapping.ToEntry(item, 7, EntryKind.Tagged, now); Assert.AreEqual(item.Owner!.Username, tagged.AuthorUsername); } [TestMethod] public void KeyStructureHidesIdentifierKeysAndValues() { using var document = JsonDocument.Parse("""{"elenco":{"17841400000":{"nome":"segreto"},"17841400001":{"nome":"altro"}},"conteggio":2,"lista":[]}"""); var structure = KeyStructure.Describe(document.RootElement); Assert.AreEqual("{elenco:{#:{nome}},conteggio,lista:[]}", structure); Assert.IsTrue(KeyStructure.LooksLikeIdentifier("17841400000")); Assert.IsTrue(KeyStructure.LooksLikeIdentifier("a1b2c3d4e5f6a7b8c9")); Assert.IsFalse(KeyStructure.LooksLikeIdentifier("image_versions2")); Assert.IsFalse(KeyStructure.LooksLikeIdentifier("larghezza")); } [TestMethod] public void TheShippedVocabulariesLoad() { var test = ReaderVocabulary.LoadTestSite(); Assert.IsTrue(test.IsUsable, test.UnusableReason); Assert.AreEqual(0, test.Relevance.Count, "On the test site every unknown answer is a format change."); Assert.IsTrue(File.Exists(Path.Combine(AppContext.BaseDirectory, "config", "lettori.prova.json")), "Editable without recompiling."); Assert.IsTrue(File.Exists(Path.Combine(AppContext.BaseDirectory, "config", "lettori.instagram.json"))); // Instagram's vocabulary has its own tests on the real cleaned samples (InstagramReaderTests). Assert.IsTrue(ReaderVocabulary.LoadInstagram().IsUsable); } [TestMethod] [DataRow("""{ "site": "x", "content": { "id": ["a"], "colore": ["b"] } }""", DisplayName = "Unknown concepts are refused")] [DataRow("""{ "content": {} }""", DisplayName = "The site is required")] [DataRow("""{ "site": "x", "status": "forse" }""", DisplayName = "Unknown status")] [DataRow("""{ "site": """, DisplayName = "Broken JSON")] public void InvalidVocabulariesAreRefused(string json) => Assert.ThrowsExactly(() => ReaderVocabulary.Parse(json, "lettori.test.json")); [TestMethod] public void TheCollectorKeepsReadsInOrderAndTracksPendingBodies() { var clock = new Febe.Tests.Browser.TestSupport.VirtualClock(); var collector = new Febe.Core.Collection.Recipes.CaptureCollector(Reader, clock); var observer = (IResponseObserver)collector; var descriptor = new ResponseDescriptor("r1", HostKind.Site, "http://127.0.0.1/dati", "127.0.0.1", "/dati", 200, "application/json", "Fetch", false, clock.Now); observer.OnResponse(descriptor); Assert.AreEqual(1, collector.Pending); var mark = collector.Mark(); using var document = JsonDocument.Parse("""{"contenuti":[],"pagina":{"altro":false}}"""); collector.Accept(descriptor, document.RootElement); Assert.AreEqual(0, collector.Pending); Assert.AreEqual(ReadShape.Empty, collector.Since(mark).Single().Result.Shape); Assert.IsNull(collector.Accept(descriptor with { HostKind = HostKind.Cdn }, document.RootElement), "Only the site's data is read."); } }