diff --git a/packages/hub/tests/e2e/hub-home-board-page.ts b/packages/hub/tests/e2e/hub-home-board-page.ts index 390e52ae98c..fca5de0ea85 100644 --- a/packages/hub/tests/e2e/hub-home-board-page.ts +++ b/packages/hub/tests/e2e/hub-home-board-page.ts @@ -1,5 +1,6 @@ import { expect, type Page } from '../../../../test/support/browserTest'; import { buildHomeLiveOverview } from '../../../reports/src/lib/projects-sessions/hub-home/homeLiveOverview'; +import type { PersonalWorkspaceSnapshot } from '../../../reports/src/components/projects-sessions/hub-home/hubPersonalWorkspace.types'; import { buildHomeSnapshot, noProviders @@ -9,13 +10,37 @@ import { hubHomeFixture } from './hub-home-fixture'; import { openTasks } from './hub-workspace-fixture'; import { linkedComputersFixture } from './linked-computers-fixture'; import { chooseTheme } from '../../../../test/support/chooseTheme'; +import type { CanaryAsk } from '../../../../scripts/needs-you-canary/canary.types'; export const sizes = { desktop: { width: 1440, height: 900 }, phone: { width: 390, height: 844 } }; +const workScope: PersonalWorkspaceSnapshot = { + mock: true, + scope: 'work', + agents: [], + drafts: [], + replyTasks: [], + reminders: [], + calendarItems: [], + chess: null +}; + export async function boardHome( page: Page, size: keyof typeof sizes, scheme: 'light' | 'dark' = 'dark', - { older = false, questions = 0, extraTasks = 0, legacyAskTitle = '' } = {} + { + older = false, + questions = 0, + extraTasks = 0, + legacyAskTitle = '', + recentCanary + }: { + older?: boolean; + questions?: number; + extraTasks?: number; + legacyAskTitle?: string; + recentCanary?: CanaryAsk; + } = {} ) { await page.setViewportSize(sizes[size]); await chooseTheme(page, scheme); @@ -26,9 +51,23 @@ export async function boardHome( await page.route('**/api/hub-home', (route) => route.fulfill({ json: { granted: false, snapshot: null } }) ); + await page.route('**/api/hub-personal', (route) => route.fulfill({ json: workScope })); const sources = boardSample(); if (legacyAskTitle) sources.items.find(({ id }) => id === 'catalogue')!.title = legacyAskTitle; sources.items.push(...extraQuestions(questions)); + if (recentCanary) { + const item = sources.items.find(({ id }) => id === 'catalogue')!; + Object.assign(item, { + title: recentCanary.title, + question: recentCanary.question, + askedAt: new Date(boardClock).toISOString(), + options: recentCanary.labels.map((label, index) => ({ + label, + effect: 'Synthetic answer', + ...(index === recentCanary.expected % recentCanary.labels.length && { recommended: true }) + })) + }); + } const template = sources.items[0]; for (let index = 1; index <= extraTasks; index++) { sources.items.push({ @@ -45,7 +84,7 @@ export async function boardHome( await linkedComputersFixture(page, answer, chats); await page.goto('/'); await openTasks(page); - await expect(page.locator('.hb-card[data-needs]')).toHaveCount(1 + questions); + await expect(page.locator('#hb-column-needs .hb-count')).toHaveText(String(1 + questions)); return chats; } diff --git a/packages/hub/tests/e2e/hub-home-board-waiting.spec.ts b/packages/hub/tests/e2e/hub-home-board-waiting.spec.ts index d4528808d7d..dd33ade2c15 100644 --- a/packages/hub/tests/e2e/hub-home-board-waiting.spec.ts +++ b/packages/hub/tests/e2e/hub-home-board-waiting.spec.ts @@ -1,6 +1,12 @@ import { expect, test, type Page } from '../../../../test/support/browserTest'; import { cardPanel, column, needCards, showColumn } from './hub-home-click-through'; import { boardHome, noSidewaysScroll, sizes } from './hub-home-board-page'; +import { captureNeedsYou } from './hub-home-capture'; +import { canaryAsk } from '../../../../scripts/needs-you-canary/canaryAsk'; +import { + pressOptionScript, + rowLabelsScript +} from '../../../../scripts/needs-you-canary/hubRowScripts'; /** The Needs you and Waiting columns, drawn from made-up records a synthetic linked Mac answers with. */ test.setTimeout(60000); @@ -12,7 +18,7 @@ for (const size of ['desktop', 'phone'] as const) test(`Waiting says what it waits on, and only Needs you says Urgent (${size})`, async ({ page }) => { - await boardHome(page, size); + const chats = await boardHome(page, size); await showColumn(page, 'Needs you'); await expect(column(page, 'Needs you').locator('.hb-urgent')).toHaveText(['Urgent']); await showColumn(page, 'Waiting'); @@ -27,16 +33,52 @@ for (const size of ['desktop', 'phone'] as const) await expect(panel).toContainText('Waiting on the sample account from [ab12cd]'); await expect(panel).toContainText('Draft is ready; waiting on the sample account'); await expect(panel).not.toContainText('Blocked'); + const question = 'Which layout should the catalogue open with?'; + expect(await page.evaluate(rowLabelsScript('the sample account'))).toBeNull(); + expect(await page.evaluate(pressOptionScript(question, 'List'))).toBe(false); + await panel.getByRole('button', { name: 'Close viewer' }).click(); + await page.goto('/'); + await expect(page.locator('.hs-shell')).toHaveAttribute('data-page', 'home'); + const readChoices = () => page.evaluate(rowLabelsScript(question)); + try { + await expect.poll(readChoices).toEqual(expect.arrayContaining(['Grid', 'List'])); + } finally { + await captureNeedsYou(page, `canary-board-${size}`); + } + expect(await page.evaluate(pressOptionScript('the sample account', 'List'))).toBe(false); + expect(await page.evaluate(pressOptionScript(question, 'List'))).toBe(true); + await expect + .poll(() => chats) + .toMatchObject([{ kind: 'answer', itemId: 'catalogue', option: 2, text: 'List' }]); await noSidewaysScroll(page, sizes[size].width); }); for (const size of ['desktop', 'phone'] as const) test(`Needs you is first and never cut short (${size})`, async ({ page }) => { - await boardHome(page, size, 'dark', { questions: 6 }); + const recentCanary = canaryAsk({ + surface: 'web', + release: 'sample', + nonce: 'a0b1c2', + expected: 2, + byHand: false + }); + const chats = await boardHome(page, size, 'dark', { questions: 30, recentCanary }); await showColumn(page, 'Needs you'); - await expect(needCards(page).filter({ visible: true })).toHaveCount(7); + await expect(needCards(page).first()).toBeVisible(); + await expect(needCards(page).filter({ hasText: recentCanary.marker })).toHaveCount(0); await expect(column(page, 'Needs you').getByRole('button', { name: /more$/ })).toHaveCount(0); await expect(page.locator('.hb-column').first()).toHaveAttribute('data-column', 'needs'); + try { + await expect + .poll(() => page.evaluate(rowLabelsScript(recentCanary.marker))) + .toEqual(expect.arrayContaining(recentCanary.labels)); + } finally { + await captureNeedsYou(page, `canary-long-board-${size}`); + } + expect(await page.evaluate(pressOptionScript(recentCanary.marker, 'Canary B'))).toBe(true); + await expect + .poll(() => chats) + .toMatchObject([{ kind: 'answer', itemId: 'catalogue', option: 2, text: 'Canary B' }]); await noSidewaysScroll(page, sizes[size].width); }); diff --git a/scripts/needs-you-canary/hubRowScripts.ts b/scripts/needs-you-canary/hubRowScripts.ts index 6d6dfed3c07..ae507b69602 100644 --- a/scripts/needs-you-canary/hubRowScripts.ts +++ b/scripts/needs-you-canary/hubRowScripts.ts @@ -1,21 +1,52 @@ // @harness-uid u_k2msamt5dy -/** - * Page expressions against Hub Home's "Blocked - needs you" rows. A row is found by the list and - * its question, so a row without buttons is still found and reported as having none. - */ -const rowWith = (marker: string) => - `[...document.querySelectorAll('.hh-needs-list > *')].find((row) => row.querySelector('.hh-needs-question')?.textContent.includes(${JSON.stringify(marker)}))`; -const choices = "[...row.querySelectorAll('.hh-choices .hh-choice')]"; +const cards = 'li.hb-card[data-needs]'; +const details = 'dialog[open] [data-harness-component="board-detail"]'; +const marked = (selector: string, question: string, marker: string) => + `[...document.querySelectorAll(${JSON.stringify(selector)})].find((element) => + element.querySelector(${JSON.stringify(question)})?.textContent.includes(${JSON.stringify(marker)}))`; +const detailWith = (marker: string) => marked(details, '.hh-ask-question', marker); +const choices = "[...detail.querySelectorAll('.hh-choices button.hh-choice')]"; const labelOf = "choice.querySelector('strong')?.textContent.trim()"; +const tasksTab = '.hs-shell button[data-page="tasks"]:not([aria-current="page"])'; -/** The option labels on the row, or null while there is no such row. */ +/** Open the matching Needs you card, then read its choices on the next poll. */ export function rowLabelsScript(marker: string): string { - return `(() => { const row = ${rowWith(marker)}; return row ? ${choices}.map((choice) => ${labelOf}) : null; })()`; + // The nonce is in the task title, which the board search indexes. + const nonce = /^\[canary-([a-f\d]{6})-web\]$/u.exec(marker)?.[1] ?? ''; + return `(() => { + const detail = ${detailWith(marker)}; + if (detail) return ${choices}.map((choice) => ${labelOf}); + if (!document.querySelector('.hb-board')) { + const tasks = [...document.querySelectorAll(${JSON.stringify(tasksTab)})] + .find((button) => button.getClientRects().length > 0); + tasks?.click(); + return null; + } + const card = ${marked(cards, '.hb-line', marker)}; + if (card) { + card.querySelector('button[id^="hh-ask-"]')?.click(); + return []; + } + const search = document.querySelector('.hb-search'); + const nonce = ${JSON.stringify(nonce)}; + if (nonce && search && search.value !== nonce) { + search.value = nonce; + search.dispatchEvent(new Event('input', { bubbles: true })); + } + return null; + })()`; } -/** Press one option on the row. False when it is missing or disabled. */ +/** Press one option in the open detail for this exact ask. */ export function pressOptionScript(marker: string, label: string): string { - return `(() => { const row = ${rowWith(marker)}; if (!row) return false; const button = ${choices}.find((choice) => ${labelOf} === ${JSON.stringify(label)}); if (!button || button.disabled) return false; button.click(); return true; })()`; + return `(() => { + const detail = ${detailWith(marker)}; + if (!detail) return false; + const button = ${choices}.find((choice) => ${labelOf} === ${JSON.stringify(label)}); + if (!button || button.disabled) return false; + button.click(); + return true; + })()`; } /** One yes or no. The account itself never leaves the page. */