@dizzlkheinz/ynab-mcpb 0.18.3 → 0.19.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 (346) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/CLAUDE.md +87 -8
  3. package/bin/ynab-mcp-server.cjs +2 -2
  4. package/bin/ynab-mcp-server.js +3 -3
  5. package/biome.json +39 -0
  6. package/dist/bundle/index.cjs +67 -67
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +27 -27
  9. package/dist/server/YNABMCPServer.d.ts +3 -4
  10. package/dist/server/YNABMCPServer.js +111 -116
  11. package/dist/server/budgetResolver.d.ts +6 -5
  12. package/dist/server/budgetResolver.js +46 -36
  13. package/dist/server/cacheKeys.js +6 -6
  14. package/dist/server/cacheManager.js +14 -11
  15. package/dist/server/completions.d.ts +2 -2
  16. package/dist/server/completions.js +20 -15
  17. package/dist/server/config.d.ts +10 -5
  18. package/dist/server/config.js +24 -7
  19. package/dist/server/deltaCache.d.ts +2 -2
  20. package/dist/server/deltaCache.js +22 -16
  21. package/dist/server/deltaCache.merge.d.ts +2 -2
  22. package/dist/server/diagnostics.d.ts +4 -4
  23. package/dist/server/diagnostics.js +38 -32
  24. package/dist/server/errorHandler.d.ts +5 -12
  25. package/dist/server/errorHandler.js +219 -217
  26. package/dist/server/prompts.d.ts +2 -2
  27. package/dist/server/prompts.js +45 -45
  28. package/dist/server/rateLimiter.js +4 -4
  29. package/dist/server/requestLogger.d.ts +1 -1
  30. package/dist/server/requestLogger.js +40 -35
  31. package/dist/server/resources.d.ts +3 -3
  32. package/dist/server/resources.js +55 -52
  33. package/dist/server/responseFormatter.js +6 -6
  34. package/dist/server/securityMiddleware.d.ts +2 -2
  35. package/dist/server/securityMiddleware.js +22 -20
  36. package/dist/server/serverKnowledgeStore.js +1 -1
  37. package/dist/server/toolRegistry.d.ts +3 -3
  38. package/dist/server/toolRegistry.js +47 -40
  39. package/dist/tools/__tests__/deltaTestUtils.d.ts +3 -3
  40. package/dist/tools/__tests__/deltaTestUtils.js +2 -2
  41. package/dist/tools/accountTools.d.ts +9 -8
  42. package/dist/tools/accountTools.js +47 -47
  43. package/dist/tools/adapters.d.ts +13 -8
  44. package/dist/tools/adapters.js +21 -11
  45. package/dist/tools/budgetTools.d.ts +8 -7
  46. package/dist/tools/budgetTools.js +22 -22
  47. package/dist/tools/categoryTools.d.ts +9 -8
  48. package/dist/tools/categoryTools.js +68 -59
  49. package/dist/tools/compareTransactions/formatter.d.ts +3 -3
  50. package/dist/tools/compareTransactions/formatter.js +9 -9
  51. package/dist/tools/compareTransactions/index.d.ts +6 -6
  52. package/dist/tools/compareTransactions/index.js +58 -43
  53. package/dist/tools/compareTransactions/matcher.d.ts +1 -1
  54. package/dist/tools/compareTransactions/matcher.js +28 -15
  55. package/dist/tools/compareTransactions/parser.d.ts +2 -2
  56. package/dist/tools/compareTransactions/parser.js +144 -138
  57. package/dist/tools/compareTransactions/types.d.ts +4 -4
  58. package/dist/tools/compareTransactions.d.ts +1 -1
  59. package/dist/tools/compareTransactions.js +1 -1
  60. package/dist/tools/deltaFetcher.d.ts +2 -2
  61. package/dist/tools/deltaFetcher.js +16 -15
  62. package/dist/tools/deltaSupport.d.ts +4 -4
  63. package/dist/tools/deltaSupport.js +35 -41
  64. package/dist/tools/exportTransactions.d.ts +5 -4
  65. package/dist/tools/exportTransactions.js +61 -59
  66. package/dist/tools/monthTools.d.ts +7 -6
  67. package/dist/tools/monthTools.js +31 -29
  68. package/dist/tools/payeeTools.d.ts +7 -6
  69. package/dist/tools/payeeTools.js +28 -28
  70. package/dist/tools/reconcileAdapter.d.ts +2 -2
  71. package/dist/tools/reconcileAdapter.js +21 -11
  72. package/dist/tools/reconciliation/analyzer.d.ts +4 -4
  73. package/dist/tools/reconciliation/analyzer.js +136 -57
  74. package/dist/tools/reconciliation/csvParser.d.ts +3 -3
  75. package/dist/tools/reconciliation/csvParser.js +128 -104
  76. package/dist/tools/reconciliation/executor.d.ts +4 -4
  77. package/dist/tools/reconciliation/executor.js +148 -109
  78. package/dist/tools/reconciliation/index.d.ts +10 -10
  79. package/dist/tools/reconciliation/index.js +96 -83
  80. package/dist/tools/reconciliation/matcher.d.ts +3 -3
  81. package/dist/tools/reconciliation/matcher.js +17 -16
  82. package/dist/tools/reconciliation/payeeNormalizer.js +19 -8
  83. package/dist/tools/reconciliation/recommendationEngine.d.ts +1 -1
  84. package/dist/tools/reconciliation/recommendationEngine.js +40 -40
  85. package/dist/tools/reconciliation/reportFormatter.d.ts +2 -2
  86. package/dist/tools/reconciliation/reportFormatter.js +79 -54
  87. package/dist/tools/reconciliation/signDetector.d.ts +1 -1
  88. package/dist/tools/reconciliation/types.d.ts +19 -16
  89. package/dist/tools/reconciliation/ynabAdapter.d.ts +2 -2
  90. package/dist/tools/schemas/common.d.ts +1 -1
  91. package/dist/tools/schemas/common.js +1 -1
  92. package/dist/tools/schemas/outputs/accountOutputs.d.ts +1 -1
  93. package/dist/tools/schemas/outputs/accountOutputs.js +24 -18
  94. package/dist/tools/schemas/outputs/budgetOutputs.d.ts +1 -1
  95. package/dist/tools/schemas/outputs/budgetOutputs.js +14 -11
  96. package/dist/tools/schemas/outputs/categoryOutputs.d.ts +1 -1
  97. package/dist/tools/schemas/outputs/categoryOutputs.js +49 -29
  98. package/dist/tools/schemas/outputs/comparisonOutputs.d.ts +1 -1
  99. package/dist/tools/schemas/outputs/comparisonOutputs.js +12 -12
  100. package/dist/tools/schemas/outputs/index.d.ts +14 -14
  101. package/dist/tools/schemas/outputs/index.js +14 -14
  102. package/dist/tools/schemas/outputs/monthOutputs.d.ts +1 -1
  103. package/dist/tools/schemas/outputs/monthOutputs.js +56 -41
  104. package/dist/tools/schemas/outputs/payeeOutputs.d.ts +1 -1
  105. package/dist/tools/schemas/outputs/payeeOutputs.js +10 -10
  106. package/dist/tools/schemas/outputs/reconciliationOutputs.d.ts +2 -2
  107. package/dist/tools/schemas/outputs/reconciliationOutputs.js +45 -45
  108. package/dist/tools/schemas/outputs/transactionMutationOutputs.d.ts +1 -1
  109. package/dist/tools/schemas/outputs/transactionMutationOutputs.js +28 -22
  110. package/dist/tools/schemas/outputs/transactionOutputs.d.ts +1 -1
  111. package/dist/tools/schemas/outputs/transactionOutputs.js +43 -35
  112. package/dist/tools/schemas/outputs/utilityOutputs.d.ts +1 -1
  113. package/dist/tools/schemas/outputs/utilityOutputs.js +5 -3
  114. package/dist/tools/schemas/shared/commonOutputs.d.ts +1 -1
  115. package/dist/tools/schemas/shared/commonOutputs.js +15 -9
  116. package/dist/tools/transactionReadTools.d.ts +11 -0
  117. package/dist/tools/transactionReadTools.js +202 -0
  118. package/dist/tools/transactionSchemas.d.ts +309 -0
  119. package/dist/tools/transactionSchemas.js +235 -0
  120. package/dist/tools/transactionTools.d.ts +6 -302
  121. package/dist/tools/transactionTools.js +7 -2054
  122. package/dist/tools/transactionUtils.d.ts +31 -0
  123. package/dist/tools/transactionUtils.js +364 -0
  124. package/dist/tools/transactionWriteTools.d.ts +20 -0
  125. package/dist/tools/transactionWriteTools.js +1342 -0
  126. package/dist/tools/utilityTools.d.ts +5 -4
  127. package/dist/tools/utilityTools.js +11 -11
  128. package/dist/types/index.d.ts +7 -7
  129. package/dist/types/index.js +6 -6
  130. package/dist/types/reconciliation.d.ts +1 -1
  131. package/dist/types/toolRegistration.d.ts +14 -12
  132. package/dist/utils/amountUtils.js +1 -1
  133. package/dist/utils/dateUtils.js +4 -4
  134. package/dist/utils/errors.d.ts +3 -3
  135. package/dist/utils/errors.js +4 -4
  136. package/dist/utils/money.d.ts +2 -2
  137. package/dist/utils/money.js +8 -8
  138. package/dist/utils/validationError.d.ts +1 -1
  139. package/dist/utils/validationError.js +1 -1
  140. package/docs/assets/examples/reconciliation-with-recommendations.json +66 -66
  141. package/docs/assets/schemas/reconciliation-v2.json +360 -336
  142. package/docs/plans/2025-12-25-transaction-tools-refactor-design.md +211 -0
  143. package/docs/plans/2025-12-25-transaction-tools-refactor.md +905 -0
  144. package/esbuild.config.mjs +53 -50
  145. package/meta.json +12548 -12548
  146. package/package.json +98 -109
  147. package/scripts/analyze-bundle.mjs +33 -30
  148. package/scripts/create-pr-description.js +169 -120
  149. package/scripts/run-all-tests.js +205 -0
  150. package/scripts/run-domain-integration-tests.js +28 -18
  151. package/scripts/run-generate-mcpb.js +19 -17
  152. package/scripts/run-throttled-integration-tests.js +92 -83
  153. package/scripts/test-delta-params.mjs +149 -120
  154. package/scripts/test-recommendations.ts +36 -32
  155. package/scripts/tmpTransaction.ts +80 -43
  156. package/scripts/validate-env.js +98 -91
  157. package/scripts/verify-build.js +78 -76
  158. package/src/__tests__/comprehensive.integration.test.ts +1281 -1154
  159. package/src/__tests__/performance.test.ts +723 -671
  160. package/src/__tests__/setup.ts +442 -395
  161. package/src/__tests__/smoke.e2e.test.ts +41 -39
  162. package/src/__tests__/testRunner.ts +314 -295
  163. package/src/__tests__/testUtils.ts +456 -364
  164. package/src/__tests__/tools/reconciliation/csvParser.integration.test.ts +109 -107
  165. package/src/__tests__/tools/reconciliation/real-world.integration.test.ts +41 -41
  166. package/src/index.ts +68 -59
  167. package/src/server/CLAUDE.md +480 -0
  168. package/src/server/YNABMCPServer.ts +821 -794
  169. package/src/server/__tests__/YNABMCPServer.integration.test.ts +929 -893
  170. package/src/server/__tests__/YNABMCPServer.test.ts +903 -899
  171. package/src/server/__tests__/budgetResolver.test.ts +466 -423
  172. package/src/server/__tests__/cacheManager.test.ts +891 -874
  173. package/src/server/__tests__/completions.integration.test.ts +115 -106
  174. package/src/server/__tests__/completions.test.ts +334 -313
  175. package/src/server/__tests__/config.test.ts +98 -86
  176. package/src/server/__tests__/deltaCache.merge.test.ts +774 -703
  177. package/src/server/__tests__/deltaCache.swr.test.ts +198 -153
  178. package/src/server/__tests__/deltaCache.test.ts +946 -759
  179. package/src/server/__tests__/diagnostics.test.ts +825 -792
  180. package/src/server/__tests__/errorHandler.integration.test.ts +512 -462
  181. package/src/server/__tests__/errorHandler.test.ts +402 -397
  182. package/src/server/__tests__/prompts.test.ts +424 -347
  183. package/src/server/__tests__/rateLimiter.test.ts +313 -309
  184. package/src/server/__tests__/requestLogger.test.ts +443 -403
  185. package/src/server/__tests__/resources.template.test.ts +196 -185
  186. package/src/server/__tests__/resources.test.ts +294 -288
  187. package/src/server/__tests__/security.integration.test.ts +487 -421
  188. package/src/server/__tests__/securityMiddleware.test.ts +519 -444
  189. package/src/server/__tests__/server-startup.integration.test.ts +509 -490
  190. package/src/server/__tests__/serverKnowledgeStore.test.ts +174 -173
  191. package/src/server/__tests__/toolRegistration.test.ts +239 -210
  192. package/src/server/__tests__/toolRegistry.test.ts +907 -845
  193. package/src/server/budgetResolver.ts +221 -181
  194. package/src/server/cacheKeys.ts +6 -6
  195. package/src/server/cacheManager.ts +498 -484
  196. package/src/server/completions.ts +267 -243
  197. package/src/server/config.ts +35 -14
  198. package/src/server/deltaCache.merge.ts +146 -128
  199. package/src/server/deltaCache.ts +352 -309
  200. package/src/server/diagnostics.ts +257 -242
  201. package/src/server/errorHandler.ts +747 -744
  202. package/src/server/prompts.ts +181 -176
  203. package/src/server/rateLimiter.ts +131 -129
  204. package/src/server/requestLogger.ts +350 -322
  205. package/src/server/resources.ts +442 -374
  206. package/src/server/responseFormatter.ts +41 -37
  207. package/src/server/securityMiddleware.ts +223 -205
  208. package/src/server/serverKnowledgeStore.ts +67 -67
  209. package/src/server/toolRegistry.ts +508 -474
  210. package/src/tools/CLAUDE.md +604 -0
  211. package/src/tools/__tests__/accountTools.delta.integration.test.ts +128 -111
  212. package/src/tools/__tests__/accountTools.integration.test.ts +129 -111
  213. package/src/tools/__tests__/accountTools.test.ts +685 -638
  214. package/src/tools/__tests__/adapters.test.ts +142 -108
  215. package/src/tools/__tests__/budgetTools.delta.integration.test.ts +73 -73
  216. package/src/tools/__tests__/budgetTools.integration.test.ts +132 -124
  217. package/src/tools/__tests__/budgetTools.test.ts +442 -413
  218. package/src/tools/__tests__/categoryTools.delta.integration.test.ts +76 -68
  219. package/src/tools/__tests__/categoryTools.integration.test.ts +314 -288
  220. package/src/tools/__tests__/categoryTools.test.ts +656 -625
  221. package/src/tools/__tests__/compareTransactions/formatter.test.ts +535 -462
  222. package/src/tools/__tests__/compareTransactions/index.test.ts +378 -358
  223. package/src/tools/__tests__/compareTransactions/matcher.test.ts +497 -398
  224. package/src/tools/__tests__/compareTransactions/parser.test.ts +765 -747
  225. package/src/tools/__tests__/compareTransactions.test.ts +352 -332
  226. package/src/tools/__tests__/compareTransactions.window.test.ts +150 -146
  227. package/src/tools/__tests__/deltaFetcher.scheduled.integration.test.ts +69 -65
  228. package/src/tools/__tests__/deltaFetcher.test.ts +325 -265
  229. package/src/tools/__tests__/deltaSupport.test.ts +211 -184
  230. package/src/tools/__tests__/deltaTestUtils.ts +37 -33
  231. package/src/tools/__tests__/exportTransactions.test.ts +205 -200
  232. package/src/tools/__tests__/monthTools.delta.integration.test.ts +68 -68
  233. package/src/tools/__tests__/monthTools.integration.test.ts +178 -166
  234. package/src/tools/__tests__/monthTools.test.ts +561 -512
  235. package/src/tools/__tests__/payeeTools.delta.integration.test.ts +68 -68
  236. package/src/tools/__tests__/payeeTools.integration.test.ts +158 -142
  237. package/src/tools/__tests__/payeeTools.test.ts +486 -434
  238. package/src/tools/__tests__/transactionSchemas.test.ts +1204 -0
  239. package/src/tools/__tests__/transactionTools.integration.test.ts +875 -825
  240. package/src/tools/__tests__/transactionTools.test.ts +4923 -4366
  241. package/src/tools/__tests__/transactionUtils.test.ts +1016 -0
  242. package/src/tools/__tests__/utilityTools.integration.test.ts +32 -32
  243. package/src/tools/__tests__/utilityTools.test.ts +68 -58
  244. package/src/tools/accountTools.ts +293 -271
  245. package/src/tools/adapters.ts +120 -63
  246. package/src/tools/budgetTools.ts +121 -116
  247. package/src/tools/categoryTools.ts +379 -339
  248. package/src/tools/compareTransactions/formatter.ts +131 -119
  249. package/src/tools/compareTransactions/index.ts +249 -214
  250. package/src/tools/compareTransactions/matcher.ts +259 -209
  251. package/src/tools/compareTransactions/parser.ts +517 -487
  252. package/src/tools/compareTransactions/types.ts +38 -38
  253. package/src/tools/compareTransactions.ts +1 -1
  254. package/src/tools/deltaFetcher.ts +281 -260
  255. package/src/tools/deltaSupport.ts +264 -259
  256. package/src/tools/exportTransactions.ts +230 -218
  257. package/src/tools/monthTools.ts +180 -165
  258. package/src/tools/payeeTools.ts +152 -140
  259. package/src/tools/reconcileAdapter.ts +297 -246
  260. package/src/tools/reconciliation/CLAUDE.md +506 -0
  261. package/src/tools/reconciliation/__tests__/adapter.causes.test.ts +135 -112
  262. package/src/tools/reconciliation/__tests__/adapter.test.ts +249 -227
  263. package/src/tools/reconciliation/__tests__/analyzer.test.ts +408 -335
  264. package/src/tools/reconciliation/__tests__/csvParser.test.ts +71 -69
  265. package/src/tools/reconciliation/__tests__/executor.integration.test.ts +348 -323
  266. package/src/tools/reconciliation/__tests__/executor.progress.test.ts +503 -457
  267. package/src/tools/reconciliation/__tests__/executor.test.ts +898 -831
  268. package/src/tools/reconciliation/__tests__/matcher.test.ts +667 -663
  269. package/src/tools/reconciliation/__tests__/payeeNormalizer.test.ts +296 -276
  270. package/src/tools/reconciliation/__tests__/recommendationEngine.integration.test.ts +692 -624
  271. package/src/tools/reconciliation/__tests__/recommendationEngine.test.ts +1008 -986
  272. package/src/tools/reconciliation/__tests__/reconciliation.delta.integration.test.ts +187 -146
  273. package/src/tools/reconciliation/__tests__/reportFormatter.test.ts +583 -530
  274. package/src/tools/reconciliation/__tests__/scenarios/adapterCurrency.scenario.test.ts +75 -71
  275. package/src/tools/reconciliation/__tests__/scenarios/extremes.scenario.test.ts +70 -58
  276. package/src/tools/reconciliation/__tests__/scenarios/repeatAmount.scenario.test.ts +102 -88
  277. package/src/tools/reconciliation/__tests__/schemaUrl.test.ts +58 -43
  278. package/src/tools/reconciliation/__tests__/signDetector.test.ts +209 -206
  279. package/src/tools/reconciliation/__tests__/ynabAdapter.test.ts +66 -60
  280. package/src/tools/reconciliation/analyzer.ts +582 -406
  281. package/src/tools/reconciliation/csvParser.ts +656 -609
  282. package/src/tools/reconciliation/executor.ts +1290 -1128
  283. package/src/tools/reconciliation/index.ts +580 -528
  284. package/src/tools/reconciliation/matcher.ts +256 -240
  285. package/src/tools/reconciliation/payeeNormalizer.ts +92 -78
  286. package/src/tools/reconciliation/recommendationEngine.ts +357 -345
  287. package/src/tools/reconciliation/reportFormatter.ts +349 -276
  288. package/src/tools/reconciliation/signDetector.ts +89 -83
  289. package/src/tools/reconciliation/types.ts +164 -153
  290. package/src/tools/reconciliation/ynabAdapter.ts +17 -15
  291. package/src/tools/schemas/CLAUDE.md +546 -0
  292. package/src/tools/schemas/common.ts +1 -1
  293. package/src/tools/schemas/outputs/__tests__/accountOutputs.test.ts +410 -409
  294. package/src/tools/schemas/outputs/__tests__/budgetOutputs.test.ts +305 -299
  295. package/src/tools/schemas/outputs/__tests__/categoryOutputs.test.ts +431 -430
  296. package/src/tools/schemas/outputs/__tests__/comparisonOutputs.test.ts +510 -495
  297. package/src/tools/schemas/outputs/__tests__/dateValidation.test.ts +179 -153
  298. package/src/tools/schemas/outputs/__tests__/discrepancyDirection.test.ts +293 -254
  299. package/src/tools/schemas/outputs/__tests__/monthOutputs.test.ts +457 -457
  300. package/src/tools/schemas/outputs/__tests__/payeeOutputs.test.ts +362 -356
  301. package/src/tools/schemas/outputs/__tests__/reconciliationOutputs.test.ts +402 -399
  302. package/src/tools/schemas/outputs/__tests__/transactionMutationSchemas.test.ts +225 -211
  303. package/src/tools/schemas/outputs/__tests__/transactionOutputs.test.ts +457 -454
  304. package/src/tools/schemas/outputs/__tests__/utilityOutputs.test.ts +316 -315
  305. package/src/tools/schemas/outputs/accountOutputs.ts +40 -34
  306. package/src/tools/schemas/outputs/budgetOutputs.ts +24 -19
  307. package/src/tools/schemas/outputs/categoryOutputs.ts +76 -56
  308. package/src/tools/schemas/outputs/comparisonOutputs.ts +192 -169
  309. package/src/tools/schemas/outputs/index.ts +163 -163
  310. package/src/tools/schemas/outputs/monthOutputs.ts +95 -80
  311. package/src/tools/schemas/outputs/payeeOutputs.ts +18 -18
  312. package/src/tools/schemas/outputs/reconciliationOutputs.ts +386 -373
  313. package/src/tools/schemas/outputs/transactionMutationOutputs.ts +259 -231
  314. package/src/tools/schemas/outputs/transactionOutputs.ts +81 -71
  315. package/src/tools/schemas/outputs/utilityOutputs.ts +90 -84
  316. package/src/tools/schemas/shared/commonOutputs.ts +27 -19
  317. package/src/tools/toolCategories.ts +114 -114
  318. package/src/tools/transactionReadTools.ts +327 -0
  319. package/src/tools/transactionSchemas.ts +484 -0
  320. package/src/tools/transactionTools.ts +107 -2990
  321. package/src/tools/transactionUtils.ts +621 -0
  322. package/src/tools/transactionWriteTools.ts +2110 -0
  323. package/src/tools/utilityTools.ts +46 -41
  324. package/src/types/CLAUDE.md +477 -0
  325. package/src/types/__tests__/index.test.ts +51 -51
  326. package/src/types/index.ts +43 -39
  327. package/src/types/integration-tests.d.ts +26 -26
  328. package/src/types/reconciliation.ts +29 -29
  329. package/src/types/toolAnnotations.ts +30 -30
  330. package/src/types/toolRegistration.ts +43 -32
  331. package/src/utils/CLAUDE.md +508 -0
  332. package/src/utils/__tests__/dateUtils.test.ts +174 -168
  333. package/src/utils/__tests__/money.test.ts +193 -187
  334. package/src/utils/amountUtils.ts +5 -5
  335. package/src/utils/baseError.ts +5 -5
  336. package/src/utils/dateUtils.ts +29 -26
  337. package/src/utils/errors.ts +14 -14
  338. package/src/utils/money.ts +66 -52
  339. package/src/utils/validationError.ts +1 -1
  340. package/tsconfig.json +29 -29
  341. package/tsconfig.prod.json +16 -16
  342. package/vitest-reporters/split-json-reporter.ts +247 -204
  343. package/vitest.config.ts +99 -95
  344. package/.prettierignore +0 -10
  345. package/.prettierrc.json +0 -10
  346. package/eslint.config.js +0 -49
