11import { 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'
39import { type TestContext , createTestContext } from '../utils/testContext.js'
410import { 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} )
0 commit comments