@@ -2,12 +2,10 @@ import {writeAppLogsToFile} from './write-app-logs.js'
22import { AppLogData , AppLogPayload , FunctionRunLog } from '../types.js'
33import camelcaseKeys from '../camelcase-keys.js'
44import { joinPath } from '@shopify/cli-kit/node/path'
5- import { writeFile } from '@shopify/cli-kit/node/fs'
5+ import { readFile , inTemporaryDirectory } from '@shopify/cli-kit/node/fs'
66import { describe , expect , test , vi , beforeEach } from 'vitest'
77import { formatLocalDate } from '@shopify/cli-kit/common/string'
88
9- vi . mock ( '@shopify/cli-kit/node/fs' )
10-
119const APP_LOG : AppLogData = {
1210 shop_id : 1 ,
1311 api_client_id : 2 ,
@@ -34,7 +32,6 @@ const NEW_APP_LOG: AppLogData = {
3432
3533const FUNCTION_RUN_PAYLOAD = new FunctionRunLog ( camelcaseKeys ( JSON . parse ( APP_LOG . payload ) ) )
3634const STORE_NAME = 'storeName'
37- const TEST_LOGS_DIR = '/test/logs/dir'
3835
3936describe ( 'writeAppLogsToFile' , ( ) => {
4037 let stdout : any
@@ -44,61 +41,64 @@ describe('writeAppLogsToFile', () => {
4441 } )
4542
4643 test ( 'calls writeLog with the FunctionRunLog payload type' , async ( ) => {
47- // Given
48- // determine the fileName
49- const fileName = `20240522_150641_827Z_${ APP_LOG . source_namespace } _${ APP_LOG . source } `
50-
51- // When
52- const returnedPath = await writeAppLogsToFile ( {
53- appLog : APP_LOG ,
54- appLogPayload : FUNCTION_RUN_PAYLOAD ,
55- stdout,
56- storeName : STORE_NAME ,
57- logsDir : TEST_LOGS_DIR ,
44+ await inTemporaryDirectory ( async ( tmpDir ) => {
45+ // Given
46+ // determine the fileName
47+ const fileName = `20240522_150641_827Z_${ APP_LOG . source_namespace } _${ APP_LOG . source } `
48+
49+ // When
50+ const returnedPath = await writeAppLogsToFile ( {
51+ appLog : APP_LOG ,
52+ appLogPayload : FUNCTION_RUN_PAYLOAD ,
53+ stdout,
54+ storeName : STORE_NAME ,
55+ logsDir : tmpDir ,
56+ } )
57+
58+ // Then
59+ const expectedSaveData = {
60+ shopId : APP_LOG . shop_id ,
61+ apiClientId : APP_LOG . api_client_id ,
62+ payload : {
63+ logs : [ 'Line 1!' , ' Line2!' ] ,
64+ } ,
65+ logType : APP_LOG . log_type ,
66+ status : APP_LOG . status ,
67+ source : APP_LOG . source ,
68+ sourceNamespace : APP_LOG . source_namespace ,
69+ logTimestamp : APP_LOG . log_timestamp ,
70+ localTime : formatLocalDate ( APP_LOG . log_timestamp ) ,
71+ storeName : STORE_NAME ,
72+ }
73+ const expectedLogData = JSON . stringify ( expectedSaveData , null , 2 )
74+
75+ const writtenContent = await readFile ( returnedPath . fullOutputPath )
76+ expect ( writtenContent ) . toEqual ( expectedLogData )
77+ expect ( returnedPath . fullOutputPath ) . toEqual ( expect . stringContaining ( joinPath ( tmpDir , fileName ) ) )
5878 } )
59-
60- // Then
61- const expectedSaveData = {
62- shopId : APP_LOG . shop_id ,
63- apiClientId : APP_LOG . api_client_id ,
64- payload : {
65- logs : [ 'Line 1!' , ' Line2!' ] ,
66- } ,
67- logType : APP_LOG . log_type ,
68- status : APP_LOG . status ,
69- source : APP_LOG . source ,
70- sourceNamespace : APP_LOG . source_namespace ,
71- logTimestamp : APP_LOG . log_timestamp ,
72- localTime : formatLocalDate ( APP_LOG . log_timestamp ) ,
73- storeName : STORE_NAME ,
74- }
75- const expectedLogData = JSON . stringify ( expectedSaveData , null , 2 )
76-
77- expect ( writeFile ) . toHaveBeenCalledWith ( expect . stringContaining ( fileName ) , expectedLogData )
78- expect ( returnedPath . fullOutputPath ) . toEqual ( expect . stringContaining ( joinPath ( TEST_LOGS_DIR , fileName ) ) )
79- expect ( returnedPath . fullOutputPath ) . toEqual ( expect . stringContaining ( TEST_LOGS_DIR ) )
8079 } )
8180
8281 test ( 'calls writeLog with strings when no matching payload type' , async ( ) => {
83- // Given
84- // determine the fileName
85- const fileName = `20240522_150641_827Z_${ NEW_APP_LOG . source_namespace } _${ NEW_APP_LOG . source } `
86-
87- // When
88- const returnedPath = await writeAppLogsToFile ( {
89- appLog : NEW_APP_LOG ,
90- appLogPayload : JSON . parse ( NEW_APP_LOG . payload ) ,
91- stdout,
92- storeName : STORE_NAME ,
93- logsDir : TEST_LOGS_DIR ,
82+ await inTemporaryDirectory ( async ( tmpDir ) => {
83+ // Given
84+ // determine the fileName
85+ const fileName = `20240522_150641_827Z_${ NEW_APP_LOG . source_namespace } _${ NEW_APP_LOG . source } `
86+
87+ // When
88+ const returnedPath = await writeAppLogsToFile ( {
89+ appLog : NEW_APP_LOG ,
90+ appLogPayload : JSON . parse ( NEW_APP_LOG . payload ) ,
91+ stdout,
92+ storeName : STORE_NAME ,
93+ logsDir : tmpDir ,
94+ } )
95+
96+ // Then
97+ const expectedLogData = expectedLogDataFromAppEvent ( NEW_APP_LOG , JSON . parse ( NEW_APP_LOG . payload ) )
98+ const writtenContent = await readFile ( returnedPath . fullOutputPath )
99+ expect ( writtenContent ) . toEqual ( expectedLogData )
100+ expect ( returnedPath . fullOutputPath ) . toEqual ( expect . stringContaining ( joinPath ( tmpDir , fileName ) ) )
94101 } )
95-
96- // Then
97- expect ( returnedPath . fullOutputPath ) . toEqual ( expect . stringContaining ( joinPath ( TEST_LOGS_DIR , fileName ) ) )
98- expect ( writeFile ) . toHaveBeenCalledWith (
99- expect . stringContaining ( fileName ) ,
100- expectedLogDataFromAppEvent ( NEW_APP_LOG , JSON . parse ( NEW_APP_LOG . payload ) ) ,
101- )
102102 } )
103103} )
104104
0 commit comments