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
This commit is contained in:
1 parent
ab51ea764d
commit
99b669bf8f
29 files changed
+1397
-248
No files matched your search
@@ -507,19 +507,12 @@ describe('tools and categories', () => {
|
||||
).toBeDisabled()
|
||||
})
|
||||
|
||||
it('undoes and redoes through the tool column', async () => {
|
||||
it('keeps no undo or redo in the tool column (they sit in the header)', async () => {
|
||||
await mount()
|
||||
await selectFromList()
|
||||
const user = userEvent.setup()
|
||||
const cx = within(
|
||||
screen.getByTestId('annotation-properties'),
|
||||
).getByLabelText('Centre x')
|
||||
await user.clear(cx)
|
||||
await user.type(cx, '60{Enter}')
|
||||
await user.click(screen.getByRole('button', { name: 'Annuler' }))
|
||||
expect(geometryOf('anno-a')).toMatchObject({ cx: 50 })
|
||||
await user.click(screen.getByRole('button', { name: 'Rétablir' }))
|
||||
expect(geometryOf('anno-a')).toMatchObject({ cx: 60 })
|
||||
const tools = screen.getByRole('toolbar', { name: 'Outils' })
|
||||
expect(
|
||||
within(tools).queryByRole('button', { name: /Annuler|Rétablir/ }),
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,389 @@
|
||||
/** Lot E: the header « Annuler » / « Rétablir », the save line's announcements, discard. */
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
act,
|
||||
cleanup,
|
||||
fireEvent,
|
||||
screen,
|
||||
waitFor,
|
||||
within,
|
||||
} from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { fr } from '@/i18n/fr'
|
||||
import {
|
||||
baseText,
|
||||
commitMove,
|
||||
resetStore,
|
||||
sb,
|
||||
serverHas,
|
||||
storedCopy,
|
||||
store,
|
||||
} from '../helpers/editorHarness'
|
||||
import {
|
||||
adminAnswer,
|
||||
grant,
|
||||
mountDraft,
|
||||
round,
|
||||
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 UNDO = 'Annuler la dernière modification'
|
||||
const REDO = 'Rétablir la dernière modification annulée'
|
||||
const ready = () =>
|
||||
screen.findByRole('heading', { level: 1, name: 'Brouillon, version 4' })
|
||||
const undoButton = () => screen.getByRole('button', { name: UNDO })
|
||||
const redoButton = () => screen.getByRole('button', { name: REDO })
|
||||
const disabled = (button: HTMLElement) =>
|
||||
button.getAttribute('aria-disabled') === 'true'
|
||||
const geometry = () =>
|
||||
store.getState().previewState?.document.annotations[0].geometry
|
||||
const nameFr = () =>
|
||||
store.getState().previewState?.document.annotations[0].name?.fr
|
||||
|
||||
async function selectAnnotation(user: ReturnType<typeof userEvent.setup>) {
|
||||
const list = await screen.findByRole('list', {
|
||||
name: 'Annotations de la vue',
|
||||
})
|
||||
await user.click(within(list).getAllByRole('button')[0])
|
||||
const panel = await screen.findByTestId('annotation-properties')
|
||||
return within(panel).getByLabelText('Nom (FR)') as HTMLInputElement
|
||||
}
|
||||
|
||||
describe('the buttons', () => {
|
||||
it('have the T15 names, titles, shortcuts and a visible label', async () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
const row = within(screen.getByTestId('history-buttons'))
|
||||
expect(undoButton()).toHaveAttribute('title', UNDO)
|
||||
expect(redoButton()).toHaveAttribute('title', REDO)
|
||||
expect(undoButton()).toHaveAttribute('aria-keyshortcuts', 'Control+Z')
|
||||
expect(redoButton()).toHaveAttribute('aria-keyshortcuts', 'Control+Shift+Z')
|
||||
expect(row.getAllByRole('button').map((b) => b.textContent)).toEqual([
|
||||
'Annuler',
|
||||
'Rétablir',
|
||||
])
|
||||
expect(undoButton()).not.toHaveAttribute('disabled')
|
||||
})
|
||||
|
||||
it('are enabled exactly when the store can step, and step', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
expect([disabled(undoButton()), disabled(redoButton())]).toEqual([
|
||||
true,
|
||||
true,
|
||||
])
|
||||
act(() => void commitMove(40))
|
||||
expect([disabled(undoButton()), disabled(redoButton())]).toEqual([
|
||||
false,
|
||||
true,
|
||||
])
|
||||
await user.click(undoButton())
|
||||
expect(geometry()).toMatchObject({ cx: 50 })
|
||||
expect([disabled(undoButton()), disabled(redoButton())]).toEqual([
|
||||
true,
|
||||
false,
|
||||
])
|
||||
await user.click(redoButton())
|
||||
expect(geometry()).toMatchObject({ cx: 40 })
|
||||
})
|
||||
|
||||
it('stay focused when the last undo empties the stack', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
await user.click(undoButton())
|
||||
expect(disabled(undoButton())).toBe(true)
|
||||
expect(undoButton()).toHaveFocus()
|
||||
})
|
||||
|
||||
it('are disabled in every mode that cannot edit', async () => {
|
||||
const modes: Array<[string, Parameters<typeof mountDraft>[0]]> = [
|
||||
['narrow', { width: 800 }],
|
||||
['rights', { grants: [grant('sheet_reviewer')], roles: [] }],
|
||||
['frozen', { answer: adminAnswer({ rounds: [round('open')] }) }],
|
||||
['historical', { answer: adminAnswer({ historical: true }) }],
|
||||
['inactive', { answer: adminAnswer({ status: 'archived' }) }],
|
||||
]
|
||||
for (const [, options] of modes) {
|
||||
serverHas(baseText(), 3)
|
||||
const view = mountDraft(options)
|
||||
await ready()
|
||||
store.setState({ undo: [{ text: baseText(), bytes: 1 }] })
|
||||
expect(disabled(undoButton())).toBe(true)
|
||||
await userEvent.setup().click(undoButton())
|
||||
expect(store.getState().undo).toHaveLength(1)
|
||||
view.unmount()
|
||||
cleanup()
|
||||
resetStore()
|
||||
}
|
||||
})
|
||||
|
||||
it('are disabled while a recovery decision is pending', async () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
act(() => store.setState({ recovery: 'pending' }))
|
||||
expect(disabled(undoButton())).toBe(true)
|
||||
})
|
||||
|
||||
it('stay enabled while saving, in step with Ctrl+Z', async () => {
|
||||
const user = userEvent.setup()
|
||||
sb.saveSheetDraft.mockReturnValue(new Promise(() => undefined) as never)
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||
await waitFor(() => expect(store.getState().saving).toBe(true))
|
||||
expect(disabled(undoButton())).toBe(false)
|
||||
fireEvent.keyDown(document.body, { key: 'z', ctrlKey: true })
|
||||
expect(geometry()).toMatchObject({ cx: 50 })
|
||||
expect(store.getState().redo).toHaveLength(1)
|
||||
await user.click(redoButton())
|
||||
expect(geometry()).toMatchObject({ cx: 40 })
|
||||
})
|
||||
|
||||
it('undo from Général and from Constats reverts a canvas change', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
await user.click(screen.getByRole('tab', { name: 'Général' }))
|
||||
await user.click(undoButton())
|
||||
expect(geometry()).toMatchObject({ cx: 50 })
|
||||
await user.click(redoButton())
|
||||
await user.click(screen.getByRole('tab', { name: 'Constats' }))
|
||||
await user.click(undoButton())
|
||||
expect(geometry()).toMatchObject({ cx: 50 })
|
||||
})
|
||||
|
||||
it('leave no undo or redo pair in the tool column', async () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
const tools = screen.getByRole('toolbar', {
|
||||
name: fr['editor.annotations.tools'],
|
||||
})
|
||||
expect(
|
||||
within(tools).queryByRole('button', { name: /Annuler|Rétablir/ }),
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('typed text and the press', () => {
|
||||
it('undo reverts exactly the typed text, with and without earlier history', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
const field = await selectAnnotation(user)
|
||||
const original = nameFr()
|
||||
await user.clear(field)
|
||||
await user.type(field, 'Nouveau')
|
||||
await user.pointer({ keys: '[MouseLeft]', target: undoButton() })
|
||||
expect(nameFr()).toBe(original)
|
||||
expect(store.getState().redo).toHaveLength(1)
|
||||
act(() => void commitMove(40))
|
||||
await user.clear(field)
|
||||
await user.type(field, 'Encore')
|
||||
await user.pointer({ keys: '[MouseLeft]', target: undoButton() })
|
||||
expect(nameFr()).toBe(original)
|
||||
expect(geometry()).toMatchObject({ cx: 40 })
|
||||
})
|
||||
|
||||
it('the pointer press blurs the focused field', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
const field = await selectAnnotation(user)
|
||||
await user.clear(field)
|
||||
await user.type(field, 'Saisie')
|
||||
fireEvent.pointerDown(undoButton())
|
||||
expect(field).not.toHaveFocus()
|
||||
expect(nameFr()).toBe('Saisie')
|
||||
})
|
||||
|
||||
it('a refused blur commit skips the undo and returns focus to the field', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
const field = await selectAnnotation(user)
|
||||
await user.clear(field)
|
||||
await user.pointer({ keys: '[MouseLeft]', target: undoButton() })
|
||||
expect(field).toHaveAttribute('aria-invalid', 'true')
|
||||
expect(field).toHaveFocus()
|
||||
expect(geometry()).toMatchObject({ cx: 40 })
|
||||
})
|
||||
|
||||
it('Ctrl+Z inside a field leaves the store untouched', async () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
const before = store.getState().documentText
|
||||
fireEvent.keyDown(document.querySelector('input') as Element, {
|
||||
key: 'z',
|
||||
ctrlKey: true,
|
||||
})
|
||||
expect(store.getState().documentText).toBe(before)
|
||||
})
|
||||
})
|
||||
|
||||
describe('the save status', () => {
|
||||
it('announces state transitions, never each count', async () => {
|
||||
const user = userEvent.setup()
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
mountDraft()
|
||||
await ready()
|
||||
const status = screen.getByTestId('save-status')
|
||||
expect(status).toHaveTextContent(/^$/)
|
||||
act(() => void commitMove(40))
|
||||
expect(status).toHaveTextContent(/^Modifications non enregistrées$/)
|
||||
act(() => void commitMove(41))
|
||||
expect(status).toHaveTextContent(/^Modifications non enregistrées$/)
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
||||
/^2 modifications non enregistrées$/,
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||
await waitFor(() =>
|
||||
expect(status).toHaveTextContent(/^Enregistré · révision 4$/),
|
||||
)
|
||||
})
|
||||
|
||||
it('shows the server time once read, « Enregistré · révision n » otherwise', async () => {
|
||||
const user = userEvent.setup()
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(
|
||||
adminAnswer({
|
||||
draft_version: { version_id: 'ver-1', version_number: 4, revision: 4 },
|
||||
draft_last_save: {
|
||||
revision: 4,
|
||||
saved_at: '2026-11-04T13:32:00Z',
|
||||
saved_by: null,
|
||||
},
|
||||
}) as never,
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
||||
/^Enregistré à 14:32 · révision 4$/,
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
it('shows « Enregistré · révision n » when the read fails, editor still mounted', async () => {
|
||||
const user = userEvent.setup()
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
sb.getOfficialSheetAdmin.mockRejectedValue(new Error('offline'))
|
||||
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
||||
/^Enregistré · révision 4$/,
|
||||
),
|
||||
)
|
||||
expect(screen.getByRole('heading', { level: 1 })).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows no saved line while the save is in flight', async () => {
|
||||
const user = userEvent.setup()
|
||||
sb.saveSheetDraft.mockReturnValue(new Promise(() => undefined) as never)
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(
|
||||
/^Enregistrement…$/,
|
||||
)
|
||||
})
|
||||
|
||||
it('reserves its widest form so the row does not move', async () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
const sizers = screen
|
||||
.getByTestId('save-line')
|
||||
.parentElement!.querySelectorAll('[aria-hidden]')
|
||||
expect([...sizers].map((el) => el.textContent)).toEqual([
|
||||
'Enregistré à 00:00 · révision 999',
|
||||
'999 modifications non enregistrées',
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('« Abandonner les modifications »', () => {
|
||||
const open = async (user: ReturnType<typeof userEvent.setup>) => {
|
||||
await user.click(
|
||||
screen.getByRole('button', { name: 'Abandonner les modifications' }),
|
||||
)
|
||||
return within(await screen.findByRole('dialog'))
|
||||
}
|
||||
|
||||
it('names the stored copy, and keeps changes and copy on cancel', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
const dialog = await open(user)
|
||||
expect(
|
||||
dialog.getByText(new RegExp(fr['editor.draft.discard_copy'])),
|
||||
).toBeInTheDocument()
|
||||
await user.click(dialog.getByRole('button', { name: 'Annuler' }))
|
||||
expect(store.getState().unsaved).toBe(true)
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
})
|
||||
|
||||
it('names a copy left by an earlier write after a quota refusal', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
expect(storedCopy()).not.toBeNull()
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
||||
throw new DOMException('full', 'QuotaExceededError')
|
||||
})
|
||||
act(() => void commitMove(41))
|
||||
await waitFor(() => expect(store.getState().copyIssue).toBe('quota'))
|
||||
const dialog = await open(user)
|
||||
expect(
|
||||
dialog.getByText(new RegExp(fr['editor.draft.discard_copy'])),
|
||||
).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('does not mention a copy when none is stored', async () => {
|
||||
const user = userEvent.setup()
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
vi.spyOn(sessionStorage, 'setItem').mockImplementation(() => {
|
||||
throw new DOMException('full', 'QuotaExceededError')
|
||||
})
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
const dialog = await open(user)
|
||||
expect(dialog.getByText(fr['editor.draft.discard_ask'])).toBeInTheDocument()
|
||||
expect(
|
||||
dialog.queryByText(new RegExp(fr['editor.draft.discard_copy'])),
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -314,7 +314,7 @@ describe('the guards of a width change', () => {
|
||||
element: point('anno-a', 60, 50, 5),
|
||||
}),
|
||||
)
|
||||
await user.click(button('Annuler les modifications'))
|
||||
await user.click(button('Abandonner les modifications'))
|
||||
expect(document.querySelectorAll('dialog[open]')).toHaveLength(1)
|
||||
act(() => store.setState({ recovery: 'pending' }))
|
||||
await waitFor(() =>
|
||||
|
||||
@@ -97,7 +97,9 @@ describe('known revision', () => {
|
||||
expect.objectContaining({ expected_revision: 5 }),
|
||||
)
|
||||
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
||||
expect(await screen.findByText('Révision 6')).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Enregistré · révision 6$/),
|
||||
)
|
||||
})
|
||||
|
||||
it('saved by the caller: the neutral own-account wording', async () => {
|
||||
@@ -137,7 +139,9 @@ describe('known revision', () => {
|
||||
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()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Révision 5$/),
|
||||
)
|
||||
})
|
||||
|
||||
it('Annuler leaves everything as it was', async () => {
|
||||
|
||||
@@ -121,9 +121,9 @@ describe('the blocking offer', () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
await user.click(screen.getByRole('button', { name: 'Restaurer la copie' }))
|
||||
expect(
|
||||
await screen.findByText('Révision 3 · modifications non enregistrées'),
|
||||
).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Modifications non enregistrées$/),
|
||||
)
|
||||
expect(
|
||||
screen.queryByRole('button', { name: 'Restaurer la copie' }),
|
||||
).toBeNull()
|
||||
@@ -231,9 +231,9 @@ describe('cap and quota', () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
expect(
|
||||
await screen.findByText(fr['editor.draft.status.copy_cap']),
|
||||
).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(fr['editor.draft.status.copy_cap']),
|
||||
)
|
||||
expect(storedCopy()).toBeNull()
|
||||
const items = screen
|
||||
.getAllByRole('listitem')
|
||||
@@ -256,7 +256,9 @@ describe('cap and quota', () => {
|
||||
await waitFor(() => expect(storedCopy()).not.toBeNull())
|
||||
expect(sessionStorage.getItem(copyKeyOf('ver-2'))).toBeNull()
|
||||
expect(sessionStorage.getItem(copyKeyOf('ver-3'))).not.toBeNull()
|
||||
expect(screen.queryByText(fr['editor.draft.status.copy_cap'])).toBeNull()
|
||||
expect(screen.getByTestId('save-line')).not.toHaveTextContent(
|
||||
fr['editor.draft.status.copy_cap'],
|
||||
)
|
||||
})
|
||||
|
||||
it('quota: the full-storage status and notice, the other copies untouched', async () => {
|
||||
@@ -267,9 +269,9 @@ describe('cap and quota', () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
expect(
|
||||
await screen.findByText(fr['editor.draft.status.copy_quota']),
|
||||
).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(fr['editor.draft.status.copy_quota']),
|
||||
)
|
||||
expect(
|
||||
screen.getByText(fr['editor.draft.copy_issue.quota']),
|
||||
).toBeInTheDocument()
|
||||
|
||||
@@ -51,8 +51,7 @@ describe('edit mode', () => {
|
||||
await ready()
|
||||
expect(screen.getByText('Brouillon')).toBeInTheDocument()
|
||||
expect(screen.getByText('Copie de travail')).toBeInTheDocument()
|
||||
expect(screen.getByRole('status', { name: '' })).toBeDefined()
|
||||
expect(screen.getByText('Révision 3')).toBeInTheDocument()
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Révision 3$/)
|
||||
const tabs = within(screen.getByRole('tablist')).getAllByRole('tab')
|
||||
expect(tabs.map((tab) => tab.textContent)).toEqual(TABS)
|
||||
expect(screen.getByRole('tab', { name: 'Annotations' })).toHaveAttribute(
|
||||
@@ -81,11 +80,13 @@ describe('edit mode', () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
expect(
|
||||
await screen.findByText('Révision 3 · modifications non enregistrées'),
|
||||
).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(/^1 modification non enregistrée$/),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||
expect(await screen.findByText('Révision 4')).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Enregistré · révision 4$/),
|
||||
)
|
||||
expect(sb.saveSheetDraft).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({ version_id: VERSION, expected_revision: 3 }),
|
||||
@@ -93,21 +94,25 @@ describe('edit mode', () => {
|
||||
expect(storedCopy()).toBeNull()
|
||||
})
|
||||
|
||||
it('« Annuler les modifications » asks first, then reloads the server draft', async () => {
|
||||
it('« Abandonner les modifications » asks first, then reloads the server draft', async () => {
|
||||
const user = userEvent.setup()
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
await user.click(
|
||||
await screen.findByRole('button', { name: 'Annuler les modifications' }),
|
||||
await screen.findByRole('button', { name: 'Abandonner les modifications' }),
|
||||
)
|
||||
const dialog = within(await screen.findByRole('dialog'))
|
||||
expect(dialog.getByText(fr['editor.draft.discard_ask'])).toBeInTheDocument()
|
||||
const asked = await screen.findByRole('dialog')
|
||||
expect(asked).toHaveTextContent(fr['editor.draft.discard_ask'])
|
||||
expect(asked).toHaveTextContent(fr['editor.draft.discard_copy'])
|
||||
const dialog = within(asked)
|
||||
await user.click(
|
||||
dialog.getByRole('button', { name: 'Abandonner les modifications' }),
|
||||
)
|
||||
await waitFor(() => expect(store.getState().unsaved).toBe(false))
|
||||
expect(await screen.findByText('Révision 3')).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(/^Révision 3$/),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -418,9 +423,9 @@ describe('permission lost during the session', () => {
|
||||
mountDraft()
|
||||
await ready()
|
||||
act(() => void commitMove(40))
|
||||
expect(
|
||||
await screen.findByText(fr['editor.draft.status.copy_quota']),
|
||||
).toBeInTheDocument()
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('save-line')).toHaveTextContent(fr['editor.draft.status.copy_quota']),
|
||||
)
|
||||
await user.click(screen.getByRole('button', { name: 'Enregistrer' }))
|
||||
const notice = await screen.findByText(/Votre session a expiré/)
|
||||
expect(notice).toHaveTextContent(fr['editor.draft.copy_not_kept'])
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
/** Lot E: the unsaved-count selector, the save line's forms and the server save time. */
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { SheetFailure } from '@faunosteo/supabase'
|
||||
import { saveLineState, savedDistance } from '@/editor/saveLine'
|
||||
import {
|
||||
HISTORY_LIMIT_BYTES,
|
||||
emptyHistory,
|
||||
recordCommit,
|
||||
stepBack,
|
||||
stepForward,
|
||||
entryOf,
|
||||
} from '@/editor/state/undo'
|
||||
import saveLineSource from '@/editor/saveLine.ts?raw'
|
||||
import saveSource from '@/editor/state/editorSave.ts?raw'
|
||||
import {
|
||||
VERSION,
|
||||
commitMove,
|
||||
openReady,
|
||||
resetStore,
|
||||
sb,
|
||||
store,
|
||||
} from '../helpers/editorHarness'
|
||||
import { adminAnswer } 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(),
|
||||
}))
|
||||
|
||||
afterEach(resetStore)
|
||||
|
||||
const distance = () => savedDistance(store.getState())
|
||||
const SAVED_AT = '2026-11-04T13:32:00Z'
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void
|
||||
const promise = new Promise<T>((res) => {
|
||||
resolve = res
|
||||
})
|
||||
return { promise, resolve }
|
||||
}
|
||||
|
||||
function adminAt(revision: number, version = VERSION) {
|
||||
return adminAnswer({
|
||||
draft_version: { version_id: version, version_number: 4, revision },
|
||||
draft_last_save: { revision, saved_at: SAVED_AT, saved_by: null },
|
||||
}) as never
|
||||
}
|
||||
|
||||
describe('savedDistance', () => {
|
||||
it('is 0 at load and counts commits', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
expect(distance()).toBe(0)
|
||||
commitMove(40)
|
||||
expect(distance()).toBe(1)
|
||||
commitMove(41)
|
||||
expect(distance()).toBe(2)
|
||||
})
|
||||
|
||||
it('follows undo and redo around the save point', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
commitMove(41)
|
||||
store.getState().undoStep()
|
||||
expect(distance()).toBe(1)
|
||||
store.getState().undoStep()
|
||||
expect(distance()).toBe(0)
|
||||
store.getState().redoStep()
|
||||
expect(distance()).toBe(1)
|
||||
})
|
||||
|
||||
it('counts the redo side when the save point lies ahead', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
commitMove(41)
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
await store.getState().save()
|
||||
store.getState().undoStep()
|
||||
store.getState().undoStep()
|
||||
expect(distance()).toBe(2)
|
||||
})
|
||||
|
||||
it('is null once a commit follows an undo past the save point', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
commitMove(41)
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
await store.getState().save()
|
||||
store.getState().undoStep()
|
||||
store.getState().undoStep()
|
||||
commitMove(50)
|
||||
expect(distance()).toBeNull()
|
||||
})
|
||||
|
||||
it('is null for a stale base', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
store.setState({ staleBase: true })
|
||||
expect(distance()).toBeNull()
|
||||
})
|
||||
|
||||
it('is null once a trim dropped the saved text (commit, undo and redo)', () => {
|
||||
const big = (mark: string) => mark.repeat(HISTORY_LIMIT_BYTES + 1)
|
||||
const state = (history: ReturnType<typeof emptyHistory>, text: string) => ({
|
||||
staleBase: false,
|
||||
documentText: text,
|
||||
savedText: 'saved',
|
||||
...history,
|
||||
})
|
||||
const first = { undo: [entryOf('saved')], redo: [] }
|
||||
expect(savedDistance(state(first, 'now'))).toBe(1)
|
||||
const committed = recordCommit(first, big('a'))
|
||||
expect(savedDistance(state(committed, 'now'))).toBeNull()
|
||||
const undone = stepBack(
|
||||
{ undo: [entryOf('saved'), entryOf('mid')], redo: [] },
|
||||
big('b'),
|
||||
)
|
||||
expect(savedDistance(state(undone!.history, undone!.text))).toBeNull()
|
||||
const ahead = { undo: [entryOf('saved')], redo: [entryOf('mid')] }
|
||||
const redone = stepForward(ahead, big('d'))
|
||||
expect(savedDistance(state(redone!.history, redone!.text))).toBeNull()
|
||||
})
|
||||
|
||||
it('is null after changes made while a save was in flight moved off the save point', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
commitMove(41)
|
||||
commitMove(42)
|
||||
const answer = deferred<unknown>()
|
||||
sb.saveSheetDraft.mockReturnValue(answer.promise as never)
|
||||
const saving = store.getState().save()
|
||||
store.getState().undoStep()
|
||||
store.getState().undoStep()
|
||||
commitMove(43)
|
||||
commitMove(44)
|
||||
commitMove(45)
|
||||
answer.resolve({ revision: 4 })
|
||||
await saving
|
||||
expect(distance()).toBeNull()
|
||||
})
|
||||
|
||||
it('is null for one undo and one commit in flight', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
commitMove(41)
|
||||
const answer = deferred<unknown>()
|
||||
sb.saveSheetDraft.mockReturnValue(answer.promise as never)
|
||||
const saving = store.getState().save()
|
||||
store.getState().undoStep()
|
||||
commitMove(43)
|
||||
answer.resolve({ revision: 4 })
|
||||
await saving
|
||||
expect(distance()).toBeNull()
|
||||
})
|
||||
|
||||
it('the nearest duplicate of the saved text wins', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
commitMove(41)
|
||||
commitMove(40)
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
await store.getState().save()
|
||||
commitMove(42)
|
||||
expect(distance()).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('saveLineState', () => {
|
||||
const base = {
|
||||
copyIssue: null,
|
||||
saving: false,
|
||||
unsaved: false,
|
||||
distance: 0 as number | null,
|
||||
lastSave: null,
|
||||
savedRevision: 5,
|
||||
}
|
||||
|
||||
it('gives the five forms, first match wins', () => {
|
||||
const saved = { revision: 5, savedAt: SAVED_AT }
|
||||
expect(
|
||||
saveLineState({ ...base, copyIssue: 'cap', saving: true, unsaved: true }),
|
||||
).toEqual({
|
||||
kind: 'copy_issue',
|
||||
issue: 'cap',
|
||||
})
|
||||
expect(saveLineState({ ...base, saving: true, unsaved: true })).toEqual({
|
||||
kind: 'saving',
|
||||
})
|
||||
expect(
|
||||
saveLineState({ ...base, unsaved: true, distance: 3, lastSave: saved }),
|
||||
).toEqual({
|
||||
kind: 'unsaved',
|
||||
count: 3,
|
||||
})
|
||||
expect(saveLineState({ ...base, lastSave: saved })).toEqual({
|
||||
kind: 'saved',
|
||||
revision: 5,
|
||||
savedAt: SAVED_AT,
|
||||
})
|
||||
expect(
|
||||
saveLineState({ ...base, lastSave: { revision: 5, savedAt: null } }),
|
||||
).toEqual({
|
||||
kind: 'saved',
|
||||
revision: 5,
|
||||
savedAt: null,
|
||||
})
|
||||
expect(saveLineState(base)).toEqual({ kind: 'revision', revision: 5 })
|
||||
})
|
||||
|
||||
it('has no count when the distance is unknown or zero', () => {
|
||||
expect(saveLineState({ ...base, unsaved: true, distance: null })).toEqual({
|
||||
kind: 'unsaved',
|
||||
count: null,
|
||||
})
|
||||
expect(saveLineState({ ...base, unsaved: true, distance: 0 })).toEqual({
|
||||
kind: 'unsaved',
|
||||
count: null,
|
||||
})
|
||||
})
|
||||
|
||||
it('shows « Révision n » when the last save is of another revision', () => {
|
||||
const old = { revision: 4, savedAt: SAVED_AT }
|
||||
expect(saveLineState({ ...base, lastSave: old })).toEqual({
|
||||
kind: 'revision',
|
||||
revision: 5,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('readSaveTime', () => {
|
||||
async function savedOnce() {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 4 } as never)
|
||||
}
|
||||
|
||||
it('writes the server time for this version and revision', async () => {
|
||||
await savedOnce()
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(adminAt(4))
|
||||
expect(await store.getState().save()).toEqual({ ok: true, revision: 4 })
|
||||
await vi.waitFor(() =>
|
||||
expect(store.getState().lastSave).toEqual({
|
||||
revision: 4,
|
||||
savedAt: SAVED_AT,
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
it('resolves the save before the read answers', async () => {
|
||||
await savedOnce()
|
||||
const read = deferred<never>()
|
||||
sb.getOfficialSheetAdmin.mockReturnValue(read.promise)
|
||||
expect(await store.getState().save()).toEqual({ ok: true, revision: 4 })
|
||||
expect(store.getState().lastSave).toEqual({ revision: 4, savedAt: null })
|
||||
read.resolve(adminAt(4))
|
||||
await vi.waitFor(() =>
|
||||
expect(store.getState().lastSave?.savedAt).toBe(SAVED_AT),
|
||||
)
|
||||
})
|
||||
|
||||
it('drops an answer for another version or another revision', async () => {
|
||||
await savedOnce()
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(adminAt(4, 'other'))
|
||||
await store.getState().save()
|
||||
await vi.waitFor(() =>
|
||||
expect(sb.getOfficialSheetAdmin).toHaveBeenCalledTimes(1),
|
||||
)
|
||||
await Promise.resolve()
|
||||
expect(store.getState().lastSave).toEqual({ revision: 4, savedAt: null })
|
||||
commitMove(41)
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 5 } as never)
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(adminAt(4))
|
||||
await store.getState().save()
|
||||
await vi.waitFor(() =>
|
||||
expect(sb.getOfficialSheetAdmin).toHaveBeenCalledTimes(2),
|
||||
)
|
||||
await Promise.resolve()
|
||||
expect(store.getState().lastSave).toEqual({ revision: 5, savedAt: null })
|
||||
})
|
||||
|
||||
it('drops a late answer after another save', async () => {
|
||||
await savedOnce()
|
||||
const late = deferred<never>()
|
||||
sb.getOfficialSheetAdmin.mockReturnValueOnce(late.promise)
|
||||
await store.getState().save()
|
||||
commitMove(41)
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 5 } as never)
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(adminAt(5))
|
||||
await store.getState().save()
|
||||
await vi.waitFor(() =>
|
||||
expect(store.getState().lastSave?.savedAt).toBe(SAVED_AT),
|
||||
)
|
||||
late.resolve(adminAt(4))
|
||||
await Promise.resolve()
|
||||
expect(store.getState().lastSave).toEqual({
|
||||
revision: 5,
|
||||
savedAt: SAVED_AT,
|
||||
})
|
||||
})
|
||||
|
||||
it('drops a late answer after a reload', async () => {
|
||||
await savedOnce()
|
||||
const late = deferred<never>()
|
||||
sb.getOfficialSheetAdmin.mockReturnValue(late.promise)
|
||||
await store.getState().save()
|
||||
await store.getState().reloadServerDraft({ confirmed: true })
|
||||
expect(store.getState().lastSave).toBeNull()
|
||||
late.resolve(adminAt(4))
|
||||
await Promise.resolve()
|
||||
expect(store.getState().lastSave).toBeNull()
|
||||
})
|
||||
|
||||
it('leaves the time unknown on a refusal, a failure or garbage', async () => {
|
||||
await savedOnce()
|
||||
sb.getOfficialSheetAdmin.mockRejectedValue(new SheetFailure('forbidden'))
|
||||
await store.getState().save()
|
||||
await vi.waitFor(() =>
|
||||
expect(sb.getOfficialSheetAdmin).toHaveBeenCalledTimes(1),
|
||||
)
|
||||
expect(store.getState().lastSave).toEqual({ revision: 4, savedAt: null })
|
||||
commitMove(41)
|
||||
sb.saveSheetDraft.mockResolvedValue({ revision: 5 } as never)
|
||||
sb.getOfficialSheetAdmin.mockResolvedValue(
|
||||
adminAnswer({
|
||||
draft_version: { version_id: VERSION, version_number: 4, revision: 5 },
|
||||
draft_last_save: {
|
||||
revision: 5,
|
||||
saved_at: 'not a date',
|
||||
saved_by: null,
|
||||
},
|
||||
}) as never,
|
||||
)
|
||||
await store.getState().save()
|
||||
await vi.waitFor(() =>
|
||||
expect(sb.getOfficialSheetAdmin).toHaveBeenCalledTimes(2),
|
||||
)
|
||||
await Promise.resolve()
|
||||
expect(store.getState().lastSave).toEqual({ revision: 5, savedAt: null })
|
||||
expect(store.getState().status).toBe('ready')
|
||||
})
|
||||
|
||||
it('is not called on draft.unchanged nor on a failed save', async () => {
|
||||
await openReady({ revision: 3 })
|
||||
commitMove(40)
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft.unchanged'))
|
||||
await store.getState().save()
|
||||
expect(store.getState().lastSave).toEqual({ revision: 3, savedAt: null })
|
||||
sb.saveSheetDraft.mockRejectedValue(new SheetFailure('draft.frozen'))
|
||||
commitMove(41)
|
||||
await store.getState().save()
|
||||
await Promise.resolve()
|
||||
expect(sb.getOfficialSheetAdmin).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('never reads the browser clock', () => {
|
||||
expect(saveLineSource).not.toMatch(/Date\.now|new Date/)
|
||||
const read = saveSource.slice(saveSource.indexOf('function savedTimeOf'))
|
||||
expect(read.slice(0, read.indexOf('interface Attempt'))).not.toMatch(
|
||||
/Date\.now|new Date/,
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user