Files
faunosteo/packages/anatomy/tests/curation.schema.test.ts
T

86 lines
2.6 KiB
TypeScript

import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import {
ChangesetSchema,
EQUINE_CURATION_DIR,
ReferencesFileSchema,
needsConfirmation,
} from '../src'
import { REPO_ROOT } from './helpers'
async function committed(file: string): Promise<unknown> {
return JSON.parse(
await readFile(join(REPO_ROOT, EQUINE_CURATION_DIR, file), 'utf8'),
)
}
const CHANGE = {
id: 'CHG-002',
level: 'B',
kind: 'terminology',
title: { fr: 'Titre', en: 'Title' },
rationale: 'Raison',
reference_ids: [],
ops: [{ op: 'remove', target: 'zone:x' }],
}
describe('curation schemas', () => {
it('parse the committed curation files', async () => {
const changeset = ChangesetSchema.parse(await committed('changes.json'))
const ids = changeset.changes.map((c) => c.id)
expect(ids[0]).toBe('CHG-001')
expect(ids).toEqual([...ids].sort())
expect(new Set(ids).size).toBe(ids.length)
expect(changeset.changes.every((c) => c.level !== 'C' || c.question)).toBe(
true,
)
const refs = ReferencesFileSchema.parse(await committed('references.json'))
const refIds = refs.references.map((r) => r.id)
expect(refIds).toContain('legacy-catalog')
expect(new Set(refIds).size).toBe(refIds.length)
const cited = new Set(changeset.changes.flatMap((c) => c.reference_ids))
for (const id of cited) expect(refIds).toContain(id)
})
it.each([
[
'an unknown op',
{ ...CHANGE, ops: [{ op: 'replace', target: 'zone:x' }] },
],
['a level C change without question', { ...CHANGE, level: 'C' }],
['a bad change id', { ...CHANGE, id: 'CHANGE-1' }],
['a change without ops', { ...CHANGE, ops: [] }],
['an unknown key', { ...CHANGE, needs_confirmation: true }],
])('rejects %s', (_label, change) => {
expect(ChangesetSchema.safeParse({ changes: [change] }).success).toBe(false)
})
it('accepts a level C change with its question', () => {
const change = {
...CHANGE,
level: 'C',
question: 'Faut-il ?',
priority: 'high',
}
expect(ChangesetSchema.safeParse({ changes: [change] }).success).toBe(true)
})
it('rejects duplicate change ids and duplicate reference ids', () => {
expect(
ChangesetSchema.safeParse({ changes: [CHANGE, CHANGE] }).success,
).toBe(false)
const ref = { id: 'a', type: 'web', citation: 'A', url: null, doi: null }
expect(
ReferencesFileSchema.safeParse({ references: [ref, ref] }).success,
).toBe(false)
})
it('derives needs_confirmation from the level', () => {
expect(['A', 'B', 'C'].map((l) => needsConfirmation(l as 'A'))).toEqual([
false,
true,
true,
])
})
})