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,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
+ })