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

feat(framework): Add support for specifying mock results #6878

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
156 changes: 156 additions & 0 deletions packages/framework/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Client } from './client';
import {
ExecutionEventPayloadInvalidError,
ExecutionStateCorruptError,
ExecutionStateResultInvalidError,
ProviderExecutionFailedError,
StepExecutionFailedError,
StepNotFoundError,
Expand Down Expand Up @@ -1472,6 +1473,161 @@ 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: [
{
id: '1',
time: '2024-01-01T00:00:00.000Z',
payload: {
comment: 'Hello',
},
},
{
id: '2',
time: '2024-01-01T00:00:00.000Z',
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 the provided preview state is invalid', 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: [
{
time: '2024-01-01T00:00:00.000Z',
payload: {
comment: 'Hello',
},
},
{
id: '2',
time: '2024-01-01T00:00:00.000Z',
payload: {
comment: 'World',
},
},
],
},
},
],
payload: {},
controls: {},
};

await expect(client.executeWorkflow(event)).rejects.toThrow(ExecutionStateResultInvalidError);
});

it('should throw an error when workflow ID is invalid', async () => {
// non-existing workflow ID
const event: Event = {
Expand Down
23 changes: 21 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,21 @@ 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 = await this.validate(
suppliedResult.outputs,
step.results.unknownSchema,
'step',
'result',
event.workflowId,
step.stepId
);
} else {
mockResult = this.mock(step.results.schema);
}

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

Expand All @@ -743,6 +758,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
Loading