using System.Net; using System.Text.Json; using Febe.Core.Domain; using Febe.Tests.Support; using Febe.Tests.Web.Support; namespace Febe.Tests.Web; /// /// «Tutte le foto» through the API (spec §4.6): mode=files on the days and on the windows, one tile per file /// with its position, cursors that go on inside a post, day rows that count files and totals that say both. /// [TestClass] public sealed class LibraryFilesApiTests { [TestMethod] public async Task WindowsOfFilesWalkEveryFileOnceAndTheDaysCountThem() { await using var server = await WebTestServer.StartAsync(); var (entries, files) = await FillAsync(server); var days = await server.GetJsonAsync("/api/library/days?mode=files"); Assert.AreEqual(entries, days.Int("entries"), "the totals still count the entries"); Assert.AreEqual(files, days.Int("files")); Assert.AreEqual(files, days.Items("days").Sum(d => d.Int("count")), "the day rows count the files"); Assert.AreEqual(JsonValueKind.Null, (await server.GetJsonAsync("/api/library/days")).GetProperty("files").ValueKind, "grouped: no file total"); var seen = new List<(long Ms, long Id, int Position, long Media)>(); string? cursor = null; var pages = 0; do { var page = await server.GetJsonAsync("/api/library?mode=files&limit=4" + (cursor is null ? string.Empty : "&cursor=" + Uri.EscapeDataString(cursor))); foreach (var tile in page.Items("tiles")) { seen.Add((tile.Long("publishedAt"), tile.Long("id"), tile.Int("position"), tile.GetProperty("cover").Long("mediaId"))); } cursor = page.GetProperty("older").ValueKind == JsonValueKind.Null ? null : page.Str("older"); pages++; } while (cursor is not null && pages < 50); Assert.HasCount(files, seen); Assert.HasCount(files, seen.Select(s => s.Media).Distinct(), "every file once"); Assert.HasCount(entries, seen.Select(s => s.Id).Distinct()); for (var i = 1; i < seen.Count; i++) { var (a, b) = (seen[i - 1], seen[i]); Assert.IsTrue(b.Ms < a.Ms || (b.Ms == a.Ms && (b.Id < a.Id || (b.Id == a.Id && b.Position > a.Position))), "entries newest first, each by position"); } // The cursor of a day starts at that day, in files. foreach (var day in days.Items("days").Take(3)) { var page = await server.GetJsonAsync($"/api/library?mode=files&limit={day.Int("count")}&cursor={Uri.EscapeDataString(day.Str("cursor"))}"); Assert.IsTrue(page.Items("tiles").All(t => t.Str("day") == day.Str("day")), day.Str("day")); Assert.AreEqual(day.Int("count"), page.Items("tiles").Count()); } // From inside a carousel: the next file of the same post, and back up. var inside = seen.FindIndex(s => s.Position == 2); var at = seen[inside]; var next = await server.GetJsonAsync($"/api/library?mode=files&limit=1&cursor={at.Ms}.{at.Id}.{at.Position}"); Assert.AreEqual(seen[inside + 1].Media, next.Items("tiles").Single().GetProperty("cover").Long("mediaId")); var back = await server.GetJsonAsync($"/api/library?mode=files&limit=2&direction=newer&cursor={at.Ms}.{at.Id}.{at.Position}"); CollectionAssert.AreEqual(seen.Skip(inside - 2).Take(2).Select(s => s.Media).ToList(), back.Items("tiles").Select(t => t.GetProperty("cover").Long("mediaId")).ToList()); // A file tile says the files of its post; the grouped tiles have no position. var carousel = seen.First(s => s.Position == 3); var tiles = (await server.GetJsonAsync("/api/library?mode=files&limit=200")).Items("tiles").Where(t => t.Long("id") == carousel.Id).ToList(); Assert.IsTrue(tiles.All(t => t.Int("files") == tiles.Count)); Assert.IsTrue((await server.GetJsonAsync("/api/library?limit=200")).Items("tiles").All(t => t.GetProperty("position").ValueKind == JsonValueKind.Null)); } [TestMethod] public async Task FiltersWorkTheSameAndBadValuesAreRefused() { await using var server = await WebTestServer.StartAsync(); await FillAsync(server); // «Video» shows only the video files, «Foto» only the photos. var videos = await server.GetJsonAsync("/api/library?mode=files&kind=videos&limit=200"); Assert.IsTrue(videos.Items("tiles").All(t => t.GetProperty("cover").Str("kind") == "video")); Assert.AreEqual(videos.Items("tiles").Count(), (await server.GetJsonAsync("/api/library/days?mode=files&kind=videos")).Int("files")); var photos = await server.GetJsonAsync("/api/library?mode=files&kind=photos&limit=200"); Assert.IsTrue(photos.Items("tiles").All(t => t.GetProperty("cover").Str("kind") == "photo")); Assert.AreEqual(4, (await server.GetJsonAsync("/api/library/days?mode=files&kind=stories")).Int("files")); Assert.AreEqual(HttpStatusCode.BadRequest, (await server.SendJsonAsync(HttpMethod.Get, "/api/library?mode=gatti")).Status); Assert.AreEqual(HttpStatusCode.BadRequest, (await server.SendJsonAsync(HttpMethod.Get, "/api/library/days?mode=tutte")).Status); Assert.AreEqual(HttpStatusCode.BadRequest, (await server.SendJsonAsync(HttpMethod.Get, "/api/library?mode=files&cursor=1.2.x")).Status); Assert.AreEqual(HttpStatusCode.BadRequest, (await server.SendJsonAsync(HttpMethod.Get, "/api/library?mode=files&cursor=1.2.-1")).Status); Assert.AreEqual(HttpStatusCode.BadRequest, (await server.SendJsonAsync(HttpMethod.Get, "/api/library?cursor=99999999999999999.2")).Status, "a time outside the calendar"); } /// Ten posts of one to four files (the third of a series is a video) and four stories: entries and files. private static async Task<(int Entries, int Files)> FillAsync(WebTestServer server) { var account = await server.Db.Accounts.AddAsync(Make.Account("marta.demo")); var files = 0; for (var i = 0; i < 10; i++) { var count = (i % 4) + 1; var media = Enumerable.Range(1, count) .Select(p => Make.Stored(p == 3 ? Make.Video(p) : Make.Photo(p), Make.Sha((i * 10) + p), $"p/{i}_{p}.jpg")) .ToList(); await server.Db.Entries.AddAsync(Make.Entry(account.Id, $"p{i}", Make.T0 - TimeSpan.FromHours(9 * i)), media); files += count; } for (var i = 0; i < 4; i++) { await server.Db.Entries.AddAsync(Make.Entry(account.Id, $"s{i}", Make.T0 - TimeSpan.FromHours((13 * i) + 2), EntryKind.Story), [Make.Stored(Make.Photo(), Make.Sha(500 + i), $"s/{i}.jpg")]); files++; } return (14, files); } }