-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02fd465
commit 52f441b
Showing
15 changed files
with
443 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/app/src/cli/services/app-logs/logs-command/render-json-logs.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {renderJsonLogs} from './render-json-logs.js' | ||
import {pollAppLogs} from './poll-app-logs.js' | ||
import {handleFetchAppLogsError} from '../utils.js' | ||
import {testDeveloperPlatformClient} from '../../../models/app/app.test-data.js' | ||
import {outputInfo} from '@shopify/cli-kit/node/output' | ||
import {describe, expect, vi, test, beforeEach, afterEach} from 'vitest' | ||
|
||
vi.mock('./poll-app-logs') | ||
vi.mock('../utils', async (importOriginal) => { | ||
const mod = await importOriginal<typeof import('../utils.js')>() | ||
return { | ||
...mod, | ||
fetchAppLogs: vi.fn(), | ||
handleFetchAppLogsError: vi.fn(), | ||
} | ||
}) | ||
vi.mock('@shopify/cli-kit/node/output') | ||
|
||
describe('renderJsonLogs', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.clearAllTimers() | ||
}) | ||
|
||
test('should handle success response correctly', async () => { | ||
const mockSuccessResponse = { | ||
cursor: 'next-cursor', | ||
appLogs: [{message: 'Log 1'}, {message: 'Log 2'}], | ||
} | ||
const pollAppLogsMock = vi.fn().mockResolvedValue(mockSuccessResponse) | ||
vi.mocked(pollAppLogs).mockImplementation(pollAppLogsMock) | ||
|
||
await renderJsonLogs({ | ||
pollOptions: {cursor: 'cursor', filters: {status: undefined, source: undefined}, jwtToken: 'jwtToken'}, | ||
options: { | ||
variables: {shopIds: ['1'], apiKey: 'key', token: 'token'}, | ||
developerPlatformClient: testDeveloperPlatformClient(), | ||
}, | ||
}) | ||
|
||
expect(outputInfo).toHaveBeenNthCalledWith(1, JSON.stringify({message: 'Log 1'})) | ||
expect(outputInfo).toHaveBeenNthCalledWith(2, JSON.stringify({message: 'Log 2'})) | ||
expect(pollAppLogs).toHaveBeenCalled() | ||
expect(vi.getTimerCount()).toEqual(1) | ||
}) | ||
|
||
test('should handle error response and retry as expected', async () => { | ||
const mockErrorResponse = { | ||
errors: [{status: 500, message: 'Server Error'}], | ||
} | ||
const pollAppLogsMock = vi.fn().mockResolvedValue(mockErrorResponse) | ||
vi.mocked(pollAppLogs).mockImplementation(pollAppLogsMock) | ||
const mockRetryInterval = 1000 | ||
const handleFetchAppLogsErrorMock = vi.fn((input) => { | ||
input.onUnknownError(mockRetryInterval) | ||
return new Promise<{retryIntervalMs: number; nextJwtToken: string | null}>((resolve, _reject) => { | ||
resolve({nextJwtToken: 'new-jwt-token', retryIntervalMs: mockRetryInterval}) | ||
}) | ||
}) | ||
vi.mocked(handleFetchAppLogsError).mockImplementation(handleFetchAppLogsErrorMock) | ||
|
||
await renderJsonLogs({ | ||
pollOptions: {cursor: 'cursor', filters: {status: undefined, source: undefined}, jwtToken: 'jwtToken'}, | ||
options: { | ||
variables: {shopIds: [], apiKey: '', token: ''}, | ||
developerPlatformClient: testDeveloperPlatformClient(), | ||
}, | ||
}) | ||
|
||
expect(outputInfo).toHaveBeenCalledWith( | ||
JSON.stringify({message: 'Error while polling app logs.', retry_in_ms: mockRetryInterval}), | ||
) | ||
expect(pollAppLogs).toHaveBeenCalled() | ||
expect(vi.getTimerCount()).toEqual(1) | ||
}) | ||
}) |
59 changes: 59 additions & 0 deletions
59
packages/app/src/cli/services/app-logs/logs-command/render-json-logs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {pollAppLogs} from './poll-app-logs.js' | ||
import {PollOptions, SubscribeOptions, ErrorResponse, SuccessResponse} from '../types.js' | ||
import {POLLING_INTERVAL_MS, handleFetchAppLogsError, subscribeToAppLogs} from '../utils.js' | ||
import {outputInfo} from '@shopify/cli-kit/node/output' | ||
|
||
export async function renderJsonLogs({ | ||
pollOptions: {cursor, filters, jwtToken}, | ||
options: {variables, developerPlatformClient}, | ||
}: { | ||
pollOptions: PollOptions | ||
options: SubscribeOptions | ||
}): Promise<void> { | ||
const response = await pollAppLogs({cursor, filters, jwtToken}) | ||
let retryIntervalMs = POLLING_INTERVAL_MS | ||
let nextJwtToken = jwtToken | ||
|
||
const errorResponse = response as ErrorResponse | ||
|
||
if (errorResponse.errors) { | ||
const result = await handleFetchAppLogsError({ | ||
response: errorResponse, | ||
onThrottle: (retryIntervalMs) => { | ||
outputInfo(JSON.stringify({message: 'Request throttled while polling app logs.', retry_in_ms: retryIntervalMs})) | ||
}, | ||
onUnknownError: (retryIntervalMs) => { | ||
outputInfo(JSON.stringify({message: 'Error while polling app logs.', retry_in_ms: retryIntervalMs})) | ||
}, | ||
onResubscribe: () => { | ||
return subscribeToAppLogs(developerPlatformClient, variables) | ||
}, | ||
}) | ||
|
||
if (result.nextJwtToken) { | ||
nextJwtToken = result.nextJwtToken | ||
} | ||
retryIntervalMs = result.retryIntervalMs | ||
} | ||
|
||
const {cursor: nextCursor, appLogs} = response as SuccessResponse | ||
|
||
if (appLogs) { | ||
appLogs.forEach((log) => { | ||
outputInfo(JSON.stringify(log)) | ||
}) | ||
} | ||
|
||
setTimeout(() => { | ||
renderJsonLogs({ | ||
options: {variables, developerPlatformClient}, | ||
pollOptions: { | ||
jwtToken: nextJwtToken || jwtToken, | ||
cursor: nextCursor || cursor, | ||
filters, | ||
}, | ||
}).catch((error) => { | ||
throw error | ||
}) | ||
}, retryIntervalMs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.