forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi-contracts.test.ts
More file actions
138 lines (131 loc) · 3.71 KB
/
Copy pathopenapi-contracts.test.ts
File metadata and controls
138 lines (131 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { describe, it, expect } from 'vitest';
import {
generateContracts,
type OpenApiSpec,
} from '../src/contracts/openapi-generator';
describe('OpenAPI client contracts (issue #97)', () => {
const spec: OpenApiSpec = {
openapi: '3.0.0',
info: { title: 'Lily API', version: '1.0.0' },
paths: {
'/v1/payments': {
get: {
operationId: 'listPayments',
parameters: [
{
name: 'limit',
in: 'query',
required: false,
schema: { type: 'integer' },
},
],
responses: {
'200': {
content: {
'application/json': {
schema: { $ref: '#/components/schemas/PaymentList' },
},
},
},
},
},
post: {
operationId: 'createPayment',
requestBody: {
content: {
'application/json': {
schema: { $ref: '#/components/schemas/CreatePaymentRequest' },
},
},
},
responses: {
'200': {
content: {
'application/json': {
schema: { $ref: '#/components/schemas/Payment' },
},
},
},
},
},
},
'/v1/payments/{id}': {
get: {
operationId: 'getPayment',
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: { type: 'string' },
},
],
responses: {
'200': {
content: {
'application/json': {
schema: { $ref: '#/components/schemas/Payment' },
},
},
},
},
},
delete: {
operationId: 'deletePayment',
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: { type: 'string' },
},
],
responses: { '204': { description: 'Deleted' } },
},
},
},
};
it('generates contracts for all operations', () => {
const contracts = generateContracts(spec);
expect(contracts).toHaveLength(4);
});
it('extracts operation IDs', () => {
const contracts = generateContracts(spec);
const ops = contracts.map((c) => c.operationId);
expect(ops).toContain('listPayments');
expect(ops).toContain('createPayment');
expect(ops).toContain('getPayment');
expect(ops).toContain('deletePayment');
});
it('extracts response types from $ref', () => {
const contracts = generateContracts(spec);
const getPayment = contracts.find((c) => c.operationId === 'getPayment');
expect(getPayment?.responseType).toBe('Payment');
});
it('extracts request types from $ref', () => {
const contracts = generateContracts(spec);
const createPayment = contracts.find(
(c) => c.operationId === 'createPayment',
);
expect(createPayment?.requestType).toBe('CreatePaymentRequest');
});
it('maps parameters with required flag', () => {
const contracts = generateContracts(spec);
const getPayment = contracts.find((c) => c.operationId === 'getPayment');
expect(getPayment?.parameters).toHaveLength(1);
expect(getPayment?.parameters[0]).toEqual({
name: 'id',
in: 'path',
required: true,
type: 'string',
});
});
it('handles empty spec gracefully', () => {
const contracts = generateContracts({
openapi: '3.0.0',
info: { title: 'Empty', version: '0' },
paths: {},
});
expect(contracts).toEqual([]);
});
});