|
| 1 | +import path from 'node:path'; |
| 2 | +import { |
| 3 | + MessageConsumerPact, |
| 4 | + Matchers, |
| 5 | + asynchronousBodyHandler, |
| 6 | +} from '@pact-foundation/pact'; |
| 7 | +import { z } from 'zod'; |
| 8 | + |
| 9 | +// I guess this would be defined in the handler source code in the event consumer and imported here |
| 10 | +// Handlers should parse the incoming event before doing anything else |
| 11 | +const $TemplateDeletedEvent = z.object({ |
| 12 | + 'detail-type': z.literal('TemplateDeleted'), |
| 13 | + version: z.literal('1.0'), |
| 14 | + detail: z.object({ |
| 15 | + id: z.string().uuid(), |
| 16 | + owner: z.string().uuid(), |
| 17 | + }), |
| 18 | +}); |
| 19 | + |
| 20 | +// Simulate consumer handler that processes the incoming event |
| 21 | +// Only check the validation - don't run actual handler logic |
| 22 | +async function handleTemplateDeleted(event: unknown): Promise<void> { |
| 23 | + $TemplateDeletedEvent.parse(event); |
| 24 | +} |
| 25 | + |
| 26 | +describe('Pact Message Consumer - TemplateDeleted Event', () => { |
| 27 | + const messagePact = new MessageConsumerPact({ |
| 28 | + consumer: 'consumer-2', |
| 29 | + provider: 'templates', |
| 30 | + dir: path.resolve(__dirname, 'pacts'), |
| 31 | + pactfileWriteMode: 'update', |
| 32 | + logLevel: 'error', |
| 33 | + }); |
| 34 | + |
| 35 | + it('should validate the template deleted event structure and handler logic', async () => { |
| 36 | + await messagePact |
| 37 | + .given('A template has been deleted') |
| 38 | + .expectsToReceive('TemplateDeleted') |
| 39 | + .withContent({ |
| 40 | + 'detail-type': 'TemplateDeleted', |
| 41 | + version: '1.0', |
| 42 | + detail: { |
| 43 | + owner: Matchers.uuid('c0574019-4629-4b3f-8987-aa34ca8bc5b9'), |
| 44 | + id: Matchers.uuid('b18a9a49-72a8-4157-8b85-76d5ac5c7804'), |
| 45 | + }, |
| 46 | + }) |
| 47 | + .verify(asynchronousBodyHandler(handleTemplateDeleted)); |
| 48 | + }); |
| 49 | +}); |
0 commit comments