|
| 1 | +import Push from './push.js' |
| 2 | +import {executeThemePush} from '../../services/push.js' |
| 3 | +import {withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output' |
| 4 | +import {loadEnvironment} from '@shopify/cli-kit/node/environments' |
| 5 | +import {ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session' |
| 6 | +import {inTemporaryDirectory} from '@shopify/cli-kit/node/fs' |
| 7 | +import {runWithCommandEvents, renderCommandEventAsJson} from '@shopify/cli-kit/node/command-events' |
| 8 | +import {Config} from '@oclif/core' |
| 9 | +import {expect, test, vi} from 'vitest' |
| 10 | + |
| 11 | +vi.mock('../../services/push.js') |
| 12 | +vi.mock('@shopify/cli-kit/node/environments') |
| 13 | +vi.mock('@shopify/cli-kit/node/session') |
| 14 | +vi.mock('@shopify/cli-kit/node/metadata') |
| 15 | + |
| 16 | +class TestPush extends Push { |
| 17 | + public parse = vi.fn() |
| 18 | +} |
| 19 | + |
| 20 | +// Exercise real environment orchestration, presenter, encoder and streams. |
| 21 | +test.each(['none', 'partial', 'total'] as const)( |
| 22 | + 'collects environment successes in requested order with %s failures', |
| 23 | + async (failures) => { |
| 24 | + await inTemporaryDirectory(async (path) => { |
| 25 | + vi.mocked(loadEnvironment).mockImplementation(async (name) => ({ |
| 26 | + store: name === 'second' ? 'second.myshopify.com' : 'first.myshopify.com', |
| 27 | + password: 'password', |
| 28 | + path, |
| 29 | + theme: '1', |
| 30 | + })) |
| 31 | + vi.mocked(ensureAuthenticatedThemes).mockImplementation(async (store) => ({storeFqdn: store, token: 'token'})) |
| 32 | + let releaseFirst: () => void = () => {} |
| 33 | + const secondStarted = new Promise<void>((resolve) => { |
| 34 | + releaseFirst = resolve |
| 35 | + }) |
| 36 | + const executionOrder: string[] = [] |
| 37 | + vi.mocked(executeThemePush).mockImplementation(async (flags, session) => { |
| 38 | + const environment = flags.environment![0]! |
| 39 | + if (environment === 'first') await secondStarted |
| 40 | + if (environment === 'second') releaseFirst() |
| 41 | + executionOrder.push(environment) |
| 42 | + if (failures === 'total' || (failures === 'partial' && environment === 'second')) |
| 43 | + throw new Error('upload failed') |
| 44 | + return { |
| 45 | + environment, |
| 46 | + theme: { |
| 47 | + id: 1, |
| 48 | + name: environment, |
| 49 | + role: 'unpublished', |
| 50 | + shop: session!.storeFqdn, |
| 51 | + editor_url: 'editor', |
| 52 | + preview_url: 'preview', |
| 53 | + }, |
| 54 | + published: false, |
| 55 | + hasErrors: false, |
| 56 | + errors: {}, |
| 57 | + } |
| 58 | + }) |
| 59 | + const command = new TestPush([], new Config({root: path})) |
| 60 | + const flags = {json: true, force: true, environment: ['first', 'second', 'third']} |
| 61 | + vi.spyOn(command, 'parse').mockResolvedValue({flags, args: {}} as never) |
| 62 | + const exitCode = process.exitCode |
| 63 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 64 | + await runWithCommandEvents({outputMode: 'json', sink: renderCommandEventAsJson}, () => command.run()) |
| 65 | + |
| 66 | + const results = JSON.parse(stdout()) |
| 67 | + expect(results.map((result: {environment: string}) => result.environment)).toEqual( |
| 68 | + failures === 'total' ? [] : ['first', ...(failures === 'none' ? ['second'] : []), 'third'], |
| 69 | + ) |
| 70 | + expect(executionOrder).toEqual(['second', 'first', 'third']) |
| 71 | + expect(process.exitCode).toBe(exitCode) |
| 72 | + const events = stderr().trim() |
| 73 | + ? stderr() |
| 74 | + .trim() |
| 75 | + .split('\n') |
| 76 | + .filter(Boolean) |
| 77 | + .map((line) => JSON.parse(line)) |
| 78 | + : [] |
| 79 | + expect(events.filter((event) => event.level === 'error')).toHaveLength( |
| 80 | + {none: 0, partial: 1, total: 3}[failures], |
| 81 | + ) |
| 82 | + }) |
| 83 | + }) |
| 84 | + }, |
| 85 | +) |
| 86 | + |
| 87 | +test('returns an empty array when every environment is invalid', async () => { |
| 88 | + vi.mocked(loadEnvironment).mockResolvedValue({}) |
| 89 | + const command = new TestPush([], new Config({root: '.'})) |
| 90 | + vi.spyOn(command, 'parse').mockResolvedValue({ |
| 91 | + flags: {json: true, force: true, environment: ['first', 'second']}, |
| 92 | + args: {}, |
| 93 | + } as never) |
| 94 | + await withCapturedStandardStreams(async ({stdout, stderr}) => { |
| 95 | + await runWithCommandEvents({outputMode: 'json', sink: renderCommandEventAsJson}, () => command.run()) |
| 96 | + |
| 97 | + expect(JSON.parse(stdout())).toEqual([]) |
| 98 | + expect(executeThemePush).not.toHaveBeenCalled() |
| 99 | + expect( |
| 100 | + stderr() |
| 101 | + .trim() |
| 102 | + .split('\n') |
| 103 | + .map((line) => JSON.parse(line)), |
| 104 | + ).toEqual([ |
| 105 | + expect.objectContaining({type: 'diagnostic', level: 'warning'}), |
| 106 | + expect.objectContaining({type: 'diagnostic', level: 'warning'}), |
| 107 | + ]) |
| 108 | + }) |
| 109 | +}) |
0 commit comments