@@ -16,7 +16,7 @@ import path from 'path'
16
16
const runMock = jest . spyOn ( main , 'run' )
17
17
18
18
// Mock the GitHub Actions core library
19
- // let debugMock: jest.SpiedFunction<typeof core.debug>
19
+ let debugMock : jest . SpiedFunction < typeof core . debug >
20
20
let errorMock : jest . SpiedFunction < typeof core . error >
21
21
let getInputMock : jest . SpiedFunction < typeof core . getInput >
22
22
let setFailedMock : jest . SpiedFunction < typeof core . setFailed >
@@ -29,7 +29,7 @@ describe('action', () => {
29
29
jest . clearAllMocks ( )
30
30
fetchMock . resetMocks ( )
31
31
32
- // debugMock = jest.spyOn(core, 'debug').mockImplementation()
32
+ debugMock = jest . spyOn ( core , 'debug' ) . mockImplementation ( )
33
33
errorMock = jest . spyOn ( core , 'error' ) . mockImplementation ( )
34
34
getInputMock = jest . spyOn ( core , 'getInput' ) . mockImplementation ( )
35
35
setFailedMock = jest . spyOn ( core , 'setFailed' ) . mockImplementation ( )
@@ -82,7 +82,9 @@ describe('action', () => {
82
82
expect ( runMock ) . toHaveReturned ( )
83
83
expect ( errorMock ) . not . toHaveBeenCalled ( )
84
84
expect ( setFailedMock ) . toHaveBeenCalledWith (
85
- 'Error: HTTP Error: 400, Bad Request, {"message":"Something went wrong"}'
85
+ expect . stringContaining (
86
+ 'Error: HTTP Error: 400, Bad Request, {"message":"Something went wrong"}'
87
+ )
86
88
)
87
89
88
90
expect ( fetchMock ) . toHaveBeenNthCalledWith (
@@ -99,4 +101,225 @@ describe('action', () => {
99
101
} )
100
102
)
101
103
} )
104
+ it ( 'can read lua spec' , async ( ) => {
105
+ // Set the action's inputs as return values from core.getInput()
106
+ getInputMock . mockImplementation ( name => {
107
+ switch ( name ) {
108
+ case 'test' :
109
+ return 'true'
110
+ case 'spec' :
111
+ return path . join ( __dirname , 'data' , 'testspec.lua' )
112
+ case 'api' :
113
+ return 'https://example.com/'
114
+ default :
115
+ return ''
116
+ }
117
+ } )
118
+
119
+ await main . run ( )
120
+ expect ( runMock ) . toHaveReturned ( )
121
+
122
+ expect ( errorMock ) . not . toHaveBeenCalled ( )
123
+ expect ( setFailedMock ) . not . toHaveBeenCalled ( )
124
+ } )
125
+ it ( 'fails if it tries to exit' , async ( ) => {
126
+ // Set the action's inputs as return values from core.getInput()
127
+ getInputMock . mockImplementation ( name => {
128
+ switch ( name ) {
129
+ case 'test' :
130
+ return 'true'
131
+ case 'spec' :
132
+ return path . join ( __dirname , 'data' , 'exit.lua' )
133
+ default :
134
+ return ''
135
+ }
136
+ } )
137
+
138
+ await main . run ( )
139
+ expect ( runMock ) . toHaveReturned ( )
140
+
141
+ expect ( errorMock ) . not . toHaveBeenCalled ( )
142
+ expect ( setFailedMock ) . toHaveBeenNthCalledWith (
143
+ 1 ,
144
+ expect . stringContaining ( 'Error: osExit not allowed' )
145
+ )
146
+ } )
147
+ it ( 'fails if it tries to load a file' , async ( ) => {
148
+ // Set the action's inputs as return values from core.getInput()
149
+ getInputMock . mockImplementation ( name => {
150
+ switch ( name ) {
151
+ case 'test' :
152
+ return 'true'
153
+ case 'spec' :
154
+ return path . join ( __dirname , 'data' , 'loadfile.lua' )
155
+ default :
156
+ return ''
157
+ }
158
+ } )
159
+
160
+ await main . run ( )
161
+ expect ( runMock ) . toHaveReturned ( )
162
+
163
+ expect ( errorMock ) . not . toHaveBeenCalled ( )
164
+ expect ( setFailedMock ) . toHaveBeenNthCalledWith (
165
+ 1 ,
166
+ expect . stringContaining ( 'Error: loadFile not allowed' )
167
+ )
168
+ } )
169
+ it ( 'can print to debug' , async ( ) => {
170
+ // Set the action's inputs as return values from core.getInput()
171
+ getInputMock . mockImplementation ( name => {
172
+ switch ( name ) {
173
+ case 'test' :
174
+ return 'true'
175
+ case 'spec' :
176
+ return path . join ( __dirname , 'data' , 'print.lua' )
177
+ default :
178
+ return ''
179
+ }
180
+ } )
181
+
182
+ await main . run ( )
183
+ expect ( runMock ) . toHaveReturned ( )
184
+ expect ( debugMock ) . toHaveBeenNthCalledWith ( 1 , 'Hello World!' )
185
+
186
+ expect ( errorMock ) . not . toHaveBeenCalled ( )
187
+ expect ( setFailedMock ) . not . toHaveBeenCalled ( )
188
+ } )
189
+ it ( 'spec must be a table' , async ( ) => {
190
+ // Set the action's inputs as return values from core.getInput()
191
+ getInputMock . mockImplementation ( name => {
192
+ switch ( name ) {
193
+ case 'test' :
194
+ return 'true'
195
+ case 'spec' :
196
+ return path . join ( __dirname , 'data' , 'notable.lua' )
197
+ default :
198
+ return ''
199
+ }
200
+ } )
201
+
202
+ await main . run ( )
203
+ expect ( runMock ) . toHaveReturned ( )
204
+
205
+ expect ( errorMock ) . not . toHaveBeenCalled ( )
206
+ expect ( setFailedMock ) . toHaveBeenNthCalledWith (
207
+ 1 ,
208
+ expect . stringContaining ( 'Error: Spec must be a table' )
209
+ )
210
+ } )
211
+ it ( 'spec can contain utf-8 characters' , async ( ) => {
212
+ // Set the action's inputs as return values from core.getInput()
213
+ getInputMock . mockImplementation ( name => {
214
+ switch ( name ) {
215
+ case 'test' :
216
+ return 'true'
217
+ case 'spec' :
218
+ return path . join ( __dirname , 'data' , 'utf8spec.lua' )
219
+ default :
220
+ return ''
221
+ }
222
+ } )
223
+
224
+ await main . run ( )
225
+ expect ( runMock ) . toHaveReturned ( )
226
+
227
+ expect ( errorMock ) . not . toHaveBeenCalled ( )
228
+ expect ( setFailedMock ) . not . toHaveBeenCalled ( )
229
+ } )
230
+ it ( 'should fail if invalid response' , async ( ) => {
231
+ getInputMock . mockImplementation ( name => {
232
+ switch ( name ) {
233
+ case 'test' :
234
+ return 'false'
235
+ case 'spec' :
236
+ return path . join ( __dirname , 'data' , 'testspec.lua' )
237
+ case 'download-url' :
238
+ return 'https://example.com/test.zip'
239
+ case 'api' :
240
+ return 'https://example.com/'
241
+ case 'token' :
242
+ return 'token'
243
+ default :
244
+ return ''
245
+ }
246
+ } )
247
+
248
+ fetchMock . mockResponseOnce (
249
+ JSON . stringify ( { message : 'Something went wrong' } ) ,
250
+ {
251
+ status : 400 ,
252
+ statusText : 'Bad Request'
253
+ }
254
+ )
255
+ await main . run ( )
256
+ expect ( setFailedMock ) . toHaveBeenCalledWith (
257
+ expect . stringContaining (
258
+ 'HTTP Error: 400, Bad Request, {"message":"Something went wrong"}'
259
+ )
260
+ )
261
+ } )
262
+ it ( 'should handle a 500 error' , async ( ) => {
263
+ getInputMock . mockImplementation ( name => {
264
+ switch ( name ) {
265
+ case 'test' :
266
+ return 'false'
267
+ case 'spec' :
268
+ return path . join ( __dirname , 'data' , 'testspec.lua' )
269
+ case 'download-url' :
270
+ return 'https://example.com/test.zip'
271
+ case 'api' :
272
+ return 'https://example.com/'
273
+ case 'token' :
274
+ return 'token'
275
+ default :
276
+ return ''
277
+ }
278
+ } )
279
+
280
+ fetchMock . mockResponseOnce ( '' , {
281
+ status : 500 ,
282
+ statusText : 'Internal Server Error'
283
+ } )
284
+ await main . run ( )
285
+ expect ( setFailedMock ) . toHaveBeenCalledWith (
286
+ expect . stringContaining ( 'HTTP Error: 500, Internal Server Error' )
287
+ )
288
+ } )
289
+ it ( 'Should create a new plugin if not found' , async ( ) => {
290
+ getInputMock . mockImplementation ( name => {
291
+ switch ( name ) {
292
+ case 'test' :
293
+ return 'false'
294
+ case 'spec' :
295
+ return path . join ( __dirname , 'data' , 'testspec.lua' )
296
+ case 'download-url' :
297
+ return 'https://example.com/test.zip'
298
+ case 'api' :
299
+ return 'https://example.com/'
300
+ case 'token' :
301
+ return '__token__'
302
+ default :
303
+ return ''
304
+ }
305
+ } )
306
+
307
+ fetchMock . mockResponseOnce ( JSON . stringify ( { items : [ ] } ) , { status : 200 } )
308
+ fetchMock . mockResponseOnce ( JSON . stringify ( { } ) , { status : 200 } )
309
+ await main . run ( )
310
+ expect ( fetchMock ) . toHaveBeenNthCalledWith (
311
+ 1 ,
312
+ 'https://example.com/api/v1/management/plugins' ,
313
+ expect . objectContaining ( {
314
+ body : expect . anything ( ) ,
315
+ method : 'POST' ,
316
+ headers : expect . objectContaining ( {
317
+ Authorization : 'Bearer __token__' ,
318
+ 'Content-Type' : 'application/json' ,
319
+ accept : 'application/json'
320
+ } )
321
+ } )
322
+ )
323
+ expect ( setFailedMock ) . not . toHaveBeenCalled ( )
324
+ } )
102
325
} )
0 commit comments