business-as-code 2.1.3 → 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 (260) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -0
  3. package/package.json +16 -13
  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/LICENSE +0 -21
  38. package/dist/business.d.ts +0 -62
  39. package/dist/business.d.ts.map +0 -1
  40. package/dist/business.js +0 -109
  41. package/dist/business.js.map +0 -1
  42. package/dist/canvas/activities.d.ts +0 -19
  43. package/dist/canvas/activities.d.ts.map +0 -1
  44. package/dist/canvas/activities.js +0 -20
  45. package/dist/canvas/activities.js.map +0 -1
  46. package/dist/canvas/channels.d.ts +0 -20
  47. package/dist/canvas/channels.d.ts.map +0 -1
  48. package/dist/canvas/channels.js +0 -21
  49. package/dist/canvas/channels.js.map +0 -1
  50. package/dist/canvas/relationships.d.ts +0 -20
  51. package/dist/canvas/relationships.d.ts.map +0 -1
  52. package/dist/canvas/relationships.js +0 -21
  53. package/dist/canvas/relationships.js.map +0 -1
  54. package/dist/canvas/resources.d.ts +0 -20
  55. package/dist/canvas/resources.d.ts.map +0 -1
  56. package/dist/canvas/resources.js +0 -30
  57. package/dist/canvas/resources.js.map +0 -1
  58. package/dist/canvas/revenue.d.ts +0 -22
  59. package/dist/canvas/revenue.d.ts.map +0 -1
  60. package/dist/canvas/revenue.js +0 -30
  61. package/dist/canvas/revenue.js.map +0 -1
  62. package/dist/canvas/segments.d.ts +0 -20
  63. package/dist/canvas/segments.d.ts.map +0 -1
  64. package/dist/canvas/segments.js +0 -28
  65. package/dist/canvas/segments.js.map +0 -1
  66. package/dist/canvas/types.d.ts +0 -232
  67. package/dist/canvas/types.d.ts.map +0 -1
  68. package/dist/canvas/types.js +0 -8
  69. package/dist/canvas/types.js.map +0 -1
  70. package/dist/canvas/value.d.ts +0 -20
  71. package/dist/canvas/value.d.ts.map +0 -1
  72. package/dist/canvas/value.js +0 -21
  73. package/dist/canvas/value.js.map +0 -1
  74. package/dist/dollar.d.ts +0 -60
  75. package/dist/dollar.d.ts.map +0 -1
  76. package/dist/dollar.js +0 -107
  77. package/dist/dollar.js.map +0 -1
  78. package/dist/entities/assets.d.ts +0 -21
  79. package/dist/entities/assets.d.ts.map +0 -1
  80. package/dist/entities/assets.js +0 -323
  81. package/dist/entities/assets.js.map +0 -1
  82. package/dist/entities/business.d.ts +0 -36
  83. package/dist/entities/business.d.ts.map +0 -1
  84. package/dist/entities/business.js +0 -370
  85. package/dist/entities/business.js.map +0 -1
  86. package/dist/entities/communication.d.ts +0 -21
  87. package/dist/entities/communication.d.ts.map +0 -1
  88. package/dist/entities/communication.js +0 -255
  89. package/dist/entities/communication.js.map +0 -1
  90. package/dist/entities/customers.d.ts +0 -58
  91. package/dist/entities/customers.d.ts.map +0 -1
  92. package/dist/entities/customers.js +0 -989
  93. package/dist/entities/customers.js.map +0 -1
  94. package/dist/entities/financials.d.ts +0 -59
  95. package/dist/entities/financials.d.ts.map +0 -1
  96. package/dist/entities/financials.js +0 -932
  97. package/dist/entities/financials.js.map +0 -1
  98. package/dist/entities/goals.d.ts +0 -58
  99. package/dist/entities/goals.d.ts.map +0 -1
  100. package/dist/entities/goals.js +0 -800
  101. package/dist/entities/goals.js.map +0 -1
  102. package/dist/entities/index.d.ts +0 -299
  103. package/dist/entities/index.d.ts.map +0 -1
  104. package/dist/entities/index.js +0 -198
  105. package/dist/entities/index.js.map +0 -1
  106. package/dist/entities/legal.d.ts +0 -21
  107. package/dist/entities/legal.d.ts.map +0 -1
  108. package/dist/entities/legal.js +0 -301
  109. package/dist/entities/legal.js.map +0 -1
  110. package/dist/entities/market.d.ts +0 -21
  111. package/dist/entities/market.d.ts.map +0 -1
  112. package/dist/entities/market.js +0 -301
  113. package/dist/entities/market.js.map +0 -1
  114. package/dist/entities/marketing.d.ts +0 -67
  115. package/dist/entities/marketing.d.ts.map +0 -1
  116. package/dist/entities/marketing.js +0 -1157
  117. package/dist/entities/marketing.js.map +0 -1
  118. package/dist/entities/offerings.d.ts +0 -51
  119. package/dist/entities/offerings.d.ts.map +0 -1
  120. package/dist/entities/offerings.js +0 -727
  121. package/dist/entities/offerings.js.map +0 -1
  122. package/dist/entities/operations.d.ts +0 -58
  123. package/dist/entities/operations.d.ts.map +0 -1
  124. package/dist/entities/operations.js +0 -787
  125. package/dist/entities/operations.js.map +0 -1
  126. package/dist/entities/organization.d.ts +0 -57
  127. package/dist/entities/organization.d.ts.map +0 -1
  128. package/dist/entities/organization.js +0 -807
  129. package/dist/entities/organization.js.map +0 -1
  130. package/dist/entities/partnerships.d.ts +0 -21
  131. package/dist/entities/partnerships.d.ts.map +0 -1
  132. package/dist/entities/partnerships.js +0 -300
  133. package/dist/entities/partnerships.js.map +0 -1
  134. package/dist/entities/planning.d.ts +0 -0
  135. package/dist/entities/planning.d.ts.map +0 -1
  136. package/dist/entities/planning.js +0 -271
  137. package/dist/entities/planning.js.map +0 -1
  138. package/dist/entities/projects.d.ts +0 -25
  139. package/dist/entities/projects.d.ts.map +0 -1
  140. package/dist/entities/projects.js +0 -349
  141. package/dist/entities/projects.js.map +0 -1
  142. package/dist/entities/risk.d.ts +0 -21
  143. package/dist/entities/risk.d.ts.map +0 -1
  144. package/dist/entities/risk.js +0 -293
  145. package/dist/entities/risk.js.map +0 -1
  146. package/dist/entities/sales.d.ts +0 -72
  147. package/dist/entities/sales.d.ts.map +0 -1
  148. package/dist/entities/sales.js +0 -1248
  149. package/dist/entities/sales.js.map +0 -1
  150. package/dist/financials.d.ts +0 -130
  151. package/dist/financials.d.ts.map +0 -1
  152. package/dist/financials.js +0 -297
  153. package/dist/financials.js.map +0 -1
  154. package/dist/goals.d.ts +0 -87
  155. package/dist/goals.d.ts.map +0 -1
  156. package/dist/goals.js +0 -215
  157. package/dist/goals.js.map +0 -1
  158. package/dist/index.d.ts +0 -97
  159. package/dist/index.d.ts.map +0 -1
  160. package/dist/index.js +0 -132
  161. package/dist/index.js.map +0 -1
  162. package/dist/kpis.d.ts +0 -118
  163. package/dist/kpis.d.ts.map +0 -1
  164. package/dist/kpis.js +0 -232
  165. package/dist/kpis.js.map +0 -1
  166. package/dist/metrics.d.ts +0 -448
  167. package/dist/metrics.d.ts.map +0 -1
  168. package/dist/metrics.js +0 -325
  169. package/dist/metrics.js.map +0 -1
  170. package/dist/okrs.d.ts +0 -123
  171. package/dist/okrs.d.ts.map +0 -1
  172. package/dist/okrs.js +0 -269
  173. package/dist/okrs.js.map +0 -1
  174. package/dist/organization.d.ts +0 -585
  175. package/dist/organization.d.ts.map +0 -1
  176. package/dist/organization.js +0 -173
  177. package/dist/organization.js.map +0 -1
  178. package/dist/process.d.ts +0 -112
  179. package/dist/process.d.ts.map +0 -1
  180. package/dist/process.js +0 -241
  181. package/dist/process.js.map +0 -1
  182. package/dist/product.d.ts +0 -85
  183. package/dist/product.d.ts.map +0 -1
  184. package/dist/product.js +0 -145
  185. package/dist/product.js.map +0 -1
  186. package/dist/queries.d.ts +0 -304
  187. package/dist/queries.d.ts.map +0 -1
  188. package/dist/queries.js +0 -415
  189. package/dist/queries.js.map +0 -1
  190. package/dist/roles.d.ts +0 -340
  191. package/dist/roles.d.ts.map +0 -1
  192. package/dist/roles.js +0 -255
  193. package/dist/roles.js.map +0 -1
  194. package/dist/service.d.ts +0 -61
  195. package/dist/service.d.ts.map +0 -1
  196. package/dist/service.js +0 -140
  197. package/dist/service.js.map +0 -1
  198. package/dist/types.d.ts +0 -459
  199. package/dist/types.d.ts.map +0 -1
  200. package/dist/types.js +0 -5
  201. package/dist/types.js.map +0 -1
  202. package/dist/vision.d.ts +0 -38
  203. package/dist/vision.d.ts.map +0 -1
  204. package/dist/vision.js +0 -68
  205. package/dist/vision.js.map +0 -1
  206. package/dist/workflow.d.ts +0 -115
  207. package/dist/workflow.d.ts.map +0 -1
  208. package/dist/workflow.js +0 -247
  209. package/dist/workflow.js.map +0 -1
  210. package/src/business.js +0 -108
  211. package/src/canvas/activities.ts +0 -32
  212. package/src/canvas/canvas.ts +0 -482
  213. package/src/canvas/channels.ts +0 -34
  214. package/src/canvas/costs.ts +0 -43
  215. package/src/canvas/economics.ts +0 -99
  216. package/src/canvas/index.ts +0 -206
  217. package/src/canvas/partnerships.ts +0 -34
  218. package/src/canvas/projections.ts +0 -141
  219. package/src/canvas/relationships.ts +0 -34
  220. package/src/canvas/resources.ts +0 -43
  221. package/src/canvas/revenue.ts +0 -56
  222. package/src/canvas/segments.ts +0 -42
  223. package/src/canvas/types.ts +0 -363
  224. package/src/canvas/value.ts +0 -34
  225. package/src/dollar.js +0 -106
  226. package/src/entities/assets.js +0 -322
  227. package/src/entities/business.js +0 -369
  228. package/src/entities/communication.js +0 -254
  229. package/src/entities/customers.js +0 -988
  230. package/src/entities/financials.js +0 -931
  231. package/src/entities/goals.js +0 -799
  232. package/src/entities/index.js +0 -197
  233. package/src/entities/legal.js +0 -300
  234. package/src/entities/market.js +0 -300
  235. package/src/entities/marketing.js +0 -1156
  236. package/src/entities/offerings.js +0 -726
  237. package/src/entities/operations.js +0 -786
  238. package/src/entities/organization.js +0 -806
  239. package/src/entities/partnerships.js +0 -299
  240. package/src/entities/planning.js +0 -270
  241. package/src/entities/projects.js +0 -348
  242. package/src/entities/risk.js +0 -292
  243. package/src/entities/sales.js +0 -1247
  244. package/src/financials.js +0 -296
  245. package/src/goals.js +0 -214
  246. package/src/index.js +0 -131
  247. package/src/index.test.js +0 -274
  248. package/src/kpis.js +0 -231
  249. package/src/metrics.js +0 -324
  250. package/src/okrs.js +0 -268
  251. package/src/organization.js +0 -172
  252. package/src/process.js +0 -240
  253. package/src/product.js +0 -144
  254. package/src/queries.js +0 -414
  255. package/src/roles.js +0 -254
  256. package/src/service.js +0 -139
  257. package/src/types.js +0 -4
  258. package/src/vision.js +0 -67
  259. package/src/workflow.js +0 -246
  260. package/tests/canvas.test.ts +0 -842
@@ -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
+ })