From 7c60a20abe193365736fe9f4c7013f74010464cf Mon Sep 17 00:00:00 2001 From: Mehdi Salemi Date: Thu, 3 Oct 2024 10:34:27 -0400 Subject: [PATCH] refactor nits on test --- .../logs-command/ui/components/Logs.test.tsx | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) diff --git a/packages/app/src/cli/services/app-logs/logs-command/ui/components/Logs.test.tsx b/packages/app/src/cli/services/app-logs/logs-command/ui/components/Logs.test.tsx index af80cd80c9..9823057606 100644 --- a/packages/app/src/cli/services/app-logs/logs-command/ui/components/Logs.test.tsx +++ b/packages/app/src/cli/services/app-logs/logs-command/ui/components/Logs.test.tsx @@ -424,6 +424,213 @@ describe('Logs', () => { renderInstance.unmount() }) + test('renders NetworkAccessRequestExecutedLog correctly with success', async () => { + const webhookLogOutput = { + appLog: new NetworkAccessRequestExecutedLog({ + attempt: 1, + connectTimeMs: 40, + writeReadTimeMs: 40, + httpRequest: NETWORK_ACCESS_HTTP_REQUEST, + httpResponse: NETWORK_ACCESS_HTTP_RESPONSE, + error: null, + }), + prefix: { + functionId: FUNCTION_ID, + logTimestamp: TIME, + description: 'network access request executed in 80 ms', + status: 'Success', + storeName: 'my-store', + source: SOURCE, + }, + } + + const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []}) + vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs) + + const renderInstance = render( + , + ) + + const lastFrame = renderInstance.lastFrame() + + expect(unstyled(lastFrame!)).toMatchInlineSnapshot(` + "2024-06-18 16:02:04.868 my-store my-function Success network access request executed in 80 ms + Attempt: 1 + Connect time: 40 ms + Write read time: 40 ms + HTTP request: + { + \\"url\\": \\"https://api.example.com/hello\\", + \\"method\\": \\"GET\\", + \\"headers\\": {}, + \\"body\\": null, + \\"policy\\": { + \\"read_timeout_ms\\": 500 + } + } + HTTP response: + { + \\"status\\": 200, + \\"body\\": \\"Success\\", + \\"headers\\": { + \\"header1\\": \\"value1\\" + } + }" + `) + + renderInstance.unmount() + }) + + test('renders NetworkAccessRequestExecutedLog correctly with failure', async () => { + const webhookLogOutput = { + appLog: new NetworkAccessRequestExecutedLog({ + attempt: 1, + connectTimeMs: null, + writeReadTimeMs: null, + httpRequest: NETWORK_ACCESS_HTTP_REQUEST, + httpResponse: null, + error: 'Timeout Error', + }), + prefix: { + functionId: FUNCTION_ID, + logTimestamp: TIME, + description: 'network access request executed', + storeName: 'my-store', + status: 'Failure', + source: SOURCE, + }, + } + + const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []}) + vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs) + + const renderInstance = render( + , + ) + + const lastFrame = renderInstance.lastFrame() + + expect(unstyled(lastFrame!)).toMatchInlineSnapshot(` + "2024-06-18 16:02:04.868 my-store my-function Failure network access request executed + Attempt: 1 + HTTP request: + { + \\"url\\": \\"https://api.example.com/hello\\", + \\"method\\": \\"GET\\", + \\"headers\\": {}, + \\"body\\": null, + \\"policy\\": { + \\"read_timeout_ms\\": 500 + } + } + Error: Timeout Error" + `) + + renderInstance.unmount() + }) + + test('renders NetworkAccessRequestExecutionInBackgroundLog correctly with NoCachedResponse', async () => { + const webhookLogOutput = { + appLog: new NetworkAccessRequestExecutionInBackgroundLog({ + reason: BackgroundExecutionReason.NoCachedResponse, + httpRequest: NETWORK_ACCESS_HTTP_REQUEST, + }), + prefix: { + functionId: FUNCTION_ID, + logTimestamp: TIME, + description: 'network access request executing in background', + storeName: 'my-store', + status: 'Success', + source: SOURCE, + }, + } + + const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []}) + vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs) + + const renderInstance = render( + , + ) + + const lastFrame = renderInstance.lastFrame() + + expect(unstyled(lastFrame!)).toMatchInlineSnapshot(` + "2024-06-18 16:02:04.868 my-store my-function Success network access request executing in background + Reason: No cached response available + HTTP request: + { + \\"url\\": \\"https://api.example.com/hello\\", + \\"method\\": \\"GET\\", + \\"headers\\": {}, + \\"body\\": null, + \\"policy\\": { + \\"read_timeout_ms\\": 500 + } + }" + `) + + renderInstance.unmount() + }) + + test('renders NetworkAccessRequestExecutionInBackgroundLog correctly with CacheAboutToExpire', async () => { + const webhookLogOutput = { + appLog: new NetworkAccessRequestExecutionInBackgroundLog({ + reason: BackgroundExecutionReason.CacheAboutToExpire, + httpRequest: NETWORK_ACCESS_HTTP_REQUEST, + }), + prefix: { + functionId: FUNCTION_ID, + logTimestamp: TIME, + description: 'network access request executing in background', + storeName: 'my-store', + status: 'Success', + source: SOURCE, + }, + } + + const mockedUsePollAppLogs = vi.fn().mockReturnValue({appLogOutputs: [webhookLogOutput], errors: []}) + vi.mocked(usePollAppLogs).mockImplementation(mockedUsePollAppLogs) + + const renderInstance = render( + , + ) + + const lastFrame = renderInstance.lastFrame() + + expect(unstyled(lastFrame!)).toMatchInlineSnapshot(` + "2024-06-18 16:02:04.868 my-store my-function Success network access request executing in background + Reason: Cache is about to expire + HTTP request: + { + \\"url\\": \\"https://api.example.com/hello\\", + \\"method\\": \\"GET\\", + \\"headers\\": {}, + \\"body\\": null, + \\"policy\\": { + \\"read_timeout_ms\\": 500 + } + }" + `) + + renderInstance.unmount() + }) + // Add more tests for other webhook log types (NetworkAccessRequestExecutedLog, NetworkAccessRequestExecutionInBackgroundLog) })