Routes /app/animals, new, :animalId, edit under one workspace gate; list with species filter; form (D-R7, U3 birth, no default sex or role, optional first client); page with current and former relations, end, dates, void; client page links its animals, adds one pre-associated, links an existing one. Card now passes aria-labelledby, which it silently dropped.
350 lines
11 KiB
TypeScript
350 lines
11 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import type { AnimalRelationItem } from '@faunosteo/supabase'
|
|
import { animalAge } from '@/animals/age'
|
|
import {
|
|
EMPTY_ANIMAL_VALUES,
|
|
animalFieldErrors,
|
|
animalSchema,
|
|
recordDateIssue,
|
|
relationIssues,
|
|
withBirthDate,
|
|
type AnimalFormValues,
|
|
} from '@/animals/validation'
|
|
import { groupRelations } from '@/relations/groups'
|
|
|
|
const TODAY = '2026-10-26'
|
|
const SPECIES = ['equine', 'canine', 'other']
|
|
const BASE: AnimalFormValues = {
|
|
...EMPTY_ANIMAL_VALUES,
|
|
name: 'Tornado',
|
|
species_id: 'equine',
|
|
sex: 'female',
|
|
}
|
|
|
|
const parse = (patch: Partial<AnimalFormValues>, today = TODAY) =>
|
|
animalSchema({ speciesIds: SPECIES, today }).safeParse({ ...BASE, ...patch })
|
|
const errorsOf = (patch: Partial<AnimalFormValues>, today = TODAY) => {
|
|
const result = parse(patch, today)
|
|
return result.success ? {} : animalFieldErrors(result.error)
|
|
}
|
|
|
|
describe('animal form schema (D-R7 on field-limits)', () => {
|
|
it('needs name, species and sex; empty optionals become null; name trimmed', () => {
|
|
const result = parse({ name: ' Tornado ' })
|
|
expect(result.success && result.data).toEqual({
|
|
name: 'Tornado',
|
|
species_id: 'equine',
|
|
species_detail: null,
|
|
sex: 'female',
|
|
breed: null,
|
|
birth_year: null,
|
|
birth_date: null,
|
|
})
|
|
})
|
|
|
|
it('refuses an empty form field by field, sex included (no default)', () => {
|
|
expect(
|
|
animalSchema({ speciesIds: SPECIES, today: TODAY }).safeParse(
|
|
EMPTY_ANIMAL_VALUES,
|
|
).success,
|
|
).toBe(false)
|
|
const result = animalSchema({
|
|
speciesIds: SPECIES,
|
|
today: TODAY,
|
|
}).safeParse(EMPTY_ANIMAL_VALUES)
|
|
expect(result.success ? {} : animalFieldErrors(result.error)).toEqual({
|
|
name: 'animals.error.required',
|
|
species_id: 'animals.error.species',
|
|
sex: 'animals.error.sex',
|
|
})
|
|
})
|
|
|
|
it('counts name code points: 80 accepted, 81 refused, astral counts once', () => {
|
|
expect(errorsOf({ name: 'é'.repeat(80) })).toEqual({})
|
|
expect(errorsOf({ name: '𝄞'.repeat(80) })).toEqual({})
|
|
expect(errorsOf({ name: 'é'.repeat(81) })).toEqual({
|
|
name: 'animals.error.too_long|80',
|
|
})
|
|
})
|
|
|
|
it('refuses control characters in the name and the breed', () => {
|
|
expect(errorsOf({ name: 'Tor\tnado' })).toEqual({
|
|
name: 'animals.error.characters',
|
|
})
|
|
expect(errorsOf({ breed: 'Pur\nsang' })).toEqual({
|
|
breed: 'animals.error.characters',
|
|
})
|
|
})
|
|
|
|
it('bounds the breed to 80', () => {
|
|
expect(errorsOf({ breed: 'a'.repeat(80) })).toEqual({})
|
|
expect(errorsOf({ breed: 'a'.repeat(81) })).toEqual({
|
|
breed: 'animals.error.too_long|80',
|
|
})
|
|
})
|
|
|
|
it('accepts only the allowed species', () => {
|
|
expect(errorsOf({ species_id: 'dragon' })).toEqual({
|
|
species_id: 'animals.error.species',
|
|
})
|
|
})
|
|
|
|
it('« other » needs a detail; any other species drops a leftover detail', () => {
|
|
expect(errorsOf({ species_id: 'other' })).toEqual({
|
|
species_detail: 'animals.error.species_detail',
|
|
})
|
|
expect(
|
|
errorsOf({ species_id: 'other', species_detail: 'f'.repeat(81) }),
|
|
).toEqual({
|
|
species_detail: 'animals.error.too_long|80',
|
|
})
|
|
const ferret = parse({ species_id: 'other', species_detail: ' Furet ' })
|
|
expect(ferret.success && ferret.data.species_detail).toBe('Furet')
|
|
const horse = parse({ species_id: 'equine', species_detail: 'Furet' })
|
|
expect(horse.success && horse.data.species_detail).toBeNull()
|
|
})
|
|
|
|
it('takes the three sexes, « inconnu » included', () => {
|
|
for (const sex of ['male', 'female', 'unknown'] as const)
|
|
expect(errorsOf({ sex })).toEqual({})
|
|
expect(errorsOf({ sex: 'x' as never })).toEqual({
|
|
sex: 'animals.error.sex',
|
|
})
|
|
})
|
|
|
|
it('keeps the five birth states of D6b', () => {
|
|
const none = parse({})
|
|
expect(
|
|
none.success && [none.data.birth_year, none.data.birth_date],
|
|
).toEqual([null, null])
|
|
const year = parse({ birth_year: '2015' })
|
|
expect(
|
|
year.success && [year.data.birth_year, year.data.birth_date],
|
|
).toEqual([2015, null])
|
|
const full = parse({ birth_year: '2015', birth_date: '2015-04-12' })
|
|
expect(
|
|
full.success && [full.data.birth_year, full.data.birth_date],
|
|
).toEqual([2015, '2015-04-12'])
|
|
expect(errorsOf({ birth_year: '2016', birth_date: '2015-04-12' })).toEqual({
|
|
birth_year: 'animals.error.date_year',
|
|
})
|
|
expect(errorsOf({ birth_date: '2015-04-12' })).toEqual({
|
|
birth_year: 'animals.error.date_year',
|
|
})
|
|
})
|
|
|
|
it('refuses malformed years and dates', () => {
|
|
expect(errorsOf({ birth_year: '15' })).toEqual({
|
|
birth_year: 'animals.error.year_format',
|
|
})
|
|
expect(errorsOf({ birth_year: '20a5' })).toEqual({
|
|
birth_year: 'animals.error.year_format',
|
|
})
|
|
expect(errorsOf({ birth_year: '2015', birth_date: '2015-02-30' })).toEqual({
|
|
birth_date: 'animals.error.date_format',
|
|
})
|
|
expect(errorsOf({ birth_year: '2015', birth_date: '12/04/2015' })).toEqual({
|
|
birth_date: 'animals.error.date_format',
|
|
})
|
|
})
|
|
|
|
it('bounds the year from 1900 to the current local year', () => {
|
|
expect(errorsOf({ birth_year: '1900' })).toEqual({})
|
|
expect(errorsOf({ birth_year: '1899' })).toEqual({
|
|
birth_year: 'animals.error.year_range',
|
|
})
|
|
expect(errorsOf({ birth_year: '2026' })).toEqual({})
|
|
expect(errorsOf({ birth_year: '2027' })).toEqual({
|
|
birth_year: 'animals.error.year_range',
|
|
})
|
|
})
|
|
|
|
it('refuses a date in the future or before 1900', () => {
|
|
expect(errorsOf({ birth_year: '2026', birth_date: TODAY })).toEqual({})
|
|
expect(errorsOf({ birth_year: '2026', birth_date: '2026-10-27' })).toEqual({
|
|
birth_date: 'animals.error.date_range',
|
|
})
|
|
expect(
|
|
errorsOf({ birth_year: '1899', birth_date: '1899-12-31' }),
|
|
).toMatchObject({
|
|
birth_date: 'animals.error.date_range',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('withBirthDate (U3)', () => {
|
|
it('fills the year from a complete date', () => {
|
|
expect(
|
|
withBirthDate({ ...BASE, birth_year: '2010' }, '2015-04-12'),
|
|
).toMatchObject({
|
|
birth_year: '2015',
|
|
birth_date: '2015-04-12',
|
|
})
|
|
})
|
|
it('keeps the year when the date is cleared or incomplete', () => {
|
|
const filled = withBirthDate(BASE, '2015-04-12')
|
|
expect(withBirthDate(filled, '')).toMatchObject({
|
|
birth_year: '2015',
|
|
birth_date: '',
|
|
})
|
|
expect(
|
|
withBirthDate({ ...BASE, birth_year: '2012' }, '2015-04'),
|
|
).toMatchObject({ birth_year: '2012' })
|
|
})
|
|
})
|
|
|
|
describe('recordDateIssue', () => {
|
|
it('accepts empty, a real date up to today; refuses the rest', () => {
|
|
expect(recordDateIssue('', TODAY)).toBeNull()
|
|
expect(recordDateIssue('1900-01-01', TODAY)).toBeNull()
|
|
expect(recordDateIssue(TODAY, TODAY)).toBeNull()
|
|
expect(recordDateIssue('1899-12-31', TODAY)).toBe(
|
|
'animals.error.date_range',
|
|
)
|
|
expect(recordDateIssue('2026-10-27', TODAY)).toBe(
|
|
'animals.error.date_range',
|
|
)
|
|
expect(recordDateIssue('2024-02-30', TODAY)).toBe(
|
|
'animals.error.date_format',
|
|
)
|
|
expect(recordDateIssue('2024-02-29', TODAY)).toBeNull()
|
|
expect(recordDateIssue('soon', TODAY)).toBe('animals.error.date_format')
|
|
})
|
|
})
|
|
|
|
describe('relationIssues', () => {
|
|
const client = { id: 'c1', display_name: 'Marie' }
|
|
it('no client: nothing to check, unless the client is the point of the form', () => {
|
|
expect(
|
|
relationIssues(
|
|
{ client: null, relation_role: '', started_on: '' },
|
|
TODAY,
|
|
false,
|
|
),
|
|
).toEqual({})
|
|
expect(
|
|
relationIssues(
|
|
{ client: null, relation_role: '', started_on: '' },
|
|
TODAY,
|
|
true,
|
|
),
|
|
).toEqual({
|
|
client: 'animals.error.client',
|
|
})
|
|
})
|
|
it('a chosen client needs a role; the start date follows the record date rules', () => {
|
|
expect(
|
|
relationIssues(
|
|
{ client, relation_role: '', started_on: '' },
|
|
TODAY,
|
|
false,
|
|
),
|
|
).toEqual({
|
|
relation_role: 'animals.error.role',
|
|
})
|
|
expect(
|
|
relationIssues(
|
|
{ client, relation_role: 'owner', started_on: '' },
|
|
TODAY,
|
|
true,
|
|
),
|
|
).toEqual({})
|
|
expect(
|
|
relationIssues(
|
|
{ client, relation_role: 'keeper', started_on: '2026-10-27' },
|
|
TODAY,
|
|
true,
|
|
),
|
|
).toEqual({
|
|
started_on: 'animals.error.date_range',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('animalAge (display only)', () => {
|
|
it('from a full date: months under a year, whole years after', () => {
|
|
expect(animalAge(2026, '2026-04-27', TODAY)).toEqual({
|
|
unit: 'month',
|
|
count: 5,
|
|
about: false,
|
|
})
|
|
expect(animalAge(2025, '2025-10-26', TODAY)).toEqual({
|
|
unit: 'year',
|
|
count: 1,
|
|
about: false,
|
|
})
|
|
expect(animalAge(2015, '2015-10-27', TODAY)).toEqual({
|
|
unit: 'year',
|
|
count: 10,
|
|
about: false,
|
|
})
|
|
})
|
|
it('from a year only: about N years, 0 for this year', () => {
|
|
expect(animalAge(2015, null, TODAY)).toEqual({
|
|
unit: 'year',
|
|
count: 11,
|
|
about: true,
|
|
})
|
|
expect(animalAge(2026, null, TODAY)).toEqual({
|
|
unit: 'year',
|
|
count: 0,
|
|
about: true,
|
|
})
|
|
})
|
|
it('nothing for unknown or future births', () => {
|
|
expect(animalAge(null, null, TODAY)).toBeNull()
|
|
expect(animalAge(2027, null, TODAY)).toBeNull()
|
|
expect(animalAge(2026, '2026-11-01', TODAY)).toBeNull()
|
|
})
|
|
})
|
|
|
|
function relation(
|
|
id: string,
|
|
patch: Partial<AnimalRelationItem> & { name?: string },
|
|
): AnimalRelationItem {
|
|
const { name = 'Client', ...rest } = patch
|
|
return {
|
|
id,
|
|
relation_role: 'owner',
|
|
started_on: null,
|
|
ended_on: null,
|
|
ended_at: null,
|
|
revision: 1,
|
|
client: {
|
|
id: `c-${id}`,
|
|
kind: 'person',
|
|
display_name: name,
|
|
phone: null,
|
|
email: null,
|
|
archived_at: null,
|
|
},
|
|
...rest,
|
|
}
|
|
}
|
|
|
|
describe('groupRelations', () => {
|
|
it('current by role then name, former most recently ended first', () => {
|
|
const groups = groupRelations([
|
|
relation('1', { relation_role: 'contact', name: 'Alice' }),
|
|
relation('2', { relation_role: 'owner', name: 'Zoé' }),
|
|
relation('3', { relation_role: 'keeper', name: 'Bob' }),
|
|
relation('4', { relation_role: 'owner', name: 'Anne' }),
|
|
relation('5', { name: 'Old', ended_at: '2020-01-01T00:00:00Z' }),
|
|
relation('6', { name: 'Recent', ended_at: '2024-01-01T00:00:00Z' }),
|
|
])
|
|
expect(groups.current.map((r) => r.id)).toEqual(['4', '2', '3', '1'])
|
|
expect(groups.former.map((r) => r.id)).toEqual(['6', '5'])
|
|
expect(groups.hasOwner).toBe(true)
|
|
})
|
|
it('no current owner with only keepers or former owners', () => {
|
|
expect(
|
|
groupRelations([relation('1', { relation_role: 'keeper' })]).hasOwner,
|
|
).toBe(false)
|
|
expect(
|
|
groupRelations([relation('1', { ended_at: '2024-01-01T00:00:00Z' })])
|
|
.hasOwner,
|
|
).toBe(false)
|
|
expect(groupRelations([]).hasOwner).toBe(false)
|
|
})
|
|
})
|