Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
retro committed Dec 3, 2023
1 parent 3b4942b commit 6fccd35
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 2 deletions.
309 changes: 309 additions & 0 deletions src/__tests__/__snapshots__/on-workflow-complete-cleanup.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`cancels or disables started or enabled tasks on workflow end 1`] = `
{
"conditions": [
{
"marking": 1,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "implicit:t1->t2",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "implicit:t1->t3",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "implicit:t1->t4",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"tasks": [
{
"generation": 0,
"name": "t1",
"state": "enabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t2",
"state": "disabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t3",
"state": "disabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t4",
"state": "disabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"workItems": [],
"workflows": [
{
"context": undefined,
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "started",
},
],
}
`;

exports[`cancels or disables started or enabled tasks on workflow end 2`] = `
{
"conditions": [
{
"marking": 0,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "implicit:t1->t2",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "implicit:t1->t3",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "implicit:t1->t4",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"tasks": [
{
"generation": 1,
"name": "t1",
"state": "completed",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t2",
"state": "enabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t3",
"state": "enabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t4",
"state": "enabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"workItems": [],
"workflows": [
{
"context": undefined,
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "started",
},
],
}
`;

exports[`cancels or disables started or enabled tasks on workflow end 3`] = `
{
"conditions": [
{
"marking": 0,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "implicit:t1->t2",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "implicit:t1->t3",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "implicit:t1->t4",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"tasks": [
{
"generation": 1,
"name": "t1",
"state": "completed",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 1,
"name": "t2",
"state": "started",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t3",
"state": "enabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t4",
"state": "enabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"workItems": [],
"workflows": [
{
"context": undefined,
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "started",
},
],
}
`;

exports[`cancels or disables started or enabled tasks on workflow end 4`] = `
{
"conditions": [
{
"marking": 0,
"name": "start",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "end",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "implicit:t1->t2",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 1,
"name": "implicit:t1->t3",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"marking": 0,
"name": "implicit:t1->t4",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"tasks": [
{
"generation": 1,
"name": "t1",
"state": "completed",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 1,
"name": "t2",
"state": "canceled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 0,
"name": "t3",
"state": "disabled",
"workflowId": "workflow-1",
"workflowName": "activities",
},
{
"generation": 1,
"name": "t4",
"state": "completed",
"workflowId": "workflow-1",
"workflowName": "activities",
},
],
"workItems": [],
"workflows": [
{
"context": undefined,
"id": "workflow-1",
"name": "activities",
"parent": null,
"state": "completed",
},
],
}
`;
55 changes: 55 additions & 0 deletions src/__tests__/on-workflow-complete-cleanup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Effect } from 'effect';
import { expect, it } from 'vitest';

import { Builder, IdGenerator, Service } from '../index.js';
import { getEnabledTaskNames, makeIdGenerator } from './shared.js';

const workflowDefinition = Builder.workflow()
.withName('activities')
.startCondition('start')
.task('t1', Builder.emptyTask().withSplitType('and'))
.task('t2')
.task('t3')
.task('t4', Builder.emptyTask())
.endCondition('end')
.connectCondition('start', (to) => to.task('t1'))
.connectTask('t1', (to) => to.task('t2').task('t3').task('t4'))
.connectTask('t2', (to) => to.condition('end'))
.connectTask('t3', (to) => to.condition('end'))
.connectTask('t4', (to) => to.condition('end'));

it('cancels or disables started or enabled tasks on workflow end', () => {
const program = Effect.gen(function* ($) {
const idGenerator = makeIdGenerator();

const service = yield* $(
workflowDefinition.build(),
Effect.flatMap((workflow) => Service.initialize(workflow)),
Effect.provideService(IdGenerator, idGenerator)
);

yield* $(service.start());
const state = yield* $(service.getState());
expect(state).toMatchSnapshot();
expect(getEnabledTaskNames(state)).toEqual(new Set(['t1']));

yield* $(service.startTask('t1'));
const state2 = yield* $(service.getState());
expect(state2).toMatchSnapshot();
expect(getEnabledTaskNames(state2)).toEqual(new Set(['t2', 't3', 't4']));

yield* $(service.startTask('t2'));
const state3 = yield* $(service.getState());
expect(state3).toMatchSnapshot();
expect(getEnabledTaskNames(state3)).toEqual(new Set(['t3', 't4']));

yield* $(service.startTask('t4'));
const state4 = yield* $(service.getState());
expect(state4).toMatchSnapshot();
expect(getEnabledTaskNames(state4)).toEqual(new Set());
expect(state4.tasks.filter((t) => t.state === 'started')).toEqual([]);
expect(state4.workflows[0]?.state).toEqual('completed');
});

Effect.runSync(program);
});
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ exports[`runs a net with an "or" join, a loop and a cancellation region 7`] = `
{
"generation": 1,
"name": "B",
"state": "enabled",
"state": "disabled",
"workflowId": "workflow-1",
"workflowName": "or-join-cancellation-region",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ it('runs a net with an "or" join, a loop and a cancellation region', () => {
yield* $(service.startTask('E'));
const state6 = yield* $(service.getState());
expect(state6).toMatchSnapshot();
expect(getEnabledTaskNames(state6)).toEqual(new Set(['B']));
expect(getEnabledTaskNames(state6)).toEqual(new Set());
expect(state6.workflows[0]?.state).toBe('completed');
});

Expand Down
8 changes: 8 additions & 0 deletions src/elements/Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ export class Workflow<
const perform = yield* $(
Effect.once(
Effect.gen(function* ($) {
yield* $(
Effect.all(
Object.values(self.tasks).map((task) =>
task.maybeCancelOrDisable(id)
),
{ batching: true }
)
);
yield* $(stateManager.updateWorkflowState(id, 'completed'));
if (self.parentTask && workflow.parent?.workflowId) {
yield* $(
Expand Down

0 comments on commit 6fccd35

Please sign in to comment.