Files
speckle-server/packages/server/modules/shared/test/unit/eventBus.spec.ts
T
Gergő Jedlicska da7f0dda0e gergo/web 2047 user joins the workspace event (#3412)
* feat(gatekeeper): add gatekeeper module feature flag

* feat(gatekeeper): add workspace pricing table domain

* feat(gatekeeper): add checkout session creation

* feat(gatekeeper): verify stripe signature

* wip(gatekeeper): checkout callbacks

* feat(gatekeeper): add unlimited and academia plan types

* refactor(envHelper): getStringFromEnv helper

* chore(gatekeeper): add future todos

* feat(gatekeeper): add productId to the subscription domain

* feat(gatekeeper): add in memory repositories

* feat(gatekeeper): add more errors

* feat(gatekeeper): complete checkout session service

* feat(gatekeeper): add stripe client implementation

* feat(gatekeeper): add checkout session completion webhook callback path

* feat(gendo): fix not needing env vars if gendo module is not enabled

* feat(gatekeeper): require a license for billing

* chore(gatekeeper): cleanup before testing

* feat(gatekeeper): subscriptionData parsing model

* ci: add billing integration and gatekeeper modules to test config

* test(gatekeeper): add checkout service tests

* feat(gatekeeper): make completeCheckout callback idempotent properly

* feat(gatekeeper): move to knex based repositories

* test(gatekeeper): billing repository tests

* feat(gatekeeper): add yearly billing cycle toggle

* feat(ci): add stripe integration context to test job

* feat(billingPage): conditionally render the checkout CTAs

* fix(gatekeeper): remove flaky test condition

* feat(helm): add billing integration feature flag

* WIP billing gql api

* feat(gatekeeper): cancel checkout session api

* feat(gatekeeper): handle existing checkout sessions, when trying to create a new one

* feat(gatekeeper): add workspace plans gql api

* feat(gatekeeper): handle cancelation and subscription updates

* fix(gatekeeper): scope initialization

* fix(gatekeeper): eliminate stripe client import sideeffect

* fix(gatekeeper): eliminate stripe client import sideeffect 2

* feat(gatekeeper): upsize subscription on workspace role change

* feat(shared): add command pattern implementation

* refactor(eventBus): remove return capabilities from the event bus

* refactor(workspaces): use new commandFactory in workspace resolver

* feat(core): facelift taskLock

* feat(gatekeeper): shedule subscription downscale

* feat(gatekeeper): manage subscription downscale

* feat(gatekeeper): get workspace subscriptions, that are about to expire

* feat(gatekeeper): manage subscription downscale

* fix(gatekeeper): do not update subscription to canceled subs

* ci: bump postgres and max connections

* feat(workspaces): fix command factory event bugs
2024-10-30 15:51:40 +01:00

175 lines
5.4 KiB
TypeScript

import { getEventBus, initializeEventBus } from '@/modules/shared/services/eventBus'
import { WorkspaceEvents } from '@/modules/workspacesCore/domain/events'
import { Workspace } from '@/modules/workspacesCore/domain/types'
import { Roles } from '@speckle/shared'
import { expect } from 'chai'
import cryptoRandomString from 'crypto-random-string'
const createFakeWorkspace = (): Omit<Workspace, 'domains'> => {
return {
id: cryptoRandomString({ length: 10 }),
slug: cryptoRandomString({ length: 10 }),
description: cryptoRandomString({ length: 10 }),
logo: null,
defaultLogoIndex: 0,
name: cryptoRandomString({ length: 10 }),
updatedAt: new Date(),
createdAt: new Date(),
defaultProjectRole: Roles.Stream.Contributor,
domainBasedMembershipProtectionEnabled: false,
discoverabilityEnabled: false
}
}
describe('Event Bus', () => {
describe('initializeEventBus creates an event bus instance, that', () => {
it('calls back all the listeners', async () => {
const testEventBus = initializeEventBus()
const eventNames: string[] = []
testEventBus.listen('test.string', ({ eventName }) => {
eventNames.push(eventName)
})
testEventBus.listen('test.string', ({ eventName }) => {
eventNames.push(eventName)
})
await testEventBus.emit({ eventName: 'test.number', payload: 1 })
expect(eventNames.length).to.equal(0)
const eventName = 'test.string' as const
await testEventBus.emit({ eventName, payload: 'fake event' })
expect(eventNames.length).to.equal(2)
expect(eventNames).to.deep.equal([eventName, eventName])
})
it('can removes listeners from itself', async () => {
const testEventBus = initializeEventBus()
const eventNumbers: number[] = []
testEventBus.listen('test.string', () => {
eventNumbers.push(1)
})
const listenerOff = testEventBus.listen('test.string', () => {
eventNumbers.push(2)
})
await testEventBus.emit({ eventName: 'test.string', payload: 'fake event' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 2])
listenerOff()
await testEventBus.emit({ eventName: 'test.string', payload: 'fake event' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 1, 2])
})
it('bubbles up listener exceptions to emitter', async () => {
const testEventBus = initializeEventBus()
testEventBus.listen('test.string', ({ payload }) => {
throw new Error(payload)
})
const lookWhatHappened = 'kabumm'
try {
await testEventBus.emit({ eventName: 'test.string', payload: lookWhatHappened })
throw new Error('this should have thrown by now')
} catch (error) {
if (error instanceof Error) {
expect(error.message).to.equal(lookWhatHappened)
} else {
throw error
}
}
})
it('can be destroyed, removing all listeners', async () => {
const testEventBus = initializeEventBus()
const eventNumbers: number[] = []
testEventBus.listen('test.string', () => {
eventNumbers.push(1)
})
testEventBus.listen('test.string', () => {
eventNumbers.push(2)
})
await testEventBus.emit({ eventName: 'test.string', payload: 'test' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 2])
testEventBus.destroy()
await testEventBus.emit({ eventName: 'test.string', payload: 'test' })
expect(eventNumbers.sort((a, b) => a - b)).to.deep.equal([1, 2])
})
})
describe('getEventBus', () => {
it('returns a unified event bus instance', async () => {
const bus1 = getEventBus()
const bus2 = getEventBus()
const workspaces: Workspace[] = []
bus1.listen(WorkspaceEvents.Created, ({ payload }) => {
workspaces.push(payload)
})
bus2.listen(WorkspaceEvents.Created, ({ payload }) => {
workspaces.push(payload)
})
const workspacePayload = {
...createFakeWorkspace(),
createdByUserId: cryptoRandomString({ length: 10 }),
eventName: WorkspaceEvents.Created,
domains: []
}
await bus1.emit({
eventName: WorkspaceEvents.Created,
payload: { ...workspacePayload }
})
expect(workspaces.length).to.equal(2)
expect(workspaces).to.deep.equal([workspacePayload, workspacePayload])
})
it('allows to subscribe to wildcard events', async () => {
const eventBus = getEventBus()
const events: string[] = []
eventBus.listen('workspace.*', ({ payload, eventName }) => {
switch (eventName) {
case 'workspace.created':
events.push(payload.id)
break
case 'workspace.role-deleted':
events.push(payload.userId)
break
}
})
const workspace = createFakeWorkspace()
await eventBus.emit({
eventName: WorkspaceEvents.Created,
payload: {
...workspace,
createdByUserId: cryptoRandomString({ length: 10 })
}
})
const workspaceAcl = {
userId: cryptoRandomString({ length: 10 }),
workspaceId: cryptoRandomString({ length: 10 }),
role: Roles.Workspace.Member
}
await eventBus.emit({
eventName: WorkspaceEvents.RoleDeleted,
payload: workspaceAcl
})
expect([workspace.id, workspaceAcl.userId]).to.deep.equal(events)
})
})
})