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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import { expect, test } from "bun:test";
import {
  transformAccount,
  transformAccountBalance,
  transformTransaction,
} from "./transform";

test("Transform expense transaction (depository)", () => {
  expect(
    transformTransaction({
      accountType: "depository",
      transaction: {
        transactionId:
          "8wwecA0PsWWCLPLazQzht2oOcxSIQ5UlWPHaNBpC3tjYtc002faJcjWzRpyO4sjz66kRpb7_5rA",
        entryReference: "5490990006",
        bookingDate: "2024-02-23",
        valueDate: "2024-02-23",
        transactionAmount: {
          amount: "-38000.00",
          currency: "SEK",
        },
        additionalInformation: "LÖN         160434262327",
        proprietaryBankTransactionCode: "Transfer",
        internalTransactionId: "86b1bc36e6a6d2a5dee8ff7138920255",
      },
    }),
  ).toMatchSnapshot();
});

test("Transform income transaction (depository)", () => {
  expect(
    transformTransaction({
      accountType: "depository",
      transaction: {
        transactionId: "income-test-123",
        bookingDate: "2024-02-23",
        transactionAmount: {
          amount: "5000.00",
          currency: "SEK",
        },
        additionalInformation: "Salary deposit",
        proprietaryBankTransactionCode: "Transfer",
        internalTransactionId: "income-internal-123",
      },
    }),
  ).toMatchSnapshot();
});

test("Transform credit card payment - should be credit-card-payment not income", () => {
  // When paying off a credit card, the amount is positive (money coming INTO the card)
  // This should NOT be categorized as income
  expect(
    transformTransaction({
      accountType: "credit",
      transaction: {
        transactionId: "cc-payment-123",
        bookingDate: "2024-02-23",
        transactionAmount: {
          amount: "500.00",
          currency: "SEK",
        },
        additionalInformation: "Credit card payment",
        proprietaryBankTransactionCode: "Transfer",
        internalTransactionId: "cc-payment-internal-123",
      },
    }),
  ).toMatchSnapshot();
});

test("Transform credit card refund - should be null (user categorizes)", () => {
  // A refund on a credit card also has positive amount but is not a payment
  expect(
    transformTransaction({
      accountType: "credit",
      transaction: {
        transactionId: "cc-refund-123",
        bookingDate: "2024-02-23",
        transactionAmount: {
          amount: "75.00",
          currency: "SEK",
        },
        creditorName: "Some Merchant",
        additionalInformation: "Refund",
        proprietaryBankTransactionCode: "Card purchase",
        internalTransactionId: "cc-refund-internal-123",
      },
    }),
  ).toMatchSnapshot();
});

test("Transform accounts", () => {
  // Get the transformed account
  const transformedAccount = transformAccount({
    id: "b11e5627-cac8-41c9-a74a-2b88438fe07d",
    created: "2024-02-23T13:29:47.314568Z",
    last_accessed: "2024-03-06T16:34:16.782598Z",
    iban: "3133",
    institution_id: "PLEO_PLEODK00",
    status: "READY",
    owner_name: "",
    account: {
      resourceId: "3133",
      currency: "SEK",
      name: "Pleo Account",
      product: "Pleo",
      cashAccountType: "TRAN",
      iban: "123",
      ownerName: "Name",
    },
    balance: {
      currency: "SEK",
      amount: "1942682.86",
    },
    institution: {
      id: "PLEO_PLEODK00",
      name: "Pleo",
      bic: "PLEODK00",
      transaction_total_days: "90",
      countries: ["DK", "GB", "DE", "SE", "ES", "IE", "DK"],
      logo: "https://cdn-logos.gocardless.com/ais/PLEO_PLEODK00.png",
    },
  });

  // Create a stable version of the account for snapshot testing
  // by replacing the dynamic expires_at date with a fixed string
  const stableAccount = {
    ...transformedAccount,
    expires_at: "FIXED_DATE_FOR_TESTING",
  };

  expect(stableAccount).toMatchSnapshot();
});

