From 69325b776ab0abd6873f5ff4d32b885d8186d46b Mon Sep 17 00:00:00 2001 From: Oana Lolea Date: Fri, 29 Nov 2024 12:44:33 +0200 Subject: [PATCH] Updated tests --- .../accounting/tigerbeetle/service.test.ts | 47 ++++++++++++++----- .../src/accounting/tigerbeetle/utils.ts | 14 +++++- .../accounting_transfer.tigerbeetle.test.ts | 4 +- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/accounting/tigerbeetle/service.test.ts b/packages/backend/src/accounting/tigerbeetle/service.test.ts index 2b437b0bad..686f77ea21 100644 --- a/packages/backend/src/accounting/tigerbeetle/service.test.ts +++ b/packages/backend/src/accounting/tigerbeetle/service.test.ts @@ -214,13 +214,7 @@ describe('TigerBeetle Accounting Service', (): void => { transferRef: '00000000-0000-0000-0000-000000000000' }) expect(transferCredit.timestamp).toBeGreaterThan(0) - - const toleranceInMs = 10 - const now = new Date().getTime() - const creditExpiresAt = transferCredit.expiresAt - ? transferCredit.expiresAt.getTime() - : now - expect(Math.abs(now - creditExpiresAt)).toBeLessThanOrEqual(toleranceInMs) + expect(transferCredit.expiresAt).toBeUndefined() const accTransfersDebit = await accountingService.getAccountTransfers(ledger) @@ -238,11 +232,42 @@ describe('TigerBeetle Accounting Service', (): void => { transferRef: '00000000-0000-0000-0000-000000000000' }) expect(transferDebit.timestamp).toBeGreaterThan(0) + expect(transferDebit.expiresAt).toBeUndefined() + }) - const debitExpiresAt = transferDebit.expiresAt - ? transferDebit.expiresAt.getTime() - : now - expect(Math.abs(now - debitExpiresAt)).toBeLessThanOrEqual(toleranceInMs) + test('Returns expiry date correctly', async (): Promise => { + const receivingAccount = await accountFactory.build() + const balances = [BigInt(100), BigInt(100)] + const accounts = await Promise.all( + balances.map(async (balance) => { + return await accountFactory.build({ + balance, + asset: receivingAccount.asset + }) + }) + ) + const timeout = 10 + await Promise.all( + accounts.map(async (account, i) => { + const transfer = await accountingService.createTransfer({ + sourceAccount: account, + sourceAmount: BigInt(10 * (i + 1)), + destinationAccount: receivingAccount, + timeout: timeout + }) + assert.ok(!isTransferError(transfer)) + await transfer.post() + }) + ) + const transfer = await accountingService.getAccountTransfers( + receivingAccount.id + ) + expect(transfer.credits).not.toHaveLength(0) + const timestamp = transfer.credits[0].timestamp + const expiresAtTime = transfer.credits[0].expiresAt?.getTime() + expect(expiresAtTime).toBe( + new Date(Number(timestamp) + timeout * 1000).getTime() + ) }) test('Returns undefined for nonexistent account', async (): Promise => { diff --git a/packages/backend/src/accounting/tigerbeetle/utils.ts b/packages/backend/src/accounting/tigerbeetle/utils.ts index 9191428bc0..fe75592f61 100644 --- a/packages/backend/src/accounting/tigerbeetle/utils.ts +++ b/packages/backend/src/accounting/tigerbeetle/utils.ts @@ -61,8 +61,18 @@ export function tbTransferToLedgerTransfer( type: transferTypeFromCode(tbTransfer.code), state: state, ledger: tbTransfer.ledger, - expiresAt: new Date( - Number(tbTransfer.timestamp / 1_000_000n) + tbTransfer.timeout * 1000 + expiresAt: expiresAtFromTimestampAndTimeout( + tbTransfer.timestamp, + tbTransfer.timeout ) } } + +function expiresAtFromTimestampAndTimeout( + timestamp: bigint, + timeout: number +): Date | undefined { + return timeout + ? new Date(Number(timestamp) / 1_000_000 + timeout * 1000) + : undefined +} diff --git a/packages/backend/src/graphql/resolvers/accounting_transfer.tigerbeetle.test.ts b/packages/backend/src/graphql/resolvers/accounting_transfer.tigerbeetle.test.ts index 0c4880829e..b6fe08cee7 100644 --- a/packages/backend/src/graphql/resolvers/accounting_transfer.tigerbeetle.test.ts +++ b/packages/backend/src/graphql/resolvers/accounting_transfer.tigerbeetle.test.ts @@ -136,7 +136,7 @@ describe('TigerBeetle: Accounting Transfer', (): void => { transferType: TransferType.Deposit, ledger, state: TransferState.Posted, - expiresAt: response.debits[0].createdAt + expiresAt: null }) // Credit: @@ -193,7 +193,7 @@ describe('TigerBeetle: Accounting Transfer', (): void => { transferType: TransferType.Deposit, ledger, state: TransferState.Posted, - expiresAt: response.credits[0].createdAt + expiresAt: null }) }) })