@@ -2,13 +2,20 @@ import FC_Client, { fc2Client } from '../../../../../src/resources/fc/impl/clien
22import { ICredentials } from '@serverless-devs/component-interface' ;
33import { Config } from '@alicloud/openapi-client' ;
44import FC2 from '@alicloud/fc2' ;
5- import { IRegion } from '../../../../../src/interface' ;
5+ import { IRegion , IFunction } from '../../../../../src/interface' ;
66import * as utils from '../../../../../src/resources/fc/impl/utils' ;
77import _ from 'lodash' ;
88
99// Mock external dependencies
1010jest . mock ( '@alicloud/openapi-client' ) ;
1111jest . mock ( '@alicloud/fc2' ) ;
12+ jest . mock ( '@alicloud/fc20230330' , ( ) => {
13+ const actual = jest . requireActual ( '@alicloud/fc20230330' ) ;
14+ return Object . assign ( { } , actual , {
15+ __esModule : true ,
16+ default : jest . fn ( ) . mockImplementation ( ( ) => ( { } ) ) ,
17+ } ) ;
18+ } ) ;
1219jest . mock ( '../../../../../src/resources/fc/impl/utils' , ( ) => ( {
1320 ...jest . requireActual ( '../../../../../src/resources/fc/impl/utils' ) ,
1421 getCustomEndpoint : jest . fn ( ) ,
@@ -201,4 +208,107 @@ describe('FC_Client', () => {
201208 expect ( result ) . toBe ( false ) ;
202209 } ) ;
203210 } ) ;
211+
212+ describe ( 'createFunction' , ( ) => {
213+ let client : FC_Client ;
214+
215+ beforeEach ( ( ) => {
216+ ( utils . getCustomEndpoint as jest . Mock ) . mockReturnValue ( {
217+ protocol : 'https' ,
218+ host : 'test-endpoint.com' ,
219+ endpoint : 'https://test-endpoint.com' ,
220+ } ) ;
221+ client = new FC_Client ( mockRegion , mockCredentials , mockOptions ) ;
222+ } ) ;
223+
224+ it ( 'should forward microSandboxConfig to the request body' , async ( ) => {
225+ const createFunctionWithOptions = jest . fn ( ) . mockResolvedValue ( { } as any ) ;
226+ Object . defineProperty ( client , 'fc20230330Client' , {
227+ value : { createFunctionWithOptions } ,
228+ writable : true ,
229+ } ) ;
230+
231+ const config : IFunction = {
232+ functionName : 'test-function' ,
233+ runtime : 'micro-sandbox' ,
234+ microSandboxConfig : {
235+ osType : 'linux' ,
236+ readyCommand : 'echo ready' ,
237+ startCommand : 'echo start' ,
238+ } ,
239+ } as IFunction ;
240+
241+ await client . createFunction ( config ) ;
242+
243+ expect ( createFunctionWithOptions ) . toHaveBeenCalledTimes ( 1 ) ;
244+ const request = createFunctionWithOptions . mock . calls [ 0 ] [ 0 ] ;
245+ const bodyMap = request . body . toMap ( ) ;
246+ expect ( bodyMap . runtime ) . toBe ( 'micro-sandbox' ) ;
247+ expect ( bodyMap . microSandboxConfig ) . toEqual ( {
248+ osType : 'linux' ,
249+ readyCommand : 'echo ready' ,
250+ startCommand : 'echo start' ,
251+ } ) ;
252+ } ) ;
253+
254+ it ( 'should not set microSandboxConfig when it is not provided' , async ( ) => {
255+ const createFunctionWithOptions = jest . fn ( ) . mockResolvedValue ( { } as any ) ;
256+ Object . defineProperty ( client , 'fc20230330Client' , {
257+ value : { createFunctionWithOptions } ,
258+ writable : true ,
259+ } ) ;
260+
261+ await client . createFunction ( {
262+ functionName : 'test-function' ,
263+ runtime : 'nodejs18' ,
264+ } as IFunction ) ;
265+
266+ const bodyMap = createFunctionWithOptions . mock . calls [ 0 ] [ 0 ] . body . toMap ( ) ;
267+ expect ( bodyMap . microSandboxConfig ) . toBeUndefined ( ) ;
268+ } ) ;
269+ } ) ;
270+
271+ describe ( 'updateFunction' , ( ) => {
272+ let client : FC_Client ;
273+
274+ beforeEach ( ( ) => {
275+ ( utils . getCustomEndpoint as jest . Mock ) . mockReturnValue ( {
276+ protocol : 'https' ,
277+ host : 'test-endpoint.com' ,
278+ endpoint : 'https://test-endpoint.com' ,
279+ } ) ;
280+ client = new FC_Client ( mockRegion , mockCredentials , mockOptions ) ;
281+ } ) ;
282+
283+ it ( 'should forward microSandboxConfig to the update request body' , async ( ) => {
284+ const updateFunctionWithOptions = jest . fn ( ) . mockResolvedValue ( { } as any ) ;
285+ Object . defineProperty ( client , 'fc20230330Client' , {
286+ value : { updateFunctionWithOptions } ,
287+ writable : true ,
288+ } ) ;
289+
290+ const config : IFunction = {
291+ functionName : 'test-function' ,
292+ runtime : 'micro-sandbox' ,
293+ microSandboxConfig : {
294+ osType : 'linux' ,
295+ readyCommand : 'echo ready' ,
296+ startCommand : 'echo start' ,
297+ } ,
298+ } as IFunction ;
299+
300+ await client . updateFunction ( config ) ;
301+
302+ expect ( updateFunctionWithOptions ) . toHaveBeenCalledTimes ( 1 ) ;
303+ // first positional arg is functionName, second is the request
304+ expect ( updateFunctionWithOptions . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'test-function' ) ;
305+ const request = updateFunctionWithOptions . mock . calls [ 0 ] [ 1 ] ;
306+ const bodyMap = request . body . toMap ( ) ;
307+ expect ( bodyMap . microSandboxConfig ) . toEqual ( {
308+ osType : 'linux' ,
309+ readyCommand : 'echo ready' ,
310+ startCommand : 'echo start' ,
311+ } ) ;
312+ } ) ;
313+ } ) ;
204314} ) ;
0 commit comments