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,539 @@
1
+ /**
2
+ * Tests for financials.ts - Financial metrics and calculations
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest'
6
+ import {
7
+ financials,
8
+ calculateGrossMargin,
9
+ calculateOperatingMargin,
10
+ calculateNetMargin,
11
+ calculateEBITDAMargin,
12
+ calculateBurnRate,
13
+ calculateRunway,
14
+ calculateCAC,
15
+ calculateLTV,
16
+ calculateLTVtoCAC,
17
+ calculatePaybackPeriod,
18
+ calculateARR,
19
+ calculateMRR,
20
+ calculateGrowthRate,
21
+ calculateCAGR,
22
+ calculateROI,
23
+ calculateROE,
24
+ calculateROA,
25
+ calculateQuickRatio,
26
+ calculateCurrentRatio,
27
+ calculateDebtToEquity,
28
+ formatCurrency,
29
+ createStatement,
30
+ getLineItem,
31
+ compareMetrics,
32
+ validateFinancials,
33
+ } from '../src/financials.js'
34
+ import type { FinancialMetrics, FinancialStatement } from '../src/types.js'
35
+
36
+ describe('Financials', () => {
37
+ describe('financials()', () => {
38
+ it('should calculate financial metrics from basic inputs', () => {
39
+ const metrics = financials({
40
+ revenue: 1000000,
41
+ cogs: 300000,
42
+ operatingExpenses: 500000,
43
+ })
44
+
45
+ expect(metrics.revenue).toBe(1000000)
46
+ expect(metrics.cogs).toBe(300000)
47
+ expect(metrics.grossProfit).toBe(700000)
48
+ expect(metrics.grossMargin).toBe(70)
49
+ expect(metrics.operatingIncome).toBe(200000)
50
+ expect(metrics.operatingMargin).toBe(20)
51
+ expect(metrics.netIncome).toBe(200000)
52
+ expect(metrics.netMargin).toBe(20)
53
+ })
54
+
55
+ it('should use defaults for missing inputs', () => {
56
+ const metrics = financials({})
57
+
58
+ expect(metrics.revenue).toBe(0)
59
+ expect(metrics.cogs).toBe(0)
60
+ expect(metrics.operatingExpenses).toBe(0)
61
+ expect(metrics.currency).toBe('USD')
62
+ expect(metrics.period).toBe('monthly')
63
+ })
64
+
65
+ it('should preserve provided values', () => {
66
+ const metrics = financials({
67
+ revenue: 1000000,
68
+ cogs: 300000,
69
+ operatingExpenses: 500000,
70
+ netIncome: 150000,
71
+ ebitda: 250000,
72
+ currency: 'EUR',
73
+ period: 'quarterly',
74
+ })
75
+
76
+ expect(metrics.netIncome).toBe(150000)
77
+ expect(metrics.ebitda).toBe(250000)
78
+ expect(metrics.currency).toBe('EUR')
79
+ expect(metrics.period).toBe('quarterly')
80
+ })
81
+
82
+ it('should handle zero revenue', () => {
83
+ const metrics = financials({
84
+ revenue: 0,
85
+ cogs: 0,
86
+ operatingExpenses: 0,
87
+ })
88
+
89
+ expect(metrics.grossMargin).toBe(0)
90
+ expect(metrics.operatingMargin).toBe(0)
91
+ expect(metrics.netMargin).toBe(0)
92
+ })
93
+ })
94
+
95
+ describe('calculateGrossMargin()', () => {
96
+ it('should calculate gross margin percentage', () => {
97
+ expect(calculateGrossMargin(100, 30)).toBe(70)
98
+ })
99
+
100
+ it('should return 0 for zero revenue', () => {
101
+ expect(calculateGrossMargin(0, 0)).toBe(0)
102
+ })
103
+
104
+ it('should handle 100% margin', () => {
105
+ expect(calculateGrossMargin(100, 0)).toBe(100)
106
+ })
107
+
108
+ it('should handle negative margin', () => {
109
+ expect(calculateGrossMargin(100, 150)).toBe(-50)
110
+ })
111
+ })
112
+
113
+ describe('calculateOperatingMargin()', () => {
114
+ it('should calculate operating margin', () => {
115
+ expect(calculateOperatingMargin(100, 30, 20)).toBe(50)
116
+ })
117
+
118
+ it('should return 0 for zero revenue', () => {
119
+ expect(calculateOperatingMargin(0, 0, 0)).toBe(0)
120
+ })
121
+
122
+ it('should handle negative operating income', () => {
123
+ expect(calculateOperatingMargin(100, 50, 70)).toBe(-20)
124
+ })
125
+ })
126
+
127
+ describe('calculateNetMargin()', () => {
128
+ it('should calculate net margin', () => {
129
+ expect(calculateNetMargin(100, 25)).toBe(25)
130
+ })
131
+
132
+ it('should return 0 for zero revenue', () => {
133
+ expect(calculateNetMargin(0, 0)).toBe(0)
134
+ })
135
+
136
+ it('should handle negative net income', () => {
137
+ expect(calculateNetMargin(100, -10)).toBe(-10)
138
+ })
139
+ })
140
+
141
+ describe('calculateEBITDAMargin()', () => {
142
+ it('should calculate EBITDA margin', () => {
143
+ expect(calculateEBITDAMargin(100, 30)).toBe(30)
144
+ })
145
+
146
+ it('should return 0 for zero revenue', () => {
147
+ expect(calculateEBITDAMargin(0, 0)).toBe(0)
148
+ })
149
+ })
150
+
151
+ describe('calculateBurnRate()', () => {
152
+ it('should calculate monthly burn rate', () => {
153
+ expect(calculateBurnRate(100000, 70000, 3)).toBe(10000)
154
+ })
155
+
156
+ it('should return 0 for zero months', () => {
157
+ expect(calculateBurnRate(100000, 70000, 0)).toBe(0)
158
+ })
159
+
160
+ it('should handle positive cash flow (negative burn)', () => {
161
+ expect(calculateBurnRate(100000, 130000, 3)).toBe(-10000)
162
+ })
163
+ })
164
+
165
+ describe('calculateRunway()', () => {
166
+ it('should calculate runway in months', () => {
167
+ expect(calculateRunway(100000, 10000)).toBe(10)
168
+ })
169
+
170
+ it('should return Infinity for zero burn rate', () => {
171
+ expect(calculateRunway(100000, 0)).toBe(Infinity)
172
+ })
173
+
174
+ it('should return Infinity for negative burn rate (profitable)', () => {
175
+ expect(calculateRunway(100000, -10000)).toBe(Infinity)
176
+ })
177
+ })
178
+
179
+ describe('calculateCAC()', () => {
180
+ it('should calculate customer acquisition cost', () => {
181
+ expect(calculateCAC(10000, 100)).toBe(100)
182
+ })
183
+
184
+ it('should return 0 for zero customers', () => {
185
+ expect(calculateCAC(10000, 0)).toBe(0)
186
+ })
187
+
188
+ it('should handle zero spend', () => {
189
+ expect(calculateCAC(0, 100)).toBe(0)
190
+ })
191
+ })
192
+
193
+ describe('calculateLTV()', () => {
194
+ it('should calculate lifetime value', () => {
195
+ // LTV = ARPU * Lifetime * (Gross Margin / 100)
196
+ // 100 * 24 * 0.7 = 1680
197
+ expect(calculateLTV(100, 24, 70)).toBe(1680)
198
+ })
199
+
200
+ it('should handle zero values', () => {
201
+ expect(calculateLTV(0, 24, 70)).toBe(0)
202
+ expect(calculateLTV(100, 0, 70)).toBe(0)
203
+ expect(calculateLTV(100, 24, 0)).toBe(0)
204
+ })
205
+ })
206
+
207
+ describe('calculateLTVtoCAC()', () => {
208
+ it('should calculate LTV:CAC ratio', () => {
209
+ expect(calculateLTVtoCAC(300, 100)).toBe(3)
210
+ })
211
+
212
+ it('should return 0 for zero CAC', () => {
213
+ expect(calculateLTVtoCAC(300, 0)).toBe(0)
214
+ })
215
+ })
216
+
217
+ describe('calculatePaybackPeriod()', () => {
218
+ it('should calculate payback period in months', () => {
219
+ expect(calculatePaybackPeriod(1200, 100)).toBe(12)
220
+ })
221
+
222
+ it('should return 0 for zero monthly revenue', () => {
223
+ expect(calculatePaybackPeriod(1200, 0)).toBe(0)
224
+ })
225
+ })
226
+
227
+ describe('calculateARR()', () => {
228
+ it('should calculate ARR from MRR', () => {
229
+ expect(calculateARR(10000)).toBe(120000)
230
+ })
231
+
232
+ it('should handle zero MRR', () => {
233
+ expect(calculateARR(0)).toBe(0)
234
+ })
235
+ })
236
+
237
+ describe('calculateMRR()', () => {
238
+ it('should calculate MRR from ARR', () => {
239
+ expect(calculateMRR(120000)).toBe(10000)
240
+ })
241
+
242
+ it('should handle zero ARR', () => {
243
+ expect(calculateMRR(0)).toBe(0)
244
+ })
245
+ })
246
+
247
+ describe('calculateGrowthRate()', () => {
248
+ it('should calculate growth rate', () => {
249
+ expect(calculateGrowthRate(120, 100)).toBe(20)
250
+ })
251
+
252
+ it('should calculate negative growth', () => {
253
+ expect(calculateGrowthRate(80, 100)).toBe(-20)
254
+ })
255
+
256
+ it('should return 0 for zero previous revenue', () => {
257
+ expect(calculateGrowthRate(100, 0)).toBe(0)
258
+ })
259
+
260
+ it('should handle same values (no growth)', () => {
261
+ expect(calculateGrowthRate(100, 100)).toBe(0)
262
+ })
263
+
264
+ it('should handle doubling', () => {
265
+ expect(calculateGrowthRate(200, 100)).toBe(100)
266
+ })
267
+ })
268
+
269
+ describe('calculateCAGR()', () => {
270
+ it('should calculate compound annual growth rate', () => {
271
+ // $100 to $200 over 5 years
272
+ const cagr = calculateCAGR(100, 200, 5)
273
+ expect(cagr).toBeCloseTo(14.87, 1)
274
+ })
275
+
276
+ it('should return 0 for zero beginning value', () => {
277
+ expect(calculateCAGR(0, 200, 5)).toBe(0)
278
+ })
279
+
280
+ it('should return 0 for zero years', () => {
281
+ expect(calculateCAGR(100, 200, 0)).toBe(0)
282
+ })
283
+
284
+ it('should handle negative growth', () => {
285
+ const cagr = calculateCAGR(200, 100, 5)
286
+ expect(cagr).toBeLessThan(0)
287
+ })
288
+ })
289
+
290
+ describe('calculateROI()', () => {
291
+ it('should calculate ROI', () => {
292
+ expect(calculateROI(150, 100)).toBe(50)
293
+ })
294
+
295
+ it('should calculate negative ROI', () => {
296
+ expect(calculateROI(80, 100)).toBe(-20)
297
+ })
298
+
299
+ it('should return 0 for zero cost', () => {
300
+ expect(calculateROI(150, 0)).toBe(0)
301
+ })
302
+
303
+ it('should handle 100% ROI', () => {
304
+ expect(calculateROI(200, 100)).toBe(100)
305
+ })
306
+ })
307
+
308
+ describe('calculateROE()', () => {
309
+ it('should calculate return on equity', () => {
310
+ expect(calculateROE(50000, 250000)).toBe(20)
311
+ })
312
+
313
+ it('should return 0 for zero equity', () => {
314
+ expect(calculateROE(50000, 0)).toBe(0)
315
+ })
316
+ })
317
+
318
+ describe('calculateROA()', () => {
319
+ it('should calculate return on assets', () => {
320
+ expect(calculateROA(50000, 500000)).toBe(10)
321
+ })
322
+
323
+ it('should return 0 for zero assets', () => {
324
+ expect(calculateROA(50000, 0)).toBe(0)
325
+ })
326
+ })
327
+
328
+ describe('calculateQuickRatio()', () => {
329
+ it('should calculate quick ratio', () => {
330
+ // (100000 - 20000) / 40000 = 2
331
+ expect(calculateQuickRatio(100000, 20000, 40000)).toBe(2)
332
+ })
333
+
334
+ it('should return 0 for zero liabilities', () => {
335
+ expect(calculateQuickRatio(100000, 20000, 0)).toBe(0)
336
+ })
337
+ })
338
+
339
+ describe('calculateCurrentRatio()', () => {
340
+ it('should calculate current ratio', () => {
341
+ expect(calculateCurrentRatio(100000, 50000)).toBe(2)
342
+ })
343
+
344
+ it('should return 0 for zero liabilities', () => {
345
+ expect(calculateCurrentRatio(100000, 0)).toBe(0)
346
+ })
347
+ })
348
+
349
+ describe('calculateDebtToEquity()', () => {
350
+ it('should calculate debt-to-equity ratio', () => {
351
+ expect(calculateDebtToEquity(100000, 200000)).toBe(0.5)
352
+ })
353
+
354
+ it('should return 0 for zero equity', () => {
355
+ expect(calculateDebtToEquity(100000, 0)).toBe(0)
356
+ })
357
+ })
358
+
359
+ describe('formatCurrency()', () => {
360
+ it('should format USD currency', () => {
361
+ const formatted = formatCurrency(1234.56)
362
+ expect(formatted).toContain('$')
363
+ expect(formatted).toContain('1,234.56')
364
+ })
365
+
366
+ it('should format EUR currency', () => {
367
+ const formatted = formatCurrency(1234.56, 'EUR')
368
+ expect(formatted).toContain('1,234.56')
369
+ })
370
+
371
+ it('should format GBP currency', () => {
372
+ const formatted = formatCurrency(1234.56, 'GBP')
373
+ expect(formatted).toContain('1,234.56')
374
+ })
375
+
376
+ it('should handle negative amounts', () => {
377
+ const formatted = formatCurrency(-1234.56)
378
+ expect(formatted).toContain('-')
379
+ })
380
+
381
+ it('should handle zero amount', () => {
382
+ const formatted = formatCurrency(0)
383
+ expect(formatted).toContain('$')
384
+ expect(formatted).toContain('0')
385
+ })
386
+
387
+ it('should handle large numbers', () => {
388
+ const formatted = formatCurrency(1000000000)
389
+ expect(formatted).toContain('1,000,000,000')
390
+ })
391
+ })
392
+
393
+ describe('createStatement()', () => {
394
+ it('should create a financial statement', () => {
395
+ const statement = createStatement('income', 'Q4 2024', {
396
+ revenue: 1000000,
397
+ cogs: 300000,
398
+ operatingExpenses: 500000,
399
+ })
400
+
401
+ expect(statement.type).toBe('income')
402
+ expect(statement.period).toBe('Q4 2024')
403
+ expect(statement.lineItems.revenue).toBe(1000000)
404
+ expect(statement.currency).toBe('USD')
405
+ })
406
+
407
+ it('should use specified currency', () => {
408
+ const statement = createStatement('balance', 'Q4 2024', {}, 'EUR')
409
+ expect(statement.currency).toBe('EUR')
410
+ })
411
+ })
412
+
413
+ describe('getLineItem()', () => {
414
+ const statement: FinancialStatement = {
415
+ type: 'income',
416
+ period: 'Q4 2024',
417
+ lineItems: {
418
+ revenue: 1000000,
419
+ cogs: 300000,
420
+ },
421
+ currency: 'USD',
422
+ }
423
+
424
+ it('should get existing line item', () => {
425
+ expect(getLineItem(statement, 'revenue')).toBe(1000000)
426
+ })
427
+
428
+ it('should return 0 for non-existent line item', () => {
429
+ expect(getLineItem(statement, 'expenses')).toBe(0)
430
+ })
431
+ })
432
+
433
+ describe('compareMetrics()', () => {
434
+ it('should compare financial metrics between periods', () => {
435
+ const current = financials({
436
+ revenue: 1200000,
437
+ cogs: 360000,
438
+ operatingExpenses: 600000,
439
+ })
440
+
441
+ const previous = financials({
442
+ revenue: 1000000,
443
+ cogs: 300000,
444
+ operatingExpenses: 500000,
445
+ })
446
+
447
+ const comparison = compareMetrics(current, previous)
448
+
449
+ expect(comparison.revenue?.change).toBe(200000)
450
+ expect(comparison.revenue?.changePercent).toBe(20)
451
+ })
452
+
453
+ it('should handle zero previous values', () => {
454
+ const current = financials({ revenue: 100000 })
455
+ const previous = financials({ revenue: 0 })
456
+
457
+ const comparison = compareMetrics(current, previous)
458
+ expect(comparison.revenue?.changePercent).toBe(0)
459
+ })
460
+ })
461
+
462
+ describe('validateFinancials()', () => {
463
+ it('should validate valid financials', () => {
464
+ const metrics = financials({
465
+ revenue: 1000000,
466
+ cogs: 300000,
467
+ operatingExpenses: 500000,
468
+ })
469
+
470
+ const result = validateFinancials(metrics)
471
+ expect(result.valid).toBe(true)
472
+ expect(result.errors).toHaveLength(0)
473
+ })
474
+
475
+ it('should fail for negative revenue', () => {
476
+ const metrics: FinancialMetrics = {
477
+ revenue: -100000,
478
+ currency: 'USD',
479
+ period: 'monthly',
480
+ }
481
+
482
+ const result = validateFinancials(metrics)
483
+ expect(result.valid).toBe(false)
484
+ expect(result.errors).toContain('Revenue cannot be negative')
485
+ })
486
+
487
+ it('should fail for negative COGS', () => {
488
+ const metrics: FinancialMetrics = {
489
+ revenue: 100000,
490
+ cogs: -30000,
491
+ currency: 'USD',
492
+ period: 'monthly',
493
+ }
494
+
495
+ const result = validateFinancials(metrics)
496
+ expect(result.valid).toBe(false)
497
+ expect(result.errors).toContain('COGS cannot be negative')
498
+ })
499
+
500
+ it('should fail for negative operating expenses', () => {
501
+ const metrics: FinancialMetrics = {
502
+ revenue: 100000,
503
+ operatingExpenses: -50000,
504
+ currency: 'USD',
505
+ period: 'monthly',
506
+ }
507
+
508
+ const result = validateFinancials(metrics)
509
+ expect(result.valid).toBe(false)
510
+ expect(result.errors).toContain('Operating expenses cannot be negative')
511
+ })
512
+
513
+ it('should fail if COGS exceeds revenue', () => {
514
+ const metrics: FinancialMetrics = {
515
+ revenue: 100000,
516
+ cogs: 150000,
517
+ currency: 'USD',
518
+ period: 'monthly',
519
+ }
520
+
521
+ const result = validateFinancials(metrics)
522
+ expect(result.valid).toBe(false)
523
+ expect(result.errors).toContain('COGS cannot exceed revenue')
524
+ })
525
+
526
+ it('should allow zero values', () => {
527
+ const metrics: FinancialMetrics = {
528
+ revenue: 0,
529
+ cogs: 0,
530
+ operatingExpenses: 0,
531
+ currency: 'USD',
532
+ period: 'monthly',
533
+ }
534
+
535
+ const result = validateFinancials(metrics)
536
+ expect(result.valid).toBe(true)
537
+ })
538
+ })
539
+ })