|
1 |
| -import { generateMock } from '@anatine/zod-mock'; |
2 |
| -import type { MSKEvent, SelfManagedKafkaEvent } from 'aws-lambda'; |
3 | 1 | import { describe, expect, it } from 'vitest';
|
4 |
| -import { ParseError } from '../../../src'; |
| 2 | +import { ZodError, z } from 'zod'; |
5 | 3 | import { KafkaEnvelope } from '../../../src/envelopes/index.js';
|
6 |
| -import { TestEvents, TestSchema } from '../schema/utils.js'; |
7 |
| - |
8 |
| -describe('Kafka', () => { |
9 |
| - describe('parse', () => { |
10 |
| - it('should parse MSK kafka envelope', () => { |
11 |
| - const mock = generateMock(TestSchema); |
12 |
| - |
13 |
| - const kafkaEvent = TestEvents.kafkaEventMsk as MSKEvent; |
14 |
| - kafkaEvent.records['mytopic-0'][0].value = Buffer.from( |
15 |
| - JSON.stringify(mock) |
16 |
| - ).toString('base64'); |
| 4 | +import { ParseError } from '../../../src/errors.js'; |
| 5 | +import { JSONStringified } from '../../../src/helpers.js'; |
| 6 | +import { getTestEvent } from '../schema/utils.js'; |
| 7 | + |
| 8 | +describe('Envelope: Kafka', () => { |
| 9 | + const baseEvent = getTestEvent({ |
| 10 | + eventsPath: 'kafka', |
| 11 | + filename: 'base', |
| 12 | + }); |
17 | 13 |
|
18 |
| - const result = KafkaEnvelope.parse(kafkaEvent, TestSchema); |
| 14 | + describe('Method: parse', () => { |
| 15 | + it('throws if the payload of the value does not match the schema', () => { |
| 16 | + // Prepare |
| 17 | + const event = structuredClone(baseEvent); |
19 | 18 |
|
20 |
| - expect(result).toEqual([[mock]]); |
| 19 | + // Act & Assess |
| 20 | + expect(() => KafkaEnvelope.parse(event, z.number())).toThrow(); |
21 | 21 | });
|
22 | 22 |
|
23 |
| - it('should parse Self Managed kafka envelope', () => { |
24 |
| - const mock = generateMock(TestSchema); |
25 |
| - |
26 |
| - const kafkaEvent = |
27 |
| - TestEvents.kafkaEventSelfManaged as SelfManagedKafkaEvent; |
28 |
| - kafkaEvent.records['mytopic-0'][0].value = Buffer.from( |
29 |
| - JSON.stringify(mock) |
30 |
| - ).toString('base64'); |
| 23 | + it('parses a Kafka event', () => { |
| 24 | + // Prepare |
| 25 | + const event = structuredClone(baseEvent); |
31 | 26 |
|
32 |
| - const result = KafkaEnvelope.parse(kafkaEvent, TestSchema); |
| 27 | + // Act |
| 28 | + const result = KafkaEnvelope.parse(event, z.string()); |
33 | 29 |
|
34 |
| - expect(result).toEqual([[mock]]); |
| 30 | + // Assess |
| 31 | + expect(result).toEqual(['{"key":"value"}']); |
35 | 32 | });
|
36 | 33 |
|
37 |
| - describe('safeParse', () => { |
38 |
| - it('should parse MSK kafka envelope', () => { |
39 |
| - const mock = generateMock(TestSchema); |
| 34 | + it('parses a Kafka event and applies the schema transformation', () => { |
| 35 | + // Prepare |
| 36 | + const event = structuredClone(baseEvent); |
40 | 37 |
|
41 |
| - const kafkaEvent = TestEvents.kafkaEventMsk as MSKEvent; |
42 |
| - kafkaEvent.records['mytopic-0'][0].value = Buffer.from( |
43 |
| - JSON.stringify(mock) |
44 |
| - ).toString('base64'); |
| 38 | + // Act |
| 39 | + const result = KafkaEnvelope.parse( |
| 40 | + event, |
| 41 | + JSONStringified(z.object({ key: z.string() })) |
| 42 | + ); |
45 | 43 |
|
46 |
| - const result = KafkaEnvelope.safeParse(kafkaEvent, TestSchema); |
47 |
| - |
48 |
| - expect(result).toEqual({ |
49 |
| - success: true, |
50 |
| - data: [mock], |
51 |
| - }); |
52 |
| - }); |
53 |
| - |
54 |
| - it('should parse Self Managed kafka envelope', () => { |
55 |
| - const mock = generateMock(TestSchema); |
| 44 | + // Assess |
| 45 | + expect(result).toEqual([{ key: 'value' }]); |
| 46 | + }); |
56 | 47 |
|
57 |
| - const kafkaEvent = |
58 |
| - TestEvents.kafkaEventSelfManaged as SelfManagedKafkaEvent; |
59 |
| - kafkaEvent.records['mytopic-0'][0].value = Buffer.from( |
60 |
| - JSON.stringify(mock) |
61 |
| - ).toString('base64'); |
| 48 | + it('parses a self managed Kafka event', () => { |
| 49 | + // Prepare |
| 50 | + const event = structuredClone(baseEvent); |
| 51 | + event.eventSource = 'SelfManagedKafka'; |
62 | 52 |
|
63 |
| - const result = KafkaEnvelope.safeParse(kafkaEvent, TestSchema); |
| 53 | + // Act |
| 54 | + const result = KafkaEnvelope.parse(event, z.string()); |
64 | 55 |
|
65 |
| - expect(result).toEqual({ |
66 |
| - success: true, |
67 |
| - data: [mock], |
68 |
| - }); |
69 |
| - }); |
| 56 | + // Assess |
| 57 | + expect(result).toEqual(['{"key":"value"}']); |
| 58 | + }); |
| 59 | + }); |
70 | 60 |
|
71 |
| - it('should return original event on failure', () => { |
72 |
| - const kafkaEvent = TestEvents.kafkaEventMsk as MSKEvent; |
73 |
| - kafkaEvent.records['mytopic-0'][0].value = 'not a valid json'; |
| 61 | + describe('Method: safeParse', () => { |
| 62 | + it('parses a Kafka event', () => { |
| 63 | + // Prepare |
| 64 | + const event = structuredClone(baseEvent); |
74 | 65 |
|
75 |
| - const parseResult = KafkaEnvelope.safeParse(kafkaEvent, TestSchema); |
| 66 | + // Act |
| 67 | + const result = KafkaEnvelope.safeParse(event, z.string()); |
76 | 68 |
|
77 |
| - expect(parseResult).toEqual({ |
78 |
| - success: false, |
79 |
| - error: expect.any(ParseError), |
80 |
| - originalEvent: kafkaEvent, |
81 |
| - }); |
| 69 | + // Assess |
| 70 | + expect(result).toEqual({ |
| 71 | + success: true, |
| 72 | + data: ['{"key":"value"}'], |
| 73 | + }); |
| 74 | + }); |
82 | 75 |
|
83 |
| - if (!parseResult.success && parseResult.error) { |
84 |
| - expect(parseResult.error.cause).toBeInstanceOf(SyntaxError); |
85 |
| - } |
| 76 | + it('returns an error if the event is not a valid Kafka event', () => { |
| 77 | + // Prepare |
| 78 | + const event = structuredClone(baseEvent); |
| 79 | + event.eventSource = 'SelfManagedKafka'; |
| 80 | + // @ts-expect-error - Intentionally invalid event |
| 81 | + event.records['mytopic-0'] = []; |
| 82 | + |
| 83 | + // Act |
| 84 | + const result = KafkaEnvelope.safeParse(event, z.string()); |
| 85 | + |
| 86 | + // Assess |
| 87 | + expect(result).toEqual({ |
| 88 | + success: false, |
| 89 | + error: new ParseError('Failed to parse Kafka envelope', { |
| 90 | + cause: new ZodError([ |
| 91 | + { |
| 92 | + code: 'too_small', |
| 93 | + minimum: 1, |
| 94 | + type: 'array', |
| 95 | + inclusive: true, |
| 96 | + exact: false, |
| 97 | + message: 'Array must contain at least 1 element(s)', |
| 98 | + path: ['records', 'mytopic-0'], |
| 99 | + }, |
| 100 | + ]), |
| 101 | + }), |
| 102 | + originalEvent: event, |
86 | 103 | });
|
87 |
| - it('should return original event and error if envelope is invalid', () => { |
88 |
| - expect(KafkaEnvelope.safeParse({ foo: 'bar' }, TestSchema)).toEqual({ |
89 |
| - success: false, |
90 |
| - error: expect.any(ParseError), |
91 |
| - originalEvent: { foo: 'bar' }, |
92 |
| - }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('returns the original event and the error if the payload of the value does not match the schema', () => { |
| 107 | + // Prepare |
| 108 | + const event = structuredClone(baseEvent); |
| 109 | + |
| 110 | + // Act |
| 111 | + const result = KafkaEnvelope.safeParse(event, z.number()); |
| 112 | + |
| 113 | + // Assess |
| 114 | + expect(result).toEqual({ |
| 115 | + success: false, |
| 116 | + error: new ParseError('Failed to parse Kafka envelope', { |
| 117 | + cause: new ZodError([ |
| 118 | + { |
| 119 | + code: 'invalid_type', |
| 120 | + expected: 'number', |
| 121 | + received: 'string', |
| 122 | + path: ['records', 'mytopic-0'], |
| 123 | + message: 'Expected number, received string', |
| 124 | + }, |
| 125 | + ]), |
| 126 | + }), |
| 127 | + originalEvent: event, |
93 | 128 | });
|
94 | 129 | });
|
95 | 130 | });
|
|
0 commit comments