vpp-ai-platform/packages/domain/test/fixtures.test.ts

42 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

import { readdirSync, readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { schemaRegistry } from '../src/index.js'
const root = fileURLToPath(new URL('../../../contracts/', import.meta.url))
const casesIn = (dir: string): Array<[string, string, string]> => {
const out: Array<[string, string, string]> = []
for (const name of readdirSync(`${root}${dir}`)) {
for (const file of readdirSync(`${root}${dir}/${name}`)) {
out.push([name, file, `${root}${dir}/${name}/${file}`])
}
}
return out
}
describe('golden fixtures validate against domain schemas', () => {
it.each(casesIn('fixtures'))('%s / %s', (name, _file, path) => {
const schema = schemaRegistry[name]
expect(schema, `no schema registered for fixture dir '${name}'`).toBeDefined()
const data = JSON.parse(readFileSync(path, 'utf8'))
const result = schema!.safeParse(data)
expect(result.success, JSON.stringify(result.error?.issues, null, 2)).toBe(true)
})
})
describe('invalid fixtures are rejected', () => {
it.each(casesIn('fixtures-invalid'))('%s / %s', (name, _file, path) => {
const schema = schemaRegistry[name]
expect(schema).toBeDefined()
const data = JSON.parse(readFileSync(path, 'utf8'))
expect(schema!.safeParse(data).success).toBe(false)
})
})
describe('every registered schema has at least one golden fixture', () => {
it.each(Object.keys(schemaRegistry))('%s', (name) => {
expect(readdirSync(`${root}fixtures/${name}`).length).toBeGreaterThan(0)
})
})