import EventHelper from '../event-helper'; import { EventNames } from '../event-names'; const makeMockHandler = () => ({ publish: jest.fn(), subscribe: jest.fn(), unsubscribe: jest.fn(), }); beforeEach(() => { jest.clearAllMocks(); }); describe('EventHelper - createEvent defensive branches', () => { it('log error cuando globalEventHandler es null', () => { const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const helper = new EventHelper(null as any); try { helper.tenant.publish.creation({} as any); } catch {} expect(consoleSpy).toHaveBeenCalled(); consoleSpy.mockRestore(); }); it('log error cuando subscribe no existe en el handler', () => { const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const handler = { publish: jest.fn(), unsubscribe: jest.fn() } as any; const helper = new EventHelper(handler); try { helper.tenant.subscribe.creation(() => {}); } catch {} expect(consoleSpy).toHaveBeenCalled(); consoleSpy.mockRestore(); }); }); describe('EventHelper - Tenant events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'creation', eventName: EventNames.CreateTenant }, { method: 'created', eventName: EventNames.TenantCreated }, { method: 'creationError', eventName: EventNames.TenantCreationError }, { method: 'update', eventName: EventNames.UpdateTenant }, { method: 'updated', eventName: EventNames.TenantUpdated }, { method: 'updateError', eventName: EventNames.TenantUpdateError }, { method: 'delete', eventName: EventNames.DeleteTenant }, { method: 'deleted', eventName: EventNames.TenantDeleted }, { method: 'deletionError', eventName: EventNames.TenantDeletionError }, { method: 'createRegistry', eventName: EventNames.CreateRegistry }, { method: 'registryCreated', eventName: EventNames.RegistryCreated }, { method: 'registryCreationError', eventName: EventNames.RegistryCreationError }, { method: 'updateRegistry', eventName: EventNames.UpdateRegistry }, { method: 'registryUpdated', eventName: EventNames.RegistryUpdated }, { method: 'registryUpdateError', eventName: EventNames.RegistryUpdateError }, { method: 'deleteRegistry', eventName: EventNames.DeleteRegistry }, { method: 'registryDeleted', eventName: EventNames.RegistryDeleted }, { method: 'registryDeletionError', eventName: EventNames.RegistryDeletionError }, { method: 'inviteUser', eventName: EventNames.InviteUser }, { method: 'userInvited', eventName: EventNames.UserInvited }, { method: 'inviteError', eventName: EventNames.UserInviteError }, { method: 'removeUser', eventName: EventNames.RemoveUserFromTenant }, { method: 'userRemoved', eventName: EventNames.UserRemovedFromTenant }, { method: 'removeUserError', eventName: EventNames.UserRemovalFromTenantError }, { method: 'acceptInvite', eventName: EventNames.AcceptInvite }, { method: 'inviteAccepted', eventName: EventNames.InviteAccepted }, { method: 'acceptInviteError', eventName: EventNames.InviteAcceptError }, { method: 'updateInvite', eventName: EventNames.UpdateInvite }, { method: 'inviteUpdated', eventName: EventNames.InviteUpdated }, { method: 'inviteUpdateError', eventName: EventNames.InviteUpdateError }, { method: 'rejectInvite', eventName: EventNames.RejectInvite }, { method: 'inviteRejected', eventName: EventNames.InviteRejected }, { method: 'inviteRejectError', eventName: EventNames.InviteRejectError }, { method: 'createToken', eventName: EventNames.CreateToken }, { method: 'tokenCreated', eventName: EventNames.TokenCreated }, { method: 'tokenCreationError', eventName: EventNames.TokenCreationError }, { method: 'deleteToken', eventName: EventNames.DeleteToken }, { method: 'tokenDeleted', eventName: EventNames.TokenDeleted }, { method: 'tokenDeletionError', eventName: EventNames.TokenDeletionError }, ]; events.forEach(({ method, eventName }) => { it(`tenant.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const payload = {}; (helper.tenant.publish as any)[method](payload); expect(handler.publish).toHaveBeenCalledWith(eventName, payload); }); it(`tenant.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.tenant.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`tenant.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.tenant.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - User events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'creation', eventName: EventNames.CreateUser }, { method: 'created', eventName: EventNames.UserCreated }, { method: 'creationError', eventName: EventNames.UserCreationError }, { method: 'update', eventName: EventNames.UpdateUser }, { method: 'updated', eventName: EventNames.UserUpdated }, { method: 'updateError', eventName: EventNames.UserUpdateError }, { method: 'load', eventName: EventNames.LoadUser }, { method: 'loaded', eventName: EventNames.UserLoaded }, { method: 'loadError', eventName: EventNames.UserLoadError }, { method: 'delete', eventName: EventNames.DeleteUser }, { method: 'deleted', eventName: EventNames.UserDeleted }, { method: 'deletionError', eventName: EventNames.UserDeletionError }, { method: 'authError', eventName: EventNames.AuthError }, ]; events.forEach(({ method, eventName }) => { it(`user.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.user.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`user.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.user.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`user.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.user.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Account events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'creation', eventName: EventNames.CreateAccount }, { method: 'created', eventName: EventNames.AccountCreated }, { method: 'creationError', eventName: EventNames.AccountCreationError }, { method: 'update', eventName: EventNames.UpdateAccount }, { method: 'updated', eventName: EventNames.AccountUpdated }, { method: 'updateError', eventName: EventNames.AccountUpdateError }, { method: 'delete', eventName: EventNames.DeleteAccount }, { method: 'deleted', eventName: EventNames.AccountDeleted }, { method: 'deletionError', eventName: EventNames.AccountDeletionError }, { method: 'clean', eventName: EventNames.CleanAccount }, { method: 'cleaned', eventName: EventNames.AccountCleaned }, { method: 'cleanError', eventName: EventNames.AccountCleanError }, ]; events.forEach(({ method, eventName }) => { it(`account.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.account.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`account.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.account.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`account.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.account.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Environment events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'creation', eventName: EventNames.CreateEnvironment }, { method: 'created', eventName: EventNames.EnvironmentCreated }, { method: 'creationError', eventName: EventNames.EnvironmentCreationError }, { method: 'update', eventName: EventNames.UpdateEnvironment }, { method: 'updated', eventName: EventNames.EnvironmentUpdated }, { method: 'updateError', eventName: EventNames.EnvironmentUpdateError }, { method: 'delete', eventName: EventNames.DeleteEnvironment }, { method: 'deleted', eventName: EventNames.EnvironmentDeleted }, { method: 'deletionError', eventName: EventNames.EvironmentDeletionError }, { method: 'clean', eventName: EventNames.CleanEnvironment }, { method: 'cleaned', eventName: EventNames.EnvironmentCleaned }, { method: 'cleanError', eventName: EventNames.EnvironmentCleanError }, { method: 'scale', eventName: EventNames.ScaleEnvironment }, { method: 'scaled', eventName: EventNames.EnvironmentScaled }, { method: 'scaleError', eventName: EventNames.EnvironmentScaleError }, ]; events.forEach(({ method, eventName }) => { it(`environment.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.environment.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`environment.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.environment.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`environment.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.environment.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Service events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'deploy', eventName: EventNames.DeployService }, { method: 'deployed', eventName: EventNames.ServiceDeployed }, { method: 'deploymentError', eventName: EventNames.ServiceDeploymentError }, { method: 'update', eventName: EventNames.UpdateService }, { method: 'updated', eventName: EventNames.ServiceUpdated }, { method: 'updateError', eventName: EventNames.ServiceUpdateError }, { method: 'delete', eventName: EventNames.DeleteService }, { method: 'deleted', eventName: EventNames.ServiceDeleted }, { method: 'deletionError', eventName: EventNames.ServiceDeletionError }, { method: 'requestLogs', eventName: EventNames.RequestLogs }, { method: 'restart', eventName: EventNames.RestartService }, { method: 'restarted', eventName: EventNames.ServiceRestarted }, { method: 'restartError', eventName: EventNames.ServiceRestartError }, { method: 'requestRevisionData', eventName: EventNames.RequestRevisionData }, { method: 'updateServiceLinks', eventName: EventNames.updateServiceLinks }, { method: 'changeRevision', eventName: EventNames.ChangeRevision }, { method: 'revisionChanged', eventName: EventNames.RevisionChanged }, { method: 'revisionChangeError', eventName: EventNames.RevisionChangeError }, { method: 'restartInstance', eventName: EventNames.RestartInstance }, { method: 'instanceRestarted', eventName: EventNames.InstanceRestarted }, { method: 'instanceRestartError', eventName: EventNames.InstanceRestartError }, ]; events.forEach(({ method, eventName }) => { it(`service.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.service.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`service.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.service.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`service.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.service.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Marketplace events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'deployItem', eventName: EventNames.DeployMarketplaceItem }, { method: 'itemDeployed', eventName: EventNames.MarketplaceItemDeployed }, { method: 'deploymentError', eventName: EventNames.MarketplaceItemDeploymentError }, { method: 'updateItem', eventName: EventNames.UpdateMarketplaceItem }, { method: 'itemUpdated', eventName: EventNames.MarketplaceItemUpdated }, { method: 'updateError', eventName: EventNames.MarketplaceItemUpdateError }, { method: 'deleteItem', eventName: EventNames.DeleteMarketplaceItem }, { method: 'itemDeleted', eventName: EventNames.MarketplaceItemDeleted }, { method: 'deletionError', eventName: EventNames.MarketplaceItemDeletionError }, { method: 'loadItems', eventName: EventNames.LoadMarketplaceItems }, { method: 'itemsLoaded', eventName: EventNames.MarketplaceItemsLoaded }, { method: 'loadSchema', eventName: EventNames.LoadMarketplaceSchema }, { method: 'schemaLoaded', eventName: EventNames.MarketplaceSchemaLoaded }, { method: 'schemaLoadError', eventName: EventNames.MarketplaceSchemaLoadError }, ]; events.forEach(({ method, eventName }) => { it(`marketplace.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.marketplace.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`marketplace.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.marketplace.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`marketplace.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.marketplace.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Resource events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'creation', eventName: EventNames.CreateResource }, { method: 'created', eventName: EventNames.ResourceCreated }, { method: 'creationError', eventName: EventNames.ResourceCreationError }, { method: 'update', eventName: EventNames.UpdateResource }, { method: 'updated', eventName: EventNames.ResourceUpdated }, { method: 'updateError', eventName: EventNames.ResourceUpdateError }, { method: 'delete', eventName: EventNames.DeleteResource }, { method: 'deleted', eventName: EventNames.ResourceDeleted }, { method: 'deletionError', eventName: EventNames.ResourceDeletionError }, ]; events.forEach(({ method, eventName }) => { it(`resource.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.resource.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`resource.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.resource.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`resource.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.resource.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Plan events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'upgrade', eventName: EventNames.UpdatePlan }, { method: 'upgraded', eventName: EventNames.PlanUpdated }, { method: 'upgradeError', eventName: EventNames.PlanUpdateError }, { method: 'downgrade', eventName: EventNames.DowngradePlan }, { method: 'downgraded', eventName: EventNames.PlanDowngraded }, { method: 'downgradeError', eventName: EventNames.PlanDowngradeError }, ]; events.forEach(({ method, eventName }) => { it(`plan.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.plan.publish as any)[method]('test'); expect(handler.publish).toHaveBeenCalledWith(eventName, 'test'); }); it(`plan.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.plan.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`plan.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.plan.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Organization events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'creation', eventName: EventNames.CreateOrganization }, { method: 'created', eventName: EventNames.OrganizationCreated }, { method: 'creationError', eventName: EventNames.OrganizationCreationError }, { method: 'update', eventName: EventNames.UpdateOrganization }, { method: 'updated', eventName: EventNames.OrganizationUpdated }, { method: 'updateError', eventName: EventNames.OrganizationUpdateError }, { method: 'delete', eventName: EventNames.DeleteOrganization }, { method: 'deleted', eventName: EventNames.OrganizationDeleted }, { method: 'deletionError', eventName: EventNames.OrganizationDeletionError }, ]; events.forEach(({ method, eventName }) => { it(`organization.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.organization.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`organization.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.organization.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`organization.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.organization.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - Notification events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); const events: Array<{ method: string; eventName: EventNames }> = [ { method: 'creation', eventName: EventNames.CreateNotification }, { method: 'deletion', eventName: EventNames.DeleteNotification }, { method: 'read', eventName: EventNames.NotificationRead }, ]; events.forEach(({ method, eventName }) => { it(`notification.publish.${method} usa ${eventName}`, () => { jest.clearAllMocks(); (helper.notification.publish as any)[method]({}); expect(handler.publish).toHaveBeenCalledWith(eventName, {}); }); it(`notification.subscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.notification.subscribe as any)[method](cb); expect(handler.subscribe).toHaveBeenCalledWith(eventName, cb); }); it(`notification.unsubscribe.${method} usa ${eventName}`, () => { jest.clearAllMocks(); const cb = jest.fn(); (helper.notification.unsubscribe as any)[method](cb); expect(handler.unsubscribe).toHaveBeenCalledWith(eventName, cb); }); }); }); describe('EventHelper - PlanProviders events', () => { const handler = makeMockHandler(); const helper = new EventHelper(handler); it('planProviders.publish.loadPlans usa LoadPlanProviders', () => { helper.planProviders.publish.loadPlans(); expect(handler.publish).toHaveBeenCalledWith(EventNames.LoadPlanProviders, undefined); }); it('planProviders.publish.plansLoaded usa PlanProvidersLoaded', () => { jest.clearAllMocks(); helper.planProviders.publish.plansLoaded([]); expect(handler.publish).toHaveBeenCalledWith(EventNames.PlanProvidersLoaded, []); }); it('planProviders.publish.loadError usa PlanProvidersLoadError', () => { jest.clearAllMocks(); helper.planProviders.publish.loadError(); expect(handler.publish).toHaveBeenCalledWith(EventNames.PlanProvidersLoadError, undefined); }); it('planProviders.subscribe.loadPlans usa LoadPlanProviders', () => { jest.clearAllMocks(); const cb = jest.fn(); helper.planProviders.subscribe.loadPlans(cb); expect(handler.subscribe).toHaveBeenCalledWith(EventNames.LoadPlanProviders, cb); }); it('planProviders.subscribe.plansLoaded usa PlanProvidersLoaded', () => { jest.clearAllMocks(); const cb = jest.fn(); helper.planProviders.subscribe.plansLoaded(cb); expect(handler.subscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoaded, cb); }); it('planProviders.subscribe.loadError usa PlanProvidersLoadError', () => { jest.clearAllMocks(); const cb = jest.fn(); helper.planProviders.subscribe.loadError(cb); expect(handler.subscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoadError, cb); }); it('planProviders.unsubscribe.loadPlans usa LoadPlanProviders', () => { jest.clearAllMocks(); const cb = jest.fn(); helper.planProviders.unsubscribe.loadPlans(cb); expect(handler.unsubscribe).toHaveBeenCalledWith(EventNames.LoadPlanProviders, cb); }); it('planProviders.unsubscribe.plansLoaded usa PlanProvidersLoaded', () => { jest.clearAllMocks(); const cb = jest.fn(); helper.planProviders.unsubscribe.plansLoaded(cb); expect(handler.unsubscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoaded, cb); }); it('planProviders.unsubscribe.loadError usa PlanProvidersLoadError', () => { jest.clearAllMocks(); const cb = jest.fn(); helper.planProviders.unsubscribe.loadError(cb); expect(handler.unsubscribe).toHaveBeenCalledWith(EventNames.PlanProvidersLoadError, cb); }); });