business-as-code 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +2 -0
  3. package/package.json +7 -4
  4. package/src/dollar.ts +5 -2
  5. package/src/entities/organization.ts +31 -18
  6. package/src/goals.ts +78 -12
  7. package/src/index.ts +48 -18
  8. package/src/kpis.ts +62 -8
  9. package/src/metrics.ts +92 -79
  10. package/src/okrs.ts +120 -20
  11. package/src/organization.ts +12 -15
  12. package/src/process.ts +11 -12
  13. package/src/product.ts +8 -9
  14. package/src/queries.ts +238 -75
  15. package/src/roles.ts +62 -61
  16. package/src/workflow.ts +22 -15
  17. package/test/business.test.ts +282 -0
  18. package/test/dollar.test.ts +270 -0
  19. package/test/entities.test.ts +628 -0
  20. package/test/financials.test.ts +539 -0
  21. package/test/goals.test.ts +451 -0
  22. package/{src → test}/index.test.ts +1 -1
  23. package/test/kpis.test.ts +440 -0
  24. package/test/metrics.test.ts +744 -0
  25. package/test/okrs.test.ts +741 -0
  26. package/test/organization.test.ts +548 -0
  27. package/test/process.test.ts +503 -0
  28. package/test/product.test.ts +430 -0
  29. package/test/queries.test.ts +556 -0
  30. package/test/roles.test.ts +546 -0
  31. package/test/service.test.ts +450 -0
  32. package/test/types.test.ts +1141 -0
  33. package/test/vision.test.ts +214 -0
  34. package/test/workflow.test.ts +501 -0
  35. package/vitest.config.ts +47 -0
  36. package/.turbo/turbo-build.log +0 -5
  37. package/dist/business.d.ts +0 -62
  38. package/dist/business.d.ts.map +0 -1
  39. package/dist/business.js +0 -109
  40. package/dist/business.js.map +0 -1
  41. package/dist/dollar.d.ts +0 -60
  42. package/dist/dollar.d.ts.map +0 -1
  43. package/dist/dollar.js +0 -107
  44. package/dist/dollar.js.map +0 -1
  45. package/dist/entities/assets.d.ts +0 -21
  46. package/dist/entities/assets.d.ts.map +0 -1
  47. package/dist/entities/assets.js +0 -323
  48. package/dist/entities/assets.js.map +0 -1
  49. package/dist/entities/business.d.ts +0 -36
  50. package/dist/entities/business.d.ts.map +0 -1
  51. package/dist/entities/business.js +0 -370
  52. package/dist/entities/business.js.map +0 -1
  53. package/dist/entities/communication.d.ts +0 -21
  54. package/dist/entities/communication.d.ts.map +0 -1
  55. package/dist/entities/communication.js +0 -255
  56. package/dist/entities/communication.js.map +0 -1
  57. package/dist/entities/customers.d.ts +0 -58
  58. package/dist/entities/customers.d.ts.map +0 -1
  59. package/dist/entities/customers.js +0 -989
  60. package/dist/entities/customers.js.map +0 -1
  61. package/dist/entities/financials.d.ts +0 -59
  62. package/dist/entities/financials.d.ts.map +0 -1
  63. package/dist/entities/financials.js +0 -932
  64. package/dist/entities/financials.js.map +0 -1
  65. package/dist/entities/goals.d.ts +0 -58
  66. package/dist/entities/goals.d.ts.map +0 -1
  67. package/dist/entities/goals.js +0 -800
  68. package/dist/entities/goals.js.map +0 -1
  69. package/dist/entities/index.d.ts +0 -299
  70. package/dist/entities/index.d.ts.map +0 -1
  71. package/dist/entities/index.js +0 -198
  72. package/dist/entities/index.js.map +0 -1
  73. package/dist/entities/legal.d.ts +0 -21
  74. package/dist/entities/legal.d.ts.map +0 -1
  75. package/dist/entities/legal.js +0 -301
  76. package/dist/entities/legal.js.map +0 -1
  77. package/dist/entities/market.d.ts +0 -21
  78. package/dist/entities/market.d.ts.map +0 -1
  79. package/dist/entities/market.js +0 -301
  80. package/dist/entities/market.js.map +0 -1
  81. package/dist/entities/marketing.d.ts +0 -67
  82. package/dist/entities/marketing.d.ts.map +0 -1
  83. package/dist/entities/marketing.js +0 -1157
  84. package/dist/entities/marketing.js.map +0 -1
  85. package/dist/entities/offerings.d.ts +0 -51
  86. package/dist/entities/offerings.d.ts.map +0 -1
  87. package/dist/entities/offerings.js +0 -727
  88. package/dist/entities/offerings.js.map +0 -1
  89. package/dist/entities/operations.d.ts +0 -58
  90. package/dist/entities/operations.d.ts.map +0 -1
  91. package/dist/entities/operations.js +0 -787
  92. package/dist/entities/operations.js.map +0 -1
  93. package/dist/entities/organization.d.ts +0 -57
  94. package/dist/entities/organization.d.ts.map +0 -1
  95. package/dist/entities/organization.js +0 -807
  96. package/dist/entities/organization.js.map +0 -1
  97. package/dist/entities/partnerships.d.ts +0 -21
  98. package/dist/entities/partnerships.d.ts.map +0 -1
  99. package/dist/entities/partnerships.js +0 -300
  100. package/dist/entities/partnerships.js.map +0 -1
  101. package/dist/entities/planning.d.ts +0 -87
  102. package/dist/entities/planning.d.ts.map +0 -1
  103. package/dist/entities/planning.js +0 -271
  104. package/dist/entities/planning.js.map +0 -1
  105. package/dist/entities/projects.d.ts +0 -25
  106. package/dist/entities/projects.d.ts.map +0 -1
  107. package/dist/entities/projects.js +0 -349
  108. package/dist/entities/projects.js.map +0 -1
  109. package/dist/entities/risk.d.ts +0 -21
  110. package/dist/entities/risk.d.ts.map +0 -1
  111. package/dist/entities/risk.js +0 -293
  112. package/dist/entities/risk.js.map +0 -1
  113. package/dist/entities/sales.d.ts +0 -72
  114. package/dist/entities/sales.d.ts.map +0 -1
  115. package/dist/entities/sales.js +0 -1248
  116. package/dist/entities/sales.js.map +0 -1
  117. package/dist/financials.d.ts +0 -130
  118. package/dist/financials.d.ts.map +0 -1
  119. package/dist/financials.js +0 -297
  120. package/dist/financials.js.map +0 -1
  121. package/dist/goals.d.ts +0 -87
  122. package/dist/goals.d.ts.map +0 -1
  123. package/dist/goals.js +0 -215
  124. package/dist/goals.js.map +0 -1
  125. package/dist/index.d.ts +0 -97
  126. package/dist/index.d.ts.map +0 -1
  127. package/dist/index.js +0 -132
  128. package/dist/index.js.map +0 -1
  129. package/dist/kpis.d.ts +0 -118
  130. package/dist/kpis.d.ts.map +0 -1
  131. package/dist/kpis.js +0 -232
  132. package/dist/kpis.js.map +0 -1
  133. package/dist/metrics.d.ts +0 -448
  134. package/dist/metrics.d.ts.map +0 -1
  135. package/dist/metrics.js +0 -325
  136. package/dist/metrics.js.map +0 -1
  137. package/dist/okrs.d.ts +0 -123
  138. package/dist/okrs.d.ts.map +0 -1
  139. package/dist/okrs.js +0 -269
  140. package/dist/okrs.js.map +0 -1
  141. package/dist/organization.d.ts +0 -585
  142. package/dist/organization.d.ts.map +0 -1
  143. package/dist/organization.js +0 -173
  144. package/dist/organization.js.map +0 -1
  145. package/dist/process.d.ts +0 -112
  146. package/dist/process.d.ts.map +0 -1
  147. package/dist/process.js +0 -241
  148. package/dist/process.js.map +0 -1
  149. package/dist/product.d.ts +0 -85
  150. package/dist/product.d.ts.map +0 -1
  151. package/dist/product.js +0 -145
  152. package/dist/product.js.map +0 -1
  153. package/dist/queries.d.ts +0 -304
  154. package/dist/queries.d.ts.map +0 -1
  155. package/dist/queries.js +0 -415
  156. package/dist/queries.js.map +0 -1
  157. package/dist/roles.d.ts +0 -340
  158. package/dist/roles.d.ts.map +0 -1
  159. package/dist/roles.js +0 -255
  160. package/dist/roles.js.map +0 -1
  161. package/dist/service.d.ts +0 -61
  162. package/dist/service.d.ts.map +0 -1
  163. package/dist/service.js +0 -140
  164. package/dist/service.js.map +0 -1
  165. package/dist/types.d.ts +0 -459
  166. package/dist/types.d.ts.map +0 -1
  167. package/dist/types.js +0 -5
  168. package/dist/types.js.map +0 -1
  169. package/dist/vision.d.ts +0 -38
  170. package/dist/vision.d.ts.map +0 -1
  171. package/dist/vision.js +0 -68
  172. package/dist/vision.js.map +0 -1
  173. package/dist/workflow.d.ts +0 -115
  174. package/dist/workflow.d.ts.map +0 -1
  175. package/dist/workflow.js +0 -247
  176. package/dist/workflow.js.map +0 -1
  177. package/src/business.js +0 -108
  178. package/src/dollar.js +0 -106
  179. package/src/entities/assets.js +0 -322
  180. package/src/entities/business.js +0 -369
  181. package/src/entities/communication.js +0 -254
  182. package/src/entities/customers.js +0 -988
  183. package/src/entities/financials.js +0 -931
  184. package/src/entities/goals.js +0 -799
  185. package/src/entities/index.js +0 -197
  186. package/src/entities/legal.js +0 -300
  187. package/src/entities/market.js +0 -300
  188. package/src/entities/marketing.js +0 -1156
  189. package/src/entities/offerings.js +0 -726
  190. package/src/entities/operations.js +0 -786
  191. package/src/entities/organization.js +0 -806
  192. package/src/entities/partnerships.js +0 -299
  193. package/src/entities/planning.js +0 -270
  194. package/src/entities/projects.js +0 -348
  195. package/src/entities/risk.js +0 -292
  196. package/src/entities/sales.js +0 -1247
  197. package/src/financials.js +0 -296
  198. package/src/goals.js +0 -214
  199. package/src/index.js +0 -131
  200. package/src/index.test.js +0 -274
  201. package/src/kpis.js +0 -231
  202. package/src/metrics.js +0 -324
  203. package/src/okrs.js +0 -268
  204. package/src/organization.js +0 -172
  205. package/src/process.js +0 -240
  206. package/src/product.js +0 -144
  207. package/src/queries.js +0 -414
  208. package/src/roles.js +0 -254
  209. package/src/service.js +0 -139
  210. package/src/types.js +0 -4
  211. package/src/vision.js +0 -67
  212. package/src/workflow.js +0 -246
