|
| 1 | +import Duplicate from './duplicate.js' |
| 2 | +import {themeDuplicateJsonOutputSchema} from '../../services/duplicate/types.js' |
| 3 | +import {findThemeById} from '../../utilities/theme-selector.js' |
| 4 | +import {Config} from '@oclif/core' |
| 5 | +import {ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session' |
| 6 | +import {themeDuplicate} from '@shopify/cli-kit/node/themes/api' |
| 7 | +import {withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output' |
| 8 | +import {outputWarn} from '@shopify/cli-kit/node/output' |
| 9 | +import {runWithCommandEventsForCommand} from '@shopify/cli-kit/node/command-events' |
| 10 | +import {describe, expect, test, vi} from 'vitest' |
| 11 | + |
| 12 | +vi.mock('@shopify/cli-kit/node/session') |
| 13 | +vi.mock('@shopify/cli-kit/node/themes/api') |
| 14 | +vi.mock('../../utilities/theme-selector.js') |
| 15 | + |
| 16 | +const originalTheme = {id: 1, name: 'Original', role: 'unpublished', processing: false, createdAtRuntime: false} |
| 17 | +const copiedTheme = {...originalTheme, id: 2, name: 'Copy'} |
| 18 | +const session = {token: 'token', storeFqdn: 'test.myshopify.com'} |
| 19 | + |
| 20 | +async function run() { |
| 21 | + const config = new Config({root: __dirname}) |
| 22 | + await config.load() |
| 23 | + vi.mocked(ensureAuthenticatedThemes).mockResolvedValue(session) |
| 24 | + const argv = ['--store', session.storeFqdn, '--theme', '1', '--force', '--json'] |
| 25 | + await runWithCommandEventsForCommand(argv, () => new Duplicate(argv, config).run()) |
| 26 | +} |
| 27 | + |
| 28 | +describe('theme duplicate JSON output', () => { |
| 29 | + test('exposes its schema in help and keeps the JSON flag', () => { |
| 30 | + expect(Duplicate.jsonOutputSchema).toBe(themeDuplicateJsonOutputSchema) |
| 31 | + expect(Duplicate.flags.json).toBeDefined() |
| 32 | + expect(Duplicate.description).toContain('ThemeDuplicateResult') |
| 33 | + }) |
| 34 | + |
| 35 | + test('writes the exact legacy document and routes diagnostics to stderr', async () => { |
| 36 | + vi.mocked(findThemeById).mockResolvedValue(originalTheme) |
| 37 | + vi.mocked(themeDuplicate).mockImplementation(async () => { |
| 38 | + outputWarn('Retrying request') |
| 39 | + return {theme: copiedTheme, userErrors: [], requestId: 'omitted-on-success'} |
| 40 | + }) |
| 41 | + |
| 42 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 43 | + await run() |
| 44 | + expect(stdout()).toBe('{"theme":{"id":2,"name":"Copy","role":"unpublished","shop":"test.myshopify.com"}}\n') |
| 45 | + expect(JSON.parse(stderr())).toMatchObject({type: 'diagnostic', level: 'warning', message: 'Retrying request'}) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + test.each([undefined, '', 'request-123'])('preserves errors and request ID omission (%s)', async (requestId) => { |
| 50 | + vi.mocked(findThemeById).mockResolvedValue(originalTheme) |
| 51 | + vi.mocked(themeDuplicate).mockResolvedValue({userErrors: [{message: 'Limit reached'}], requestId}) |
| 52 | + const exitCode = process.exitCode |
| 53 | + |
| 54 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 55 | + await run() |
| 56 | + expect(stdout()).toBe( |
| 57 | + `${JSON.stringify({message: "The theme 'Original' could not be duplicated due to errors", errors: ['Limit reached'], requestId})}\n`, |
| 58 | + ) |
| 59 | + expect(stderr()).toBe('') |
| 60 | + expect(process.exitCode).toBe(exitCode) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + test('keeps the trailing space in unexpected failure messages', async () => { |
| 65 | + vi.mocked(findThemeById).mockResolvedValue(originalTheme) |
| 66 | + vi.mocked(themeDuplicate).mockResolvedValue({userErrors: []}) |
| 67 | + await withCapturedStandardStreams(async ({stdout}) => { |
| 68 | + await run() |
| 69 | + expect(stdout()).toBe('{"message":"The theme \'Original\' unexpectedly could not be duplicated ","errors":[]}\n') |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + test('does not write a result when the API throws', async () => { |
| 74 | + vi.mocked(findThemeById).mockResolvedValue(originalTheme) |
| 75 | + vi.mocked(themeDuplicate).mockRejectedValue(new Error('Network failure')) |
| 76 | + await withCapturedStandardStreams(async ({stdout}) => { |
| 77 | + await expect(run()).rejects.toThrow('Network failure') |
| 78 | + expect(stdout()).toBe('') |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + test.each([ |
| 83 | + {theme: {id: '2', name: 'Copy', role: 'unpublished', shop: session.storeFqdn}}, |
| 84 | + {theme: {id: 2, name: null, role: 'unpublished', shop: session.storeFqdn}}, |
| 85 | + {message: 'Failed', errors: [1]}, |
| 86 | + {message: 'Failed', errors: [], requestId: null}, |
| 87 | + ])('rejects malformed public results %#', (result) => { |
| 88 | + expect(() => themeDuplicateJsonOutputSchema.validate(result)).toThrow() |
| 89 | + }) |
| 90 | +}) |
0 commit comments