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.
- package/CHANGELOG.md +18 -0
- package/README.md +2 -0
- package/package.json +7 -4
- package/src/dollar.ts +5 -2
- package/src/entities/organization.ts +31 -18
- package/src/goals.ts +78 -12
- package/src/index.ts +48 -18
- package/src/kpis.ts +62 -8
- package/src/metrics.ts +92 -79
- package/src/okrs.ts +120 -20
- package/src/organization.ts +12 -15
- package/src/process.ts +11 -12
- package/src/product.ts +8 -9
- package/src/queries.ts +238 -75
- package/src/roles.ts +62 -61
- package/src/workflow.ts +22 -15
- package/test/business.test.ts +282 -0
- package/test/dollar.test.ts +270 -0
- package/test/entities.test.ts +628 -0
- package/test/financials.test.ts +539 -0
- package/test/goals.test.ts +451 -0
- package/{src → test}/index.test.ts +1 -1
- package/test/kpis.test.ts +440 -0
- package/test/metrics.test.ts +744 -0
- package/test/okrs.test.ts +741 -0
- package/test/organization.test.ts +548 -0
- package/test/process.test.ts +503 -0
- package/test/product.test.ts +430 -0
- package/test/queries.test.ts +556 -0
- package/test/roles.test.ts +546 -0
- package/test/service.test.ts +450 -0
- package/test/types.test.ts +1141 -0
- package/test/vision.test.ts +214 -0
- package/test/workflow.test.ts +501 -0
- package/vitest.config.ts +47 -0
- package/.turbo/turbo-build.log +0 -5
- package/dist/business.d.ts +0 -62
- package/dist/business.d.ts.map +0 -1
- package/dist/business.js +0 -109
- package/dist/business.js.map +0 -1
- package/dist/dollar.d.ts +0 -60
- package/dist/dollar.d.ts.map +0 -1
- package/dist/dollar.js +0 -107
- package/dist/dollar.js.map +0 -1
- package/dist/entities/assets.d.ts +0 -21
- package/dist/entities/assets.d.ts.map +0 -1
- package/dist/entities/assets.js +0 -323
- package/dist/entities/assets.js.map +0 -1
- package/dist/entities/business.d.ts +0 -36
- package/dist/entities/business.d.ts.map +0 -1
- package/dist/entities/business.js +0 -370
- package/dist/entities/business.js.map +0 -1
- package/dist/entities/communication.d.ts +0 -21
- package/dist/entities/communication.d.ts.map +0 -1
- package/dist/entities/communication.js +0 -255
- package/dist/entities/communication.js.map +0 -1
- package/dist/entities/customers.d.ts +0 -58
- package/dist/entities/customers.d.ts.map +0 -1
- package/dist/entities/customers.js +0 -989
- package/dist/entities/customers.js.map +0 -1
- package/dist/entities/financials.d.ts +0 -59
- package/dist/entities/financials.d.ts.map +0 -1
- package/dist/entities/financials.js +0 -932
- package/dist/entities/financials.js.map +0 -1
- package/dist/entities/goals.d.ts +0 -58
- package/dist/entities/goals.d.ts.map +0 -1
- package/dist/entities/goals.js +0 -800
- package/dist/entities/goals.js.map +0 -1
- package/dist/entities/index.d.ts +0 -299
- package/dist/entities/index.d.ts.map +0 -1
- package/dist/entities/index.js +0 -198
- package/dist/entities/index.js.map +0 -1
- package/dist/entities/legal.d.ts +0 -21
- package/dist/entities/legal.d.ts.map +0 -1
- package/dist/entities/legal.js +0 -301
- package/dist/entities/legal.js.map +0 -1
- package/dist/entities/market.d.ts +0 -21
- package/dist/entities/market.d.ts.map +0 -1
- package/dist/entities/market.js +0 -301
- package/dist/entities/market.js.map +0 -1
- package/dist/entities/marketing.d.ts +0 -67
- package/dist/entities/marketing.d.ts.map +0 -1
- package/dist/entities/marketing.js +0 -1157
- package/dist/entities/marketing.js.map +0 -1
- package/dist/entities/offerings.d.ts +0 -51
- package/dist/entities/offerings.d.ts.map +0 -1
- package/dist/entities/offerings.js +0 -727
- package/dist/entities/offerings.js.map +0 -1
- package/dist/entities/operations.d.ts +0 -58
- package/dist/entities/operations.d.ts.map +0 -1
- package/dist/entities/operations.js +0 -787
- package/dist/entities/operations.js.map +0 -1
- package/dist/entities/organization.d.ts +0 -57
- package/dist/entities/organization.d.ts.map +0 -1
- package/dist/entities/organization.js +0 -807
- package/dist/entities/organization.js.map +0 -1
- package/dist/entities/partnerships.d.ts +0 -21
- package/dist/entities/partnerships.d.ts.map +0 -1
- package/dist/entities/partnerships.js +0 -300
- package/dist/entities/partnerships.js.map +0 -1
- package/dist/entities/planning.d.ts +0 -87
- package/dist/entities/planning.d.ts.map +0 -1
- package/dist/entities/planning.js +0 -271
- package/dist/entities/planning.js.map +0 -1
- package/dist/entities/projects.d.ts +0 -25
- package/dist/entities/projects.d.ts.map +0 -1
- package/dist/entities/projects.js +0 -349
- package/dist/entities/projects.js.map +0 -1
- package/dist/entities/risk.d.ts +0 -21
- package/dist/entities/risk.d.ts.map +0 -1
- package/dist/entities/risk.js +0 -293
- package/dist/entities/risk.js.map +0 -1
- package/dist/entities/sales.d.ts +0 -72
- package/dist/entities/sales.d.ts.map +0 -1
- package/dist/entities/sales.js +0 -1248
- package/dist/entities/sales.js.map +0 -1
- package/dist/financials.d.ts +0 -130
- package/dist/financials.d.ts.map +0 -1
- package/dist/financials.js +0 -297
- package/dist/financials.js.map +0 -1
- package/dist/goals.d.ts +0 -87
- package/dist/goals.d.ts.map +0 -1
- package/dist/goals.js +0 -215
- package/dist/goals.js.map +0 -1
- package/dist/index.d.ts +0 -97
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -132
- package/dist/index.js.map +0 -1
- package/dist/kpis.d.ts +0 -118
- package/dist/kpis.d.ts.map +0 -1
- package/dist/kpis.js +0 -232
- package/dist/kpis.js.map +0 -1
- package/dist/metrics.d.ts +0 -448
- package/dist/metrics.d.ts.map +0 -1
- package/dist/metrics.js +0 -325
- package/dist/metrics.js.map +0 -1
- package/dist/okrs.d.ts +0 -123
- package/dist/okrs.d.ts.map +0 -1
- package/dist/okrs.js +0 -269
- package/dist/okrs.js.map +0 -1
- package/dist/organization.d.ts +0 -585
- package/dist/organization.d.ts.map +0 -1
- package/dist/organization.js +0 -173
- package/dist/organization.js.map +0 -1
- package/dist/process.d.ts +0 -112
- package/dist/process.d.ts.map +0 -1
- package/dist/process.js +0 -241
- package/dist/process.js.map +0 -1
- package/dist/product.d.ts +0 -85
- package/dist/product.d.ts.map +0 -1
- package/dist/product.js +0 -145
- package/dist/product.js.map +0 -1
- package/dist/queries.d.ts +0 -304
- package/dist/queries.d.ts.map +0 -1
- package/dist/queries.js +0 -415
- package/dist/queries.js.map +0 -1
- package/dist/roles.d.ts +0 -340
- package/dist/roles.d.ts.map +0 -1
- package/dist/roles.js +0 -255
- package/dist/roles.js.map +0 -1
- package/dist/service.d.ts +0 -61
- package/dist/service.d.ts.map +0 -1
- package/dist/service.js +0 -140
- package/dist/service.js.map +0 -1
- package/dist/types.d.ts +0 -459
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -5
- package/dist/types.js.map +0 -1
- package/dist/vision.d.ts +0 -38
- package/dist/vision.d.ts.map +0 -1
- package/dist/vision.js +0 -68
- package/dist/vision.js.map +0 -1
- package/dist/workflow.d.ts +0 -115
- package/dist/workflow.d.ts.map +0 -1
- package/dist/workflow.js +0 -247
- package/dist/workflow.js.map +0 -1
- package/src/business.js +0 -108
- package/src/dollar.js +0 -106
- package/src/entities/assets.js +0 -322
- package/src/entities/business.js +0 -369
- package/src/entities/communication.js +0 -254
- package/src/entities/customers.js +0 -988
- package/src/entities/financials.js +0 -931
- package/src/entities/goals.js +0 -799
- package/src/entities/index.js +0 -197
- package/src/entities/legal.js +0 -300
- package/src/entities/market.js +0 -300
- package/src/entities/marketing.js +0 -1156
- package/src/entities/offerings.js +0 -726
- package/src/entities/operations.js +0 -786
- package/src/entities/organization.js +0 -806
- package/src/entities/partnerships.js +0 -299
- package/src/entities/planning.js +0 -270
- package/src/entities/projects.js +0 -348
- package/src/entities/risk.js +0 -292
- package/src/entities/sales.js +0 -1247
- package/src/financials.js +0 -296
- package/src/goals.js +0 -214
- package/src/index.js +0 -131
- package/src/index.test.js +0 -274
- package/src/kpis.js +0 -231
- package/src/metrics.js +0 -324
- package/src/okrs.js +0 -268
- package/src/organization.js +0 -172
- package/src/process.js +0 -240
- package/src/product.js +0 -144
- package/src/queries.js +0 -414
- package/src/roles.js +0 -254
- package/src/service.js +0 -139
- package/src/types.js +0 -4
- package/src/vision.js +0 -67
- package/src/workflow.js +0 -246
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for kpis.ts - Key Performance Indicators management
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect } from 'vitest'
|
|
6
|
+
import {
|
|
7
|
+
kpis,
|
|
8
|
+
kpi,
|
|
9
|
+
calculateAchievement,
|
|
10
|
+
meetsTarget,
|
|
11
|
+
updateCurrent,
|
|
12
|
+
updateTarget,
|
|
13
|
+
getKPIsByCategory,
|
|
14
|
+
getKPIsByFrequency,
|
|
15
|
+
getKPIsOnTarget,
|
|
16
|
+
getKPIsOffTarget,
|
|
17
|
+
calculateHealthScore,
|
|
18
|
+
groupByCategory,
|
|
19
|
+
calculateVariance,
|
|
20
|
+
calculateVariancePercentage,
|
|
21
|
+
formatValue,
|
|
22
|
+
comparePerformance,
|
|
23
|
+
validateKPIs,
|
|
24
|
+
} from '../src/kpis.js'
|
|
25
|
+
import type { KPIDefinition } from '../src/types.js'
|
|
26
|
+
|
|
27
|
+
describe('KPIs', () => {
|
|
28
|
+
describe('kpis()', () => {
|
|
29
|
+
it('should create multiple KPIs', () => {
|
|
30
|
+
const kpiList = kpis([
|
|
31
|
+
{ name: 'Revenue', target: 100000 },
|
|
32
|
+
{ name: 'Churn', target: 5 },
|
|
33
|
+
])
|
|
34
|
+
|
|
35
|
+
expect(kpiList).toHaveLength(2)
|
|
36
|
+
expect(kpiList[0]?.name).toBe('Revenue')
|
|
37
|
+
expect(kpiList[1]?.name).toBe('Churn')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('should normalize KPI defaults', () => {
|
|
41
|
+
const kpiList = kpis([{ name: 'Test KPI' }])
|
|
42
|
+
|
|
43
|
+
expect(kpiList[0]?.category).toBe('operations')
|
|
44
|
+
expect(kpiList[0]?.frequency).toBe('monthly')
|
|
45
|
+
expect(kpiList[0]?.metadata).toEqual({})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should throw error for KPI without name', () => {
|
|
49
|
+
expect(() => kpis([{ name: '' }])).toThrow('KPI name is required')
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('kpi()', () => {
|
|
54
|
+
it('should create a single KPI', () => {
|
|
55
|
+
const singleKpi = kpi({
|
|
56
|
+
name: 'Monthly Revenue',
|
|
57
|
+
category: 'financial',
|
|
58
|
+
target: 100000,
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
expect(singleKpi.name).toBe('Monthly Revenue')
|
|
62
|
+
expect(singleKpi.category).toBe('financial')
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe('calculateAchievement()', () => {
|
|
67
|
+
it('should calculate achievement percentage', () => {
|
|
68
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 75 }
|
|
69
|
+
expect(calculateAchievement(testKpi)).toBe(75)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('should handle over-achievement', () => {
|
|
73
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 150 }
|
|
74
|
+
expect(calculateAchievement(testKpi)).toBe(150)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('should return 0 if target is undefined', () => {
|
|
78
|
+
const testKpi: KPIDefinition = { name: 'Test', current: 75 }
|
|
79
|
+
expect(calculateAchievement(testKpi)).toBe(0)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('should return 0 if current is undefined', () => {
|
|
83
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100 }
|
|
84
|
+
expect(calculateAchievement(testKpi)).toBe(0)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('should return 100 if target is 0', () => {
|
|
88
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 0, current: 50 }
|
|
89
|
+
expect(calculateAchievement(testKpi)).toBe(100)
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe('meetsTarget()', () => {
|
|
94
|
+
it('should return true when current exceeds target', () => {
|
|
95
|
+
const testKpi: KPIDefinition = { name: 'Revenue', target: 100, current: 120 }
|
|
96
|
+
expect(meetsTarget(testKpi)).toBe(true)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('should return true when current equals target', () => {
|
|
100
|
+
const testKpi: KPIDefinition = { name: 'Revenue', target: 100, current: 100 }
|
|
101
|
+
expect(meetsTarget(testKpi)).toBe(true)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('should return false when current is below target', () => {
|
|
105
|
+
const testKpi: KPIDefinition = { name: 'Revenue', target: 100, current: 80 }
|
|
106
|
+
expect(meetsTarget(testKpi)).toBe(false)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('should handle lower-is-better metrics (churn)', () => {
|
|
110
|
+
const churnKpi: KPIDefinition = { name: 'Customer Churn Rate', target: 5, current: 3 }
|
|
111
|
+
expect(meetsTarget(churnKpi)).toBe(true)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('should handle lower-is-better metrics (cost)', () => {
|
|
115
|
+
const costKpi: KPIDefinition = { name: 'Operational Cost', target: 10000, current: 8000 }
|
|
116
|
+
expect(meetsTarget(costKpi)).toBe(true)
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('should handle lower-is-better metrics (time)', () => {
|
|
120
|
+
const timeKpi: KPIDefinition = { name: 'Response Time', target: 100, current: 150 }
|
|
121
|
+
expect(meetsTarget(timeKpi)).toBe(false)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('should handle lower-is-better metrics (error)', () => {
|
|
125
|
+
const errorKpi: KPIDefinition = { name: 'Error Rate', target: 1, current: 0.5 }
|
|
126
|
+
expect(meetsTarget(errorKpi)).toBe(true)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('should handle lower-is-better metrics (downtime)', () => {
|
|
130
|
+
const downtimeKpi: KPIDefinition = { name: 'System Downtime', target: 5, current: 10 }
|
|
131
|
+
expect(meetsTarget(downtimeKpi)).toBe(false)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('should return false for undefined values', () => {
|
|
135
|
+
expect(meetsTarget({ name: 'Test' })).toBe(false)
|
|
136
|
+
expect(meetsTarget({ name: 'Test', target: 100 })).toBe(false)
|
|
137
|
+
expect(meetsTarget({ name: 'Test', current: 80 })).toBe(false)
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
describe('updateCurrent()', () => {
|
|
142
|
+
it('should update current value', () => {
|
|
143
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 50 }
|
|
144
|
+
const updated = updateCurrent(testKpi, 75)
|
|
145
|
+
|
|
146
|
+
expect(updated.current).toBe(75)
|
|
147
|
+
expect(updated.target).toBe(100)
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
describe('updateTarget()', () => {
|
|
152
|
+
it('should update target value', () => {
|
|
153
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 50 }
|
|
154
|
+
const updated = updateTarget(testKpi, 150)
|
|
155
|
+
|
|
156
|
+
expect(updated.target).toBe(150)
|
|
157
|
+
expect(updated.current).toBe(50)
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
describe('getKPIsByCategory()', () => {
|
|
162
|
+
const kpiList = kpis([
|
|
163
|
+
{ name: 'Revenue', category: 'financial' },
|
|
164
|
+
{ name: 'NPS', category: 'customer' },
|
|
165
|
+
{ name: 'MRR', category: 'financial' },
|
|
166
|
+
{ name: 'Uptime', category: 'operations' },
|
|
167
|
+
])
|
|
168
|
+
|
|
169
|
+
it('should filter by category', () => {
|
|
170
|
+
const financial = getKPIsByCategory(kpiList, 'financial')
|
|
171
|
+
|
|
172
|
+
expect(financial).toHaveLength(2)
|
|
173
|
+
expect(financial[0]?.name).toBe('Revenue')
|
|
174
|
+
expect(financial[1]?.name).toBe('MRR')
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('should return empty array for non-existent category', () => {
|
|
178
|
+
const growth = getKPIsByCategory(kpiList, 'growth')
|
|
179
|
+
expect(growth).toHaveLength(0)
|
|
180
|
+
})
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
describe('getKPIsByFrequency()', () => {
|
|
184
|
+
const kpiList = kpis([
|
|
185
|
+
{ name: 'Daily Active Users', frequency: 'daily' },
|
|
186
|
+
{ name: 'MRR', frequency: 'monthly' },
|
|
187
|
+
{ name: 'Weekly Sales', frequency: 'weekly' },
|
|
188
|
+
{ name: 'Monthly Revenue', frequency: 'monthly' },
|
|
189
|
+
])
|
|
190
|
+
|
|
191
|
+
it('should filter by frequency', () => {
|
|
192
|
+
const monthly = getKPIsByFrequency(kpiList, 'monthly')
|
|
193
|
+
|
|
194
|
+
expect(monthly).toHaveLength(2)
|
|
195
|
+
expect(monthly[0]?.name).toBe('MRR')
|
|
196
|
+
expect(monthly[1]?.name).toBe('Monthly Revenue')
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
describe('getKPIsOnTarget() and getKPIsOffTarget()', () => {
|
|
201
|
+
const kpiList: KPIDefinition[] = [
|
|
202
|
+
{ name: 'Revenue', target: 100, current: 120 },
|
|
203
|
+
{ name: 'NPS', target: 50, current: 45 },
|
|
204
|
+
{ name: 'Churn Rate', target: 5, current: 3 },
|
|
205
|
+
{ name: 'Cost', target: 10000, current: 12000 },
|
|
206
|
+
]
|
|
207
|
+
|
|
208
|
+
it('should get KPIs on target', () => {
|
|
209
|
+
const onTarget = getKPIsOnTarget(kpiList)
|
|
210
|
+
|
|
211
|
+
expect(onTarget).toHaveLength(2)
|
|
212
|
+
expect(onTarget.map((k) => k.name)).toContain('Revenue')
|
|
213
|
+
expect(onTarget.map((k) => k.name)).toContain('Churn Rate')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('should get KPIs off target', () => {
|
|
217
|
+
const offTarget = getKPIsOffTarget(kpiList)
|
|
218
|
+
|
|
219
|
+
expect(offTarget).toHaveLength(2)
|
|
220
|
+
expect(offTarget.map((k) => k.name)).toContain('NPS')
|
|
221
|
+
expect(offTarget.map((k) => k.name)).toContain('Cost')
|
|
222
|
+
})
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
describe('calculateHealthScore()', () => {
|
|
226
|
+
it('should calculate health score as percentage on target', () => {
|
|
227
|
+
const kpiList: KPIDefinition[] = [
|
|
228
|
+
{ name: 'KPI 1', target: 100, current: 120 },
|
|
229
|
+
{ name: 'KPI 2', target: 100, current: 80 },
|
|
230
|
+
{ name: 'KPI 3', target: 100, current: 150 },
|
|
231
|
+
{ name: 'KPI 4', target: 100, current: 90 },
|
|
232
|
+
]
|
|
233
|
+
|
|
234
|
+
expect(calculateHealthScore(kpiList)).toBe(50)
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it('should return 0 for empty KPI list', () => {
|
|
238
|
+
expect(calculateHealthScore([])).toBe(0)
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('should return 100 when all KPIs on target', () => {
|
|
242
|
+
const kpiList: KPIDefinition[] = [
|
|
243
|
+
{ name: 'KPI 1', target: 100, current: 120 },
|
|
244
|
+
{ name: 'KPI 2', target: 100, current: 100 },
|
|
245
|
+
]
|
|
246
|
+
|
|
247
|
+
expect(calculateHealthScore(kpiList)).toBe(100)
|
|
248
|
+
})
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
describe('groupByCategory()', () => {
|
|
252
|
+
const kpiList = kpis([
|
|
253
|
+
{ name: 'Revenue', category: 'financial' },
|
|
254
|
+
{ name: 'NPS', category: 'customer' },
|
|
255
|
+
{ name: 'MRR', category: 'financial' },
|
|
256
|
+
])
|
|
257
|
+
|
|
258
|
+
it('should group KPIs by category', () => {
|
|
259
|
+
const grouped = groupByCategory(kpiList)
|
|
260
|
+
|
|
261
|
+
expect(grouped.get('financial')).toHaveLength(2)
|
|
262
|
+
expect(grouped.get('customer')).toHaveLength(1)
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
it('should handle KPIs without category', () => {
|
|
266
|
+
const kpisWithoutCategory: KPIDefinition[] = [{ name: 'Test' }]
|
|
267
|
+
const grouped = groupByCategory(kpisWithoutCategory)
|
|
268
|
+
|
|
269
|
+
expect(grouped.get('other')).toHaveLength(1)
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
describe('calculateVariance()', () => {
|
|
274
|
+
it('should calculate variance from target', () => {
|
|
275
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 120 }
|
|
276
|
+
expect(calculateVariance(testKpi)).toBe(20)
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
it('should handle negative variance', () => {
|
|
280
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 80 }
|
|
281
|
+
expect(calculateVariance(testKpi)).toBe(-20)
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
it('should return 0 for undefined values', () => {
|
|
285
|
+
expect(calculateVariance({ name: 'Test' })).toBe(0)
|
|
286
|
+
})
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
describe('calculateVariancePercentage()', () => {
|
|
290
|
+
it('should calculate variance percentage', () => {
|
|
291
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 120 }
|
|
292
|
+
expect(calculateVariancePercentage(testKpi)).toBe(20)
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
it('should handle negative variance', () => {
|
|
296
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 100, current: 80 }
|
|
297
|
+
expect(calculateVariancePercentage(testKpi)).toBe(-20)
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
it('should return 0 for zero target', () => {
|
|
301
|
+
const testKpi: KPIDefinition = { name: 'Test', target: 0, current: 100 }
|
|
302
|
+
expect(calculateVariancePercentage(testKpi)).toBe(0)
|
|
303
|
+
})
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
describe('formatValue()', () => {
|
|
307
|
+
it('should format with USD unit', () => {
|
|
308
|
+
const testKpi: KPIDefinition = { name: 'Revenue', unit: 'USD', current: 1234.56 }
|
|
309
|
+
expect(formatValue(testKpi)).toMatch(/\$1,234\.56/)
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
it('should format with percent unit', () => {
|
|
313
|
+
const testKpi: KPIDefinition = { name: 'Growth', unit: 'percent', current: 25.5 }
|
|
314
|
+
expect(formatValue(testKpi)).toBe('25.5%')
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
it('should format with % unit', () => {
|
|
318
|
+
const testKpi: KPIDefinition = { name: 'Growth', unit: '%', current: 25.5 }
|
|
319
|
+
expect(formatValue(testKpi)).toBe('25.5%')
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
it('should format with custom unit', () => {
|
|
323
|
+
const testKpi: KPIDefinition = { name: 'Users', unit: 'users', current: 1000 }
|
|
324
|
+
expect(formatValue(testKpi)).toBe('1,000 users')
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
it('should format without unit', () => {
|
|
328
|
+
const testKpi: KPIDefinition = { name: 'Count', current: 500 }
|
|
329
|
+
expect(formatValue(testKpi)).toBe('500')
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
it('should return N/A for undefined value', () => {
|
|
333
|
+
const testKpi: KPIDefinition = { name: 'Test' }
|
|
334
|
+
expect(formatValue(testKpi)).toBe('N/A')
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
it('should format explicit value parameter', () => {
|
|
338
|
+
const testKpi: KPIDefinition = { name: 'Revenue', unit: 'USD', current: 100 }
|
|
339
|
+
expect(formatValue(testKpi, 500)).toMatch(/\$500/)
|
|
340
|
+
})
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
describe('comparePerformance()', () => {
|
|
344
|
+
it('should compare performance between periods', () => {
|
|
345
|
+
const current: KPIDefinition = { name: 'Revenue', current: 120 }
|
|
346
|
+
const previous: KPIDefinition = { name: 'Revenue', current: 100 }
|
|
347
|
+
|
|
348
|
+
const result = comparePerformance(current, previous)
|
|
349
|
+
|
|
350
|
+
expect(result.change).toBe(20)
|
|
351
|
+
expect(result.changePercent).toBe(20)
|
|
352
|
+
expect(result.improved).toBe(true)
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
it('should detect regression', () => {
|
|
356
|
+
const current: KPIDefinition = { name: 'Revenue', current: 80 }
|
|
357
|
+
const previous: KPIDefinition = { name: 'Revenue', current: 100 }
|
|
358
|
+
|
|
359
|
+
const result = comparePerformance(current, previous)
|
|
360
|
+
|
|
361
|
+
expect(result.change).toBe(-20)
|
|
362
|
+
expect(result.changePercent).toBe(-20)
|
|
363
|
+
expect(result.improved).toBe(false)
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
it('should handle lower-is-better metrics', () => {
|
|
367
|
+
const current: KPIDefinition = { name: 'Churn Rate', current: 3 }
|
|
368
|
+
const previous: KPIDefinition = { name: 'Churn Rate', current: 5 }
|
|
369
|
+
|
|
370
|
+
const result = comparePerformance(current, previous)
|
|
371
|
+
|
|
372
|
+
expect(result.change).toBe(-2)
|
|
373
|
+
expect(result.improved).toBe(true)
|
|
374
|
+
})
|
|
375
|
+
|
|
376
|
+
it('should handle cost reduction as improvement', () => {
|
|
377
|
+
const current: KPIDefinition = { name: 'Cost', current: 8000 }
|
|
378
|
+
const previous: KPIDefinition = { name: 'Cost', current: 10000 }
|
|
379
|
+
|
|
380
|
+
const result = comparePerformance(current, previous)
|
|
381
|
+
|
|
382
|
+
expect(result.improved).toBe(true)
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
it('should handle undefined values', () => {
|
|
386
|
+
const current: KPIDefinition = { name: 'Test' }
|
|
387
|
+
const previous: KPIDefinition = { name: 'Test' }
|
|
388
|
+
|
|
389
|
+
const result = comparePerformance(current, previous)
|
|
390
|
+
|
|
391
|
+
expect(result.change).toBe(0)
|
|
392
|
+
expect(result.changePercent).toBe(0)
|
|
393
|
+
expect(result.improved).toBe(false)
|
|
394
|
+
})
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
describe('validateKPIs()', () => {
|
|
398
|
+
it('should validate valid KPIs', () => {
|
|
399
|
+
const kpiList: KPIDefinition[] = [
|
|
400
|
+
{ name: 'Revenue', target: 100000, current: 80000 },
|
|
401
|
+
{ name: 'NPS', target: 50, current: 48 },
|
|
402
|
+
]
|
|
403
|
+
|
|
404
|
+
const result = validateKPIs(kpiList)
|
|
405
|
+
expect(result.valid).toBe(true)
|
|
406
|
+
expect(result.errors).toHaveLength(0)
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
it('should fail for KPI without name', () => {
|
|
410
|
+
const kpiList: KPIDefinition[] = [{ name: '' }]
|
|
411
|
+
|
|
412
|
+
const result = validateKPIs(kpiList)
|
|
413
|
+
expect(result.valid).toBe(false)
|
|
414
|
+
expect(result.errors).toContain('KPI name is required')
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
it('should fail for negative target', () => {
|
|
418
|
+
const kpiList: KPIDefinition[] = [{ name: 'Test', target: -100 }]
|
|
419
|
+
|
|
420
|
+
const result = validateKPIs(kpiList)
|
|
421
|
+
expect(result.valid).toBe(false)
|
|
422
|
+
expect(result.errors).toContain('KPI Test target cannot be negative')
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
it('should fail for negative current value', () => {
|
|
426
|
+
const kpiList: KPIDefinition[] = [{ name: 'Test', current: -50 }]
|
|
427
|
+
|
|
428
|
+
const result = validateKPIs(kpiList)
|
|
429
|
+
expect(result.valid).toBe(false)
|
|
430
|
+
expect(result.errors).toContain('KPI Test current value cannot be negative')
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
it('should allow zero values', () => {
|
|
434
|
+
const kpiList: KPIDefinition[] = [{ name: 'Test', target: 0, current: 0 }]
|
|
435
|
+
|
|
436
|
+
const result = validateKPIs(kpiList)
|
|
437
|
+
expect(result.valid).toBe(true)
|
|
438
|
+
})
|
|
439
|
+
})
|
|
440
|
+
})
|