@@ -16,18 +16,10 @@ describe('Gateway', () => {
16
16
expect ( gateway [ 'pluginIndexUrl' ] ) . toBe ( 'https://test-market-index-url.com' ) ;
17
17
} ) ;
18
18
19
- it ( 'run with ajv validator' , async ( ) => {
20
- const mockResult = JSON . stringify ( { success : true } ) ;
21
- vi . mocked ( SwaggerClient ) . mockResolvedValue ( {
22
- execute : vi . fn ( ) . mockResolvedValue ( {
23
- status : 200 ,
24
- text : mockResult ,
25
- } ) ,
26
- } ) ;
27
-
19
+ describe ( 'using AJV Validator' , ( ) => {
28
20
const gateway = new Gateway ( {
29
21
Validator : ( schema , value ) => {
30
- const ajv = new Ajv ( { validateFormats : false } ) ;
22
+ const ajv = new Ajv ( { strict : false } ) ;
31
23
const validate = ajv . compile ( schema ) ;
32
24
33
25
const valid = validate ( value ) ;
@@ -38,53 +30,141 @@ describe('Gateway', () => {
38
30
} ,
39
31
} ) ;
40
32
41
- const payload = {
42
- apiName : 'getSupportedVendors' ,
43
- arguments : '{}' ,
44
- identifier : 'mock-credit-card' ,
45
- manifest : {
46
- $schema : '../node_modules/@lobehub/chat-plugin-sdk/schema.json' ,
47
- api : [
48
- {
49
- description : 'It is API to get list of supported vendors' ,
50
- name : 'getSupportedVendors' ,
51
- parameters : {
52
- properties : { } ,
53
- type : 'object' ,
33
+ it ( 'run with ajv validator' , async ( ) => {
34
+ const mockResult = JSON . stringify ( { success : true } ) ;
35
+ vi . mocked ( SwaggerClient ) . mockResolvedValue ( {
36
+ execute : vi . fn ( ) . mockResolvedValue ( {
37
+ status : 200 ,
38
+ text : mockResult ,
39
+ } ) ,
40
+ } ) ;
41
+
42
+ const payload = {
43
+ apiName : 'getSupportedVendors' ,
44
+ arguments : '{}' ,
45
+ identifier : 'mock-credit-card' ,
46
+ manifest : {
47
+ $schema : '../node_modules/@lobehub/chat-plugin-sdk/schema.json' ,
48
+ api : [
49
+ {
50
+ description : 'It is API to get list of supported vendors' ,
51
+ name : 'getSupportedVendors' ,
52
+ parameters : {
53
+ properties : { } ,
54
+ type : 'object' ,
55
+ } ,
54
56
} ,
57
+ ] ,
58
+ author : 'arvinxx' ,
59
+ createdAt : '2023-12-11' ,
60
+ identifier : 'mock-credit-card' ,
61
+ meta : {
62
+ avatar : '💳' ,
63
+ description : 'Credit Card Number Generator' ,
64
+ tags : [ 'credit-card' , 'mockup' , 'generator' ] ,
65
+ title : 'Credit Card Generator' ,
55
66
} ,
56
- ] ,
57
- author : 'arvinxx' ,
58
- createdAt : '2023-12-11' ,
59
- identifier : 'mock-credit-card' ,
60
- meta : {
61
- avatar : '💳' ,
62
- description : 'Credit Card Number Generator' ,
63
- tags : [ 'credit-card' , 'mockup' , 'generator' ] ,
64
- title : 'Credit Card Generator' ,
67
+ openapi :
68
+ 'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json' ,
69
+ settings : {
70
+ properties : {
71
+ apiKeyAuth : {
72
+ description : 'apiKeyAuth API Key' ,
73
+ format : 'password' ,
74
+ title : 'X-OpenAPIHub-Key' ,
75
+ type : 'string' ,
76
+ } ,
77
+ } ,
78
+ required : [ 'apiKeyAuth' ] ,
79
+ type : 'object' ,
80
+ } ,
81
+ version : '1' ,
65
82
} ,
66
- openapi :
67
- 'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json' ,
68
- settings : {
69
- properties : {
70
- apiKeyAuth : {
71
- description : 'apiKeyAuth API Key' ,
72
- format : 'password' ,
73
- title : 'X-OpenAPIHub-Key' ,
74
- type : 'string' ,
83
+ type : 'default' ,
84
+ } as PluginRequestPayload ;
85
+
86
+ const res = await gateway . execute ( payload , { apiKeyAuth : 'abc' } ) ;
87
+
88
+ expect ( res . success ) . toBeTruthy ( ) ;
89
+ expect ( res . data ) . toEqual ( mockResult ) ;
90
+ } ) ;
91
+
92
+ it ( 'run with ajv with $$refs' , async ( ) => {
93
+ const mockResult = JSON . stringify ( { success : true } ) ;
94
+ vi . mocked ( SwaggerClient ) . mockResolvedValue ( {
95
+ execute : vi . fn ( ) . mockResolvedValue ( {
96
+ status : 200 ,
97
+ text : mockResult ,
98
+ } ) ,
99
+ } ) ;
100
+
101
+ const payload = {
102
+ apiName : 'getRandomCardNumber' ,
103
+ arguments : '{"vendor":"visa"}' ,
104
+ identifier : 'mock-credit-card' ,
105
+ manifest : {
106
+ $schema : '../node_modules/@lobehub/chat-plugin-sdk/schema.json' ,
107
+ api : [
108
+ {
109
+ description : 'It is API to generate random credit card numbers' ,
110
+ name : 'getRandomCardNumber' ,
111
+ parameters : {
112
+ properties : {
113
+ vendor : {
114
+ $$ref : 'https://chat-dev.lobehub.com/chat#/components/schemas/Vendor' ,
115
+ enum : [
116
+ 'visa' ,
117
+ 'master-card' ,
118
+ 'diners-club' ,
119
+ 'american-express' ,
120
+ 'discover' ,
121
+ 'jcb' ,
122
+ ] ,
123
+ type : 'string' ,
124
+ } ,
125
+ } ,
126
+ required : [ 'vendor' ] ,
127
+ type : 'object' ,
128
+ } ,
129
+ } ,
130
+ {
131
+ description : 'It is API to get list of supported vendors' ,
132
+ name : 'getSupportedVendors' ,
133
+ parameters : { properties : { } , type : 'object' } ,
75
134
} ,
135
+ ] ,
136
+ author : 'arvinxx' ,
137
+ createdAt : '2023-12-11' ,
138
+ identifier : 'mock-credit-card' ,
139
+ meta : {
140
+ avatar : '💳' ,
141
+ description : 'Credit Card Number Generator' ,
142
+ tags : [ 'credit-card' , 'mockup' , 'generator' ] ,
143
+ title : 'Credit Card Generator' ,
76
144
} ,
77
- required : [ 'apiKeyAuth' ] ,
78
- type : 'object' ,
145
+ openapi :
146
+ 'https://lobe-plugin-mock-credit-card-arvinxx.vercel.app/credit-card-number-generator.json' ,
147
+ settings : {
148
+ properties : {
149
+ apiKeyAuth : {
150
+ description : 'apiKeyAuth API Key' ,
151
+ format : 'password' ,
152
+ title : 'X-OpenAPIHub-Key' ,
153
+ type : 'string' ,
154
+ } ,
155
+ } ,
156
+ required : [ 'apiKeyAuth' ] ,
157
+ type : 'object' ,
158
+ } ,
159
+ version : '1' ,
79
160
} ,
80
- version : '1' ,
81
- } ,
82
- type : 'default' ,
83
- } as PluginRequestPayload ;
161
+ type : 'default' ,
162
+ } as PluginRequestPayload ;
84
163
85
- const res = await gateway . execute ( payload , { apiKeyAuth : 'abc' } ) ;
164
+ const res = await gateway . execute ( payload , { apiKeyAuth : 'abc' } ) ;
86
165
87
- expect ( res . success ) . toBeTruthy ( ) ;
88
- expect ( res . data ) . toEqual ( mockResult ) ;
166
+ expect ( res . success ) . toBeTruthy ( ) ;
167
+ expect ( res . data ) . toEqual ( mockResult ) ;
168
+ } ) ;
89
169
} ) ;
90
170
} ) ;
0 commit comments