using Febe.Tests.Support; using Febe.Tests.Web.Support; using Febe.Web.Demo; namespace Febe.Tests.Web; /// /// «Raggruppa i post» / «Tutte le foto» in a real browser, on the demo (spec §4.6, tappa D1): the switch of the tools /// row, the file tiles with their place («2/4»), the day rows in files, the viewer that opens on the file and counts /// files, the selection of files that acts on their entries, the choice remembered per page (Libreria apart from the /// account views) and grouped when the browser keeps nothing. Chromium only (CI). /// [TestClass] public sealed class LibraryModeInterfaceTests { private const string Switch = "document.querySelector('.strumenti .modo-mosaico')"; private const string Mode = Switch + ".querySelector('[aria-checked=true]').dataset.value"; /// The viewer shows a video: the player, or (the demo has no video files on disk) the cover of a missing video. private const string ShowsAVideo = "(document.querySelector('.vis .scena video') !== null || /video/i.test(document.querySelector('.vis .scena .mancante')?.textContent || ''))"; /// /// Walks «Video» in «Tutte le foto» through the API, newest first, to the first video that is the second file of a /// series whose first file is no tile there: «dal=…&al=…», from its day to the day of the tile before it, or "". /// private const string FindAVideoInSecondPlace = """ (async () => { let cursor = null; const list = []; for (let page = 0; page < 60; page++) { const answer = await (await fetch('/api/library?mode=files&kind=videos&limit=500' + (cursor ? '&cursor=' + cursor : ''))).json(); list.push(...answer.tiles); const at = list.findIndex((t, i) => i > 0 && t.position === 2 && t.files > 1 && t.cover && t.cover.kind === 'video' && !list.some((o) => o.id === t.id && o.position === 1)); if (at > 0) return 'dal=' + list[at].day + '&al=' + list[at - 1].day; if (!answer.older) return ''; cursor = answer.older; } return ''; })() """; [TestMethod] public async Task TutteLeFotoShowsTheFilesOpensTheViewerOnTheFileAndIsRememberedPerPage() { await using var ui = await UiBrowser.StartAsync(1440, 900); using var temp = new TempFolder("modo-ui"); await using var demo = await DemoHost.StartAsync(new DemoOptions(0, TestZones.Rome, temp.Path)); await ui.OpenAsync(new Uri(demo.Address, "/libreria")); await ui.WaitAsync($"{Switch} !== null && document.querySelector('.mosaico .tess') !== null", "Libreria con l'interruttore"); Assert.AreEqual("grouped", (await ui.EvalAsync(Mode)).GetString(), "predefinito «Raggruppa i post» " + ui.Report()); Assert.AreEqual(2, (await ui.EvalAsync($"{Switch}.querySelectorAll('[role=radio]').length")).GetInt32()); var entries = (await ui.EvalAsync("document.querySelector('.quanti')?.textContent || ''")).GetString(); StringAssert.Contains(entries, "elementi"); // «Tutte le foto»: tiles of single files, those of a series with their place. await ui.EvalAsync($"{Switch}.querySelector('[data-value=files]').click()"); await ui.WaitAsync("[...document.querySelectorAll('.mosaico .tess .segno span')].some((s) => /^\\d+\\/\\d+$/.test(s.textContent))", "tessere con «2/4»"); StringAssert.Contains((await ui.EvalAsync("document.querySelector('.mosaico .giorno span')?.textContent || ''")).GetString(), "file", "la riga del giorno conta i file"); await ui.WaitAsync("/ file in .* elementi di /.test(document.querySelector('.quanti')?.textContent || '')", "«61.034 file in 48.212 elementi di 38 account»"); // The viewer opens on the second file of a series and counts files. const string Second = "[...document.querySelectorAll('.mosaico .tess')].find((t) => /^2\\/\\d+$/.test(t.querySelector('.segno span')?.textContent || ''))"; await ui.WaitAsync($"({Second}) !== undefined", "un secondo file di una serie"); await ui.EvalAsync($"({Second}).click()"); await ui.WaitAsync("document.querySelector('.vis .serie button[aria-current=true]') !== null", "visualizzatore con la serie"); Assert.AreEqual(1, (await ui.EvalAsync("[...document.querySelectorAll('.vis .serie button')].findIndex((b) => b.getAttribute('aria-current') === 'true')")).GetInt32(), "aperto sul file della tessera"); StringAssert.Contains((await ui.EvalAsync("document.querySelector('.vis .dove').textContent")).GetString(), "file", "«Libreria, file 12 di …»"); var where = (await ui.EvalAsync("document.querySelector('.vis .dove').textContent")).GetString(); await ui.EvalAsync("document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight', bubbles: true }))"); await ui.WaitAsync($"document.querySelector('.vis .dove').textContent !== {System.Text.Json.JsonSerializer.Serialize(where)}", "la freccia va al file seguente"); await ui.EvalAsync("document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))"); await ui.WaitAsync("document.querySelector('.vis') === null", "Esc chiude"); // Selection of files: two files of one post are one entry for «Elimina». await ui.EvalAsync("[...document.querySelectorAll('.strumenti .btn')].find((b) => b.textContent.includes('Seleziona')).click()"); await ui.EvalAsync($"(() => {{ const t = {Second}; const entry = t.dataset.entry; const all = [...document.querySelectorAll(`.mosaico .tess[data-entry='${{entry}}']`)]; all.slice(0, 2).forEach((x) => x.click()); return all.length; }})()"); await ui.WaitAsync("/2 file scelti, di 1 elemento/.test(document.querySelector('.salva .nota')?.textContent || '')", "«2 file scelti, di 1 elemento»"); StringAssert.Contains((await ui.EvalAsync("document.querySelector('.salva .btn.pericolo').textContent")).GetString(), "Elimina 1 elemento"); // Remembered for the Libreria, not for the account views. await ui.OpenAsync(new Uri(demo.Address, "/libreria")); await ui.WaitAsync($"{Switch} !== null", "Libreria di nuovo"); Assert.AreEqual("files", (await ui.EvalAsync(Mode)).GetString(), "la Libreria ricorda «Tutte le foto»"); await ui.OpenAsync(new Uri(demo.Address, "/account")); await ui.WaitAsync("document.querySelector('a[href^=\"/account/\"]') !== null", "elenco degli account"); await ui.EvalAsync("document.querySelector('a[href^=\"/account/\"]').click()"); await ui.WaitAsync($"location.pathname.startsWith('/account/') && {Switch} !== null", "scheda account con l'interruttore"); Assert.AreEqual("grouped", (await ui.EvalAsync(Mode)).GetString(), "le viste degli account hanno la loro scelta"); } [TestMethod] public async Task UnderVideoTheArrowsFollowTheTilesAndTheChosenVoiceChangesNothing() { await using var ui = await UiBrowser.StartAsync(1440, 900); using var temp = new TempFolder("modo-video-ui"); await using var demo = await DemoHost.StartAsync(new DemoOptions(0, TestZones.Rome, temp.Path)); // In the demo the videos that are the second file of a post (the first a photo, no tile under «Video») are few and // far back in time: the API finds the first one, and the Libreria opens on its days, from it to the tile before. await ui.OpenAsync(new Uri(demo.Address, "/libreria?tipo=video")); await ui.WaitAsync($"{Switch} !== null && document.querySelector('.mosaico .tess') !== null", "Libreria con «Video»"); var days = (await ui.EvalAsync(FindAVideoInSecondPlace)).GetString(); Assert.IsFalse(string.IsNullOrEmpty(days), "la demo ha un video in seconda posizione dopo una foto"); await ui.OpenAsync(new Uri(demo.Address, "/libreria?tipo=video&" + days)); await ui.WaitAsync($"{Switch} !== null && document.querySelector('.mosaico .tess') !== null", "Libreria con «Video» in quei giorni"); await ui.EvalAsync($"{Switch}.querySelector('[data-value=files]').click()"); // That video, not the first tile. A video tile has two marks, its length and its place: the place is the one. const string Second = "[...document.querySelectorAll('.mosaico .tess')].slice(1).find((t) => [...t.querySelectorAll('.segno span')].some((s) => /^2\\/\\d+$/.test(s.textContent.trim())))"; await ui.WaitAsync($"({Second}) !== undefined", "un video in seconda posizione"); // Clicking the voice already chosen keeps the mosaic, its place and the selection. await ui.EvalAsync("document.querySelector('.mosaico').dataset.prova = 'stesso'"); await ui.EvalAsync($"{Switch}.querySelector('[data-value=files]').click()"); Assert.AreEqual("stesso", (await ui.EvalAsync("document.querySelector('.mosaico')?.dataset.prova || ''")).GetString(), "«Tutte le foto» di nuovo non ridisegna il mosaico"); await ui.EvalAsync($"({Second}).click()"); await ui.WaitAsync($"{ShowsAVideo} && /file/.test(document.querySelector('.vis .dove').textContent)", "visualizzatore sul video"); const string Index = "Number((document.querySelector('.vis .dove').textContent.match(/file ([\\d.]+)/) || [])[1]?.replace(/\\./g, '') || 0)"; var at = (await ui.EvalAsync(Index)).GetInt32(); Assert.IsGreaterThan(1, at, ui.Report()); // Left: the photo before it is not a tile, so the viewer goes to the tile before, a video of another post. await ui.EvalAsync("document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowLeft', bubbles: true }))"); await ui.WaitAsync($"{Index} === {at - 1}", "la freccia va alla tessera precedente"); await ui.WaitAsync(ShowsAVideo, "di nuovo un video, mai la foto filtrata"); await ui.EvalAsync("document.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight', bubbles: true }))"); await ui.WaitAsync($"{Index} === {at}", "e torna"); Assert.IsTrue((await ui.EvalAsync(ShowsAVideo)).GetBoolean(), "un video, all'andata e al ritorno"); } [TestMethod] public async Task WithoutTheBrowserStorageTheMosaicIsGrouped() { await using var ui = await UiBrowser.StartAsync(1440, 900); using var temp = new TempFolder("modo-senza-memoria"); await using var demo = await DemoHost.StartAsync(new DemoOptions(0, TestZones.Rome, temp.Path)); await ui.AddInitScriptAsync("Object.defineProperty(window, 'localStorage', { configurable: true, get() { throw new DOMException('bloccato', 'SecurityError'); } });"); await ui.OpenAsync(new Uri(demo.Address, "/libreria")); await ui.WaitAsync($"{Switch} !== null && document.querySelector('.mosaico .tess') !== null", "Libreria senza memoria del browser"); Assert.AreEqual("grouped", (await ui.EvalAsync(Mode)).GetString(), ui.Report()); // The choice still works for the page that is open. await ui.EvalAsync($"{Switch}.querySelector('[data-value=files]').click()"); await ui.WaitAsync("[...document.querySelectorAll('.mosaico .tess .segno span')].some((s) => /^\\d+\\/\\d+$/.test(s.textContent))", "«Tutte le foto» anche senza memoria"); await ui.OpenAsync(new Uri(demo.Address, "/libreria")); await ui.WaitAsync($"{Switch} !== null", "Libreria riaperta"); Assert.AreEqual("grouped", (await ui.EvalAsync(Mode)).GetString(), "nulla da ricordare: di nuovo «Raggruppa i post»"); } }