From fb89a4c26daabf03fab4cbb87fac58124aae7d76 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Sat, 21 Nov 2026 11:13:43 -0800 Subject: [PATCH] test: cover reading and navigation browser journeys --- .../DeployedReadingJourneyTests.cs | 29 +++++ .../ReadingJourney.cs | 119 ++++++++++++++++++ README.md | 18 +++ .../EssentialCSharp.Web.AppHost.Tests.csproj | 1 + .../Journeys/ReadingJourneyTests.cs | 34 +++++ 5 files changed, 201 insertions(+) create mode 100644 EssentialCSharp.Web.E2ETests/DeployedReadingJourneyTests.cs create mode 100644 EssentialCSharp.Web.E2ETests/ReadingJourney.cs create mode 100644 dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ReadingJourneyTests.cs diff --git a/EssentialCSharp.Web.E2ETests/DeployedReadingJourneyTests.cs b/EssentialCSharp.Web.E2ETests/DeployedReadingJourneyTests.cs new file mode 100644 index 00000000..26f4ad65 --- /dev/null +++ b/EssentialCSharp.Web.E2ETests/DeployedReadingJourneyTests.cs @@ -0,0 +1,29 @@ +using Microsoft.Playwright; +using TUnit.Playwright; + +namespace EssentialCSharp.Web.E2ETests; + +[Category("Deployed")] +[RequiresDeployedSite] +[CaptureOnFailure] +public class DeployedReadingJourneyTests : PageTest +{ + public override IReadOnlyList? TraceContextOrigins => + [DeployedSite.RequiredBaseUrl.GetLeftPart(UriPartial.Authority)]; + + [Test] + public async Task Reader_CanFilterContentsTurnPagesAndFollowSectionDeepLinks() + { + await Context.AddCookiesAsync( + [ + new Cookie + { + Name = "essential-csharp-consent", + Value = "%7B%22analytics_storage%22%3A%22granted%22%2C%22_version%22%3A%223%22%7D", + Url = DeployedSite.RequiredBaseUrl.GetLeftPart(UriPartial.Authority), + }, + ]); + + await ReadingJourney.RunAsync(Page, DeployedSite.RequiredBaseUrl); + } +} diff --git a/EssentialCSharp.Web.E2ETests/ReadingJourney.cs b/EssentialCSharp.Web.E2ETests/ReadingJourney.cs new file mode 100644 index 00000000..3cceb7ec --- /dev/null +++ b/EssentialCSharp.Web.E2ETests/ReadingJourney.cs @@ -0,0 +1,119 @@ +using System.Text.Json; +using Microsoft.Playwright; +using static Microsoft.Playwright.Assertions; + +namespace EssentialCSharp.Web.E2ETests; + +internal static class ReadingJourney +{ + public static async Task RunAsync(IPage page, Uri baseUrl) + { + await page.AddInitScriptAsync("if (localStorage.getItem('sidebarShown') === null) localStorage.setItem('sidebarShown', 'false');"); + await page.GotoAsync(new Uri(baseUrl, "home").ToString()); + await page.Locator("#sidebar-toggle-button").ClickAsync(); + await Expect(page.Locator("#sidebar")).ToBeVisibleAsync(); + + string tocJson = await page.EvaluateAsync("() => JSON.stringify(window.TOC_DATA)"); + TocEntry[] chapters = JsonSerializer.Deserialize(tocJson, JsonSerializerOptions.Web) + ?? throw new InvalidOperationException("The page did not provide table-of-contents data."); + await Assert.That(chapters.Length).IsGreaterThan(1); + TocEntry chapter = chapters[0]; + TocEntry section = chapter.Items[0]; + ILocator summaries = page.Locator("#toc > ul > li > details > summary"); + await Expect(summaries).ToHaveCountAsync(chapters.Length); + await page.GetByRole(AriaRole.Button, new() { Name = "Filter contents", Exact = true }).ClickAsync(); + ILocator filter = page.Locator("#toc-filter-input"); + await Expect(filter).ToBeVisibleAsync(); + await filter.FillAsync("no matching chapter 9e76d102"); + await Expect(summaries).ToHaveCountAsync(0); + await filter.FillAsync(chapter.Title); + await Expect(summaries).ToHaveCountAsync(1); + await Expect(summaries).ToHaveTextAsync(chapter.Title); + + Uri chapterUrl = Resolve(baseUrl, chapter.Href); + await TocLink(page, chapter.Href).ClickAsync(); + await Expect(page).ToHaveURLAsync(chapterUrl.ToString()); + await ExpectChapterAsync(page, chapter); + + string nextHref = await page.EvaluateAsync("() => window.NEXT_PAGE"); + TocEntry next = Flatten(chapters).Single(entry => entry.Href == nextHref); + Uri nextUrl = Resolve(baseUrl, nextHref); + // Arrow buttons intentionally omit fragments; TOC links and keyboard navigation retain them. + await Expect(page.Locator("#next-btn")).ToHaveAttributeAsync("href", next.Key); + await page.Locator("#next-btn").ClickAsync(); + await Expect(page).ToHaveURLAsync(WithoutFragment(nextUrl)); + await ExpectSectionAsync(page, next, inViewport: false); + + string previousHref = await page.EvaluateAsync("() => window.PREVIOUS_PAGE"); + await Assert.That(previousHref).IsEqualTo(chapter.Href); + await Expect(page.Locator("#prev-btn")).ToHaveAttributeAsync("href", chapter.Key); + await page.Locator("#prev-btn").ClickAsync(); + await Expect(page).ToHaveURLAsync(WithoutFragment(chapterUrl)); + await ExpectChapterAsync(page, chapter); + + if (section.Items.Length > 0) + { + await TocLink(page, section.Href).Locator("xpath=ancestor::details[1]/summary").ClickAsync(); + } + await Expect(TocLink(page, section.Href)).ToBeVisibleAsync(); + await TocLink(page, section.Href).ClickAsync(); + Uri sectionUrl = Resolve(baseUrl, section.Href); + await Expect(page).ToHaveURLAsync(sectionUrl.ToString()); + await ExpectSectionAsync(page, section, inViewport: true); + await Expect(TocLink(page, section.Href)).ToHaveClassAsync(new System.Text.RegularExpressions.Regex("current-section")); + + await page.GotoAsync(new Uri(baseUrl, "home").ToString()); + IResponse? response = await page.GotoAsync(sectionUrl.ToString()); + await Assert.That(response?.Status).IsEqualTo(200); + await Expect(page).ToHaveURLAsync(sectionUrl.ToString()); + await ExpectSectionAsync(page, section, inViewport: true); + + return new(chapter.Href, section.Href, nextHref, previousHref); + } + + private static ILocator TocLink(IPage page, string href) => + page.Locator($"#toc a[href=\"{EscapeAttribute(href)}\"]"); + + private static async Task ExpectChapterAsync(IPage page, TocEntry chapter) + { + ILocator heading = page.Locator("#site-layout .container .chapter-title"); + await Expect(heading).ToBeVisibleAsync(); + await Expect(heading).ToHaveTextAsync(chapter.Title[(chapter.Title.IndexOf(':') + 1)..].Trim()); + } + + private static async Task ExpectSectionAsync(IPage page, TocEntry section, bool inViewport) + { + string anchor = Uri.UnescapeDataString(new Uri("https://reading.test/" + section.Href).Fragment.TrimStart('#')); + await Assert.That(anchor).IsNotNullOrWhiteSpace(); + // Generated pages can repeat an id on a nested span; target the actual section heading container. + ILocator target = page.Locator($"#site-layout .container .section-heading[id=\"{EscapeAttribute(anchor)}\"]"); + await Expect(target).ToHaveCountAsync(1); + await Expect(target).ToBeVisibleAsync(); + await Expect(target).ToContainTextAsync(section.Title); + if (inViewport) + { + await Expect(target).ToBeInViewportAsync(); + } + } + + private static Uri Resolve(Uri baseUrl, string href) + { + Uri url = new(baseUrl, href); + if (url.GetLeftPart(UriPartial.Authority) != baseUrl.GetLeftPart(UriPartial.Authority)) + { + throw new InvalidOperationException($"Reading link points outside the tested site: {href}"); + } + return url; + } + + private static string WithoutFragment(Uri url) => new UriBuilder(url) { Fragment = "" }.Uri.ToString(); + + private static string EscapeAttribute(string value) => value.Replace("\\", "\\\\").Replace("\"", "\\\""); + + private static IEnumerable Flatten(IEnumerable entries) => + entries.SelectMany(entry => new[] { entry }.Concat(Flatten(entry.Items))); + + internal sealed record TocEntry(string Key, string Href, string Title, TocEntry[] Items); +} + +internal sealed record ReadingJourneyResult(string ChapterHref, string SectionHref, string NextHref, string PreviousHref); diff --git a/README.md b/README.md index 76c66a6c..63cb9b87 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,24 @@ mode contains no migration tools, browser helpers, local AI, or Development envi To run only graph checks, use `dotnet test --project dev\EssentialCSharp.Web.AppHost.Tests --treenode-filter "/*/*/(AppHostGraphTests|WebHostingExtensionsTests)/*"`. +Browser journeys share the fresh-database fixture. The reading journey opens and filters the TOC, +selects a chapter, follows next/previous links, and verifies a rendered section heading both from +its TOC link and a fresh anchored URL load. It uses the repository's `introducing-c` / `hello-world` +placeholder mappings locally; the deployed version discovers links from the real TOC, without +production-only chapter keys or login credentials. Generated aliases can serve the same document: +TOC links retain anchors, while arrow buttons deliberately omit them. The shared journey makes +five content requests (plus two home loads), keeping a deployed suite run below the anonymous +10/minute and 20/hour content limits when run once. Concurrent or repeated runs share the IP budget. + +Run only the local reading journey after installing Playwright Chromium: + +```powershell +dotnet test --project dev\EssentialCSharp.Web.AppHost.Tests --treenode-filter "/*/*/ReadingJourneyTests/*" +``` + +`DeployedReadingJourneyTests` runs with the existing `Category=Deployed` promotion gate and +six-hour scheduled checks when `E2E_BASE_URL` is set. No extra workflow or secrets are required. + The AppHost references the Web project so building the AppHost builds Web before the EF migration resource runs its `--no-build` database update; Web starts only after that update completes successfully. diff --git a/dev/EssentialCSharp.Web.AppHost.Tests/EssentialCSharp.Web.AppHost.Tests.csproj b/dev/EssentialCSharp.Web.AppHost.Tests/EssentialCSharp.Web.AppHost.Tests.csproj index 5448a490..203bd570 100644 --- a/dev/EssentialCSharp.Web.AppHost.Tests/EssentialCSharp.Web.AppHost.Tests.csproj +++ b/dev/EssentialCSharp.Web.AppHost.Tests/EssentialCSharp.Web.AppHost.Tests.csproj @@ -8,6 +8,7 @@ + diff --git a/dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ReadingJourneyTests.cs b/dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ReadingJourneyTests.cs new file mode 100644 index 00000000..8ae29e5d --- /dev/null +++ b/dev/EssentialCSharp.Web.AppHost.Tests/Journeys/ReadingJourneyTests.cs @@ -0,0 +1,34 @@ +using EssentialCSharp.Web.E2ETests; +using Microsoft.Playwright; +using TUnit.Playwright; + +namespace EssentialCSharp.Web.AppHost.Tests.Journeys; + +[Category("Docker")] +[Category("Journey")] +[RequiresLinuxContainers] +[CaptureOnFailure] +[ClassDataSource(Shared = SharedType.PerTestSession)] +public class ReadingJourneyTests(JourneyFixture fixture) : PageTest +{ + [Test] + public async Task Reader_CanFilterContentsTurnPagesAndFollowSectionDeepLinks() + { + await Context.AddCookiesAsync( + [ + new Cookie + { + Name = "essential-csharp-consent", + Value = "%7B%22analytics_storage%22%3A%22granted%22%2C%22_version%22%3A%223%22%7D", + Url = fixture.WebUrl.GetLeftPart(UriPartial.Authority), + }, + ]); + + ReadingJourneyResult journey = await ReadingJourney.RunAsync(Page, fixture.WebUrl); + + await Assert.That(journey.ChapterHref).IsEqualTo("introducing-c#overview"); + await Assert.That(journey.SectionHref).IsEqualTo("hello-world#hello-world"); + await Assert.That(journey.NextHref).IsEqualTo("c-syntax-fundamentals#c-syntax-fundamentals"); + await Assert.That(journey.PreviousHref).IsEqualTo("introducing-c#overview"); + } +}