Files
faunosteo/apps/app/tests/unit/editor-reference-sort.test.ts
T
bchanot 3baac4dbb5 feat(editor): search, sort, count and add in the Références tab
Search (citation, DOI, URL) with « Effacer la recherche » and Escape;
« Trier par » sheet order, citation (locale collator), citation count,
type (business order); every sort stable on stored order, display only:
document, stored order, ids and history unchanged (unit + e2e). Count
« {n} références » / « {n} sur {total} » counts matching references
only. « Monter » / « Descendre » only in sheet order without search
(A1). « Ajouter une référence » resets search and sort and appends a
draft tile in edit; a tile in edit stays pinned while sorted or
searched; tile state pruned of absent ids. Reset path ready for « Tout
afficher » (R2.4).

Gates: typecheck, lint, test:run (app 1452), build, e2e:ui 251;
e2e:shell 16 specs on db:sandbox (editor-equine never listed): 115
passed / 102 skipped / 2 failed: editor-structures (test-side, fixed,
rerun with editor-references 12 passed / 11 skipped) and admin.spec A0
« Target crashed » during axe on the rights screen (not R2: that
screen renders every grant, 1 282 accumulated e2e fixtures; reported as
an open risk); full-row guard: no pre-existing row changed in any table.

## R-24 (removed or rewritten assertions)
| file | old | new |
|---|---|---|
| editor-references-tab.test.tsx | `#tab-reference-new-citation` present on load | null until « Ajouter une référence » |
| same | no match text « Aucune référence ne correspond. » | notice « Aucune référence ne contient « zzz ». » |
| same | add: form always visible, add clears the search, `getByText('Nouvelle source')` | click « Ajouter une référence » first; new tile header open, focused, search empty, 4 tiles |
| same | « a refused add keeps the search » | REMOVED (P24: add resets the search at once); replaced by « draft opens with citation focused, refused add keeps the draft, no write » |
| editor-tabs-lists.test.tsx | add form directly | click « Ajouter une référence » before the form assertions (same assertions after) |
| editor-keep-alive.test.tsx | form survives tab round trip | same, after opening the draft |
| editor-no-loss.test.tsx | reference form keeps typed text | same, after opening the draft |
| e2e editor-shell.spec (2), editor-shell-crossing.spec, editor-structures.spec | fill `#tab-reference-new-*` directly; `getByText(citation)` visible | open the draft first; structures asserts the new header is focused |
| e2e editor-references.spec keyboard walk | focus search, Tab reaches header 0 | focus « Ajouter une référence » (search, sort, add precede the headers) |
Other readers of `editor.reference.add`/new ids: grep of src, tests, e2e incl. editor-equine found none left unlisted. editor-equine is not affected ("updated, not run": nothing to update).

