88 getRun ,
99 cancelApplicationRun ,
1010 listRunResults ,
11+ createApplicationRun ,
1112} from './cli-functions.js' ;
1213import { PlatformSDK , PlatformSDKHttp } from '@aignostics/platform-typescript-sdk' ;
1314import { AuthService } from './utils/auth.js' ;
@@ -32,6 +33,7 @@ const platformSDKMock = {
3233 getRun : vi . fn ( ) ,
3334 cancelApplicationRun : vi . fn ( ) ,
3435 listRunResults : vi . fn ( ) ,
36+ createApplicationRun : vi . fn ( ) ,
3537 getConfig : vi . fn ( ) ,
3638 getVersion : vi . fn ( ) ,
3739} satisfies PlatformSDK ;
@@ -60,6 +62,9 @@ describe('CLI Functions Unit Tests', () => {
6062 } ;
6163
6264 beforeEach ( ( ) => {
65+ // Clear all mocks
66+ vi . clearAllMocks ( ) ;
67+
6368 // Mock console methods to avoid noise in tests
6469 consoleSpy = {
6570 log : vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ,
@@ -342,4 +347,112 @@ describe('CLI Functions Unit Tests', () => {
342347 expect ( mockExit ) . toHaveBeenCalledWith ( 1 ) ;
343348 } ) ;
344349 } ) ;
350+
351+ describe ( 'createApplicationRun' , ( ) => {
352+ it ( 'should create application run successfully with empty items' , async ( ) => {
353+ const runResponse = {
354+ application_run_id : 'run-123' ,
355+ } ;
356+ platformSDKMock . createApplicationRun . mockResolvedValue ( runResponse ) ;
357+
358+ await createApplicationRun (
359+ 'https://api.example.com' ,
360+ mockAuthService ,
361+ 'test-app:v1.0.0' ,
362+ '[]'
363+ ) ;
364+
365+ expect ( platformSDKMock . createApplicationRun ) . toHaveBeenCalledWith ( {
366+ application_version_id : 'test-app:v1.0.0' ,
367+ items : [ ] ,
368+ } ) ;
369+ expect ( consoleSpy . log ) . toHaveBeenCalledWith (
370+ '✅ Application run created successfully:' ,
371+ JSON . stringify ( runResponse , null , 2 )
372+ ) ;
373+ } ) ;
374+
375+ it ( 'should create application run successfully with items' , async ( ) => {
376+ const runResponse = {
377+ application_run_id : 'run-456' ,
378+ } ;
379+ const items = [
380+ {
381+ reference : 'slide_1' ,
382+ input_artifacts : [
383+ {
384+ name : 'input_slide' ,
385+ download_url : 'https://example.com/slide1.tiff' ,
386+ metadata : { mime_type : 'image/tiff' } ,
387+ } ,
388+ ] ,
389+ } ,
390+ ] ;
391+ platformSDKMock . createApplicationRun . mockResolvedValue ( runResponse ) ;
392+
393+ await createApplicationRun (
394+ 'https://api.example.com' ,
395+ mockAuthService ,
396+ 'test-app:v1.0.0' ,
397+ JSON . stringify ( items )
398+ ) ;
399+
400+ expect ( platformSDKMock . createApplicationRun ) . toHaveBeenCalledWith ( {
401+ application_version_id : 'test-app:v1.0.0' ,
402+ items : items ,
403+ } ) ;
404+ expect ( consoleSpy . log ) . toHaveBeenCalledWith (
405+ '✅ Application run created successfully:' ,
406+ JSON . stringify ( runResponse , null , 2 )
407+ ) ;
408+ } ) ;
409+
410+ it ( 'should handle invalid JSON in items parameter' , async ( ) => {
411+ await createApplicationRun (
412+ 'https://api.example.com' ,
413+ mockAuthService ,
414+ 'test-app:v1.0.0' ,
415+ 'invalid-json'
416+ ) ;
417+
418+ expect ( consoleSpy . error ) . toHaveBeenCalledWith ( '❌ Invalid items JSON:' , expect . any ( Error ) ) ;
419+ expect ( mockExit ) . toHaveBeenCalledWith ( 1 ) ;
420+ expect ( platformSDKMock . createApplicationRun ) . not . toHaveBeenCalled ( ) ;
421+ } ) ;
422+
423+ it ( 'should handle non-array items parameter' , async ( ) => {
424+ await createApplicationRun (
425+ 'https://api.example.com' ,
426+ mockAuthService ,
427+ 'test-app:v1.0.0' ,
428+ '{"not": "an array"}'
429+ ) ;
430+
431+ expect ( consoleSpy . error ) . toHaveBeenCalledWith (
432+ '❌ Invalid items JSON:' ,
433+ expect . objectContaining ( {
434+ message : 'Items must be an array' ,
435+ } )
436+ ) ;
437+ expect ( mockExit ) . toHaveBeenCalledWith ( 1 ) ;
438+ expect ( platformSDKMock . createApplicationRun ) . not . toHaveBeenCalled ( ) ;
439+ } ) ;
440+
441+ it ( 'should handle API error during run creation' , async ( ) => {
442+ platformSDKMock . createApplicationRun . mockRejectedValue ( new Error ( 'API error' ) ) ;
443+
444+ await createApplicationRun (
445+ 'https://api.example.com' ,
446+ mockAuthService ,
447+ 'test-app:v1.0.0' ,
448+ '[]'
449+ ) ;
450+
451+ expect ( consoleSpy . error ) . toHaveBeenCalledWith (
452+ '❌ Failed to create application run:' ,
453+ expect . any ( Error )
454+ ) ;
455+ expect ( mockExit ) . toHaveBeenCalledWith ( 1 ) ;
456+ } ) ;
457+ } ) ;
345458} ) ;
0 commit comments