|
| 1 | +import Open from './open.js' |
| 2 | +import {open} from '../../services/open.js' |
| 3 | +import {themeOpenJsonOutputSchema, type ThemeOpenResult} from '../../services/open/types.js' |
| 4 | +import {ensureThemeStore} from '../../utilities/theme-store.js' |
| 5 | +import {captureStandardStreams} from '../../utilities/testing/output.js' |
| 6 | +import {Config} from '@oclif/core' |
| 7 | +import {expect, test, vi} from 'vitest' |
| 8 | +import {ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session' |
| 9 | +import {openURL} from '@shopify/cli-kit/node/system' |
| 10 | +import {renderInfo} from '@shopify/cli-kit/node/ui' |
| 11 | + |
| 12 | +vi.mock('../../services/open.js') |
| 13 | +vi.mock('../../utilities/theme-store.js') |
| 14 | +vi.mock('@shopify/cli-kit/node/session') |
| 15 | +vi.mock('@shopify/cli-kit/node/system', async (importOriginal) => ({ |
| 16 | + ...(await importOriginal<typeof import('@shopify/cli-kit/node/system')>()), |
| 17 | + openURL: vi.fn(), |
| 18 | +})) |
| 19 | +vi.mock('@shopify/cli-kit/node/ui') |
| 20 | +vi.mock('@shopify/cli-kit/node/environments') |
| 21 | +vi.mock('@shopify/cli-kit/node/analytics') |
| 22 | +vi.mock('@shopify/cli-kit/node/metadata') |
| 23 | + |
| 24 | +const session = {token: 'token', storeFqdn: 'store.myshopify.com'} |
| 25 | +const result: ThemeOpenResult = { |
| 26 | + theme: {id: 1, name: 'my theme', role: 'live', processing: false, createdAtRuntime: false}, |
| 27 | + preview_url: 'https://store.myshopify.com?preview_theme_id=1', |
| 28 | + editor_url: 'https://store.myshopify.com/admin/themes/1/editor', |
| 29 | +} |
| 30 | + |
| 31 | +async function run(argv: string[]) { |
| 32 | + const config = new Config({root: __dirname}) |
| 33 | + await config.load() |
| 34 | + vi.mocked(ensureThemeStore).mockReturnValue(session.storeFqdn) |
| 35 | + vi.mocked(ensureAuthenticatedThemes).mockResolvedValue(session) |
| 36 | + await new Open(['--store=store.myshopify.com', ...argv], config).run() |
| 37 | +} |
| 38 | + |
| 39 | +test('exposes the result schema and JSON flag in help', () => { |
| 40 | + expect(Open.jsonOutputSchema).toBe(themeOpenJsonOutputSchema) |
| 41 | + expect(Open.flags.json).toBeDefined() |
| 42 | + expect(Open.description).toContain('ThemeOpenResult') |
| 43 | + expect(Open.description).toContain('preview_url') |
| 44 | +}) |
| 45 | + |
| 46 | +test.each([false, true])('preserves browser selection with editor=%s', async (editor) => { |
| 47 | + vi.mocked(open).mockResolvedValue(result) |
| 48 | + |
| 49 | + await run(['--theme=1', ...(editor ? ['--editor'] : [])]) |
| 50 | + |
| 51 | + expect(renderInfo).toHaveBeenCalled() |
| 52 | + expect(openURL).toHaveBeenCalledWith(editor ? result.editor_url : result.preview_url) |
| 53 | +}) |
| 54 | + |
| 55 | +test('writes one JSON result', async () => { |
| 56 | + vi.mocked(open).mockResolvedValue(result) |
| 57 | + |
| 58 | + const streams = await captureStandardStreams(() => run(['--theme=1', '--json'])) |
| 59 | + |
| 60 | + expect(JSON.parse(streams.stdout)).toEqual(result) |
| 61 | + expect(streams.stderr).toBe('') |
| 62 | + expect(renderInfo).not.toHaveBeenCalled() |
| 63 | + expect(openURL).toHaveBeenCalledWith(result.preview_url) |
| 64 | + expect(open).toHaveBeenCalledWith(session, expect.objectContaining({theme: '1', json: true})) |
| 65 | +}) |
| 66 | + |
| 67 | +test('propagates selection errors without opening the browser or rendering a result', async () => { |
| 68 | + const error = new Error('Theme not found') |
| 69 | + vi.mocked(open).mockRejectedValue(error) |
| 70 | + |
| 71 | + await expect(run(['--theme=1', '--json'])).rejects.toBe(error) |
| 72 | + |
| 73 | + expect(openURL).not.toHaveBeenCalled() |
| 74 | + expect(renderInfo).not.toHaveBeenCalled() |
| 75 | +}) |
| 76 | + |
| 77 | +test('preserves browser failures after writing the result', async () => { |
| 78 | + vi.mocked(open).mockResolvedValue(result) |
| 79 | + const error = new Error('Browser unavailable') |
| 80 | + vi.mocked(openURL).mockRejectedValue(error) |
| 81 | + |
| 82 | + const streams = await captureStandardStreams(async () => { |
| 83 | + await expect(run(['--theme=1', '--json'])).rejects.toBe(error) |
| 84 | + }) |
| 85 | + |
| 86 | + expect(JSON.parse(streams.stdout)).toEqual(result) |
| 87 | +}) |
0 commit comments