Files
faunosteo/apps/app/e2e/shell/editor-recovery.spec.ts
T

387 lines
14 KiB
TypeScript

import { fr } from '@/i18n/fr'
import type { Locator, Page, TestInfo } from '@playwright/test'
import { rpc } from '../../../../packages/supabase/tests/sandbox/stack'
import { expect, expectAccessible, test } from '../support/browser'
import { dropLateReplays, routeToSandbox } from '../support/sandbox'
import {
copyKeyOf,
editorActor,
expiringEditor,
grantIdOf,
openDraft,
openEditor,
ownerActor,
reloadAccepting,
saveDraft,
seedDraft,
selectTab,
serverDocument,
serverRevision,
standardShape,
storedCopy,
type Actor,
} from '../support/editor'
import { stepUpAdmin } from '../support/admin'
import { secondSession } from '../support/editor'
import { signIn } from '../support/shell'
// V1-2.6 recovery and concurrency (contract C27 to C30, owner 2026-11-04): two sessions
// saving one draft, an older local copy restored then saved over a newer server revision,
// a pending offer that blocks editing until answered, an unreadable copy kept until
// « Ignorer », the conflict read that can be left with Escape, and the rights that change
// under an open editing session.
const dict = fr
const isPhone = (info: TestInfo) => info.project.name === 'chromium-375'
const nameFr = (page: Page) =>
page.getByLabel(dict['editor.general.name_fr'], { exact: true })
async function commitText(field: Locator, value: string) {
await field.fill(value)
await field.press('Enter')
}
const button = (scope: Page | Locator, name: string) =>
scope.getByRole('button', { name, exact: true })
async function openGeneral(page: Page, actor: Actor, sheetId: string) {
await openEditor(page, dict, actor, sheetId)
await selectTab(page, dict, 'general')
}
async function signInAgain(
page: Page,
actor: Actor,
sheetId: string,
): Promise<void> {
await signIn(page, actor.account, dict)
await page.goto('/app/admin')
await stepUpAdmin(page, dict, actor.secret)
await openDraft(page, dict, sheetId)
await selectTab(page, dict, 'general')
}
/** Moves the server draft on as another session would (same account, aal2). */
async function advanceServer(actor: Actor, draftId: string, revision: number) {
const document = await serverDocument<Record<string, unknown>>(draftId)
document.description = { fr: `Serveur ${revision}`, en: `Server ${revision}` }
const saved = await rpc(
'save_sheet_draft',
{
version_id: draftId,
expected_revision: revision,
document,
change_note: 'e2e',
},
actor.aal2,
)
expect(saved.status).toBe(200)
}
test.describe('editor recovery and concurrency', () => {
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; phone read-only covered in editor-access
await routeToSandbox(context)
})
test.afterEach(async ({ context }) => {
await dropLateReplays(context)
})
test('two sessions of one account: the second save meets the conflict, reload keeps the server', async ({
page,
browser,
baseURL,
requests,
}) => {
const actor = await editorActor('fr')
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
await openGeneral(page, actor, seed.sheetId)
const other = await secondSession(browser, baseURL ?? '', 'fr-FR')
await signInAgain(other.page, actor, seed.sheetId)
await commitText(nameFr(page), 'Première session')
await saveDraft(page, dict, 2)
await commitText(nameFr(other.page), 'Seconde session')
await button(other.page, dict['common.save']).click()
const conflict = other.page.getByRole('dialog', {
name: dict['editor.draft.conflict.known'].replace('{revision}', '2'),
})
await expect(conflict).toBeVisible()
await expect(
conflict.getByText(
dict['editor.draft.conflict.by_you'].split('{date}')[0],
{
exact: false,
},
),
).toBeVisible()
await button(conflict, dict['editor.draft.conflict.reload']).click()
await button(conflict, dict['editor.draft.conflict.reload']).click()
await expect(conflict).toBeHidden()
await expect(nameFr(other.page)).toHaveValue('Première session')
expect(await serverRevision(seed.draftId)).toBe(2)
// The reloaded session then saves without a conflict.
await commitText(nameFr(other.page), 'Seconde session, reprise')
await saveDraft(other.page, dict, 3)
await other.context.close()
})
test('an older copy restored then saved: conflict, then « Garder ma version » after the confirmation', async ({
page,
requests,
}) => {
const actor = await editorActor('fr')
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
await openGeneral(page, actor, seed.sheetId)
await commitText(nameFr(page), 'Mon travail local')
await expect(
page.getByText(dict['editor.draft.status.unsaved'], { exact: false }),
).toBeVisible()
await advanceServer(actor, seed.draftId, 1)
expect(await reloadAccepting(page)).toContain('beforeunload')
await expect(
page.getByText(
dict['editor.draft.recovery.older']
.replace('{revision}', '1')
.replace('{server}', '2'),
{ exact: false },
),
).toBeVisible()
await expectAccessible(page, 'the recovery offer')
await button(page, dict['editor.draft.recovery.restore']).click()
await button(page, dict['common.save']).click()
const conflict = page.getByRole('dialog', {
name: dict['editor.draft.conflict.known'].replace('{revision}', '2'),
})
await expect(conflict).toBeVisible()
await button(conflict, dict['editor.draft.conflict.keep']).click()
await expect(conflict).toContainText('Remplacer la révision 2')
await button(conflict, dict['editor.draft.conflict.keep']).click()
await expect(conflict).toBeHidden()
await expect(
page.getByText(
dict['editor.draft.status.revision'].replace('{revision}', '3'),
{
exact: true,
},
),
).toBeVisible()
const saved = await serverDocument<{ name: { fr: string } }>(seed.draftId)
expect(saved.name.fr).toBe('Mon travail local')
})
test('a pending offer blocks editing until answered; ignoring it removes the copy', async ({
page,
requests,
}) => {
const actor = await editorActor('fr')
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
const key = copyKeyOf(actor.account.id, seed.draftId)
await openGeneral(page, actor, seed.sheetId)
await commitText(nameFr(page), 'À conserver')
expect(await storedCopy(page, key)).not.toBeNull()
await reloadAccepting(page)
await expect(
button(page, dict['editor.draft.recovery.restore']),
).toBeVisible()
await expect(button(page, dict['common.save'])).toBeDisabled()
await expect(button(page, dict['editor.draft.discard'])).toBeDisabled()
await selectTab(page, dict, 'general')
await expect(
page.getByTestId('editor-tab-general').getByRole('textbox'),
).toHaveCount(0)
await selectTab(page, dict, 'annotations')
await expect(
page.getByRole('toolbar', { name: dict['editor.annotations.tools'] }),
).toHaveCount(0)
await button(page, dict['editor.draft.recovery.ignore']).click()
await button(
page.getByRole('dialog'),
dict['editor.draft.recovery.ignore_confirm'],
).click()
await expect(
button(page, dict['editor.draft.recovery.restore']),
).toHaveCount(0)
expect(await storedCopy(page, key)).toBeNull()
await selectTab(page, dict, 'general')
await expect(nameFr(page)).toBeEditable()
})
test('an unreadable copy is kept, offers only « Ignorer », and goes when ignored', async ({
page,
requests,
}) => {
const actor = await editorActor('fr')
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
const key = copyKeyOf(actor.account.id, seed.draftId)
await openGeneral(page, actor, seed.sheetId)
await page.evaluate(
([name]) => sessionStorage.setItem(name, '{not json'),
[key],
)
await page.reload()
await expect(
page.getByText(dict['editor.draft.recovery.unreadable'], {
exact: false,
}),
).toBeVisible()
await expect(
button(page, dict['editor.draft.recovery.restore']),
).toHaveCount(0)
await selectTab(page, dict, 'verification')
await selectTab(page, dict, 'general')
expect(await storedCopy(page, key)).toBe('{not json')
await button(page, dict['editor.draft.recovery.ignore']).click()
await expect(
page
.getByRole('dialog')
.getByText(dict['editor.draft.recovery.ignore_unreadable_body']),
).toBeVisible()
await button(
page.getByRole('dialog'),
dict['editor.draft.recovery.ignore_confirm'],
).click()
expect(await storedCopy(page, key)).toBeNull()
await expect(nameFr(page)).toBeEditable()
})
test('the conflict read in progress can be left with Escape', async ({
page,
browser,
baseURL,
requests,
}) => {
const actor = await editorActor('fr')
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
await openGeneral(page, actor, seed.sheetId)
const other = await secondSession(browser, baseURL ?? '', 'fr-FR')
await signInAgain(other.page, actor, seed.sheetId)
await commitText(nameFr(other.page), 'Devant')
await saveDraft(other.page, dict, 2)
await other.context.close()
let release = () => {}
const gate = new Promise<void>((resolve) => (release = resolve))
await page.route('**/rpc/get_official_sheet_admin*', async (route) => {
await gate
await route.fallback()
})
await commitText(nameFr(page), 'Derrière')
await button(page, dict['common.save']).click()
const reading = page.getByRole('dialog', {
name: dict['editor.draft.conflict.reading_title'],
})
await expect(reading).toBeVisible()
await expect(
reading.getByText(dict['editor.draft.conflict.reading']),
).toBeVisible()
await page.keyboard.press('Escape')
await expect(reading).toBeHidden()
// The late answer of the dismissed read arrives, then is dropped: no dialog returns.
const answered = page.waitForResponse((r) =>
r.url().includes('get_official_sheet_admin'),
)
release()
await answered
await page.evaluate(
() =>
new Promise((done) =>
requestAnimationFrame(() => requestAnimationFrame(done)),
),
)
await expect(
page.getByRole('dialog', { name: /enregistré la révision/ }),
).toHaveCount(0)
await expect(button(page, dict['common.save'])).toBeEnabled()
await expect(nameFr(page)).toHaveValue('Derrière')
})
})
test.describe('rights changing under an open editing session', () => {
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; phone read-only covered in editor-access
await routeToSandbox(context)
})
test.afterEach(async ({ context }) => {
await dropLateReplays(context)
})
/** The save after the change: refused, read-only, the copy wording true. */
async function expectRefusedSave(page: Page, key: string) {
await commitText(nameFr(page), 'Après le retrait')
await button(page, dict['common.save']).click()
await expect(
page.getByText(dict['editor.draft.read_only'], { exact: true }),
).toBeVisible()
await expect(
page.getByText(dict['editor.draft.copy_kept'], { exact: false }),
).toBeVisible()
expect(await storedCopy(page, key)).not.toBeNull()
await expect(button(page, dict['common.save'])).toHaveCount(0)
}
test('the editor grant revoked by the owner: the next save is refused', async ({
page,
requests,
}) => {
const [actor, owner] = [await editorActor('fr'), await ownerActor('fr')]
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
await openGeneral(page, actor, seed.sheetId)
const revoked = await rpc(
'revoke_catalog_right',
{
grant_id: await grantIdOf(actor.account, 'sheet_editor'),
reason: 'e2e',
},
owner.aal2,
)
expect(revoked.status).toBe(200)
await expectRefusedSave(page, copyKeyOf(actor.account.id, seed.draftId))
})
test('the admin role removed by the owner: the next save is refused', async ({
page,
requests,
}) => {
const [actor, owner] = [await editorActor('fr'), await ownerActor('fr')]
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
await openGeneral(page, actor, seed.sheetId)
const revoked = await rpc(
'revoke_platform_role',
{ target_user_id: actor.account.id, role: 'admin', reason: 'e2e' },
owner.aal2,
)
expect(revoked.status).toBe(200)
await expectRefusedSave(page, copyKeyOf(actor.account.id, seed.draftId))
})
test('the editor grant expired while editing: the next save is refused', async ({
page,
requests,
}) => {
test.setTimeout(90_000)
const actor = await expiringEditor('fr', 25)
requests.forbid(actor.account.email, actor.account.password)
const seed = await seedDraft(actor, { shape: standardShape })
await openGeneral(page, actor, seed.sheetId)
await page.waitForTimeout(26_000)
await expectRefusedSave(page, copyKeyOf(actor.account.id, seed.draftId))
})
})