## i18n (fr / en)
Added: `editor.references.add` Ajouter une référence / Add a reference; `add_help` Type et citation obligatoires. / Type and citation required.; `clear_search` Effacer la recherche / Clear the search; `sort_by` Trier par / Sort by; `sort.sheet|citation|count|type` Ordre de la fiche, Citation (A → Z), Nombre de citations, Type / Sheet order, Citation (A → Z), Number of citations, Type; `count_one|other` {count} référence(s) / reference(s); `count_of_one|other` {count} référence(s) sur {total} / {count} reference(s) of {total}; `reorder_hint` Pour réorganiser, affichez l'ordre de la fiche sans recherche. / To reorder, show the sheet order without a search.; `no_result` Aucune référence ne contient « {q} ». / No reference contains “{q}”.; `no_result_scope` La recherche porte sur la citation, le DOI et l'URL. / The search covers the citation, the DOI and the URL. (REVUE sentence split in two keys so the first half is bold).
Changed: none. Removed: none (`editor.references.no_match` kept, the panel's EmptyLine still uses it).
2026-11-12 21:18:56 +01:00

148 lines
4.3 KiB
TypeScript

/** R2.3: the four display orders and the shown set are pure and never touch their inputs. */
import { describe, expect, it } from 'vitest'
import type { Reference, ReferenceType } from '@faunosteo/core-wasm/types'
import { sortReferenceIds } from '@/editor/references/sort'
import { shownPositions } from '@/editor/references/view'
const ref = (id: string, citation: string, type: ReferenceType): Reference => ({
id,
type,
citation,
url: null,
doi: null,
})
// Accents, case, numbers, equal citations and mixed types, in stored order r1..r5.
const refs: readonly Reference[] = [
ref('r1', 'Zèbre 10', 'web'),
ref('r2', 'zebre 9', 'journal'),
ref('r3', 'Étude', 'book'),
ref('r4', 'etude', 'book'),
ref('r5', 'Alpha', 'legal'),
]
const COUNTS: Record<string, number> = { r1: 1, r2: 2, r5: 1 }
const context = {
locale: 'fr' as const,
citedCount: (id: string) => COUNTS[id] ?? 0,
}
const all = [0, 1, 2, 3, 4]
const ids = (positions: number[]) => positions.map((at) => refs[at].id)
function permutations<T>(items: readonly T[]): T[][] {
if (items.length < 2) return [[...items]]
return items.flatMap((item, at) =>
permutations([...items.slice(0, at), ...items.slice(at + 1)]).map(
(rest) => [item, ...rest],
),
)
}
describe('sortReferenceIds', () => {
it('sheet keeps the stored order', () => {
expect(ids(sortReferenceIds(refs, all, 'sheet', context))).toEqual([
'r1',
'r2',
'r3',
'r4',
'r5',
])
})
it('citation: collator, accents and case ignored, numbers by value, ties stored', () => {
expect(ids(sortReferenceIds(refs, all, 'citation', context))).toEqual([
'r5',
'r3',
'r4',
'r2',
'r1',
])
})
it('count: most cited first, ties in sheet order', () => {
expect(ids(sortReferenceIds(refs, all, 'count', context))).toEqual([
'r2',
'r1',
'r5',
'r3',
'r4',
])
})
it('type: REFERENCE_TYPES order, ties in sheet order', () => {
expect(ids(sortReferenceIds(refs, all, 'type', context))).toEqual([
'r3',
'r4',
'r2',
'r5',
'r1',
])
})
it('a type outside the list sorts after the listed ones, stored order among them', () => {
const odd = [
ref('x1', 'a', 'unknown' as ReferenceType),
ref('x2', 'b', 'web'),
ref('x3', 'c', 'other' as ReferenceType),
]
expect(
sortReferenceIds(odd, [0, 1, 2], 'type', context).map((at) => odd[at].id),
).toEqual(['x2', 'x1', 'x3'])
})
it('every permutation of the input gives the same output', () => {
for (const sort of ['sheet', 'citation', 'count', 'type'] as const) {
const expected = sortReferenceIds(refs, all, sort, context)
for (const input of permutations(all)) {
expect(sortReferenceIds(refs, input, sort, context)).toEqual(expected)
}
}
})
it('returns a new array and leaves frozen inputs untouched', () => {
const frozen = Object.freeze([...refs.map((r) => Object.freeze({ ...r }))])
const positions = Object.freeze([4, 3, 2, 1, 0])
const out = sortReferenceIds(frozen, positions, 'citation', context)
expect(out).not.toBe(positions)
expect(positions).toEqual([4, 3, 2, 1, 0])
expect(frozen.map((r) => r.id)).toEqual(['r1', 'r2', 'r3', 'r4', 'r5'])
})
it('numbers in English too', () => {
const en = { ...context, locale: 'en' as const }
expect(ids(sortReferenceIds(refs, all, 'citation', en))[3]).toBe('r2')
})
})
describe('shownPositions', () => {
const view = { query: '', sort: 'citation' } as const
it('searches, then sorts', () => {
expect(
ids(
shownPositions(
refs,
{ query: 'ZEBRE', sort: 'citation' },
context,
null,
),
),
).toEqual(['r2', 'r1'])
})
it('puts the pinned tile back at its index, hidden by the search or not', () => {
const search = { query: 'etude', sort: 'citation' } as const
const pinned = shownPositions(refs, search, context, {
position: 0,
index: 1,
})
expect(ids(pinned)).toEqual(['r3', 'r1', 'r4'])
const clamped = shownPositions(refs, view, context, {
position: 0,
index: 99,
})
expect(ids(clamped)).toEqual(['r5', 'r3', 'r4', 'r2', 'r1'])
const first = shownPositions(refs, view, context, { position: 1, index: 0 })
expect(ids(first)).toEqual(['r2', 'r5', 'r3', 'r4', 'r1'])
})
})