Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(framework): Make discovery completely asynchronous #6879

Merged
merged 6 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/api/src/app/events/e2e/bridge-trigger.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import { expect } from 'chai';
import { v4 as uuidv4 } from 'uuid';
import sinon from 'sinon';

import { SubscribersService, UserSession } from '@novu/testing';
import {
Expand Down Expand Up @@ -724,8 +725,9 @@ contexts.forEach((context: Context) => {
* Delete `preferences` from the Workflow Definition to simulate an old
* Workflow Definition (i.e. from old Framework version) that doesn't have the `preferences` property.
*/
// @ts-ignore - The operand of a 'delete' operator must be optional.
delete newWorkflow.definition.preferences;
const { preferences, ...rest } = await newWorkflow.discover();
// @ts-expect-error - preferences is not part of the resolved object
sinon.stub(newWorkflow, 'discover').resolves(rest);

await bridgeServer.start({ workflows: [newWorkflow] });

Expand Down
82 changes: 41 additions & 41 deletions packages/framework/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Novu Client', () => {
});

client = new Client({ secretKey: 'some-secret-key' });
client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);
});

describe('client constructor', () => {
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('Novu Client', () => {
}));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

// wait for discovery to finish
await new Promise((resolve) => {
Expand Down Expand Up @@ -301,7 +301,7 @@ describe('Novu Client', () => {
);
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const discovery = client.discover();
expect(discovery.workflows).toHaveLength(2);
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const emailEvent: Event = {
action: PostActionEnum.PREVIEW,
Expand All @@ -381,7 +381,7 @@ describe('Novu Client', () => {
it('should sanitize the step result of all delivery channel step types', async () => {
const script = `<script>alert('Hello there')</script>`;

client.addWorkflows([
await client.addWorkflows([
workflow('test-workflow', async ({ step }) => {
await step.email('send-email', async () => ({
body: `Start of body. ${script}`,
Expand Down Expand Up @@ -409,7 +409,7 @@ describe('Novu Client', () => {
it('should not sanitize the step result of custom step type', async () => {
const script = `<script>alert('Hello there')</script>`;

client.addWorkflows([
await client.addWorkflows([
workflow('test-workflow', async ({ step }) => {
await step.custom(
'send-email',
Expand Down Expand Up @@ -468,7 +468,7 @@ describe('Novu Client', () => {
controls: {},
};

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const emailExecutionResult = await client.executeWorkflow(emailEvent);

Expand Down Expand Up @@ -564,7 +564,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const emailEvent: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -633,7 +633,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -699,7 +699,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -762,7 +762,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -825,7 +825,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -888,7 +888,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -935,7 +935,7 @@ describe('Novu Client', () => {
);
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -981,7 +981,7 @@ describe('Novu Client', () => {
);
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const emailEvent: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1030,7 +1030,7 @@ describe('Novu Client', () => {
);
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const emailEvent: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1061,7 +1061,7 @@ describe('Novu Client', () => {
await step.email('send-email', async () => ({ body: 'Test Body', subject: 'Subject' }));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1100,7 +1100,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1153,7 +1153,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1207,7 +1207,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1249,7 +1249,7 @@ describe('Novu Client', () => {
await step.email('inactive-step-id', async () => ({ body: 'Test Body', subject: 'Subject' }));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1277,7 +1277,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1306,7 +1306,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1344,7 +1344,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1386,7 +1386,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([workflowMock]);
await client.addWorkflows([workflowMock]);

const event: Event = {
action: PostActionEnum.PREVIEW,
Expand All @@ -1410,7 +1410,7 @@ describe('Novu Client', () => {
await step.email('send-email', async () => ({ body: 'Test Body', subject: 'Subject' }));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.PREVIEW,
Expand Down Expand Up @@ -1469,7 +1469,7 @@ describe('Novu Client', () => {
await step.inApp('send-in-app', async () => ({ body: 'Test Body', subject: 'Subject' }));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.PREVIEW,
Expand Down Expand Up @@ -1508,7 +1508,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.PREVIEW,
Expand Down Expand Up @@ -1571,7 +1571,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.PREVIEW,
Expand Down Expand Up @@ -1656,7 +1656,7 @@ describe('Novu Client', () => {
}
);

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.PREVIEW,
Expand Down Expand Up @@ -1713,7 +1713,7 @@ describe('Novu Client', () => {
await step.email('send-email', async () => ({ body: 'Test Body', subject: 'Subject' }));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

// @ts-expect-error - no workflow id
const event2: Event = {
Expand All @@ -1730,7 +1730,7 @@ describe('Novu Client', () => {
await step.email('send-email', async () => ({ body: 'Test Body', subject: 'Subject' }));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand All @@ -1750,7 +1750,7 @@ describe('Novu Client', () => {
await step.email('send-email', async () => ({ body: 'Test Body', subject: 'Subject' }));
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

// @ts-expect-error - no action
const event: Event = {
Expand All @@ -1771,7 +1771,7 @@ describe('Novu Client', () => {
});
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand Down Expand Up @@ -1808,7 +1808,7 @@ describe('Novu Client', () => {
);
});

client.addWorkflows([newWorkflow]);
await client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.EXECUTE,
Expand All @@ -1828,7 +1828,7 @@ describe('Novu Client', () => {
it('should sanitize the step output of all channel step types by default', async () => {
const script = `<script>alert('Hello there')</script>`;

client.addWorkflows([
await client.addWorkflows([
workflow('test-workflow', async ({ step }) => {
await step.email('send-email', async () => ({
body: `Start of body. ${script}`,
Expand Down Expand Up @@ -1856,7 +1856,7 @@ describe('Novu Client', () => {
it('should sanitize the step output of channel step types when `disableOutputSanitization: false`', async () => {
const script = `<script>alert('Hello there')</script>`;

client.addWorkflows([
await client.addWorkflows([
workflow('test-workflow', async ({ step }) => {
await step.email(
'send-email',
Expand Down Expand Up @@ -1891,7 +1891,7 @@ describe('Novu Client', () => {
const link =
'/pipeline/Oee4d54-ca52-4d70-86b3-cd10a67b6810/requirements?requirementId=dc25a578-ecf1-4835-9310-2236f8244bd&commentId=e259b16b-68f9-43af-b252-fce68bc7cb2f';

client.addWorkflows([
await client.addWorkflows([
workflow('test-workflow', async ({ step }) => {
await step.inApp(
'send-inapp',
Expand Down Expand Up @@ -1927,7 +1927,7 @@ describe('Novu Client', () => {
it('should not sanitize the step result of custom step type', async () => {
const script = `<script>alert('Hello there')</a>`;

client.addWorkflows([
await client.addWorkflows([
workflow('test-workflow', async ({ step }) => {
await step.custom(
'send-email',
Expand Down Expand Up @@ -1981,7 +1981,7 @@ describe('Novu Client', () => {

const newWorkflow = workflow('setup-workflow', workflowExecuteFunc);

getCodeClientInstance.addWorkflows([newWorkflow]);
await getCodeClientInstance.addWorkflows([newWorkflow]);
});

it('should throw an error when workflow ID is not found', () => {
Expand All @@ -1991,7 +1991,7 @@ describe('Novu Client', () => {
it('should throw an error when step ID is provided but not found in the workflow', async () => {
const newWorkflow = workflow('test-workflow', workflowExecuteFunc);

getCodeClientInstance.addWorkflows([newWorkflow]);
await getCodeClientInstance.addWorkflows([newWorkflow]);

expect(() => getCodeClientInstance.getCode('test-workflow', 'non-existent-step')).toThrow(StepNotFoundError);
});
Expand Down
Loading
Loading