|
| 1 | +import {testAppLinked, testFunctionExtension} from '../../../models/app/app.test-data.js' |
| 2 | +import {AppErrors} from '../../../models/app/loader.js' |
| 3 | +import {Config} from '@oclif/core' |
| 4 | +import {afterEach, expect, test, vi} from 'vitest' |
| 5 | +import {formatSection} from '@shopify/cli-kit/node/output' |
| 6 | +import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output' |
| 7 | +// eslint-disable-next-line n/prefer-global/console |
| 8 | +import {Console} from 'node:console' |
| 9 | + |
| 10 | +vi.mock('../../../services/app-context.js') |
| 11 | + |
| 12 | +const originalUnitTestEnvironment = process.env.SHOPIFY_UNIT_TEST |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + if (originalUnitTestEnvironment === undefined) { |
| 16 | + delete process.env.SHOPIFY_UNIT_TEST |
| 17 | + } else { |
| 18 | + process.env.SHOPIFY_UNIT_TEST = originalUnitTestEnvironment |
| 19 | + } |
| 20 | + mockAndCaptureOutput().clear() |
| 21 | + vi.resetModules() |
| 22 | +}) |
| 23 | + |
| 24 | +// Captures the real standard streams so JSON and text output are proven at the process boundary. |
| 25 | +function captureStandardStreams() { |
| 26 | + const stdout: string[] = [] |
| 27 | + const stderr: string[] = [] |
| 28 | + |
| 29 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: string | Uint8Array) => { |
| 30 | + stdout.push(typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8')) |
| 31 | + return true |
| 32 | + }) as typeof process.stdout.write) |
| 33 | + const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(((chunk: string | Uint8Array) => { |
| 34 | + stderr.push(typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8')) |
| 35 | + return true |
| 36 | + }) as typeof process.stderr.write) |
| 37 | + // Vitest intercepts console.warn; use Node's console to exercise the captured streams. |
| 38 | + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(new Console(process.stdout, process.stderr).warn) |
| 39 | + |
| 40 | + return { |
| 41 | + stdout: () => stdout.join(''), |
| 42 | + stderr: () => stderr.join(''), |
| 43 | + restore: () => { |
| 44 | + warnSpy.mockRestore() |
| 45 | + stdoutSpy.mockRestore() |
| 46 | + stderrSpy.mockRestore() |
| 47 | + }, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +async function runCommand(argv: string[], app = testAppLinked()) { |
| 52 | + const {linkedAppContext} = await import('../../../services/app-context.js') |
| 53 | + vi.mocked(linkedAppContext).mockResolvedValue({app} as Awaited<ReturnType<typeof linkedAppContext>>) |
| 54 | + const {default: Sources} = await import('./sources.js') |
| 55 | + return new Sources(argv, await Config.load()).run() |
| 56 | +} |
| 57 | + |
| 58 | +test('writes one JSON document to stdout without terminal text', async () => { |
| 59 | + process.env.SHOPIFY_UNIT_TEST = 'false' |
| 60 | + vi.resetModules() |
| 61 | + const app = testAppLinked({ |
| 62 | + allExtensions: [ |
| 63 | + await testFunctionExtension({config: {...(await testFunctionExtension()).configuration, handle: 'discount'}}), |
| 64 | + ], |
| 65 | + }) |
| 66 | + const streams = captureStandardStreams() |
| 67 | + try { |
| 68 | + await expect(runCommand(['--json'], app)).resolves.toEqual({app}) |
| 69 | + } finally { |
| 70 | + streams.restore() |
| 71 | + } |
| 72 | + const extension = app.allExtensions[0]! |
| 73 | + expect(JSON.parse(streams.stdout())).toEqual([ |
| 74 | + { |
| 75 | + source: 'extensions.discount', |
| 76 | + namespace: 'extensions', |
| 77 | + handle: 'discount', |
| 78 | + name: extension.configuration.name, |
| 79 | + type: extension.type, |
| 80 | + externalType: extension.externalType, |
| 81 | + humanName: extension.humanName, |
| 82 | + uid: extension.uid, |
| 83 | + directory: extension.directory, |
| 84 | + configurationPath: extension.configurationPath, |
| 85 | + configuration: extension.configuration, |
| 86 | + entrySourceFilePath: extension.entrySourceFilePath, |
| 87 | + outputPath: extension.outputPath, |
| 88 | + surface: extension.surface, |
| 89 | + features: extension.features, |
| 90 | + ...(extension.dependency === undefined ? {} : {dependency: extension.dependency}), |
| 91 | + }, |
| 92 | + ]) |
| 93 | + expect(streams.stderr()).toBe('') |
| 94 | +}) |
| 95 | + |
| 96 | +test('writes an empty JSON array when no sources exist', async () => { |
| 97 | + process.env.SHOPIFY_UNIT_TEST = 'false' |
| 98 | + vi.resetModules() |
| 99 | + const streams = captureStandardStreams() |
| 100 | + try { |
| 101 | + await runCommand(['--json']) |
| 102 | + } finally { |
| 103 | + streams.restore() |
| 104 | + } |
| 105 | + expect(streams.stdout()).toBe('[]\n') |
| 106 | + expect(streams.stderr()).toBe('') |
| 107 | +}) |
| 108 | + |
| 109 | +test('keeps namespace sections on stdout in text mode', async () => { |
| 110 | + process.env.SHOPIFY_UNIT_TEST = 'false' |
| 111 | + vi.resetModules() |
| 112 | + const app = testAppLinked({ |
| 113 | + allExtensions: [ |
| 114 | + await testFunctionExtension({config: {...(await testFunctionExtension()).configuration, handle: 'discount'}}), |
| 115 | + ], |
| 116 | + }) |
| 117 | + const streams = captureStandardStreams() |
| 118 | + try { |
| 119 | + await runCommand([], app) |
| 120 | + } finally { |
| 121 | + streams.restore() |
| 122 | + } |
| 123 | + expect(streams.stdout()).toBe(`${formatSection('extensions', 'extensions.discount')}\n`) |
| 124 | + expect(streams.stderr()).toBe('') |
| 125 | +}) |
| 126 | + |
| 127 | +test.each([{argv: []}, {argv: ['--json']}])( |
| 128 | + 'exits with status 2 before output for invalid apps (%j)', |
| 129 | + async ({argv}) => { |
| 130 | + const errors = new AppErrors() |
| 131 | + errors.addError({file: 'shopify.app.toml', message: 'Invalid app'}) |
| 132 | + const exit = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 133 | + throw new Error('exit') |
| 134 | + }) |
| 135 | + try { |
| 136 | + await expect(runCommand(argv, testAppLinked({errors}))).rejects.toThrow('exit') |
| 137 | + expect(exit).toHaveBeenCalledWith(2) |
| 138 | + expect(mockAndCaptureOutput().output()).toBe('') |
| 139 | + } finally { |
| 140 | + exit.mockRestore() |
| 141 | + } |
| 142 | + }, |
| 143 | +) |
| 144 | + |
| 145 | +test('exposes the schema in help', async () => { |
| 146 | + const {default: Sources} = await import('./sources.js') |
| 147 | + const {appLogSourcesJsonOutputSchema} = await import('../../../services/app-logs/sources/types.js') |
| 148 | + expect(Sources.jsonOutputSchema).toBe(appLogSourcesJsonOutputSchema) |
| 149 | + expect(Sources.descriptionForHelp()).toContain('`AppLogSourcesResult` schema') |
| 150 | +}) |
0 commit comments