feat(editor): a reference made in the citations draft saves with it

This commit is contained in:
bchanot committed 2026-11-15 19:55:41 +01:00
1 parent 8c3fcca0b9
commit 70eb2fcbb4
15 files changed
+671 -162

No files matched your search

@@ -1,12 +1,24 @@
/** Rev 5 Task 12: the citations draft, held as additions and removals. */
import { describe, expect, it } from 'vitest'
import type { Reference } from '@faunosteo/core-wasm/types'
import {
addId,
applyEdits,
citationCommands,
NO_EDITS,
putCreated,
removeId,
sameEdits,
} from '@/editor/references/citationEdits'
const value = (id: string, citation: string): Reference => ({
id,
type: 'book',
citation,
doi: null,
url: null,
})
describe('citation edits', () => {
it('applies removals and additions to the current list, each id once', () => {
const edits = addId(removeId(NO_EDITS, 'a'), 'b')
@@ -26,4 +38,40 @@ describe('citation edits', () => {
const gone = removeId(NO_EDITS, 'a')
expect(removeId(gone, 'a')).toBe(gone)
})
it('a created reference joins the draft with its id; a second put replaces it', () => {
const once = putCreated(NO_EDITS, value('ref-n', 'Neuf'))
expect(once).toEqual({
added: ['ref-n'],
removed: [],
created: [value('ref-n', 'Neuf')],
})
const twice = putCreated(once, value('ref-n', 'Neuf 2'))
expect(twice.created).toEqual([value('ref-n', 'Neuf 2')])
expect(twice.added).toEqual(['ref-n'])
expect(sameEdits(once, twice)).toBe(false)
})
it('removing a created id drops it entirely', () => {
const edits = putCreated(addId(NO_EDITS, 'b'), value('ref-n', 'Neuf'))
expect(removeId(edits, 'ref-n')).toEqual(addId(NO_EDITS, 'b'))
})
it('the save: an insert per created reference under a minted id, then the update', () => {
const edits = putCreated(removeId(NO_EDITS, 'a'), value('ref-n', 'Neuf'))
const minted = ['ref-m']
const commands = citationCommands(
edits,
['a', 'c'],
(ids) => ({ op: 'remove', key: { kind: 'reference', id: ids.join() } }),
() => minted.shift()!,
)
expect(commands).toEqual([
{
op: 'insert',
element: { kind: 'reference', value: value('ref-m', 'Neuf') },
},
{ op: 'remove', key: { kind: 'reference', id: 'c,ref-m' } },
])
})
})
@@ -44,6 +44,17 @@ vi.mock('@faunosteo/supabase', async (orig) => ({
submitSheetReview: vi.fn(),
}))
/** Ids handed out before the real generator: a clash at the save (Rev 6). */
const minted = vi.hoisted((): string[] => [])
vi.mock('@/editor/ids', async (orig) => {
const real = await orig<typeof import('@/editor/ids')>()
return {
...real,
newElementId: (prefix: Parameters<typeof real.newElementId>[0]) =>
minted.shift() ?? real.newElementId(prefix),
}
})
let navigate: NavigateFunction
let location: Location
const probe = (n: NavigateFunction, l: Location) => {
@@ -59,6 +70,7 @@ beforeEach(() => {
TAG = resolveSessionTag(USER)
})
afterEach(() => {
minted.length = 0
cleanup()
resetStore()
setWidth(1024)
@@ -197,6 +209,24 @@ async function cite(user: User, id: string) {
await user.click(button('Citer'))
}
/** The reference dialog over the edit mode: type and citation, then « Ajouter la référence ». */
async function createPending(user: User, citation: string) {
await waitFor(() => expect(referenceDialog()).toBeDefined())
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), citation)
await user.click(
inReference().getByRole('button', { name: 'Ajouter la référence' }),
)
await waitFor(() => expect(referenceDialog()).toBeUndefined())
}
const pendingRow = () =>
[...citations()!.querySelectorAll<HTMLElement>('[data-citation-row]')][2]
const status = () =>
citations()!.querySelector('[data-place="citations"] > [role="status"]')!
.textContent
const live = () =>
document.querySelector('p.sr-only[aria-live="polite"]')!.textContent ?? ''
describe('the « Modifier » of the references dialog', () => {
it('edit mode: no history entry, no « Tout afficher », no « Aller à »', async () => {
const user = await mount()
@@ -391,9 +421,6 @@ describe('« Citer » (Rev 6)', () => {
})
})
const live = () =>
document.querySelector('p.sr-only[aria-live="polite"]')!.textContent ?? ''
describe('leaving the edit mode with changes', () => {
it('an explicit « Abandonner » after ✕ closes the dialog without a word', async () => {
const user = await mount()
@@ -509,7 +536,7 @@ describe('the reference dialog over the edit mode', () => {
expect(rows()).toEqual(['ref-a'])
})
it('« Nouvelle référence » creates it and cites it in the draft only', async () => {
it('« Nouvelle référence » cites it in the draft only; the library is unchanged', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
@@ -518,40 +545,163 @@ describe('the reference dialog over the edit mode', () => {
'aria-selected',
'true',
)
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), 'Neuf')
await user.click(
inReference().getByRole('button', { name: 'Ajouter la référence' }),
await createPending(user, 'Neuf')
expect(library()).not.toContain('Neuf')
expect(rows()).toHaveLength(3)
expect(rows().slice(0, 2)).toEqual(['ref-a', 'ref-c'])
const row = pendingRow()
expect(within(row).getByText('Neuf')).toBeVisible()
expect(within(row).getByText('Nouvelle')).toBeVisible()
expect(status()).toBe(
'Nouvelle référence citée dans le brouillon : Neuf. ' +
'Elle sera enregistrée avec les citations.',
)
await waitFor(() => expect(referenceDialog()).toBeUndefined())
expect(library()).toContain('Neuf')
expect(live()).not.toContain('sera enregistrée')
expect(undos()).toBe(0)
})
it('« Enregistrer » writes the reference and the citations as ONE undo step', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
await user.click(button('Enregistrer'))
await waitFor(() => expect(button('Modifier')).toHaveFocus())
const id = doc().references.find(
(r: { citation: string }) => r.citation === 'Neuf',
).id
expect(rows()).toEqual(['ref-a', 'ref-c', id])
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
await user.click(button('Enregistrer'))
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c', id])
expect(undos()).toBe(2)
expect(undos()).toBe(1)
act(() => {
store.getState().undoStep()
})
expect(library()).not.toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
act(() => {
store.getState().redoStep()
})
expect(library()).toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c', id])
})
it('a reference created, then the draft abandoned, stays in the library uncited', async () => {
it('created, then the draft abandoned: library unchanged, nothing said', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
await user.click(button('Annuler'))
await user.click(button('Abandonner'))
expect(library()).not.toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
expect(undos()).toBe(0)
await user.click(button('Fermer'))
await waitFor(() => expect(citations()).toBeUndefined())
await user.click(inFindingDialog().getByRole('button', { name: 'Fermer' }))
await waitFor(() => expect(findingDialog()).toBeNull())
await act(async () => undefined)
expect(live()).not.toContain('sera enregistrée')
})
it('a pending reference is modified, prefilled; nothing is written', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
const pendingId = rows()[2]
await user.click(button(/^Modifier la référence Neuf/))
await waitFor(() => expect(referenceDialog()).toBeDefined())
expect(
inReference().getByRole('heading', { name: 'Nouvelle référence' }),
).toBeVisible()
const citation = inReference().getByLabelText('Citation')
expect(citation).toHaveValue('Neuf')
await user.type(citation, ' 2')
await user.click(inReference().getByRole('button', { name: 'Valider' }))
await waitFor(() => expect(referenceDialog()).toBeUndefined())
expect(rows()).toEqual(['ref-a', 'ref-c', pendingId])
expect(within(pendingRow()).getByText('Neuf 2')).toBeVisible()
expect(library()).not.toContain('Neuf 2')
await user.click(button('Enregistrer'))
expect(library().filter((c) => c.startsWith('Neuf'))).toEqual(['Neuf 2'])
expect(undos()).toBe(1)
})
it('« Retirer » drops a pending reference entirely and says so in the dialog', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
await removeRow(user, /Neuf/)
expect(rows()).toEqual(['ref-a', 'ref-c'])
expect(status()).toBe('Nouvelle référence retirée : Neuf')
await user.click(button('Annuler'))
await waitFor(() => expect(button('Modifier')).toHaveFocus())
expect(library()).not.toContain('Neuf')
expect(undos()).toBe(0)
})
it('a double submit of the creation makes one pending row', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await waitFor(() => expect(referenceDialog()).toBeDefined())
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), 'Neuf')
const form = referenceDialog()!.querySelector('form')!
act(() => {
fireEvent.submit(form)
fireEvent.submit(form)
})
await waitFor(() => expect(referenceDialog()).toBeUndefined())
expect(rows()).toHaveLength(3)
})
it('the creation is dry-run: a refusal shows in its form, nothing cited', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await waitFor(() => expect(referenceDialog()).toBeDefined())
await user.selectOptions(inReference().getByLabelText('Type'), 'book')
await user.type(inReference().getByLabelText('Citation'), 'Neuf')
minted.push('ref-a')
await user.click(
inReference().getByRole('button', { name: 'Ajouter la référence' }),
)
await waitFor(() => expect(referenceDialog()).toBeUndefined())
await user.click(button('Annuler'))
await user.click(button('Abandonner'))
expect(library()).toContain('Neuf')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c'])
expect(inReference().getByRole('alert')).toBeVisible()
expect(referenceDialog()).toBeDefined()
expect(rows()).toEqual(['ref-a', 'ref-c'])
expect(undos()).toBe(0)
})
it('a clashing id at the save is minted again once', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
minted.push('ref-a')
await user.click(button('Enregistrer'))
await waitFor(() => expect(button('Modifier')).toHaveFocus())
const added = doc().references.find(
(r: { citation: string }) => r.citation === 'Neuf',
)
expect(added.id).not.toBe('ref-a')
expect(nodeA().reference_ids).toEqual(['ref-a', 'ref-c', added.id])
expect(undos()).toBe(1)
})
it('a save refused twice keeps the draft and names the pending reference', async () => {
const user = await mount()
await editCitations(user)
await user.click(button('Nouvelle référence'))
await createPending(user, 'Neuf')
minted.push('ref-a', 'ref-c')
await user.click(button('Enregistrer'))
const alert = inCitations().getByRole('alert')
expect(alert).toHaveTextContent(/Neuf/)
expect(rows()).toHaveLength(3)
expect(library()).not.toContain('Neuf')
expect(undos()).toBe(0)
})
})
describe('a finding draft and a references draft on the same element', () => {
@@ -152,55 +152,62 @@ describe('closeCitations', () => {
})
})
describe('a creation started over the citations dialog', () => {
it('hands its id to the form that started it; no « added » notice', async () => {
describe('a creation started over the citations dialog (Rev 6: a draft creation)', () => {
const pending = {
id: 'ref-p',
type: 'book',
citation: 'Neuf',
doi: null,
url: null,
} as const
it('holds its citing form and the pending value; created() never cites', async () => {
cited()
const cite = vi.fn()
act(() => nav.references.start(null, cite))
act(() => nav.references.start(null, cite, pending))
expect(nav.state).toMatchObject({
tab: 'findings',
reference: { create: true },
})
await act(async () => nav.references.created('ref-new'))
expect(cite).toHaveBeenCalledWith('ref-new')
expect(nav.references.added).toBeNull()
expect(nav.references.drafting()).toEqual({ initial: pending, cite })
act(() => nav.references.close(true))
expect(nav.references.drafting()).toBeNull()
expect(nav.state.reference).toBeNull()
expect(nav.state.finding).toMatchObject({ citations: true })
expect(cite).not.toHaveBeenCalled()
expect(nav.references.added).toBeNull()
})
it('a creation closed or replaced forgets its citing form', async () => {
it('a new one has no initial value', () => {
cited()
const cite = vi.fn()
act(() => nav.references.start(null, cite))
act(() => nav.references.close(true))
await act(async () => nav.references.created('ref-x'))
expect(cite).not.toHaveBeenCalled()
act(() => nav.references.start(null, cite))
expect(nav.references.drafting()).toEqual({ initial: null, cite })
})
it('a creation replaced forgets its citing form', () => {
cited()
act(() => nav.references.start(null, vi.fn(), pending))
act(() => nav.references.replace(null))
await act(async () => nav.references.created('ref-y'))
expect(cite).not.toHaveBeenCalled()
expect(nav.references.drafting()).toBeNull()
})
it('a Back that removes the creation dialog forgets its citing form', async () => {
it('a Back that removes the creation dialog forgets its citing form', () => {
cited()
const cite = vi.fn()
act(() => nav.references.start(null, cite))
act(() => nav.references.start(null, vi.fn(), pending))
act(() => navigate(-1))
expect(nav.state.reference).toBeNull()
await act(async () => nav.references.created('ref-z'))
expect(cite).not.toHaveBeenCalled()
expect(nav.references.added).toMatchObject({ id: 'ref-z' })
expect(nav.references.drafting()).toBeNull()
})
it('a later creation from the library sets the notice, cites nothing', async () => {
it('a creation from the library is no draft; created() sets the notice', async () => {
cited()
const cite = vi.fn()
act(() => nav.references.start(null, cite))
act(() => nav.references.start(null, vi.fn()))
act(() => nav.references.close(true))
act(() => nav.selectTab('references'))
act(() => nav.references.start(null))
expect(nav.references.drafting()).toBeNull()
await act(async () => nav.references.created('ref-lib'))
expect(cite).not.toHaveBeenCalled()
expect(nav.references.added).toMatchObject({ id: 'ref-lib' })
})
})