EdgeFunctionRecentInvocations.utils.test.ts87 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { parseEdgeFunctionEventMessage } from './EdgeFunctionRecentInvocations.utils' |
| 4 | |
| 5 | describe('parseEdgeFunctionEventMessage', () => { |
| 6 | it('should strip method and status code when they match the structured fields', () => { |
| 7 | const message = 'POST | 200 | https://example.briven.red/functions/v1/hello-world' |
| 8 | expect(parseEdgeFunctionEventMessage(message, 'POST', '200')).toBe( |
| 9 | 'https://example.briven.red/functions/v1/hello-world' |
| 10 | ) |
| 11 | }) |
| 12 | |
| 13 | it('should strip method and status code for different HTTP methods', () => { |
| 14 | const message = 'GET | 404 | https://example.briven.red/functions/v1/not-found' |
| 15 | expect(parseEdgeFunctionEventMessage(message, 'GET', '404')).toBe( |
| 16 | 'https://example.briven.red/functions/v1/not-found' |
| 17 | ) |
| 18 | }) |
| 19 | |
| 20 | it('should strip method and status code for 500 errors', () => { |
| 21 | const message = 'POST | 500 | https://example.briven.red/functions/v1/broken' |
| 22 | expect(parseEdgeFunctionEventMessage(message, 'POST', '500')).toBe( |
| 23 | 'https://example.briven.red/functions/v1/broken' |
| 24 | ) |
| 25 | }) |
| 26 | |
| 27 | it('should preserve the rest of the message when it contains pipe separators', () => { |
| 28 | const message = 'POST | 200 | some | complex | message' |
| 29 | expect(parseEdgeFunctionEventMessage(message, 'POST', '200')).toBe('some | complex | message') |
| 30 | }) |
| 31 | |
| 32 | it('should return original message when method does not match', () => { |
| 33 | const message = 'POST | 200 | https://example.briven.red/functions/v1/hello-world' |
| 34 | expect(parseEdgeFunctionEventMessage(message, 'GET', '200')).toBe(message) |
| 35 | }) |
| 36 | |
| 37 | it('should return original message when status code does not match', () => { |
| 38 | const message = 'POST | 200 | https://example.briven.red/functions/v1/hello-world' |
| 39 | expect(parseEdgeFunctionEventMessage(message, 'POST', '500')).toBe(message) |
| 40 | }) |
| 41 | |
| 42 | it('should return original message when method is undefined', () => { |
| 43 | const message = 'POST | 200 | https://example.briven.red/functions/v1/hello-world' |
| 44 | expect(parseEdgeFunctionEventMessage(message, undefined, '200')).toBe(message) |
| 45 | }) |
| 46 | |
| 47 | it('should return original message when status code is undefined', () => { |
| 48 | const message = 'POST | 200 | https://example.briven.red/functions/v1/hello-world' |
| 49 | expect(parseEdgeFunctionEventMessage(message, 'POST', undefined)).toBe(message) |
| 50 | }) |
| 51 | |
| 52 | it('should return original message when both method and status code are undefined', () => { |
| 53 | const message = 'POST | 200 | https://example.briven.red/functions/v1/hello-world' |
| 54 | expect(parseEdgeFunctionEventMessage(message, undefined, undefined)).toBe(message) |
| 55 | }) |
| 56 | |
| 57 | it('should return original message when format has fewer than 3 parts', () => { |
| 58 | const message = 'some simple message' |
| 59 | expect(parseEdgeFunctionEventMessage(message, 'POST', '200')).toBe(message) |
| 60 | }) |
| 61 | |
| 62 | it('should return original message when format has only 2 pipe-separated parts', () => { |
| 63 | const message = 'POST | 200' |
| 64 | expect(parseEdgeFunctionEventMessage(message, 'POST', '200')).toBe(message) |
| 65 | }) |
| 66 | |
| 67 | it('should return empty string for empty input', () => { |
| 68 | expect(parseEdgeFunctionEventMessage('', 'POST', '200')).toBe('') |
| 69 | }) |
| 70 | |
| 71 | it('should handle whitespace around parts correctly', () => { |
| 72 | const message = 'POST | 200 | https://example.briven.red/functions/v1/hello-world' |
| 73 | expect(parseEdgeFunctionEventMessage(message, 'POST', '200')).toBe( |
| 74 | 'https://example.briven.red/functions/v1/hello-world' |
| 75 | ) |
| 76 | }) |
| 77 | |
| 78 | it('should return original message when the message does not follow the expected format', () => { |
| 79 | const message = 'Error: function crashed unexpectedly' |
| 80 | expect(parseEdgeFunctionEventMessage(message, 'POST', '500')).toBe(message) |
| 81 | }) |
| 82 | |
| 83 | it('should handle messages with extra whitespace in the URL portion', () => { |
| 84 | const message = 'DELETE | 204 | ' |
| 85 | expect(parseEdgeFunctionEventMessage(message, 'DELETE', '204')).toBe('') |
| 86 | }) |
| 87 | }) |