import { expect, test, type Page, type Route } from "@playwright/test"; // The channel finder (common/lib/channelFind.ts) in the filter panel's // Channels row: a magnifier, offered only on a site with more than // CHANNEL_FIND_MIN_CHANNELS (12) channels, that opens into a small field and // narrows the group cards to the channels whose name, slug or group name holds // the text. Self-contained fixtures, like channel-group-chips.spec.ts. type Chan = { name: string; slug: string; groupId: string }; const CHANNELS: Chan[] = [ { name: "Alpha Works", slug: "alpha-works", groupId: "g1" }, { name: "Beta Desk", slug: "beta-desk", groupId: "g1" }, { name: "Gamma Talk", slug: "gamma_talk_live", groupId: "g1" }, { name: "Delta Notes", slug: "delta-notes", groupId: "g1" }, { name: "Epsilon", slug: "epsilon", groupId: "g2" }, { name: "Zeta Review", slug: "zeta-review", groupId: "g2" }, { name: "Eta Corner", slug: "eta-corner", groupId: "g2" }, { name: "Théta Café", slug: "theta-cafe", groupId: "g2" }, { name: "Iota", slug: "iota", groupId: "g3" }, { name: "Kappa", slug: "kappa", groupId: "g3" }, { name: "Lambda", slug: "lambda", groupId: "g3" }, { name: "Mu Stream", slug: "mu-stream", groupId: "g3" }, { name: "Nu Show", slug: "nu-show", groupId: "g4" }, { name: "Xi Clips", slug: "xi-clips", groupId: "g4" }, ]; const GROUPS = [ { id: "g1", name: "Group One", selectedByDefault: true, order: 1 }, { id: "g2", name: "Reviewers", selectedByDefault: true, order: 2 }, { id: "g3", name: "Group Three", selectedByDefault: true, order: 3 }, { id: "g4", name: "Group Four", selectedByDefault: true, order: 4 }, ]; function summary(c: Chan) { return { slug: `${c.slug}/vid-${c.slug}`, id: `vid-${c.slug}`, channelSlug: c.slug, title: `Video from ${c.name}`, uploadDate: "20250101", date: "2025-01-01", duration: "5:00", channel: c.name, isLivestream: false, ageRestricted: false, isDeleted: false, isUnlisted: false, platform: "youtube" as const, webpageUrl: `https://example.com/vid-${c.slug}`, }; } async function fulfillJson(route: Route, body: unknown) { await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(body), }); } async function installRoutes(page: Page, channels: Chan[] = CHANNELS) { const list = channels.map(summary); await page.route("**/summaries/manifest.json", async (route) => { await fulfillJson(route, { version: 3, totalCount: list.length, pageSize: 1000, pageCount: 1, generatedAt: new Date().toISOString(), channels: channels.map((c) => ({ name: c.name, slug: c.slug, count: 1, groupId: c.groupId, })), groups: GROUPS, defaultGroupId: "g1", }); }); await page.route("**/summaries/page-*.json", async (route) => { await fulfillJson(route, list); }); await page.route(/\/transcripts\/[^/]+\/manifest\.json$/, async (route) => { await fulfillJson(route, { version: 1, channelSlug: "unused", pageCount: 0, maxPageBytes: 8388608, generatedAt: new Date().toISOString(), slugToPage: {}, }); }); await page.route(/\/transcripts\/[^/]+\/page-\d+\.json$/, async (route) => { await fulfillJson(route, []); }); await page.route("**/subs/manifest.json", async (route) => { await fulfillJson(route, { version: 4, channels: [], totalCount: 0, liveChatTotalCount: 0, generatedAt: new Date().toISOString(), }); }); // No curated tags: their disclosures are
too. await page.route("**/tags.json", async (route) => { await route.fulfill({ status: 404, contentType: "application/json", body: "{}" }); }); } async function waitForHydration(page: Page) { await page.getByTestId("query-builder").waitFor(); } function groupCard(page: Page, name: string) { return groupCards(page).filter({ hasText: name }); } // Every group card, and only group cards: "Advanced options" (and a tag // disclosure) is a nested
too, so the count goes by the group's // select-all box. function groupCards(page: Page) { return page .locator("details details") .filter({ has: page.getByRole("checkbox", { name: /^Select all in / }) }); } function memberCheckbox(page: Page, group: string, channel: string) { return groupCard(page, group).getByRole("checkbox", { name: channel, exact: true, }); } const opener = (page: Page) => page.getByTestId("channel-find-open"); const field = (page: Page) => page.getByTestId("channel-find"); test.describe("channel finder", () => { test("is not offered on a site with 12 channels or fewer", async ({ page }) => { await installRoutes(page, CHANNELS.slice(0, 12)); await page.goto("/"); await waitForHydration(page); await expect(page.getByText("12 of 12 channels")).toBeVisible(); await expect(opener(page)).toHaveCount(0); }); test("is offered from 13 channels", async ({ page }) => { await installRoutes(page, CHANNELS.slice(0, 13)); await page.goto("/"); await waitForHydration(page); await expect(page.getByText("13 of 13 channels")).toBeVisible(); await expect(opener(page)).toBeVisible(); }); test("an empty open field never moves the row under a click: All and Reset channels land", async ({ page, }) => { await installRoutes(page); await page.goto("/"); await waitForHydration(page); const all = page.getByRole("checkbox", { name: "All", exact: true }); await expect(all).toBeChecked(); // Open, leave empty, click All with a real press and release. await opener(page).click(); await expect(field(page)).toBeFocused(); const at = await all.boundingBox(); await page.mouse.move(at!.x + at!.width / 2, at!.y + at!.height / 2); await page.mouse.down(); // While the press is held the field is still there, so All has not moved. await expect(field(page)).toBeVisible(); expect((await all.boundingBox())!.x).toBe(at!.x); await page.mouse.up(); await expect(page.getByText("0 of 14 channels")).toBeVisible(); await expect(all).not.toBeChecked(); // …and only then does the empty field fold back to the magnifier. await expect(field(page)).toHaveCount(0); // Reset channels from an open empty field: the click lands too. await opener(page).click(); await expect(field(page)).toBeFocused(); await page.getByRole("button", { name: "Reset channels" }).click(); await expect(page.getByText("14 of 14 channels")).toBeVisible(); await expect(field(page)).toHaveCount(0); }); test("while filtering, a group's None, box and count act on the channels listed", async ({ page, }) => { await installRoutes(page); await page.goto("/"); await waitForHydration(page); await opener(page).click(); // One match in a four-channel group: the count is matches-selected/matches. await field(page).fill("talk"); const one = groupCard(page, "Group One"); await expect(one.locator("summary")).toContainText("1/1"); await one.getByRole("button", { name: "None" }).click(); await expect(page.getByText("13 of 14 channels")).toBeVisible(); await expect(one.locator("summary")).toContainText("0/1"); // The group's box re-selects just the match. await page.getByRole("checkbox", { name: "Select all in Group One" }).click(); await expect(page.getByText("14 of 14 channels")).toBeVisible(); await one.getByRole("button", { name: "None" }).click(); // A matched group NAME lists the whole group, so None is the whole group. await field(page).fill("reviewer"); await groupCard(page, "Reviewers").getByRole("button", { name: "None" }).click(); await expect(page.getByText("9 of 14 channels")).toBeVisible(); // No query: the whole group again. await field(page).press("Escape"); await expect(groupCard(page, "Group One").locator("summary")).toContainText("3/4"); }); test("the live region is there before the first query, so the first count is announced", async ({ page, }) => { await installRoutes(page); await page.goto("/"); await waitForHydration(page); await opener(page).click(); const live = page.getByTestId("channel-find-live"); await expect(live).toHaveCount(1); await expect(live).toHaveText(""); await field(page).fill("kappa"); await expect(live).toHaveText("1 channel matches"); await field(page).fill("zzz"); await expect(live).toHaveText("No channel matches"); }); test("narrows the groups by channel name, slug or group name, and Esc gives the panel back", async ({ page, }, testInfo) => { await installRoutes(page); await page.goto("/"); await waitForHydration(page); await expect(page.getByText("14 of 14 channels")).toBeVisible(); // A multi-group site starts with every group folded. await expect(groupCards(page).and(page.locator("details[open]"))).toHaveCount(0); const where = await opener(page).boundingBox(); await page.getByTestId("filters-panel").screenshot({ path: testInfo.outputPath("channel-find-closed-desktop.png"), }); await opener(page).click(); await expect(field(page)).toBeFocused(); // The field opened where the magnifier was. const at = await field(page).boundingBox(); expect(Math.abs(at!.x - where!.x)).toBeLessThan(2); expect(Math.abs(at!.height - where!.height)).toBeLessThan(2); // A channel name: its group alone, open, listing that channel alone. await field(page).fill("gamma"); await expect(groupCards(page)).toHaveCount(1); await expect(groupCard(page, "Group One")).toHaveAttribute("open", ""); await expect(memberCheckbox(page, "Group One", "Gamma Talk")).toBeVisible(); await expect(memberCheckbox(page, "Group One", "Alpha Works")).toHaveCount(0); // While filtering the count is the matches': selected of listed. await expect(groupCard(page, "Group One").locator("summary")).toContainText("1/1"); // A slug: separators read as spaces. await field(page).fill("talk live"); await expect(memberCheckbox(page, "Group One", "Gamma Talk")).toBeVisible(); // Accents fold. await field(page).fill("theta cafe"); await expect(memberCheckbox(page, "Reviewers", "Théta Café")).toBeVisible(); // A group name: the whole group. await field(page).fill("reviewer"); await expect(groupCards(page)).toHaveCount(1); await expect(groupCard(page, "Reviewers").getByRole("checkbox")).toHaveCount(5); await page.getByTestId("filters-panel").screenshot({ path: testInfo.outputPath("channel-find-open-desktop.png"), }); // Nothing: said, not a blank panel. await field(page).fill("no such thing"); await expect(page.getByTestId("channel-find-empty")).toContainText("no such thing"); await expect(groupCards(page)).toHaveCount(0); // Esc: cleared, closed, focus back on the magnifier, the folded panel back. await field(page).press("Escape"); await expect(field(page)).toHaveCount(0); await expect(opener(page)).toBeFocused(); await expect(groupCards(page)).toHaveCount(4); await expect(groupCards(page).and(page.locator("details[open]"))).toHaveCount(0); }); test("keyboard: Enter opens it, a channel toggles from the found list", async ({ page, }) => { await installRoutes(page); await page.goto("/"); await waitForHydration(page); await opener(page).focus(); await page.keyboard.press("Enter"); await expect(field(page)).toBeFocused(); await page.keyboard.type("kappa"); const box = memberCheckbox(page, "Group Three", "Kappa"); await expect(box).toBeVisible(); await box.click(); await expect(page.getByText("13 of 14 channels")).toBeVisible(); // Clearing the query keeps the selection and the reader's fold state. await field(page).fill(""); await field(page).press("Escape"); await expect(page.getByText("13 of 14 channels")).toBeVisible(); await expect(groupCards(page).and(page.locator("details[open]"))).toHaveCount(0); }); test.describe("on a phone", () => { test.use({ viewport: { width: 390, height: 844 } }); test("fits the sheet and does not scroll the page sideways", async ({ page, }, testInfo) => { await installRoutes(page); await page.goto("/"); await waitForHydration(page); await page.getByTestId("filters-trigger").click(); await expect(opener(page)).toBeVisible(); await page.screenshot({ path: testInfo.outputPath("channel-find-closed-phone.png"), }); await opener(page).click(); await field(page).fill("show"); await expect(memberCheckbox(page, "Group Four", "Nu Show")).toBeVisible(); const at = await field(page).boundingBox(); expect(at!.x + at!.width).toBeLessThanOrEqual(390); const sideways = await page.evaluate( () => document.documentElement.scrollWidth > window.innerWidth, ); expect(sideways).toBe(false); await page.screenshot({ path: testInfo.outputPath("channel-find-open-phone.png"), }); // Esc is the finder's first: the sheet stays, the magnifier comes back. await field(page).press("Escape"); await expect(field(page)).toHaveCount(0); await expect(opener(page)).toBeVisible(); await expect(opener(page)).toBeFocused(); // The second Esc is the sheet's. await page.keyboard.press("Escape"); await expect(page.getByTestId("filters-panel")).toHaveCount(0); await expect(page.getByTestId("filters-trigger")).toBeVisible(); }); }); });