UNPKG

hey-api-builders

Version:

A custom plugin for @hey-api/openapi-ts that generates mock data builders with a lightweight custom runtime, Zod integration, or static mock generation.

412 lines 18.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const static_mock_generator_1 = require("./static-mock-generator"); (0, vitest_1.describe)('Static Mock Generator', () => { (0, vitest_1.describe)('generateStaticMockCode', () => { (0, vitest_1.it)('generates correct mock for string with email format', () => { const schema = { type: 'string', format: 'email', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Email'); (0, vitest_1.expect)(result).toBe('"user@example.com"'); }); (0, vitest_1.it)('generates correct mock for string with uuid format', () => { const schema = { type: 'string', format: 'uuid', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'UUID'); (0, vitest_1.expect)(result).toBe('"550e8400-e29b-41d4-a716-446655440000"'); }); (0, vitest_1.it)('generates correct mock for string with url format', () => { const schema = { type: 'string', format: 'url', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'URL'); (0, vitest_1.expect)(result).toBe('"https://example.com"'); }); (0, vitest_1.it)('generates correct mock for string with date format', () => { const schema = { type: 'string', format: 'date', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Date'); (0, vitest_1.expect)(result).toBe('"2024-01-01"'); }); (0, vitest_1.it)('generates correct mock for string with date-time format', () => { const schema = { type: 'string', format: 'date-time', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'DateTime'); (0, vitest_1.expect)(result).toBe('"2024-01-01T00:00:00.000Z"'); }); (0, vitest_1.it)('generates correct mock for plain string', () => { const schema = { type: 'string', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'String'); (0, vitest_1.expect)(result).toBe('"aaaaa"'); }); (0, vitest_1.it)('respects minLength for strings', () => { const schema = { type: 'string', minLength: 10, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'String'); (0, vitest_1.expect)(result).toBe('"aaaaaaaaaa"'); }); (0, vitest_1.it)('generates correct mock for number', () => { const schema = { type: 'number', minimum: 0, maximum: 100, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Number'); (0, vitest_1.expect)(result).toBe('50'); }); (0, vitest_1.it)('generates correct mock for integer', () => { const schema = { type: 'integer', minimum: 0, maximum: 10, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Integer'); (0, vitest_1.expect)(result).toBe('5'); }); (0, vitest_1.it)('generates correct mock for boolean', () => { const schema = { type: 'boolean', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Boolean'); (0, vitest_1.expect)(result).toBe('true'); }); (0, vitest_1.it)('generates correct mock for enum', () => { const schema = { enum: ['active', 'inactive', 'pending'], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Status'); (0, vitest_1.expect)(result).toBe('"active"'); }); (0, vitest_1.it)('generates correct mock for array', () => { const schema = { type: 'array', items: { type: 'string', }, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'StringArray'); (0, vitest_1.expect)(result).toBe('["aaaaa"]'); }); (0, vitest_1.it)('generates correct mock for simple object', () => { const schema = { type: 'object', properties: { id: { type: 'string', format: 'uuid' }, name: { type: 'string' }, }, required: ['id', 'name'], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'User'); (0, vitest_1.expect)(result).toContain('id: "550e8400-e29b-41d4-a716-446655440000"'); (0, vitest_1.expect)(result).toContain('name: "aaaaa"'); }); (0, vitest_1.it)('only includes required properties in objects', () => { const schema = { type: 'object', properties: { id: { type: 'string' }, name: { type: 'string' }, optional: { type: 'string' }, }, required: ['id', 'name'], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'User'); (0, vitest_1.expect)(result).toContain('id:'); (0, vitest_1.expect)(result).toContain('name:'); (0, vitest_1.expect)(result).not.toContain('optional:'); }); (0, vitest_1.it)('handles nested objects', () => { const schema = { type: 'object', properties: { user: { type: 'object', properties: { id: { type: 'string' }, name: { type: 'string' }, }, required: ['id'], }, }, required: ['user'], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Wrapper'); (0, vitest_1.expect)(result).toContain('user:'); (0, vitest_1.expect)(result).toContain('id:'); }); (0, vitest_1.it)('uses default value if provided', () => { const schema = { type: 'string', default: 'default-value', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'String'); (0, vitest_1.expect)(result).toBe('"default-value"'); }); (0, vitest_1.it)('uses example value if provided', () => { const schema = { type: 'number', examples: [42], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Number'); (0, vitest_1.expect)(result).toBe('42'); }); (0, vitest_1.it)('handles const value in schema default/example', () => { const schema = { type: 'string', default: 'constant-value', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Const'); (0, vitest_1.expect)(result).toBe('"constant-value"'); }); (0, vitest_1.it)('handles oneOf schemas', () => { const schema = { oneOf: [{ type: 'string' }, { type: 'number' }], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Union'); (0, vitest_1.expect)(result).toBe('null'); }); (0, vitest_1.it)('handles anyOf schemas with enums', () => { const schema = { anyOf: [{ const: 'value1' }, { const: 'value2' }], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'AnyUnion'); (0, vitest_1.expect)(result).toBe('"value1"'); }); (0, vitest_1.it)('handles allOf schemas', () => { const schema = { allOf: [ { type: 'object', properties: { name: { type: 'string' }, }, }, { type: 'object', properties: { age: { type: 'number' }, }, }, ], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Combined'); (0, vitest_1.expect)(result).toBe('null'); }); (0, vitest_1.it)('handles number with minimum constraint', () => { const schema = { type: 'number', minimum: 100, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'MinNum'); const value = Number(result); (0, vitest_1.expect)(value).toBeGreaterThanOrEqual(100); }); (0, vitest_1.it)('handles number with maximum constraint', () => { const schema = { type: 'number', maximum: 10, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'MaxNum'); const value = Number(result); (0, vitest_1.expect)(value).toBeLessThanOrEqual(10); }); (0, vitest_1.it)('handles integer type specifically', () => { const schema = { type: 'integer', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Int'); const value = Number(result); (0, vitest_1.expect)(Number.isInteger(value)).toBe(true); }); (0, vitest_1.it)('handles string with minLength', () => { const schema = { type: 'string', minLength: 20, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'LongString'); const unquoted = result.slice(1, -1); (0, vitest_1.expect)(unquoted.length).toBeGreaterThanOrEqual(20); }); (0, vitest_1.it)('handles string with maxLength', () => { const schema = { type: 'string', maxLength: 5, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'ShortString'); const unquoted = result.slice(1, -1); (0, vitest_1.expect)(unquoted.length).toBeLessThanOrEqual(5); }); (0, vitest_1.it)('handles string with pattern as placeholder', () => { const schema = { type: 'string', pattern: '^[0-9]{3}$', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Pattern'); (0, vitest_1.expect)(result).toBe('"pattern-match"'); }); (0, vitest_1.it)('handles uuid format', () => { const schema = { type: 'string', format: 'uuid', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'UUID'); const unquoted = result.slice(1, -1); (0, vitest_1.expect)(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(unquoted)).toBe(true); }); (0, vitest_1.it)('handles email format', () => { const schema = { type: 'string', format: 'email', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Email'); const unquoted = result.slice(1, -1); (0, vitest_1.expect)(unquoted).toMatch(/@/); }); (0, vitest_1.it)('handles uri/url format', () => { const schema = { type: 'string', format: 'uri', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'URI'); const unquoted = result.slice(1, -1); (0, vitest_1.expect)(unquoted).toMatch(/^https?:\/\//); }); (0, vitest_1.it)('handles date-time format', () => { const schema = { type: 'string', format: 'date-time', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'DateTime'); const unquoted = result.slice(1, -1); (0, vitest_1.expect)(!isNaN(Date.parse(unquoted))).toBe(true); }); (0, vitest_1.it)('handles date format', () => { const schema = { type: 'string', format: 'date', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Date'); const unquoted = result.slice(1, -1); (0, vitest_1.expect)(/^\d{4}-\d{2}-\d{2}$/.test(unquoted)).toBe(true); }); (0, vitest_1.it)('handles time format with fallback', () => { const schema = { type: 'string', format: 'time', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Time'); (0, vitest_1.expect)(typeof result).toBe('string'); (0, vitest_1.expect)(result.length).toBeGreaterThan(0); }); (0, vitest_1.it)('handles array with minItems', () => { const schema = { type: 'array', items: { type: 'number' }, minItems: 5, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'MinArray'); const arr = JSON.parse(result); (0, vitest_1.expect)(arr.length).toBeGreaterThanOrEqual(5); }); (0, vitest_1.it)('handles array with maxItems', () => { const schema = { type: 'array', items: { type: 'string' }, maxItems: 2, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'MaxArray'); const arr = JSON.parse(result); (0, vitest_1.expect)(arr.length).toBeLessThanOrEqual(2); }); (0, vitest_1.it)('handles null type', () => { const schema = { type: 'null', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Null'); (0, vitest_1.expect)(result).toBe('null'); }); (0, vitest_1.it)('handles nullable string', () => { const schema = { type: 'string', nullable: true, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'NullableString'); (0, vitest_1.expect)(typeof result).toBe('string'); }); (0, vitest_1.it)('handles empty object without properties', () => { const schema = { type: 'object', }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'EmptyObj'); (0, vitest_1.expect)(result).toBe('{}'); }); (0, vitest_1.it)('handles complex nested structures', () => { const schema = { type: 'object', properties: { users: { type: 'array', items: { type: 'object', properties: { profile: { type: 'object', properties: { name: { type: 'string' }, }, required: ['name'], }, }, required: ['profile'], }, }, }, required: ['users'], }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Nested'); (0, vitest_1.expect)(result).toContain('users:'); (0, vitest_1.expect)(result).toContain('profile:'); (0, vitest_1.expect)(result).toContain('name:'); }); (0, vitest_1.it)('handles schema without type', () => { const schema = {}; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Unknown'); (0, vitest_1.expect)(result).toBeTruthy(); }); (0, vitest_1.it)('handles multipleOf constraint for numbers', () => { const schema = { type: 'number', multipleOf: 5, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Multiple'); const value = Number(result); (0, vitest_1.expect)(value % 5).toBe(0); }); (0, vitest_1.it)('handles number constraints with min and max', () => { const schema = { type: 'number', minimum: 10, maximum: 20, }; const result = (0, static_mock_generator_1.generateStaticMockCode)(schema, 'Constrained'); const value = Number(result); (0, vitest_1.expect)(value).toBeGreaterThanOrEqual(10); (0, vitest_1.expect)(value).toBeLessThanOrEqual(20); }); }); }); //# sourceMappingURL=static-mock-generator.test.js.map