|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { simplifyParameterSchema } from '@/rest/scripts/utils/parameter-schema' |
| 4 | + |
| 5 | +describe('simplifyParameterSchema', () => { |
| 6 | + test('keeps only type, default, and enum for a typed schema', () => { |
| 7 | + expect( |
| 8 | + simplifyParameterSchema({ |
| 9 | + type: 'string', |
| 10 | + default: 'open', |
| 11 | + enum: ['open', 'closed'], |
| 12 | + example: 'open', |
| 13 | + }), |
| 14 | + ).toEqual({ type: 'string', default: 'open', enum: ['open', 'closed'] }) |
| 15 | + }) |
| 16 | + |
| 17 | + test('derives the type from oneOf variants', () => { |
| 18 | + expect( |
| 19 | + simplifyParameterSchema({ |
| 20 | + oneOf: [{ type: 'integer' }, { type: 'string' }], |
| 21 | + }), |
| 22 | + ).toEqual({ type: ['integer', 'string'] }) |
| 23 | + }) |
| 24 | + |
| 25 | + test('describes array variants by item type and omits enum when a variant is unrestricted', () => { |
| 26 | + expect( |
| 27 | + simplifyParameterSchema({ |
| 28 | + oneOf: [{ type: 'string' }, { type: 'array', items: { type: 'string', enum: ['patch'] } }], |
| 29 | + }), |
| 30 | + ).toEqual({ type: ['string', 'array of strings'] }) |
| 31 | + }) |
| 32 | + |
| 33 | + test('combines enum values when every variant is restricted', () => { |
| 34 | + expect( |
| 35 | + simplifyParameterSchema({ |
| 36 | + oneOf: [ |
| 37 | + { type: 'string', enum: ['patch'] }, |
| 38 | + { type: 'array', items: { type: 'string', enum: ['patch', 'deployment'] } }, |
| 39 | + ], |
| 40 | + }), |
| 41 | + ).toEqual({ type: ['string', 'array of strings'], enum: ['patch', 'deployment'] }) |
| 42 | + }) |
| 43 | + |
| 44 | + test('derives the type from anyOf variants and drops duplicate types', () => { |
| 45 | + expect( |
| 46 | + simplifyParameterSchema({ |
| 47 | + anyOf: [{ type: 'string' }, { type: 'string', enum: ['all'] }], |
| 48 | + default: 'all', |
| 49 | + }), |
| 50 | + ).toEqual({ type: 'string', default: 'all' }) |
| 51 | + }) |
| 52 | + |
| 53 | + test('leaves the type undefined for an empty schema', () => { |
| 54 | + expect(simplifyParameterSchema({})).toEqual({ type: undefined }) |
| 55 | + }) |
| 56 | +}) |
0 commit comments