Skip to content

Commit

Permalink
test(client): add test for mocking non-preview step outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont committed Nov 6, 2024
1 parent 3573b1d commit f93b790
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
81 changes: 81 additions & 0 deletions packages/framework/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,87 @@ describe('Novu Client', () => {
expect(metadata.duration).toEqual(expect.any(Number));
});

it('should use the provided state to mock non previewed step outputs', async () => {
const newWorkflow = workflow(
'test-workflow',
async ({ step }) => {
const digestOutput = await step.digest('digest-output', async () => ({
type: 'regular',
amount: 1,
unit: 'seconds',
}));

await step.inApp(
'send-email',
async () => ({
body: digestOutput.events.map((event) => event.payload.comment).join(','),
}),
{
skip: () => true,
}
);
},
{
payloadSchema: {
type: 'object',
properties: {
comment: { type: 'string' },
},
required: ['comment'],
} as const,
}
);

client.addWorkflows([newWorkflow]);

const event: Event = {
action: PostActionEnum.PREVIEW,
workflowId: 'test-workflow',
stepId: 'send-email',
subscriber: {},
state: [
{
stepId: 'digest-output',
state: {
status: 'success',
},
outputs: {
events: [
{
payload: {
comment: 'Hello',
},
},
{
payload: {
comment: 'World',
},
},
],
},
},
],
payload: {},
controls: {},
};

const executionResult = await client.executeWorkflow(event);

expect(executionResult).toBeDefined();
expect(executionResult.outputs).toBeDefined();
if (!executionResult.outputs) throw new Error('executionResult.outputs is undefined');

const { body } = executionResult.outputs;
expect(body).toBe('Hello,World');

expect(executionResult.providers).toEqual({});

const { metadata } = executionResult;
expect(metadata.status).toBe('success');
expect(metadata.error).toBe(false);
expect(metadata.duration).toEqual(expect.any(Number));
});

it('should throw an error when workflow ID is invalid', async () => {
// non-existing workflow ID
const event: Event = {
Expand Down
16 changes: 14 additions & 2 deletions packages/framework/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
HealthCheck,
Schema,
Skip,
State,
ValidationError,
Workflow,
} from './types';
Expand Down Expand Up @@ -634,7 +635,7 @@ export class Client {
}
} else {
try {
const result = event.state.find((state) => state.stepId === step.stepId);
const result = this.getStepState(event, step.stepId);

if (result) {
const validatedOutput = await this.validate(
Expand Down Expand Up @@ -723,7 +724,14 @@ export class Client {
providers: await this.executeProviders(event, step, validatedOutput),
};
} else {
const mockResult = this.mock(step.results.schema);
let mockResult: Record<string, unknown>;
const suppliedResult = this.getStepState(event, step.stepId);

if (suppliedResult) {
mockResult = suppliedResult.outputs;
} else {
mockResult = this.mock(step.results.schema);
}

console.log(` ${EMOJI.MOCK} Mocked stepId: \`${step.stepId}\``);

Expand All @@ -743,6 +751,10 @@ export class Client {
}
}

private getStepState(event: Event, stepId: string): State | undefined {
return event.state.find((state) => state.stepId === stepId);
}

private getStepCode(workflowId: string, stepId: string): CodeResult {
const step = this.getStep(workflowId, stepId);

Expand Down

0 comments on commit f93b790

Please sign in to comment.