@kumori/aurora-backend-handler 1.1.42 → 1.1.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,152 +1,537 @@
1
-
2
1
  import EventHelper from '../event-helper';
3
- import { EventNames } from './../event-names';
2
+ import { EventNames } from '../event-names';
3
+
4
+ const makeMockHandler = () => ({
5
+ publish: jest.fn(),
6
+ subscribe: jest.fn(),
7
+ unsubscribe: jest.fn(),
8
+ });
4
9
 
5
10
  beforeEach(() => {
6
11
  jest.clearAllMocks();
7
12
  });
8
13
 
9
- describe('EventHelper', () => {
10
- const dummyGlobalEventHandler = {
11
- publish: jest.fn(),
12
- subscribe: jest.fn(),
13
- unsubscribe: jest.fn(),
14
- };
15
-
16
- const eventHelper = new EventHelper(dummyGlobalEventHandler);
14
+ describe('EventHelper - createEvent defensive branches', () => {
15
+ it('log error cuando globalEventHandler es null', () => {
16
+ const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
17
+ const helper = new EventHelper(null as any);
18
+ try { helper.tenant.publish.creation({} as any); } catch {}
19
+ expect(consoleSpy).toHaveBeenCalled();
20
+ consoleSpy.mockRestore();
21
+ });
17
22
 
23
+ it('log error cuando subscribe no existe en el handler', () => {
24
+ const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
25
+ const handler = { publish: jest.fn(), unsubscribe: jest.fn() } as any;
26
+ const helper = new EventHelper(handler);
27
+ try { helper.tenant.subscribe.creation(() => {}); } catch {}
28
+ expect(consoleSpy).toHaveBeenCalled();
29
+ consoleSpy.mockRestore();
30
+ });
31
+ });
18
32
 
19
- // --- Tenant ---
20
- describe('Tenant events', () => {
21
- test('tenant.publish.creation llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
22
- const payload = { id: 'tenant1', name: 'Tenant One' };
23
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
24
- eventHelper.tenant.publish.creation(payload as any);
25
- expect(spy).toHaveBeenCalledWith(EventNames.CreateTenant, payload);
33
+ describe('EventHelper - Tenant events', () => {
34
+ const handler = makeMockHandler();
35
+ const helper = new EventHelper(handler);
36
+
37
+ const events: Array<{ method: string; eventName: EventNames }> = [
38
+ { method: 'creation', eventName: EventNames.CreateTenant },
39
+ { method: 'created', eventName: EventNames.TenantCreated },
40
+ { method: 'creationError', eventName: EventNames.TenantCreationError },
41
+ { method: 'update', eventName: EventNames.UpdateTenant },
42
+ { method: 'updated', eventName: EventNames.TenantUpdated },
43
+ { method: 'updateError', eventName: EventNames.TenantUpdateError },
44
+ { method: 'delete', eventName: EventNames.DeleteTenant },
45
+ { method: 'deleted', eventName: EventNames.TenantDeleted },
46
+ { method: 'deletionError', eventName: EventNames.TenantDeletionError },
47
+ { method: 'createRegistry', eventName: EventNames.CreateRegistry },
48
+ { method: 'registryCreated', eventName: EventNames.RegistryCreated },
49
+ { method: 'registryCreationError', eventName: EventNames.RegistryCreationError },
50
+ { method: 'updateRegistry', eventName: EventNames.UpdateRegistry },
51
+ { method: 'registryUpdated', eventName: EventNames.RegistryUpdated },
52
+ { method: 'registryUpdateError', eventName: EventNames.RegistryUpdateError },
53
+ { method: 'deleteRegistry', eventName: EventNames.DeleteRegistry },
54
+ { method: 'registryDeleted', eventName: EventNames.RegistryDeleted },
55
+ { method: 'registryDeletionError', eventName: EventNames.RegistryDeletionError },
56
+ { method: 'inviteUser', eventName: EventNames.InviteUser },
57
+ { method: 'userInvited', eventName: EventNames.UserInvited },
58
+ { method: 'inviteError', eventName: EventNames.UserInviteError },
59
+ { method: 'removeUser', eventName: EventNames.RemoveUserFromTenant },
60
+ { method: 'userRemoved', eventName: EventNames.UserRemovedFromTenant },
61
+ { method: 'removeUserError', eventName: EventNames.UserRemovalFromTenantError },
62
+ { method: 'acceptInvite', eventName: EventNames.AcceptInvite },
63
+ { method: 'inviteAccepted', eventName: EventNames.InviteAccepted },
64
+ { method: 'acceptInviteError', eventName: EventNames.InviteAcceptError },
65
+ { method: 'updateInvite', eventName: EventNames.UpdateInvite },
66
+ { method: 'inviteUpdated', eventName: EventNames.InviteUpdated },
67
+ { method: 'inviteUpdateError', eventName: EventNames.InviteUpdateError },
68
+ { method: 'rejectInvite', eventName: EventNames.RejectInvite },
69
+ { method: 'inviteRejected', eventName: EventNames.InviteRejected },
70
+ { method: 'inviteRejectError', eventName: EventNames.InviteRejectError },
71
+ { method: 'createToken', eventName: EventNames.CreateToken },
72
+ { method: 'tokenCreated', eventName: EventNames.TokenCreated },
73
+ { method: 'tokenCreationError', eventName: EventNames.TokenCreationError },
74
+ { method: 'deleteToken', eventName: EventNames.DeleteToken },
75
+ { method: 'tokenDeleted', eventName: EventNames.TokenDeleted },
76
+ { method: 'tokenDeletionError', eventName: EventNames.TokenDeletionError },
77
+ ];
78
+
79
+ events.forEach(({ method, eventName }) => {
80
+ it(`tenant.publish.${method} usa ${eventName}`, () => {
81
+ jest.clearAllMocks();
82
+ const payload = {};
83
+ (helper.tenant.publish as any)[method](payload);
84
+ expect(handler.publish).toHaveBeenCalledWith(eventName, payload);
26
85
  });
27
86
 
28
- test('tenant.subscribe.creation llama a globalEventHandler.subscribe con el nombre de evento correcto y callback', () => {
29
- const callback = jest.fn();
30
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'subscribe');
31
- eventHelper.tenant.subscribe.creation(callback);
32
- expect(spy).toHaveBeenCalledWith(EventNames.CreateTenant, callback);
87
+ it(`tenant.subscribe.${method} usa ${eventName}`, () => {
88
+ jest.clearAllMocks();
89
+ const cb = jest.fn();
90
+ (helper.tenant.subscribe as any)[method](cb);
91
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
33
92
  });
34
93
 
35
- test('tenant.unsubscribe.creation llama a globalEventHandler.unsubscribe con el nombre de evento correcto y callback', () => {
36
- const callback = jest.fn();
37
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'unsubscribe');
38
- eventHelper.tenant.unsubscribe.creation(callback);
39
- expect(spy).toHaveBeenCalledWith(EventNames.CreateTenant, callback);
94
+ it(`tenant.unsubscribe.${method} usa ${eventName}`, () => {
95
+ jest.clearAllMocks();
96
+ const cb = jest.fn();
97
+ (helper.tenant.unsubscribe as any)[method](cb);
98
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
40
99
  });
41
100
  });
101
+ });
102
+
103
+ describe('EventHelper - User events', () => {
104
+ const handler = makeMockHandler();
105
+ const helper = new EventHelper(handler);
106
+
107
+ const events: Array<{ method: string; eventName: EventNames }> = [
108
+ { method: 'creation', eventName: EventNames.CreateUser },
109
+ { method: 'created', eventName: EventNames.UserCreated },
110
+ { method: 'creationError', eventName: EventNames.UserCreationError },
111
+ { method: 'update', eventName: EventNames.UpdateUser },
112
+ { method: 'updated', eventName: EventNames.UserUpdated },
113
+ { method: 'updateError', eventName: EventNames.UserUpdateError },
114
+ { method: 'load', eventName: EventNames.LoadUser },
115
+ { method: 'loaded', eventName: EventNames.UserLoaded },
116
+ { method: 'loadError', eventName: EventNames.UserLoadError },
117
+ { method: 'delete', eventName: EventNames.DeleteUser },
118
+ { method: 'deleted', eventName: EventNames.UserDeleted },
119
+ { method: 'deletionError', eventName: EventNames.UserDeletionError },
120
+ { method: 'authError', eventName: EventNames.AuthError },
121
+ ];
42
122
 
43
- // --- User ---
44
- describe('User events', () => {
45
- test('user.publish.creation llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
46
- const payload = { id: 'user1', username: 'john.doe' };
47
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
48
- eventHelper.user.publish.creation(payload as any);
49
- expect(spy).toHaveBeenCalledWith(EventNames.CreateUser, payload);
123
+ events.forEach(({ method, eventName }) => {
124
+ it(`user.publish.${method} usa ${eventName}`, () => {
125
+ jest.clearAllMocks();
126
+ (helper.user.publish as any)[method]({});
127
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
50
128
  });
51
129
 
52
- test('user.subscribe.creation llama a globalEventHandler.subscribe con el nombre de evento correcto y callback', () => {
53
- const callback = jest.fn();
54
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'subscribe');
55
- eventHelper.user.subscribe.creation(callback);
56
- expect(spy).toHaveBeenCalledWith(EventNames.CreateUser, callback);
130
+ it(`user.subscribe.${method} usa ${eventName}`, () => {
131
+ jest.clearAllMocks();
132
+ const cb = jest.fn();
133
+ (helper.user.subscribe as any)[method](cb);
134
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
135
+ });
136
+
137
+ it(`user.unsubscribe.${method} usa ${eventName}`, () => {
138
+ jest.clearAllMocks();
139
+ const cb = jest.fn();
140
+ (helper.user.unsubscribe as any)[method](cb);
141
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
57
142
  });
58
143
  });
144
+ });
59
145
 
60
- // --- Account ---
61
- describe('Account events', () => {
62
- test('account.publish.creation llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
63
- const payload = { id: 'acc1', name: 'Account One' };
64
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
65
- eventHelper.account.publish.creation(payload as any);
66
- expect(spy).toHaveBeenCalledWith(EventNames.CreateAccount, payload);
146
+ describe('EventHelper - Account events', () => {
147
+ const handler = makeMockHandler();
148
+ const helper = new EventHelper(handler);
149
+
150
+ const events: Array<{ method: string; eventName: EventNames }> = [
151
+ { method: 'creation', eventName: EventNames.CreateAccount },
152
+ { method: 'created', eventName: EventNames.AccountCreated },
153
+ { method: 'creationError', eventName: EventNames.AccountCreationError },
154
+ { method: 'update', eventName: EventNames.UpdateAccount },
155
+ { method: 'updated', eventName: EventNames.AccountUpdated },
156
+ { method: 'updateError', eventName: EventNames.AccountUpdateError },
157
+ { method: 'delete', eventName: EventNames.DeleteAccount },
158
+ { method: 'deleted', eventName: EventNames.AccountDeleted },
159
+ { method: 'deletionError', eventName: EventNames.AccountDeletionError },
160
+ { method: 'clean', eventName: EventNames.CleanAccount },
161
+ { method: 'cleaned', eventName: EventNames.AccountCleaned },
162
+ { method: 'cleanError', eventName: EventNames.AccountCleanError },
163
+ ];
164
+
165
+ events.forEach(({ method, eventName }) => {
166
+ it(`account.publish.${method} usa ${eventName}`, () => {
167
+ jest.clearAllMocks();
168
+ (helper.account.publish as any)[method]({});
169
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
170
+ });
171
+
172
+ it(`account.subscribe.${method} usa ${eventName}`, () => {
173
+ jest.clearAllMocks();
174
+ const cb = jest.fn();
175
+ (helper.account.subscribe as any)[method](cb);
176
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
177
+ });
178
+
179
+ it(`account.unsubscribe.${method} usa ${eventName}`, () => {
180
+ jest.clearAllMocks();
181
+ const cb = jest.fn();
182
+ (helper.account.unsubscribe as any)[method](cb);
183
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
67
184
  });
68
185
  });
186
+ });
187
+
188
+ describe('EventHelper - Environment events', () => {
189
+ const handler = makeMockHandler();
190
+ const helper = new EventHelper(handler);
69
191
 
70
- // --- Environment ---
71
- describe('Environment events', () => {
72
- test('environment.publish.creation llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
73
- const payload = { id: 'env1', name: 'Environment One' };
74
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
75
- eventHelper.environment.publish.creation(payload as any);
76
- expect(spy).toHaveBeenCalledWith(EventNames.CreateEnvironment, payload);
192
+ const events: Array<{ method: string; eventName: EventNames }> = [
193
+ { method: 'creation', eventName: EventNames.CreateEnvironment },
194
+ { method: 'created', eventName: EventNames.EnvironmentCreated },
195
+ { method: 'creationError', eventName: EventNames.EnvironmentCreationError },
196
+ { method: 'update', eventName: EventNames.UpdateEnvironment },
197
+ { method: 'updated', eventName: EventNames.EnvironmentUpdated },
198
+ { method: 'updateError', eventName: EventNames.EnvironmentUpdateError },
199
+ { method: 'delete', eventName: EventNames.DeleteEnvironment },
200
+ { method: 'deleted', eventName: EventNames.EnvironmentDeleted },
201
+ { method: 'deletionError', eventName: EventNames.EvironmentDeletionError },
202
+ { method: 'clean', eventName: EventNames.CleanEnvironment },
203
+ { method: 'cleaned', eventName: EventNames.EnvironmentCleaned },
204
+ { method: 'cleanError', eventName: EventNames.EnvironmentCleanError },
205
+ { method: 'scale', eventName: EventNames.ScaleEnvironment },
206
+ { method: 'scaled', eventName: EventNames.EnvironmentScaled },
207
+ { method: 'scaleError', eventName: EventNames.EnvironmentScaleError },
208
+ ];
209
+
210
+ events.forEach(({ method, eventName }) => {
211
+ it(`environment.publish.${method} usa ${eventName}`, () => {
212
+ jest.clearAllMocks();
213
+ (helper.environment.publish as any)[method]({});
214
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
215
+ });
216
+
217
+ it(`environment.subscribe.${method} usa ${eventName}`, () => {
218
+ jest.clearAllMocks();
219
+ const cb = jest.fn();
220
+ (helper.environment.subscribe as any)[method](cb);
221
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
222
+ });
223
+
224
+ it(`environment.unsubscribe.${method} usa ${eventName}`, () => {
225
+ jest.clearAllMocks();
226
+ const cb = jest.fn();
227
+ (helper.environment.unsubscribe as any)[method](cb);
228
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
77
229
  });
78
230
  });
231
+ });
79
232
 
80
- // --- Service ---
81
- describe('Service events', () => {
82
- test('service.publish.deploy llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
83
- const payload = { id: 'svc1', name: 'Service One' };
84
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
85
- eventHelper.service.publish.deploy(payload as any);
86
- expect(spy).toHaveBeenCalledWith(EventNames.DeployService, payload);
233
+ describe('EventHelper - Service events', () => {
234
+ const handler = makeMockHandler();
235
+ const helper = new EventHelper(handler);
236
+
237
+ const events: Array<{ method: string; eventName: EventNames }> = [
238
+ { method: 'deploy', eventName: EventNames.DeployService },
239
+ { method: 'deployed', eventName: EventNames.ServiceDeployed },
240
+ { method: 'deploymentError', eventName: EventNames.ServiceDeploymentError },
241
+ { method: 'update', eventName: EventNames.UpdateService },
242
+ { method: 'updated', eventName: EventNames.ServiceUpdated },
243
+ { method: 'updateError', eventName: EventNames.ServiceUpdateError },
244
+ { method: 'delete', eventName: EventNames.DeleteService },
245
+ { method: 'deleted', eventName: EventNames.ServiceDeleted },
246
+ { method: 'deletionError', eventName: EventNames.ServiceDeletionError },
247
+ { method: 'requestLogs', eventName: EventNames.RequestLogs },
248
+ { method: 'restart', eventName: EventNames.RestartService },
249
+ { method: 'restarted', eventName: EventNames.ServiceRestarted },
250
+ { method: 'restartError', eventName: EventNames.ServiceRestartError },
251
+ { method: 'requestRevisionData', eventName: EventNames.RequestRevisionData },
252
+ { method: 'updateServiceLinks', eventName: EventNames.updateServiceLinks },
253
+ { method: 'changeRevision', eventName: EventNames.ChangeRevision },
254
+ { method: 'revisionChanged', eventName: EventNames.RevisionChanged },
255
+ { method: 'revisionChangeError', eventName: EventNames.RevisionChangeError },
256
+ { method: 'restartInstance', eventName: EventNames.RestartInstance },
257
+ { method: 'instanceRestarted', eventName: EventNames.InstanceRestarted },
258
+ { method: 'instanceRestartError', eventName: EventNames.InstanceRestartError },
259
+ ];
260
+
261
+ events.forEach(({ method, eventName }) => {
262
+ it(`service.publish.${method} usa ${eventName}`, () => {
263
+ jest.clearAllMocks();
264
+ (helper.service.publish as any)[method]({});
265
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
266
+ });
267
+
268
+ it(`service.subscribe.${method} usa ${eventName}`, () => {
269
+ jest.clearAllMocks();
270
+ const cb = jest.fn();
271
+ (helper.service.subscribe as any)[method](cb);
272
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
273
+ });
274
+
275
+ it(`service.unsubscribe.${method} usa ${eventName}`, () => {
276
+ jest.clearAllMocks();
277
+ const cb = jest.fn();
278
+ (helper.service.unsubscribe as any)[method](cb);
279
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
87
280
  });
88
281
  });
282
+ });
283
+
284
+ describe('EventHelper - Marketplace events', () => {
285
+ const handler = makeMockHandler();
286
+ const helper = new EventHelper(handler);
287
+
288
+ const events: Array<{ method: string; eventName: EventNames }> = [
289
+ { method: 'deployItem', eventName: EventNames.DeployMarketplaceItem },
290
+ { method: 'itemDeployed', eventName: EventNames.MarketplaceItemDeployed },
291
+ { method: 'deploymentError', eventName: EventNames.MarketplaceItemDeploymentError },
292
+ { method: 'updateItem', eventName: EventNames.UpdateMarketplaceItem },
293
+ { method: 'itemUpdated', eventName: EventNames.MarketplaceItemUpdated },
294
+ { method: 'updateError', eventName: EventNames.MarketplaceItemUpdateError },
295
+ { method: 'deleteItem', eventName: EventNames.DeleteMarketplaceItem },
296
+ { method: 'itemDeleted', eventName: EventNames.MarketplaceItemDeleted },
297
+ { method: 'deletionError', eventName: EventNames.MarketplaceItemDeletionError },
298
+ { method: 'loadItems', eventName: EventNames.LoadMarketplaceItems },
299
+ { method: 'itemsLoaded', eventName: EventNames.MarketplaceItemsLoaded },
300
+ { method: 'loadSchema', eventName: EventNames.LoadMarketplaceSchema },
301
+ { method: 'schemaLoaded', eventName: EventNames.MarketplaceSchemaLoaded },
302
+ { method: 'schemaLoadError', eventName: EventNames.MarketplaceSchemaLoadError },
303
+ ];
304
+
305
+ events.forEach(({ method, eventName }) => {
306
+ it(`marketplace.publish.${method} usa ${eventName}`, () => {
307
+ jest.clearAllMocks();
308
+ (helper.marketplace.publish as any)[method]({});
309
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
310
+ });
311
+
312
+ it(`marketplace.subscribe.${method} usa ${eventName}`, () => {
313
+ jest.clearAllMocks();
314
+ const cb = jest.fn();
315
+ (helper.marketplace.subscribe as any)[method](cb);
316
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
317
+ });
89
318
 
90
- // --- Marketplace ---
91
- describe('Marketplace events', () => {
92
- test('marketplace.publish.deployItem llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
93
- const payload = { id: 'mkt1', name: 'Marketplace Item' };
94
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
95
- eventHelper.marketplace.publish.deployItem(payload as any);
96
- expect(spy).toHaveBeenCalledWith(EventNames.DeployMarketplaceItem, payload);
319
+ it(`marketplace.unsubscribe.${method} usa ${eventName}`, () => {
320
+ jest.clearAllMocks();
321
+ const cb = jest.fn();
322
+ (helper.marketplace.unsubscribe as any)[method](cb);
323
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
97
324
  });
98
325
  });
326
+ });
327
+
328
+ describe('EventHelper - Resource events', () => {
329
+ const handler = makeMockHandler();
330
+ const helper = new EventHelper(handler);
99
331
 
100
- // --- Resource ---
101
- describe('Resource events', () => {
102
- test('resource.publish.creation llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
103
- const payload = { id: 'res1', name: 'Resource One' };
104
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
105
- eventHelper.resource.publish.creation(payload as any);
106
- expect(spy).toHaveBeenCalledWith(EventNames.CreateResource, payload);
332
+ const events: Array<{ method: string; eventName: EventNames }> = [
333
+ { method: 'creation', eventName: EventNames.CreateResource },
334
+ { method: 'created', eventName: EventNames.ResourceCreated },
335
+ { method: 'creationError', eventName: EventNames.ResourceCreationError },
336
+ { method: 'update', eventName: EventNames.UpdateResource },
337
+ { method: 'updated', eventName: EventNames.ResourceUpdated },
338
+ { method: 'updateError', eventName: EventNames.ResourceUpdateError },
339
+ { method: 'delete', eventName: EventNames.DeleteResource },
340
+ { method: 'deleted', eventName: EventNames.ResourceDeleted },
341
+ { method: 'deletionError', eventName: EventNames.ResourceDeletionError },
342
+ ];
343
+
344
+ events.forEach(({ method, eventName }) => {
345
+ it(`resource.publish.${method} usa ${eventName}`, () => {
346
+ jest.clearAllMocks();
347
+ (helper.resource.publish as any)[method]({});
348
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
349
+ });
350
+
351
+ it(`resource.subscribe.${method} usa ${eventName}`, () => {
352
+ jest.clearAllMocks();
353
+ const cb = jest.fn();
354
+ (helper.resource.subscribe as any)[method](cb);
355
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
356
+ });
357
+
358
+ it(`resource.unsubscribe.${method} usa ${eventName}`, () => {
359
+ jest.clearAllMocks();
360
+ const cb = jest.fn();
361
+ (helper.resource.unsubscribe as any)[method](cb);
362
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
107
363
  });
108
364
  });
365
+ });
109
366
 
110
- // --- Plan (payload de tipo string) ---
111
- describe('Plan events', () => {
112
- test('plan.publish.upgrade llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
113
- const payload = 'Upgrade Data';
114
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
115
- eventHelper.plan.publish.upgrade(payload);
116
- expect(spy).toHaveBeenCalledWith(EventNames.UpdatePlan, payload);
367
+ describe('EventHelper - Plan events', () => {
368
+ const handler = makeMockHandler();
369
+ const helper = new EventHelper(handler);
370
+
371
+ const events: Array<{ method: string; eventName: EventNames }> = [
372
+ { method: 'upgrade', eventName: EventNames.UpdatePlan },
373
+ { method: 'upgraded', eventName: EventNames.PlanUpdated },
374
+ { method: 'upgradeError', eventName: EventNames.PlanUpdateError },
375
+ { method: 'downgrade', eventName: EventNames.DowngradePlan },
376
+ { method: 'downgraded', eventName: EventNames.PlanDowngraded },
377
+ { method: 'downgradeError', eventName: EventNames.PlanDowngradeError },
378
+ ];
379
+
380
+ events.forEach(({ method, eventName }) => {
381
+ it(`plan.publish.${method} usa ${eventName}`, () => {
382
+ jest.clearAllMocks();
383
+ (helper.plan.publish as any)[method]('test');
384
+ expect(handler.publish).toHaveBeenCalledWith(eventName, 'test');
385
+ });
386
+
387
+ it(`plan.subscribe.${method} usa ${eventName}`, () => {
388
+ jest.clearAllMocks();
389
+ const cb = jest.fn();
390
+ (helper.plan.subscribe as any)[method](cb);
391
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
392
+ });
393
+
394
+ it(`plan.unsubscribe.${method} usa ${eventName}`, () => {
395
+ jest.clearAllMocks();
396
+ const cb = jest.fn();
397
+ (helper.plan.unsubscribe as any)[method](cb);
398
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
117
399
  });
118
400
  });
401
+ });
402
+
403
+ describe('EventHelper - Organization events', () => {
404
+ const handler = makeMockHandler();
405
+ const helper = new EventHelper(handler);
406
+
407
+ const events: Array<{ method: string; eventName: EventNames }> = [
408
+ { method: 'creation', eventName: EventNames.CreateOrganization },
409
+ { method: 'created', eventName: EventNames.OrganizationCreated },
410
+ { method: 'creationError', eventName: EventNames.OrganizationCreationError },
411
+ { method: 'update', eventName: EventNames.UpdateOrganization },
412
+ { method: 'updated', eventName: EventNames.OrganizationUpdated },
413
+ { method: 'updateError', eventName: EventNames.OrganizationUpdateError },
414
+ { method: 'delete', eventName: EventNames.DeleteOrganization },
415
+ { method: 'deleted', eventName: EventNames.OrganizationDeleted },
416
+ { method: 'deletionError', eventName: EventNames.OrganizationDeletionError },
417
+ ];
418
+
419
+ events.forEach(({ method, eventName }) => {
420
+ it(`organization.publish.${method} usa ${eventName}`, () => {
421
+ jest.clearAllMocks();
422
+ (helper.organization.publish as any)[method]({});
423
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
424
+ });
425
+
426
+ it(`organization.subscribe.${method} usa ${eventName}`, () => {
427
+ jest.clearAllMocks();
428
+ const cb = jest.fn();
429
+ (helper.organization.subscribe as any)[method](cb);
430
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
431
+ });
119
432
 
120
- // --- Organization ---
121
- describe('Organization events', () => {
122
- test('organization.publish.creation llama a globalEventHandler.publish con el nombre de evento correcto y payload', () => {
123
- const payload = { id: 'org1', name: 'Organization One' };
124
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
125
- eventHelper.organization.publish.creation(payload as any);
126
- expect(spy).toHaveBeenCalledWith(EventNames.CreateOrganization, payload);
433
+ it(`organization.unsubscribe.${method} usa ${eventName}`, () => {
434
+ jest.clearAllMocks();
435
+ const cb = jest.fn();
436
+ (helper.organization.unsubscribe as any)[method](cb);
437
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
127
438
  });
128
439
  });
440
+ });
441
+
442
+ describe('EventHelper - Notification events', () => {
443
+ const handler = makeMockHandler();
444
+ const helper = new EventHelper(handler);
129
445
 
130
- // --- PlanProviders ---
131
- describe('PlanProviders events', () => {
132
- test('planProviders.publish.getPlans llama a globalEventHandler.publish con el nombre de evento correcto', () => {
133
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'publish');
134
- eventHelper.planProviders.publish.loadPlans();
135
- expect(spy).toHaveBeenCalledWith(EventNames.LoadPlanProviders, undefined);
446
+ const events: Array<{ method: string; eventName: EventNames }> = [
447
+ { method: 'creation', eventName: EventNames.CreateNotification },
448
+ { method: 'deletion', eventName: EventNames.DeleteNotification },
449
+ { method: 'read', eventName: EventNames.NotificationRead },
450
+ ];
451
+
452
+ events.forEach(({ method, eventName }) => {
453
+ it(`notification.publish.${method} usa ${eventName}`, () => {
454
+ jest.clearAllMocks();
455
+ (helper.notification.publish as any)[method]({});
456
+ expect(handler.publish).toHaveBeenCalledWith(eventName, {});
136
457
  });
137
458
 
138
- test('planProviders.subscribe.getPlans llama a globalEventHandler.subscribe con el nombre de evento correcto y callback', () => {
139
- const callback = jest.fn();
140
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'subscribe');
141
- eventHelper.planProviders.subscribe.loadPlans(callback);
142
- expect(spy).toHaveBeenCalledWith(EventNames.LoadPlanProviders, callback);
459
+ it(`notification.subscribe.${method} usa ${eventName}`, () => {
460
+ jest.clearAllMocks();
461
+ const cb = jest.fn();
462
+ (helper.notification.subscribe as any)[method](cb);
463
+ expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb);
143
464
  });
144
465
 
145
- test('planProviders.unsubscribe.getPlans llama a globalEventHandler.unsubscribe con el nombre de evento correcto y callback', () => {
146
- const callback = jest.fn();
147
- const spy = jest.spyOn(eventHelper.globalEventHandler, 'unsubscribe');
148
- eventHelper.planProviders.unsubscribe.loadPlans(callback);
149
- expect(spy).toHaveBeenCalledWith(EventNames.LoadPlanProviders, callback);
466
+ it(`notification.unsubscribe.${method} usa ${eventName}`, () => {
467
+ jest.clearAllMocks();
468
+ const cb = jest.fn();
469
+ (helper.notification.unsubscribe as any)[method](cb);
470
+ expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb);
150
471
  });
151
472
  });
152
473
  });
474
+
475
+ describe('EventHelper - PlanProviders events', () => {
476
+ const handler = makeMockHandler();
477
+ const helper = new EventHelper(handler);
478
+
479
+ it('planProviders.publish.loadPlans usa LoadPlanProviders', () => {
480
+ helper.planProviders.publish.loadPlans();
481
+ expect(handler.publish).toHaveBeenCalledWith(EventNames.LoadPlanProviders, undefined);
482
+ });
483
+
484
+ it('planProviders.publish.plansLoaded usa PlanProvidersLoaded', () => {
485
+ jest.clearAllMocks();
486
+ helper.planProviders.publish.plansLoaded([]);
487
+ expect(handler.publish).toHaveBeenCalledWith(EventNames.PlanProvidersLoaded, []);
488
+ });
489
+
490
+ it('planProviders.publish.loadError usa PlanProvidersLoadError', () => {
491
+ jest.clearAllMocks();
492
+ helper.planProviders.publish.loadError();
493
+ expect(handler.publish).toHaveBeenCalledWith(EventNames.PlanProvidersLoadError, undefined);
494
+ });
495
+
496
+ it('planProviders.subscribe.loadPlans usa LoadPlanProviders', () => {
497
+ jest.clearAllMocks();
498
+ const cb = jest.fn();
499
+ helper.planProviders.subscribe.loadPlans(cb);
500
+ expect(handler.subscribe).toHaveBeenCalledWith(EventNames.LoadPlanProviders, cb);
501
+ });
502
+
503
+ it('planProviders.subscribe.plansLoaded usa PlanProvidersLoaded', () => {
504
+ jest.clearAllMocks();
505
+ const cb = jest.fn();
506
+ helper.planProviders.subscribe.plansLoaded(cb);
507
+ expect(handler.subscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoaded, cb);
508
+ });
509
+
510
+ it('planProviders.subscribe.loadError usa PlanProvidersLoadError', () => {
511
+ jest.clearAllMocks();
512
+ const cb = jest.fn();
513
+ helper.planProviders.subscribe.loadError(cb);
514
+ expect(handler.subscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoadError, cb);
515
+ });
516
+
517
+ it('planProviders.unsubscribe.loadPlans usa LoadPlanProviders', () => {
518
+ jest.clearAllMocks();
519
+ const cb = jest.fn();
520
+ helper.planProviders.unsubscribe.loadPlans(cb);
521
+ expect(handler.unsubscribe).toHaveBeenCalledWith(EventNames.LoadPlanProviders, cb);
522
+ });
523
+
524
+ it('planProviders.unsubscribe.plansLoaded usa PlanProvidersLoaded', () => {
525
+ jest.clearAllMocks();
526
+ const cb = jest.fn();
527
+ helper.planProviders.unsubscribe.plansLoaded(cb);
528
+ expect(handler.unsubscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoaded, cb);
529
+ });
530
+
531
+ it('planProviders.unsubscribe.loadError usa PlanProvidersLoadError', () => {
532
+ jest.clearAllMocks();
533
+ const cb = jest.fn();
534
+ helper.planProviders.unsubscribe.loadError(cb);
535
+ expect(handler.unsubscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoadError, cb);
536
+ });
537
+ });