|
| 1 | +import StoreDelete from './delete.js' |
| 2 | +import {deleteDevStore} from '../../services/store/delete/dev.js' |
| 3 | +import {fetchDestinationsContext} from '../../services/store/info/destinations.js' |
| 4 | +import {selectOrg} from '@shopify/organizations' |
| 5 | +import {AbortError} from '@shopify/cli-kit/node/error' |
| 6 | +import {outputResult} from '@shopify/cli-kit/node/output' |
| 7 | +import {terminalSupportsPrompting} from '@shopify/cli-kit/node/system' |
| 8 | +import {describe, expect, test, vi, beforeEach} from 'vitest' |
| 9 | + |
| 10 | +vi.mock('../../services/store/delete/dev.js') |
| 11 | +vi.mock('../../services/store/info/destinations.js') |
| 12 | +vi.mock('@shopify/cli-kit/node/system') |
| 13 | + |
| 14 | +vi.mock('@shopify/organizations', () => ({ |
| 15 | + selectOrg: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@shopify/cli-kit/node/output', async (importOriginal) => { |
| 19 | + const actual: Record<string, unknown> = await importOriginal() |
| 20 | + return { |
| 21 | + ...actual, |
| 22 | + outputResult: vi.fn(), |
| 23 | + } |
| 24 | +}) |
| 25 | + |
| 26 | +const defaultOrg = {id: '12345', businessName: 'Test Org'} |
| 27 | +const inferredOrg = {id: '67890', businessName: 'Inferred Org'} |
| 28 | + |
| 29 | +beforeEach(() => { |
| 30 | + vi.mocked(selectOrg).mockResolvedValue(defaultOrg) |
| 31 | + vi.mocked(fetchDestinationsContext).mockResolvedValue({ |
| 32 | + owningOrg: {id: inferredOrg.id, name: inferredOrg.businessName}, |
| 33 | + }) |
| 34 | + vi.mocked(terminalSupportsPrompting).mockReturnValue(true) |
| 35 | +}) |
| 36 | + |
| 37 | +describe('store delete command', () => { |
| 38 | + test('resolves the organization and passes parsed flags through to the service', async () => { |
| 39 | + await StoreDelete.run(['--store', 'my-store.myshopify.com', '--organization-id', '12345']) |
| 40 | + |
| 41 | + expect(selectOrg).toHaveBeenCalledWith('12345') |
| 42 | + expect(deleteDevStore).toHaveBeenCalledWith({ |
| 43 | + store: 'my-store.myshopify.com', |
| 44 | + organization: defaultOrg, |
| 45 | + json: false, |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + test('normalizes the store flag before passing it to the service', async () => { |
| 50 | + await StoreDelete.run(['--store', 'my-store', '--organization-id', '12345']) |
| 51 | + |
| 52 | + expect(deleteDevStore).toHaveBeenCalledWith(expect.objectContaining({store: 'my-store.myshopify.com'})) |
| 53 | + }) |
| 54 | + |
| 55 | + test('passes json flag through to the service', async () => { |
| 56 | + await StoreDelete.run(['--store', 'my-store.myshopify.com', '--json', '--organization-id', '12345']) |
| 57 | + |
| 58 | + expect(deleteDevStore).toHaveBeenCalledWith({ |
| 59 | + store: 'my-store.myshopify.com', |
| 60 | + organization: defaultOrg, |
| 61 | + json: true, |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + test('infers the organization from the store when --organization-id is omitted', async () => { |
| 66 | + await StoreDelete.run(['--store', 'my-store.myshopify.com']) |
| 67 | + |
| 68 | + expect(fetchDestinationsContext).toHaveBeenCalledWith({store: 'my-store.myshopify.com'}) |
| 69 | + expect(selectOrg).not.toHaveBeenCalled() |
| 70 | + expect(deleteDevStore).toHaveBeenCalledWith(expect.objectContaining({organization: inferredOrg})) |
| 71 | + }) |
| 72 | + |
| 73 | + test('falls back to prompting for the organization when store ownership cannot be inferred interactively', async () => { |
| 74 | + vi.mocked(fetchDestinationsContext).mockResolvedValueOnce({}) |
| 75 | + |
| 76 | + await StoreDelete.run(['--store', 'my-store.myshopify.com']) |
| 77 | + |
| 78 | + expect(selectOrg).toHaveBeenCalledWith() |
| 79 | + expect(deleteDevStore).toHaveBeenCalledWith(expect.objectContaining({organization: defaultOrg})) |
| 80 | + }) |
| 81 | + |
| 82 | + test('does not require --organization-id non-interactively when store ownership can be inferred', async () => { |
| 83 | + vi.mocked(terminalSupportsPrompting).mockReturnValue(false) |
| 84 | + |
| 85 | + await StoreDelete.run(['--store', 'my-store.myshopify.com']) |
| 86 | + |
| 87 | + expect(fetchDestinationsContext).toHaveBeenCalledWith({store: 'my-store.myshopify.com', noPrompt: true}) |
| 88 | + expect(selectOrg).not.toHaveBeenCalled() |
| 89 | + expect(deleteDevStore).toHaveBeenCalledWith(expect.objectContaining({organization: inferredOrg})) |
| 90 | + }) |
| 91 | + |
| 92 | + test('fails in a non-interactive environment when store ownership cannot be inferred and --organization-id is missing', async () => { |
| 93 | + vi.mocked(terminalSupportsPrompting).mockReturnValue(false) |
| 94 | + vi.mocked(fetchDestinationsContext).mockResolvedValueOnce({}) |
| 95 | + const mockExit = vi.spyOn(process, 'exit').mockImplementation((() => { |
| 96 | + throw new Error('process.exit') |
| 97 | + }) as never) |
| 98 | + |
| 99 | + await expect(StoreDelete.run(['--store', 'my-store.myshopify.com'])).rejects.toThrow('process.exit') |
| 100 | + expect(selectOrg).not.toHaveBeenCalled() |
| 101 | + expect(deleteDevStore).not.toHaveBeenCalled() |
| 102 | + expect(mockExit).toHaveBeenCalledWith(1) |
| 103 | + |
| 104 | + mockExit.mockRestore() |
| 105 | + }) |
| 106 | + |
| 107 | + test('defines the expected flags', () => { |
| 108 | + expect(StoreDelete.flags.store).toBeDefined() |
| 109 | + expect(StoreDelete.flags['organization-id']).toBeDefined() |
| 110 | + expect(StoreDelete.flags.json).toBeDefined() |
| 111 | + }) |
| 112 | + |
| 113 | + test('outputs structured JSON error when --json is active and service throws AbortError', async () => { |
| 114 | + vi.mocked(deleteDevStore).mockRejectedValueOnce(new AbortError('Something went wrong')) |
| 115 | + const mockExit = vi.spyOn(process, 'exit').mockImplementation((() => { |
| 116 | + throw new Error('process.exit') |
| 117 | + }) as never) |
| 118 | + |
| 119 | + await expect( |
| 120 | + StoreDelete.run(['--store', 'my-store.myshopify.com', '--organization-id', '12345', '--json']), |
| 121 | + ).rejects.toThrow('process.exit') |
| 122 | + |
| 123 | + const call = vi.mocked(outputResult).mock.calls[0]![0] as string |
| 124 | + const parsed = JSON.parse(call) |
| 125 | + expect(parsed).toEqual({ |
| 126 | + error: true, |
| 127 | + message: 'Something went wrong', |
| 128 | + nextSteps: [], |
| 129 | + exitCode: 1, |
| 130 | + }) |
| 131 | + expect(mockExit).toHaveBeenCalledWith(1) |
| 132 | + |
| 133 | + mockExit.mockRestore() |
| 134 | + }) |
| 135 | + |
| 136 | + test('does not output JSON for non-AbortError even when --json is active', async () => { |
| 137 | + vi.mocked(deleteDevStore).mockRejectedValueOnce(new Error('unexpected')) |
| 138 | + |
| 139 | + await expect( |
| 140 | + StoreDelete.run(['--store', 'my-store.myshopify.com', '--organization-id', '12345', '--json']), |
| 141 | + ).rejects.toThrow() |
| 142 | + expect(vi.mocked(outputResult)).not.toHaveBeenCalled() |
| 143 | + }) |
| 144 | + |
| 145 | + test('does not output JSON for AbortError when --json is not active', async () => { |
| 146 | + vi.mocked(deleteDevStore).mockRejectedValueOnce(new AbortError('Something went wrong')) |
| 147 | + |
| 148 | + await expect(StoreDelete.run(['--store', 'my-store.myshopify.com', '--organization-id', '12345'])).rejects.toThrow() |
| 149 | + expect(vi.mocked(outputResult)).not.toHaveBeenCalled() |
| 150 | + }) |
| 151 | +}) |
0 commit comments