Files
faunosteo/apps/app/tests/unit/editor-route-conflict.test.tsx
T
bchanot 99b669bf8f feat(editor): header history, save line and Abandonner les modifications
Header Annuler/Retablir on every tab (aria-disabled, commit on pointerdown,
refused blur commit skips the undo). Save line: unsaved count selector
(savedDistance), saveLineState forms, server save time via readSaveTime
(lastSave in the store, never the browser clock). Discard relabelled and
names the stored copy. Tool column pair removed, glyphs in EditorGlyph.

Rewritten or removed assertions (R-24):
- editor-route.test: Revision 3 / unsaved text / Annuler les modifications
  -> save-line testid, Abandonner label, copy sentence in the dialog
- editor-route-conflict, editor-route-recovery: Revision n and copy_cap/
  copy_quota text queries -> save-line testid
- editor-no-loss: discard button label
- editor-annotations-tab: tool column undo/redo test -> no pair left
  (behaviour moved to editor-history-buttons.test)
- e2e journey/annotations/recovery/structures/support saveDraft/lan:
  undo/redo and revision/unsaved text -> historyButton, saveLine helpers;
  structures toHaveCount(0) on old text -> exact Revision n line
- editor-shell clickability: header Annuler/Retablir added
2026-11-08 17:55:49 +01:00

212 lines
7.9 KiB
TypeScript

