From b9b6bab598b998fd21a6e050fc2d432c243ebbd4 Mon Sep 17 00:00:00 2001 From: Alex Bradley Date: Wed, 31 Jul 2024 13:46:32 -0400 Subject: [PATCH 1/7] split log newlines into array --- .../src/cli/services/app-logs/dev/write-app-logs.test.ts | 9 +++++++-- .../app/src/cli/services/app-logs/dev/write-app-logs.ts | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts index 524a9f9005..63a045db28 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts @@ -10,7 +10,7 @@ vi.mock('@shopify/cli-kit/node/logs') const APP_LOG: AppLogData = { shop_id: 1, api_client_id: 2, - payload: JSON.stringify({someJson: 'someJSOn'}), + payload: JSON.stringify({someJson: 'someJSOn', logs: 'Line 1!\n Line2!\n'}), log_type: 'function_run', cursor: '2024-05-22T15:06:43.841156Z', status: 'success', @@ -55,10 +55,15 @@ describe('writeAppLogsToFile', () => { }) function expectedLogDataFromAppEvent(event: AppLogData): string { + const payload = JSON.parse(event.payload) + + if (event.log_type === 'function_run') { + payload.logs = payload.logs.split('\n').filter(Boolean) + } const data = camelcaseKeys( { ...event, - payload: JSON.parse(event.payload), + payload, }, {deep: true}, ) diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts index 63dca79d9e..3fcaa8b762 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts @@ -27,10 +27,16 @@ export const writeAppLogsToFile = async ({ const fullOutputPath = joinPath(getLogsDir(), path) try { + const payload = JSON.parse(appLog.payload) + + if (appLog.log_type === 'function_run') { + payload.logs = payload.logs.split('\n').filter(Boolean) + } + const toSaveData = camelcaseKeys( { ...appLog, - payload: JSON.parse(appLog.payload), + payload, }, {deep: true}, ) From cf062343733b76a78b252db11bbe4836384acf78 Mon Sep 17 00:00:00 2001 From: Alex Bradley Date: Wed, 31 Jul 2024 14:01:09 -0400 Subject: [PATCH 2/7] use LOG_TYPE_FUNCTION_RUN type --- .../app/src/cli/services/app-logs/dev/write-app-logs.test.ts | 3 ++- packages/app/src/cli/services/app-logs/dev/write-app-logs.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts index 63a045db28..6d00e164fb 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts @@ -1,5 +1,6 @@ import {writeAppLogsToFile} from './write-app-logs.js' import {AppLogData} from '../types.js' +import {LOG_TYPE_FUNCTION_RUN} from '../utils.js' import {joinPath} from '@shopify/cli-kit/node/path' import {writeLog} from '@shopify/cli-kit/node/logs' import {describe, expect, test, vi, beforeEach} from 'vitest' @@ -57,7 +58,7 @@ describe('writeAppLogsToFile', () => { function expectedLogDataFromAppEvent(event: AppLogData): string { const payload = JSON.parse(event.payload) - if (event.log_type === 'function_run') { + if (event.log_type === LOG_TYPE_FUNCTION_RUN) { payload.logs = payload.logs.split('\n').filter(Boolean) } const data = camelcaseKeys( diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts index 3fcaa8b762..9df8568ebc 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts @@ -1,4 +1,5 @@ import {AppLogData} from '../types.js' +import {LOG_TYPE_FUNCTION_RUN} from '../utils.js' import {joinPath} from '@shopify/cli-kit/node/path' import {writeLog, getLogsDir} from '@shopify/cli-kit/node/logs' import {randomUUID} from '@shopify/cli-kit/node/crypto' @@ -29,7 +30,7 @@ export const writeAppLogsToFile = async ({ try { const payload = JSON.parse(appLog.payload) - if (appLog.log_type === 'function_run') { + if (appLog.log_type === LOG_TYPE_FUNCTION_RUN) { payload.logs = payload.logs.split('\n').filter(Boolean) } From 098d19e6820c763d79d93e0b525a76c87fba4863 Mon Sep 17 00:00:00 2001 From: Alex Bradley Date: Wed, 31 Jul 2024 17:21:12 -0400 Subject: [PATCH 3/7] pass parsed appLogPayload directly to writeAppLogs --- .../app-logs/dev/poll-app-logs.test.ts | 42 +++++++++++++++++++ .../services/app-logs/dev/poll-app-logs.ts | 1 + .../app-logs/dev/write-app-logs.test.ts | 19 +++------ .../services/app-logs/dev/write-app-logs.ts | 9 ++-- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts index 2f9c9a631b..c15dadac45 100644 --- a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts @@ -266,16 +266,19 @@ describe('pollAppLogs', () => { expect(writeAppLogsToFile).toHaveBeenCalledWith({ appLog: RESPONSE_DATA.app_logs[0], + appLogPayload: JSON.parse(RESPONSE_DATA.app_logs[0]!.payload), apiKey: API_KEY, stdout, }) expect(writeAppLogsToFile).toHaveBeenCalledWith({ appLog: RESPONSE_DATA.app_logs[1], + appLogPayload: JSON.parse(RESPONSE_DATA.app_logs[1]!.payload), apiKey: API_KEY, stdout, }) expect(writeAppLogsToFile).toHaveBeenCalledWith({ appLog: RESPONSE_DATA.app_logs[2], + appLogPayload: JSON.parse(RESPONSE_DATA.app_logs[2]!.payload), apiKey: API_KEY, stdout, }) @@ -396,4 +399,43 @@ describe('pollAppLogs', () => { expect(outputWarnSpy).toHaveBeenCalledWith('Error while polling app logs.') expect(vi.getTimerCount()).toEqual(1) }) + + test('displays error message, waits, and retries if response contained bad JSON', async () => { + // Given + const outputDebugSpy = vi.spyOn(output, 'outputDebug') + const outputWarnSpy = vi.spyOn(output, 'outputWarn') + + const badFunctionLogPayload = 'invalid json' + const responseDataWithBadJson = { + app_logs: [ + { + shop_id: 1, + api_client_id: 1830457, + payload: badFunctionLogPayload, + log_type: FUNCTION_RUN, + cursor: '2024-05-23T19:17:02.321773Z', + status: 'success', + source: SOURCE, + source_namespace: 'extensions', + log_timestamp: '2024-05-23T19:17:00.240053Z', + }, + ], + } + const mockedFetch = vi.fn().mockResolvedValueOnce(Response.json(responseDataWithBadJson)) + vi.mocked(fetch).mockImplementation(mockedFetch) + + // When + await pollAppLogs({ + stdout, + appLogsFetchInput: {jwtToken: JWT_TOKEN}, + apiKey: API_KEY, + resubscribeCallback: MOCKED_RESUBSCRIBE_CALLBACK, + }) + + // When/Then + await expect(writeAppLogsToFile).not.toHaveBeenCalled + expect(outputWarnSpy).toHaveBeenCalledWith('Error while polling app logs.') + expect(outputWarnSpy).toHaveBeenCalledWith('Retrying in 5 seconds.') + expect(outputDebugSpy).toHaveBeenCalledWith(expect.stringContaining('is not valid JSON')) + }) }) diff --git a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts index 5d2fe1976d..6feca18364 100644 --- a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts +++ b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts @@ -88,6 +88,7 @@ export const pollAppLogs = async ({ const logFile = await writeAppLogsToFile({ appLog: log, + appLogPayload: payload, apiKey, stdout, }) diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts index 6d00e164fb..4a1f956579 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts @@ -35,24 +35,17 @@ describe('writeAppLogsToFile', () => { const path = joinPath(API_KEY, fileName) // When - const returnedPath = await writeAppLogsToFile({appLog: APP_LOG, apiKey: API_KEY, stdout}) + const returnedPath = await writeAppLogsToFile({ + appLog: APP_LOG, + appLogPayload: JSON.parse(APP_LOG.payload), + apiKey: API_KEY, + stdout, + }) // Then expect(returnedPath.fullOutputPath.startsWith(path)).toBe(true) expect(writeLog).toHaveBeenCalledWith(expect.stringContaining(path), expectedLogDataFromAppEvent(APP_LOG)) }) - - test('prints and re-throws parsing errors', async () => { - // Given - const appLog = { - ...APP_LOG, - payload: 'invalid JSON', - } - - // When/Then - await expect(writeAppLogsToFile({appLog, apiKey: API_KEY, stdout})).rejects.toThrow() - expect(stdout.write).toHaveBeenCalledWith(expect.stringContaining('Error while writing log to file: ')) - }) }) function expectedLogDataFromAppEvent(event: AppLogData): string { diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts index 9df8568ebc..26e17cec7c 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts @@ -13,10 +13,13 @@ interface AppLogFile { export const writeAppLogsToFile = async ({ appLog, + appLogPayload, apiKey, stdout, }: { appLog: AppLogData + // eslint-disable-next-line @typescript-eslint/no-explicit-any + appLogPayload: any apiKey: string stdout: Writable }): Promise => { @@ -28,16 +31,14 @@ export const writeAppLogsToFile = async ({ const fullOutputPath = joinPath(getLogsDir(), path) try { - const payload = JSON.parse(appLog.payload) - if (appLog.log_type === LOG_TYPE_FUNCTION_RUN) { - payload.logs = payload.logs.split('\n').filter(Boolean) + appLogPayload.logs = appLogPayload.logs.split('\n').filter(Boolean) } const toSaveData = camelcaseKeys( { ...appLog, - payload, + payload: appLogPayload, }, {deep: true}, ) From 34a10e9755bf20f374c1f6790486648a125e2423 Mon Sep 17 00:00:00 2001 From: Alex Bradley Date: Thu, 1 Aug 2024 09:25:22 -0400 Subject: [PATCH 4/7] update JSON checking test for different node versions --- .../app/src/cli/services/app-logs/dev/poll-app-logs.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts index c15dadac45..f6a28217d3 100644 --- a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts @@ -436,6 +436,6 @@ describe('pollAppLogs', () => { await expect(writeAppLogsToFile).not.toHaveBeenCalled expect(outputWarnSpy).toHaveBeenCalledWith('Error while polling app logs.') expect(outputWarnSpy).toHaveBeenCalledWith('Retrying in 5 seconds.') - expect(outputDebugSpy).toHaveBeenCalledWith(expect.stringContaining('is not valid JSON')) + expect(outputDebugSpy).toHaveBeenCalledWith(expect.stringContaining('JSON')) }) }) From 518a6e51eb92509193de5fa577aac93cd4e5a9bd Mon Sep 17 00:00:00 2001 From: Alex Bradley Date: Thu, 1 Aug 2024 11:57:14 -0400 Subject: [PATCH 5/7] Use FunctionRunLog type when polling and writing --- .../app-logs/dev/poll-app-logs.test.ts | 13 +++- .../services/app-logs/dev/poll-app-logs.ts | 7 ++- .../app-logs/dev/write-app-logs.test.ts | 62 +++++++++++++++---- .../services/app-logs/dev/write-app-logs.ts | 13 ++-- 4 files changed, 71 insertions(+), 24 deletions(-) diff --git a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts index f6a28217d3..fd81b72c10 100644 --- a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.test.ts @@ -1,10 +1,12 @@ import {pollAppLogs} from './poll-app-logs.js' import {writeAppLogsToFile} from './write-app-logs.js' +import {FunctionRunLog} from '../types.js' import {partnersFqdn} from '@shopify/cli-kit/node/context/fqdn' import {describe, expect, test, vi, beforeEach, afterEach} from 'vitest' import {fetch} from '@shopify/cli-kit/node/http' import * as components from '@shopify/cli-kit/node/ui/components' import * as output from '@shopify/cli-kit/node/output' +import camelcaseKeys from 'camelcase-keys' const JWT_TOKEN = 'jwtToken' const API_KEY = 'apiKey' @@ -264,15 +266,22 @@ describe('pollAppLogs', () => { }, }) + const appLogPayloadZero = new FunctionRunLog( + camelcaseKeys(JSON.parse(RESPONSE_DATA.app_logs[0]!.payload), {deep: true}), + ) expect(writeAppLogsToFile).toHaveBeenCalledWith({ appLog: RESPONSE_DATA.app_logs[0], - appLogPayload: JSON.parse(RESPONSE_DATA.app_logs[0]!.payload), + appLogPayload: appLogPayloadZero, apiKey: API_KEY, stdout, }) + + const appLogPayloadOne = new FunctionRunLog( + camelcaseKeys(JSON.parse(RESPONSE_DATA.app_logs[1]!.payload), {deep: true}), + ) expect(writeAppLogsToFile).toHaveBeenCalledWith({ appLog: RESPONSE_DATA.app_logs[1], - appLogPayload: JSON.parse(RESPONSE_DATA.app_logs[1]!.payload), + appLogPayload: appLogPayloadOne, apiKey: API_KEY, stdout, }) diff --git a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts index 6feca18364..2bff1282ac 100644 --- a/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts +++ b/packages/app/src/cli/services/app-logs/dev/poll-app-logs.ts @@ -13,9 +13,10 @@ import { REQUEST_EXECUTION_IN_BACKGROUND_CACHE_ABOUT_TO_EXPIRE_REASON, handleFetchAppLogsError, } from '../utils.js' -import {AppLogData, ErrorResponse} from '../types.js' +import {AppLogData, ErrorResponse, FunctionRunLog} from '../types.js' import {outputContent, outputDebug, outputToken, outputWarn} from '@shopify/cli-kit/node/output' import {useConcurrentOutputContext} from '@shopify/cli-kit/node/ui/components' +import camelcaseKeys from 'camelcase-keys' import {Writable} from 'stream' export const pollAppLogs = async ({ @@ -74,12 +75,12 @@ export const pollAppLogs = async ({ const {app_logs: appLogs} = data for (const log of appLogs) { - const payload = JSON.parse(log.payload) - + let payload = JSON.parse(log.payload) // eslint-disable-next-line no-await-in-loop await useConcurrentOutputContext({outputPrefix: log.source, stripAnsi: false}, async () => { if (log.log_type === LOG_TYPE_FUNCTION_RUN) { handleFunctionRunLog(log, payload, stdout) + payload = new FunctionRunLog(camelcaseKeys(payload, {deep: true})) } else if (log.log_type.startsWith(LOG_TYPE_FUNCTION_NETWORK_ACCESS)) { handleFunctionNetworkAccessLog(log, payload, stdout) } else { diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts index 4a1f956579..3ec4366490 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts @@ -1,6 +1,5 @@ import {writeAppLogsToFile} from './write-app-logs.js' -import {AppLogData} from '../types.js' -import {LOG_TYPE_FUNCTION_RUN} from '../utils.js' +import {AppLogData, AppLogPayload, FunctionRunLog} from '../types.js' import {joinPath} from '@shopify/cli-kit/node/path' import {writeLog} from '@shopify/cli-kit/node/logs' import {describe, expect, test, vi, beforeEach} from 'vitest' @@ -19,6 +18,20 @@ const APP_LOG: AppLogData = { source_namespace: 'extensions', log_timestamp: '2024-05-22T15:06:41.827379Z', } + +const NEW_APP_LOG: AppLogData = { + shop_id: 1, + api_client_id: 2, + payload: JSON.stringify({someJson: 'someJSOn'}), + log_type: 'new_app_log_type', + cursor: '2024-05-22T15:06:43.841156Z', + status: 'success', + source: 'my-function', + source_namespace: 'extensions', + log_timestamp: '2024-05-22T15:06:41.827379Z', +} + +const FUNCTION_RUN_PAYLOAD = new FunctionRunLog(camelcaseKeys(JSON.parse(APP_LOG.payload))) const API_KEY = 'apiKey' describe('writeAppLogsToFile', () => { @@ -28,7 +41,7 @@ describe('writeAppLogsToFile', () => { stdout = {write: vi.fn()} }) - test('calls writeLog with the right data', async () => { + test('calls writeLog with the FunctionRunLog payload type', async () => { // Given // determine the fileName and path const fileName = `20240522_150641_827Z_${APP_LOG.source_namespace}_${APP_LOG.source}` @@ -37,29 +50,54 @@ describe('writeAppLogsToFile', () => { // When const returnedPath = await writeAppLogsToFile({ appLog: APP_LOG, - appLogPayload: JSON.parse(APP_LOG.payload), + appLogPayload: FUNCTION_RUN_PAYLOAD, apiKey: API_KEY, stdout, }) // Then expect(returnedPath.fullOutputPath.startsWith(path)).toBe(true) - expect(writeLog).toHaveBeenCalledWith(expect.stringContaining(path), expectedLogDataFromAppEvent(APP_LOG)) + expect(writeLog).toHaveBeenCalledWith( + expect.stringContaining(path), + expectedLogDataFromAppEvent(APP_LOG, FUNCTION_RUN_PAYLOAD), + ) }) -}) -function expectedLogDataFromAppEvent(event: AppLogData): string { - const payload = JSON.parse(event.payload) + test('calls writeLog with strings when no matching payload type', async () => { + // Given + // determine the fileName and path + const fileName = `20240522_150641_827Z_${APP_LOG.source_namespace}_${APP_LOG.source}` + const path = joinPath(API_KEY, fileName) + + // When + const returnedPath = await writeAppLogsToFile({ + appLog: NEW_APP_LOG, + appLogPayload: JSON.parse(NEW_APP_LOG.payload), + apiKey: API_KEY, + stdout, + }) - if (event.log_type === LOG_TYPE_FUNCTION_RUN) { - payload.logs = payload.logs.split('\n').filter(Boolean) - } + // Then + expect(returnedPath.fullOutputPath.startsWith(path)).toBe(true) + expect(writeLog).toHaveBeenCalledWith( + expect.stringContaining(path), + expectedLogDataFromAppEvent(NEW_APP_LOG, JSON.parse(NEW_APP_LOG.payload)), + ) + }) +}) + +function expectedLogDataFromAppEvent(event: AppLogData, payload: AppLogPayload | string): string { const data = camelcaseKeys( { ...event, - payload, + payload: payload as any, }, {deep: true}, ) + + if (payload instanceof FunctionRunLog) { + data.payload.logs = payload.logs.split('\n').filter(Boolean) + } + return JSON.stringify(data, null, 2) } diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts index 26e17cec7c..636e0112c8 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts @@ -1,5 +1,4 @@ -import {AppLogData} from '../types.js' -import {LOG_TYPE_FUNCTION_RUN} from '../utils.js' +import {AppLogData, AppLogPayload, FunctionRunLog} from '../types.js' import {joinPath} from '@shopify/cli-kit/node/path' import {writeLog, getLogsDir} from '@shopify/cli-kit/node/logs' import {randomUUID} from '@shopify/cli-kit/node/crypto' @@ -19,7 +18,7 @@ export const writeAppLogsToFile = async ({ }: { appLog: AppLogData // eslint-disable-next-line @typescript-eslint/no-explicit-any - appLogPayload: any + appLogPayload: AppLogPayload | any apiKey: string stdout: Writable }): Promise => { @@ -31,10 +30,6 @@ export const writeAppLogsToFile = async ({ const fullOutputPath = joinPath(getLogsDir(), path) try { - if (appLog.log_type === LOG_TYPE_FUNCTION_RUN) { - appLogPayload.logs = appLogPayload.logs.split('\n').filter(Boolean) - } - const toSaveData = camelcaseKeys( { ...appLog, @@ -43,6 +38,10 @@ export const writeAppLogsToFile = async ({ {deep: true}, ) + if (appLogPayload instanceof FunctionRunLog) { + toSaveData.payload.logs = appLogPayload.logs.split('\n').filter(Boolean) + } + const logData = JSON.stringify(toSaveData, null, 2) await writeLog(path, logData) From 9000a5ec08a1eb977e6f583611e5e95a185aa241 Mon Sep 17 00:00:00 2001 From: Alex Bradley Date: Fri, 2 Aug 2024 09:07:59 -0400 Subject: [PATCH 6/7] use any type for saveData --- packages/app/src/cli/services/app-logs/dev/write-app-logs.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts index 636e0112c8..6364c6afeb 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.ts @@ -30,7 +30,8 @@ export const writeAppLogsToFile = async ({ const fullOutputPath = joinPath(getLogsDir(), path) try { - const toSaveData = camelcaseKeys( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const toSaveData: any = camelcaseKeys( { ...appLog, payload: appLogPayload, From a4e6064019a4611760c44939936d0d26e64dddb8 Mon Sep 17 00:00:00 2001 From: Alex Bradley Date: Fri, 2 Aug 2024 09:52:24 -0400 Subject: [PATCH 7/7] use primitive data for specific payload type tests --- .../app-logs/dev/write-app-logs.test.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts index 3ec4366490..b0b14e0d41 100644 --- a/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts +++ b/packages/app/src/cli/services/app-logs/dev/write-app-logs.test.ts @@ -56,11 +56,23 @@ describe('writeAppLogsToFile', () => { }) // Then + const expectedSaveData = { + shopId: APP_LOG.shop_id, + apiClientId: APP_LOG.api_client_id, + payload: { + logs: ['Line 1!', ' Line2!'], + }, + logType: APP_LOG.log_type, + cursor: APP_LOG.cursor, + status: APP_LOG.status, + source: APP_LOG.source, + sourceNamespace: APP_LOG.source_namespace, + logTimestamp: APP_LOG.log_timestamp, + } + const expectedLogData = JSON.stringify(expectedSaveData, null, 2) + expect(returnedPath.fullOutputPath.startsWith(path)).toBe(true) - expect(writeLog).toHaveBeenCalledWith( - expect.stringContaining(path), - expectedLogDataFromAppEvent(APP_LOG, FUNCTION_RUN_PAYLOAD), - ) + expect(writeLog).toHaveBeenCalledWith(expect.stringContaining(path), expectedLogData) }) test('calls writeLog with strings when no matching payload type', async () => { @@ -86,18 +98,14 @@ describe('writeAppLogsToFile', () => { }) }) -function expectedLogDataFromAppEvent(event: AppLogData, payload: AppLogPayload | string): string { - const data = camelcaseKeys( +function expectedLogDataFromAppEvent(event: AppLogData, payload: AppLogPayload | any): string { + const data: any = camelcaseKeys( { ...event, - payload: payload as any, + payload, }, {deep: true}, ) - if (payload instanceof FunctionRunLog) { - data.payload.logs = payload.logs.split('\n').filter(Boolean) - } - return JSON.stringify(data, null, 2) }