@@ -7,13 +7,19 @@ import {
77 versionDeepLink ,
88} from './app-management-client.js'
99import { OrganizationBetaFlagsQuerySchema } from './app-management-client/graphql/organization_beta_flags.js'
10- import { testUIExtension , testRemoteExtensionTemplates , testOrganizationApp } from '../../models/app/app.test-data.js'
10+ import {
11+ testUIExtension ,
12+ testRemoteExtensionTemplates ,
13+ testOrganizationApp ,
14+ testOrganization ,
15+ } from '../../models/app/app.test-data.js'
1116import { ExtensionInstance } from '../../models/extensions/extension-instance.js'
1217import { ListApps } from '../../api/graphql/app-management/generated/apps.js'
1318import { PublicApiVersionsQuery } from '../../api/graphql/webhooks/generated/public-api-versions.js'
1419import { AvailableTopicsQuery } from '../../api/graphql/webhooks/generated/available-topics.js'
1520import { CliTesting , CliTestingMutation } from '../../api/graphql/webhooks/generated/cli-testing.js'
1621import { SendSampleWebhookVariables } from '../../services/webhook/request-sample.js'
22+ import { CreateApp } from '../../api/graphql/app-management/generated/create-app.js'
1723import { describe , expect , test , vi } from 'vitest'
1824import { CLI_KIT_VERSION } from '@shopify/cli-kit/common/version'
1925import { fetch } from '@shopify/cli-kit/node/http'
@@ -295,6 +301,83 @@ describe('searching for apps', () => {
295301 } )
296302} )
297303
304+ describe ( 'createApp' , ( ) => {
305+ test ( 'fetches latest stable API version for webhooks module' , async ( ) => {
306+ // Given
307+ const client = new AppManagementClient ( )
308+ const org = testOrganization ( )
309+ const mockedApiVersionResult : PublicApiVersionsQuery = {
310+ publicApiVersions : [ { handle : '2024-07' } , { handle : '2024-10' } , { handle : '2025-01' } , { handle : 'unstable' } ] ,
311+ }
312+ vi . mocked ( webhooksRequest ) . mockResolvedValueOnce ( mockedApiVersionResult )
313+ vi . mocked ( appManagementRequestDoc ) . mockResolvedValueOnce ( { appCreate : { app : { id : '1' , key : 'key' } , userErrors : [ ] } } )
314+
315+ // When
316+ client . token = ( ) => Promise . resolve ( 'token' )
317+ await client . createApp ( org , 'app-name' )
318+
319+ // Then
320+ expect ( webhooksRequest ) . toHaveBeenCalledWith ( org . id , expect . anything ( ) , 'token' , expect . any ( Object ) )
321+ expect ( appManagementRequestDoc ) . toHaveBeenCalledWith (
322+ org . id ,
323+ CreateApp ,
324+ 'token' ,
325+ expect . objectContaining ( {
326+ appSource : {
327+ appModules : expect . arrayContaining ( [
328+ {
329+ config : {
330+ api_version : '2025-01' ,
331+ } ,
332+ specificationIdentifier : 'webhooks' ,
333+ uid : 'webhooks' ,
334+ } ,
335+ ] ) ,
336+ } ,
337+ } ) ,
338+ )
339+ } )
340+
341+ test ( 'creates app successfully and returns expected app structure' , async ( ) => {
342+ // Given
343+ const appName = 'app-name'
344+ const client = new AppManagementClient ( )
345+ const org = testOrganization ( )
346+ const expectedApp = {
347+ id : '1' ,
348+ key : 'api-key' ,
349+ apiKey : 'api-key' ,
350+ apiSecretKeys : [ ] ,
351+ flags : [ ] ,
352+ grantedScopes : [ ] ,
353+ organizationId : '1' ,
354+ title : appName ,
355+ newApp : true ,
356+ developerPlatformClient : expect . any ( AppManagementClient ) ,
357+ }
358+
359+ vi . mocked ( webhooksRequest ) . mockResolvedValueOnce ( {
360+ publicApiVersions : [ { handle : '2024-07' } , { handle : '2024-10' } , { handle : '2025-01' } , { handle : 'unstable' } ] ,
361+ } )
362+ vi . mocked ( appManagementRequestDoc ) . mockResolvedValueOnce ( {
363+ appCreate : {
364+ app : {
365+ id : expectedApp . id ,
366+ key : expectedApp . key ,
367+ } ,
368+ userErrors : [ ] ,
369+ } ,
370+ } )
371+
372+ // When
373+ client . token = ( ) => Promise . resolve ( 'token' )
374+ const result = await client . createApp ( org , appName )
375+
376+ // Then
377+ expect ( result ) . toMatchObject ( expectedApp )
378+ } )
379+ } )
380+
298381describe ( 'apiVersions' , ( ) => {
299382 test ( 'fetches available public API versions' , async ( ) => {
300383 // Given
0 commit comments