@@ -2,13 +2,16 @@
2
2
  * Performance and load tests for YNAB MCP Server
3
3
  */
4
4
 
5
- import { describe, it, expect, beforeEach, vi } from 'vitest';
6
- import { executeToolCall, parseToolResult } from './testUtils.js';
7
- import { executeReconciliation, type AccountSnapshot } from '../tools/reconciliation/executor.js';
8
- import type { ReconciliationAnalysis } from '../tools/reconciliation/types.js';
9
- import type { ReconcileAccountRequest } from '../tools/reconciliation/index.js';
10
- import type * as ynab from 'ynab';
11
- import { SecurityErrorCode } from '../server/errorHandler.js';
5
+ import { beforeEach, describe, expect, it, vi } from "vitest";
6
+ import type * as ynab from "ynab";
7
+ import { SecurityErrorCode } from "../server/errorHandler.js";
8
+ import {
9
+ type AccountSnapshot,
10
+ executeReconciliation,
11
+ } from "../tools/reconciliation/executor.js";
12
+ import type { ReconcileAccountRequest } from "../tools/reconciliation/index.js";
13
+ import type { ReconciliationAnalysis } from "../tools/reconciliation/types.js";
14
+ import { executeToolCall, parseToolResult } from "./testUtils.js";
12
15
 
