|
| 1 | +import { expectTypeOf } from 'vitest' |
| 2 | +import { z } from 'zod/v4' |
| 3 | +import { enrichMessageSchemaWithBase } from '../messages/baseMessageSchemas.ts' |
| 4 | +import type { |
| 5 | + AnyEventHandler, |
| 6 | + CommonEventDefinition, |
| 7 | + CommonEventDefinitionConsumerSchemaType, |
| 8 | + CommonEventDefinitionPublisherSchemaType, |
| 9 | + SingleEventHandler, |
| 10 | +} from './eventTypes.ts' |
| 11 | + |
| 12 | +const myEvents = { |
| 13 | + plainEvent: { |
| 14 | + ...enrichMessageSchemaWithBase('entity.created', z.object({ name: z.string() })), |
| 15 | + }, |
| 16 | + transformingEvent: { |
| 17 | + ...enrichMessageSchemaWithBase( |
| 18 | + 'entity.updated', |
| 19 | + z.object({ |
| 20 | + // Forward-compatible field: unknown values are dropped instead of failing validation |
| 21 | + mode: z.preprocess( |
| 22 | + (value) => (value === 'live' ? value : undefined), |
| 23 | + z.literal('live').optional(), |
| 24 | + ), |
| 25 | + }), |
| 26 | + ), |
| 27 | + }, |
| 28 | +} as const satisfies Record<string, CommonEventDefinition> |
| 29 | + |
| 30 | +describe('eventTypes', () => { |
| 31 | + describe('CommonEventDefinitionConsumerSchemaType', () => { |
| 32 | + it('resolves transformed fields to their output type, not their input type', () => { |
| 33 | + type ConsumerMessage = CommonEventDefinitionConsumerSchemaType< |
| 34 | + typeof myEvents.transformingEvent |
| 35 | + > |
| 36 | + |
| 37 | + // DomainEventEmitter hands handlers the event parsed by the schema |
| 38 | + expectTypeOf<ConsumerMessage['payload']['mode']>().toEqualTypeOf<'live' | undefined>() |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + describe('CommonEventDefinitionPublisherSchemaType', () => { |
| 43 | + it('keeps the input type for transformed fields', () => { |
| 44 | + type PublisherMessage = CommonEventDefinitionPublisherSchemaType< |
| 45 | + typeof myEvents.transformingEvent |
| 46 | + > |
| 47 | + |
| 48 | + // Publishers pass the raw payload that the schema parses on emit |
| 49 | + expectTypeOf<PublisherMessage['payload']['mode']>().toBeUnknown() |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('SingleEventHandler', () => { |
| 54 | + it('receives the parsed event', () => { |
| 55 | + type Handler = SingleEventHandler<[typeof myEvents.transformingEvent], 'entity.updated'> |
| 56 | + type HandledEvent = Parameters<Handler['handleEvent']>[0] |
| 57 | + |
| 58 | + expectTypeOf<HandledEvent['payload']['mode']>().toEqualTypeOf<'live' | undefined>() |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + describe('AnyEventHandler', () => { |
| 63 | + it('receives the parsed event union', () => { |
| 64 | + type Handler = AnyEventHandler< |
| 65 | + [typeof myEvents.plainEvent, typeof myEvents.transformingEvent] |
| 66 | + > |
| 67 | + type HandledEvent = Parameters<Handler['handleEvent']>[0] |
| 68 | + |
| 69 | + expectTypeOf<HandledEvent['type']>().toEqualTypeOf<'entity.created' | 'entity.updated'>() |
| 70 | + expectTypeOf< |
| 71 | + Extract<HandledEvent, { type: 'entity.updated' }>['payload']['mode'] |
| 72 | + >().toEqualTypeOf<'live' | undefined>() |
| 73 | + }) |
| 74 | + }) |
| 75 | +}) |
0 commit comments