Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oana-lolea committed Nov 29, 2024
1 parent 309718a commit 69325b7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
47 changes: 36 additions & 11 deletions packages/backend/src/accounting/tigerbeetle/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<void> => {
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<void> => {
Expand Down
14 changes: 12 additions & 2 deletions packages/backend/src/accounting/tigerbeetle/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('TigerBeetle: Accounting Transfer', (): void => {
transferType: TransferType.Deposit,
ledger,
state: TransferState.Posted,
expiresAt: response.debits[0].createdAt
expiresAt: null
})

// Credit:
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('TigerBeetle: Accounting Transfer', (): void => {
transferType: TransferType.Deposit,
ledger,
state: TransferState.Posted,
expiresAt: response.credits[0].createdAt
expiresAt: null
})
})
})

0 comments on commit 69325b7

Please sign in to comment.