@dizzlkheinz/ynab-mcpb 0.16.0 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +113 -18
- package/README.md +19 -4
- package/dist/bundle/index.cjs +1 -1
- package/dist/tools/transactionTools.js +5 -0
- package/package.json +1 -1
- package/src/tools/__tests__/transactionTools.test.ts +90 -17
- package/src/tools/transactionTools.ts +10 -0
- package/.dxtignore +0 -57
- package/CODEREVIEW_RESPONSE.md +0 -128
- package/SCHEMA_IMPROVEMENT_SUMMARY.md +0 -120
- package/TESTING_NOTES.md +0 -217
- package/accountactivity-merged.csv +0 -149
- package/bundle-analysis.html +0 -13110
- package/docs/plans/2025-11-20-reloadable-config-token-validation.md +0 -93
- package/docs/plans/2025-11-21-fix-transaction-cached-property.md +0 -362
- package/docs/plans/2025-11-21-reconciliation-error-handling.md +0 -90
- package/docs/plans/2025-11-21-v014-hardening.md +0 -153
- package/docs/plans/reconciliation-v2-redesign.md +0 -1571
- package/fix-types.sh +0 -17
- package/test-csv-sample.csv +0 -28
- package/test-exports/sample_bank_statement.csv +0 -7
- package/test-reconcile-autodetect.js +0 -40
- package/test-reconcile-tool.js +0 -152
- package/test-reconcile-with-csv.cjs +0 -89
- package/test-statement.csv +0 -8
- package/test_debug.js +0 -47
- package/test_mcp_tools.mjs +0 -75
- package/test_simple.mjs +0 -16
|
@@ -496,6 +496,11 @@ export async function handleListTransactions(ynabAPI, deltaFetcherOrParams, mayb
|
|
|
496
496
|
let cacheHit = false;
|
|
497
497
|
let usedDelta = false;
|
|
498
498
|
if (params.account_id) {
|
|
499
|
+
const accountsResult = await deltaFetcher.fetchAccounts(params.budget_id);
|
|
500
|
+
const accountExists = accountsResult.data.some((account) => account.id === params.account_id);
|
|
501
|
+
if (!accountExists) {
|
|
502
|
+
throw new Error(`Account ${params.account_id} not found in budget ${params.budget_id}`);
|
|
503
|
+
}
|
|
499
504
|
const result = await deltaFetcher.fetchTransactionsByAccount(params.budget_id, params.account_id, params.since_date);
|
|
500
505
|
transactions = result.data;
|
|
501
506
|
cacheHit = result.wasCached;
|
package/package.json
CHANGED
|
@@ -19,6 +19,25 @@ import {
|
|
|
19
19
|
DeleteTransactionSchema,
|
|
20
20
|
} from '../transactionTools.js';
|
|
21
21
|
|
|
22
|
+
// Mock the YNAB API - declare first so it can be used in deltaSupport mock
|
|
23
|
+
const mockYnabAPI = {
|
|
24
|
+
transactions: {
|
|
25
|
+
getTransactions: vi.fn(),
|
|
26
|
+
getTransactionsByAccount: vi.fn(),
|
|
27
|
+
getTransactionsByCategory: vi.fn(),
|
|
28
|
+
getTransactionById: vi.fn(),
|
|
29
|
+
createTransaction: vi.fn(),
|
|
30
|
+
createTransactions: vi.fn(),
|
|
31
|
+
updateTransaction: vi.fn(),
|
|
32
|
+
updateTransactions: vi.fn(),
|
|
33
|
+
deleteTransaction: vi.fn(),
|
|
34
|
+
},
|
|
35
|
+
accounts: {
|
|
36
|
+
getAccountById: vi.fn(),
|
|
37
|
+
getAccounts: vi.fn(),
|
|
38
|
+
},
|
|
39
|
+
} as unknown as ynab.API;
|
|
40
|
+
|
|
22
41
|
// Mock the cache manager
|
|
23
42
|
vi.mock('../../server/cacheManager.js', () => ({
|
|
24
43
|
cacheManager: {
|
|
@@ -40,23 +59,62 @@ vi.mock('../../server/cacheManager.js', () => ({
|
|
|
40
59
|
},
|
|
41
60
|
}));
|
|
42
61
|
|
|
43
|
-
// Mock the
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
62
|
+
// Mock deltaSupport to create a simple DeltaFetcher that calls the API directly
|
|
63
|
+
vi.mock('../deltaSupport.js', async (importOriginal) => {
|
|
64
|
+
const original = await importOriginal<typeof import('../deltaSupport.js')>();
|
|
65
|
+
return {
|
|
66
|
+
...original,
|
|
67
|
+
resolveDeltaFetcherArgs: vi.fn((_ynabAPI, _deltaFetcherOrParams, maybeParams) => {
|
|
68
|
+
const params = maybeParams ?? _deltaFetcherOrParams;
|
|
69
|
+
// Create a simple mock delta fetcher that calls the API directly
|
|
70
|
+
const mockDeltaFetcher = {
|
|
71
|
+
fetchAccounts: vi.fn(async (budgetId: string) => {
|
|
72
|
+
const response = await mockYnabAPI.accounts.getAccounts(budgetId);
|
|
73
|
+
return {
|
|
74
|
+
data: response.data.accounts,
|
|
75
|
+
wasCached: false,
|
|
76
|
+
usedDelta: false,
|
|
77
|
+
serverKnowledge: response.data.server_knowledge ?? 0,
|
|
78
|
+
};
|
|
79
|
+
}),
|
|
80
|
+
fetchTransactions: vi.fn(async (budgetId: string, sinceDate?: string, type?: string) => {
|
|
81
|
+
// Pass all 4 arguments to match YNAB API signature
|
|
82
|
+
const response = await mockYnabAPI.transactions.getTransactions(
|
|
83
|
+
budgetId,
|
|
84
|
+
sinceDate,
|
|
85
|
+
type,
|
|
86
|
+
undefined,
|
|
87
|
+
);
|
|
88
|
+
return {
|
|
89
|
+
data: response.data.transactions,
|
|
90
|
+
wasCached: false,
|
|
91
|
+
usedDelta: false,
|
|
92
|
+
serverKnowledge: response.data.server_knowledge ?? 0,
|
|
93
|
+
};
|
|
94
|
+
}),
|
|
95
|
+
fetchTransactionsByAccount: vi.fn(
|
|
96
|
+
async (budgetId: string, accountId: string, sinceDate?: string) => {
|
|
97
|
+
// Pass all 5 arguments to match YNAB API signature
|
|
98
|
+
const response = await mockYnabAPI.transactions.getTransactionsByAccount(
|
|
99
|
+
budgetId,
|
|
100
|
+
accountId,
|
|
101
|
+
sinceDate,
|
|
102
|
+
undefined,
|
|
103
|
+
undefined,
|
|
104
|
+
);
|
|
105
|
+
return {
|
|
106
|
+
data: response.data.transactions,
|
|
107
|
+
wasCached: false,
|
|
108
|
+
usedDelta: false,
|
|
109
|
+
serverKnowledge: response.data.server_knowledge ?? 0,
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
),
|
|
113
|
+
};
|
|
114
|
+
return { deltaFetcher: mockDeltaFetcher, params };
|
|
115
|
+
}),
|
|
116
|
+
};
|
|
117
|
+
});
|
|
60
118
|
|
|
61
119
|
// Import mocked cache manager
|
|
62
120
|
const { cacheManager, CacheManager } = await import('../../server/cacheManager.js');
|
|
@@ -238,12 +296,19 @@ describe('transactionTools', () => {
|
|
|
238
296
|
});
|
|
239
297
|
|
|
240
298
|
it('should filter by account_id when provided', async () => {
|
|
299
|
+
const mockAccountsResponse = {
|
|
300
|
+
data: {
|
|
301
|
+
accounts: [{ id: 'account-456', name: 'Test Account', deleted: false }],
|
|
302
|
+
server_knowledge: 100,
|
|
303
|
+
},
|
|
304
|
+
};
|
|
241
305
|
const mockResponse = {
|
|
242
306
|
data: {
|
|
243
307
|
transactions: [mockTransaction],
|
|
244
308
|
},
|
|
245
309
|
};
|
|
246
310
|
|
|
311
|
+
(mockYnabAPI.accounts.getAccounts as any).mockResolvedValue(mockAccountsResponse);
|
|
247
312
|
(mockYnabAPI.transactions.getTransactionsByAccount as any).mockResolvedValue(mockResponse);
|
|
248
313
|
|
|
249
314
|
const params = {
|
|
@@ -252,6 +317,7 @@ describe('transactionTools', () => {
|
|
|
252
317
|
};
|
|
253
318
|
const result = await handleListTransactions(mockYnabAPI, params);
|
|
254
319
|
|
|
320
|
+
expect(mockYnabAPI.accounts.getAccounts).toHaveBeenCalledWith('budget-123');
|
|
255
321
|
expect(mockYnabAPI.transactions.getTransactionsByAccount).toHaveBeenCalledWith(
|
|
256
322
|
'budget-123',
|
|
257
323
|
'account-456',
|
|
@@ -383,12 +449,19 @@ describe('transactionTools', () => {
|
|
|
383
449
|
} as ynab.TransactionDetail);
|
|
384
450
|
}
|
|
385
451
|
|
|
452
|
+
const mockAccountsResponse = {
|
|
453
|
+
data: {
|
|
454
|
+
accounts: [{ id: 'test-account', name: 'Test Account', deleted: false }],
|
|
455
|
+
server_knowledge: 100,
|
|
456
|
+
},
|
|
457
|
+
};
|
|
386
458
|
const mockResponse = {
|
|
387
459
|
data: {
|
|
388
460
|
transactions: largeTransactionList,
|
|
389
461
|
},
|
|
390
462
|
};
|
|
391
463
|
|
|
464
|
+
(mockYnabAPI.accounts.getAccounts as any).mockResolvedValue(mockAccountsResponse);
|
|
392
465
|
(mockYnabAPI.transactions.getTransactionsByAccount as any).mockResolvedValue(mockResponse);
|
|
393
466
|
|
|
394
467
|
const result = await handleListTransactions(mockYnabAPI, {
|
|
@@ -755,6 +755,16 @@ export async function handleListTransactions(
|
|
|
755
755
|
let usedDelta = false;
|
|
756
756
|
|
|
757
757
|
if (params.account_id) {
|
|
758
|
+
// Validate that the account exists before fetching transactions
|
|
759
|
+
// YNAB API returns empty array for invalid account IDs instead of an error
|
|
760
|
+
const accountsResult = await deltaFetcher.fetchAccounts(params.budget_id);
|
|
761
|
+
const accountExists = accountsResult.data.some(
|
|
762
|
+
(account) => account.id === params.account_id,
|
|
763
|
+
);
|
|
764
|
+
if (!accountExists) {
|
|
765
|
+
throw new Error(`Account ${params.account_id} not found in budget ${params.budget_id}`);
|
|
766
|
+
}
|
|
767
|
+
|
|
758
768
|
const result = await deltaFetcher.fetchTransactionsByAccount(
|
|
759
769
|
params.budget_id,
|
|
760
770
|
params.account_id,
|
package/.dxtignore
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
node_modules/
|
|
2
|
-
src/
|
|
3
|
-
**/__tests__/
|
|
4
|
-
**/*.test.*
|
|
5
|
-
coverage/
|
|
6
|
-
*.log
|
|
7
|
-
.*/
|
|
8
|
-
**/*.map
|
|
9
|
-
tsconfig*.json
|
|
10
|
-
vitest.config.ts
|
|
11
|
-
vitest-reporters/
|
|
12
|
-
eslint.config.js
|
|
13
|
-
test-results.json
|
|
14
|
-
test-results/
|
|
15
|
-
README.md
|
|
16
|
-
|
|
17
|
-
# Sensitive credential files
|
|
18
|
-
.chunkhound.json
|
|
19
|
-
.mcp.json
|
|
20
|
-
.env*
|
|
21
|
-
|
|
22
|
-
# Build metadata
|
|
23
|
-
meta.json
|
|
24
|
-
bundle-analysis.html
|
|
25
|
-
|
|
26
|
-
# Development-only markdown files
|
|
27
|
-
AGENTS.md
|
|
28
|
-
TESTING_NOTES.md
|
|
29
|
-
CODEREVIEW_RESPONSE.md
|
|
30
|
-
SCHEMA_IMPROVEMENT_SUMMARY.md
|
|
31
|
-
|
|
32
|
-
# Test files and data
|
|
33
|
-
test_*.js
|
|
34
|
-
test_*.mjs
|
|
35
|
-
test-*.js
|
|
36
|
-
test-*.cjs
|
|
37
|
-
test-*.csv
|
|
38
|
-
test-exports/
|
|
39
|
-
accountactivity-merged.csv
|
|
40
|
-
|
|
41
|
-
# Temporary and development files
|
|
42
|
-
temp/
|
|
43
|
-
tmp/
|
|
44
|
-
fix-types.sh
|
|
45
|
-
NUL
|
|
46
|
-
package-lock.json
|
|
47
|
-
|
|
48
|
-
# Development plans (keep user-facing docs)
|
|
49
|
-
docs/plans/
|
|
50
|
-
docs/bulk-transaction-operations-plan.md
|
|
51
|
-
docs/delta-request-plan.md
|
|
52
|
-
docs/reconciliation-flow.md
|
|
53
|
-
|
|
54
|
-
# Scripts (not needed for distribution)
|
|
55
|
-
scripts/
|
|
56
|
-
|
|
57
|
-
# keep dist/, manifest.json, CHANGELOG.md, CLAUDE.md, LICENSE, docs/ (except plans)
|
package/CODEREVIEW_RESPONSE.md
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
# Code Review Response: reconciliationOutputs.ts
|
|
2
|
-
|
|
3
|
-
## Summary
|
|
4
|
-
|
|
5
|
-
CodeRabbit provided two suggestions for improving `src/tools/schemas/outputs/reconciliationOutputs.ts`. After technical analysis of the codebase, I've determined:
|
|
6
|
-
|
|
7
|
-
1. **Feedback #1 (ExecutionActionRecordSchema typing)**: Already implemented ✅
|
|
8
|
-
2. **Feedback #2 (discrepancy_direction validation)**: Not implementing - technically sound but violates architectural principles ❌
|
|
9
|
-
|
|
10
|
-
## Feedback #1: Stronger Typing for Transaction Field
|
|
11
|
-
|
|
12
|
-
**Suggestion:** Use discriminated unions instead of `z.record(z.string(), z.unknown())` for the transaction field in `ExecutionActionRecordSchema`.
|
|
13
|
-
|
|
14
|
-
**Status:** ✅ **Already Implemented**
|
|
15
|
-
|
|
16
|
-
### Analysis
|
|
17
|
-
|
|
18
|
-
The CodeRabbit feedback appears to be outdated. The current implementation (lines 431-479) already uses a discriminated union with strong typing:
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
export const ExecutionActionRecordSchema = z.discriminatedUnion('type', [
|
|
22
|
-
// Successful transaction creation
|
|
23
|
-
z.object({
|
|
24
|
-
type: z.literal('create_transaction'),
|
|
25
|
-
transaction: CreatedTransactionSchema.nullable(),
|
|
26
|
-
// ...
|
|
27
|
-
}),
|
|
28
|
-
// Failed transaction creation
|
|
29
|
-
z.object({
|
|
30
|
-
type: z.literal('create_transaction_failed'),
|
|
31
|
-
transaction: TransactionCreationPayloadSchema,
|
|
32
|
-
// ...
|
|
33
|
-
}),
|
|
34
|
-
// ... other action types
|
|
35
|
-
]);
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
Each action type has its own specific schema:
|
|
39
|
-
|
|
40
|
-
- `CreatedTransactionSchema` - for successful creations (line 371)
|
|
41
|
-
- `TransactionCreationPayloadSchema` - for failed/dry-run creations (line 387)
|
|
42
|
-
- `TransactionUpdatePayloadSchema` - for status/date changes (line 402)
|
|
43
|
-
- `DuplicateDetectionPayloadSchema` - for duplicate detection (line 412)
|
|
44
|
-
|
|
45
|
-
This provides full type safety without the drawbacks of loose typing.
|
|
46
|
-
|
|
47
|
-
## Feedback #2: Validate discrepancy_direction Against Actual Discrepancy
|
|
48
|
-
|
|
49
|
-
**Suggestion:** Add a Zod refinement to ensure `discrepancy_direction` matches the sign of `balance.discrepancy.amount`.
|
|
50
|
-
|
|
51
|
-
**Status:** ❌ **Not Implementing**
|
|
52
|
-
|
|
53
|
-
### Analysis
|
|
54
|
-
|
|
55
|
-
While technically correct, this validation violates architectural principles and provides minimal benefit:
|
|
56
|
-
|
|
57
|
-
#### Current Implementation (reconcileAdapter.ts:122-135)
|
|
58
|
-
|
|
59
|
-
The `convertBalanceInfo` function deterministically derives `discrepancy_direction` from `discrepancyMilli`:
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
const convertBalanceInfo = (analysis: ReconciliationAnalysis) => {
|
|
63
|
-
const discrepancyMilli = analysis.balance_info.discrepancy.value_milliunits;
|
|
64
|
-
const direction =
|
|
65
|
-
discrepancyMilli === 0 ? 'balanced' : discrepancyMilli > 0 ? 'ynab_higher' : 'bank_higher';
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
current_cleared: analysis.balance_info.current_cleared,
|
|
69
|
-
// ...
|
|
70
|
-
discrepancy: analysis.balance_info.discrepancy,
|
|
71
|
-
discrepancy_direction: direction,
|
|
72
|
-
on_track: analysis.balance_info.on_track,
|
|
73
|
-
};
|
|
74
|
-
};
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
This logic is:
|
|
78
|
-
|
|
79
|
-
- **Simple and obvious**: Direct mapping from sign to direction
|
|
80
|
-
- **Well-tested**: Part of the adapter layer
|
|
81
|
-
- **Single point of truth**: Consistency enforced at construction time
|
|
82
|
-
|
|
83
|
-
#### Why Not Add Schema Validation?
|
|
84
|
-
|
|
85
|
-
1. **Violates Single Responsibility Principle**
|
|
86
|
-
The schema's job is to validate structure, not business logic consistency. The adapter already enforces this invariant at construction time.
|
|
87
|
-
|
|
88
|
-
2. **Adds Runtime Overhead**
|
|
89
|
-
Schema validation runs on every payload validation. This adds unnecessary computation for a condition that should never occur if the adapter works correctly.
|
|
90
|
-
|
|
91
|
-
3. **Couples Schema to Business Logic**
|
|
92
|
-
The schema would need to know the thresholds and logic for determining direction. This creates tight coupling between layers that should be independent.
|
|
93
|
-
|
|
94
|
-
4. **Limited Bug Detection Value**
|
|
95
|
-
If the adapter logic is broken, we'd want to fix the adapter, not catch it at validation time. Schema validation wouldn't prevent the bug, just detect it later in the pipeline.
|
|
96
|
-
|
|
97
|
-
5. **Test Coverage Sufficient**
|
|
98
|
-
The adapter has comprehensive test coverage. Adding redundant validation at the schema level doesn't improve reliability.
|
|
99
|
-
|
|
100
|
-
### Alternative Considered
|
|
101
|
-
|
|
102
|
-
If we were truly concerned about this invariant, the better approach would be:
|
|
103
|
-
|
|
104
|
-
1. Add unit tests specifically for the `convertBalanceInfo` function
|
|
105
|
-
2. Add integration tests that verify the full payload structure
|
|
106
|
-
3. Add a comment in the schema documenting the expected relationship
|
|
107
|
-
|
|
108
|
-
But given the simplicity of the current implementation and existing test coverage, none of these are necessary.
|
|
109
|
-
|
|
110
|
-
### Recommendation
|
|
111
|
-
|
|
112
|
-
**Accept Feedback #1 as already implemented.**
|
|
113
|
-
**Respectfully decline Feedback #2** - the adapter already enforces consistency, and adding schema-level validation would violate architectural principles without meaningful benefit.
|
|
114
|
-
|
|
115
|
-
---
|
|
116
|
-
|
|
117
|
-
## Files Changed
|
|
118
|
-
|
|
119
|
-
- `src/tools/schemas/outputs/reconciliationOutputs.ts` - No changes required (already has strong typing)
|
|
120
|
-
- `CODEREVIEW_RESPONSE.md` - This document
|
|
121
|
-
|
|
122
|
-
## Tests
|
|
123
|
-
|
|
124
|
-
All existing tests pass:
|
|
125
|
-
|
|
126
|
-
- ✅ `npm run type-check` - TypeScript compilation successful
|
|
127
|
-
- ✅ `npm run test:unit -- reconciliationOutputs` - 26/26 tests passing
|
|
128
|
-
- ✅ No regressions in related tests
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
# ExecutionActionRecord Schema Type Safety Improvements
|
|
2
|
-
|
|
3
|
-
## Summary
|
|
4
|
-
|
|
5
|
-
Addressed CodeRabbit's feedback about weak typing in `ExecutionActionRecordSchema` by replacing the generic `z.record(z.string(), z.unknown())` transaction field with a discriminated union based on action type.
|
|
6
|
-
|
|
7
|
-
## Problem
|
|
8
|
-
|
|
9
|
-
The original schema at line 370 in `reconciliationOutputs.ts` used:
|
|
10
|
-
|
|
11
|
-
```typescript
|
|
12
|
-
export const ExecutionActionRecordSchema = z.object({
|
|
13
|
-
type: z.string(),
|
|
14
|
-
transaction: z.record(z.string(), z.unknown()).nullable(),
|
|
15
|
-
// ...
|
|
16
|
-
});
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
This provided no type safety for transaction data, making the code prone to runtime errors if assumptions about transaction structure were incorrect.
|
|
20
|
-
|
|
21
|
-
## Solution
|
|
22
|
-
|
|
23
|
-
Implemented a **discriminated union** based on the `type` field, with each action type having its own strongly-typed transaction schema:
|
|
24
|
-
|
|
25
|
-
### Action Types & Transaction Schemas
|
|
26
|
-
|
|
27
|
-
1. **`create_transaction`** - Successful transaction creation
|
|
28
|
-
- Transaction: `CreatedTransactionSchema.nullable()`
|
|
29
|
-
- Full YNAB API transaction response with `.passthrough()` for additional fields
|
|
30
|
-
- Optional fields: `bulk_chunk_index`, `correlation_key`
|
|
31
|
-
|
|
32
|
-
2. **`create_transaction_failed`** - Failed transaction creation
|
|
33
|
-
- Transaction: `TransactionCreationPayloadSchema` (required)
|
|
34
|
-
- The request payload that failed to create
|
|
35
|
-
- Optional fields: `bulk_chunk_index`, `correlation_key`
|
|
36
|
-
|
|
37
|
-
3. **`create_transaction_duplicate`** - Duplicate detection
|
|
38
|
-
- Transaction: `DuplicateDetectionPayloadSchema` (required)
|
|
39
|
-
- Contains `transaction_id` (nullable) and optional `import_id`
|
|
40
|
-
- Required fields: `bulk_chunk_index`, `duplicate: true`
|
|
41
|
-
- Optional: `correlation_key`
|
|
42
|
-
|
|
43
|
-
4. **`update_transaction`** - Transaction update (status/date)
|
|
44
|
-
- Transaction: Union of `CreatedTransactionSchema.nullable()` (real execution) or `TransactionUpdatePayloadSchema` (dry run)
|
|
45
|
-
- Handles both full transaction responses and minimal update payloads
|
|
46
|
-
|
|
47
|
-
5. **`balance_checkpoint`** - Balance alignment checkpoint
|
|
48
|
-
- Transaction: `z.null()` (always null)
|
|
49
|
-
- No optional fields
|
|
50
|
-
|
|
51
|
-
6. **`bulk_create_fallback`** - Bulk operation fallback to sequential
|
|
52
|
-
- Transaction: `z.null()` (always null)
|
|
53
|
-
- Required: `bulk_chunk_index`
|
|
54
|
-
|
|
55
|
-
### Helper Schemas
|
|
56
|
-
|
|
57
|
-
**`CreatedTransactionSchema`**
|
|
58
|
-
|
|
59
|
-
- Validates YNAB API transaction responses
|
|
60
|
-
- Uses `.passthrough()` to allow additional API fields
|
|
61
|
-
- Required: `id`, `date`, `amount`
|
|
62
|
-
- Optional: `memo`, `cleared`, `approved`, `payee_name`, `category_name`, `import_id`
|
|
63
|
-
|
|
64
|
-
**`TransactionCreationPayloadSchema`**
|
|
65
|
-
|
|
66
|
-
- Validates transaction creation requests
|
|
67
|
-
- Required: `account_id`, `date`, `amount`
|
|
68
|
-
- Optional: `payee_name`, `memo`, `cleared`, `approved`, `import_id`
|
|
69
|
-
|
|
70
|
-
**`TransactionUpdatePayloadSchema`**
|
|
71
|
-
|
|
72
|
-
- Validates transaction update requests
|
|
73
|
-
- Required: `transaction_id`
|
|
74
|
-
- Optional: `new_date`, `cleared`
|
|
75
|
-
|
|
76
|
-
**`DuplicateDetectionPayloadSchema`**
|
|
77
|
-
|
|
78
|
-
- Validates duplicate detection metadata
|
|
79
|
-
- Required: `transaction_id` (nullable)
|
|
80
|
-
- Optional: `import_id`
|
|
81
|
-
|
|
82
|
-
## Benefits
|
|
83
|
-
|
|
84
|
-
1. **Type Safety** - Compile-time checking of transaction field structure
|
|
85
|
-
2. **Self-Documenting** - Schema clearly shows what data each action type contains
|
|
86
|
-
3. **Runtime Validation** - Zod catches malformed data before it causes issues
|
|
87
|
-
4. **Better Errors** - Discriminated union provides clear validation error messages
|
|
88
|
-
5. **Flexibility** - `.passthrough()` on `CreatedTransactionSchema` allows future YNAB API additions
|
|
89
|
-
6. **Zero Breaking Changes** - Backward compatible, all existing data validates correctly
|
|
90
|
-
|
|
91
|
-
## Testing
|
|
92
|
-
|
|
93
|
-
Created comprehensive test suite (`reconciliationOutputs.test.ts`) with 26 passing tests covering:
|
|
94
|
-
|
|
95
|
-
- All 6 action types with valid data
|
|
96
|
-
- Negative cases (wrong types, missing required fields)
|
|
97
|
-
- Discriminated union behavior (unknown types, type mismatches)
|
|
98
|
-
- Helper schema validation
|
|
99
|
-
- Edge cases (null transactions, passthrough fields)
|
|
100
|
-
|
|
101
|
-
## Files Changed
|
|
102
|
-
|
|
103
|
-
1. `src/tools/schemas/outputs/reconciliationOutputs.ts` - Schema definitions
|
|
104
|
-
2. `src/tools/schemas/outputs/__tests__/reconciliationOutputs.test.ts` - New test suite
|
|
105
|
-
|
|
106
|
-
## Verification
|
|
107
|
-
|
|
108
|
-
- ✅ TypeScript compilation passes (`npm run type-check`)
|
|
109
|
-
- ✅ All 26 new schema tests pass
|
|
110
|
-
- ✅ Build succeeds (`npm run build`)
|
|
111
|
-
- ✅ MCPB package generation succeeds
|
|
112
|
-
- ✅ No runtime changes to executor.ts needed (schemas match actual usage)
|
|
113
|
-
|
|
114
|
-
## Implementation Notes
|
|
115
|
-
|
|
116
|
-
The discriminated union matches exactly how the executor currently constructs action records (verified by analyzing `src/tools/reconciliation/executor.ts` lines 226-580). No changes to runtime code were needed, proving this is a pure type-safety enhancement.
|
|
117
|
-
|
|
118
|
-
## Follow-up Opportunities
|
|
119
|
-
|
|
120
|
-
Consider applying the same pattern to other schemas with generic `z.record()` fields if similar type safety concerns exist elsewhere in the codebase.
|