Skip to content

Commit

Permalink
tests: improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Jan 17, 2025
1 parent f082480 commit f36c11c
Showing 1 changed file with 68 additions and 72 deletions.
140 changes: 68 additions & 72 deletions packages/parser/tests/unit/envelopes/kafka.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { z } from 'zod';
import { ZodError, z } from 'zod';
import { KafkaEnvelope } from '../../../src/envelopes/index.js';
import { ParseError } from '../../../src/errors.js';
import { JSONStringified } from '../../../src/helpers.js';
Expand Down Expand Up @@ -44,92 +44,88 @@ describe('Envelope: Kafka', () => {
// Assess
expect(result).toEqual([{ key: 'value' }]);
});
});
/* describe('parse', () => {
it('should parse MSK kafka envelope', () => {
const mock = generateMock(TestSchema);
const kafkaEvent = TestEvents.kafkaEventMsk as MSKEvent;
kafkaEvent.records['mytopic-0'][0].value = Buffer.from(
JSON.stringify(mock)
).toString('base64');
const result = KafkaEnvelope.parse(kafkaEvent, TestSchema);
expect(result).toEqual([[mock]]);
});

it('should parse Self Managed kafka envelope', () => {
const mock = generateMock(TestSchema);
const kafkaEvent =
TestEvents.kafkaEventSelfManaged as SelfManagedKafkaEvent;
kafkaEvent.records['mytopic-0'][0].value = Buffer.from(
JSON.stringify(mock)
).toString('base64');
it('parses a self managed Kafka event', () => {
// Prepare
const event = structuredClone(baseEvent);
event.eventSource = 'SelfManagedKafka';

const result = KafkaEnvelope.parse(kafkaEvent, TestSchema);
// Act
const result = KafkaEnvelope.parse(event, z.string());

expect(result).toEqual([[mock]]);
// Assess
expect(result).toEqual(['{"key":"value"}']);
});
});

describe('safeParse', () => {
it('should parse MSK kafka envelope', () => {
const mock = generateMock(TestSchema);
const kafkaEvent = TestEvents.kafkaEventMsk as MSKEvent;
kafkaEvent.records['mytopic-0'][0].value = Buffer.from(
JSON.stringify(mock)
).toString('base64');
describe('Method: safeParse', () => {
it('parses a Kafka event', () => {
// Prepare
const event = structuredClone(baseEvent);

const result = KafkaEnvelope.safeParse(kafkaEvent, TestSchema);
// Act
const result = KafkaEnvelope.safeParse(event, z.string());

expect(result).toEqual({
success: true,
data: [mock],
});
// Assess
expect(result).toEqual({
success: true,
data: ['{"key":"value"}'],
});
});

it('should parse Self Managed kafka envelope', () => {
const mock = generateMock(TestSchema);
const kafkaEvent =
TestEvents.kafkaEventSelfManaged as SelfManagedKafkaEvent;
kafkaEvent.records['mytopic-0'][0].value = Buffer.from(
JSON.stringify(mock)
).toString('base64');
it('returns an error if the event is not a valid Kafka event', () => {
// Prepare
const event = structuredClone(baseEvent);
event.eventSource = 'SelfManagedKafka';
// @ts-expect-error - Intentionally invalid event
event.records['mytopic-0'] = [];

const result = KafkaEnvelope.safeParse(kafkaEvent, TestSchema);
// Act
const result = KafkaEnvelope.safeParse(event, z.string());

expect(result).toEqual({
success: true,
data: [mock],
});
// Assess
expect(result).toEqual({
success: false,
error: new ParseError('Failed to parse Kafka envelope', {
cause: new ZodError([
{
code: 'too_small',
minimum: 1,
type: 'array',
inclusive: true,
exact: false,
message: 'Array must contain at least 1 element(s)',
path: ['records', 'mytopic-0'],
},
]),
}),
originalEvent: event,
});
});

it('should return original event on failure', () => {
const kafkaEvent = TestEvents.kafkaEventMsk as MSKEvent;
kafkaEvent.records['mytopic-0'][0].value = 'not a valid json';
const parseResult = KafkaEnvelope.safeParse(kafkaEvent, TestSchema);
it('returns the original event and the error if the payload of the value does not match the schema', () => {
// Prepare
const event = structuredClone(baseEvent);

expect(parseResult).toEqual({
success: false,
error: expect.any(ParseError),
originalEvent: kafkaEvent,
});
// Act
const result = KafkaEnvelope.safeParse(event, z.number());

if (!parseResult.success && parseResult.error) {
expect(parseResult.error.cause).toBeInstanceOf(SyntaxError);
}
});
it('should return original event and error if envelope is invalid', () => {
expect(KafkaEnvelope.safeParse({ foo: 'bar' }, TestSchema)).toEqual({
success: false,
error: expect.any(ParseError),
originalEvent: { foo: 'bar' },
});
// Assess
expect(result).toEqual({
success: false,
error: new ParseError('Failed to parse Kafka envelope', {
cause: new ZodError([
{
code: 'invalid_type',
expected: 'number',
received: 'string',
path: ['records', 'mytopic-0'],
message: 'Expected number, received string',
},
]),
}),
originalEvent: event,
});
});
}); */
});
});

0 comments on commit f36c11c

Please sign in to comment.