import { fr } from '@/i18n/fr' import type { Page, TestInfo } from '@playwright/test' import { expect, expectAccessible, test } from '../support/browser' import { dropLateReplays, routeToSandbox } from '../support/sandbox' import { editorActor, expectNoDialog, findingDialog, historyButton, libraryRoot, openEditor, openFinding, openTab, place, referenceDialog, referenceSettled, revealFinding, seedDraft, showCitations, standardShape, } from '../support/editor' // R2 UX finish, task 3 (R2F-5, R2F-6): the reference dialog, the reading tiles, the // references filtered by a finding, and the owner's journey through the browser history: // finding A, its references dialog, « Tout afficher », the filtered tab, a reference edited, Back, // Forward, « Revenir au constat », then every Back down to the first entry. The top // layer, the focus and the history rules are the browser's, so they are proved here. const dict = fr const isPhone = (info: TestInfo) => info.project.name === 'chromium-375' const panel = (page: Page) => page.getByTestId('editor-citations-panel') const chip = (page: Page) => libraryRoot(page).getByTestId('editor-references-filter') const headers = (page: Page) => libraryRoot(page).locator('button[aria-controls][id$="-header"]') const citedHeaders = (page: Page) => panel(page).locator('button[aria-controls][id$="-header"]') const undo = (page: Page) => historyButton(page, dict, 'undo') const dialogs = (page: Page) => page.locator('dialog[open]') /** « Constat A » cites A and B; « Source libre » is cited by nobody. */ function shape(document: Record) { standardShape(document) const reference = (id: string, citation: string) => ({ id, type: 'book', citation, url: null, doi: null, }) document.references = [ reference('ref-a', 'Référence A'), reference('ref-b', 'Référence B'), reference('ref-c', 'Source libre'), ] const zone = (document.finding_zones as { categories: unknown[] }[])[0] const family = zone.categories[0] as { children: { reference_ids: string[] }[] } family.children[0].reference_ids = ['ref-a', 'ref-b'] } async function open(page: Page) { const actor = await editorActor('fr') const seed = await seedDraft(actor, { shape }) await openEditor(page, dict, actor, seed.sheetId) return seed } /** The id of the history entry on top: it changes with every push, never with a replace. */ const entryId = (page: Page) => page.evaluate(() => history.state?.usr?.editorEntry?.id as string) /** Waits until the entry on top satisfies `view`: the adapter writes a moment after a move. */ async function entryHolds(page: Page, test: string) { const view = 'history.state?.usr?.editorEntry?.view' await page.waitForFunction( `(() => { const view = ${view}; return !!(${test}) })()`, ) } /** A Back restores the page offset within this many pixels. */ const SCROLL_SLACK = 4 const scrollY = (page: Page) => page.evaluate(() => window.scrollY) /** * Pads the page so it can scroll whatever the viewport, then moves it to `y` (a page * behind a modal still scrolls by script). Answers the offset reached. */ async function scrollTreeTo(page: Page, y: number): Promise { await page.evaluate((top) => { document.body.style.paddingBottom = '3000px' window.scrollTo(0, top) }, y) const reached = await scrollY(page) expect(reached).toBeGreaterThan(0) return reached } async function expectScrollNear(page: Page, y: number) { await expect .poll(async () => Math.abs((await scrollY(page)) - y)) .toBeLessThanOrEqual(SCROLL_SLACK) } /** Whether the references dialog is on top at the right edge of the viewport. */ const citationsOnTop = (page: Page) => page.evaluate(() => { const top = document.elementFromPoint(window.innerWidth - 40, 100) return !!top?.closest('dialog')?.querySelector('[data-place="citations"]') }) /** Finding A, its references dialog, then « Tout afficher »: the References tab filtered by Constat A. */ async function filteredTab(page: Page) { await openFinding(page, dict, 'Constat A') await entryHolds(page, 'view.finding !== null') await showCitations(page) await entryHolds(page, 'view.finding && view.finding.citations') await panel(page) .getByRole('button', { name: dict['editor.references.panel_show_all'] }) .click() await expectNoDialog(page) await entryHolds(page, "view.tab === 'references' && !!view.referenceFilter") await expect(chip(page)).toContainText('Constat A') } test.describe('the owner journey through the history', () => { test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) test.beforeEach(async ({ context }) => { await routeToSandbox(context) }) test.afterEach(async ({ context }) => { await dropLateReplays(context) }) test("the owner's eight steps, nothing in between: Back returns to finding A with its dialog open", async ({ page, }, info) => { test.setTimeout(240_000) await open(page) // 1. the Constats tab; 2. finding A opens. await openTab(page, dict, 'findings') await (await revealFinding(page, 'Constat A')).click() await expect(findingDialog(page)).toBeVisible() await entryHolds(page, 'view.finding !== null') // 3. « 2 références » (a tap at 375 px); 4. the references dialog lists the two. const trigger = page.locator('[data-citations-trigger]:visible') await expect(trigger).toContainText('2 références') await (isPhone(info) ? trigger.tap() : trigger.click()) await expect(place(page, 'citations')).toBeVisible() await expect(citedHeaders(page)).toHaveCount(2) await expectAccessible(page, 'the references dialog over finding A') await entryHolds(page, 'view.finding && view.finding.citations') // The tree behind the dialogs sits at an offset of its own (the page is padded so it // can scroll whatever the viewport): Back must return to it. const left = await scrollTreeTo(page, 400) // 5. « Tout afficher dans l'onglet Références »; 6. the tab filtered on A's two references. await panel(page) .getByRole('button', { name: dict['editor.references.panel_show_all'] }) .click() await expectNoDialog(page) await entryHolds( page, "view.tab === 'references' && !!view.referenceFilter", ) await expect(chip(page)).toContainText('Constat A') await expect(headers(page)).toHaveCount(2) await expect( libraryRoot(page).getByText('2 références', { exact: true }), ).toBeVisible() expect(Math.abs((await scrollY(page)) - left)).toBeGreaterThan(SCROLL_SLACK) // 7. browser Back; 8. finding A with its references dialog on top, as left. await page.goBack() await expect(dialogs(page)).toHaveCount(2) await expect(findingDialog(page)).toBeVisible() await expect(findingDialog(page)).toContainText('Constat A') expect(await citationsOnTop(page)).toBe(true) await expect(citedHeaders(page)).toHaveCount(2) await expectScrollNear(page, left) // Keyboard: from its ✕, Tab reaches « Tout afficher » and both tiles inside the // references dialog; Escape closes it only, « 2 références » takes the focus back; then // the finding dialog. The tree stays where it was left. const reached = new Set() for (let step = 0; step < 3; step++) { await page.keyboard.press('Tab') const inside = await page.evaluate( () => !!document.activeElement ?.closest('dialog') ?.querySelector('[data-place="citations"]'), ) expect(inside).toBe(true) reached.add( await page.evaluate(() => document.activeElement?.textContent ?? ''), ) } expect([...reached].join('|')).toContain( dict['editor.references.panel_show_all'], ) expect([...reached].join('|')).toContain('Référence A') expect([...reached].join('|')).toContain('Référence B') await page.keyboard.press('Escape') await expect(dialogs(page)).toHaveCount(1) await expect(trigger).toBeFocused() await page.keyboard.press('Escape') await expectNoDialog(page) await expectScrollNear(page, left) }) test('eight steps: finding, references, filter, edit, Back, Forward, Revenir au constat, every Back', async ({ page, }, info) => { test.setTimeout(240_000) await open(page) // 1 to 3: the finding dialog, its references, « Tout afficher »; 4: the filtered tab. await filteredTab(page) await expect(headers(page)).toHaveCount(2) await expect( libraryRoot(page).getByText('2 références', { exact: true }), ).toBeVisible() await expect(page.getByText('Source libre')).toHaveCount(0) const filteredId = await entryId(page) if (!isPhone(info)) { // 5: « Modifier » a tile: the dialog is a layer above the filtered tab, one entry. await headers(page).first().click() await libraryRoot(page) .getByRole('button', { name: dict['editor.references.edit'] }) .click() const form = referenceDialog(page) await expect(form).toBeVisible() await entryHolds(page, 'view.reference !== null') await form .getByLabel(dict['editor.reference.citation']) .fill('Référence A bis') await form .getByRole('button', { name: dict['editor.findings.save'] }) .click() await expectNoDialog(page) await referenceSettled(page) // One command, one undo; the entry on top is the filtered tab again, no orphan. await expect(undo(page)).toHaveAttribute('aria-disabled', 'false') expect(await entryId(page)).toBe(filteredId) await expect(chip(page)).toBeVisible() } // 6: Back lands on finding A with its references dialog on top; Forward, on the filter. await page.goBack() await expect(dialogs(page)).toHaveCount(2) expect(await citationsOnTop(page)).toBe(true) await expect(findingDialog(page)).toBeVisible() await page.goForward() await expectNoDialog(page) await expect(chip(page)).toBeVisible() await expect(headers(page)).toHaveCount(2) // 7: « Revenir au constat » pops to the dialogs (the entry below shows them). await page .getByRole('button', { name: dict['editor.references.filter_back'] }) .click() await expect(dialogs(page)).toHaveCount(2) expect(await citationsOnTop(page)).toBe(true) await entryHolds(page, 'view.finding && view.finding.citations') // 8: every Back down to the first entry: references, finding, tab, drawing, then out. await page.goBack() await expect(dialogs(page)).toHaveCount(1) await expect(findingDialog(page)).toBeVisible() await page.goBack() await expectNoDialog(page) await expect(place(page, 'findings')).toBeVisible() await page.goBack() await expect(page.getByRole('tab', { selected: true })).toHaveText( new RegExp(`^${dict['editor.tab.annotations']}`), ) const here = page.url() await page.goBack() await expect.poll(() => page.url()).not.toBe(here) }) test('a reload keeps the filtered tab and the chip, never a form; Back then returns to the dialog', async ({ page, }) => { test.setTimeout(150_000) await open(page) await filteredTab(page) await page.reload() await expect(place(page, 'references')).toBeVisible() await expect(chip(page)).toContainText('Constat A') await expect(headers(page)).toHaveCount(2) await page.goBack() await expect(dialogs(page)).toHaveCount(2) expect(await citationsOnTop(page)).toBe(true) }) test('the × removes the filter by replace; the search text is not in the history', async ({ page, }) => { test.setTimeout(150_000) await open(page) await filteredTab(page) const id = await entryId(page) await libraryRoot(page).getByRole('searchbox').fill('Référence') await chip(page) .getByRole('button', { name: dict['editor.references.filter_remove'].replace( '{name}', 'Constat A', ), }) .click() await entryHolds(page, 'view.referenceFilter === null') expect(await entryId(page)).toBe(id) expect( await page.evaluate(() => JSON.stringify(history.state?.usr)), ).not.toContain('Référence') // The search is a personal one: removing the filter keeps it (two titles start with it). await expect(headers(page)).toHaveCount(2) await expect(chip(page)).toHaveCount(0) }) }) test.describe('the reference dialog and the history', () => { test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) test.beforeEach(async ({ context }, info) => { test.skip(isPhone(info), 'editing needs 1 024 px') // floor-guard: allow C16 editing needs >= 1024 px; the reading journey above runs at 375 await routeToSandbox(context) }) test.afterEach(async ({ context }) => { await dropLateReplays(context) }) /** The References tab with the dialog of the first tile open on its pushed entry. */ async function dialogOpen(page: Page) { await open(page) await openTab(page, dict, 'references') await entryHolds(page, "view.tab === 'references'") const base = await entryId(page) await headers(page).first().click() await libraryRoot(page) .getByRole('button', { name: dict['editor.references.edit'] }) .click() await expect(referenceDialog(page)).toBeVisible() await entryHolds(page, 'view.reference !== null') return base } test('a double click on ✕ and on « Enregistrer » pops at most one entry', async ({ page, }) => { test.setTimeout(150_000) const base = await dialogOpen(page) const form = referenceDialog(page) await form .getByRole('button', { name: dict['common.close'], exact: true }) .dblclick() await expectNoDialog(page) await referenceSettled(page) expect(await entryId(page)).toBe(base) await libraryRoot(page) .getByRole('button', { name: dict['editor.references.edit'] }) .click() await entryHolds(page, 'view.reference !== null') await form .getByLabel(dict['editor.reference.citation']) .fill('Référence A bis') await form .getByRole('button', { name: dict['editor.findings.save'] }) .dblclick() await expectNoDialog(page) await referenceSettled(page) expect(await entryId(page)).toBe(base) // One command: one « Défaire » restores the citation. await expect(libraryRoot(page).getByText('Référence A bis')).toBeVisible() await undo(page).click() await expect(libraryRoot(page).getByText('Référence A bis')).toHaveCount(0) await expect(undo(page)).toHaveAttribute('aria-disabled', 'true') }) test('a held Escape closes a pristine dialog and pops at most one entry', async ({ page, }) => { test.setTimeout(150_000) const base = await dialogOpen(page) await page.keyboard.down('Escape') await page.keyboard.down('Escape') await page.keyboard.down('Escape') await page.keyboard.up('Escape') await expectNoDialog(page) await referenceSettled(page) expect(await entryId(page)).toBe(base) await expect(place(page, 'references')).toBeVisible() }) test('a real dialog.close() on a dirty dialog keeps it, with its confirmation', async ({ page, }) => { test.setTimeout(150_000) await dialogOpen(page) const form = referenceDialog(page) await form.getByLabel(dict['editor.reference.doi']).fill('10.1000/x') await page.evaluate(() => { const open = [...document.querySelectorAll('dialog[open]')] ;(open.at(-1) as HTMLDialogElement).close() }) await expect(form).toBeVisible() await expect( form.getByText(dict['editor.findings.discard_ask']), ).toBeVisible() await expect(form.getByLabel(dict['editor.reference.doi'])).toHaveValue( '10.1000/x', ) }) test('a dirty dialog keeps its draft on Back, one step and a jump across tabs', async ({ page, }) => { test.setTimeout(180_000) await open(page) await openTab(page, dict, 'findings') await entryHolds(page, "view.tab === 'findings'") await openTab(page, dict, 'references') await entryHolds(page, "view.tab === 'references'") await headers(page).first().click() await libraryRoot(page) .getByRole('button', { name: dict['editor.references.edit'] }) .click() await entryHolds(page, 'view.reference !== null') const form = referenceDialog(page) const doi = form.getByLabel(dict['editor.reference.doi']) await doi.fill('10.1000/x') const top = await entryId(page) await page.goBack() await expect( form.getByText(dict['editor.findings.discard_ask']), ).toBeVisible() await expect(doi).toHaveValue('10.1000/x') await form .getByRole('button', { name: dict['editor.findings.keep_typing'] }) .click() // The refused Back pushed the view again: a jump of two entries is refused the same way. await entryHolds(page, 'view.reference !== null') await page.evaluate(() => history.go(-2)) await expect( form.getByText(dict['editor.findings.discard_ask']), ).toBeVisible() await expect(doi).toHaveValue('10.1000/x') await expect(form).toBeVisible() expect(await entryId(page)).not.toBe(top) await entryHolds(page, 'view.reference !== null') }) })