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

[IOPAE-253] Propose type interfaces for a Finite State Machine using fp-ts #3

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
112 changes: 112 additions & 0 deletions packages/fp-ts-fsm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# fp-ts-fms

Finite State Machine (FSM) implementation using [fp-ts](https://github.com/gcanti/fp-ts).

## Example
Given a FSM of a door:

```mermaid
stateDiagram-v2
* --> Open: create
Open --> Closed: close
Closed --> Open: open
Closed --> Locked: lock
Locked --> Closed: unlock
```

A FMS can be defined as

```ts
import { State, Action, Transition, FSM } from "@pagopa/fp-ts-fsm"

type DoorStates = [
State<"Open">,
State<"Closed">,
State<"Locked">
];
type DoorActions = [
Action<"create">,
Action<"close">,
Action<"open">,
Action<"lock">,
Action<"unlock">,
];
type DoorTransitions = [
Transition<void, "Open", "create", DoorStates, DoorActions>,
Transition<"Open", "Closed", "close", DoorStates, DoorActions>,
Transition<"Closed", "Open", "open", DoorStates, DoorActions>,
Transition<"Closed", "Locked", "lock", DoorStates, DoorActions>,
Transition<"Locked", "Closed", "unlock", DoorStates, DoorActions>,
];
type DoorFSM = FSM<DoorStates, DoorActions, DoorTransitions>;
```

and implemented as:
```ts
import * as t from "io-ts";

const fsm: DoorFSM = {
states: {
Open: t.unknown,
Closed: t.unknown,
Locked: t.unknown,
},
transitions: [
{
id: "apply create on *",
action: "create",
from: "*",
to: "Open",
exec: () => ({ fsm: { state: "Open" } }),
},
{
id: "apply close on Open",
action: "close",
from: "Open",
to: "Closed",
exec: (current) => ({ fsm: { state: "Closed" } }),
},
{
id: "apply open on Closed",
action: "open",
from: "Closed",
to: "Open",
exec: (current) => ({ fsm: { state: "Open" } }),
},
{
id: "apply lock on Closed",
action: "lock",
from: "Closed",
to: "Locked",
exec: (current) => ({ fsm: { state: "Locked" } }),
},
{
id: "apply unlock on Locked",
action: "unlock",
from: "Locked",
to: "Closed",
exec: (current) => ({ fsm: { state: "Closed" } }),
},
],
};
```

To apply a transition, we must create an actual implementation of `TransitionApplication`:
```ts
const applyOpen =
// Any actual implementation of TransitionApplication
(
(() => {}) as unknown as TransitionApplication<
"open",
DoorStates,
DoorActions,
DoorTransitions
>
)(fsm, "open");

// Apply Open to an element of id "door-id"
applyOpen("door-id") // TaskEither<Error, {fsm: {state: "Open"}}>;
```

## Known issues
1. `exec` can be implemented with void as returning type, and the type system does not complain
28 changes: 28 additions & 0 deletions packages/fp-ts-fsm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@pagopa/fp-ts-fsm",
"main": "./dist/index.js",
"typesVersions": {
"*": {
"*": [
"./dist/*"
]
}
},
"files": [
"dist"
],
"packageManager": "[email protected]",
"scripts": {
"build": "rm -rf ./dist & tsc",
"test": "vitest",
"typecheck": "tsc -p ./tsconfig.test.json --noEmit && vitest typecheck"
},
"dependencies": {
"fp-ts": "^2.13.1",
"io-ts": "^2.2.20"
},
"devDependencies": {
"typescript": "^5.0.4",
"vitest": "^0.30.1"
}
}
190 changes: 190 additions & 0 deletions packages/fp-ts-fsm/src/__tests__/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import { assertType, test } from "vitest";
import { Transition, FSM, TransitionApplication, StateSet } from "../types";
import * as E from "fp-ts/Either";
import * as TE from "fp-ts/TaskEither";

// helpers
type Equals<A, B extends A> = never;
type FuncEquals<
A extends (...args: unknown[]) => unknown,
P extends Parameters<A>,
R extends ReturnType<A>
> = never;
type Never<A extends never> = never;