/** Lot E: the conflict dialog, known and unknown variants, and its two confirmations. */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { SheetFailure } from '@faunosteo/supabase'
import {
USER,
VERSION,
baseText,
commitMove,
resetStore,
sb,
serverHas,
store,
} from '../helpers/editorHarness'
import { adminAnswer, mountDraft, setWidth } from '../helpers/editorRoute'
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
vi.mock('@faunosteo/supabase', async (orig) => ({
...(await orig<typeof import('@faunosteo/supabase')>()),
getSheetVersion: vi.fn(),
downloadSheetDrawing: vi.fn(),
saveSheetDraft: vi.fn(),
getOfficialSheetAdmin: vi.fn(),
submitSheetReview: vi.fn(),
}))
beforeEach(() => serverHas(baseText(), 3))
afterEach(() => {
cleanup()
resetStore()
setWidth(1024)
})
const ready = () =>
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
const conflict = () => new SheetFailure('draft_conflict')
function serverDraft(revision: number, lastSaveBy: string | null) {
const save =
lastSaveBy === null
? null
: {
revision,
saved_at: '2026-11-04T13:32:00Z',
saved_by: {
user_id: lastSaveBy,
display_name: lastSaveBy === USER ? 'Moi' : 'Autre Praticien',
},
}
return adminAnswer({
draft_version: { version_id: VERSION, version_number: 4, revision },
draft_last_save: save,
})
}
/** What the conflict read answers: a draft, or a failure (built when the read runs). */
type Read = ReturnType<typeof serverDraft> | (() => Promise<never>)
async function startConflict(next: Read) {
const user = userEvent.setup()
sb.saveSheetDraft.mockRejectedValueOnce(conflict())
mountDraft()
await ready()
act(() => void commitMove(40))
if (typeof next === 'function')
sb.getOfficialSheetAdmin.mockImplementationOnce(next as never)
else sb.getOfficialSheetAdmin.mockResolvedValueOnce(next as never)
await user.click(await screen.findByRole('button', { name: 'Enregistrer' }))
return { user, dialog: within(await screen.findByRole('dialog')) }
}
describe('known revision', () => {
it('names N, the author and the Paris time; keep asks first, then saves against N', async () => {
const { user, dialog } = await startConflict(serverDraft(5, 'other-user'))
expect(
await dialog.findByText(
'par Autre Praticien, le 4 novembre 2026 à 14:32',
),
).toBeInTheDocument()
expect(screen.getByRole('dialog')).toHaveAccessibleName(
'Une autre session a enregistré la révision 5',
)
expect(dialog.queryByRole('button', { name: 'Réessayer' })).toBeNull()
sb.saveSheetDraft.mockResolvedValueOnce({ revision: 6 } as never)
await user.click(dialog.getByRole('button', { name: 'Garder ma version' }))
expect(
dialog.getByText(
'Remplacer la révision 5 (Autre Praticien, 4 novembre 2026 à 14:32) par votre version ?',
),
).toBeInTheDocument()
expect(sb.saveSheetDraft).toHaveBeenCalledTimes(1)
await user.click(dialog.getByRole('button', { name: 'Garder ma version' }))
await waitFor(() => expect(sb.saveSheetDraft).toHaveBeenCalledTimes(2))
expect(sb.saveSheetDraft).toHaveBeenLastCalledWith(
expect.anything(),
expect.objectContaining({ expected_revision: 5 }),
)
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
await waitFor(() =>
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Enregistré · révision 6$/),
)
})
it('saved by the caller: the neutral own-account wording', async () => {
const { dialog } = await startConflict(serverDraft(5, USER))
expect(
await dialog.findByText(
'depuis une autre session de votre compte, le 4 novembre 2026 à 14:32',
),
).toBeInTheDocument()
expect(dialog.queryByText(/Moi/)).toBeNull()
})
it('no matching last save: N alone, never an invented author', async () => {
const { user, dialog } = await startConflict(serverDraft(5, null))
expect(
await screen.findByRole('dialog', {
name: 'Une autre session a enregistré la révision 5',
}),
).toBeInTheDocument()
expect(dialog.queryByText(/par |depuis/)).toBeNull()
await user.click(dialog.getByRole('button', { name: 'Garder ma version' }))
expect(
dialog.getByText('Remplacer la révision 5 par votre version ?'),
).toBeInTheDocument()
})
it('reload asks first, then reloads the server draft and clears the dialog', async () => {
const { user, dialog } = await startConflict(serverDraft(5, 'other-user'))
await dialog.findByText(/Autre Praticien/)
serverHas(baseText(), 5)
await user.click(dialog.getByRole('button', { name: 'Recharger' }))
expect(
dialog.getByText(
'Abandonner vos modifications et recharger la révision 5 ?',
),
).toBeInTheDocument()
await user.click(dialog.getByRole('button', { name: 'Recharger' }))
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
expect(store.getState()).toMatchObject({ unsaved: false, savedRevision: 5 })
await waitFor(() =>
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Révision 5$/),
)
})
it('Annuler leaves everything as it was', async () => {
const { user, dialog } = await startConflict(serverDraft(5, 'other-user'))
await dialog.findByText(/Autre Praticien/)
await user.click(dialog.getByRole('button', { name: 'Annuler' }))
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
expect(store.getState().unsaved).toBe(true)
expect(sb.saveSheetDraft).toHaveBeenCalledTimes(1)
})
})
describe('unknown revision (failed read)', () => {
const failed = () => Promise.reject(new SheetFailure('unavailable')) // a Read: built when called
it('no number, no author; keep disabled with its hint; reload and retry offered', async () => {
const { user, dialog } = await startConflict(failed)
expect(
await screen.findByRole('dialog', {
name: 'Une autre session a enregistré une version plus récente de ce brouillon.',
}),
).toBeInTheDocument()
expect(dialog.queryByText(/révision \d/)).toBeNull()
const keep = dialog.getByRole('button', { name: 'Garder ma version' })
expect(keep).toBeDisabled()
expect(keep).toHaveAccessibleDescription(
'Révision du serveur inconnue : rechargez ou réessayez.',
)
expect(dialog.getByRole('button', { name: 'Recharger' })).toBeEnabled()
// « Réessayer » repeats the read and reaches the known variant
sb.getOfficialSheetAdmin.mockResolvedValueOnce(
serverDraft(7, 'other-user') as never,
)
await user.click(dialog.getByRole('button', { name: 'Réessayer' }))
expect(
await screen.findByRole('dialog', {
name: 'Une autre session a enregistré la révision 7',
}),
).toBeInTheDocument()
})
it('reload names the server version without a number', async () => {
const { user, dialog } = await startConflict(failed)
await user.click(await dialog.findByRole('button', { name: 'Recharger' }))
expect(
dialog.getByText(
'Abandonner vos modifications et recharger la version du serveur ?',
),
).toBeInTheDocument()
})
})
describe('after the dialog is dismissed', () => {
it('the header keeps a notice that a newer version exists', async () => {
const { user, dialog } = await startConflict(() =>
Promise.reject(new SheetFailure('unavailable')),
)
await dialog.findByRole('button', { name: 'Garder ma version' })
await user.click(dialog.getByRole('button', { name: 'Annuler' }))
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
expect(
screen.getByText(
'Une autre session a enregistré une version plus récente de ce brouillon.',
),
).toBeInTheDocument()
})
})