Skip to content

Commit

Permalink
feat(framework): add auto deterministic preview for required payload …
Browse files Browse the repository at this point in the history
…variables (#5743)

* feat: add preview for payload

* feat: add mock value for preview

* fix(client): remove unused parameter in shuffle function

* fix: echo pointer

* fix: mock test

---------

Co-authored-by: Richard Fontein <[email protected]>
  • Loading branch information
scopsy and rifont authored Jun 23, 2024
1 parent 22ad596 commit 2ea8954
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
37 changes: 37 additions & 0 deletions packages/framework/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,43 @@ describe('Novu Client', () => {
await expect(client.executeWorkflow(event)).rejects.toThrow(ExecutionEventInputInvalidError);
});

it('should preview with mocked data during preview', async () => {
const workflowMock = workflow(
'mock-workflow',
async ({ step, payload }) => {
// eslint-disable-next-line @typescript-eslint/no-base-to-string
await step.email('send-email', async () => ({ body: 'Test: ' + payload.name, subject: 'Subject' }));
},
{
payloadSchema: {
type: 'object',
properties: {
name: { type: 'string' },
},
required: ['name'],
},
}
);

client.addWorkflows([workflowMock]);

const event: IEvent = {
action: 'preview',
workflowId: 'mock-workflow',
stepId: 'send-email',
subscriber: {},
state: [],
data: {},
inputs: {},
};

const executionResult = await client.executeWorkflow(event);
expect(executionResult).toBeDefined();
expect(executionResult.outputs).toBeDefined();

expect((executionResult.outputs as any).body).toBe('Test: [placeholder]');
});

it('should preview workflow successfully when action is preview', async () => {
const newWorkflow = workflow('test-workflow', async ({ step }) => {
await step.email('send-email', async () => ({ body: 'Test Body', subject: 'Subject' }));
Expand Down
18 changes: 15 additions & 3 deletions packages/framework/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ import { VERSION } from './version';
import { Skip } from './types/skip.types';
import { Liquid } from 'liquidjs';

/**
* We want to respond with a consistent string value for preview
*/
JSONSchemaFaker.random.shuffle = function () {
return ['[placeholder]'];
};

JSONSchemaFaker.option({
useDefaultValue: true,
alwaysFakeOptionals: true,
Expand Down Expand Up @@ -429,11 +436,16 @@ export class Client {
}

private createExecutionInputs(event: IEvent, workflow: DiscoverWorkflowOutput): Record<string, unknown> {
const executionData = event.data;
let payload = event.data;
if (event.action === 'preview') {
const mockResult = this.mock(workflow.data.schema);

payload = Object.assign(mockResult, payload);
}

this.validate(event.data, workflow.data.validate, workflow.data.schema, 'event', 'input', event.workflowId);
this.validate(payload, workflow.data.validate, workflow.data.schema, 'event', 'input', event.workflowId);

return executionData;
return payload;
}

private prettyPrintExecute(payload: IEvent, duration: number, error?: Error): void {
Expand Down

0 comments on commit 2ea8954

Please sign in to comment.