test("Transitions", () => {
type MyStates = StateSet<{ fooState: void }>;
type MyActions = { fooAction: {} };

type __tests__ = [
// OK a correct transition
Transition<"fooState", "fooState", "fooAction", MyStates, MyActions>,

// OK using void as current state
Transition<void, "fooState", "fooAction", MyStates, MyActions>,

// using undeclared state
Transition<
"fooState",
// @ts-expect-error
"undeclared_state",
"fooAction",
MyStates,
MyActions
>,

// using undeclared action
Transition<
"fooState",
"fooState",
// @ts-expect-error
"undeclared_action",
MyStates,
MyActions
>,

// @ts-expect-error using void as target state
Transition<"fooState", void, "fooAction", MyStates, MyActions>,

// There should be no transition for empty state set
Never<
Transition<
void,
never /* any other type won't be allowed */,
"fooAction",
{},
MyActions
>
>,

// There should be no transition for empty action set
Never<
Transition<
void,
"fooState",
never /* any other type won't be allowed */,
MyStates,
{}
>
>
];

// correct definition
assertType<
Transition<"fooState", "fooState", "fooAction", MyStates, MyActions>
>({
id: "apply fooAction on fooState",
action: "fooAction",
exec: () => E.right({ fsm: { state: "fooState" } }),
from: "fooState",
to: "fooState",
});

// wrong transition id
assertType<
Transition<"fooState", "fooState", "fooAction", MyStates, MyActions>
>({
// @ts-expect-error
id: "mispelled apply fooAction on fooState",
action: "fooAction",
exec: () => E.right({ fsm: { state: "fooState" } }),
from: "fooState",
to: "fooState",
});

// exec doesn't return the correct shape
assertType<
Transition<"fooState", "fooState", "fooAction", MyStates, MyActions>
>({
id: "apply fooAction on fooState",
action: "fooAction",
// @ts-expect-error
exec: () => ({ bad: {} }),
from: "fooState",
to: "fooState",
});

// exec cannot return void
assertType<
Transition<"fooState", "fooState", "fooAction", MyStates, MyActions>
>({
id: "apply fooAction on fooState",
action: "fooAction",
// FIXME @ts-expect-error
exec: () => void 0,
from: "fooState",
to: "fooState",
});
});

test("TransitionApplications", () => {
// Example: door FSM
type DoorStates = StateSet<{
Open: void;
Closed: void;
Locked: { date: number };
}>;
type DoorActions = {
create: {};
close: {};
open: {};
lock: { date: number };
unlock: {};
};
type DoorTransitions = [
Transition<void, "Open", "create", DoorStates, DoorActions>,
Transition<"Open", "Closed", "close", DoorStates, DoorActions>,
Transition<"Closed", "Open", "open", DoorStates, DoorActions>,
Transition<"Closed", "Locked", "lock", DoorStates, DoorActions>,
Transition<"Locked", "Closed", "unlock", DoorStates, DoorActions>
];

type __tests__ = [
// FSM is a valid definition
FSM<DoorStates, DoorActions, DoorTransitions>,

FuncEquals<
TransitionApplication<DoorStates, DoorActions, DoorTransitions, "create">,
[FSM<DoorStates, DoorActions, DoorTransitions>, "create", string],
TE.TaskEither<Error, void & { fsm: { state: "Open" } }>
>,

FuncEquals<
TransitionApplication<DoorStates, DoorActions, DoorTransitions, "open">,
[FSM<DoorStates, DoorActions, DoorTransitions>, "open", string],
TE.TaskEither<Error, void & { fsm: { state: "Open" } }>
>,

FuncEquals<
TransitionApplication<DoorStates, DoorActions, DoorTransitions, "lock">,
[
FSM<DoorStates, DoorActions, DoorTransitions>,
"lock",
string,
{ date: number }
],
TE.TaskEither<Error, { date: number } & { fsm: { state: "Locked" } }>
>,

// must provide arguments for lock action
FuncEquals<
TransitionApplication<DoorStates, DoorActions, DoorTransitions, "lock">,
// @ts-expect-error
[FSM<DoorStates, DoorActions, DoorTransitions>, "lock", string],
TE.TaskEither<Error, { date: number } & { fsm: { state: "Locked" } }>
>,

// must provide arguments in correct shape for lock action
FuncEquals<
TransitionApplication<DoorStates, DoorActions, DoorTransitions, "lock">,
// @ts-expect-error
[
FSM<DoorStates, DoorActions, DoorTransitions>,
"lock",
string,
{ foo: "wrong" }
],
TE.TaskEither<Error, { date: number } & { fsm: { state: "Locked" } }>
>
];
});
Loading