13
16
  /**
14
17
  * Helper function to validate tool responses and extract array data
@@ -19,705 +22,754 @@ import { SecurityErrorCode } from '../server/errorHandler.js';
19
22
  * @returns The validated array data
20
23
  * @throws Error if response contains errors or invalid data
21
24
  */
22
- function validateToolResponse<T>(result: any, fieldSelector: (data: any) => T[] | undefined): T[] {
23
- const parsed = parseToolResult(result);
24
-
25
- // Check for errors in the response
26
- const hasError = parsed.error || parsed.data?.error;
27
- if (hasError) {
28
- throw new Error(
29
- `Tool returned error: ${JSON.stringify(hasError, null, 2)}
25
+ function validateToolResponse<T>(
26
+ result: any,
27
+ fieldSelector: (data: any) => T[] | undefined,
28
+ ): T[] {
29
+ const parsed = parseToolResult(result);
30
+
31
+ // Check for errors in the response
32
+ const hasError = parsed.error || parsed.data?.error;
33
+ if (hasError) {
34
+ throw new Error(
35
+ `Tool returned error: ${JSON.stringify(hasError, null, 2)}
30
36
  Full response: ${JSON.stringify(parsed, null, 2)}`,
31
- );
32
- }
37
+ );
38
+ }
33
39
 
34
- // Ensure data exists
35
- if (!parsed.data) {
36
- throw new Error(`Tool returned no data. Full response: ${JSON.stringify(parsed, null, 2)}`);
37
- }
40
+ // Ensure data exists
41
+ if (!parsed.data) {
42
+ throw new Error(
43
+ `Tool returned no data. Full response: ${JSON.stringify(parsed, null, 2)}`,
44
+ );
45
+ }
38
46
 
39
- // Select the specific array field
40
- const arrayData = fieldSelector(parsed.data);
47
+ // Select the specific array field
48
+ const arrayData = fieldSelector(parsed.data);
41
49
 
42
- // Validate it's a non-empty array
43
- expect(arrayData).toBeDefined();
44
- expect(Array.isArray(arrayData)).toBe(true);
45
- expect(arrayData!.length).toBeGreaterThan(0);
50
+ // Validate it's a non-empty array
51
+ expect(arrayData).toBeDefined();
52
+ expect(Array.isArray(arrayData)).toBe(true);
53
+ expect(arrayData?.length).toBeGreaterThan(0);
46
54
 
47
- return arrayData!;
55
+ return arrayData!;
48
56
  }
49
57
 
50
58
  // Mock the YNAB SDK for performance tests
51
- vi.mock('ynab', () => {
52
- const mockAPI = {
53
- budgets: {
54
- getBudgets: vi.fn(),
55
- getBudgetById: vi.fn(),
56
- },
57
- accounts: {
58
- getAccounts: vi.fn(),
59
- getAccountById: vi.fn(),
60
- },
61
- transactions: {
62
- getTransactions: vi.fn(),
63
- getTransactionById: vi.fn(),
64
- createTransaction: vi.fn(),
65
- },
66
- categories: {
67
- getCategories: vi.fn(),
68
- },
69
- user: {
70
- getUser: vi.fn(),
71
- },
72
- };
73
-
74
- return {
75
- API: vi.fn(() => mockAPI),
76
- };
59
+ vi.mock("ynab", () => {
60
+ const mockAPI = {
61
+ budgets: {
62
+ getBudgets: vi.fn(),
63
+ getBudgetById: vi.fn(),
64
+ },
65
+ accounts: {
66
+ getAccounts: vi.fn(),
67
+ getAccountById: vi.fn(),
68
+ },
69
+ transactions: {
70
+ getTransactions: vi.fn(),
71
+ getTransactionById: vi.fn(),
72
+ createTransaction: vi.fn(),
73
+ },
74
+ categories: {
75
+ getCategories: vi.fn(),
76
+ },
77
+ user: {
78
+ getUser: vi.fn(),
79
+ },
80
+ };
81
+
82
+ return {
83
+ API: vi.fn(() => mockAPI),
84
+ };
77
85
  });
78
86
 
79
87
  // NOTE: These performance tests need updated mocking for the reconciliation executor
80
88
  // Skipping temporarily - reconciliation functionality is covered by integration tests
81
- describe.skip('Reconciliation Performance - Bulk vs Sequential', () => {
82
- it('processes 20 transactions in bulk mode in under 8 seconds', async () => {
83
- const { duration, result } = await measurePerformanceScenario({
84
- transactionCount: 20,
85
- bulkDelay: 50,
86
- });
87
- console.log(`Bulk benchmark (20 txns): ${duration}ms`);
88
- expect(duration).toBeLessThan(8000);
89
- expect(result.summary.transactions_created).toBe(20);
90
- expect(result.bulk_operation_details?.bulk_successes).toBe(1);
91
- }, 60000);
92
-
93
- it('pure sequential mode (single transaction) takes longer than 20 seconds', async () => {
94
- // Pure sequential baseline: only 1 transaction per "unmatched_bank" to avoid bulk mode
95
- const { duration, result } = await measurePerformanceScenario({
96
- transactionCount: 1, // This ensures bulk mode is never entered
97
- bulkDelay: 50,
98
- sequentialDelay: 1050,
99
- multipleRuns: 20, // Run 20 times to simulate 20 sequential transactions
100
- });
101
- console.log(`Pure sequential baseline (20 txns, 1 at a time): ${duration}ms`);
102
- expect(duration).toBeGreaterThan(20000);
103
- expect(result.summary.transactions_created).toBe(1);
104
- expect(result.bulk_operation_details).toBeUndefined(); // No bulk operations at all
105
- }, 90000);
106
-
107
- it('sequential fallback takes longer than 20 seconds for 20 transactions', async () => {
108
- const { duration, result } = await measurePerformanceScenario({
109
- transactionCount: 20,
110
- bulkDelay: 50,
111
- sequentialDelay: 1050,
112
- forceSequential: true,
113
- });
114
- console.log(`Sequential fallback (20 txns): ${duration}ms`);
115
- expect(duration).toBeGreaterThan(20000);
116
- expect(result.summary.transactions_created).toBe(20);
117
- expect(result.bulk_operation_details?.sequential_fallbacks).toBe(1);
118
- expect(result.bulk_operation_details?.bulk_successes).toBe(0);
119
- }, 90000);
120
-
121
- it('achieves at least a 3x speedup over pure sequential mode', async () => {
122
- const bulkRun = await measurePerformanceScenario({
123
- transactionCount: 20,
124
- bulkDelay: 50,
125
- });
126
- // Use pure sequential baseline for canonical comparison
127
- const pureSequentialRun = await measurePerformanceScenario({
128
- transactionCount: 1,
129
- bulkDelay: 50,
130
- sequentialDelay: 1050,
131
- multipleRuns: 20,
132
- });
133
- const speedup = pureSequentialRun.duration / bulkRun.duration;
134
- console.log(`Bulk vs pure sequential speedup: ${speedup.toFixed(2)}x faster`);
135
- expect(speedup).toBeGreaterThanOrEqual(3);
136
- }, 120000);
137
-
138
- it('handles 150-transaction chunking without significant overhead', async () => {
139
- const { duration, result } = await measurePerformanceScenario({
140
- transactionCount: 150,
141
- bulkDelay: 60,
142
- });
143
- console.log(`Chunking benchmark (150 txns): ${duration}ms`);
144
- expect(duration).toBeLessThan(15000);
145
- expect(result.summary.transactions_created).toBe(150);
146
- expect(result.bulk_operation_details?.chunks_processed).toBeGreaterThanOrEqual(2);
147
- }, 60000);
148
-
149
- it('stays within 10MB of heap growth for 100 bulk transactions', async () => {
150
- const before = process.memoryUsage().heapUsed;
151
- const { result } = await measurePerformanceScenario({
152
- transactionCount: 100,
153
- bulkDelay: 30,
154
- });
155
- const after = process.memoryUsage().heapUsed;
156
- const deltaMb = (after - before) / (1024 * 1024);
157
- expect(result.summary.transactions_created).toBe(100);
158
- expect(deltaMb).toBeLessThan(10);
159
- });
89
+ describe.skip("Reconciliation Performance - Bulk vs Sequential", () => {
90
+ it("processes 20 transactions in bulk mode in under 8 seconds", async () => {
91
+ const { duration, result } = await measurePerformanceScenario({
92
+ transactionCount: 20,
93
+ bulkDelay: 50,
94
+ });
95
+ console.log(`Bulk benchmark (20 txns): ${duration}ms`);
96
+ expect(duration).toBeLessThan(8000);
97
+ expect(result.summary.transactions_created).toBe(20);
98
+ expect(result.bulk_operation_details?.bulk_successes).toBe(1);
99
+ }, 60000);
100
+
101
+ it("pure sequential mode (single transaction) takes longer than 20 seconds", async () => {
102
+ // Pure sequential baseline: only 1 transaction per "unmatched_bank" to avoid bulk mode
103
+ const { duration, result } = await measurePerformanceScenario({
104
+ transactionCount: 1, // This ensures bulk mode is never entered
105
+ bulkDelay: 50,
106
+ sequentialDelay: 1050,
107
+ multipleRuns: 20, // Run 20 times to simulate 20 sequential transactions
108
+ });
109
+ console.log(
110
+ `Pure sequential baseline (20 txns, 1 at a time): ${duration}ms`,
111
+ );
112
+ expect(duration).toBeGreaterThan(20000);
113
+ expect(result.summary.transactions_created).toBe(1);
114
+ expect(result.bulk_operation_details).toBeUndefined(); // No bulk operations at all
115
+ }, 90000);
116
+
117
+ it("sequential fallback takes longer than 20 seconds for 20 transactions", async () => {
118
+ const { duration, result } = await measurePerformanceScenario({
119
+ transactionCount: 20,
120
+ bulkDelay: 50,
121
+ sequentialDelay: 1050,
122
+ forceSequential: true,
123
+ });
124
+ console.log(`Sequential fallback (20 txns): ${duration}ms`);
125
+ expect(duration).toBeGreaterThan(20000);
126
+ expect(result.summary.transactions_created).toBe(20);
127
+ expect(result.bulk_operation_details?.sequential_fallbacks).toBe(1);
128
+ expect(result.bulk_operation_details?.bulk_successes).toBe(0);
129
+ }, 90000);
130
+
131
+ it("achieves at least a 3x speedup over pure sequential mode", async () => {
132
+ const bulkRun = await measurePerformanceScenario({
133
+ transactionCount: 20,
134
+ bulkDelay: 50,
135
+ });
136
+ // Use pure sequential baseline for canonical comparison
137
+ const pureSequentialRun = await measurePerformanceScenario({
138
+ transactionCount: 1,
139
+ bulkDelay: 50,
140
+ sequentialDelay: 1050,
141
+ multipleRuns: 20,
142
+ });
143
+ const speedup = pureSequentialRun.duration / bulkRun.duration;
144
+ console.log(
145
+ `Bulk vs pure sequential speedup: ${speedup.toFixed(2)}x faster`,
146
+ );
147
+ expect(speedup).toBeGreaterThanOrEqual(3);
148
+ }, 120000);
149
+
150
+ it("handles 150-transaction chunking without significant overhead", async () => {
151
+ const { duration, result } = await measurePerformanceScenario({
152
+ transactionCount: 150,
153
+ bulkDelay: 60,
154
+ });
155
+ console.log(`Chunking benchmark (150 txns): ${duration}ms`);
156
+ expect(duration).toBeLessThan(15000);
157
+ expect(result.summary.transactions_created).toBe(150);
158
+ expect(
159
+ result.bulk_operation_details?.chunks_processed,
160
+ ).toBeGreaterThanOrEqual(2);
161
+ }, 60000);
162
+
163
+ it("stays within 10MB of heap growth for 100 bulk transactions", async () => {
164
+ const before = process.memoryUsage().heapUsed;
165
+ const { result } = await measurePerformanceScenario({
166
+ transactionCount: 100,
167
+ bulkDelay: 30,
168
+ });
169
+ const after = process.memoryUsage().heapUsed;
170
+ const deltaMb = (after - before) / (1024 * 1024);
171
+ expect(result.summary.transactions_created).toBe(100);
172
+ expect(deltaMb).toBeLessThan(10);
173
+ });
160
174
  });
161
175
 
162
176
  const performanceInitialAccount: AccountSnapshot = {
163
- balance: 0,
164
- cleared_balance: 0,
165
- uncleared_balance: 0,
177
+ balance: 0,
178
+ cleared_balance: 0,
179
+ uncleared_balance: 0,
166
180
  };
167
181
 
168
182
  function buildPerformanceAnalysis(
169
- count: number,
170
- amount = 5,
171
- statementMultiplier = count,
183
+ count: number,
184
+ amount = 5,
185
+ statementMultiplier = count,
172
186
  ): ReconciliationAnalysis {
173
- const statementBalance = amount * statementMultiplier;
174
- const baseDate = Date.parse('2025-08-01');
175
-
176
- return {
177
- success: true,
178
- phase: 'analysis',
179
- summary: {
180
- statement_date_range: 'Performance suite',
181
- bank_transactions_count: count,
182
- ynab_transactions_count: 0,
183
- auto_matched: 0,
184
- suggested_matches: 0,
185
- unmatched_bank: count,
186
- unmatched_ynab: 0,
187
- current_cleared_balance: 0,
188
- target_statement_balance: statementBalance,
189
- discrepancy: statementBalance,
190
- discrepancy_explanation: 'Synthetic performance delta',
191
- },
192
- auto_matches: [],
193
- suggested_matches: [],
194
- unmatched_bank: Array.from({ length: count }, (_, index) => {
195
- const date = new Date(baseDate + index * 24 * 60 * 60 * 1000);
196
- return {
197
- id: `perf-bank-${index}`,
198
- date: date.toISOString().slice(0, 10),
199
- amount,
200
- payee: `Performance Payee ${index}`,
201
- memo: `Performance memo ${index}`,
202
- original_csv_row: index + 1,
203
- };
204
- }),
205
- unmatched_ynab: [],
206
- balance_info: {
207
- current_cleared: 0,
208
- current_uncleared: 0,
209
- current_total: 0,
210
- target_statement: statementBalance,
211
- discrepancy: statementBalance,
212
- on_track: false,
213
- },
214
- next_steps: [],
215
- insights: [],
216
- };
187
+ const statementBalance = amount * statementMultiplier;
188
+ const baseDate = Date.parse("2025-08-01");
189
+
190
+ return {
191
+ success: true,
192
+ phase: "analysis",
193
+ summary: {
194
+ statement_date_range: "Performance suite",
195
+ bank_transactions_count: count,
196
+ ynab_transactions_count: 0,
197
+ auto_matched: 0,
198
+ suggested_matches: 0,
199
+ unmatched_bank: count,
200
+ unmatched_ynab: 0,
201
+ current_cleared_balance: 0,
202
+ target_statement_balance: statementBalance,
203
+ discrepancy: statementBalance,
204
+ discrepancy_explanation: "Synthetic performance delta",
205
+ },
206
+ auto_matches: [],
207
+ suggested_matches: [],
208
+ unmatched_bank: Array.from({ length: count }, (_, index) => {
209
+ const date = new Date(baseDate + index * 24 * 60 * 60 * 1000);
210
+ return {
211
+ id: `perf-bank-${index}`,
212
+ date: date.toISOString().slice(0, 10),
213
+ amount,
214
+ payee: `Performance Payee ${index}`,
215
+ memo: `Performance memo ${index}`,
216
+ original_csv_row: index + 1,
217
+ };
218
+ }),
219
+ unmatched_ynab: [],
220
+ balance_info: {
221
+ current_cleared: 0,
222
+ current_uncleared: 0,
223
+ current_total: 0,
224
+ target_statement: statementBalance,
225
+ discrepancy: statementBalance,
226
+ on_track: false,
227
+ },
228
+ next_steps: [],
229
+ insights: [],
230
+ };
217
231
  }
218
232
 
219
233
  function buildPerformanceParams(
220
- statementBalance: number,
221
- overrides: Partial<ReconcileAccountRequest> = {},
234
+ statementBalance: number,
235
+ overrides: Partial<ReconcileAccountRequest> = {},
222
236
  ): ReconcileAccountRequest {
223
- return {
224
- budget_id: 'budget-performance',
225
- account_id: 'account-performance',
226
- csv_data: 'Date,Description,Amount',
227
- statement_balance: statementBalance,
228
- statement_date: '2025-08-31',
229
- date_tolerance_days: 1,
230
- amount_tolerance_cents: 1,
231
- auto_match_threshold: 90,
232
- suggestion_threshold: 60,
233
- auto_create_transactions: true,
234
- auto_update_cleared_status: false,
235
- auto_unclear_missing: false,
236
- auto_adjust_dates: false,
237
- dry_run: false,
238
- require_exact_match: true,
239
- confidence_threshold: 0.8,
240
- max_resolution_attempts: 3,
241
- include_structured_data: false,
242
- ...overrides,
243
- };
237
+ return {
238
+ budget_id: "budget-performance",
239
+ account_id: "account-performance",
240
+ csv_data: "Date,Description,Amount",
241
+ statement_balance: statementBalance,
242
+ statement_date: "2025-08-31",
243
+ date_tolerance_days: 1,
244
+ amount_tolerance_cents: 1,
245
+ auto_match_threshold: 90,
246
+ suggestion_threshold: 60,
247
+ auto_create_transactions: true,
248
+ auto_update_cleared_status: false,
249
+ auto_unclear_missing: false,
250
+ auto_adjust_dates: false,
251
+ dry_run: false,
252
+ require_exact_match: true,
253
+ confidence_threshold: 0.8,
254
+ max_resolution_attempts: 3,
255
+ include_structured_data: false,
256
+ ...overrides,
257
+ };
244
258
  }
245
259
 
246
260
  function delay(ms: number): Promise<void> {
247
- return new Promise((resolve) => setTimeout(resolve, ms));
261
+ return new Promise((resolve) => setTimeout(resolve, ms));
248
262
  }
249
263
 
250
264
  function createPerformanceApi(options: {
251
- bulkDelay?: number;
252
- sequentialDelay?: number;
253
- failBulk?: boolean;
265
+ bulkDelay?: number;
266
+ sequentialDelay?: number;
267
+ failBulk?: boolean;
254
268
  }) {
255
- const createTransactions = vi.fn().mockImplementation(async (_budgetId, body: any) => {
256
- if (options.failBulk) {
257
- throw new Error('bulk failure');
258
- }
259
- if (options.bulkDelay) {
260
- await delay(options.bulkDelay);
261
- }
262
- const transactions = (body.transactions ?? []).map((txn: any, index: number) => ({
263
- id: `bulk-${index}-${Date.now()}`,
264
- account_id: txn.account_id,
265
- amount: txn.amount,
266
- date: txn.date,
267
- cleared: 'cleared',
268
- approved: true,
269
- }));
270
- return { data: { transactions } };
271
- });
272
-
273
- const createTransaction = vi.fn().mockImplementation(async (_budgetId, body: any) => {
274
- if (options.sequentialDelay) {
275
- const asyncWait = Math.min(options.sequentialDelay, 50);
276
- await delay(asyncWait);
277
- const busyWait = Math.max(options.sequentialDelay - asyncWait, 0);
278
- const start = Date.now();
279
- while (Date.now() - start < busyWait) {
280
- // busy-wait to simulate processing overhead
281
- }
282
- }
283
- return {
284
- data: {
285
- transaction: {
286
- id: `seq-${Date.now()}`,
287
- amount: body.transaction?.amount ?? 0,
288
- date: body.transaction?.date ?? '2025-09-01',
289
- cleared: 'cleared',
290
- approved: true,
291
- },
292
- },
293
- };
294
- });
295
-
296
- const updateTransactions = vi.fn().mockResolvedValue({ data: { transactions: [] } });
297
- const getTransactionsByAccount = vi.fn().mockResolvedValue({ data: { transactions: [] } });
298
- const getAccountById = vi.fn().mockResolvedValue({
299
- data: {
300
- account: {
301
- id: 'account-performance',
302
- balance: performanceInitialAccount.balance,
303
- cleared_balance: performanceInitialAccount.cleared_balance,
304
- uncleared_balance: performanceInitialAccount.uncleared_balance,
305
- },
306
- },
307
- });
308
-
309
- const api = {
310
- transactions: {
311
- createTransactions,
312
- createTransaction,
313
- updateTransactions,
314
- getTransactionsByAccount,
315
- },
316
- accounts: {
317
- getAccountById,
318
- },
319
- } as unknown as ynab.API;
320
-
321
- return { api, mocks: { createTransactions, createTransaction } };
269
+ const createTransactions = vi
270
+ .fn()
271
+ .mockImplementation(async (_budgetId, body: any) => {
272
+ if (options.failBulk) {
273
+ throw new Error("bulk failure");
274
+ }
275
+ if (options.bulkDelay) {
276
+ await delay(options.bulkDelay);
277
+ }
278
+ const transactions = (body.transactions ?? []).map(
279
+ (txn: any, index: number) => ({
280
+ id: `bulk-${index}-${Date.now()}`,
281
+ account_id: txn.account_id,
282
+ amount: txn.amount,
283
+ date: txn.date,
284
+ cleared: "cleared",
285
+ approved: true,
286
+ }),
287
+ );
288
+ return { data: { transactions } };
289
+ });
290
+
291
+ const createTransaction = vi
292
+ .fn()
293
+ .mockImplementation(async (_budgetId, body: any) => {
294
+ if (options.sequentialDelay) {
295
+ const asyncWait = Math.min(options.sequentialDelay, 50);
296
+ await delay(asyncWait);
297
+ const busyWait = Math.max(options.sequentialDelay - asyncWait, 0);
298
+ const start = Date.now();
299
+ while (Date.now() - start < busyWait) {
300
+ // busy-wait to simulate processing overhead
301
+ }
302
+ }
303
+ return {
304
+ data: {
305
+ transaction: {
306
+ id: `seq-${Date.now()}`,
307
+ amount: body.transaction?.amount ?? 0,
308
+ date: body.transaction?.date ?? "2025-09-01",
309
+ cleared: "cleared",
310
+ approved: true,
311
+ },
312
+ },
313
+ };
314
+ });
315
+
316
+ const updateTransactions = vi
317
+ .fn()
318
+ .mockResolvedValue({ data: { transactions: [] } });
319
+ const getTransactionsByAccount = vi
320
+ .fn()
321
+ .mockResolvedValue({ data: { transactions: [] } });
322
+ const getAccountById = vi.fn().mockResolvedValue({
323
+ data: {
324
+ account: {
325
+ id: "account-performance",
326
+ balance: performanceInitialAccount.balance,
327
+ cleared_balance: performanceInitialAccount.cleared_balance,
328
+ uncleared_balance: performanceInitialAccount.uncleared_balance,
329
+ },
330
+ },
331
+ });
332
+
333
+ const api = {
334
+ transactions: {
335
+ createTransactions,
336
+ createTransaction,
337
+ updateTransactions,
338
+ getTransactionsByAccount,
339
+ },
340
+ accounts: {
341
+ getAccountById,
342
+ },
343
+ } as unknown as ynab.API;
344
+
345
+ return { api, mocks: { createTransactions, createTransaction } };
322
346
  }
323
347
 
324
348
  async function measurePerformanceScenario(options: {
325
- transactionCount: number;
326
- amount?: number;
327
- bulkDelay?: number;
328
- sequentialDelay?: number;
329
- forceSequential?: boolean;
330
- multipleRuns?: number;
349
+ transactionCount: number;
350
+ amount?: number;
351
+ bulkDelay?: number;
352
+ sequentialDelay?: number;
353
+ forceSequential?: boolean;
354
+ multipleRuns?: number;
331
355
  }): Promise<{
332
- duration: number;
333
- result: Awaited<ReturnType<typeof executeReconciliation>>;
356
+ duration: number;
357
+ result: Awaited<ReturnType<typeof executeReconciliation>>;
334
358
  }> {
335
- const analysis = buildPerformanceAnalysis(options.transactionCount, options.amount ?? 5);
336
- const params = buildPerformanceParams(analysis.summary.target_statement_balance);
337
- const { api } = createPerformanceApi({
338
- bulkDelay: options.bulkDelay,
339
- sequentialDelay: options.sequentialDelay,
340
- failBulk: options.forceSequential,
341
- });
342
-
343
- const start = Date.now();
344
- let result: Awaited<ReturnType<typeof executeReconciliation>>;
345
-
346
- if (options.multipleRuns) {
347
- // Run the scenario multiple times sequentially to measure pure sequential performance
348
- for (let i = 0; i < options.multipleRuns; i++) {
349
- result = await executeReconciliation({
350
- ynabAPI: api,
351
- analysis,
352
- params,
353
- budgetId: params.budget_id,
354
- accountId: params.account_id,
355
- initialAccount: performanceInitialAccount,
356
- currencyCode: 'USD',
357
- });
358
- }
359
- } else {
360
- result = await executeReconciliation({
361
- ynabAPI: api,
362
- analysis,
363
- params,
364
- budgetId: params.budget_id,
365
- accountId: params.account_id,
366
- initialAccount: performanceInitialAccount,
367
- currencyCode: 'USD',
368
- });
369
- }
370
- const duration = Date.now() - start;
371
- return { duration, result: result! };
359
+ const analysis = buildPerformanceAnalysis(
360
+ options.transactionCount,
361
+ options.amount ?? 5,
362
+ );
363
+ const params = buildPerformanceParams(
364
+ analysis.summary.target_statement_balance,
365
+ );
366
+ const { api } = createPerformanceApi({
367
+ bulkDelay: options.bulkDelay,
368
+ sequentialDelay: options.sequentialDelay,
369
+ failBulk: options.forceSequential,
370
+ });
371
+
372
+ const start = Date.now();
373
+ let result: Awaited<ReturnType<typeof executeReconciliation>>;
374
+
375
+ if (options.multipleRuns) {
376
+ // Run the scenario multiple times sequentially to measure pure sequential performance
377
+ for (let i = 0; i < options.multipleRuns; i++) {
378
+ result = await executeReconciliation({
379
+ ynabAPI: api,
380
+ analysis,
381
+ params,
382
+ budgetId: params.budget_id,
383
+ accountId: params.account_id,
384
+ initialAccount: performanceInitialAccount,
385
+ currencyCode: "USD",
386
+ });
387
+ }
388
+ } else {
389
+ result = await executeReconciliation({
390
+ ynabAPI: api,
391
+ analysis,
392
+ params,
393
+ budgetId: params.budget_id,
394
+ accountId: params.account_id,
395
+ initialAccount: performanceInitialAccount,
396
+ currencyCode: "USD",
397
+ });
398
+ }
399
+ const duration = Date.now() - start;
400
+ return { duration, result: result! };
372
401
  }
373
402
 
374
- describe('YNAB MCP Server - Performance Tests', () => {
375
- let server: InstanceType<typeof import('../server/YNABMCPServer.js').YNABMCPServer>;
376
- let mockYnabAPI: any;
377
-
378
- beforeEach(async () => {
379
- // Ensure YNAB_ACCESS_TOKEN is set for all tests, even if just a placeholder
380
- process.env['YNAB_ACCESS_TOKEN'] = 'test-token-performance';
381
- // Clear modules to ensure fresh import of server with new env var
382
- vi.resetModules();
383
- const { YNABMCPServer } = await import('../server/YNABMCPServer.js');
384
- server = new YNABMCPServer();
385
-
386
- // Mock the YNAB API constructor to ensure it receives the correct access token
387
- const { API } = await import('ynab');
388
- mockYnabAPI = new (API as any)('test-token-performance');
389
-
390
- vi.clearAllMocks();
391
- // Clear cache to ensure mocks are called in each test
392
- await executeToolCall(server, 'ynab:clear_cache');
393
- });
394
-
395
- describe('Response Time Performance', () => {
396
- it('should respond to budget listing within acceptable time', async () => {
397
- // Mock quick response
398
- mockYnabAPI.budgets.getBudgets.mockResolvedValue({
399
- data: {
400
- budgets: Array.from({ length: 5 }, (_, i) => ({
401
- id: `budget-${i}`,
402
- name: `Budget ${i}`,
403
- last_modified_on: '2024-01-01T00:00:00Z',
404
- first_month: '2024-01-01',
405
- last_month: '2024-12-01',
406
- })),
407
- },
408
- });
409
-
410
- const startTime = Date.now();
411
- const result = await executeToolCall(server, 'ynab:list_budgets');
412
- const endTime = Date.now();
413
-
414
- const responseTime = endTime - startTime;
415
-
416
- expect(result).toBeDefined();
417
- expect(responseTime).toBeLessThan(1000); // Should respond within 1 second
418
-
419
- const budgets = parseToolResult(result);
420
- expect(budgets.data.budgets).toHaveLength(5);
421
- });
422
-
423
- it('should handle large transaction lists efficiently', async () => {
424
- // Use smaller list to avoid size limit and ensure we get 'transactions' not 'preview_transactions'
425
- const largeTransactionList = Array.from({ length: 100 }, (_, i) => ({
426
- id: `transaction-${i}`,
427
- date: '2024-01-01',
428
- amount: -1000 * (i + 1),
429
- memo: `Transaction ${i}`,
430
- cleared: 'cleared' as const,
431
- approved: true,
432
- account_id: 'account-1',
433
- category_id: 'category-1',
434
- deleted: false,
435
- payee_name: `Payee ${i}`,
436
- category_name: `Category ${i}`,
437
- }));
438
-
439
- // Mock the method that list_transactions actually uses for budget-wide queries
440
- // Use mockImplementation to ensure it works with any arguments (including lastKnowledge)
441
- mockYnabAPI.transactions.getTransactions.mockImplementation(async () => ({
442
- data: {
443
- transactions: largeTransactionList,
444
- server_knowledge: 100,
445
- },
446
- }));
447
-
448
- const startTime = Date.now();
449
- const result = await executeToolCall(server, 'ynab:list_transactions', {
450
- budget_id: '123e4567-e89b-12d3-a456-426614174000', // Valid UUID
451
- });
452
- const endTime = Date.now();
453
-
454
- const responseTime = endTime - startTime;
455
-
456
- expect(result).toBeDefined();
457
- expect(responseTime).toBeLessThan(2000); // Should handle large lists within 2 seconds
458
-
459
- // Validate response structure
460
- validateToolResponse(result, (data) => data.transactions || data.preview_transactions);
461
- });
462
-
463
- it('should handle concurrent requests efficiently', async () => {
464
- // Mock responses for concurrent requests
465
- mockYnabAPI.budgets.getBudgets.mockResolvedValue({
466
- data: { budgets: [{ id: 'budget-1', name: 'Test Budget' }] },
467
- });
468
-
469
- mockYnabAPI.accounts.getAccounts.mockResolvedValue({
470
- data: {
471
- accounts: [{ id: 'account-1', name: 'Test Account', type: 'checking', balance: 0 }],
472
- },
473
- });
474
-
475
- mockYnabAPI.user.getUser.mockResolvedValue({
476
- data: { user: { id: 'user-1', email: 'test@example.com' } },
477
- });
478
-
479
- const startTime = Date.now();
480
-
481
- // Execute multiple concurrent requests
482
- const promises = [
483
- executeToolCall(server, 'ynab:list_budgets'),
484
- executeToolCall(server, 'ynab:list_accounts', { budget_id: 'test-budget' }),
485
- executeToolCall(server, 'ynab:get_user'),
486
- executeToolCall(server, 'ynab:list_budgets'),
487
- executeToolCall(server, 'ynab:list_accounts', { budget_id: 'test-budget' }),
488
- ];
489
-
490
- const results = await Promise.all(promises);
491
- const endTime = Date.now();
492
-
493
- const totalTime = endTime - startTime;
494
-
495
- expect(results).toHaveLength(5);
496
- results.forEach((result) => expect(result).toBeDefined());
497
- expect(totalTime).toBeLessThan(3000); // All concurrent requests within 3 seconds
498
- });
499
- });
500
-
501
- describe('Memory Usage Performance', () => {
502
- it('should handle memory efficiently with large datasets', async () => {
503
- // Create a large mock dataset
504
- const largeCategoryList = Array.from({ length: 100 }, (_, groupIndex) => ({
505
- id: `group-${groupIndex}`,
506
- name: `Category Group ${groupIndex}`,
507
- hidden: false,
508
- deleted: false,
509
- categories: Array.from({ length: 20 }, (_, catIndex) => ({
510
- id: `category-${groupIndex}-${catIndex}`,
511
- category_group_id: `group-${groupIndex}`,
512
- name: `Category ${groupIndex}-${catIndex}`,
513
- hidden: false,
514
- deleted: false,
515
- budgeted: 1000 * catIndex,
516
- activity: -500 * catIndex,
517
- balance: 500 * catIndex,
518
- })),
519
- }));
520
-
521
- // Use mockImplementation to ensure it works with any arguments (including lastKnowledge)
522
- mockYnabAPI.categories.getCategories.mockImplementation(async () => ({
523
- data: {
524
- category_groups: largeCategoryList,
525
- server_knowledge: 100,
526
- },
527
- }));
528
-
529
- const initialMemory = process.memoryUsage();
530
-
531
- // Process large dataset multiple times
532
- for (let i = 0; i < 10; i++) {
533
- const result = await executeToolCall(server, 'ynab:list_categories', {
534
- budget_id: '123e4567-e89b-12d3-a456-426614174000', // Valid UUID
535
- });
536
-
537
- // Validate response structure
538
- validateToolResponse(result, (data) => data.category_groups);
539
-
540
- // Force garbage collection if available
541
- if (global.gc) {
542
- global.gc();
543
- }
544
- }
545
-
546
- const finalMemory = process.memoryUsage();
547
-
548
- // Memory usage shouldn't grow excessively (allow for some variance)
549
- const memoryGrowth = finalMemory.heapUsed - initialMemory.heapUsed;
550
- // With large datasets (2000 categories × 10 iterations), allow more memory growth
551
- // Each category has multiple fields, and we're dealing with substantial JSON parsing
552
- expect(memoryGrowth).toBeLessThan(100 * 1024 * 1024); // Less than 100MB growth
553
- });
554
- });
555
-
556
- describe('Error Handling Performance', () => {
557
- it('should handle errors quickly without blocking', async () => {
558
- // Mock API errors
559
- const apiError = new Error('API Error');
560
- mockYnabAPI.budgets.getBudgets.mockRejectedValue(apiError);
561
- mockYnabAPI.accounts.getAccounts.mockRejectedValue(apiError);
562
-
563
- const startTime = Date.now();
564
-
565
- // Execute multiple failing requests
566
- const promises = [
567
- executeToolCall(server, 'ynab:list_budgets'),
568
- executeToolCall(server, 'ynab:list_accounts', { budget_id: 'test' }),
569
- executeToolCall(server, 'ynab:list_budgets'),
570
- ];
571
-
572
- const results = await Promise.all(promises);
573
- const endTime = Date.now();
574
-
575
- const totalTime = endTime - startTime;
576
-
577
- // Check that all results are error responses
578
- results.forEach((result) => {
579
- const parsed = parseToolResult(result);
580
- expect(parsed.error || parsed.data?.error).toBeDefined();
581
- });
582
- expect(totalTime).toBeLessThan(1000); // Errors should be handled quickly
583
- });
584
-
585
- it('should recover from rate limiting gracefully', async () => {
586
- let callCount = 0;
587
-
588
- // Mock rate limiting on first few calls, then success
589
- mockYnabAPI.budgets.getBudgets.mockImplementation(() => {
590
- callCount++;
591
- if (callCount <= 2) {
592
- const rateLimitError = new Error('Rate Limited');
593
- (rateLimitError as any).error = { id: '429', name: 'rate_limit' };
594
- return Promise.reject(rateLimitError);
595
- }
596
- return Promise.resolve({
597
- data: { budgets: [{ id: 'budget-1', name: 'Test Budget' }] },
598
- });
599
- });
600
-
601
- const startTime = Date.now();
602
-
603
- try {
604
- // This should fail due to rate limiting
605
- await executeToolCall(server, 'ynab:list_budgets');
606
- expect.fail('Should have thrown rate limit error');
607
- } catch (error) {
608
- expect(error).toBeDefined();
609
- }
610
-
611
- const endTime = Date.now();
612
- const errorTime = endTime - startTime;
613
-
614
- expect(errorTime).toBeLessThan(500); // Rate limit errors should be fast
615
- expect(callCount).toBe(1);
616
- });
617
- });
618
-
619
- describe('Validation Performance', () => {
620
- it('should validate input parameters quickly', async () => {
621
- const startTime = Date.now();
622
-
623
- // Test multiple validation scenarios
624
- const validationTests = [
625
- // Invalid parameters (should fail quickly)
626
- executeToolCall(server, 'ynab:get_budget', {
627
- budget_id: '', // Empty string should fail validation
628
- }),
629
-
630
- executeToolCall(server, 'ynab:create_transaction', {
631
- budget_id: 'test',
632
- account_id: 'test',
633
- amount: 'not-a-number', // Invalid type
634
- date: '2024-01-01',
635
- }),
636
- ];
637
-
638
- const results = await Promise.all(validationTests);
639
- const parsed = results.map((result) => parseToolResult(result));
640
- const endTime = Date.now();
641
-
642
- const totalTime = endTime - startTime;
643
-
644
- expect(parsed).toHaveLength(2);
645
- const firstError = parsed[0].error ?? parsed[0].data?.error;
646
- const secondError = parsed[1].error ?? parsed[1].data?.error;
647
- expect(firstError?.code).toBe(SecurityErrorCode.VALIDATION_ERROR); // Invalid calls should fail
648
- expect(secondError?.code).toBe(SecurityErrorCode.VALIDATION_ERROR);
649
- expect(totalTime).toBeLessThan(1000); // Validation should be fast
650
- });
651
- });
652
-
653
- describe('Stress Testing', () => {
654
- it('should handle rapid sequential requests', async () => {
655
- mockYnabAPI.user.getUser.mockResolvedValue({
656
- data: { user: { id: 'user-1', email: 'test@example.com' } },
657
- });
658
-
659
- const startTime = Date.now();
660
-
661
- // Execute 50 rapid sequential requests
662
- const results = [];
663
- for (let i = 0; i < 50; i++) {
664
- const result = await executeToolCall(server, 'ynab:get_user');
665
- results.push(result);
666
- }
667
-
668
- const endTime = Date.now();
669
- const totalTime = endTime - startTime;
670
- const averageTime = totalTime / 50;
671
-
672
- expect(results).toHaveLength(50);
673
- results.forEach((result) => expect(result).toBeDefined());
674
- expect(averageTime).toBeLessThan(100); // Average less than 100ms per request
675
- expect(totalTime).toBeLessThan(5000); // Total less than 5 seconds
676
- });
677
-
678
- it('should maintain performance under mixed workload', async () => {
679
- // Mock various endpoints
680
- mockYnabAPI.budgets.getBudgets.mockResolvedValue({
681
- data: { budgets: [{ id: 'budget-1', name: 'Test Budget' }] },
682
- });
683
-
684
- mockYnabAPI.accounts.getAccounts.mockResolvedValue({
685
- data: { accounts: [{ id: 'account-1', name: 'Test Account' }] },
686
- });
687
-
688
- mockYnabAPI.transactions.getTransactions.mockImplementation(() =>
689
- Promise.resolve({
690
- data: { transactions: [] },
691
- }),
692
- );
693
-
694
- mockYnabAPI.categories.getCategories.mockImplementation(() =>
695
- Promise.resolve({
696
- data: { category_groups: [] },
697
- }),
698
- );
699
-
700
- const startTime = Date.now();
701
-
702
- // Mixed workload: different tools with different complexities
703
- const mixedPromises = [];
704
- for (let i = 0; i < 20; i++) {
705
- mixedPromises.push(
706
- executeToolCall(server, 'ynab:list_budgets'),
707
- executeToolCall(server, 'ynab:list_accounts', { budget_id: 'test' }),
708
- executeToolCall(server, 'ynab:list_transactions', { budget_id: 'test' }),
709
- executeToolCall(server, 'ynab:list_categories', { budget_id: 'test' }),
710
- );
711
- }
712
-
713
- const results = await Promise.all(mixedPromises);
714
- const endTime = Date.now();
715
-
716
- const totalTime = endTime - startTime;
717
-
718
- expect(results).toHaveLength(80); // 20 iterations × 4 tools
719
- results.forEach((result) => expect(result).toBeDefined());
720
- expect(totalTime).toBeLessThan(10000); // Should complete within 10 seconds
721
- });
722
- });
403
+ describe("YNAB MCP Server - Performance Tests", () => {
404
+ let server: InstanceType<
405
+ typeof import("../server/YNABMCPServer.js").YNABMCPServer
406
+ >;
407
+ let mockYnabAPI: any;
408
+
409
+ beforeEach(async () => {
410
+ // Ensure YNAB_ACCESS_TOKEN is set for all tests, even if just a placeholder
411
+ process.env.YNAB_ACCESS_TOKEN = "test-token-performance";
412
+ // Clear modules to ensure fresh import of server with new env var
413
+ vi.resetModules();
414
+ const { YNABMCPServer } = await import("../server/YNABMCPServer.js");
415
+ server = new YNABMCPServer();
416
+
417
+ // Mock the YNAB API constructor to ensure it receives the correct access token
418
+ const { API } = await import("ynab");
419
+ mockYnabAPI = new (API as any)("test-token-performance");
420
+
421
+ vi.clearAllMocks();
422
+ // Clear cache to ensure mocks are called in each test
423
+ await executeToolCall(server, "ynab:clear_cache");
424
+ });
425
+
426
+ describe("Response Time Performance", () => {
427
+ it("should respond to budget listing within acceptable time", async () => {
428
+ // Mock quick response
429
+ mockYnabAPI.budgets.getBudgets.mockResolvedValue({
430
+ data: {
431
+ budgets: Array.from({ length: 5 }, (_, i) => ({
432
+ id: `budget-${i}`,
433
+ name: `Budget ${i}`,
434
+ last_modified_on: "2024-01-01T00:00:00Z",
435
+ first_month: "2024-01-01",
436
+ last_month: "2024-12-01",
437
+ })),
438
+ },
439
+ });
440
+
441
+ const startTime = Date.now();
442
+ const result = await executeToolCall(server, "ynab:list_budgets");
443
+ const endTime = Date.now();
444
+
445
+ const responseTime = endTime - startTime;
446
+
447
+ expect(result).toBeDefined();
448
+ expect(responseTime).toBeLessThan(1000); // Should respond within 1 second
449
+
450
+ const budgets = parseToolResult(result);
451
+ expect(budgets.data.budgets).toHaveLength(5);
452
+ });
453
+
454
+ it("should handle large transaction lists efficiently", async () => {
455
+ // Use smaller list to avoid size limit and ensure we get 'transactions' not 'preview_transactions'
456
+ const largeTransactionList = Array.from({ length: 100 }, (_, i) => ({
457
+ id: `transaction-${i}`,
458
+ date: "2024-01-01",
459
+ amount: -1000 * (i + 1),
460
+ memo: `Transaction ${i}`,
461
+ cleared: "cleared" as const,
462
+ approved: true,
463
+ account_id: "account-1",
464
+ category_id: "category-1",
465
+ deleted: false,
466
+ payee_name: `Payee ${i}`,
467
+ category_name: `Category ${i}`,
468
+ }));
469
+
470
+ // Mock the method that list_transactions actually uses for budget-wide queries
471
+ // Use mockImplementation to ensure it works with any arguments (including lastKnowledge)
472
+ mockYnabAPI.transactions.getTransactions.mockImplementation(async () => ({
473
+ data: {
474
+ transactions: largeTransactionList,
475
+ server_knowledge: 100,
476
+ },
477
+ }));
478
+
479
+ const startTime = Date.now();
480
+ const result = await executeToolCall(server, "ynab:list_transactions", {
481
+ budget_id: "123e4567-e89b-12d3-a456-426614174000", // Valid UUID
482
+ });
483
+ const endTime = Date.now();
484
+
485
+ const responseTime = endTime - startTime;
486
+
487
+ expect(result).toBeDefined();
488
+ expect(responseTime).toBeLessThan(2000); // Should handle large lists within 2 seconds
489
+
490
+ // Validate response structure
491
+ validateToolResponse(
492
+ result,
493
+ (data) => data.transactions || data.preview_transactions,
494
+ );
495
+ });
496
+
497
+ it("should handle concurrent requests efficiently", async () => {
498
+ // Mock responses for concurrent requests
499
+ mockYnabAPI.budgets.getBudgets.mockResolvedValue({
500
+ data: { budgets: [{ id: "budget-1", name: "Test Budget" }] },
501
+ });
502
+
503
+ mockYnabAPI.accounts.getAccounts.mockResolvedValue({
504
+ data: {
505
+ accounts: [
506
+ {
507
+ id: "account-1",
508
+ name: "Test Account",
509
+ type: "checking",
510
+ balance: 0,
511
+ },
512
+ ],
513
+ },
514
+ });
515
+
516
+ mockYnabAPI.user.getUser.mockResolvedValue({
517
+ data: { user: { id: "user-1", email: "test@example.com" } },
518
+ });
519
+
520
+ const startTime = Date.now();
521
+
522
+ // Execute multiple concurrent requests
523
+ const promises = [
524
+ executeToolCall(server, "ynab:list_budgets"),
525
+ executeToolCall(server, "ynab:list_accounts", {
526
+ budget_id: "test-budget",
527
+ }),
528
+ executeToolCall(server, "ynab:get_user"),
529
+ executeToolCall(server, "ynab:list_budgets"),
530
+ executeToolCall(server, "ynab:list_accounts", {
531
+ budget_id: "test-budget",
532
+ }),
533
+ ];
534
+
535
+ const results = await Promise.all(promises);
536
+ const endTime = Date.now();
537
+
538
+ const totalTime = endTime - startTime;
539
+
540
+ expect(results).toHaveLength(5);
541
+ results.forEach((result) => expect(result).toBeDefined());
542
+ expect(totalTime).toBeLessThan(3000); // All concurrent requests within 3 seconds
543
+ });
544
+ });
545
+
546
+ describe("Memory Usage Performance", () => {
547
+ it("should handle memory efficiently with large datasets", async () => {
548
+ // Create a large mock dataset
549
+ const largeCategoryList = Array.from(
550
+ { length: 100 },
551
+ (_, groupIndex) => ({
552
+ id: `group-${groupIndex}`,
553
+ name: `Category Group ${groupIndex}`,
554
+ hidden: false,
555
+ deleted: false,
556
+ categories: Array.from({ length: 20 }, (_, catIndex) => ({
557
+ id: `category-${groupIndex}-${catIndex}`,
558
+ category_group_id: `group-${groupIndex}`,
559
+ name: `Category ${groupIndex}-${catIndex}`,
560
+ hidden: false,
561
+ deleted: false,
562
+ budgeted: 1000 * catIndex,
563
+ activity: -500 * catIndex,
564
+ balance: 500 * catIndex,
565
+ })),
566
+ }),
567
+ );
568
+
569
+ // Use mockImplementation to ensure it works with any arguments (including lastKnowledge)
570
+ mockYnabAPI.categories.getCategories.mockImplementation(async () => ({
571
+ data: {
572
+ category_groups: largeCategoryList,
573
+ server_knowledge: 100,
574
+ },
575
+ }));
576
+
577
+ const initialMemory = process.memoryUsage();
578
+
579
+ // Process large dataset multiple times
580
+ for (let i = 0; i < 10; i++) {
581
+ const result = await executeToolCall(server, "ynab:list_categories", {
582
+ budget_id: "123e4567-e89b-12d3-a456-426614174000", // Valid UUID
583
+ });
584
+
585
+ // Validate response structure
586
+ validateToolResponse(result, (data) => data.category_groups);
587
+
588
+ // Force garbage collection if available
589
+ if (global.gc) {
590
+ global.gc();
591
+ }
592
+ }
593
+
594
+ const finalMemory = process.memoryUsage();
595
+
596
+ // Memory usage shouldn't grow excessively (allow for some variance)
597
+ const memoryGrowth = finalMemory.heapUsed - initialMemory.heapUsed;
598
+ // With large datasets (2000 categories × 10 iterations), allow more memory growth
599
+ // Each category has multiple fields, and we're dealing with substantial JSON parsing
600
+ expect(memoryGrowth).toBeLessThan(100 * 1024 * 1024); // Less than 100MB growth
601
+ });
602
+ });
603
+
604
+ describe("Error Handling Performance", () => {
605
+ it("should handle errors quickly without blocking", async () => {
606
+ // Mock API errors
607
+ const apiError = new Error("API Error");
608
+ mockYnabAPI.budgets.getBudgets.mockRejectedValue(apiError);
609
+ mockYnabAPI.accounts.getAccounts.mockRejectedValue(apiError);
610
+
611
+ const startTime = Date.now();
612
+
613
+ // Execute multiple failing requests
614
+ const promises = [
615
+ executeToolCall(server, "ynab:list_budgets"),
616
+ executeToolCall(server, "ynab:list_accounts", { budget_id: "test" }),
617
+ executeToolCall(server, "ynab:list_budgets"),
618
+ ];
619
+
620
+ const results = await Promise.all(promises);
621
+ const endTime = Date.now();
622
+
623
+ const totalTime = endTime - startTime;
624
+
625
+ // Check that all results are error responses
626
+ results.forEach((result) => {
627
+ const parsed = parseToolResult(result);
628
+ expect(parsed.error || parsed.data?.error).toBeDefined();
629
+ });
630
+ expect(totalTime).toBeLessThan(1000); // Errors should be handled quickly
631
+ });
632
+
633
+ it("should recover from rate limiting gracefully", async () => {
634
+ let callCount = 0;
635
+
636
+ // Mock rate limiting on first few calls, then success
637
+ mockYnabAPI.budgets.getBudgets.mockImplementation(() => {
638
+ callCount++;
639
+ if (callCount <= 2) {
640
+ const rateLimitError = new Error("Rate Limited");
641
+ (rateLimitError as any).error = { id: "429", name: "rate_limit" };
642
+ return Promise.reject(rateLimitError);
643
+ }
644
+ return Promise.resolve({
645
+ data: { budgets: [{ id: "budget-1", name: "Test Budget" }] },
646
+ });
647
+ });
648
+
649
+ const startTime = Date.now();
650
+
651
+ try {
652
+ // This should fail due to rate limiting
653
+ await executeToolCall(server, "ynab:list_budgets");
654
+ expect.fail("Should have thrown rate limit error");
655
+ } catch (error) {
656
+ expect(error).toBeDefined();
657
+ }
658
+
659
+ const endTime = Date.now();
660
+ const errorTime = endTime - startTime;
661
+
662
+ expect(errorTime).toBeLessThan(500); // Rate limit errors should be fast
663
+ expect(callCount).toBe(1);
664
+ });
665
+ });
666
+
667
+ describe("Validation Performance", () => {
668
+ it("should validate input parameters quickly", async () => {
669
+ const startTime = Date.now();
670
+
671
+ // Test multiple validation scenarios
672
+ const validationTests = [
673
+ // Invalid parameters (should fail quickly)
674
+ executeToolCall(server, "ynab:get_budget", {
675
+ budget_id: "", // Empty string should fail validation
676
+ }),
677
+
678
+ executeToolCall(server, "ynab:create_transaction", {
679
+ budget_id: "test",
680
+ account_id: "test",
681
+ amount: "not-a-number", // Invalid type
682
+ date: "2024-01-01",
683
+ }),
684
+ ];
685
+
686
+ const results = await Promise.all(validationTests);
687
+ const parsed = results.map((result) => parseToolResult(result));
688
+ const endTime = Date.now();
689
+
690
+ const totalTime = endTime - startTime;
691
+
692
+ expect(parsed).toHaveLength(2);
693
+ const firstError = parsed[0].error ?? parsed[0].data?.error;
694
+ const secondError = parsed[1].error ?? parsed[1].data?.error;
695
+ expect(firstError?.code).toBe(SecurityErrorCode.VALIDATION_ERROR); // Invalid calls should fail
696
+ expect(secondError?.code).toBe(SecurityErrorCode.VALIDATION_ERROR);
697
+ expect(totalTime).toBeLessThan(1000); // Validation should be fast
698
+ });
699
+ });
700
+
701
+ describe("Stress Testing", () => {
702
+ it("should handle rapid sequential requests", async () => {
703
+ mockYnabAPI.user.getUser.mockResolvedValue({
704
+ data: { user: { id: "user-1", email: "test@example.com" } },
705
+ });
706
+
707
+ const startTime = Date.now();
708
+
709
+ // Execute 50 rapid sequential requests
710
+ const results = [];
711
+ for (let i = 0; i < 50; i++) {
712
+ const result = await executeToolCall(server, "ynab:get_user");
713
+ results.push(result);
714
+ }
715
+
716
+ const endTime = Date.now();
717
+ const totalTime = endTime - startTime;
718
+ const averageTime = totalTime / 50;
719
+
720
+ expect(results).toHaveLength(50);
721
+ results.forEach((result) => expect(result).toBeDefined());
722
+ expect(averageTime).toBeLessThan(100); // Average less than 100ms per request
723
+ expect(totalTime).toBeLessThan(5000); // Total less than 5 seconds
724
+ });
725
+
726
+ it("should maintain performance under mixed workload", async () => {
727
+ // Mock various endpoints
728
+ mockYnabAPI.budgets.getBudgets.mockResolvedValue({
729
+ data: { budgets: [{ id: "budget-1", name: "Test Budget" }] },
730
+ });
731
+
732
+ mockYnabAPI.accounts.getAccounts.mockResolvedValue({
733
+ data: { accounts: [{ id: "account-1", name: "Test Account" }] },
734
+ });
735
+
736
+ mockYnabAPI.transactions.getTransactions.mockImplementation(() =>
737
+ Promise.resolve({
738
+ data: { transactions: [] },
739
+ }),
740
+ );
741
+
742
+ mockYnabAPI.categories.getCategories.mockImplementation(() =>
743
+ Promise.resolve({
744
+ data: { category_groups: [] },
745
+ }),
746
+ );
747
+
748
+ const startTime = Date.now();
749
+
750
+ // Mixed workload: different tools with different complexities
751
+ const mixedPromises = [];
752
+ for (let i = 0; i < 20; i++) {
753
+ mixedPromises.push(
754
+ executeToolCall(server, "ynab:list_budgets"),
755
+ executeToolCall(server, "ynab:list_accounts", { budget_id: "test" }),
756
+ executeToolCall(server, "ynab:list_transactions", {
757
+ budget_id: "test",
758
+ }),
759
+ executeToolCall(server, "ynab:list_categories", {
760
+ budget_id: "test",
761
+ }),
762
+ );
763
+ }
764
+
765
+ const results = await Promise.all(mixedPromises);
766
+ const endTime = Date.now();
767
+
768
+ const totalTime = endTime - startTime;
769
+
770
+ expect(results).toHaveLength(80); // 20 iterations × 4 tools
771
+ results.forEach((result) => expect(result).toBeDefined());
772
+ expect(totalTime).toBeLessThan(10000); // Should complete within 10 seconds
773
+ });
774
+ });
723
775
  });