@@ -5,13 +5,15 @@ import {AppLinkedInterface} from '../../models/app/app.js'
55import { OrganizationApp } from '../../models/organization.js'
66import { renderSuccess , renderWarning } from '@shopify/cli-kit/node/ui'
77import { ensureAuthenticatedAdminAsApp } from '@shopify/cli-kit/node/session'
8+ import { readStdin } from '@shopify/cli-kit/node/system'
89import { inTemporaryDirectory , writeFile } from '@shopify/cli-kit/node/fs'
910import { joinPath } from '@shopify/cli-kit/node/path'
1011import { describe , test , expect , vi , beforeEach } from 'vitest'
1112
1213vi . mock ( './run-query.js' )
1314vi . mock ( './run-mutation.js' )
1415vi . mock ( '@shopify/cli-kit/node/ui' )
16+ vi . mock ( '@shopify/cli-kit/node/system' )
1517vi . mock ( '@shopify/cli-kit/node/session' , async ( ) => {
1618 const actual = await vi . importActual ( '@shopify/cli-kit/node/session' )
1719 return {
@@ -335,4 +337,67 @@ describe('executeBulkOperation', () => {
335337 expect ( runBulkOperationMutation ) . not . toHaveBeenCalled ( )
336338 } )
337339 } )
340+
341+ test ( 'reads query from stdin when query flag is not provided' , async ( ) => {
342+ const stdinQuery = '{ products { edges { node { id } } } }'
343+ vi . mocked ( readStdin ) . mockResolvedValue ( stdinQuery )
344+
345+ const mockResponse = {
346+ bulkOperation : successfulBulkOperation ,
347+ userErrors : [ ] ,
348+ }
349+ vi . mocked ( runBulkOperationQuery ) . mockResolvedValue ( mockResponse as any )
350+
351+ await executeBulkOperation ( {
352+ app : mockApp ,
353+ remoteApp : mockRemoteApp ,
354+ storeFqdn,
355+ } )
356+
357+ expect ( readStdin ) . toHaveBeenCalled ( )
358+ expect ( runBulkOperationQuery ) . toHaveBeenCalledWith ( {
359+ adminSession : mockAdminSession ,
360+ query : stdinQuery ,
361+ } )
362+ } )
363+
364+ test ( 'prefers query flag over stdin when both are available' , async ( ) => {
365+ const flagQuery = 'query { orders { edges { node { id } } } }'
366+ const stdinQuery = '{ products { edges { node { id } } } }'
367+ vi . mocked ( readStdin ) . mockResolvedValue ( stdinQuery )
368+
369+ const mockResponse = {
370+ bulkOperation : successfulBulkOperation ,
371+ userErrors : [ ] ,
372+ }
373+ vi . mocked ( runBulkOperationQuery ) . mockResolvedValue ( mockResponse as any )
374+
375+ await executeBulkOperation ( {
376+ app : mockApp ,
377+ remoteApp : mockRemoteApp ,
378+ storeFqdn,
379+ query : flagQuery ,
380+ } )
381+
382+ expect ( readStdin ) . not . toHaveBeenCalled ( )
383+ expect ( runBulkOperationQuery ) . toHaveBeenCalledWith ( {
384+ adminSession : mockAdminSession ,
385+ query : flagQuery ,
386+ } )
387+ } )
388+
389+ test ( 'throws error when no query is provided and stdin is empty' , async ( ) => {
390+ vi . mocked ( readStdin ) . mockResolvedValue ( undefined )
391+
392+ await expect (
393+ executeBulkOperation ( {
394+ app : mockApp ,
395+ remoteApp : mockRemoteApp ,
396+ storeFqdn,
397+ } ) ,
398+ ) . rejects . toThrow ( 'No query provided' )
399+
400+ expect ( runBulkOperationQuery ) . not . toHaveBeenCalled ( )
401+ expect ( runBulkOperationMutation ) . not . toHaveBeenCalled ( )
402+ } )
338403} )
0 commit comments