EditHookPanel.constants.test.ts91 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { FormSchema } from './EditHookPanel.constants' |
| 4 | |
| 5 | describe('EditHookPanel FormSchema', () => { |
| 6 | it('rejects incomplete http_request hostnames', () => { |
| 7 | const result = FormSchema.safeParse({ |
| 8 | name: 'Test hook', |
| 9 | table_id: 'public.messages', |
| 10 | http_method: 'POST' as const, |
| 11 | timeout_ms: 1000, |
| 12 | events: ['INSERT'], |
| 13 | httpHeaders: [], |
| 14 | httpParameters: [], |
| 15 | function_type: 'http_request' as const, |
| 16 | http_url: 'https://webhook', |
| 17 | }) |
| 18 | |
| 19 | expect(result.success).toBe(false) |
| 20 | if (!result.success) { |
| 21 | expect( |
| 22 | result.error.issues.some((issue) => issue.message === 'Please provide a valid URL') |
| 23 | ).toBe(true) |
| 24 | } |
| 25 | }) |
| 26 | |
| 27 | it('rejects http_request URLs without an explicit protocol', () => { |
| 28 | const result = FormSchema.safeParse({ |
| 29 | name: 'Test hook', |
| 30 | table_id: 'public.messages', |
| 31 | http_method: 'POST' as const, |
| 32 | timeout_ms: 1000, |
| 33 | events: ['INSERT'], |
| 34 | httpHeaders: [], |
| 35 | httpParameters: [], |
| 36 | function_type: 'http_request' as const, |
| 37 | http_url: 'hooks.example.com/webhook', |
| 38 | }) |
| 39 | |
| 40 | expect(result.success).toBe(false) |
| 41 | if (!result.success) { |
| 42 | expect( |
| 43 | result.error.issues.some( |
| 44 | (issue) => issue.message === 'Please prefix your URL with http:// or https://' |
| 45 | ) |
| 46 | ).toBe(true) |
| 47 | } |
| 48 | }) |
| 49 | |
| 50 | it('rejects key-only webhook headers', () => { |
| 51 | const result = FormSchema.safeParse({ |
| 52 | name: 'Test hook', |
| 53 | table_id: 'public.messages', |
| 54 | http_method: 'POST' as const, |
| 55 | timeout_ms: 1000, |
| 56 | events: ['INSERT'], |
| 57 | httpHeaders: [{ id: 'header-1', name: 'X-Test', value: '' }], |
| 58 | httpParameters: [], |
| 59 | function_type: 'http_request' as const, |
| 60 | http_url: 'https://hooks.example.com/webhook', |
| 61 | }) |
| 62 | |
| 63 | expect(result.success).toBe(false) |
| 64 | if (!result.success) { |
| 65 | expect( |
| 66 | result.error.issues.some((issue) => issue.message === 'Header value is required') |
| 67 | ).toBe(true) |
| 68 | } |
| 69 | }) |
| 70 | |
| 71 | it('rejects value-only webhook parameters', () => { |
| 72 | const result = FormSchema.safeParse({ |
| 73 | name: 'Test hook', |
| 74 | table_id: 'public.messages', |
| 75 | http_method: 'POST' as const, |
| 76 | timeout_ms: 1000, |
| 77 | events: ['INSERT'], |
| 78 | httpHeaders: [], |
| 79 | httpParameters: [{ id: 'param-1', name: '', value: 'tenant' }], |
| 80 | function_type: 'http_request' as const, |
| 81 | http_url: 'https://hooks.example.com/webhook', |
| 82 | }) |
| 83 | |
| 84 | expect(result.success).toBe(false) |
| 85 | if (!result.success) { |
| 86 | expect( |
| 87 | result.error.issues.some((issue) => issue.message === 'Parameter name is required') |
| 88 | ).toBe(true) |
| 89 | } |
| 90 | }) |
| 91 | }) |