|
| 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 $UserCreatedEvent = z.object({ |
| 12 | + 'detail-type': z.literal('UserCreated'), |
| 13 | + detail: z.object({ |
| 14 | + userId: z.string().uuid(), |
| 15 | + clientId: z.string().uuid(), |
| 16 | + }), |
| 17 | +}); |
| 18 | + |
| 19 | +// Simulate consumer handler that processes the incoming event |
| 20 | +// Only check the validation - don't run actual handler logic |
| 21 | +async function handleUserCreated(event: unknown): Promise<void> { |
| 22 | + $UserCreatedEvent.parse(event); |
| 23 | +} |
| 24 | + |
| 25 | +describe('Pact Message Consumer - UserCreated Event', () => { |
| 26 | + const messagePact = new MessageConsumerPact({ |
| 27 | + consumer: 'templates', |
| 28 | + provider: 'auth', |
| 29 | + dir: path.resolve(__dirname, 'pacts'), |
| 30 | + pactfileWriteMode: 'update', |
| 31 | + logLevel: 'error', |
| 32 | + }); |
| 33 | + |
| 34 | + it('should validate the template deleted event structure and handler logic', async () => { |
| 35 | + await messagePact |
| 36 | + .given('a user has been created') |
| 37 | + .expectsToReceive('UserCreated') |
| 38 | + .withContent({ |
| 39 | + 'detail-type': 'UserCreated', |
| 40 | + detail: { |
| 41 | + userId: Matchers.uuid('eec2e415-dbb2-4e4d-9afb-ab64e280a3c9'), |
| 42 | + clientId: Matchers.uuid('f5a3daf2-8fa5-4582-ba2c-478eea955b6f'), |
| 43 | + }, |
| 44 | + }) |
| 45 | + .verify(asynchronousBodyHandler(handleUserCreated)); |
| 46 | + }); |
| 47 | +}); |
0 commit comments