@dizzlkheinz/ynab-mcpb 0.18.4 → 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 (343) hide show
  1. package/CLAUDE.md +87 -8
  2. package/bin/ynab-mcp-server.cjs +2 -2
  3. package/bin/ynab-mcp-server.js +3 -3
  4. package/biome.json +39 -0
  5. package/dist/bundle/index.cjs +67 -67
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +27 -27
  8. package/dist/server/YNABMCPServer.d.ts +3 -4
  9. package/dist/server/YNABMCPServer.js +111 -116
  10. package/dist/server/budgetResolver.d.ts +6 -5
  11. package/dist/server/budgetResolver.js +46 -36
  12. package/dist/server/cacheKeys.js +6 -6
  13. package/dist/server/cacheManager.js +14 -11
  14. package/dist/server/completions.d.ts +2 -2
  15. package/dist/server/completions.js +20 -15
  16. package/dist/server/config.d.ts +10 -5
  17. package/dist/server/config.js +24 -7
  18. package/dist/server/deltaCache.d.ts +2 -2
  19. package/dist/server/deltaCache.js +22 -16
  20. package/dist/server/deltaCache.merge.d.ts +2 -2
  21. package/dist/server/diagnostics.d.ts +4 -4
  22. package/dist/server/diagnostics.js +38 -32
  23. package/dist/server/errorHandler.d.ts +5 -12
  24. package/dist/server/errorHandler.js +219 -217
  25. package/dist/server/prompts.d.ts +2 -2
  26. package/dist/server/prompts.js +45 -45
  27. package/dist/server/rateLimiter.js +4 -4
  28. package/dist/server/requestLogger.d.ts +1 -1
  29. package/dist/server/requestLogger.js +40 -35
  30. package/dist/server/resources.d.ts +3 -3
  31. package/dist/server/resources.js +55 -52
  32. package/dist/server/responseFormatter.js +6 -6
  33. package/dist/server/securityMiddleware.d.ts +2 -2
  34. package/dist/server/securityMiddleware.js +22 -20
  35. package/dist/server/serverKnowledgeStore.js +1 -1
  36. package/dist/server/toolRegistry.d.ts +3 -3
  37. package/dist/server/toolRegistry.js +47 -40
  38. package/dist/tools/__tests__/deltaTestUtils.d.ts +3 -3
  39. package/dist/tools/__tests__/deltaTestUtils.js +2 -2
  40. package/dist/tools/accountTools.d.ts +9 -8
  41. package/dist/tools/accountTools.js +47 -47
  42. package/dist/tools/adapters.d.ts +13 -8
  43. package/dist/tools/adapters.js +21 -11
  44. package/dist/tools/budgetTools.d.ts +8 -7
  45. package/dist/tools/budgetTools.js +22 -22
  46. package/dist/tools/categoryTools.d.ts +9 -8
  47. package/dist/tools/categoryTools.js +68 -59
  48. package/dist/tools/compareTransactions/formatter.d.ts +3 -3
  49. package/dist/tools/compareTransactions/formatter.js +9 -9
  50. package/dist/tools/compareTransactions/index.d.ts +6 -6
  51. package/dist/tools/compareTransactions/index.js +58 -43
  52. package/dist/tools/compareTransactions/matcher.d.ts +1 -1
  53. package/dist/tools/compareTransactions/matcher.js +28 -15
  54. package/dist/tools/compareTransactions/parser.d.ts +2 -2
  55. package/dist/tools/compareTransactions/parser.js +144 -138
  56. package/dist/tools/compareTransactions/types.d.ts +4 -4
  57. package/dist/tools/compareTransactions.d.ts +1 -1
  58. package/dist/tools/compareTransactions.js +1 -1
  59. package/dist/tools/deltaFetcher.d.ts +2 -2
  60. package/dist/tools/deltaFetcher.js +16 -15
  61. package/dist/tools/deltaSupport.d.ts +4 -4
  62. package/dist/tools/deltaSupport.js +35 -41
  63. package/dist/tools/exportTransactions.d.ts +5 -4
  64. package/dist/tools/exportTransactions.js +61 -59
  65. package/dist/tools/monthTools.d.ts +7 -6
  66. package/dist/tools/monthTools.js +31 -29
  67. package/dist/tools/payeeTools.d.ts +7 -6
  68. package/dist/tools/payeeTools.js +28 -28
  69. package/dist/tools/reconcileAdapter.d.ts +2 -2
  70. package/dist/tools/reconcileAdapter.js +19 -12
  71. package/dist/tools/reconciliation/analyzer.d.ts +4 -4
  72. package/dist/tools/reconciliation/analyzer.js +73 -59
  73. package/dist/tools/reconciliation/csvParser.d.ts +3 -3
  74. package/dist/tools/reconciliation/csvParser.js +128 -104
  75. package/dist/tools/reconciliation/executor.d.ts +4 -4
  76. package/dist/tools/reconciliation/executor.js +148 -109
  77. package/dist/tools/reconciliation/index.d.ts +10 -10
  78. package/dist/tools/reconciliation/index.js +96 -83
  79. package/dist/tools/reconciliation/matcher.d.ts +3 -3
  80. package/dist/tools/reconciliation/matcher.js +17 -16
  81. package/dist/tools/reconciliation/payeeNormalizer.js +19 -8
  82. package/dist/tools/reconciliation/recommendationEngine.d.ts +1 -1
  83. package/dist/tools/reconciliation/recommendationEngine.js +40 -40
  84. package/dist/tools/reconciliation/reportFormatter.d.ts +2 -2
  85. package/dist/tools/reconciliation/reportFormatter.js +59 -58
  86. package/dist/tools/reconciliation/signDetector.d.ts +1 -1
  87. package/dist/tools/reconciliation/types.d.ts +16 -16
  88. package/dist/tools/reconciliation/ynabAdapter.d.ts +2 -2
  89. package/dist/tools/schemas/common.d.ts +1 -1
  90. package/dist/tools/schemas/common.js +1 -1
  91. package/dist/tools/schemas/outputs/accountOutputs.d.ts +1 -1
  92. package/dist/tools/schemas/outputs/accountOutputs.js +24 -18
  93. package/dist/tools/schemas/outputs/budgetOutputs.d.ts +1 -1
  94. package/dist/tools/schemas/outputs/budgetOutputs.js +14 -11
  95. package/dist/tools/schemas/outputs/categoryOutputs.d.ts +1 -1
  96. package/dist/tools/schemas/outputs/categoryOutputs.js +49 -29
  97. package/dist/tools/schemas/outputs/comparisonOutputs.d.ts +1 -1
  98. package/dist/tools/schemas/outputs/comparisonOutputs.js +12 -12
  99. package/dist/tools/schemas/outputs/index.d.ts +14 -14
  100. package/dist/tools/schemas/outputs/index.js +14 -14
  101. package/dist/tools/schemas/outputs/monthOutputs.d.ts +1 -1
  102. package/dist/tools/schemas/outputs/monthOutputs.js +56 -41
  103. package/dist/tools/schemas/outputs/payeeOutputs.d.ts +1 -1
  104. package/dist/tools/schemas/outputs/payeeOutputs.js +10 -10
  105. package/dist/tools/schemas/outputs/reconciliationOutputs.d.ts +2 -2
  106. package/dist/tools/schemas/outputs/reconciliationOutputs.js +45 -45
  107. package/dist/tools/schemas/outputs/transactionMutationOutputs.d.ts +1 -1
  108. package/dist/tools/schemas/outputs/transactionMutationOutputs.js +28 -22
  109. package/dist/tools/schemas/outputs/transactionOutputs.d.ts +1 -1
  110. package/dist/tools/schemas/outputs/transactionOutputs.js +43 -35
  111. package/dist/tools/schemas/outputs/utilityOutputs.d.ts +1 -1
  112. package/dist/tools/schemas/outputs/utilityOutputs.js +5 -3
  113. package/dist/tools/schemas/shared/commonOutputs.d.ts +1 -1
  114. package/dist/tools/schemas/shared/commonOutputs.js +15 -9
  115. package/dist/tools/transactionReadTools.d.ts +11 -0
  116. package/dist/tools/transactionReadTools.js +202 -0
  117. package/dist/tools/transactionSchemas.d.ts +7 -7
  118. package/dist/tools/transactionSchemas.js +77 -57
  119. package/dist/tools/transactionTools.d.ts +6 -24
  120. package/dist/tools/transactionTools.js +7 -1499
  121. package/dist/tools/transactionUtils.d.ts +6 -6
  122. package/dist/tools/transactionUtils.js +78 -63
  123. package/dist/tools/transactionWriteTools.d.ts +20 -0
  124. package/dist/tools/transactionWriteTools.js +1342 -0
  125. package/dist/tools/utilityTools.d.ts +5 -4
  126. package/dist/tools/utilityTools.js +11 -11
  127. package/dist/types/index.d.ts +7 -7
  128. package/dist/types/index.js +6 -6
  129. package/dist/types/reconciliation.d.ts +1 -1
  130. package/dist/types/toolRegistration.d.ts +14 -12
  131. package/dist/utils/amountUtils.js +1 -1
  132. package/dist/utils/dateUtils.js +4 -4
  133. package/dist/utils/errors.d.ts +3 -3
  134. package/dist/utils/errors.js +4 -4
  135. package/dist/utils/money.d.ts +2 -2
  136. package/dist/utils/money.js +8 -8
  137. package/dist/utils/validationError.d.ts +1 -1
  138. package/dist/utils/validationError.js +1 -1
  139. package/docs/assets/examples/reconciliation-with-recommendations.json +66 -66
  140. package/docs/assets/schemas/reconciliation-v2.json +360 -336
  141. package/esbuild.config.mjs +53 -50
  142. package/meta.json +12548 -12548
  143. package/package.json +98 -111
  144. package/scripts/analyze-bundle.mjs +33 -30
  145. package/scripts/create-pr-description.js +169 -120
  146. package/scripts/run-all-tests.js +178 -169
  147. package/scripts/run-domain-integration-tests.js +28 -18
  148. package/scripts/run-generate-mcpb.js +19 -17
  149. package/scripts/run-throttled-integration-tests.js +92 -83
  150. package/scripts/test-delta-params.mjs +149 -120
  151. package/scripts/test-recommendations.ts +36 -32
  152. package/scripts/tmpTransaction.ts +80 -43
  153. package/scripts/validate-env.js +98 -91
  154. package/scripts/verify-build.js +78 -76
  155. package/src/__tests__/comprehensive.integration.test.ts +1281 -1154
  156. package/src/__tests__/performance.test.ts +723 -671
  157. package/src/__tests__/setup.ts +442 -395
  158. package/src/__tests__/smoke.e2e.test.ts +41 -39
  159. package/src/__tests__/testRunner.ts +314 -295
  160. package/src/__tests__/testUtils.ts +456 -364
  161. package/src/__tests__/tools/reconciliation/csvParser.integration.test.ts +109 -107
  162. package/src/__tests__/tools/reconciliation/real-world.integration.test.ts +41 -41
  163. package/src/index.ts +68 -59
  164. package/src/server/CLAUDE.md +480 -0
  165. package/src/server/YNABMCPServer.ts +821 -794
  166. package/src/server/__tests__/YNABMCPServer.integration.test.ts +929 -893
  167. package/src/server/__tests__/YNABMCPServer.test.ts +903 -899
  168. package/src/server/__tests__/budgetResolver.test.ts +466 -423
  169. package/src/server/__tests__/cacheManager.test.ts +891 -874
  170. package/src/server/__tests__/completions.integration.test.ts +115 -106
  171. package/src/server/__tests__/completions.test.ts +334 -313
  172. package/src/server/__tests__/config.test.ts +98 -86
  173. package/src/server/__tests__/deltaCache.merge.test.ts +774 -703
  174. package/src/server/__tests__/deltaCache.swr.test.ts +198 -153
  175. package/src/server/__tests__/deltaCache.test.ts +946 -759
  176. package/src/server/__tests__/diagnostics.test.ts +825 -792
  177. package/src/server/__tests__/errorHandler.integration.test.ts +512 -462
  178. package/src/server/__tests__/errorHandler.test.ts +402 -397
  179. package/src/server/__tests__/prompts.test.ts +424 -347
  180. package/src/server/__tests__/rateLimiter.test.ts +313 -309
  181. package/src/server/__tests__/requestLogger.test.ts +443 -403
  182. package/src/server/__tests__/resources.template.test.ts +196 -185
  183. package/src/server/__tests__/resources.test.ts +294 -288
  184. package/src/server/__tests__/security.integration.test.ts +487 -421
  185. package/src/server/__tests__/securityMiddleware.test.ts +519 -444
  186. package/src/server/__tests__/server-startup.integration.test.ts +509 -490
  187. package/src/server/__tests__/serverKnowledgeStore.test.ts +174 -173
  188. package/src/server/__tests__/toolRegistration.test.ts +239 -210
  189. package/src/server/__tests__/toolRegistry.test.ts +907 -845
  190. package/src/server/budgetResolver.ts +221 -181
  191. package/src/server/cacheKeys.ts +6 -6
  192. package/src/server/cacheManager.ts +498 -484
  193. package/src/server/completions.ts +267 -243
  194. package/src/server/config.ts +35 -14
  195. package/src/server/deltaCache.merge.ts +146 -128
  196. package/src/server/deltaCache.ts +352 -309
  197. package/src/server/diagnostics.ts +257 -242
  198. package/src/server/errorHandler.ts +747 -744
  199. package/src/server/prompts.ts +181 -176
  200. package/src/server/rateLimiter.ts +131 -129
  201. package/src/server/requestLogger.ts +350 -322
  202. package/src/server/resources.ts +442 -374
  203. package/src/server/responseFormatter.ts +41 -37
  204. package/src/server/securityMiddleware.ts +223 -205
  205. package/src/server/serverKnowledgeStore.ts +67 -67
  206. package/src/server/toolRegistry.ts +508 -474
  207. package/src/tools/CLAUDE.md +604 -0
  208. package/src/tools/__tests__/accountTools.delta.integration.test.ts +128 -111
  209. package/src/tools/__tests__/accountTools.integration.test.ts +129 -111
  210. package/src/tools/__tests__/accountTools.test.ts +685 -638
  211. package/src/tools/__tests__/adapters.test.ts +142 -108
  212. package/src/tools/__tests__/budgetTools.delta.integration.test.ts +73 -73
  213. package/src/tools/__tests__/budgetTools.integration.test.ts +132 -124
  214. package/src/tools/__tests__/budgetTools.test.ts +442 -413
  215. package/src/tools/__tests__/categoryTools.delta.integration.test.ts +76 -68
  216. package/src/tools/__tests__/categoryTools.integration.test.ts +314 -288
  217. package/src/tools/__tests__/categoryTools.test.ts +656 -625
  218. package/src/tools/__tests__/compareTransactions/formatter.test.ts +535 -462
  219. package/src/tools/__tests__/compareTransactions/index.test.ts +378 -358
  220. package/src/tools/__tests__/compareTransactions/matcher.test.ts +497 -398
  221. package/src/tools/__tests__/compareTransactions/parser.test.ts +765 -747
  222. package/src/tools/__tests__/compareTransactions.test.ts +352 -332
  223. package/src/tools/__tests__/compareTransactions.window.test.ts +150 -146
  224. package/src/tools/__tests__/deltaFetcher.scheduled.integration.test.ts +69 -65
  225. package/src/tools/__tests__/deltaFetcher.test.ts +325 -265
  226. package/src/tools/__tests__/deltaSupport.test.ts +211 -184
  227. package/src/tools/__tests__/deltaTestUtils.ts +37 -33
  228. package/src/tools/__tests__/exportTransactions.test.ts +205 -200
  229. package/src/tools/__tests__/monthTools.delta.integration.test.ts +68 -68
  230. package/src/tools/__tests__/monthTools.integration.test.ts +178 -166
  231. package/src/tools/__tests__/monthTools.test.ts +561 -512
  232. package/src/tools/__tests__/payeeTools.delta.integration.test.ts +68 -68
  233. package/src/tools/__tests__/payeeTools.integration.test.ts +158 -142
  234. package/src/tools/__tests__/payeeTools.test.ts +486 -434
  235. package/src/tools/__tests__/transactionSchemas.test.ts +1202 -1186
  236. package/src/tools/__tests__/transactionTools.integration.test.ts +875 -825
  237. package/src/tools/__tests__/transactionTools.test.ts +4923 -4366
  238. package/src/tools/__tests__/transactionUtils.test.ts +1004 -977
  239. package/src/tools/__tests__/utilityTools.integration.test.ts +32 -32
  240. package/src/tools/__tests__/utilityTools.test.ts +68 -58
  241. package/src/tools/accountTools.ts +293 -271
  242. package/src/tools/adapters.ts +120 -63
  243. package/src/tools/budgetTools.ts +121 -116
  244. package/src/tools/categoryTools.ts +379 -339
  245. package/src/tools/compareTransactions/formatter.ts +131 -119
  246. package/src/tools/compareTransactions/index.ts +249 -214
  247. package/src/tools/compareTransactions/matcher.ts +259 -209
  248. package/src/tools/compareTransactions/parser.ts +517 -487
  249. package/src/tools/compareTransactions/types.ts +38 -38
  250. package/src/tools/compareTransactions.ts +1 -1
  251. package/src/tools/deltaFetcher.ts +281 -260
  252. package/src/tools/deltaSupport.ts +264 -259
  253. package/src/tools/exportTransactions.ts +230 -218
  254. package/src/tools/monthTools.ts +180 -165
  255. package/src/tools/payeeTools.ts +152 -140
  256. package/src/tools/reconcileAdapter.ts +297 -252
  257. package/src/tools/reconciliation/CLAUDE.md +506 -0
  258. package/src/tools/reconciliation/__tests__/adapter.causes.test.ts +133 -124
  259. package/src/tools/reconciliation/__tests__/adapter.test.ts +249 -230
  260. package/src/tools/reconciliation/__tests__/analyzer.test.ts +408 -400
  261. package/src/tools/reconciliation/__tests__/csvParser.test.ts +71 -69
  262. package/src/tools/reconciliation/__tests__/executor.integration.test.ts +348 -323
  263. package/src/tools/reconciliation/__tests__/executor.progress.test.ts +503 -457
  264. package/src/tools/reconciliation/__tests__/executor.test.ts +898 -831
  265. package/src/tools/reconciliation/__tests__/matcher.test.ts +667 -663
  266. package/src/tools/reconciliation/__tests__/payeeNormalizer.test.ts +296 -276
  267. package/src/tools/reconciliation/__tests__/recommendationEngine.integration.test.ts +692 -624
  268. package/src/tools/reconciliation/__tests__/recommendationEngine.test.ts +1008 -989
  269. package/src/tools/reconciliation/__tests__/reconciliation.delta.integration.test.ts +187 -146
  270. package/src/tools/reconciliation/__tests__/reportFormatter.test.ts +583 -533
  271. package/src/tools/reconciliation/__tests__/scenarios/adapterCurrency.scenario.test.ts +75 -74
  272. package/src/tools/reconciliation/__tests__/scenarios/extremes.scenario.test.ts +70 -62
  273. package/src/tools/reconciliation/__tests__/scenarios/repeatAmount.scenario.test.ts +102 -88
  274. package/src/tools/reconciliation/__tests__/schemaUrl.test.ts +56 -55
  275. package/src/tools/reconciliation/__tests__/signDetector.test.ts +209 -206
  276. package/src/tools/reconciliation/__tests__/ynabAdapter.test.ts +66 -60
  277. package/src/tools/reconciliation/analyzer.ts +564 -504
  278. package/src/tools/reconciliation/csvParser.ts +656 -609
  279. package/src/tools/reconciliation/executor.ts +1290 -1128
  280. package/src/tools/reconciliation/index.ts +580 -528
  281. package/src/tools/reconciliation/matcher.ts +256 -240
  282. package/src/tools/reconciliation/payeeNormalizer.ts +92 -78
  283. package/src/tools/reconciliation/recommendationEngine.ts +357 -345
  284. package/src/tools/reconciliation/reportFormatter.ts +343 -307
  285. package/src/tools/reconciliation/signDetector.ts +89 -83
  286. package/src/tools/reconciliation/types.ts +164 -159
  287. package/src/tools/reconciliation/ynabAdapter.ts +17 -15
  288. package/src/tools/schemas/CLAUDE.md +546 -0
  289. package/src/tools/schemas/common.ts +1 -1
  290. package/src/tools/schemas/outputs/__tests__/accountOutputs.test.ts +410 -409
  291. package/src/tools/schemas/outputs/__tests__/budgetOutputs.test.ts +305 -299
  292. package/src/tools/schemas/outputs/__tests__/categoryOutputs.test.ts +431 -430
  293. package/src/tools/schemas/outputs/__tests__/comparisonOutputs.test.ts +510 -495
  294. package/src/tools/schemas/outputs/__tests__/dateValidation.test.ts +179 -153
  295. package/src/tools/schemas/outputs/__tests__/discrepancyDirection.test.ts +293 -254
  296. package/src/tools/schemas/outputs/__tests__/monthOutputs.test.ts +457 -457
  297. package/src/tools/schemas/outputs/__tests__/payeeOutputs.test.ts +362 -356
  298. package/src/tools/schemas/outputs/__tests__/reconciliationOutputs.test.ts +402 -399
  299. package/src/tools/schemas/outputs/__tests__/transactionMutationSchemas.test.ts +225 -211
  300. package/src/tools/schemas/outputs/__tests__/transactionOutputs.test.ts +457 -454
  301. package/src/tools/schemas/outputs/__tests__/utilityOutputs.test.ts +316 -315
  302. package/src/tools/schemas/outputs/accountOutputs.ts +40 -34
  303. package/src/tools/schemas/outputs/budgetOutputs.ts +24 -19
  304. package/src/tools/schemas/outputs/categoryOutputs.ts +76 -56
  305. package/src/tools/schemas/outputs/comparisonOutputs.ts +192 -169
  306. package/src/tools/schemas/outputs/index.ts +163 -163
  307. package/src/tools/schemas/outputs/monthOutputs.ts +95 -80
  308. package/src/tools/schemas/outputs/payeeOutputs.ts +18 -18
  309. package/src/tools/schemas/outputs/reconciliationOutputs.ts +386 -373
  310. package/src/tools/schemas/outputs/transactionMutationOutputs.ts +259 -231
  311. package/src/tools/schemas/outputs/transactionOutputs.ts +81 -71
  312. package/src/tools/schemas/outputs/utilityOutputs.ts +90 -84
  313. package/src/tools/schemas/shared/commonOutputs.ts +27 -19
  314. package/src/tools/toolCategories.ts +114 -114
  315. package/src/tools/transactionReadTools.ts +327 -0
  316. package/src/tools/transactionSchemas.ts +322 -291
  317. package/src/tools/transactionTools.ts +84 -2246
  318. package/src/tools/transactionUtils.ts +507 -422
  319. package/src/tools/transactionWriteTools.ts +2110 -0
  320. package/src/tools/utilityTools.ts +46 -41
  321. package/src/types/CLAUDE.md +477 -0
  322. package/src/types/__tests__/index.test.ts +51 -51
  323. package/src/types/index.ts +43 -39
  324. package/src/types/integration-tests.d.ts +26 -26
  325. package/src/types/reconciliation.ts +29 -29
  326. package/src/types/toolAnnotations.ts +30 -30
  327. package/src/types/toolRegistration.ts +43 -32
  328. package/src/utils/CLAUDE.md +508 -0
  329. package/src/utils/__tests__/dateUtils.test.ts +174 -168
  330. package/src/utils/__tests__/money.test.ts +193 -187
  331. package/src/utils/amountUtils.ts +5 -5
  332. package/src/utils/baseError.ts +5 -5
  333. package/src/utils/dateUtils.ts +29 -26
  334. package/src/utils/errors.ts +14 -14
  335. package/src/utils/money.ts +66 -52
  336. package/src/utils/validationError.ts +1 -1
  337. package/tsconfig.json +29 -29
  338. package/tsconfig.prod.json +16 -16
  339. package/vitest-reporters/split-json-reporter.ts +247 -204
  340. package/vitest.config.ts +99 -95
  341. package/.prettierignore +0 -10
  342. package/.prettierrc.json +0 -10
  343. package/eslint.config.js +0 -49
