Skip to content

Commit 72e55b7

Browse files
committed
Adding missing tests
1 parent 02214b7 commit 72e55b7

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

packages/kafka/test/publisher/PermissionPublisher.spec.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { InternalError } from '@lokalise/node-core'
2-
import { type PermissionAdded, type PermissionRemoved, TOPICS } from '../utils/permissionSchemas.js'
2+
import {
3+
PERMISSION_ADDED_SCHEMA,
4+
PERMISSION_GENERAL_TOPIC,
5+
type PermissionAdded,
6+
type PermissionRemoved,
7+
TOPICS,
8+
} from '../utils/permissionSchemas.js'
39
import { type TestContext, createTestContext } from '../utils/testContext.js'
410
import { PermissionPublisher } from './PermissionPublisher.js'
511

@@ -96,14 +102,22 @@ describe('PermissionPublisher - init', () => {
96102
expect(emittedEvent.message).toMatchObject({ id: '1', type: 'added' })
97103
},
98104
)
105+
106+
it('should fail if message type is not supported and a topic has different schemas', () => {
107+
// Given
108+
expect(
109+
() => new PermissionPublisher(testContext.cradle, { supportMessageTypes: false }),
110+
).toThrowErrorMatchingInlineSnapshot(
111+
'[Error: if messageTypeField is not provided, messageSchemas must have a single schema]',
112+
)
113+
})
99114
})
100115

101116
describe('publish', () => {
102-
beforeEach(() => {
117+
it('should fail if topic is not supported', async () => {
118+
// Given
103119
publisher = new PermissionPublisher(testContext.cradle)
104-
})
105120

106-
it('should fail if topic is not supported', async () => {
107121
// When
108122
await expect(
109123
publisher.publish('bad topic' as any, {} as any), // Intentionally bad topic to force the error
@@ -114,6 +128,8 @@ describe('PermissionPublisher - init', () => {
114128

115129
it('should fail if there is no schema for message type', async () => {
116130
// Given
131+
publisher = new PermissionPublisher(testContext.cradle)
132+
117133
const message = {
118134
id: '1',
119135
type: 'bad' as any, // Intentionally bad type to force the error
@@ -128,6 +144,8 @@ describe('PermissionPublisher - init', () => {
128144

129145
it('should fail if message does not match schema', async () => {
130146
// Given
147+
publisher = new PermissionPublisher(testContext.cradle)
148+
131149
const message = {
132150
id: 1 as unknown as string,
133151
type: 'added',
@@ -154,6 +172,8 @@ describe('PermissionPublisher - init', () => {
154172

155173
it('should publish messages', async () => {
156174
// Given
175+
publisher = new PermissionPublisher(testContext.cradle)
176+
157177
const message1 = {
158178
id: '1',
159179
type: 'added',
@@ -176,5 +196,39 @@ describe('PermissionPublisher - init', () => {
176196
const emittedEvent2 = await publisher.handlerSpy.waitForMessageWithId('2', 'published')
177197
expect(emittedEvent2.message).toMatchObject(message2)
178198
})
199+
200+
it('should publish only messages meeting schema', async () => {
201+
// Given
202+
publisher = new PermissionPublisher(testContext.cradle, {
203+
supportMessageTypes: false,
204+
topicsConfig: [
205+
{
206+
topic: PERMISSION_GENERAL_TOPIC,
207+
schemas: [PERMISSION_ADDED_SCHEMA],
208+
},
209+
] as any, // we are not adding the other topics intentionally
210+
})
211+
212+
const messageValid = {
213+
id: '1',
214+
type: 'added',
215+
permissions: [],
216+
} satisfies PermissionAdded
217+
const messageInvalid = {
218+
id: '2',
219+
type: 'removed',
220+
permissions: [],
221+
} satisfies PermissionRemoved
222+
223+
// When&Then - valid
224+
await publisher.publish(PERMISSION_GENERAL_TOPIC, messageValid)
225+
const emittedEvent = await publisher.handlerSpy.waitForMessageWithId('1', 'published')
226+
expect(emittedEvent.message).toMatchObject(messageValid)
227+
228+
// When&Then - invalid
229+
await expect(
230+
publisher.publish(PERMISSION_GENERAL_TOPIC, messageInvalid),
231+
).rejects.toThrowErrorMatchingInlineSnapshot('[Error: Unsupported message type: removed]')
232+
})
179233
})
180234
})

packages/kafka/test/publisher/PermissionPublisher.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ type PermissionPublisherOptions = Partial<
1111
KafkaPublisherOptions<typeof PERMISSION_TOPIC_MESSAGES_CONFIG>,
1212
'handlerSpy' | 'kafka' | 'autocreateTopics' | 'topicsConfig'
1313
>
14-
>
14+
> & {
15+
supportMessageTypes?: boolean
16+
}
17+
1518
export class PermissionPublisher extends AbstractKafkaPublisher<
1619
typeof PERMISSION_TOPIC_MESSAGES_CONFIG
1720
> {
@@ -22,8 +25,8 @@ export class PermissionPublisher extends AbstractKafkaPublisher<
2225
autocreateTopics: options.autocreateTopics ?? true,
2326
handlerSpy: options?.handlerSpy ?? true,
2427
logMessages: true,
25-
messageIdField: 'id',
26-
messageTypeField: 'type',
28+
messageIdField: options.supportMessageTypes === false ? undefined : 'id',
29+
messageTypeField: options.supportMessageTypes === false ? undefined : 'type',
2730
})
2831
}
2932
}

packages/kafka/test/utils/permissionSchemas.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ export const PERMISSION_REMOVED_SCHEMA = BASE_SCHEMA.extend({
1616
})
1717
export type PermissionRemoved = z.infer<typeof PERMISSION_REMOVED_SCHEMA>
1818

19-
const PERMISSION_ADDED_TOPIC = 'permission-added'
20-
const PERMISSION_REMOVED_TOPIC = 'permission-removed'
21-
const PERMISSION_GENERAL_TOPIC = 'permission-general'
19+
export const PERMISSION_ADDED_TOPIC = 'permission-added'
20+
export const PERMISSION_REMOVED_TOPIC = 'permission-removed'
21+
export const PERMISSION_GENERAL_TOPIC = 'permission-general'
2222
export const TOPICS = [PERMISSION_ADDED_TOPIC, PERMISSION_REMOVED_TOPIC, PERMISSION_GENERAL_TOPIC]
2323

2424
export const PERMISSION_TOPIC_MESSAGES_CONFIG = [

0 commit comments

Comments
 (0)