@@ -0,0 +1,503 @@
1
+ /**
2
+ * Tests for process.ts - Business process definition and management
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest'
6
+ import {
7
+ Process,
8
+ getStepsInOrder,
9
+ getStepsByAutomationLevel,
10
+ calculateTotalDuration,
11
+ formatDuration,
12
+ calculateAutomationPercentage,
13
+ getMetric,
14
+ meetsTarget,
15
+ calculateMetricAchievement,
16
+ updateMetric,
17
+ addStep,
18
+ removeStep,
19
+ validateProcess,
20
+ } from '../src/process.js'
21
+ import type { ProcessDefinition, ProcessStep, ProcessMetric } from '../src/types.js'
22
+
23
+ describe('Process', () => {
24
+ describe('Process()', () => {
25
+ it('should create a process with required fields', () => {
26
+ const process = Process({ name: 'Test Process' })
27
+
28
+ expect(process.name).toBe('Test Process')
29
+ expect(process.category).toBe('support')
30
+ expect(process.steps).toEqual([])
31
+ expect(process.inputs).toEqual([])
32
+ expect(process.outputs).toEqual([])
33
+ expect(process.metrics).toEqual([])
34
+ expect(process.metadata).toEqual({})
35
+ })
36
+
37
+ it('should create a process with all fields', () => {
38
+ const process = Process({
39
+ name: 'Customer Onboarding',
40
+ description: 'Onboard new customers',
41
+ category: 'core',
42
+ owner: 'Customer Success',
43
+ steps: [
44
+ { order: 1, name: 'Welcome Email', automationLevel: 'automated' },
45
+ { order: 2, name: 'Setup Call', automationLevel: 'manual' },
46
+ ],
47
+ inputs: ['Customer Info', 'Subscription Plan'],
48
+ outputs: ['Configured Account'],
49
+ metrics: [{ name: 'Time to Value', target: 24, unit: 'hours' }],
50
+ metadata: { version: 1 },
51
+ })
52
+
53
+ expect(process.name).toBe('Customer Onboarding')
54
+ expect(process.category).toBe('core')
55
+ expect(process.steps).toHaveLength(2)
56
+ expect(process.inputs).toHaveLength(2)
57
+ expect(process.outputs).toHaveLength(1)
58
+ expect(process.metrics).toHaveLength(1)
59
+ })
60
+
61
+ it('should throw error if name is missing', () => {
62
+ expect(() => Process({ name: '' })).toThrow('Process name is required')
63
+ })
64
+ })
65
+
66
+ describe('getStepsInOrder()', () => {
67
+ const process = Process({
68
+ name: 'Test',
69
+ steps: [
70
+ { order: 3, name: 'Third' },
71
+ { order: 1, name: 'First' },
72
+ { order: 2, name: 'Second' },
73
+ ],
74
+ })
75
+
76
+ it('should return steps sorted by order', () => {
77
+ const sorted = getStepsInOrder(process)
78
+
79
+ expect(sorted[0]?.name).toBe('First')
80
+ expect(sorted[1]?.name).toBe('Second')
81
+ expect(sorted[2]?.name).toBe('Third')
82
+ })
83
+
84
+ it('should handle empty steps', () => {
85
+ const emptyProcess = Process({ name: 'Empty' })
86
+ const sorted = getStepsInOrder(emptyProcess)
87
+
88
+ expect(sorted).toEqual([])
89
+ })
90
+ })
91
+
92
+ describe('getStepsByAutomationLevel()', () => {
93
+ const process = Process({
94
+ name: 'Test',
95
+ steps: [
96
+ { order: 1, name: 'Step 1', automationLevel: 'automated' },
97
+ { order: 2, name: 'Step 2', automationLevel: 'manual' },
98
+ { order: 3, name: 'Step 3', automationLevel: 'automated' },
99
+ { order: 4, name: 'Step 4', automationLevel: 'semi-automated' },
100
+ ],
101
+ })
102
+
103
+ it('should filter by automated level', () => {
104
+ const automated = getStepsByAutomationLevel(process, 'automated')
105
+
106
+ expect(automated).toHaveLength(2)
107
+ expect(automated[0]?.name).toBe('Step 1')
108
+ expect(automated[1]?.name).toBe('Step 3')
109
+ })
110
+
111
+ it('should filter by manual level', () => {
112
+ const manual = getStepsByAutomationLevel(process, 'manual')
113
+
114
+ expect(manual).toHaveLength(1)
115
+ expect(manual[0]?.name).toBe('Step 2')
116
+ })
117
+
118
+ it('should return empty for non-existent level', () => {
119
+ const emptyProcess = Process({ name: 'Empty' })
120
+ const result = getStepsByAutomationLevel(emptyProcess, 'automated')
121
+
122
+ expect(result).toEqual([])
123
+ })
124
+ })
125
+
126
+ describe('calculateTotalDuration()', () => {
127
+ it('should calculate total duration in minutes', () => {
128
+ const process = Process({
129
+ name: 'Test',
130
+ steps: [
131
+ { order: 1, name: 'Step 1', duration: '5 minutes' },
132
+ { order: 2, name: 'Step 2', duration: '30 minutes' },
133
+ { order: 3, name: 'Step 3', duration: '1 hour' },
134
+ ],
135
+ })
136
+
137
+ expect(calculateTotalDuration(process)).toBe(95)
138
+ })
139
+
140
+ it('should handle hours', () => {
141
+ const process = Process({
142
+ name: 'Test',
143
+ steps: [{ order: 1, name: 'Step 1', duration: '2 hours' }],
144
+ })
145
+
146
+ expect(calculateTotalDuration(process)).toBe(120)
147
+ })
148
+
149
+ it('should handle days', () => {
150
+ const process = Process({
151
+ name: 'Test',
152
+ steps: [{ order: 1, name: 'Step 1', duration: '1 day' }],
153
+ })
154
+
155
+ expect(calculateTotalDuration(process)).toBe(1440)
156
+ })
157
+
158
+ it('should handle weeks', () => {
159
+ const process = Process({
160
+ name: 'Test',
161
+ steps: [{ order: 1, name: 'Step 1', duration: '1 week' }],
162
+ })
163
+
164
+ expect(calculateTotalDuration(process)).toBe(10080)
165
+ })
166
+
167
+ it('should handle missing duration', () => {
168
+ const process = Process({
169
+ name: 'Test',
170
+ steps: [
171
+ { order: 1, name: 'Step 1' },
172
+ { order: 2, name: 'Step 2', duration: '10 minutes' },
173
+ ],
174
+ })
175
+
176
+ expect(calculateTotalDuration(process)).toBe(10)
177
+ })
178
+
179
+ it('should return 0 for empty steps', () => {
180
+ const process = Process({ name: 'Test' })
181
+ expect(calculateTotalDuration(process)).toBe(0)
182
+ })
183
+
184
+ it('should handle singular/plural units', () => {
185
+ const process = Process({
186
+ name: 'Test',
187
+ steps: [
188
+ { order: 1, name: 'Step 1', duration: '1 minute' },
189
+ { order: 2, name: 'Step 2', duration: '1 hour' },
190
+ { order: 3, name: 'Step 3', duration: '1 day' },
191
+ ],
192
+ })
193
+
194
+ expect(calculateTotalDuration(process)).toBe(1 + 60 + 1440)
195
+ })
196
+
197
+ it('should handle abbreviations', () => {
198
+ const process = Process({
199
+ name: 'Test',
200
+ steps: [
201
+ { order: 1, name: 'Step 1', duration: '5 min' },
202
+ { order: 2, name: 'Step 2', duration: '1 hr' },
203
+ ],
204
+ })
205
+
206
+ expect(calculateTotalDuration(process)).toBe(65)
207
+ })
208
+ })
209
+
210
+ describe('formatDuration()', () => {
211
+ it('should format minutes', () => {
212
+ expect(formatDuration(45)).toBe('45 minutes')
213
+ })
214
+
215
+ it('should format hours', () => {
216
+ expect(formatDuration(120)).toBe('2 hours')
217
+ })
218
+
219
+ it('should format hours and minutes', () => {
220
+ expect(formatDuration(150)).toBe('2 hours 30 minutes')
221
+ })
222
+
223
+ it('should format days', () => {
224
+ expect(formatDuration(2880)).toBe('2 days')
225
+ })
226
+
227
+ it('should format days and hours', () => {
228
+ expect(formatDuration(1560)).toBe('1 days 2 hours')
229
+ })
230
+ })
231
+
232
+ describe('calculateAutomationPercentage()', () => {
233
+ it('should calculate percentage of automated steps', () => {
234
+ const process = Process({
235
+ name: 'Test',
236
+ steps: [
237
+ { order: 1, name: 'Step 1', automationLevel: 'automated' },
238
+ { order: 2, name: 'Step 2', automationLevel: 'automated' },
239
+ { order: 3, name: 'Step 3', automationLevel: 'manual' },
240
+ { order: 4, name: 'Step 4', automationLevel: 'semi-automated' },
241
+ ],
242
+ })
243
+
244
+ expect(calculateAutomationPercentage(process)).toBe(75)
245
+ })
246
+
247
+ it('should return 0 for empty steps', () => {
248
+ const process = Process({ name: 'Test' })
249
+ expect(calculateAutomationPercentage(process)).toBe(0)
250
+ })
251
+
252
+ it('should return 100 for fully automated process', () => {
253
+ const process = Process({
254
+ name: 'Test',
255
+ steps: [
256
+ { order: 1, name: 'Step 1', automationLevel: 'automated' },
257
+ { order: 2, name: 'Step 2', automationLevel: 'semi-automated' },
258
+ ],
259
+ })
260
+
261
+ expect(calculateAutomationPercentage(process)).toBe(100)
262
+ })
263
+
264
+ it('should return 0 for fully manual process', () => {
265
+ const process = Process({
266
+ name: 'Test',
267
+ steps: [
268
+ { order: 1, name: 'Step 1', automationLevel: 'manual' },
269
+ { order: 2, name: 'Step 2', automationLevel: 'manual' },
270
+ ],
271
+ })
272
+
273
+ expect(calculateAutomationPercentage(process)).toBe(0)
274
+ })
275
+ })
276
+
277
+ describe('getMetric()', () => {
278
+ const process = Process({
279
+ name: 'Test',
280
+ metrics: [
281
+ { name: 'Time to Value', target: 24 },
282
+ { name: 'Completion Rate', target: 90 },
283
+ ],
284
+ })
285
+
286
+ it('should find metric by name', () => {
287
+ const metric = getMetric(process, 'Time to Value')
288
+
289
+ expect(metric?.name).toBe('Time to Value')
290
+ expect(metric?.target).toBe(24)
291
+ })
292
+
293
+ it('should return undefined for non-existent metric', () => {
294
+ const metric = getMetric(process, 'NonExistent')
295
+ expect(metric).toBeUndefined()
296
+ })
297
+ })
298
+
299
+ describe('meetsTarget()', () => {
300
+ it('should return true when current meets target', () => {
301
+ const metric: ProcessMetric = { name: 'Test', target: 90, current: 95 }
302
+ expect(meetsTarget(metric)).toBe(true)
303
+ })
304
+
305
+ it('should return true when current equals target', () => {
306
+ const metric: ProcessMetric = { name: 'Test', target: 90, current: 90 }
307
+ expect(meetsTarget(metric)).toBe(true)
308
+ })
309
+
310
+ it('should return false when current is below target', () => {
311
+ const metric: ProcessMetric = { name: 'Test', target: 90, current: 85 }
312
+ expect(meetsTarget(metric)).toBe(false)
313
+ })
314
+
315
+ it('should return false when target is undefined', () => {
316
+ const metric: ProcessMetric = { name: 'Test', current: 90 }
317
+ expect(meetsTarget(metric)).toBe(false)
318
+ })
319
+
320
+ it('should return false when current is undefined', () => {
321
+ const metric: ProcessMetric = { name: 'Test', target: 90 }
322
+ expect(meetsTarget(metric)).toBe(false)
323
+ })
324
+ })
325
+
326
+ describe('calculateMetricAchievement()', () => {
327
+ it('should calculate achievement percentage', () => {
328
+ const metric: ProcessMetric = { name: 'Test', target: 100, current: 75 }
329
+ expect(calculateMetricAchievement(metric)).toBe(75)
330
+ })
331
+
332
+ it('should handle over-achievement', () => {
333
+ const metric: ProcessMetric = { name: 'Test', target: 100, current: 150 }
334
+ expect(calculateMetricAchievement(metric)).toBe(150)
335
+ })
336
+
337
+ it('should return 100 when target is 0', () => {
338
+ const metric: ProcessMetric = { name: 'Test', target: 0, current: 50 }
339
+ expect(calculateMetricAchievement(metric)).toBe(100)
340
+ })
341
+
342
+ it('should return 0 when values are undefined', () => {
343
+ const metric: ProcessMetric = { name: 'Test' }
344
+ expect(calculateMetricAchievement(metric)).toBe(0)
345
+ })
346
+ })
347
+
348
+ describe('updateMetric()', () => {
349
+ const process = Process({
350
+ name: 'Test',
351
+ metrics: [
352
+ { name: 'Metric 1', target: 100, current: 50 },
353
+ { name: 'Metric 2', target: 200, current: 100 },
354
+ ],
355
+ })
356
+
357
+ it('should update metric current value', () => {
358
+ const updated = updateMetric(process, 'Metric 1', 75)
359
+ const metric = updated.metrics?.find((m) => m.name === 'Metric 1')
360
+
361
+ expect(metric?.current).toBe(75)
362
+ })
363
+
364
+ it('should not affect other metrics', () => {
365
+ const updated = updateMetric(process, 'Metric 1', 75)
366
+ const metric2 = updated.metrics?.find((m) => m.name === 'Metric 2')
367
+
368
+ expect(metric2?.current).toBe(100)
369
+ })
370
+
371
+ it('should handle non-existent metric', () => {
372
+ const updated = updateMetric(process, 'NonExistent', 75)
373
+ expect(updated.metrics).toHaveLength(2)
374
+ })
375
+ })
376
+
377
+ describe('addStep()', () => {
378
+ it('should add step to process', () => {
379
+ const process = Process({
380
+ name: 'Test',
381
+ steps: [{ order: 1, name: 'First' }],
382
+ })
383
+
384
+ const updated = addStep(process, { order: 2, name: 'Second' })
385
+
386
+ expect(updated.steps).toHaveLength(2)
387
+ expect(updated.steps?.[1]?.name).toBe('Second')
388
+ })
389
+
390
+ it('should add step to empty process', () => {
391
+ const process = Process({ name: 'Test' })
392
+ const updated = addStep(process, { order: 1, name: 'First' })
393
+
394
+ expect(updated.steps).toHaveLength(1)
395
+ })
396
+ })
397
+
398
+ describe('removeStep()', () => {
399
+ const process = Process({
400
+ name: 'Test',
401
+ steps: [
402
+ { order: 1, name: 'First' },
403
+ { order: 2, name: 'Second' },
404
+ { order: 3, name: 'Third' },
405
+ ],
406
+ })
407
+
408
+ it('should remove step by order', () => {
409
+ const updated = removeStep(process, 2)
410
+
411
+ expect(updated.steps).toHaveLength(2)
412
+ expect(updated.steps?.find((s) => s.order === 2)).toBeUndefined()
413
+ })
414
+
415
+ it('should handle removing non-existent step', () => {
416
+ const updated = removeStep(process, 99)
417
+ expect(updated.steps).toHaveLength(3)
418
+ })
419
+ })
420
+
421
+ describe('validateProcess()', () => {
422
+ it('should validate valid process', () => {
423
+ const process: ProcessDefinition = {
424
+ name: 'Valid Process',
425
+ steps: [
426
+ { order: 1, name: 'Step 1' },
427
+ { order: 2, name: 'Step 2' },
428
+ ],
429
+ metrics: [{ name: 'Metric 1', target: 100, current: 50 }],
430
+ }
431
+
432
+ const result = validateProcess(process)
433
+ expect(result.valid).toBe(true)
434
+ expect(result.errors).toHaveLength(0)
435
+ })
436
+
437
+ it('should fail if name is missing', () => {
438
+ const process: ProcessDefinition = { name: '' }
439
+
440
+ const result = validateProcess(process)
441
+ expect(result.valid).toBe(false)
442
+ expect(result.errors).toContain('Process name is required')
443
+ })
444
+
445
+ it('should fail if step has no name', () => {
446
+ const process: ProcessDefinition = {
447
+ name: 'Test',
448
+ steps: [{ order: 1, name: '' }],
449
+ }
450
+
451
+ const result = validateProcess(process)
452
+ expect(result.valid).toBe(false)
453
+ expect(result.errors).toContain('Step at order 1 must have a name')
454
+ })
455
+
456
+ it('should fail for duplicate step orders', () => {
457
+ const process: ProcessDefinition = {
458
+ name: 'Test',
459
+ steps: [
460
+ { order: 1, name: 'First' },
461
+ { order: 1, name: 'Duplicate' },
462
+ ],
463
+ }
464
+
465
+ const result = validateProcess(process)
466
+ expect(result.valid).toBe(false)
467
+ expect(result.errors).toContain('Duplicate step order: 1')
468
+ })
469
+
470
+ it('should fail if metric has no name', () => {
471
+ const process: ProcessDefinition = {
472
+ name: 'Test',
473
+ metrics: [{ name: '', target: 100 }],
474
+ }
475
+
476
+ const result = validateProcess(process)
477
+ expect(result.valid).toBe(false)
478
+ expect(result.errors).toContain('Metric must have a name')
479
+ })
480
+
481
+ it('should fail if metric target is negative', () => {
482
+ const process: ProcessDefinition = {
483
+ name: 'Test',
484
+ metrics: [{ name: 'Test Metric', target: -10 }],
485
+ }
486
+
487
+ const result = validateProcess(process)
488
+ expect(result.valid).toBe(false)
489
+ expect(result.errors).toContain('Metric Test Metric target cannot be negative')
490
+ })
491
+
492
+ it('should fail if metric current is negative', () => {
493
+ const process: ProcessDefinition = {
494
+ name: 'Test',
495
+ metrics: [{ name: 'Test Metric', target: 100, current: -5 }],
496
+ }
497
+
498
+ const result = validateProcess(process)
499
+ expect(result.valid).toBe(false)
500
+ expect(result.errors).toContain('Metric Test Metric current value cannot be negative')
501
+ })
502
+ })
503
+ })