test("Transform credit card account (CARD type)", () => {
  const transformedAccount = transformAccount({
    id: "credit-card-account-123",
    created: "2024-02-23T13:29:47.314568Z",
    last_accessed: "2024-03-06T16:34:16.782598Z",
    iban: "4444",
    institution_id: "BANK_CREDIT",
    status: "READY",
    owner_name: "John Doe",
    account: {
      resourceId: "4444",
      currency: "EUR",
      name: "Credit Card",
      product: "Visa Gold",
      cashAccountType: "CARD",
      iban: "4444",
      ownerName: "John Doe",
    },
    balance: {
      currency: "EUR",
      amount: "-1500.00",
    },
    institution: {
      id: "BANK_CREDIT",
      name: "Test Bank",
      bic: "TESTBIC",
      transaction_total_days: "90",
      countries: ["DE"],
      logo: "https://example.com/logo.png",
    },
  });

  const stableAccount = {
    ...transformedAccount,
    expires_at: "FIXED_DATE_FOR_TESTING",
  };

  expect(stableAccount).toMatchSnapshot();
});

test("Transform account balance - depository (positive stays positive)", () => {
  expect(
    transformAccountBalance({
      balance: {
        currency: "SEK",
        amount: "1942682.86",
      },
      accountType: "depository",
    }),
  ).toMatchSnapshot();
});

