Skip to content

Commit

Permalink
feat: reworked getOrderedSteps & fixed tests (#2868)
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterkmr authored Dec 2, 2024
1 parent 85eea80 commit b3ec333
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 21 deletions.
4 changes: 4 additions & 0 deletions apps/backoffice-v2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-browser-sdk@0.6.69
- @ballerine/workflow-node-sdk@0.6.69
- @ballerine/workflow-browser-sdk@0.6.69
- @ballerine/workflow-node-sdk@0.6.69

Expand Down
3 changes: 3 additions & 0 deletions apps/kyb-app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@

## 0.3.86

## 0.3.84

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-browser-sdk@0.6.69
- @ballerine/workflow-browser-sdk@0.6.71

## 0.3.85
Expand Down
3 changes: 3 additions & 0 deletions examples/headless-example/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

## 0.3.70

## 0.3.68

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- @ballerine/workflow-browser-sdk@0.6.69
- @ballerine/workflow-browser-sdk@0.6.71

## 0.3.69
Expand Down
1 change: 1 addition & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Patch Changes

- Reworked getOrderedSteps & fixed tests
- bump

## 0.9.53
Expand Down
35 changes: 22 additions & 13 deletions packages/common/src/utils/collection-flow/get-ordered-steps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { createMachine, interpret } from 'xstate';

export interface IGetOrderedStepsParams {
// The event to send to the machine to move to the next step
eventName?: string;
Expand All @@ -14,25 +12,36 @@ export const getOrderedSteps = (
) => {
const { eventName = 'NEXT', finalStates = [] } = params;

if (!definition?.states || !definition?.initial) {
throw new Error('Invalid state machine definition');
}

const steps: string[] = [definition.initial];
let currentState = definition.initial;

while (currentState) {
const stateConfig = definition.states[currentState];

const machine = createMachine({
initial: definition.initial,
context: {},
states: definition.states,
});
if (!stateConfig) {
throw new Error(`Invalid state: ${currentState}`);
}

// Check if state has transition for the event
const transition = stateConfig?.on?.[eventName];

const service = interpret(machine).start();
if (!transition) {
break;
}

while (service.getSnapshot().can({ type: eventName })) {
service.send({ type: eventName });
const stateValue = service.getSnapshot().value as string;
// Get next state from transition
const nextState = typeof transition === 'string' ? transition : transition.target;

if (finalStates.includes(stateValue)) {
if (!nextState || stateConfig.type === 'final' || finalStates.includes(nextState)) {
break;
}

steps.push(stateValue);
steps.push(nextState);
currentState = nextState;
}

return steps;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DefaultContextSchema } from '@/schemas';
import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { setCollectionFlowStatus } from './set-collection-flow-status';

describe('setCollectionFlowStatus', () => {
Expand All @@ -19,17 +19,25 @@ describe('setCollectionFlowStatus', () => {
expect(updatedContext).toBe(context);
});

it('will throw an error if the status is invalid', () => {
const context = {};
it('will log a warning if the status is invalid', () => {
const context = {
collectionFlow: { state: {} },
};
const consoleSpy = vi.spyOn(console, 'warn');

expect(() =>
setCollectionFlowStatus(context as DefaultContextSchema, 'invalid' as any),
).toThrow();
const result = setCollectionFlowStatus(context as DefaultContextSchema, 'invalid' as any);

expect(consoleSpy).toHaveBeenCalledWith('Invalid status: invalid');
expect(result).toBe(context);
});

it('will throw an error if the collection flow state is not present', () => {
it('will log a warning if the collection flow state is not present', () => {
const context = {};
const consoleSpy = vi.spyOn(console, 'warn');

const result = setCollectionFlowStatus(context as DefaultContextSchema, 'completed');

expect(() => setCollectionFlowStatus(context as DefaultContextSchema, 'completed')).toThrow();
expect(consoleSpy).toHaveBeenCalledWith('Collection flow state is not present.');
expect(result).toBe(context);
});
});
1 change: 1 addition & 0 deletions packages/workflow-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- version bump

## 0.6.71
## 0.6.69

### Patch Changes

Expand Down
2 changes: 2 additions & 0 deletions sdks/workflow-browser-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

## 0.6.71

## 0.6.69

### Patch Changes

- Updated dependencies
Expand Down
1 change: 1 addition & 0 deletions sdks/workflow-node-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

### Patch Changes

- @ballerine/workflow-core@0.6.69
- Updated dependencies
- @ballerine/workflow-core@0.6.69

Expand Down
2 changes: 2 additions & 0 deletions services/workflows-service/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

### Patch Changes

- Updated dependencies
- @ballerine/common@0.9.54
- version bump
- Updated dependencies
- @ballerine/workflow-core@0.6.69
Expand Down

0 comments on commit b3ec333

Please sign in to comment.