Files
faunosteo/apps/app/tests/unit/editor-route-conflict.test.tsx
T

208 lines
7.8 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())
expect(await screen.findByText('Révision 6')).toBeInTheDocument()
})
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 })
expect(await screen.findByText('Révision 5')).toBeInTheDocument()
})
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()
})
})