@@ -8,6 +8,8 @@ import {AbortError} from '@shopify/cli-kit/node/error'
88import { fileExists , fileRealPath , inTemporaryDirectory } from '@shopify/cli-kit/node/fs'
99import { unstyled } from '@shopify/cli-kit/node/output'
1010import { joinPath , normalizePath } from '@shopify/cli-kit/node/path'
11+ import { renderSelectPrompt } from '@shopify/cli-kit/node/ui'
12+ import clipboard from 'clipboardy'
1113import { describe , expect , test , vi } from 'vitest'
1214import { mkdir , readFile , writeFile } from 'node:fs/promises'
1315// eslint-disable-next-line n/prefer-global/console
@@ -33,6 +35,13 @@ vi.mock('../../../services/context.js', async (importOriginal) => ({
3335 ...( await importOriginal < typeof import ( '../../../services/context.js' ) > ( ) ) ,
3436 appFromIdentifiers : vi . fn ( ) ,
3537} ) )
38+ // Tests answer the prompts. How a real terminal draws them is checked by hand.
39+ vi . mock ( '@shopify/cli-kit/node/ui' , async ( importOriginal ) => ( {
40+ ...( await importOriginal < typeof import ( '@shopify/cli-kit/node/ui' ) > ( ) ) ,
41+ renderSelectPrompt : vi . fn ( ) ,
42+ } ) )
43+ // Tests must not replace the clipboard of the machine that runs them.
44+ vi . mock ( 'clipboardy' , ( ) => ( { default : { write : vi . fn ( ) } } ) )
3645
3746async function createApp ( directory : string ) : Promise < { nestedDirectory : string } > {
3847 const routesDirectory = joinPath ( directory , 'app' , 'routes' )
@@ -47,6 +56,13 @@ async function createApp(directory: string): Promise<{nestedDirectory: string}>
4756 return { nestedDirectory : routesDirectory }
4857}
4958
59+ /** An app whose source holds a Shopify access token, which the deterministic scan reports as a high finding. */
60+ async function createAppWithSecret ( directory : string ) : Promise < void > {
61+ await createApp ( directory )
62+ const testToken = [ 'shpat' , '0123456789abcdef0123456789abcdef' ] . join ( '_' )
63+ await writeFile ( joinPath ( directory , 'app' , 'routes' , 'index.ts' ) , `const access_token = "${ testToken } "\n` )
64+ }
65+
5066async function readJson ( path : string ) : Promise < unknown > {
5167 return JSON . parse ( await readFile ( path , 'utf8' ) )
5268}
@@ -96,6 +112,31 @@ async function runCommand(argv: string[]) {
96112 }
97113}
98114
115+ /**
116+ * Runs the command as if stdin and stderr were a terminal, where it prompts unless `--no-input` is passed. CLI Kit reads
117+ * `--no-input` from the process arguments, not from the arguments given to `run`, so the environment sets it as well.
118+ */
119+ async function runInTerminal ( argv : string [ ] , { noInput = false } = { } ) {
120+ vi . stubEnv ( 'CI' , '' )
121+ if ( noInput ) vi . stubEnv ( 'SHOPIFY_FLAG_NO_INPUT' , '1' )
122+ const streams = [ process . stdin , process . stderr ]
123+ const descriptors = streams . map ( ( stream ) => Object . getOwnPropertyDescriptor ( stream , 'isTTY' ) )
124+ for ( const stream of streams ) Object . defineProperty ( stream , 'isTTY' , { value : true , configurable : true } )
125+ try {
126+ return await runCommand ( noInput ? [ ...argv , '--no-input' ] : argv )
127+ } finally {
128+ streams . forEach ( ( stream , index ) => {
129+ const descriptor = descriptors [ index ]
130+ if ( descriptor ) {
131+ Object . defineProperty ( stream , 'isTTY' , descriptor )
132+ } else {
133+ Reflect . deleteProperty ( stream , 'isTTY' )
134+ }
135+ } )
136+ vi . unstubAllEnvs ( )
137+ }
138+ }
139+
99140describe ( 'app security check command boundary' , ( ) => {
100141 test ( 'scans an app from a nested directory and writes deterministic-findings.json and agent-checks.json' , async ( ) => {
101142 await inTemporaryDirectory ( async ( directory ) => {
@@ -336,6 +377,32 @@ describe('app security check command boundary', () => {
336377 } )
337378 } )
338379
380+ test . each ( [
381+ { blocking : 'none' , exitCode : 0 } ,
382+ { blocking : 'high' , exitCode : 1 } ,
383+ ] ) (
384+ 'warns when the instructions cannot be copied, after the report, and exits as --blocking $blocking requires' ,
385+ async ( { blocking, exitCode} ) => {
386+ await inTemporaryDirectory ( async ( directory ) => {
387+ await createAppWithSecret ( directory )
388+ vi . mocked ( renderSelectPrompt ) . mockResolvedValue ( 'copy' )
389+ vi . mocked ( clipboard . write ) . mockRejectedValue ( new Error ( 'Command failed with ENOENT: pbcopy' ) )
390+
391+ const result = await runInTerminal ( [ '--path' , directory , '--blocking' , blocking ] )
392+
393+ expect ( result . exitCode ) . toBe ( exitCode )
394+ expect ( result . stdout ) . toBe ( '' )
395+ const stderr = boxText ( result . stderr )
396+ expect ( stderr ) . toContain (
397+ "Couldn't copy the app security check instructions to the clipboard: Command failed with ENOENT: pbcopy" ,
398+ )
399+ expect ( stderr ) . toContain ( 'Run the check again with `--print-instructions` to print them.' )
400+ expect ( stderr . indexOf ( 'Config file shopify.app.toml' ) ) . toBeLessThan ( stderr . indexOf ( "Couldn't copy" ) )
401+ expect ( clipboard . write ) . toHaveBeenCalledOnce ( )
402+ } )
403+ } ,
404+ )
405+
339406 test ( 'looks up --client-id, not the TOML client ID' , async ( ) => {
340407 await inTemporaryDirectory ( async ( directory ) => {
341408 await createApp ( directory )
0 commit comments