@@ -1,664 +1,668 @@
1
- import { describe, it, expect, beforeEach } from 'vitest';
2
- import { findBestMatch, findMatches } from '../matcher.js';
3
- import type { BankTransaction, YNABTransaction, MatchingConfig } from '../types.js';
4
-
5
- describe('matcher', () => {
6
- let config: MatchingConfig;
7
-
8
- beforeEach(() => {
9
- config = {
10
- weights: {
11
- amount: 0.5,
12
- date: 0.15,
13
- payee: 0.35,
14
- },
15
- amountToleranceMilliunits: 10,
16
- dateToleranceDays: 2,
17
- autoMatchThreshold: 90,
18
- suggestedMatchThreshold: 60,
19
- minimumCandidateScore: 40,
20
- exactAmountBonus: 10,
21
- exactDateBonus: 5,
22
- exactPayeeBonus: 10,
23
- };
24
- });
25
-
26
- describe('findBestMatch', () => {
27
- describe('high confidence matches (≥90%)', () => {
28
- it('should return high confidence for exact match', () => {
29
- const bankTxn: BankTransaction = {
30
- id: 'b1',
31
- date: '2025-10-15',
32
- amount: -45230, // milliunits (-45.23)
33
- payee: 'Shell Gas Station',
34
- original_csv_row: 2,
35
- };
36
-
37
- const ynabTxns: YNABTransaction[] = [
38
- {
39
- id: 'y1',
40
- date: '2025-10-15',
41
- amount: -45230, // milliunits
42
- payee_name: 'Shell Gas Station',
43
- category_name: 'Auto: Gas',
44
- cleared: 'uncleared',
45
- approved: true,
46
- },
47
- ];
48
-
49
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
50
-
51
- expect(match.confidence).toBe('high');
52
- expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
53
- expect(match.bestMatch?.ynabTransaction).toEqual(ynabTxns[0]);
54
- });
55
-
56
- it('should return high confidence for normalized payee match', () => {
57
- const bankTxn: BankTransaction = {
58
- id: 'b1',
59
- date: '2025-10-15',
60
- amount: -100000, // milliunits (-100.00)
61
- payee: 'NETFLIX.COM',
62
- original_csv_row: 2,
63
- };
64
-
65
- const ynabTxns: YNABTransaction[] = [
66
- {
67
- id: 'y1',
68
- date: '2025-10-15',
69
- amount: -100000,
70
- payee_name: 'Netflix Com',
71
- category_name: 'Entertainment',
72
- cleared: 'uncleared',
73
- approved: true,
74
- },
75
- ];
76
-
77
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
78
-
79
- expect(match.confidence).toBe('high');
80
- expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
81
- });
82
-
83
- it('should handle date within tolerance', () => {
84
- const bankTxn: BankTransaction = {
85
- id: 'b1',
86
- date: '2025-10-15',
87
- amount: -50000, // milliunits (-50.00)
88
- payee: 'Restaurant',
89
- original_csv_row: 2,
90
- };
91
-
92
- const ynabTxns: YNABTransaction[] = [
93
- {
94
- id: 'y1',
95
- date: '2025-10-14', // 1 day difference
96
- amount: -50000,
97
- payee: 'Restaurant',
98
- categoryName: 'Dining',
99
- cleared: 'uncleared',
100
- approved: true,
101
- },
102
- ];
103
-
104
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
105
-
106
- expect(match.confidence).toBe('high');
107
- expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
108
- });
109
-
110
- it('should return high confidence for fuzzy payee match', () => {
111
- const bankTxn: BankTransaction = {
112
- id: 'b1',
113
- date: '2025-10-20',
114
- amount: -127430, // milliunits (-127.43)
115
- payee: 'AMAZON.COM',
116
- original_csv_row: 2,
117
- };
118
-
119
- const ynabTxns: YNABTransaction[] = [
120
- {
121
- id: 'y1',
122
- date: '2025-10-20',
123
- amount: -127430,
124
- payee_name: 'Amazon Prime',
125
- category_name: 'Shopping',
126
- cleared: 'uncleared',
127
- approved: true,
128
- },
129
- ];
130
-
131
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
132
-
133
- expect(match.confidence).toBe('high');
134
- expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
135
- expect(match.candidates).toBeDefined();
136
- expect(match.candidates!.length).toBeGreaterThan(0);
137
- });
138
- });
139
-
140
- describe('medium confidence matches (60-89%)', () => {
141
- it('should provide multiple candidates for medium confidence', () => {
142
- const bankTxn: BankTransaction = {
143
- id: 'b1',
144
- date: '2025-10-15',
145
- amount: -50000,
146
- payee: 'Restaurant',
147
- original_csv_row: 2,
148
- };
149
-
150
- const ynabTxns: YNABTransaction[] = [
151
- {
152
- id: 'y1',
153
- date: '2025-10-15',
154
- amount: -50000,
155
- payee_name: 'Italian Restaurant',
156
- category_name: 'Dining',
157
- cleared: 'uncleared',
158
- approved: true,
159
- },
160
- {
161
- id: 'y2',
162
- date: '2025-10-16',
163
- amount: -50000,
164
- payee_name: 'Chinese Restaurant',
165
- category_name: 'Dining',
166
- cleared: 'uncleared',
167
- approved: true,
168
- },
169
- ];
170
-
171
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
172
-
173
- expect(match.candidates).toBeDefined();
174
- expect(match.candidates!.length).toBeGreaterThan(0);
175
- expect(match.candidates!.length).toBeLessThanOrEqual(3); // Top 3
176
- });
177
- });
178
-
179
- describe('low/no confidence matches', () => {
180
- it('should return no match when amount differs', () => {
181
- const bankTxn: BankTransaction = {
182
- id: 'b1',
183
- date: '2025-10-15',
184
- amount: -45230,
185
- payee: 'Shell',
186
- original_csv_row: 2,
187
- };
188
-
189
- const ynabTxns: YNABTransaction[] = [
190
- {
191
- id: 'y1',
192
- date: '2025-10-15',
193
- amount: -100000, // Different amount
194
- payee_name: 'Shell',
195
- category_name: 'Auto: Gas',
196
- cleared: 'uncleared',
197
- approved: true,
198
- },
199
- ];
200
-
201
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
202
-
203
- expect(match.confidence).toBe('none');
204
- expect(match.confidenceScore).toBe(0);
205
- expect(match.bestMatch).toBeNull();
206
- });
207
-
208
- it('should not match opposite-signed transactions', () => {
209
- const bankTxn: BankTransaction = {
210
- id: 'b1',
211
- date: '2025-10-15',
212
- amount: 50000, // Positive (refund) in milliunits
213
- payee: 'Amazon',
214
- original_csv_row: 2,
215
- };
216
-
217
- const ynabTxns: YNABTransaction[] = [
218
- {
219
- id: 'y1',
220
- date: '2025-10-15',
221
- amount: -50000, // Negative (purchase)
222
- payee_name: 'Amazon',
223
- category_name: 'Shopping',
224
- cleared: 'uncleared',
225
- approved: true,
226
- },
227
- ];
228
-
229
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
230
-
231
- expect(match.confidence).toBe('none');
232
- expect(match.bestMatch).toBeNull();
233
- });
234
- });
235
-
236
- describe('prioritization', () => {
237
- it('should prioritize uncleared transactions over cleared', () => {
238
- const bankTxn: BankTransaction = {
239
- id: 'b1',
240
- date: '2025-10-15',
241
- amount: -50000,
242
- payee: 'Coffee Shop',
243
- original_csv_row: 2,
244
- };
245
-
246
- const ynabTxns: YNABTransaction[] = [
247
- {
248
- id: 'y1',
249
- date: '2025-10-15',
250
- amount: -50000,
251
- payee_name: 'Coffee Shop',
252
- category_name: 'Dining',
253
- cleared: 'cleared',
254
- approved: true,
255
- },
256
- {
257
- id: 'y2',
258
- date: '2025-10-15',
259
- amount: -50000,
260
- payee_name: 'Coffee Shop',
261
- category_name: 'Dining',
262
- cleared: 'uncleared',
263
- approved: true,
264
- },
265
- ];
266
-
267
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
268
-
269
- // Should prefer uncleared transaction
270
- expect(match.bestMatch?.ynabTransaction.id).toBe('y2');
271
- });
272
-
273
- it('should use date proximity as tiebreaker', () => {
274
- const bankTxn: BankTransaction = {
275
- id: 'b1',
276
- date: '2025-10-15',
277
- amount: -50000,
278
- payee: 'Store',
279
- original_csv_row: 2,
280
- };
281
-
282
- const ynabTxns: YNABTransaction[] = [
283
- {
284
- id: 'y1',
285
- date: '2025-10-13', // 2 days away
286
- amount: -50000,
287
- payee_name: 'Store',
288
- category_name: 'Shopping',
289
- cleared: 'uncleared',
290
- approved: true,
291
- },
292
- {
293
- id: 'y2',
294
- date: '2025-10-14', // 1 day away
295
- amount: -50000,
296
- payee_name: 'Store',
297
- category_name: 'Shopping',
298
- cleared: 'uncleared',
299
- approved: true,
300
- },
301
- ];
302
-
303
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
304
-
305
- // Should prefer closer date
306
- expect(match.bestMatch?.ynabTransaction.id).toBe('y2');
307
- });
308
- });
309
-
310
- describe('amount tolerance', () => {
311
- it('should match within amount tolerance', () => {
312
- const bankTxn: BankTransaction = {
313
- id: 'b1',
314
- date: '2025-10-15',
315
- amount: -45230,
316
- payee: 'Shell',
317
- original_csv_row: 2,
318
- };
319
-
320
- const ynabTxns: YNABTransaction[] = [
321
- {
322
- id: 'y1',
323
- date: '2025-10-15',
324
- amount: -45240, // $45.24 - within 1 cent tolerance
325
- payee_name: 'Shell',
326
- category_name: 'Auto: Gas',
327
- cleared: 'uncleared',
328
- approved: true,
329
- },
330
- ];
331
-
332
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
333
-
334
- expect(match.confidence).not.toBe('none');
335
- expect(match.bestMatch?.ynabTransaction).toBeDefined();
336
- });
337
-
338
- it('should not match outside amount tolerance', () => {
339
- config.amountToleranceMilliunits = 10; // 1 cent
340
-
341
- const bankTxn: BankTransaction = {
342
- id: 'b1',
343
- date: '2025-10-15',
344
- amount: -45000,
345
- payee: 'Shell',
346
- original_csv_row: 2,
347
- };
348
-
349
- const ynabTxns: YNABTransaction[] = [
350
- {
351
- id: 'y1',
352
- date: '2025-10-15',
353
- amount: -45050, // $45.05 - outside 1 cent tolerance
354
- payee_name: 'Shell',
355
- category_name: 'Auto: Gas',
356
- cleared: 'uncleared',
357
- approved: true,
358
- },
359
- ];
360
-
361
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
362
-
363
- expect(match.confidence).toBe('none');
364
- });
365
- });
366
-
367
- describe('used IDs', () => {
368
- it('should skip already-used YNAB transaction IDs', () => {
369
- const bankTxn: BankTransaction = {
370
- id: 'b1',
371
- date: '2025-10-15',
372
- amount: -50000,
373
- payee: 'Store',
374
- original_csv_row: 2,
375
- };
376
-
377
- const ynabTxns: YNABTransaction[] = [
378
- {
379
- id: 'y1',
380
- date: '2025-10-15',
381
- amount: -50000,
382
- payee_name: 'Store',
383
- category_name: 'Shopping',
384
- cleared: 'uncleared',
385
- approved: true,
386
- },
387
- ];
388
-
389
- const usedIds = new Set(['y1']);
390
- const match = findBestMatch(bankTxn, ynabTxns, usedIds, config);
391
-
392
- expect(match.confidence).toBe('none');
393
- expect(match.bestMatch).toBeNull();
394
- });
395
- });
396
- });
397
-
398
- describe('findMatches', () => {
399
- it('should match multiple bank transactions', () => {
400
- const bankTxns: BankTransaction[] = [
401
- {
402
- id: 'b1',
403
- date: '2025-10-15',
404
- amount: -45230,
405
- payee: 'Shell',
406
- original_csv_row: 2,
407
- },
408
- {
409
- id: 'b2',
410
- date: '2025-10-16',
411
- amount: -100000,
412
- payee: 'Netflix',
413
- original_csv_row: 3,
414
- },
415
- ];
416
-
417
- const ynabTxns: YNABTransaction[] = [
418
- {
419
- id: 'y1',
420
- date: '2025-10-15',
421
- amount: -45230,
422
- payee_name: 'Shell Gas',
423
- category_name: 'Auto: Gas',
424
- cleared: 'uncleared',
425
- approved: true,
426
- },
427
- {
428
- id: 'y2',
429
- date: '2025-10-16',
430
- amount: -100000,
431
- payee_name: 'Netflix',
432
- category_name: 'Entertainment',
433
- cleared: 'uncleared',
434
- approved: true,
435
- },
436
- ];
437
-
438
- const matches = findMatches(bankTxns, ynabTxns, config);
439
-
440
- expect(matches).toHaveLength(2);
441
- expect(matches[0].bankTransaction.id).toBe('b1');
442
- expect(matches[1].bankTransaction.id).toBe('b2');
443
- });
444
-
445
- it('should prevent duplicate matching of YNAB transactions', () => {
446
- const bankTxns: BankTransaction[] = [
447
- {
448
- id: 'b1',
449
- date: '2025-10-15',
450
- amount: -50000,
451
- payee: 'Store',
452
- original_csv_row: 2,
453
- },
454
- {
455
- id: 'b2',
456
- date: '2025-10-15',
457
- amount: -50000,
458
- payee: 'Store',
459
- original_csv_row: 3,
460
- },
461
- ];
462
-
463
- const ynabTxns: YNABTransaction[] = [
464
- {
465
- id: 'y1',
466
- date: '2025-10-15',
467
- amount: -50000,
468
- payee_name: 'Store',
469
- category_name: 'Shopping',
470
- cleared: 'uncleared',
471
- approved: true,
472
- },
473
- ];
474
-
475
- const matches = findMatches(bankTxns, ynabTxns, config);
476
-
477
- expect(matches).toHaveLength(2);
478
-
479
- // First should match
480
- expect(matches[0].confidence).toBe('high');
481
- expect(matches[0].bestMatch?.ynabTransaction.id).toBe('y1');
482
-
483
- // Second should not match (y1 already used)
484
- expect(matches[1].confidence).toBe('none');
485
- });
486
-
487
- it('should handle mix of matched and unmatched transactions', () => {
488
- const bankTxns: BankTransaction[] = [
489
- {
490
- id: 'b1',
491
- date: '2025-10-15',
492
- amount: -45230,
493
- payee: 'Shell',
494
- original_csv_row: 2,
495
- },
496
- {
497
- id: 'b2',
498
- date: '2025-10-16',
499
- amount: -15990,
500
- payee: 'NewStore',
501
- original_csv_row: 3,
502
- },
503
- ];
504
-
505
- const ynabTxns: YNABTransaction[] = [
506
- {
507
- id: 'y1',
508
- date: '2025-10-15',
509
- amount: -45230,
510
- payee_name: 'Shell',
511
- category_name: 'Auto: Gas',
512
- cleared: 'uncleared',
513
- approved: true,
514
- },
515
- ];
516
-
517
- const matches = findMatches(bankTxns, ynabTxns, config);
518
-
519
- expect(matches).toHaveLength(2);
520
- expect(matches[0].confidence).toBe('high');
521
- expect(matches[1].confidence).toBe('none');
522
- expect(matches[1].bestMatch).toBeNull();
523
- });
524
-
525
- it('should use custom configuration', () => {
526
- const customConfig: MatchingConfig = {
527
- weights: {
528
- amount: 0.5,
529
- date: 0.15,
530
- payee: 0.35,
531
- },
532
- amountToleranceMilliunits: 100, // 10 cents
533
- dateToleranceDays: 5,
534
- autoMatchThreshold: 85,
535
- suggestedMatchThreshold: 50,
536
- minimumCandidateScore: 40,
537
- exactAmountBonus: 10,
538
- exactDateBonus: 5,
539
- exactPayeeBonus: 10,
540
- };
541
-
542
- const bankTxns: BankTransaction[] = [
543
- {
544
- id: 'b1',
545
- date: '2025-10-15',
546
- amount: -50000,
547
- payee: 'Store',
548
- original_csv_row: 2,
549
- },
550
- ];
551
-
552
- const ynabTxns: YNABTransaction[] = [
553
- {
554
- id: 'y1',
555
- date: '2025-10-11', // 4 days difference (within custom tolerance)
556
- amount: -50090, // $50.09 (within custom tolerance)
557
- payee_name: 'Store',
558
- category_name: 'Shopping',
559
- cleared: 'uncleared',
560
- approved: true,
561
- },
562
- ];
563
-
564
- const matches = findMatches(bankTxns, ynabTxns, customConfig);
565
-
566
- expect(matches[0].confidence).not.toBe('none');
567
- });
568
- });
569
-
570
- describe('edge cases', () => {
571
- it('should handle empty YNAB transactions list', () => {
572
- const bankTxn: BankTransaction = {
573
- id: 'b1',
574
- date: '2025-10-15',
575
- amount: -50.0,
576
- payee: 'Store',
577
- original_csv_row: 2,
578
- };
579
-
580
- const match = findBestMatch(bankTxn, [], new Set(), config);
581
-
582
- expect(match.confidence).toBe('none');
583
- expect(match.bestMatch).toBeNull();
584
- });
585
-
586
- it('should handle null payee names', () => {
587
- const bankTxn: BankTransaction = {
588
- id: 'b1',
589
- date: '2025-10-15',
590
- amount: -50000,
591
- payee: 'Store',
592
- original_csv_row: 2,
593
- };
594
-
595
- const ynabTxns: YNABTransaction[] = [
596
- {
597
- id: 'y1',
598
- date: '2025-10-15',
599
- amount: -50000,
600
- payee_name: null,
601
- category_name: 'Shopping',
602
- cleared: 'uncleared',
603
- approved: true,
604
- },
605
- ];
606
-
607
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
608
-
609
- // Should still match based on amount and date
610
- expect(match.confidence).not.toBe('none');
611
- });
612
-
613
- it('should handle very small amounts', () => {
614
- const bankTxn: BankTransaction = {
615
- id: 'b1',
616
- date: '2025-10-15',
617
- amount: -10, // 1 cent in milliunits
618
- payee: 'Micro Transaction',
619
- original_csv_row: 2,
620
- };
621
-
622
- const ynabTxns: YNABTransaction[] = [
623
- {
624
- id: 'y1',
625
- date: '2025-10-15',
626
- amount: -10, // 1 cent in milliunits
627
- payee_name: 'Micro Transaction',
628
- category_name: 'Misc',
629
- cleared: 'uncleared',
630
- approved: true,
631
- },
632
- ];
633
-
634
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
635
- expect(match.confidence).toBe('high');
636
- });
637
-
638
- it('should handle large amounts', () => {
639
- const bankTxn: BankTransaction = {
640
- id: 'b1',
641
- date: '2025-10-15',
642
- amount: -10000000, // $10,000 in milliunits
643
- payee: 'Large Purchase',
644
- original_csv_row: 2,
645
- };
646
-
647
- const ynabTxns: YNABTransaction[] = [
648
- {
649
- id: 'y1',
650
- date: '2025-10-15',
651
- amount: -10000000, // $10,000 in milliunits
652
- payee_name: 'Large Purchase',
653
- category_name: 'Shopping',
654
- cleared: 'uncleared',
655
- approved: true,
656
- },
657
- ];
658
-
659
- const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
660
-
661
- expect(match.confidence).toBe('high');
662
- });
663
- });
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+ import { findBestMatch, findMatches } from "../matcher.js";
3
+ import type {
4
+ BankTransaction,
5
+ MatchingConfig,
6
+ YNABTransaction,
7
+ } from "../types.js";
8
+
9
+ describe("matcher", () => {
10
+ let config: MatchingConfig;
11
+
12
+ beforeEach(() => {
13
+ config = {
14
+ weights: {
15
+ amount: 0.5,
16
+ date: 0.15,
17
+ payee: 0.35,
18
+ },
19
+ amountToleranceMilliunits: 10,
20
+ dateToleranceDays: 2,
21
+ autoMatchThreshold: 90,
22
+ suggestedMatchThreshold: 60,
23
+ minimumCandidateScore: 40,
24
+ exactAmountBonus: 10,
25
+ exactDateBonus: 5,
26
+ exactPayeeBonus: 10,
27
+ };
28
+ });
29
+
30
+ describe("findBestMatch", () => {
31
+ describe("high confidence matches (≥90%)", () => {
32
+ it("should return high confidence for exact match", () => {
33
+ const bankTxn: BankTransaction = {
34
+ id: "b1",
35
+ date: "2025-10-15",
36
+ amount: -45230, // milliunits (-45.23)
37
+ payee: "Shell Gas Station",
38
+ original_csv_row: 2,
39
+ };
40
+
41
+ const ynabTxns: YNABTransaction[] = [
42
+ {
43
+ id: "y1",
44
+ date: "2025-10-15",
45
+ amount: -45230, // milliunits
46
+ payee_name: "Shell Gas Station",
47
+ category_name: "Auto: Gas",
48
+ cleared: "uncleared",
49
+ approved: true,
50
+ },
51
+ ];
52
+
53
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
54
+
55
+ expect(match.confidence).toBe("high");
56
+ expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
57
+ expect(match.bestMatch?.ynabTransaction).toEqual(ynabTxns[0]);
58
+ });
59
+
60
+ it("should return high confidence for normalized payee match", () => {
61
+ const bankTxn: BankTransaction = {
62
+ id: "b1",
63
+ date: "2025-10-15",
64
+ amount: -100000, // milliunits (-100.00)
65
+ payee: "NETFLIX.COM",
66
+ original_csv_row: 2,
67
+ };
68
+
69
+ const ynabTxns: YNABTransaction[] = [
70
+ {
71
+ id: "y1",
72
+ date: "2025-10-15",
73
+ amount: -100000,
74
+ payee_name: "Netflix Com",
75
+ category_name: "Entertainment",
76
+ cleared: "uncleared",
77
+ approved: true,
78
+ },
79
+ ];
80
+
81
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
82
+
83
+ expect(match.confidence).toBe("high");
84
+ expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
85
+ });
86
+
87
+ it("should handle date within tolerance", () => {
88
+ const bankTxn: BankTransaction = {
89
+ id: "b1",
90
+ date: "2025-10-15",
91
+ amount: -50000, // milliunits (-50.00)
92
+ payee: "Restaurant",
93
+ original_csv_row: 2,
94
+ };
95
+
96
+ const ynabTxns: YNABTransaction[] = [
97
+ {
98
+ id: "y1",
99
+ date: "2025-10-14", // 1 day difference
100
+ amount: -50000,
101
+ payee: "Restaurant",
102
+ categoryName: "Dining",
103
+ cleared: "uncleared",
104
+ approved: true,
105
+ },
106
+ ];
107
+
108
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
109
+
110
+ expect(match.confidence).toBe("high");
111
+ expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
112
+ });
113
+
114
+ it("should return high confidence for fuzzy payee match", () => {
115
+ const bankTxn: BankTransaction = {
116
+ id: "b1",
117
+ date: "2025-10-20",
118
+ amount: -127430, // milliunits (-127.43)
119
+ payee: "AMAZON.COM",
120
+ original_csv_row: 2,
121
+ };
122
+
123
+ const ynabTxns: YNABTransaction[] = [
124
+ {
125
+ id: "y1",
126
+ date: "2025-10-20",
127
+ amount: -127430,
128
+ payee_name: "Amazon Prime",
129
+ category_name: "Shopping",
130
+ cleared: "uncleared",
131
+ approved: true,
132
+ },
133
+ ];
134
+
135
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
136
+
137
+ expect(match.confidence).toBe("high");
138
+ expect(match.confidenceScore).toBeGreaterThanOrEqual(90);
139
+ expect(match.candidates).toBeDefined();
140
+ expect(match.candidates?.length).toBeGreaterThan(0);
141
+ });
142
+ });
143
+
144
+ describe("medium confidence matches (60-89%)", () => {
145
+ it("should provide multiple candidates for medium confidence", () => {
146
+ const bankTxn: BankTransaction = {
147
+ id: "b1",
148
+ date: "2025-10-15",
149
+ amount: -50000,
150
+ payee: "Restaurant",
151
+ original_csv_row: 2,
152
+ };
153
+
154
+ const ynabTxns: YNABTransaction[] = [
155
+ {
156
+ id: "y1",
157
+ date: "2025-10-15",
158
+ amount: -50000,
159
+ payee_name: "Italian Restaurant",
160
+ category_name: "Dining",
161
+ cleared: "uncleared",
162
+ approved: true,
163
+ },
164
+ {
165
+ id: "y2",
166
+ date: "2025-10-16",
167
+ amount: -50000,
168
+ payee_name: "Chinese Restaurant",
169
+ category_name: "Dining",
170
+ cleared: "uncleared",
171
+ approved: true,
172
+ },
173
+ ];
174
+
175
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
176
+
177
+ expect(match.candidates).toBeDefined();
178
+ expect(match.candidates?.length).toBeGreaterThan(0);
179
+ expect(match.candidates?.length).toBeLessThanOrEqual(3); // Top 3
180
+ });
181
+ });
182
+
183
+ describe("low/no confidence matches", () => {
184
+ it("should return no match when amount differs", () => {
185
+ const bankTxn: BankTransaction = {
186
+ id: "b1",
187
+ date: "2025-10-15",
188
+ amount: -45230,
189
+ payee: "Shell",
190
+ original_csv_row: 2,
191
+ };
192
+
193
+ const ynabTxns: YNABTransaction[] = [
194
+ {
195
+ id: "y1",
196
+ date: "2025-10-15",
197
+ amount: -100000, // Different amount
198
+ payee_name: "Shell",
199
+ category_name: "Auto: Gas",
200
+ cleared: "uncleared",
201
+ approved: true,
202
+ },
203
+ ];
204
+
205
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
206
+
207
+ expect(match.confidence).toBe("none");
208
+ expect(match.confidenceScore).toBe(0);
209
+ expect(match.bestMatch).toBeNull();
210
+ });
211
+
212
+ it("should not match opposite-signed transactions", () => {
213
+ const bankTxn: BankTransaction = {
214
+ id: "b1",
215
+ date: "2025-10-15",
216
+ amount: 50000, // Positive (refund) in milliunits
217
+ payee: "Amazon",
218
+ original_csv_row: 2,
219
+ };
220
+
221
+ const ynabTxns: YNABTransaction[] = [
222
+ {
223
+ id: "y1",
224
+ date: "2025-10-15",
225
+ amount: -50000, // Negative (purchase)
226
+ payee_name: "Amazon",
227
+ category_name: "Shopping",
228
+ cleared: "uncleared",
229
+ approved: true,
230
+ },
231
+ ];
232
+
233
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
234
+
235
+ expect(match.confidence).toBe("none");
236
+ expect(match.bestMatch).toBeNull();
237
+ });
238
+ });
239
+
240
+ describe("prioritization", () => {
241
+ it("should prioritize uncleared transactions over cleared", () => {
242
+ const bankTxn: BankTransaction = {
243
+ id: "b1",
244
+ date: "2025-10-15",
245
+ amount: -50000,
246
+ payee: "Coffee Shop",
247
+ original_csv_row: 2,
248
+ };
249
+
250
+ const ynabTxns: YNABTransaction[] = [
251
+ {
252
+ id: "y1",
253
+ date: "2025-10-15",
254
+ amount: -50000,
255
+ payee_name: "Coffee Shop",
256
+ category_name: "Dining",
257
+ cleared: "cleared",
258
+ approved: true,
259
+ },
260
+ {
261
+ id: "y2",
262
+ date: "2025-10-15",
263
+ amount: -50000,
264
+ payee_name: "Coffee Shop",
265
+ category_name: "Dining",
266
+ cleared: "uncleared",
267
+ approved: true,
268
+ },
269
+ ];
270
+
271
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
272
+
273
+ // Should prefer uncleared transaction
274
+ expect(match.bestMatch?.ynabTransaction.id).toBe("y2");
275
+ });
276
+
277
+ it("should use date proximity as tiebreaker", () => {
278
+ const bankTxn: BankTransaction = {
279
+ id: "b1",
280
+ date: "2025-10-15",
281
+ amount: -50000,
282
+ payee: "Store",
283
+ original_csv_row: 2,
284
+ };
285
+
286
+ const ynabTxns: YNABTransaction[] = [
287
+ {
288
+ id: "y1",
289
+ date: "2025-10-13", // 2 days away
290
+ amount: -50000,
291
+ payee_name: "Store",
292
+ category_name: "Shopping",
293
+ cleared: "uncleared",
294
+ approved: true,
295
+ },
296
+ {
297
+ id: "y2",
298
+ date: "2025-10-14", // 1 day away
299
+ amount: -50000,
300
+ payee_name: "Store",
301
+ category_name: "Shopping",
302
+ cleared: "uncleared",
303
+ approved: true,
304
+ },
305
+ ];
306
+
307
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
308
+
309
+ // Should prefer closer date
310
+ expect(match.bestMatch?.ynabTransaction.id).toBe("y2");
311
+ });
312
+ });
313
+
314
+ describe("amount tolerance", () => {
315
+ it("should match within amount tolerance", () => {
316
+ const bankTxn: BankTransaction = {
317
+ id: "b1",
318
+ date: "2025-10-15",
319
+ amount: -45230,
320
+ payee: "Shell",
321
+ original_csv_row: 2,
322
+ };
323
+
324
+ const ynabTxns: YNABTransaction[] = [
325
+ {
326
+ id: "y1",
327
+ date: "2025-10-15",
328
+ amount: -45240, // $45.24 - within 1 cent tolerance
329
+ payee_name: "Shell",
330
+ category_name: "Auto: Gas",
331
+ cleared: "uncleared",
332
+ approved: true,
333
+ },
334
+ ];
335
+
336
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
337
+
338
+ expect(match.confidence).not.toBe("none");
339
+ expect(match.bestMatch?.ynabTransaction).toBeDefined();
340
+ });
341
+
342
+ it("should not match outside amount tolerance", () => {
343
+ config.amountToleranceMilliunits = 10; // 1 cent
344
+
345
+ const bankTxn: BankTransaction = {
346
+ id: "b1",
347
+ date: "2025-10-15",
348
+ amount: -45000,
349
+ payee: "Shell",
350
+ original_csv_row: 2,
351
+ };
352
+
353
+ const ynabTxns: YNABTransaction[] = [
354
+ {
355
+ id: "y1",
356
+ date: "2025-10-15",
357
+ amount: -45050, // $45.05 - outside 1 cent tolerance
358
+ payee_name: "Shell",
359
+ category_name: "Auto: Gas",
360
+ cleared: "uncleared",
361
+ approved: true,
362
+ },
363
+ ];
364
+
365
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
366
+
367
+ expect(match.confidence).toBe("none");
368
+ });
369
+ });
370
+
371
+ describe("used IDs", () => {
372
+ it("should skip already-used YNAB transaction IDs", () => {
373
+ const bankTxn: BankTransaction = {
374
+ id: "b1",
375
+ date: "2025-10-15",
376
+ amount: -50000,
377
+ payee: "Store",
378
+ original_csv_row: 2,
379
+ };
380
+
381
+ const ynabTxns: YNABTransaction[] = [
382
+ {
383
+ id: "y1",
384
+ date: "2025-10-15",
385
+ amount: -50000,
386
+ payee_name: "Store",
387
+ category_name: "Shopping",
388
+ cleared: "uncleared",
389
+ approved: true,
390
+ },
391
+ ];
392
+
393
+ const usedIds = new Set(["y1"]);
394
+ const match = findBestMatch(bankTxn, ynabTxns, usedIds, config);
395
+
396
+ expect(match.confidence).toBe("none");
397
+ expect(match.bestMatch).toBeNull();
398
+ });
399
+ });
400
+ });
401
+
402
+ describe("findMatches", () => {
403
+ it("should match multiple bank transactions", () => {
404
+ const bankTxns: BankTransaction[] = [
405
+ {
406
+ id: "b1",
407
+ date: "2025-10-15",
408
+ amount: -45230,
409
+ payee: "Shell",
410
+ original_csv_row: 2,
411
+ },
412
+ {
413
+ id: "b2",
414
+ date: "2025-10-16",
415
+ amount: -100000,
416
+ payee: "Netflix",
417
+ original_csv_row: 3,
418
+ },
419
+ ];
420
+
421
+ const ynabTxns: YNABTransaction[] = [
422
+ {
423
+ id: "y1",
424
+ date: "2025-10-15",
425
+ amount: -45230,
426
+ payee_name: "Shell Gas",
427
+ category_name: "Auto: Gas",
428
+ cleared: "uncleared",
429
+ approved: true,
430
+ },
431
+ {
432
+ id: "y2",
433
+ date: "2025-10-16",
434
+ amount: -100000,
435
+ payee_name: "Netflix",
436
+ category_name: "Entertainment",
437
+ cleared: "uncleared",
438
+ approved: true,
439
+ },
440
+ ];
441
+
442
+ const matches = findMatches(bankTxns, ynabTxns, config);
443
+
444
+ expect(matches).toHaveLength(2);
445
+ expect(matches[0].bankTransaction.id).toBe("b1");
446
+ expect(matches[1].bankTransaction.id).toBe("b2");
447
+ });
448
+
449
+ it("should prevent duplicate matching of YNAB transactions", () => {
450
+ const bankTxns: BankTransaction[] = [
451
+ {
452
+ id: "b1",
453
+ date: "2025-10-15",
454
+ amount: -50000,
455
+ payee: "Store",
456
+ original_csv_row: 2,
457
+ },
458
+ {
459
+ id: "b2",
460
+ date: "2025-10-15",
461
+ amount: -50000,
462
+ payee: "Store",
463
+ original_csv_row: 3,
464
+ },
465
+ ];
466
+
467
+ const ynabTxns: YNABTransaction[] = [
468
+ {
469
+ id: "y1",
470
+ date: "2025-10-15",
471
+ amount: -50000,
472
+ payee_name: "Store",
473
+ category_name: "Shopping",
474
+ cleared: "uncleared",
475
+ approved: true,
476
+ },
477
+ ];
478
+
479
+ const matches = findMatches(bankTxns, ynabTxns, config);
480
+
481
+ expect(matches).toHaveLength(2);
482
+
483
+ // First should match
484
+ expect(matches[0].confidence).toBe("high");
485
+ expect(matches[0].bestMatch?.ynabTransaction.id).toBe("y1");
486
+
487
+ // Second should not match (y1 already used)
488
+ expect(matches[1].confidence).toBe("none");
489
+ });
490
+
491
+ it("should handle mix of matched and unmatched transactions", () => {
492
+ const bankTxns: BankTransaction[] = [
493
+ {
494
+ id: "b1",
495
+ date: "2025-10-15",
496
+ amount: -45230,
497
+ payee: "Shell",
498
+ original_csv_row: 2,
499
+ },
500
+ {
501
+ id: "b2",
502
+ date: "2025-10-16",
503
+ amount: -15990,
504
+ payee: "NewStore",
505
+ original_csv_row: 3,
506
+ },
507
+ ];
508
+
509
+ const ynabTxns: YNABTransaction[] = [
510
+ {
511
+ id: "y1",
512
+ date: "2025-10-15",
513
+ amount: -45230,
514
+ payee_name: "Shell",
515
+ category_name: "Auto: Gas",
516
+ cleared: "uncleared",
517
+ approved: true,
518
+ },
519
+ ];
520
+
521
+ const matches = findMatches(bankTxns, ynabTxns, config);
522
+
523
+ expect(matches).toHaveLength(2);
524
+ expect(matches[0].confidence).toBe("high");
525
+ expect(matches[1].confidence).toBe("none");
526
+ expect(matches[1].bestMatch).toBeNull();
527
+ });
528
+
529
+ it("should use custom configuration", () => {
530
+ const customConfig: MatchingConfig = {
531
+ weights: {
532
+ amount: 0.5,
533
+ date: 0.15,
534
+ payee: 0.35,
535
+ },
536
+ amountToleranceMilliunits: 100, // 10 cents
537
+ dateToleranceDays: 5,
538
+ autoMatchThreshold: 85,
539
+ suggestedMatchThreshold: 50,
540
+ minimumCandidateScore: 40,
541
+ exactAmountBonus: 10,
542
+ exactDateBonus: 5,
543
+ exactPayeeBonus: 10,
544
+ };
545
+
546
+ const bankTxns: BankTransaction[] = [
547
+ {
548
+ id: "b1",
549
+ date: "2025-10-15",
550
+ amount: -50000,
551
+ payee: "Store",
552
+ original_csv_row: 2,
553
+ },
554
+ ];
555
+
556
+ const ynabTxns: YNABTransaction[] = [
557
+ {
558
+ id: "y1",
559
+ date: "2025-10-11", // 4 days difference (within custom tolerance)
560
+ amount: -50090, // $50.09 (within custom tolerance)
561
+ payee_name: "Store",
562
+ category_name: "Shopping",
563
+ cleared: "uncleared",
564
+ approved: true,
565
+ },
566
+ ];
567
+
568
+ const matches = findMatches(bankTxns, ynabTxns, customConfig);
569
+
570
+ expect(matches[0].confidence).not.toBe("none");
571
+ });
572
+ });
573
+
574
+ describe("edge cases", () => {
575
+ it("should handle empty YNAB transactions list", () => {
576
+ const bankTxn: BankTransaction = {
577
+ id: "b1",
578
+ date: "2025-10-15",
579
+ amount: -50.0,
580
+ payee: "Store",
581
+ original_csv_row: 2,
582
+ };
583
+
584
+ const match = findBestMatch(bankTxn, [], new Set(), config);
585
+
586
+ expect(match.confidence).toBe("none");
587
+ expect(match.bestMatch).toBeNull();
588
+ });
589
+
590
+ it("should handle null payee names", () => {
591
+ const bankTxn: BankTransaction = {
592
+ id: "b1",
593
+ date: "2025-10-15",
594
+ amount: -50000,
595
+ payee: "Store",
596
+ original_csv_row: 2,
597
+ };
598
+
599
+ const ynabTxns: YNABTransaction[] = [
600
+ {
601
+ id: "y1",
602
+ date: "2025-10-15",
603
+ amount: -50000,
604
+ payee_name: null,
605
+ category_name: "Shopping",
606
+ cleared: "uncleared",
607
+ approved: true,
608
+ },
609
+ ];
610
+
611
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
612
+
613
+ // Should still match based on amount and date
614
+ expect(match.confidence).not.toBe("none");
615
+ });
616
+
617
+ it("should handle very small amounts", () => {
618
+ const bankTxn: BankTransaction = {
619
+ id: "b1",
620
+ date: "2025-10-15",
621
+ amount: -10, // 1 cent in milliunits
622
+ payee: "Micro Transaction",
623
+ original_csv_row: 2,
624
+ };
625
+
626
+ const ynabTxns: YNABTransaction[] = [
627
+ {
628
+ id: "y1",
629
+ date: "2025-10-15",
630
+ amount: -10, // 1 cent in milliunits
631
+ payee_name: "Micro Transaction",
632
+ category_name: "Misc",
633
+ cleared: "uncleared",
634
+ approved: true,
635
+ },
636
+ ];
637
+
638
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
639
+ expect(match.confidence).toBe("high");
640
+ });
641
+
642
+ it("should handle large amounts", () => {
643
+ const bankTxn: BankTransaction = {
644
+ id: "b1",
645
+ date: "2025-10-15",
646
+ amount: -10000000, // $10,000 in milliunits
647
+ payee: "Large Purchase",
648
+ original_csv_row: 2,
649
+ };
650
+
651
+ const ynabTxns: YNABTransaction[] = [
652
+ {
653
+ id: "y1",
654
+ date: "2025-10-15",
655
+ amount: -10000000, // $10,000 in milliunits
656
+ payee_name: "Large Purchase",
657
+ category_name: "Shopping",
658
+ cleared: "uncleared",
659
+ approved: true,
660
+ },
661
+ ];
662
+
663
+ const match = findBestMatch(bankTxn, ynabTxns, new Set(), config);
664
+
665
+ expect(match.confidence).toBe("high");
666
+ });
667
+ });
664
668
  });