import { fr } from '@/i18n/fr' import type { BrowserContext, Page, TestInfo } from '@playwright/test' import { stepUpAdmin } from '../support/admin' import { expect, test } from '../support/browser' import { dropLateReplays, routeToSandbox } from '../support/sandbox' import { signIn } from '../support/shell' import { draftPath, draftTitle, editorActor, findingDialog, openFinding, openTab, citationsDialog, place, referenceDialog, seedDraft, showCitations, standardShape, startReference, } from '../support/editor' // R2 UX finish, task 8 (criteria 22, 23): a jump of several entries in the browser's // history (the history menu) that leaves the editor while a dialog holds changes asks // « Abandonner les modifications ? » before anything is lost. Chromium's Navigation API // lets the page cancel it; the jump is the history menu's own (CDP // Page.navigateToHistoryEntry: page.goBack() waits for a commit that never comes once // cancelled). The entries carry a session tag, never the account id. const dict = fr const isPhone = (info: TestInfo) => info.project.name === 'chromium-375' const overviewPath = (sheetId: string) => `/app/admin/sheets/${sheetId}` const confirmText = dict['editor.findings.discard_ask'] const leftNotice = dict['editor.leave.draft_dropped'] /** * The overview, then the draft through its link: one document, so the jump back to the * overview is a same-document traversal the page can cancel. */ async function open(page: Page) { const actor = await editorActor('fr') const seed = await seedDraft(actor, { shape: standardShape }) await signIn(page, actor.account, dict) await page.goto('/app/admin') await stepUpAdmin(page, dict, actor.secret) await page.goto(overviewPath(seed.sheetId)) await reopenDraft(page) return seed.sheetId } /** From the overview, the draft through its link (same document). */ async function reopenDraft(page: Page) { await page .getByRole('link', { name: dict['admin.overview.open_draft'] }) .click() await expect( page.getByRole('heading', { level: 1, name: draftTitle(dict) }), ).toBeVisible() } /** The history menu's jump to the last entry of the overview, several entries back. */ async function jumpToOverview( context: BrowserContext, page: Page, sheetId: string, ) { const cdp = await context.newCDPSession(page) const { entries, currentIndex } = await cdp.send('Page.getNavigationHistory') const target = entries .slice(0, currentIndex) .filter((e) => new URL(e.url).pathname === overviewPath(sheetId)) .pop() if (!target) throw new Error('no overview entry in the history') expect(currentIndex - entries.indexOf(target)).toBeGreaterThan(1) await cdp.send('Page.navigateToHistoryEntry', { entryId: target.id }) await cdp.detach() } const leftFlags = (page: Page) => page.evaluate( () => Object.keys(sessionStorage).filter((k) => k.startsWith('faunosteo:editor:left-draft:'), ).length, ) test.describe('leaving the editor by a history jump', () => { 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 guard is the same at every width await routeToSandbox(context) }) test.afterEach(async ({ context }) => { await dropLateReplays(context) }) test('a dirty finding dialog asks first, then keeps it or goes', async ({ page, context, }) => { test.setTimeout(150_000) const sheetId = await open(page) await openFinding(page, dict, 'Dysfonction A') const dialog = findingDialog(page) await dialog .getByRole('button', { name: dict['editor.findings.edit'], exact: true }) .click() const name = dialog.getByLabel(dict['editor.general.name_fr']) await name.fill('Dysfonction brouillon') const entry = await page.evaluate(() => history.state?.usr?.editorEntry) expect(entry?.tag).toMatch(/^[0-9a-f]{32}$/) expect(Object.keys(entry ?? {})).not.toContain('userId') await jumpToOverview(context, page, sheetId) await expect(dialog.getByText(confirmText)).toBeVisible() expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) await dialog .getByRole('button', { name: dict['editor.findings.keep_typing'] }) .click() await expect(name).toHaveValue('Dysfonction brouillon') expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) await jumpToOverview(context, page, sheetId) await expect(dialog.getByText(confirmText)).toBeVisible() await dialog .getByRole('button', { name: dict['editor.findings.discard'] }) .click() await expect(page).toHaveURL( (url) => url.pathname === overviewPath(sheetId), ) expect(await leftFlags(page)).toBe(0) await reopenDraft(page) await expect(page.getByText(leftNotice)).toHaveCount(0) }) test('a dirty reference dialog asks the same before the jump goes', async ({ page, context, }) => { test.setTimeout(150_000) const sheetId = await open(page) await openTab(page, dict, 'references') await startReference(page, dict) const form = referenceDialog(page) await form.locator('#reference-form-type').selectOption('manual') await form.locator('#reference-form-citation').fill('Brouillon') await jumpToOverview(context, page, sheetId) await expect(form.getByText(confirmText)).toBeVisible() expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) await form .getByRole('button', { name: dict['editor.findings.keep_typing'] }) .click() await expect(form.locator('#reference-form-citation')).toHaveValue( 'Brouillon', ) await jumpToOverview(context, page, sheetId) await form .getByRole('button', { name: dict['editor.findings.discard'] }) .click() await expect(page).toHaveURL( (url) => url.pathname === overviewPath(sheetId), ) expect(await leftFlags(page)).toBe(0) await reopenDraft(page) await expect(page.getByText(leftNotice)).toHaveCount(0) }) test('« Tout afficher » from the references dialog over a dirty finding: it closes, the form asks in view', async ({ page, }) => { test.setTimeout(150_000) const sheetId = await open(page) await openFinding(page, dict, 'Constat A') const finding = findingDialog(page) await finding .getByRole('button', { name: dict['editor.findings.edit'], exact: true }) .click() const name = finding.getByLabel(dict['editor.general.name_fr']) await name.fill('Constat brouillon') await showCitations(page) await page .getByTestId('editor-citations-panel') .getByRole('button', { name: dict['editor.references.panel_show_all'] }) .click() // Refused by the dirty form under it: the clean references dialog closes first, so // the question is the one in view and holds the focus; nothing moved. await expect(page.locator('dialog[open]')).toHaveCount(1) await expect(finding.getByText(confirmText)).toBeVisible() const keep = finding.getByRole('button', { name: dict['editor.findings.keep_typing'], }) await expect(keep).toBeFocused() await expect(place(page, 'references')).toBeHidden() await keep.click() await expect(name).toHaveValue('Constat brouillon') expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) }) test('the references dialog over a dirty finding form: Back closes it alone, a jump asks in view', async ({ page, context, }) => { // Final review rev 4, item 1: the references dialog is no form, so a Back that closes // it asks nothing; a jump that leaves closes it first, so the finding's question is seen. const sheetId = await open(page) await openFinding(page, dict, 'Constat A') const finding = findingDialog(page) await finding .getByRole('button', { name: dict['editor.findings.edit'], exact: true }) .click() const name = finding.getByLabel(dict['editor.general.name_fr']) await name.fill('Constat brouillon') await showCitations(page) await expect(page.locator('dialog[open]')).toHaveCount(2) await page.goBack() await expect(page.locator('dialog[open]')).toHaveCount(1) await expect(page.getByText(confirmText)).toHaveCount(0) await expect(name).toHaveValue('Constat brouillon') await showCitations(page) await expect(page.locator('dialog[open]')).toHaveCount(2) await jumpToOverview(context, page, sheetId) await expect(page.locator('dialog[open]')).toHaveCount(1) await expect(finding.getByText(confirmText)).toBeVisible() const keep = finding.getByRole('button', { name: dict['editor.findings.keep_typing'], }) await expect(keep).toBeFocused() await keep.click() await expect(name).toHaveValue('Constat brouillon') expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) }) test('a dirty references draft asks before the jump goes', async ({ page, context, }) => { test.setTimeout(150_000) const sheetId = await open(page) await openFinding(page, dict, 'Constat A') await showCitations(page) const dialog = citationsDialog(page) await dialog .getByRole('button', { name: dict['editor.citations.edit'], exact: true }) .click() await dialog .getByRole('button', { name: new RegExp(`^${dict['editor.action.remove']} Référence A`), }) .click() const rows = dialog.locator('[data-citation-row]') await expect(rows).toHaveCount(0) await jumpToOverview(context, page, sheetId) await expect(dialog.getByText(confirmText)).toBeVisible() expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) const keep = dialog.getByRole('button', { name: dict['editor.findings.keep_typing'], }) await expect(keep).toBeFocused() await keep.click() await expect(rows).toHaveCount(0) expect(new URL(page.url()).pathname).toBe(draftPath(sheetId)) await jumpToOverview(context, page, sheetId) await expect(dialog.getByText(confirmText)).toBeVisible() await dialog .getByRole('button', { name: dict['editor.findings.discard'] }) .click() await expect(page).toHaveURL( (url) => url.pathname === overviewPath(sheetId), ) expect(await leftFlags(page)).toBe(0) await reopenDraft(page) await expect(page.getByText(leftNotice)).toHaveCount(0) }) })