-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstate-machine.test.ts
More file actions
124 lines (114 loc) · 2.91 KB
/
Copy pathstate-machine.test.ts
File metadata and controls
124 lines (114 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { describe, it, expect } from 'vitest';
import { StateMachineSchema } from './state-machine.zod';
describe('StateMachineSchema', () => {
it('should validate a simple state machine', () => {
const machine = {
id: 'simple_flow',
initial: 'start',
states: {
start: {
on: {
NEXT: 'end',
},
},
end: {
type: 'final',
},
},
};
const result = StateMachineSchema.parse(machine);
expect(result.id).toBe('simple_flow');
expect(result.initial).toBe('start');
});
it('should validate complex state machine with guards and actions', () => {
const machine = {
id: 'approval_flow',
initial: 'draft',
states: {
draft: {
on: {
SUBMIT: {
target: 'pending',
cond: 'isComplete',
actions: ['notifyManager'],
},
},
},
pending: {
on: {
APPROVE: 'approved',
REJECT: 'rejected',
},
meta: {
aiInstructions: 'Review carefully',
},
},
approved: { type: 'final' },
rejected: { type: 'final' },
},
};
expect(() => StateMachineSchema.parse(machine)).not.toThrow();
});
it('should validate hierarchical states', () => {
const machine = {
id: 'nested_flow',
initial: 'active',
states: {
active: {
initial: 'running',
states: {
running: {
on: { PAUSE: 'paused' },
},
paused: {
on: { RESUME: 'running' },
},
},
on: { STOP: 'stopped' },
},
stopped: { type: 'final' },
},
};
expect(() => StateMachineSchema.parse(machine)).not.toThrow();
});
it('should validate parallel states', () => {
const machine = {
id: 'parallel_flow',
initial: 'processing',
states: {
processing: {
type: 'parallel',
states: {
upload: {
initial: 'pending',
states: {
pending: { on: { START: 'uploading' } },
uploading: { on: { DONE: 'uploaded' } },
uploaded: { type: 'final' },
},
},
validate: {
initial: 'pending',
states: {
pending: { on: { CHECK: 'checking' } },
checking: { on: { PASS: 'passed' } },
passed: { type: 'final' },
},
},
},
},
},
};
expect(() => StateMachineSchema.parse(machine)).not.toThrow();
});
it('should reject invalid identifier', () => {
const machine = {
id: 'Invalid Name',
initial: 'start',
states: {
start: {},
},
};
expect(() => StateMachineSchema.parse(machine)).toThrow();
});
});