|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { schemaToOpenAPI } from '../../src/openapi'; |
| 4 | +import { OpenAPIV3 } from 'openapi-types'; |
| 5 | +import { Schema } from '../../src/ir'; |
| 6 | + |
| 7 | +// Test numeric constraints |
| 8 | +test('numeric constraints are properly mapped', () => { |
| 9 | + // Create a schema with numeric constraints |
| 10 | + const schema: Schema = { |
| 11 | + type: 'number', |
| 12 | + minimum: 0, |
| 13 | + maximum: 100, |
| 14 | + multipleOf: 5, |
| 15 | + comment: { |
| 16 | + description: 'A number with constraints', |
| 17 | + tags: [], |
| 18 | + source: [], |
| 19 | + problems: [] |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + const result = schemaToOpenAPI(schema) as OpenAPIV3.SchemaObject; |
| 24 | + |
| 25 | + // Check that the result has the correct type and description |
| 26 | + assert.equal(result.type, 'number'); |
| 27 | + assert.equal(result.description, 'A number with constraints'); |
| 28 | + |
| 29 | + // Check that the constraints are properly mapped |
| 30 | + assert.equal(result.minimum, 0); |
| 31 | + assert.equal(result.maximum, 100); |
| 32 | + assert.equal(result.multipleOf, 5); |
| 33 | +}); |
| 34 | + |
| 35 | +// Test exclusive constraints |
| 36 | +test('exclusive numeric constraints are properly mapped', () => { |
| 37 | + // Create a schema with exclusive constraints |
| 38 | + const schema: Schema = { |
| 39 | + type: 'number', |
| 40 | + minimum: 0, |
| 41 | + maximum: 100, |
| 42 | + exclusiveMinimum: true, |
| 43 | + exclusiveMaximum: true, |
| 44 | + comment: { |
| 45 | + description: 'A number with exclusive constraints', |
| 46 | + tags: [], |
| 47 | + source: [], |
| 48 | + problems: [] |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + const result = schemaToOpenAPI(schema) as OpenAPIV3.SchemaObject; |
| 53 | + |
| 54 | + // Check that the result has the correct type and description |
| 55 | + assert.equal(result.type, 'number'); |
| 56 | + assert.equal(result.description, 'A number with exclusive constraints'); |
| 57 | + |
| 58 | + // Check that the constraints are properly mapped |
| 59 | + assert.equal(result.minimum, 0); |
| 60 | + assert.equal(result.maximum, 100); |
| 61 | + assert.equal(result.exclusiveMinimum, true); |
| 62 | + assert.equal(result.exclusiveMaximum, true); |
| 63 | +}); |
| 64 | + |
| 65 | +// Test string-encoded numeric types |
| 66 | +test('string-encoded numeric types are properly mapped', () => { |
| 67 | + // Create a schema for a string-encoded number |
| 68 | + const schema: Schema = { |
| 69 | + type: 'string', |
| 70 | + format: 'number', |
| 71 | + decodedType: 'number' |
| 72 | + }; |
| 73 | + |
| 74 | + const result = schemaToOpenAPI(schema) as OpenAPIV3.SchemaObject; |
| 75 | + |
| 76 | + // Check that the result has the correct type |
| 77 | + assert.equal(result.type, 'number'); |
| 78 | +}); |
| 79 | + |
| 80 | +// Test string-encoded integer types |
| 81 | +test('string-encoded integer types are properly mapped', () => { |
| 82 | + // Create a schema for a string-encoded integer |
| 83 | + const schema: Schema = { |
| 84 | + type: 'string', |
| 85 | + format: 'integer', |
| 86 | + decodedType: 'number' |
| 87 | + }; |
| 88 | + |
| 89 | + const result = schemaToOpenAPI(schema) as OpenAPIV3.SchemaObject; |
| 90 | + |
| 91 | + // Check that the result has the correct type |
| 92 | + assert.equal(result.type, 'integer'); |
| 93 | +}); |
| 94 | + |
| 95 | +// Test BigInt types |
| 96 | +test('BigInt types are properly mapped', () => { |
| 97 | + // Create a schema for a BigInt |
| 98 | + const schema: Schema = { |
| 99 | + type: 'string', |
| 100 | + format: 'number', |
| 101 | + decodedType: 'bigint' |
| 102 | + }; |
| 103 | + |
| 104 | + const result = schemaToOpenAPI(schema) as OpenAPIV3.SchemaObject; |
| 105 | + |
| 106 | + // Check that the result has the correct type and format |
| 107 | + assert.equal(result.type, 'integer'); |
| 108 | + assert.equal(result.format, 'int64'); |
| 109 | +}); |
| 110 | + |
| 111 | +// Test BigInt types with constraints |
| 112 | +test('BigInt types with constraints are properly mapped', () => { |
| 113 | + // Create a schema for a BigInt with constraints |
| 114 | + const schema: Schema = { |
| 115 | + type: 'string', |
| 116 | + format: 'number', |
| 117 | + decodedType: 'bigint', |
| 118 | + minimum: 0, |
| 119 | + maximum: 1000000000000000000 |
| 120 | + }; |
| 121 | + |
| 122 | + const result = schemaToOpenAPI(schema) as OpenAPIV3.SchemaObject; |
| 123 | + |
| 124 | + // Check that the result has the correct type, format, and constraints |
| 125 | + assert.equal(result.type, 'integer'); |
| 126 | + assert.equal(result.format, 'int64'); |
| 127 | + assert.equal(result.minimum, 0); |
| 128 | + assert.equal(result.maximum, 1000000000000000000); |
| 129 | +}); |
0 commit comments