test("Transform account balance - credit card with negative balance (normalized to positive)", () => {
  // GoCardless stores credit card debt as NEGATIVE (e.g., -1500 means $1500 owed)
  // We normalize to positive for consistency with other providers
  expect(
    transformAccountBalance({
      balance: {
        currency: "EUR",
        amount: "-1500.00",
      },
      accountType: "credit",
    }),
  ).toEqual({
    currency: "EUR",
    amount: 1500,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - credit card with positive balance (overpayment stays positive)", () => {
  // If someone overpays their credit card, the balance is positive (credit in their favor)
  // This should stay positive
  expect(
    transformAccountBalance({
      balance: {
        currency: "EUR",
        amount: "200.00",
      },
      accountType: "credit",
    }),
  ).toEqual({
    currency: "EUR",
    amount: 200,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - credit card with zero balance", () => {
  expect(
    transformAccountBalance({
      balance: {
        currency: "USD",
        amount: "0.00",
      },
      accountType: "credit",
    }),
  ).toEqual({
    currency: "USD",
    amount: 0,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - no accountType provided", () => {
  // When accountType is not provided, don't normalize (safety for backwards compatibility)
  expect(
    transformAccountBalance({
      balance: {
        currency: "EUR",
        amount: "-500.00",
      },
    }),
  ).toEqual({
    currency: "EUR",
    amount: -500,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - handles undefined balance gracefully", () => {
  expect(
    transformAccountBalance({
      balance: undefined,
      accountType: "depository",
    }),
  ).toEqual({
    currency: "EUR",
    amount: 0,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - very large credit card balance", () => {
  // Verify large balances are normalized correctly
  expect(
    transformAccountBalance({
      balance: {
        currency: "USD",
        amount: "-50000.00",
      },
      accountType: "credit",
    }),
  ).toEqual({
    currency: "USD",
    amount: 50000,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - decimal precision maintained", () => {
  expect(
    transformAccountBalance({
      balance: {
        currency: "EUR",
        amount: "-1234.56",
      },
      accountType: "credit",
    }),
  ).toEqual({
    currency: "EUR",
    amount: 1234.56,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - loan type keeps negative (if bank reports that way)", () => {
  // Note: GoCardless transform only normalizes credit cards, not loans
  // Loans are typically reported as positive by banks anyway
  // This test documents the current behavior
  expect(
    transformAccountBalance({
      balance: {
        currency: "GBP",
        amount: "-25000.00",
      },
      accountType: "loan",
    }),
  ).toEqual({
    currency: "GBP",
    amount: -25000, // Not normalized (only credit type is normalized)
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account balance - loan type with positive value", () => {
  // Most banks report loan balances as positive (amount owed)
  expect(
    transformAccountBalance({
      balance: {
        currency: "GBP",
        amount: "25000.00",
      },
      accountType: "loan",
    }),
  ).toEqual({
    currency: "GBP",
    amount: 25000,
    available_balance: null,
    credit_limit: null,
  });
});

test("Transform account - extracts available_balance from balances array", () => {
  const result = transformAccount({
    id: "gc_account_123",
    created: "2024-02-23T13:29:47.314568Z",
    last_accessed: "2024-03-06T16:34:16.782598Z",
    iban: "DE89370400440532013000",
    institution_id: "REVOLUT_REVOGB21",
    status: "READY",
    owner_name: "John Doe",
    account: {
      currency: "EUR",
      iban: "DE89370400440532013000",
      ownerName: "John Doe",
      name: "Main Account",
      product: "Current Account",
      cashAccountType: "CACC",
      resourceId: "resource_123",
    },
    balance: {
      currency: "EUR",
      amount: "5000.00",
    },
    balances: [
      {
        balanceType: "interimBooked",
        balanceAmount: { amount: "5000.00", currency: "EUR" },
        creditLimitIncluded: false,
      },
      {
        balanceType: "interimAvailable",
        balanceAmount: { amount: "4800.00", currency: "EUR" },
        creditLimitIncluded: false,
      },
    ],
    institution: {
      id: "REVOLUT_REVOGB21",
      name: "Revolut",
      bic: "REVOGB21",
      transaction_total_days: "90",
      countries: ["GB"],
      logo: "https://cdn.nordigen.com/revolut.png",
    },
  });

  expect(result.available_balance).toBe(4800);
  expect(result.credit_limit).toBeNull(); // GoCardless doesn't provide credit limit
});

test("Transform account - available_balance falls back to expected balance", () => {
  const result = transformAccount({
    id: "gc_account_456",
    created: "2024-02-23T13:29:47.314568Z",
    last_accessed: "2024-03-06T16:34:16.782598Z",
    iban: "DE89370400440532013001",
    institution_id: "REVOLUT_REVOGB21",
    status: "READY",
    owner_name: "Jane Doe",
    account: {
      currency: "EUR",
      iban: "DE89370400440532013001",
      ownerName: "Jane Doe",
      name: "Savings",
      product: "Savings Account",
      cashAccountType: "SVGS",
      resourceId: "resource_456",
    },
    balance: {
      currency: "EUR",
      amount: "10000.00",
    },
    balances: [
      {
        balanceType: "expected",
        balanceAmount: { amount: "10000.00", currency: "EUR" },
        creditLimitIncluded: false,
      },
    ],
    institution: {
      id: "REVOLUT_REVOGB21",
      name: "Revolut",
      bic: "REVOGB21",
      transaction_total_days: "90",
      countries: ["GB"],
      logo: "https://cdn.nordigen.com/revolut.png",
    },
  });

  expect(result.available_balance).toBe(10000);
});

test("Transform account - handles empty balances array", () => {
  const result = transformAccount({
    id: "gc_account_789",
    created: "2024-02-23T13:29:47.314568Z",
    last_accessed: "2024-03-06T16:34:16.782598Z",
    iban: "GB82WEST12345698765432",
    institution_id: "MONZO_MONZGB21",
    status: "READY",
    owner_name: "Test User",
    account: {
      currency: "GBP",
      iban: "GB82WEST12345698765432",
      ownerName: "Test User",
      name: "Current",
      product: "Current Account",
      cashAccountType: "CACC",
      resourceId: "resource_789",
    },
    balance: {
      currency: "GBP",
      amount: "1000.00",
    },
    balances: [],
    institution: {
      id: "MONZO_MONZGB21",
      name: "Monzo",
      bic: "MONZGB21",
      transaction_total_days: "90",
      countries: ["GB"],
      logo: "https://cdn.nordigen.com/monzo.png",
    },
  });

  expect(result.available_balance).toBeNull();
  expect(result.credit_limit).toBeNull();
});