|
| 1 | +import List from './list.js' |
| 2 | +import {list} from '../../services/list.js' |
| 3 | +import {themeListJsonOutputSchema} from '../../services/list/types.js' |
| 4 | +import {captureStandardStreams} from '../../utilities/testing/streams.js' |
| 5 | +import {Config} from '@oclif/core' |
| 6 | +import {afterEach, expect, test, vi} from 'vitest' |
| 7 | +import {ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session' |
| 8 | +import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output' |
| 9 | + |
| 10 | +vi.mock('../../services/list.js') |
| 11 | +vi.mock('@shopify/cli-kit/node/session') |
| 12 | +vi.mock('@shopify/cli-kit/node/environments') |
| 13 | + |
| 14 | +const theme = {id: 1, name: 'Dawn', processing: false, createdAtRuntime: false, role: 'live'} |
| 15 | + |
| 16 | +afterEach(() => vi.unstubAllEnvs()) |
| 17 | + |
| 18 | +test('exposes the schema, preserves false fields and rejects invalid themes', () => { |
| 19 | + expect(List.jsonOutputSchema).toBe(themeListJsonOutputSchema) |
| 20 | + expect(List.flags.json).toBeDefined() |
| 21 | + expect(JSON.parse(themeListJsonOutputSchema.encode([theme]))).toEqual([theme]) |
| 22 | + expect(themeListJsonOutputSchema.encode([])).toBe('[]') |
| 23 | + expect(() => themeListJsonOutputSchema.validate([{...theme, id: '1'}])).toThrow() |
| 24 | + expect(() => themeListJsonOutputSchema.validate([{...theme, processing: null}])).toThrow() |
| 25 | +}) |
| 26 | + |
| 27 | +test.each(['single', 'multiple', 'partial failure', 'total failure'])( |
| 28 | + 'writes one final document to stdout for %s environments', |
| 29 | + async (mode) => { |
| 30 | + vi.stubEnv('SHOPIFY_UNIT_TEST', 'false') |
| 31 | + vi.resetModules() |
| 32 | + const {default: StreamList} = await import('./list.js') |
| 33 | + const {list: listService} = await import('../../services/list.js') |
| 34 | + const {ensureAuthenticatedThemes: authenticate} = await import('@shopify/cli-kit/node/session') |
| 35 | + const {loadEnvironment: load} = await import('@shopify/cli-kit/node/environments') |
| 36 | + const {runWithCommandEventsForCommand} = await import('@shopify/cli-kit/node/command-events') |
| 37 | + const {Config: StreamConfig} = await import('@oclif/core') |
| 38 | + const config = new StreamConfig({root: __dirname}) |
| 39 | + await config.load() |
| 40 | + vi.mocked(authenticate).mockImplementation(async (store) => ({storeFqdn: store, token: 'token'})) |
| 41 | + vi.mocked(load).mockImplementation(async (environment) => ({ |
| 42 | + store: `${environment}.myshopify.com`, |
| 43 | + password: 'token', |
| 44 | + })) |
| 45 | + vi.mocked(listService).mockImplementation(async (_flags, session) => { |
| 46 | + if (mode === 'total failure' || (mode === 'partial failure' && session.storeFqdn.startsWith('first'))) { |
| 47 | + throw new Error('Fetch failed') |
| 48 | + } |
| 49 | + // Complete in reverse order to prove that completion order does not affect output. |
| 50 | + if (session.storeFqdn.startsWith('first')) await new Promise((resolve) => setTimeout(resolve, 10)) |
| 51 | + return [theme] |
| 52 | + }) |
| 53 | + const streams = captureStandardStreams() |
| 54 | + try { |
| 55 | + const args = |
| 56 | + mode === 'single' |
| 57 | + ? ['--store=single.myshopify.com', '--password=token'] |
| 58 | + : ['--environment=first', '--environment=second'] |
| 59 | + const argv = [...args, '--json'] |
| 60 | + await runWithCommandEventsForCommand(argv, () => new StreamList(argv, config).run()) |
| 61 | + } finally { |
| 62 | + streams.restore() |
| 63 | + } |
| 64 | + let successfulEnvironments = ['first', 'second'] |
| 65 | + if (mode === 'total failure') successfulEnvironments = [] |
| 66 | + if (mode === 'partial failure') successfulEnvironments = ['second'] |
| 67 | + const expected = |
| 68 | + mode === 'single' |
| 69 | + ? [theme] |
| 70 | + : { |
| 71 | + environments: successfulEnvironments.map((environment) => ({environment, result: [theme]})), |
| 72 | + } |
| 73 | + expect(streams.stdout()).toBe(`${JSON.stringify(expected, null, 2)}\n`) |
| 74 | + expect(JSON.parse(streams.stdout())).toEqual(expected) |
| 75 | + if (mode.includes('failure')) { |
| 76 | + expect(streams.stderr()).toContain('Fetch failed') |
| 77 | + const events = streams |
| 78 | + .stderr() |
| 79 | + .trim() |
| 80 | + .split('\n') |
| 81 | + .map((line) => JSON.parse(line)) |
| 82 | + expect(events.every((event) => event.type === 'diagnostic')).toBe(true) |
| 83 | + } else expect(streams.stderr()).toBe('') |
| 84 | + }, |
| 85 | +) |
| 86 | + |
| 87 | +test('propagates a single-environment failure without producing a result', async () => { |
| 88 | + const output = mockAndCaptureOutput() |
| 89 | + const config = new Config({root: __dirname}) |
| 90 | + await config.load() |
| 91 | + vi.mocked(ensureAuthenticatedThemes).mockResolvedValue({storeFqdn: 'shop.myshopify.com', token: 'token'}) |
| 92 | + vi.mocked(list).mockRejectedValue(new Error('Fetch failed')) |
| 93 | + await expect(new List(['--store=shop.myshopify.com', '--password=token', '--json'], config).run()).rejects.toThrow( |
| 94 | + 'Fetch failed', |
| 95 | + ) |
| 96 | + expect(output.output()).toBe('') |
| 97 | +}) |
0 commit comments