EdgeFunctionRecentInvocations.utils.test.ts 3.8 KB

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