/** 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 = { 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(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']) }) })