|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import WebhookEditDialog from '../components/WebhookEditDialog.vue' |
| 4 | +import type { WebhookEditForm } from '../composables/useWebhookEditor' |
| 5 | +import type { WebhookSubscription } from '../types' |
| 6 | +import { emptyWebhookAdvancedForm } from '../utils/webhookAdvanced' |
| 7 | + |
| 8 | +function form(): WebhookEditForm { |
| 9 | + return { |
| 10 | + name: 'Payments hook', |
| 11 | + description: 'Payment lifecycle events', |
| 12 | + url: 'https://example.test/hook', |
| 13 | + event_types: ['budget.updated'], |
| 14 | + event_categories: ['budget'], |
| 15 | + scope_filter: 'tenant:acme/*', |
| 16 | + disable_after_failures: '10', |
| 17 | + metadata: '{"team":"payments"}', |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function webhook(overrides: Partial<WebhookSubscription> = {}): WebhookSubscription { |
| 22 | + return { |
| 23 | + subscription_id: 'wh-1', |
| 24 | + tenant_id: 'acme', |
| 25 | + name: 'Payments hook', |
| 26 | + url: 'https://example.test/hook', |
| 27 | + event_types: ['budget.updated'], |
| 28 | + status: 'ACTIVE', |
| 29 | + created_at: '2026-07-20T00:00:00Z', |
| 30 | + ...overrides, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function mountDialog(overrides: Record<string, unknown> = {}) { |
| 35 | + return mount(WebhookEditDialog, { |
| 36 | + props: { |
| 37 | + loading: false, |
| 38 | + error: '', |
| 39 | + metadataError: '', |
| 40 | + form: form(), |
| 41 | + advanced: emptyWebhookAdvancedForm(), |
| 42 | + advancedHasConfig: false, |
| 43 | + eventTypes: ['budget.updated', 'tenant.updated'], |
| 44 | + eventCategories: ['budget', 'tenant'], |
| 45 | + tenantOwned: true, |
| 46 | + hiddenLegacySelectorCount: 0, |
| 47 | + webhook: webhook(), |
| 48 | + ...overrides, |
| 49 | + }, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +describe('WebhookEditDialog', () => { |
| 54 | + it('renders and updates the shared form DTO, then emits submit and cancel', async () => { |
| 55 | + const sharedForm = form() |
| 56 | + const wrapper = mountDialog({ form: sharedForm }) |
| 57 | + |
| 58 | + expect(wrapper.get('[role="dialog"]').attributes('aria-label')).toBe('Edit Webhook') |
| 59 | + expect(wrapper.findAll('fieldset').map(group => group.get('legend').text())).toEqual([ |
| 60 | + 'Event types', |
| 61 | + 'Event categories (additive — subscribes to all events in category, including future ones)', |
| 62 | + ]) |
| 63 | + expect(wrapper.get('#ew-failures').attributes()).toMatchObject({ |
| 64 | + min: '1', |
| 65 | + step: '1', |
| 66 | + required: '', |
| 67 | + }) |
| 68 | + await wrapper.get('#ew-name').setValue('Renamed hook') |
| 69 | + await wrapper.get('input[value="tenant.updated"]').setValue(true) |
| 70 | + await wrapper.get('form').trigger('submit') |
| 71 | + const cancel = wrapper.findAll('button').find(button => button.text() === 'Cancel') |
| 72 | + expect(cancel).toBeDefined() |
| 73 | + await cancel!.trigger('click') |
| 74 | + |
| 75 | + expect(sharedForm.name).toBe('Renamed hook') |
| 76 | + expect(sharedForm.event_types).toEqual(['budget.updated', 'tenant.updated']) |
| 77 | + expect(wrapper.emitted('submit')).toHaveLength(1) |
| 78 | + expect(wrapper.emitted('cancel')).toHaveLength(1) |
| 79 | + }) |
| 80 | + |
| 81 | + it('exposes tenant legacy-selector and metadata validation guidance', () => { |
| 82 | + const wrapper = mountDialog({ |
| 83 | + metadataError: 'Metadata must be a JSON object', |
| 84 | + hiddenLegacySelectorCount: 2, |
| 85 | + }) |
| 86 | + |
| 87 | + expect(wrapper.text()).toContain('Tenant-owned subscriptions can only receive tenant-scoped events') |
| 88 | + expect(wrapper.get('[data-testid="hidden-legacy-selectors-hint"]').text()).toContain( |
| 89 | + '2 legacy admin-only selectors are hidden', |
| 90 | + ) |
| 91 | + const alert = wrapper.get('[role="alert"]') |
| 92 | + expect(alert.text()).toBe('Metadata must be a JSON object') |
| 93 | + }) |
| 94 | + |
| 95 | + it('shows masked header names and opens existing advanced configuration', () => { |
| 96 | + const advanced = emptyWebhookAdvancedForm() |
| 97 | + advanced.max_retries = '5' |
| 98 | + const wrapper = mountDialog({ |
| 99 | + tenantOwned: false, |
| 100 | + webhook: webhook({ headers: { Authorization: '********', 'X-Team': '********' } }), |
| 101 | + advanced, |
| 102 | + advancedHasConfig: true, |
| 103 | + }) |
| 104 | + |
| 105 | + expect(wrapper.text()).toContain('Custom headers') |
| 106 | + expect(wrapper.text()).toContain('Authorization: ********') |
| 107 | + expect(wrapper.text()).toContain('X-Team: ********') |
| 108 | + expect(wrapper.get('[data-testid="ew-adv-toggle"]').attributes('aria-expanded')).toBe('true') |
| 109 | + expect((wrapper.get('#ew-adv-retries').element as HTMLInputElement).value).toBe('5') |
| 110 | + }) |
| 111 | +}) |
0 commit comments