Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions EssentialCSharp.Web.E2ETests/DeployedReadingJourneyTests.cs
Original file line number Diff line number Diff line change
@@ -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<string>? 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);
}
}
119 changes: 119 additions & 0 deletions EssentialCSharp.Web.E2ETests/ReadingJourney.cs
Original file line number Diff line number Diff line change
@@ -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<ReadingJourneyResult> 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<string>("() => JSON.stringify(window.TOC_DATA)");
TocEntry[] chapters = JsonSerializer.Deserialize<TocEntry[]>(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<string>("() => 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<string>("() => 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<TocEntry> Flatten(IEnumerable<TocEntry> 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);
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageReference Include="TUnit" />
<PackageReference Include="TUnit.Aspire" />
<PackageReference Include="TUnit.Playwright" />
<Compile Include="..\..\EssentialCSharp.Web.E2ETests\ReadingJourney.cs" Link="Journeys\ReadingJourney.cs" />
<PackageReference Include="Aspire.Hosting.Testing" />
<ProjectReference Include="..\EssentialCSharp.Web.AppHost\EssentialCSharp.Web.AppHost.csproj" />
<ProjectReference Include="..\..\EssentialCSharp.Web.Hosting\EssentialCSharp.Web.Hosting.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JourneyFixture>(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");
}
}
Loading