|
| 1 | +import {executeOperation} from './execute-operation.js' |
| 2 | +import {createAdminSessionAsApp} from './graphql/common.js' |
| 3 | +import {OrganizationApp} from '../models/organization.js' |
| 4 | +import {renderSuccess, renderError} from '@shopify/cli-kit/node/ui' |
| 5 | +import {adminRequestDoc} from '@shopify/cli-kit/node/api/admin' |
| 6 | +import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs' |
| 7 | +import {joinPath} from '@shopify/cli-kit/node/path' |
| 8 | +import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output' |
| 9 | +import {describe, test, expect, vi, beforeEach, afterEach} from 'vitest' |
| 10 | + |
| 11 | +vi.mock('./graphql/common.js') |
| 12 | +vi.mock('@shopify/cli-kit/node/ui') |
| 13 | +vi.mock('@shopify/cli-kit/node/api/admin') |
| 14 | +vi.mock('@shopify/cli-kit/node/fs') |
| 15 | + |
| 16 | +describe('executeOperation', () => { |
| 17 | + const mockRemoteApp = { |
| 18 | + apiKey: 'test-app-client-id', |
| 19 | + apiSecretKeys: [{secret: 'test-api-secret'}], |
| 20 | + title: 'Test App', |
| 21 | + } as OrganizationApp |
| 22 | + |
| 23 | + const storeFqdn = 'test-store.myshopify.com' |
| 24 | + const mockAdminSession = {token: 'test-token', storeFqdn} |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + vi.mocked(createAdminSessionAsApp).mockResolvedValue(mockAdminSession) |
| 28 | + }) |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + mockAndCaptureOutput().clear() |
| 32 | + }) |
| 33 | + |
| 34 | + test('executes GraphQL operation successfully', async () => { |
| 35 | + const query = 'query { shop { name } }' |
| 36 | + const mockResult = {data: {shop: {name: 'Test Shop'}}} |
| 37 | + vi.mocked(adminRequestDoc).mockResolvedValue(mockResult) |
| 38 | + |
| 39 | + await executeOperation({ |
| 40 | + remoteApp: mockRemoteApp, |
| 41 | + storeFqdn, |
| 42 | + query, |
| 43 | + }) |
| 44 | + |
| 45 | + expect(createAdminSessionAsApp).toHaveBeenCalledWith(mockRemoteApp, storeFqdn) |
| 46 | + expect(adminRequestDoc).toHaveBeenCalledWith({ |
| 47 | + // parsed GraphQL document |
| 48 | + query: expect.any(Object), |
| 49 | + session: mockAdminSession, |
| 50 | + variables: undefined, |
| 51 | + version: undefined, |
| 52 | + responseOptions: {handleErrors: false}, |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + test('passes variables correctly when provided', async () => { |
| 57 | + const query = 'mutation UpdateProduct($input: ProductInput!) { productUpdate(input: $input) { product { id } } }' |
| 58 | + const variables = '{"input":{"id":"gid://shopify/Product/123","title":"Updated"}}' |
| 59 | + const mockResult = {data: {productUpdate: {product: {id: 'gid://shopify/Product/123'}}}} |
| 60 | + vi.mocked(adminRequestDoc).mockResolvedValue(mockResult) |
| 61 | + |
| 62 | + await executeOperation({ |
| 63 | + remoteApp: mockRemoteApp, |
| 64 | + storeFqdn, |
| 65 | + query, |
| 66 | + variables, |
| 67 | + }) |
| 68 | + |
| 69 | + expect(adminRequestDoc).toHaveBeenCalledWith( |
| 70 | + expect.objectContaining({ |
| 71 | + variables: JSON.parse(variables), |
| 72 | + }), |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + test('throws AbortError when variables contain invalid JSON', async () => { |
| 77 | + const query = 'query { shop { name } }' |
| 78 | + const invalidVariables = '{invalid json}' |
| 79 | + |
| 80 | + await expect( |
| 81 | + executeOperation({ |
| 82 | + remoteApp: mockRemoteApp, |
| 83 | + storeFqdn, |
| 84 | + query, |
| 85 | + variables: invalidVariables, |
| 86 | + }), |
| 87 | + ).rejects.toThrow('Invalid JSON') |
| 88 | + |
| 89 | + expect(adminRequestDoc).not.toHaveBeenCalled() |
| 90 | + }) |
| 91 | + |
| 92 | + test('uses specified API version when provided', async () => { |
| 93 | + const query = 'query { shop { name } }' |
| 94 | + const apiVersion = '2024-01' |
| 95 | + const mockResult = {data: {shop: {name: 'Test Shop'}}} |
| 96 | + vi.mocked(adminRequestDoc).mockResolvedValue(mockResult) |
| 97 | + |
| 98 | + await executeOperation({ |
| 99 | + remoteApp: mockRemoteApp, |
| 100 | + storeFqdn, |
| 101 | + query, |
| 102 | + apiVersion, |
| 103 | + }) |
| 104 | + |
| 105 | + expect(adminRequestDoc).toHaveBeenCalledWith( |
| 106 | + expect.objectContaining({ |
| 107 | + version: apiVersion, |
| 108 | + }), |
| 109 | + ) |
| 110 | + }) |
| 111 | + |
| 112 | + test('writes formatted JSON results to stdout by default', async () => { |
| 113 | + const query = 'query { shop { name } }' |
| 114 | + const mockResult = {data: {shop: {name: 'Test Shop'}}} |
| 115 | + vi.mocked(adminRequestDoc).mockResolvedValue(mockResult) |
| 116 | + |
| 117 | + const mockOutput = mockAndCaptureOutput() |
| 118 | + |
| 119 | + await executeOperation({ |
| 120 | + remoteApp: mockRemoteApp, |
| 121 | + storeFqdn, |
| 122 | + query, |
| 123 | + }) |
| 124 | + |
| 125 | + const expectedOutput = JSON.stringify(mockResult, null, 2) |
| 126 | + expect(mockOutput.info()).toContain(expectedOutput) |
| 127 | + expect(writeFile).not.toHaveBeenCalled() |
| 128 | + }) |
| 129 | + |
| 130 | + test('writes results to file when outputFile is provided', async () => { |
| 131 | + await inTemporaryDirectory(async (tmpDir) => { |
| 132 | + const outputFile = joinPath(tmpDir, 'results.json') |
| 133 | + const query = 'query { shop { name } }' |
| 134 | + const mockResult = {data: {shop: {name: 'Test Shop'}}} |
| 135 | + vi.mocked(adminRequestDoc).mockResolvedValue(mockResult) |
| 136 | + |
| 137 | + await executeOperation({ |
| 138 | + remoteApp: mockRemoteApp, |
| 139 | + storeFqdn, |
| 140 | + query, |
| 141 | + outputFile, |
| 142 | + }) |
| 143 | + |
| 144 | + const expectedContent = JSON.stringify(mockResult, null, 2) |
| 145 | + expect(writeFile).toHaveBeenCalledWith(outputFile, expectedContent) |
| 146 | + expect(renderSuccess).toHaveBeenCalledWith( |
| 147 | + expect.objectContaining({ |
| 148 | + body: expect.stringContaining(outputFile), |
| 149 | + }), |
| 150 | + ) |
| 151 | + }) |
| 152 | + }) |
| 153 | + |
| 154 | + test('renders success message after successful execution', async () => { |
| 155 | + const query = 'query { shop { name } }' |
| 156 | + const mockResult = {data: {shop: {name: 'Test Shop'}}} |
| 157 | + vi.mocked(adminRequestDoc).mockResolvedValue(mockResult) |
| 158 | + |
| 159 | + await executeOperation({ |
| 160 | + remoteApp: mockRemoteApp, |
| 161 | + storeFqdn, |
| 162 | + query, |
| 163 | + }) |
| 164 | + |
| 165 | + expect(renderSuccess).toHaveBeenCalledWith( |
| 166 | + expect.objectContaining({ |
| 167 | + headline: 'Operation completed successfully.', |
| 168 | + }), |
| 169 | + ) |
| 170 | + }) |
| 171 | + |
| 172 | + test('renders error and rethrows when API request fails', async () => { |
| 173 | + const query = 'query { shop { name } }' |
| 174 | + const apiError = new Error('API request failed') |
| 175 | + vi.mocked(adminRequestDoc).mockRejectedValue(apiError) |
| 176 | + |
| 177 | + await expect( |
| 178 | + executeOperation({ |
| 179 | + remoteApp: mockRemoteApp, |
| 180 | + storeFqdn, |
| 181 | + query, |
| 182 | + }), |
| 183 | + ).rejects.toThrow('API request failed') |
| 184 | + |
| 185 | + expect(renderError).toHaveBeenCalledWith( |
| 186 | + expect.objectContaining({ |
| 187 | + headline: 'Operation failed.', |
| 188 | + body: 'API request failed', |
| 189 | + }), |
| 190 | + ) |
| 191 | + }) |
| 192 | + |
| 193 | + test('handles GraphQL errors in response', async () => { |
| 194 | + const query = 'query { shop { name } }' |
| 195 | + const mockResult = { |
| 196 | + data: null, |
| 197 | + errors: [{message: 'Field "name" not found'}], |
| 198 | + } |
| 199 | + vi.mocked(adminRequestDoc).mockResolvedValue(mockResult) |
| 200 | + |
| 201 | + await executeOperation({ |
| 202 | + remoteApp: mockRemoteApp, |
| 203 | + storeFqdn, |
| 204 | + query, |
| 205 | + }) |
| 206 | + |
| 207 | + // Should still format and output the result with errors |
| 208 | + const mockOutput = mockAndCaptureOutput() |
| 209 | + const expectedOutput = JSON.stringify(mockResult, null, 2) |
| 210 | + expect(mockOutput.info()).toContain(expectedOutput) |
| 211 | + }) |
| 212 | +}) |
0 commit comments